diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-08-14 17:29:56 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-08-14 17:29:56 +0900 |
commit | cdebfcf496dd65cff5d52667b625d49d1643bf16 (patch) | |
tree | d26b792da3d0e2ca63aedf9ee38d4374c0d8723e /lib/plum/hpack | |
parent | 6b0787d8ceb85f4eb6feeba091529e71c82dd07d (diff) | |
download | plum-cdebfcf496dd65cff5d52667b625d49d1643bf16.tar.gz |
hpack: force_encoding(Encoding::BINARY)
Diffstat (limited to 'lib/plum/hpack')
-rw-r--r-- | lib/plum/hpack/encoder.rb | 29 |
1 files changed, 16 insertions, 13 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 |