@@ -31,22 +31,46 @@ defmodule HPAX do
31
31
32
32
@ valid_header_actions [ :store , :store_name , :no_store , :never_store ]
33
33
34
+ @ doc """
35
+ Creates a new HPACK table.
36
+
37
+ Same as `new/2` with default options.
38
+ """
39
+ @ spec new ( non_neg_integer ( ) ) :: Table . t ( )
40
+ def new ( max_table_size ) , do: new ( max_table_size , [ ] )
41
+
34
42
@ doc """
35
43
Create a new HPACK table that can be used as encoding or decoding context.
36
44
37
45
See the "Encoding and decoding contexts" section in the module documentation.
38
46
39
47
`max_table_size` is the maximum table size (in bytes) for the newly created table.
40
48
49
+ ## Options
50
+
51
+ This function accepts the following `options`:
52
+
53
+ * `:huffman_encoding` - (since 0.3.0) `:always` or `:never`. If `:always`,
54
+ then HPAX will always encode headers using Huffman encoding. If `:never`,
55
+ HPAX will not use any Huffman encoding. Defaults to `:never`.
56
+
41
57
## Examples
42
58
43
59
encoding_context = HPAX.new(4096)
44
60
45
61
"""
46
- @ spec new ( non_neg_integer ( ) , [ Table . option ( ) ] ) :: Table . t ( )
47
- def new ( max_table_size , options \\ [ ] )
48
- when is_integer ( max_table_size ) and max_table_size >= 0 do
49
- Table . new ( max_table_size , options )
62
+ @ doc since: "0.3.0"
63
+ @ spec new ( non_neg_integer ( ) , [ keyword ( ) ] ) :: Table . t ( )
64
+ def new ( max_table_size , options )
65
+ when is_integer ( max_table_size ) and max_table_size >= 0 and is_list ( options ) do
66
+ options = Keyword . put_new ( options , :huffman_encoding , :never )
67
+
68
+ Enum . each ( options , fn
69
+ { :huffman_encoding , _huffman_encoding } -> :ok
70
+ { key , _value } -> raise ArgumentError , "unknown option: #{ inspect ( key ) } "
71
+ end )
72
+
73
+ Table . new ( max_table_size , Keyword . fetch! ( options , :huffman_encoding ) )
50
74
end
51
75
52
76
@ doc """
@@ -252,7 +276,7 @@ defmodule HPAX do
252
276
253
277
defp encode_headers ( [ { action , name , value } | rest ] , table , acc )
254
278
when action in @ valid_header_actions and is_binary ( name ) and is_binary ( value ) do
255
- huffman? = table . huffman == :always
279
+ huffman? = table . huffman_encoding == :always
256
280
257
281
{ encoded , table } =
258
282
case Table . lookup_by_header ( table , name , value ) do
@@ -287,27 +311,27 @@ defmodule HPAX do
287
311
<< 1 :: 1 , Types . encode_integer ( index , 7 ) :: bitstring >>
288
312
end
289
313
290
- defp encode_literal_header_with_indexing ( index , value , huffman ) when is_integer ( index ) do
291
- [ << 1 :: 2 , Types . encode_integer ( index , 6 ) :: bitstring >> , Types . encode_binary ( value , huffman ) ]
314
+ defp encode_literal_header_with_indexing ( index , value , huffman? ) when is_integer ( index ) do
315
+ [ << 1 :: 2 , Types . encode_integer ( index , 6 ) :: bitstring >> , Types . encode_binary ( value , huffman? ) ]
292
316
end
293
317
294
- defp encode_literal_header_with_indexing ( name , value , huffman ) when is_binary ( name ) do
295
- [ << 1 :: 2 , 0 :: 6 >> , Types . encode_binary ( name , huffman ) , Types . encode_binary ( value , huffman ) ]
318
+ defp encode_literal_header_with_indexing ( name , value , huffman? ) when is_binary ( name ) do
319
+ [ << 1 :: 2 , 0 :: 6 >> , Types . encode_binary ( name , huffman? ) , Types . encode_binary ( value , huffman? ) ]
296
320
end
297
321
298
- defp encode_literal_header_without_indexing ( index , value , huffman ) when is_integer ( index ) do
299
- [ << 0 :: 4 , Types . encode_integer ( index , 4 ) :: bitstring >> , Types . encode_binary ( value , huffman ) ]
322
+ defp encode_literal_header_without_indexing ( index , value , huffman? ) when is_integer ( index ) do
323
+ [ << 0 :: 4 , Types . encode_integer ( index , 4 ) :: bitstring >> , Types . encode_binary ( value , huffman? ) ]
300
324
end
301
325
302
- defp encode_literal_header_without_indexing ( name , value , huffman ) when is_binary ( name ) do
303
- [ << 0 :: 4 , 0 :: 4 >> , Types . encode_binary ( name , huffman ) , Types . encode_binary ( value , huffman ) ]
326
+ defp encode_literal_header_without_indexing ( name , value , huffman? ) when is_binary ( name ) do
327
+ [ << 0 :: 4 , 0 :: 4 >> , Types . encode_binary ( name , huffman? ) , Types . encode_binary ( value , huffman? ) ]
304
328
end
305
329
306
- defp encode_literal_header_never_indexed ( index , value , huffman ) when is_integer ( index ) do
307
- [ << 1 :: 4 , Types . encode_integer ( index , 4 ) :: bitstring >> , Types . encode_binary ( value , huffman ) ]
330
+ defp encode_literal_header_never_indexed ( index , value , huffman? ) when is_integer ( index ) do
331
+ [ << 1 :: 4 , Types . encode_integer ( index , 4 ) :: bitstring >> , Types . encode_binary ( value , huffman? ) ]
308
332
end
309
333
310
- defp encode_literal_header_never_indexed ( name , value , huffman ) when is_binary ( name ) do
311
- [ << 1 :: 4 , 0 :: 4 >> , Types . encode_binary ( name , huffman ) , Types . encode_binary ( value , huffman ) ]
334
+ defp encode_literal_header_never_indexed ( name , value , huffman? ) when is_binary ( name ) do
335
+ [ << 1 :: 4 , 0 :: 4 >> , Types . encode_binary ( name , huffman? ) , Types . encode_binary ( value , huffman? ) ]
312
336
end
313
337
end
0 commit comments