aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-08-13 10:41:15 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-08-13 10:41:15 +0900
commit6af5416b16bdc2693fbf3a6f0686a136050ee52a (patch)
tree4f2352cd5f22a92d6feae12fbbeefc5da5d59f05
parentf54124648d55344bdbc5d42ba62f427d8dbe922b (diff)
downloadplum-6af5416b16bdc2693fbf3a6f0686a136050ee52a.tar.gz
hpack: encoder: use Huffman encoding if it use fewer bytes
-rw-r--r--lib/plum/hpack/encoder.rb19
1 files changed, 13 insertions, 6 deletions
diff --git a/lib/plum/hpack/encoder.rb b/lib/plum/hpack/encoder.rb
index a6c4fcf..551ec58 100644
--- a/lib/plum/hpack/encoder.rb
+++ b/lib/plum/hpack/encoder.rb
@@ -25,12 +25,9 @@ module Plum
def encode(headers)
out = ""
headers.each do |name, value|
- name = name.to_s; value = value.to_s
out << "\x00"
- out << encode_integer(name.bytesize, 7)
- out << name
- out << encode_integer(value.bytesize, 7)
- out << value
+ out << encode_string(name.to_s)
+ out << encode_string(value.to_s)
end
out
end
@@ -43,7 +40,6 @@ module Plum
if value < mask
out.push_uint8(value)
else
- bytes = [mask]
value -= mask
out.push_uint8(mask)
while value >= mask
@@ -53,6 +49,17 @@ module Plum
out.push_uint8(value)
end
end
+
+ def encode_string(str)
+ huffman_str = Huffman.encode(str)
+ if huffman_str.bytesize < str.bytesize
+ lenstr = encode_integer(huffman_str.bytesize, 7).force_encoding(Encoding::BINARY)
+ lenstr.setbyte(0, lenstr.uint8(0) | 0b10000000)
+ lenstr << huffman_str
+ else
+ encode_integer(str.bytesize, 7) << str
+ end
+ end
end
end
end