diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-07-18 00:32:49 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-07-18 00:32:49 +0900 |
commit | 07c9bf9c672ac8ac7d86d8cb7814226233133f08 (patch) | |
tree | 99670e7217910c77073adc09b94f3f3f45cdbb23 /lib/plum/hpack | |
parent | 975794acebcbbda6a324f1118cfff6831b82eae2 (diff) | |
download | plum-07c9bf9c672ac8ac7d86d8cb7814226233133f08.tar.gz |
introduce BinaryString
Diffstat (limited to 'lib/plum/hpack')
-rw-r--r-- | lib/plum/hpack/decoder.rb | 16 | ||||
-rw-r--r-- | lib/plum/hpack/encoder.rb | 13 | ||||
-rw-r--r-- | lib/plum/hpack/huffman.rb | 4 |
3 files changed, 16 insertions, 17 deletions
diff --git a/lib/plum/hpack/decoder.rb b/lib/plum/hpack/decoder.rb index e52ecb2..cfa3bac 100644 --- a/lib/plum/hpack/decoder.rb +++ b/lib/plum/hpack/decoder.rb @@ -19,12 +19,12 @@ module Plum private def read_integer!(str, prefix_length) mask = (1 << prefix_length) - 1 - i = str.slice!(0, 1).ord & mask + i = str.uint8! & mask if i == mask m = 0 begin - next_value = str.slice!(0, 1).ord + next_value = str.uint8! i += (next_value & ~(0b10000000)) << m m += 7 end until next_value & 0b10000000 == 0 @@ -34,13 +34,13 @@ module Plum end def read_string!(str, length, huffman) - bin = str.slice!(0, length) + bin = str.shift(length) bin = Huffman.decode(bin) if huffman bin end def parse!(str) - first_byte = str[0].ord + first_byte = str.uint8 if first_byte & 0b10000000 == 0b10000000 # indexed # +---+---+---+---+---+---+---+---+ @@ -74,14 +74,14 @@ module Plum # +-------------------------------+ index = read_integer!(str, 6) if index == 0 - hname = (str[0].ord >> 7) == 1 + hname = (str.uint8 >> 7) == 1 lname = read_integer!(str, 7) name = read_string!(str, lname, hname) else name, = fetch(index) end - hval = (str[0].ord >> 7) == 1 + hval = (str.uint8 >> 7) == 1 lval = read_integer!(str, 7) val = read_string!(str, lval, hval) store(name, val) @@ -110,14 +110,14 @@ module Plum # +-------------------------------+ index = read_integer!(str, 4) if index == 0 - hname = (str[0].ord >> 7) == 1 + hname = (str.uint8 >> 7) == 1 lname = read_integer!(str, 7) name = read_string!(str, lname, hname) else name, = fetch(index) end - hval = (str[0].ord >> 7) == 1 + hval = (str.uint8 >> 7) == 1 lval = read_integer!(str, 7) val = read_string!(str, lval, hval) diff --git a/lib/plum/hpack/encoder.rb b/lib/plum/hpack/encoder.rb index 3f7e43f..46f2001 100644 --- a/lib/plum/hpack/encoder.rb +++ b/lib/plum/hpack/encoder.rb @@ -21,7 +21,7 @@ module Plum # | Value String (Length octets) | # +-------------------------------+ def encode(headers) - out = "".b + out = BinaryString.new headers.each do |name, value| name = name.to_s; value = value.to_s out << "\x00" @@ -30,26 +30,25 @@ module Plum out << encode_integer(value.bytesize, 7) out << value end - out end private def encode_integer(value, prefix_length) mask = (1 << prefix_length) - 1 + out = BinaryString.new if value < mask - [value].pack("C").b + out.push_uint8(value) else bytes = [mask] value -= mask + out.push_uint8(mask) while value >= mask - bytes << (value % 0b10000000) + 0b10000000 + out.push_uint8((value % 0b10000000) + 0b10000000) value >>= 7 end - bytes << value - - bytes.pack("C*").b + out.push_uint8(value) end end end diff --git a/lib/plum/hpack/huffman.rb b/lib/plum/hpack/huffman.rb index a3f5ce3..9e88708 100644 --- a/lib/plum/hpack/huffman.rb +++ b/lib/plum/hpack/huffman.rb @@ -10,7 +10,7 @@ module Plum out << HUFFMAN_TABLE[b] end out << "1" * (8 - (out.size % 8)) - [out].pack("B*").b + BinaryString.new([out].pack("B*")) end # TODO: performance @@ -30,7 +30,7 @@ module Plum elsif buf != "1" * buf.size raise HPACKError.new("huffman: unknown suffix: #{buf}") else - outl.pack("C*").b + BinaryString.new(outl.pack("C*")) end end end |