diff options
-rw-r--r-- | lib/plum/hpack/encoder.rb | 29 | ||||
-rw-r--r-- | test/plum/stream/test_handle_frame.rb | 2 |
2 files changed, 17 insertions, 14 deletions
diff --git a/lib/plum/hpack/encoder.rb b/lib/plum/hpack/encoder.rb index 58d13be..11e4485 100644 --- a/lib/plum/hpack/encoder.rb +++ b/lib/plum/hpack/encoder.rb @@ -5,20 +5,22 @@ module Plum class Encoder include HPACK::Context - def initialize(dynamic_table_limit) - super + def initialize(dynamic_table_limit, indexing: true) + super(dynamic_table_limit) + @indexing = indexing end def encode(headers) out = "" headers.each do |name, value| - name = name.to_s; value = value.to_s + name = name.to_s.force_encoding(Encoding::BINARY) + value = value.to_s.force_encoding(Encoding::BINARY) if index = search(name, value) out << encode_indexed(index) elsif index = search(name, nil) - out << encode_half_indexed(index, value, true) # incremental indexing + out << encode_half_indexed(index, value) else - out << encode_literal(name, value, true) # incremental indexing + out << encode_literal(name, value) end end out.force_encoding(Encoding::BINARY) @@ -36,14 +38,14 @@ module Plum # +---+---------------------------+ # | Value String (Length octets) | # +-------------------------------+ - def encode_literal(name, value, indexing = true) - if indexing + def encode_literal(name, value) + if @indexing store(name, value) fb = "\x40" else fb = "\x00" end - fb << encode_string(name) << encode_string(value) + fb.force_encoding(Encoding::BINARY) << encode_string(name) << encode_string(value) end # +---+---+---+---+---+---+---+---+ @@ -53,8 +55,8 @@ module Plum # +---+---------------------------+ # | Value String (Length octets) | # +-------------------------------+ - def encode_half_indexed(index, value, indexing = true) - if indexing + def encode_half_indexed(index, value) + if @indexing store(fetch(index)[0], value) fb = encode_integer(index, 6) fb.setbyte(0, fb.uint8 | 0b01000000) @@ -88,16 +90,17 @@ module Plum end out.push_uint8(value) end + out.force_encoding(Encoding::BINARY) end def encode_string(str) - huffman_str = Huffman.encode(str).force_encoding(__ENCODING__) + huffman_str = Huffman.encode(str) if huffman_str.bytesize < str.bytesize lenstr = encode_integer(huffman_str.bytesize, 7) lenstr.setbyte(0, lenstr.uint8(0) | 0b10000000) - lenstr << huffman_str + lenstr.force_encoding(Encoding::BINARY) << huffman_str else - encode_integer(str.bytesize, 7) << str + encode_integer(str.bytesize, 7) << str.force_encoding(Encoding::BINARY) end end end diff --git a/test/plum/stream/test_handle_frame.rb b/test/plum/stream/test_handle_frame.rb index dce496f..ee9a394 100644 --- a/test/plum/stream/test_handle_frame.rb +++ b/test/plum/stream/test_handle_frame.rb @@ -160,7 +160,7 @@ class StreamHandleFrameTest < Minitest::Test header_block = HPACK::Encoder.new(0).encode([[":path", "/"]]) payload = "".push_uint32((1 << 31) | parent.id) .push_uint8(50) - .push(header_block) + .push(header_block) stream.receive_frame(Frame.new(type: :headers, stream_id: stream.id, flags: [:end_headers, :priority], |