diff options
Diffstat (limited to 'lib/plum')
-rw-r--r-- | lib/plum/event_emitter.rb | 10 | ||||
-rw-r--r-- | lib/plum/frame_factory.rb | 17 | ||||
-rw-r--r-- | lib/plum/hpack/context.rb | 15 | ||||
-rw-r--r-- | lib/plum/hpack/encoder.rb | 4 | ||||
-rw-r--r-- | lib/plum/rack/listener.rb | 2 | ||||
-rw-r--r-- | lib/plum/rack/session.rb | 2 | ||||
-rw-r--r-- | lib/plum/stream_utils.rb | 2 |
7 files changed, 24 insertions, 28 deletions
diff --git a/lib/plum/event_emitter.rb b/lib/plum/event_emitter.rb index 7bc9695..e0684dd 100644 --- a/lib/plum/event_emitter.rb +++ b/lib/plum/event_emitter.rb @@ -5,18 +5,14 @@ module Plum # @param name [Symbol] The name of event. # @yield Gives event-specific parameters. def on(name, &blk) - (callbacks[name] ||= []) << blk + @callbacks ||= {} + (@callbacks[name] ||= []) << blk end # Invokes an event and call handlers with args. # @param name [Symbol] The identifier of event. def callback(name, *args) - (cbs = callbacks[name]) && cbs.each {|cb| cb.call(*args) } - end - - private - def callbacks - @callbacks ||= {} + @callbacks&.[](name)&.each { |cb| cb.call(*args) } end end end diff --git a/lib/plum/frame_factory.rb b/lib/plum/frame_factory.rb index de5a0b4..b4919e5 100644 --- a/lib/plum/frame_factory.rb +++ b/lib/plum/frame_factory.rb @@ -17,7 +17,7 @@ module Plum # @param message [String] Additional debug data. # @see RFC 7540 Section 6.8 def goaway(last_id, error_type, message = "") - payload = String.new.push_uint32((last_id || 0) | (0 << 31)) + payload = String.new.push_uint32(last_id) .push_uint32(HTTPError::ERROR_CODES[error_type]) .push(message) Frame.new(type: :goaway, stream_id: 0, payload: payload) @@ -55,10 +55,9 @@ module Plum # @param stream_id [Integer] The stream ID. # @param payload [String] Payload. # @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 - fval = 0 - fval += 1 if end_stream + def data(stream_id, payload = "", end_stream: false) + payload = payload.b if payload&.encoding != Encoding::BINARY + fval = end_stream ? 1 : 0 Frame.new(type_value: 0, stream_id: stream_id, flags_value: fval, payload: payload) end @@ -68,8 +67,7 @@ module Plum # @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 = end_stream ? 1 : 0 fval += 4 if end_headers Frame.new(type_value: 1, stream_id: stream_id, flags_value: fval, payload: encoded) end @@ -82,8 +80,7 @@ module Plum def push_promise(stream_id, new_id, encoded, end_headers: false) payload = String.new.push_uint32(new_id) .push(encoded) - fval = 0 - fval += 4 if end_headers + fval = end_headers ? 4 : 0 Frame.new(type: :push_promise, stream_id: stream_id, flags_value: fval, payload: payload) end @@ -92,7 +89,7 @@ module Plum # @param payload [String] 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) + 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/context.rb b/lib/plum/hpack/context.rb index 1d7f7d6..86dc244 100644 --- a/lib/plum/hpack/context.rb +++ b/lib/plum/hpack/context.rb @@ -36,18 +36,21 @@ module Plum end def search(name, value) - pr = proc { |n, v| - n == name && (!value || v == value) - } + si = STATIC_TABLE.index { |n, v| n == name && v == value } + return si + 1 if si + di = @dynamic_table.index { |n, v| n == name && v == value } + return di + STATIC_TABLE_SIZE + 1 if di + end - si = STATIC_TABLE.index &pr + def search_half(name) + si = STATIC_TABLE.index { |n, v| n == name } return si + 1 if si - di = @dynamic_table.index &pr + di = @dynamic_table.index { |n, v| n == name } return di + STATIC_TABLE_SIZE + 1 if di end def evict - while @limit && @size > @limit + while @size > @limit name, value = @dynamic_table.pop @size -= name.bytesize + value.bytesize + 32 end diff --git a/lib/plum/hpack/encoder.rb b/lib/plum/hpack/encoder.rb index d817589..2653c5b 100644 --- a/lib/plum/hpack/encoder.rb +++ b/lib/plum/hpack/encoder.rb @@ -12,13 +12,13 @@ module Plum @huffman = huffman end def encode(headers) - out = String.new.force_encoding(Encoding::BINARY) + out = "".b headers.each do |name, value| name = name.to_s value = value.to_s if index = search(name, value) out << encode_indexed(index) - elsif index = search(name, nil) + elsif index = search_half(name) out << encode_half_indexed(index, value) else out << encode_literal(name, value) diff --git a/lib/plum/rack/listener.rb b/lib/plum/rack/listener.rb index 4b8fd45..bc29e5b 100644 --- a/lib/plum/rack/listener.rb +++ b/lib/plum/rack/listener.rb @@ -46,7 +46,7 @@ module Plum *ctx.extra_chain_cert, ctx.cert = parse_chained_cert(cert) ctx.key = OpenSSL::PKey::RSA.new(key) ctx.servername_cb = proc { |sock, hostname| - if lc[:sni] && (host = lc[:sni][hostname]) + if host = lc[:sni]&.[](hostname) new_ctx = ctx.dup *new_ctx.extra_chain_cert, new_ctx.cert = parse_chained_cert(File.read(host[:certificate])) new_ctx.key = OpenSSL::PKey::RSA.new(File.read(host[:certificate_key])) diff --git a/lib/plum/rack/session.rb b/lib/plum/rack/session.rb index 24830d3..151a2f0 100644 --- a/lib/plum/rack/session.rb +++ b/lib/plum/rack/session.rb @@ -83,7 +83,7 @@ module Plum end else body.each { |part| stream.send_data(part, end_stream: false) } - stream.send_data(nil, end_stream: true) + stream.send_data(end_stream: true) end ensure body.close if body.respond_to?(:close) diff --git a/lib/plum/stream_utils.rb b/lib/plum/stream_utils.rb index dbb8d96..5fd6cc2 100644 --- a/lib/plum/stream_utils.rb +++ b/lib/plum/stream_utils.rb @@ -28,7 +28,7 @@ module Plum # Sends DATA frame. If the data is larger than MAX_FRAME_SIZE, DATA frame will be splitted. # @param data [String, IO] The data to send. # @param end_stream [Boolean] Set END_STREAM flag or not. - def send_data(data, end_stream: true) + def send_data(data = "", end_stream: true) max = @connection.remote_settings[:max_frame_size] if data.is_a?(IO) until data.eof? |