aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/hpack/encoder.rb
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-07-16 17:24:46 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-07-16 17:24:46 +0900
commitec517cc134e69a73f2d87c9584e7ed52a3204560 (patch)
tree8edd8d1c9894ad6a0d0b1eba529f752c11032b0d /lib/plum/hpack/encoder.rb
parente71a90e7b0048f650a1ca9ae304ad9b61ce99a47 (diff)
downloadplum-ec517cc134e69a73f2d87c9584e7ed52a3204560.tar.gz
hpack: implement HPACK encoding (only "Literal Header Field without Indexing")
Diffstat (limited to 'lib/plum/hpack/encoder.rb')
-rw-r--r--lib/plum/hpack/encoder.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/plum/hpack/encoder.rb b/lib/plum/hpack/encoder.rb
new file mode 100644
index 0000000..4a04eb7
--- /dev/null
+++ b/lib/plum/hpack/encoder.rb
@@ -0,0 +1,57 @@
+module Plum
+ module HPACK
+ class Encoder
+ attr_reader :context
+
+ def initialize(dynamic_table_limit)
+ @context = Context.new(dynamic_table_limit)
+ end
+
+ # currently only support 0x0000XXXX type (without indexing)
+ # and not huffman
+ # +---+---+---+---+---+---+---+---+
+ # | 0 | 0 | 0 | 0 | 0 |
+ # +---+---+-----------------------+
+ # | H | Name Length (7+) |
+ # +---+---------------------------+
+ # | Name String (Length octets) |
+ # +---+---------------------------+
+ # | H | Value Length (7+) |
+ # +---+---------------------------+
+ # | Value String (Length octets) |
+ # +-------------------------------+
+ def encode(headers)
+ out = "".b
+ 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
+ end
+
+ out
+ end
+
+ private
+ def encode_integer(value, prefix_length)
+ mask = (1 << prefix_length) - 1
+
+ if value < mask
+ [value].pack("C").b
+ else
+ bytes = [mask]
+ value -= mask
+ while value >= mask
+ bytes << (value % 0b10000000) + 0b10000000
+ value >>= 7
+ end
+ bytes << value
+
+ bytes.pack("C*").b
+ end
+ end
+ end
+ end
+end