http-2 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.rubocop.yml +18 -0
- data/.rubocop_todo.yml +46 -0
- data/.travis.yml +10 -2
- data/Gemfile +7 -5
- data/README.md +35 -37
- data/Rakefile +9 -10
- data/example/README.md +0 -13
- data/example/client.rb +12 -15
- data/example/helper.rb +2 -2
- data/example/server.rb +19 -19
- data/example/upgrade_server.rb +191 -0
- data/http-2.gemspec +10 -9
- data/lib/http/2.rb +13 -13
- data/lib/http/2/buffer.rb +6 -10
- data/lib/http/2/client.rb +5 -10
- data/lib/http/2/compressor.rb +134 -146
- data/lib/http/2/connection.rb +104 -100
- data/lib/http/2/emitter.rb +2 -4
- data/lib/http/2/error.rb +7 -7
- data/lib/http/2/flow_buffer.rb +11 -10
- data/lib/http/2/framer.rb +78 -87
- data/lib/http/2/huffman.rb +265 -274
- data/lib/http/2/huffman_statemachine.rb +257 -257
- data/lib/http/2/server.rb +81 -6
- data/lib/http/2/stream.rb +195 -130
- data/lib/http/2/version.rb +1 -1
- data/lib/tasks/generate_huffman_table.rb +30 -24
- data/spec/buffer_spec.rb +11 -13
- data/spec/client_spec.rb +41 -42
- data/spec/compressor_spec.rb +243 -242
- data/spec/connection_spec.rb +252 -248
- data/spec/emitter_spec.rb +12 -12
- data/spec/framer_spec.rb +177 -179
- data/spec/helper.rb +56 -57
- data/spec/huffman_spec.rb +33 -33
- data/spec/server_spec.rb +15 -15
- data/spec/stream_spec.rb +356 -265
- metadata +7 -6
- data/spec/hpack_test_spec.rb +0 -83
data/lib/http/2/huffman.rb
CHANGED
@@ -1,24 +1,17 @@
|
|
1
1
|
require_relative 'error'
|
2
2
|
|
3
3
|
module HTTP2
|
4
|
-
|
5
4
|
# Implementation of huffman encoding for HPACK
|
6
5
|
#
|
7
|
-
# - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-
|
6
|
+
# - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-10
|
8
7
|
module Header
|
9
|
-
|
10
8
|
# Huffman encoder/decoder
|
11
9
|
class Huffman
|
12
10
|
include Error
|
13
11
|
|
14
12
|
BITS_AT_ONCE = 4
|
15
|
-
BINARY = "binary"
|
16
13
|
EOS = 256
|
17
|
-
private_constant :
|
18
|
-
|
19
|
-
def initialize
|
20
|
-
@@encode_table ||= CODES.map{|c,l| [c].pack("N").unpack("B*").first[-l..-1]}
|
21
|
-
end
|
14
|
+
private_constant :EOS
|
22
15
|
|
23
16
|
# Encodes provided value via huffman encoding.
|
24
17
|
# Length is not encoded in this method.
|
@@ -26,9 +19,9 @@ module HTTP2
|
|
26
19
|
# @param str [String]
|
27
20
|
# @return [String] binary string
|
28
21
|
def encode(str)
|
29
|
-
bitstring = str.each_byte.map{|chr|
|
30
|
-
bitstring <<
|
31
|
-
[bitstring].pack(
|
22
|
+
bitstring = str.each_byte.map { |chr| ENCODE_TABLE[chr] }.join
|
23
|
+
bitstring << '1' * ((8 - bitstring.size) % 8)
|
24
|
+
[bitstring].pack('B*')
|
32
25
|
end
|
33
26
|
|
34
27
|
# Decodes provided Huffman coded string.
|
@@ -50,283 +43,281 @@ module HTTP2
|
|
50
43
|
# [emit] character to be emitted on this transition, empty string, or EOS.
|
51
44
|
# [next] next state number.
|
52
45
|
trans = MACHINE[state][branch]
|
53
|
-
trans.first == EOS
|
54
|
-
|
55
|
-
trans.first && emit << trans.first
|
46
|
+
fail CompressionError, 'Huffman decode error (EOS found)' if trans.first == EOS
|
47
|
+
emit << trans.first.chr if trans.first
|
56
48
|
state = trans.last
|
57
49
|
end
|
58
50
|
end
|
59
51
|
# Check whether partial input is correctly filled
|
60
52
|
unless state <= MAX_FINAL_STATE
|
61
|
-
|
53
|
+
fail CompressionError, 'Huffman decode error (EOS invalid)'
|
62
54
|
end
|
63
|
-
emit.force_encoding(BINARY)
|
55
|
+
emit.force_encoding(Encoding::BINARY)
|
64
56
|
end
|
65
57
|
|
66
58
|
# Huffman table as specified in
|
67
|
-
# - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-
|
59
|
+
# - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-10#appendix-B
|
68
60
|
CODES = [
|
69
|
-
[
|
70
|
-
[
|
71
|
-
[
|
72
|
-
[
|
73
|
-
[
|
74
|
-
[
|
75
|
-
[
|
76
|
-
[
|
77
|
-
[
|
78
|
-
[
|
61
|
+
[0x1ff8, 13],
|
62
|
+
[0x7fffd8, 23],
|
63
|
+
[0xfffffe2, 28],
|
64
|
+
[0xfffffe3, 28],
|
65
|
+
[0xfffffe4, 28],
|
66
|
+
[0xfffffe5, 28],
|
67
|
+
[0xfffffe6, 28],
|
68
|
+
[0xfffffe7, 28],
|
69
|
+
[0xfffffe8, 28],
|
70
|
+
[0xffffea, 24],
|
79
71
|
[0x3ffffffc, 30],
|
80
|
-
[
|
81
|
-
[
|
72
|
+
[0xfffffe9, 28],
|
73
|
+
[0xfffffea, 28],
|
82
74
|
[0x3ffffffd, 30],
|
83
|
-
[
|
84
|
-
[
|
85
|
-
[
|
86
|
-
[
|
87
|
-
[
|
88
|
-
[
|
89
|
-
[
|
90
|
-
[
|
75
|
+
[0xfffffeb, 28],
|
76
|
+
[0xfffffec, 28],
|
77
|
+
[0xfffffed, 28],
|
78
|
+
[0xfffffee, 28],
|
79
|
+
[0xfffffef, 28],
|
80
|
+
[0xffffff0, 28],
|
81
|
+
[0xffffff1, 28],
|
82
|
+
[0xffffff2, 28],
|
91
83
|
[0x3ffffffe, 30],
|
92
|
-
[
|
93
|
-
[
|
94
|
-
[
|
95
|
-
[
|
96
|
-
[
|
97
|
-
[
|
98
|
-
[
|
99
|
-
[
|
100
|
-
[
|
101
|
-
[
|
102
|
-
[
|
103
|
-
[
|
104
|
-
[
|
105
|
-
[
|
106
|
-
[
|
107
|
-
[
|
108
|
-
[
|
109
|
-
[
|
110
|
-
[
|
111
|
-
[
|
112
|
-
[
|
113
|
-
[
|
114
|
-
[
|
115
|
-
[
|
116
|
-
[
|
117
|
-
[
|
118
|
-
[
|
119
|
-
[
|
120
|
-
[
|
121
|
-
[
|
122
|
-
[
|
123
|
-
[
|
124
|
-
[
|
125
|
-
[
|
126
|
-
[
|
127
|
-
[
|
128
|
-
[
|
129
|
-
[
|
130
|
-
[
|
131
|
-
[
|
132
|
-
[
|
133
|
-
[
|
134
|
-
[
|
135
|
-
[
|
136
|
-
[
|
137
|
-
[
|
138
|
-
[
|
139
|
-
[
|
140
|
-
[
|
141
|
-
[
|
142
|
-
[
|
143
|
-
[
|
144
|
-
[
|
145
|
-
[
|
146
|
-
[
|
147
|
-
[
|
148
|
-
[
|
149
|
-
[
|
150
|
-
[
|
151
|
-
[
|
152
|
-
[
|
153
|
-
[
|
154
|
-
[
|
155
|
-
[
|
156
|
-
[
|
157
|
-
[
|
158
|
-
[
|
159
|
-
[
|
160
|
-
[
|
161
|
-
[
|
162
|
-
[
|
163
|
-
[
|
164
|
-
[
|
165
|
-
[
|
166
|
-
[
|
167
|
-
[
|
168
|
-
[
|
169
|
-
[
|
170
|
-
[
|
171
|
-
[
|
172
|
-
[
|
173
|
-
[
|
174
|
-
[
|
175
|
-
[
|
176
|
-
[
|
177
|
-
[
|
178
|
-
[
|
179
|
-
[
|
180
|
-
[
|
181
|
-
[
|
182
|
-
[
|
183
|
-
[
|
184
|
-
[
|
185
|
-
[
|
186
|
-
[
|
187
|
-
[
|
188
|
-
[
|
189
|
-
[
|
190
|
-
[
|
191
|
-
[
|
192
|
-
[
|
193
|
-
[
|
194
|
-
[
|
195
|
-
[
|
196
|
-
[
|
197
|
-
[
|
198
|
-
[
|
199
|
-
[
|
200
|
-
[
|
201
|
-
[
|
202
|
-
[
|
203
|
-
[
|
204
|
-
[
|
205
|
-
[
|
206
|
-
[
|
207
|
-
[
|
208
|
-
[
|
209
|
-
[
|
210
|
-
[
|
211
|
-
[
|
212
|
-
[
|
213
|
-
[
|
214
|
-
[
|
215
|
-
[
|
216
|
-
[
|
217
|
-
[
|
218
|
-
[
|
219
|
-
[
|
220
|
-
[
|
221
|
-
[
|
222
|
-
[
|
223
|
-
[
|
224
|
-
[
|
225
|
-
[
|
226
|
-
[
|
227
|
-
[
|
228
|
-
[
|
229
|
-
[
|
230
|
-
[
|
231
|
-
[
|
232
|
-
[
|
233
|
-
[
|
234
|
-
[
|
235
|
-
[
|
236
|
-
[
|
237
|
-
[
|
238
|
-
[
|
239
|
-
[
|
240
|
-
[
|
241
|
-
[
|
242
|
-
[
|
243
|
-
[
|
244
|
-
[
|
245
|
-
[
|
246
|
-
[
|
247
|
-
[
|
248
|
-
[
|
249
|
-
[
|
250
|
-
[
|
251
|
-
[
|
252
|
-
[
|
253
|
-
[
|
254
|
-
[
|
255
|
-
[
|
256
|
-
[
|
257
|
-
[
|
258
|
-
[
|
259
|
-
[
|
260
|
-
[
|
261
|
-
[
|
262
|
-
[
|
263
|
-
[
|
264
|
-
[
|
265
|
-
[
|
266
|
-
[
|
267
|
-
[
|
268
|
-
[
|
269
|
-
[
|
270
|
-
[
|
271
|
-
[
|
272
|
-
[
|
273
|
-
[
|
274
|
-
[
|
275
|
-
[
|
276
|
-
[
|
277
|
-
[
|
278
|
-
[
|
279
|
-
[
|
280
|
-
[
|
281
|
-
[
|
282
|
-
[
|
283
|
-
[
|
284
|
-
[
|
285
|
-
[
|
286
|
-
[
|
287
|
-
[
|
288
|
-
[
|
289
|
-
[
|
290
|
-
[
|
291
|
-
[
|
292
|
-
[
|
293
|
-
[
|
294
|
-
[
|
295
|
-
[
|
296
|
-
[
|
297
|
-
[
|
298
|
-
[
|
299
|
-
[
|
300
|
-
[
|
301
|
-
[
|
302
|
-
[
|
303
|
-
[
|
304
|
-
[
|
305
|
-
[
|
306
|
-
[
|
307
|
-
[
|
308
|
-
[
|
309
|
-
[
|
310
|
-
[
|
311
|
-
[
|
312
|
-
[
|
313
|
-
[
|
314
|
-
[
|
315
|
-
[
|
316
|
-
[
|
317
|
-
[
|
318
|
-
[
|
319
|
-
[
|
320
|
-
[
|
321
|
-
[
|
322
|
-
[
|
323
|
-
[
|
324
|
-
[
|
84
|
+
[0xffffff3, 28],
|
85
|
+
[0xffffff4, 28],
|
86
|
+
[0xffffff5, 28],
|
87
|
+
[0xffffff6, 28],
|
88
|
+
[0xffffff7, 28],
|
89
|
+
[0xffffff8, 28],
|
90
|
+
[0xffffff9, 28],
|
91
|
+
[0xffffffa, 28],
|
92
|
+
[0xffffffb, 28],
|
93
|
+
[0x14, 6],
|
94
|
+
[0x3f8, 10],
|
95
|
+
[0x3f9, 10],
|
96
|
+
[0xffa, 12],
|
97
|
+
[0x1ff9, 13],
|
98
|
+
[0x15, 6],
|
99
|
+
[0xf8, 8],
|
100
|
+
[0x7fa, 11],
|
101
|
+
[0x3fa, 10],
|
102
|
+
[0x3fb, 10],
|
103
|
+
[0xf9, 8],
|
104
|
+
[0x7fb, 11],
|
105
|
+
[0xfa, 8],
|
106
|
+
[0x16, 6],
|
107
|
+
[0x17, 6],
|
108
|
+
[0x18, 6],
|
109
|
+
[0x0, 5],
|
110
|
+
[0x1, 5],
|
111
|
+
[0x2, 5],
|
112
|
+
[0x19, 6],
|
113
|
+
[0x1a, 6],
|
114
|
+
[0x1b, 6],
|
115
|
+
[0x1c, 6],
|
116
|
+
[0x1d, 6],
|
117
|
+
[0x1e, 6],
|
118
|
+
[0x1f, 6],
|
119
|
+
[0x5c, 7],
|
120
|
+
[0xfb, 8],
|
121
|
+
[0x7ffc, 15],
|
122
|
+
[0x20, 6],
|
123
|
+
[0xffb, 12],
|
124
|
+
[0x3fc, 10],
|
125
|
+
[0x1ffa, 13],
|
126
|
+
[0x21, 6],
|
127
|
+
[0x5d, 7],
|
128
|
+
[0x5e, 7],
|
129
|
+
[0x5f, 7],
|
130
|
+
[0x60, 7],
|
131
|
+
[0x61, 7],
|
132
|
+
[0x62, 7],
|
133
|
+
[0x63, 7],
|
134
|
+
[0x64, 7],
|
135
|
+
[0x65, 7],
|
136
|
+
[0x66, 7],
|
137
|
+
[0x67, 7],
|
138
|
+
[0x68, 7],
|
139
|
+
[0x69, 7],
|
140
|
+
[0x6a, 7],
|
141
|
+
[0x6b, 7],
|
142
|
+
[0x6c, 7],
|
143
|
+
[0x6d, 7],
|
144
|
+
[0x6e, 7],
|
145
|
+
[0x6f, 7],
|
146
|
+
[0x70, 7],
|
147
|
+
[0x71, 7],
|
148
|
+
[0x72, 7],
|
149
|
+
[0xfc, 8],
|
150
|
+
[0x73, 7],
|
151
|
+
[0xfd, 8],
|
152
|
+
[0x1ffb, 13],
|
153
|
+
[0x7fff0, 19],
|
154
|
+
[0x1ffc, 13],
|
155
|
+
[0x3ffc, 14],
|
156
|
+
[0x22, 6],
|
157
|
+
[0x7ffd, 15],
|
158
|
+
[0x3, 5],
|
159
|
+
[0x23, 6],
|
160
|
+
[0x4, 5],
|
161
|
+
[0x24, 6],
|
162
|
+
[0x5, 5],
|
163
|
+
[0x25, 6],
|
164
|
+
[0x26, 6],
|
165
|
+
[0x27, 6],
|
166
|
+
[0x6, 5],
|
167
|
+
[0x74, 7],
|
168
|
+
[0x75, 7],
|
169
|
+
[0x28, 6],
|
170
|
+
[0x29, 6],
|
171
|
+
[0x2a, 6],
|
172
|
+
[0x7, 5],
|
173
|
+
[0x2b, 6],
|
174
|
+
[0x76, 7],
|
175
|
+
[0x2c, 6],
|
176
|
+
[0x8, 5],
|
177
|
+
[0x9, 5],
|
178
|
+
[0x2d, 6],
|
179
|
+
[0x77, 7],
|
180
|
+
[0x78, 7],
|
181
|
+
[0x79, 7],
|
182
|
+
[0x7a, 7],
|
183
|
+
[0x7b, 7],
|
184
|
+
[0x7ffe, 15],
|
185
|
+
[0x7fc, 11],
|
186
|
+
[0x3ffd, 14],
|
187
|
+
[0x1ffd, 13],
|
188
|
+
[0xffffffc, 28],
|
189
|
+
[0xfffe6, 20],
|
190
|
+
[0x3fffd2, 22],
|
191
|
+
[0xfffe7, 20],
|
192
|
+
[0xfffe8, 20],
|
193
|
+
[0x3fffd3, 22],
|
194
|
+
[0x3fffd4, 22],
|
195
|
+
[0x3fffd5, 22],
|
196
|
+
[0x7fffd9, 23],
|
197
|
+
[0x3fffd6, 22],
|
198
|
+
[0x7fffda, 23],
|
199
|
+
[0x7fffdb, 23],
|
200
|
+
[0x7fffdc, 23],
|
201
|
+
[0x7fffdd, 23],
|
202
|
+
[0x7fffde, 23],
|
203
|
+
[0xffffeb, 24],
|
204
|
+
[0x7fffdf, 23],
|
205
|
+
[0xffffec, 24],
|
206
|
+
[0xffffed, 24],
|
207
|
+
[0x3fffd7, 22],
|
208
|
+
[0x7fffe0, 23],
|
209
|
+
[0xffffee, 24],
|
210
|
+
[0x7fffe1, 23],
|
211
|
+
[0x7fffe2, 23],
|
212
|
+
[0x7fffe3, 23],
|
213
|
+
[0x7fffe4, 23],
|
214
|
+
[0x1fffdc, 21],
|
215
|
+
[0x3fffd8, 22],
|
216
|
+
[0x7fffe5, 23],
|
217
|
+
[0x3fffd9, 22],
|
218
|
+
[0x7fffe6, 23],
|
219
|
+
[0x7fffe7, 23],
|
220
|
+
[0xffffef, 24],
|
221
|
+
[0x3fffda, 22],
|
222
|
+
[0x1fffdd, 21],
|
223
|
+
[0xfffe9, 20],
|
224
|
+
[0x3fffdb, 22],
|
225
|
+
[0x3fffdc, 22],
|
226
|
+
[0x7fffe8, 23],
|
227
|
+
[0x7fffe9, 23],
|
228
|
+
[0x1fffde, 21],
|
229
|
+
[0x7fffea, 23],
|
230
|
+
[0x3fffdd, 22],
|
231
|
+
[0x3fffde, 22],
|
232
|
+
[0xfffff0, 24],
|
233
|
+
[0x1fffdf, 21],
|
234
|
+
[0x3fffdf, 22],
|
235
|
+
[0x7fffeb, 23],
|
236
|
+
[0x7fffec, 23],
|
237
|
+
[0x1fffe0, 21],
|
238
|
+
[0x1fffe1, 21],
|
239
|
+
[0x3fffe0, 22],
|
240
|
+
[0x1fffe2, 21],
|
241
|
+
[0x7fffed, 23],
|
242
|
+
[0x3fffe1, 22],
|
243
|
+
[0x7fffee, 23],
|
244
|
+
[0x7fffef, 23],
|
245
|
+
[0xfffea, 20],
|
246
|
+
[0x3fffe2, 22],
|
247
|
+
[0x3fffe3, 22],
|
248
|
+
[0x3fffe4, 22],
|
249
|
+
[0x7ffff0, 23],
|
250
|
+
[0x3fffe5, 22],
|
251
|
+
[0x3fffe6, 22],
|
252
|
+
[0x7ffff1, 23],
|
253
|
+
[0x3ffffe0, 26],
|
254
|
+
[0x3ffffe1, 26],
|
255
|
+
[0xfffeb, 20],
|
256
|
+
[0x7fff1, 19],
|
257
|
+
[0x3fffe7, 22],
|
258
|
+
[0x7ffff2, 23],
|
259
|
+
[0x3fffe8, 22],
|
260
|
+
[0x1ffffec, 25],
|
261
|
+
[0x3ffffe2, 26],
|
262
|
+
[0x3ffffe3, 26],
|
263
|
+
[0x3ffffe4, 26],
|
264
|
+
[0x7ffffde, 27],
|
265
|
+
[0x7ffffdf, 27],
|
266
|
+
[0x3ffffe5, 26],
|
267
|
+
[0xfffff1, 24],
|
268
|
+
[0x1ffffed, 25],
|
269
|
+
[0x7fff2, 19],
|
270
|
+
[0x1fffe3, 21],
|
271
|
+
[0x3ffffe6, 26],
|
272
|
+
[0x7ffffe0, 27],
|
273
|
+
[0x7ffffe1, 27],
|
274
|
+
[0x3ffffe7, 26],
|
275
|
+
[0x7ffffe2, 27],
|
276
|
+
[0xfffff2, 24],
|
277
|
+
[0x1fffe4, 21],
|
278
|
+
[0x1fffe5, 21],
|
279
|
+
[0x3ffffe8, 26],
|
280
|
+
[0x3ffffe9, 26],
|
281
|
+
[0xffffffd, 28],
|
282
|
+
[0x7ffffe3, 27],
|
283
|
+
[0x7ffffe4, 27],
|
284
|
+
[0x7ffffe5, 27],
|
285
|
+
[0xfffec, 20],
|
286
|
+
[0xfffff3, 24],
|
287
|
+
[0xfffed, 20],
|
288
|
+
[0x1fffe6, 21],
|
289
|
+
[0x3fffe9, 22],
|
290
|
+
[0x1fffe7, 21],
|
291
|
+
[0x1fffe8, 21],
|
292
|
+
[0x7ffff3, 23],
|
293
|
+
[0x3fffea, 22],
|
294
|
+
[0x3fffeb, 22],
|
295
|
+
[0x1ffffee, 25],
|
296
|
+
[0x1ffffef, 25],
|
297
|
+
[0xfffff4, 24],
|
298
|
+
[0xfffff5, 24],
|
299
|
+
[0x3ffffea, 26],
|
300
|
+
[0x7ffff4, 23],
|
301
|
+
[0x3ffffeb, 26],
|
302
|
+
[0x7ffffe6, 27],
|
303
|
+
[0x3ffffec, 26],
|
304
|
+
[0x3ffffed, 26],
|
305
|
+
[0x7ffffe7, 27],
|
306
|
+
[0x7ffffe8, 27],
|
307
|
+
[0x7ffffe9, 27],
|
308
|
+
[0x7ffffea, 27],
|
309
|
+
[0x7ffffeb, 27],
|
310
|
+
[0xffffffe, 28],
|
311
|
+
[0x7ffffec, 27],
|
312
|
+
[0x7ffffed, 27],
|
313
|
+
[0x7ffffee, 27],
|
314
|
+
[0x7ffffef, 27],
|
315
|
+
[0x7fffff0, 27],
|
316
|
+
[0x3ffffee, 26],
|
325
317
|
[0x3fffffff, 30],
|
326
|
-
]
|
318
|
+
].each(&:freeze).freeze
|
327
319
|
|
320
|
+
ENCODE_TABLE = CODES.map { |c, l| [c].pack('N').unpack('B*').first[-l..-1] }.each(&:freeze).freeze
|
328
321
|
end
|
329
|
-
|
330
322
|
end
|
331
|
-
|
332
323
|
end
|