From dc0e97de3cda4e104e48ecaaba9181b7d4906d9d Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Wed, 11 Nov 2015 18:48:31 +0900 Subject: frame_factory: specify flags by kwargs --- lib/plum/frame_factory.rb | 32 ++++++++++++++++++++------------ lib/plum/hpack/constants.rb | 2 ++ lib/plum/hpack/context.rb | 8 ++++---- lib/plum/server/http_connection.rb | 4 ++-- lib/plum/stream_utils.rb | 9 ++++----- 5 files changed, 32 insertions(+), 23 deletions(-) (limited to 'lib') diff --git a/lib/plum/frame_factory.rb b/lib/plum/frame_factory.rb index b4c1c01..de5a0b4 100644 --- a/lib/plum/frame_factory.rb +++ b/lib/plum/frame_factory.rb @@ -54,37 +54,45 @@ module Plum # Creates a DATA frame. # @param stream_id [Integer] The stream ID. # @param payload [String] Payload. - # @param flags [Array] Flags. - def data(stream_id, payload, *flags) + # @param end_stream [Boolean] add END_STREAM flag + def data(stream_id, payload, end_stream: false) payload = payload.b if payload && payload.encoding != Encoding::BINARY - Frame.new(type: :data, stream_id: stream_id, flags: flags, payload: payload) + fval = 0 + fval += 1 if end_stream + Frame.new(type_value: 0, stream_id: stream_id, flags_value: fval, payload: payload) end # Creates a HEADERS frame. # @param stream_id [Integer] The stream ID. # @param encoded [String] Headers. - # @param flags [Array] Flags. - def headers(stream_id, encoded, *flags) - Frame.new(type: :headers, stream_id: stream_id, flags: flags, payload: encoded) + # @param end_stream [Boolean] add END_STREAM flag + # @param end_headers [Boolean] add END_HEADERS flag + def headers(stream_id, encoded, end_stream: false, end_headers: false) + fval = 0 + fval += 1 if end_stream + fval += 4 if end_headers + Frame.new(type_value: 1, stream_id: stream_id, flags_value: fval, payload: encoded) end # Creates a PUSH_PROMISE frame. # @param stream_id [Integer] The stream ID. # @param new_id [Integer] The stream ID to create. # @param encoded [String] Request headers. - # @param flags [Array] Flags. - def push_promise(stream_id, new_id, encoded, *flags) + # @param end_headers [Boolean] add END_HEADERS flag + def push_promise(stream_id, new_id, encoded, end_headers: false) payload = String.new.push_uint32(new_id) .push(encoded) - Frame.new(type: :push_promise, stream_id: stream_id, flags: flags, payload: payload) + fval = 0 + fval += 4 if end_headers + Frame.new(type: :push_promise, stream_id: stream_id, flags_value: fval, payload: payload) end # Creates a CONTINUATION frame. # @param stream_id [Integer] The stream ID. # @param payload [String] Payload. - # @param flags [Array] Flags. - def continuation(stream_id, payload, *flags) - Frame.new(type: :continuation, stream_id: stream_id, flags: flags, payload: payload) + # @param end_headers [Boolean] add END_HEADERS flag + def continuation(stream_id, payload, end_headers: false) + Frame.new(type: :continuation, stream_id: stream_id, flags_value: (end_headers && 4 || 0), payload: payload) end end end diff --git a/lib/plum/hpack/constants.rb b/lib/plum/hpack/constants.rb index 4eac05c..6993735 100644 --- a/lib/plum/hpack/constants.rb +++ b/lib/plum/hpack/constants.rb @@ -67,6 +67,8 @@ module Plum ["www-authenticate", ""], ].freeze + STATIC_TABLE_SIZE = STATIC_TABLE.size + HUFFMAN_TABLE = [ "1111111111000", "11111111111111111011000", diff --git a/lib/plum/hpack/context.rb b/lib/plum/hpack/context.rb index 622fe36..1d7f7d6 100644 --- a/lib/plum/hpack/context.rb +++ b/lib/plum/hpack/context.rb @@ -26,10 +26,10 @@ module Plum def fetch(index) if index == 0 raise HPACKError.new("index can't be 0") - elsif index <= STATIC_TABLE.size - STATIC_TABLE[index - 1] + elsif index <= STATIC_TABLE_SIZE + STATIC_TABLE[index - 1] elsif index <= STATIC_TABLE.size + @dynamic_table.size - @dynamic_table[index - STATIC_TABLE.size - 1] + @dynamic_table[index - STATIC_TABLE_SIZE - 1] else raise HPACKError.new("invalid index: #{index}") end @@ -43,7 +43,7 @@ module Plum si = STATIC_TABLE.index &pr return si + 1 if si di = @dynamic_table.index &pr - return di + STATIC_TABLE.size + 1 if di + return di + STATIC_TABLE_SIZE + 1 if di end def evict diff --git a/lib/plum/server/http_connection.rb b/lib/plum/server/http_connection.rb index 116fdcb..8f91ca4 100644 --- a/lib/plum/server/http_connection.rb +++ b/lib/plum/server/http_connection.rb @@ -77,8 +77,8 @@ module Plum ":authority" => @_headers["host"] }) .reject {|n, v| ["connection", "http2-settings", "upgrade", "host"].include?(n) } - stream.receive_frame Frame.headers(1, encoder.encode(headers), :end_headers) - stream.receive_frame Frame.data(1, @_body, :end_stream) + stream.receive_frame Frame.headers(1, encoder.encode(headers), end_headers: true) + stream.receive_frame Frame.data(1, @_body, end_stream: true) end end end diff --git a/lib/plum/stream_utils.rb b/lib/plum/stream_utils.rb index ff14c7a..dbb8d96 100644 --- a/lib/plum/stream_utils.rb +++ b/lib/plum/stream_utils.rb @@ -9,7 +9,7 @@ module Plum def promise(headers) stream = @connection.reserve_stream(weight: self.weight + 1, parent: self) encoded = @connection.hpack_encoder.encode(headers) - frame = Frame.push_promise(id, stream.id, encoded, :end_headers) + frame = Frame.push_promise(id, stream.id, encoded, end_headers: true) send frame stream end @@ -20,7 +20,7 @@ module Plum def send_headers(headers, end_stream:) max = @connection.remote_settings[:max_frame_size] encoded = @connection.hpack_encoder.encode(headers) - frame = Frame.headers(id, encoded, :end_headers, (end_stream && :end_stream || nil)) + frame = Frame.headers(id, encoded, end_headers: true, end_stream: end_stream) send frame @state = :half_closed_local if end_stream end @@ -33,11 +33,10 @@ module Plum if data.is_a?(IO) until data.eof? fragment = data.readpartial(max) - send Frame.data(id, fragment, (end_stream && data.eof? && :end_stream)) + send Frame.data(id, fragment, end_stream: end_stream && data.eof?) end else - frame = Frame.data(id, data, (end_stream && :end_stream)) - send frame + send Frame.data(id, data, end_stream: end_stream) end @state = :half_closed_local if end_stream end -- cgit v1.2.3