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.
@@ -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-08
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 :BINARY, :EOS
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| @@encode_table[chr]}.join
30
- bitstring << "1" * ((8 - bitstring.size) % 8)
31
- [bitstring].pack("B*")
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 and
54
- raise CompressionError.new('Huffman decode error (EOS found)')
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
- raise CompressionError.new('Huffman decode error (EOS invalid)')
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-08#appendix-C
59
+ # - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-10#appendix-B
68
60
  CODES = [
69
- [ 0x1ff8, 13],
70
- [ 0x7fffd8, 23],
71
- [ 0xfffffe2, 28],
72
- [ 0xfffffe3, 28],
73
- [ 0xfffffe4, 28],
74
- [ 0xfffffe5, 28],
75
- [ 0xfffffe6, 28],
76
- [ 0xfffffe7, 28],
77
- [ 0xfffffe8, 28],
78
- [ 0xffffea, 24],
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
- [ 0xfffffe9, 28],
81
- [ 0xfffffea, 28],
72
+ [0xfffffe9, 28],
73
+ [0xfffffea, 28],
82
74
  [0x3ffffffd, 30],
83
- [ 0xfffffeb, 28],
84
- [ 0xfffffec, 28],
85
- [ 0xfffffed, 28],
86
- [ 0xfffffee, 28],
87
- [ 0xfffffef, 28],
88
- [ 0xffffff0, 28],
89
- [ 0xffffff1, 28],
90
- [ 0xffffff2, 28],
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
- [ 0xffffff3, 28],
93
- [ 0xffffff4, 28],
94
- [ 0xffffff5, 28],
95
- [ 0xffffff6, 28],
96
- [ 0xffffff7, 28],
97
- [ 0xffffff8, 28],
98
- [ 0xffffff9, 28],
99
- [ 0xffffffa, 28],
100
- [ 0xffffffb, 28],
101
- [ 0x14, 6],
102
- [ 0x3f8, 10],
103
- [ 0x3f9, 10],
104
- [ 0xffa, 12],
105
- [ 0x1ff9, 13],
106
- [ 0x15, 6],
107
- [ 0xf8, 8],
108
- [ 0x7fa, 11],
109
- [ 0x3fa, 10],
110
- [ 0x3fb, 10],
111
- [ 0xf9, 8],
112
- [ 0x7fb, 11],
113
- [ 0xfa, 8],
114
- [ 0x16, 6],
115
- [ 0x17, 6],
116
- [ 0x18, 6],
117
- [ 0x0, 5],
118
- [ 0x1, 5],
119
- [ 0x2, 5],
120
- [ 0x19, 6],
121
- [ 0x1a, 6],
122
- [ 0x1b, 6],
123
- [ 0x1c, 6],
124
- [ 0x1d, 6],
125
- [ 0x1e, 6],
126
- [ 0x1f, 6],
127
- [ 0x5c, 7],
128
- [ 0xfb, 8],
129
- [ 0x7ffc, 15],
130
- [ 0x20, 6],
131
- [ 0xffb, 12],
132
- [ 0x3fc, 10],
133
- [ 0x1ffa, 13],
134
- [ 0x21, 6],
135
- [ 0x5d, 7],
136
- [ 0x5e, 7],
137
- [ 0x5f, 7],
138
- [ 0x60, 7],
139
- [ 0x61, 7],
140
- [ 0x62, 7],
141
- [ 0x63, 7],
142
- [ 0x64, 7],
143
- [ 0x65, 7],
144
- [ 0x66, 7],
145
- [ 0x67, 7],
146
- [ 0x68, 7],
147
- [ 0x69, 7],
148
- [ 0x6a, 7],
149
- [ 0x6b, 7],
150
- [ 0x6c, 7],
151
- [ 0x6d, 7],
152
- [ 0x6e, 7],
153
- [ 0x6f, 7],
154
- [ 0x70, 7],
155
- [ 0x71, 7],
156
- [ 0x72, 7],
157
- [ 0xfc, 8],
158
- [ 0x73, 7],
159
- [ 0xfd, 8],
160
- [ 0x1ffb, 13],
161
- [ 0x7fff0, 19],
162
- [ 0x1ffc, 13],
163
- [ 0x3ffc, 14],
164
- [ 0x22, 6],
165
- [ 0x7ffd, 15],
166
- [ 0x3, 5],
167
- [ 0x23, 6],
168
- [ 0x4, 5],
169
- [ 0x24, 6],
170
- [ 0x5, 5],
171
- [ 0x25, 6],
172
- [ 0x26, 6],
173
- [ 0x27, 6],
174
- [ 0x6, 5],
175
- [ 0x74, 7],
176
- [ 0x75, 7],
177
- [ 0x28, 6],
178
- [ 0x29, 6],
179
- [ 0x2a, 6],
180
- [ 0x7, 5],
181
- [ 0x2b, 6],
182
- [ 0x76, 7],
183
- [ 0x2c, 6],
184
- [ 0x8, 5],
185
- [ 0x9, 5],
186
- [ 0x2d, 6],
187
- [ 0x77, 7],
188
- [ 0x78, 7],
189
- [ 0x79, 7],
190
- [ 0x7a, 7],
191
- [ 0x7b, 7],
192
- [ 0x7ffe, 15],
193
- [ 0x7fc, 11],
194
- [ 0x3ffd, 14],
195
- [ 0x1ffd, 13],
196
- [ 0xffffffc, 28],
197
- [ 0xfffe6, 20],
198
- [ 0x3fffd2, 22],
199
- [ 0xfffe7, 20],
200
- [ 0xfffe8, 20],
201
- [ 0x3fffd3, 22],
202
- [ 0x3fffd4, 22],
203
- [ 0x3fffd5, 22],
204
- [ 0x7fffd9, 23],
205
- [ 0x3fffd6, 22],
206
- [ 0x7fffda, 23],
207
- [ 0x7fffdb, 23],
208
- [ 0x7fffdc, 23],
209
- [ 0x7fffdd, 23],
210
- [ 0x7fffde, 23],
211
- [ 0xffffeb, 24],
212
- [ 0x7fffdf, 23],
213
- [ 0xffffec, 24],
214
- [ 0xffffed, 24],
215
- [ 0x3fffd7, 22],
216
- [ 0x7fffe0, 23],
217
- [ 0xffffee, 24],
218
- [ 0x7fffe1, 23],
219
- [ 0x7fffe2, 23],
220
- [ 0x7fffe3, 23],
221
- [ 0x7fffe4, 23],
222
- [ 0x1fffdc, 21],
223
- [ 0x3fffd8, 22],
224
- [ 0x7fffe5, 23],
225
- [ 0x3fffd9, 22],
226
- [ 0x7fffe6, 23],
227
- [ 0x7fffe7, 23],
228
- [ 0xffffef, 24],
229
- [ 0x3fffda, 22],
230
- [ 0x1fffdd, 21],
231
- [ 0xfffe9, 20],
232
- [ 0x3fffdb, 22],
233
- [ 0x3fffdc, 22],
234
- [ 0x7fffe8, 23],
235
- [ 0x7fffe9, 23],
236
- [ 0x1fffde, 21],
237
- [ 0x7fffea, 23],
238
- [ 0x3fffdd, 22],
239
- [ 0x3fffde, 22],
240
- [ 0xfffff0, 24],
241
- [ 0x1fffdf, 21],
242
- [ 0x3fffdf, 22],
243
- [ 0x7fffeb, 23],
244
- [ 0x7fffec, 23],
245
- [ 0x1fffe0, 21],
246
- [ 0x1fffe1, 21],
247
- [ 0x3fffe0, 22],
248
- [ 0x1fffe2, 21],
249
- [ 0x7fffed, 23],
250
- [ 0x3fffe1, 22],
251
- [ 0x7fffee, 23],
252
- [ 0x7fffef, 23],
253
- [ 0xfffea, 20],
254
- [ 0x3fffe2, 22],
255
- [ 0x3fffe3, 22],
256
- [ 0x3fffe4, 22],
257
- [ 0x7ffff0, 23],
258
- [ 0x3fffe5, 22],
259
- [ 0x3fffe6, 22],
260
- [ 0x7ffff1, 23],
261
- [ 0x3ffffe0, 26],
262
- [ 0x3ffffe1, 26],
263
- [ 0xfffeb, 20],
264
- [ 0x7fff1, 19],
265
- [ 0x3fffe7, 22],
266
- [ 0x7ffff2, 23],
267
- [ 0x3fffe8, 22],
268
- [ 0x1ffffec, 25],
269
- [ 0x3ffffe2, 26],
270
- [ 0x3ffffe3, 26],
271
- [ 0x3ffffe4, 26],
272
- [ 0x7ffffde, 27],
273
- [ 0x7ffffdf, 27],
274
- [ 0x3ffffe5, 26],
275
- [ 0xfffff1, 24],
276
- [ 0x1ffffed, 25],
277
- [ 0x7fff2, 19],
278
- [ 0x1fffe3, 21],
279
- [ 0x3ffffe6, 26],
280
- [ 0x7ffffe0, 27],
281
- [ 0x7ffffe1, 27],
282
- [ 0x3ffffe7, 26],
283
- [ 0x7ffffe2, 27],
284
- [ 0xfffff2, 24],
285
- [ 0x1fffe4, 21],
286
- [ 0x1fffe5, 21],
287
- [ 0x3ffffe8, 26],
288
- [ 0x3ffffe9, 26],
289
- [ 0xffffffd, 28],
290
- [ 0x7ffffe3, 27],
291
- [ 0x7ffffe4, 27],
292
- [ 0x7ffffe5, 27],
293
- [ 0xfffec, 20],
294
- [ 0xfffff3, 24],
295
- [ 0xfffed, 20],
296
- [ 0x1fffe6, 21],
297
- [ 0x3fffe9, 22],
298
- [ 0x1fffe7, 21],
299
- [ 0x1fffe8, 21],
300
- [ 0x7ffff3, 23],
301
- [ 0x3fffea, 22],
302
- [ 0x3fffeb, 22],
303
- [ 0x1ffffee, 25],
304
- [ 0x1ffffef, 25],
305
- [ 0xfffff4, 24],
306
- [ 0xfffff5, 24],
307
- [ 0x3ffffea, 26],
308
- [ 0x7ffff4, 23],
309
- [ 0x3ffffeb, 26],
310
- [ 0x7ffffe6, 27],
311
- [ 0x3ffffec, 26],
312
- [ 0x3ffffed, 26],
313
- [ 0x7ffffe7, 27],
314
- [ 0x7ffffe8, 27],
315
- [ 0x7ffffe9, 27],
316
- [ 0x7ffffea, 27],
317
- [ 0x7ffffeb, 27],
318
- [ 0xffffffe, 28],
319
- [ 0x7ffffec, 27],
320
- [ 0x7ffffed, 27],
321
- [ 0x7ffffee, 27],
322
- [ 0x7ffffef, 27],
323
- [ 0x7fffff0, 27],
324
- [ 0x3ffffee, 26],
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