1- # Vendored from hpax v1.0.3 , do not edit manually
1+ # Vendored from hpax v1.1.0 , do not edit manually
22
33defmodule Hex.Mint.HPAX do
44 _ = """
@@ -84,30 +84,62 @@ defmodule Hex.Mint.HPAX do
8484 end
8585
8686 @ doc """
87- Resizes the given table to the given maximum size.
87+ Resizes the given encoding table to the given maximum size.
8888
89- This is intended for use where the overlying protocol has signaled a change to the table's
90- maximum size, such as when an HTTP/2 `SETTINGS` frame is received.
89+ This is intended for use on an encoding context where the overlying protocol has signaled a
90+ change to the maximum size the encoder is permitted to use, such as when an HTTP/2 `SETTINGS`
91+ frame is received. The table takes the whole size it is permitted, which
92+ [RFC7541§4.2](https://datatracker.ietf.org/doc/html/rfc7541#section-4.2) requires it to declare
93+ to the decoder, so a "dynamic table size update" command is prefixed to the next block encoded
94+ with this table.
9195
9296 If the indicated size is less than the table's current size, entries
9397 will be evicted as needed to fit within the specified size, and the table's
94- maximum size will be decreased to the specified value. A flag will also be
95- set which will enqueue a "dynamic table size update" command to be prefixed
96- to the next block encoded with this table, per
97- [RFC9113§4.3.1](https://www.rfc-editor.org/rfc/rfc9113.html#section-4.3.1).
98+ maximum size will be decreased to the specified value.
9899
99100 If the indicated size is greater than or equal to the table's current max size, no entries are evicted
100101 and the table's maximum size changes to the specified value.
101102
103+ Use `protocol_resize/2` for a decoding context, whose maximum size is chosen by the peer's
104+ encoder rather than by the protocol.
105+
102106 ## Examples
103107
104- decoding_context = Hex.Mint.HPAX.new(4096)
105- Hex.Mint.HPAX.resize(decoding_context , 8192)
108+ encoding_context = Hex.Mint.HPAX.new(4096)
109+ Hex.Mint.HPAX.resize(encoding_context , 8192)
106110
107111 """
108112 @ spec resize ( table ( ) , non_neg_integer ( ) ) :: table ( )
109113 defdelegate resize ( table , new_max_size ) , to: Table
110114
115+ @ doc """
116+ Changes the maximum size the peer's encoder is permitted to use for the given decoding table.
117+
118+ This is intended for use on a decoding context when the overlying protocol communicates a new
119+ maximum to the peer, such as when an HTTP/2 client sends `SETTINGS_HEADER_TABLE_SIZE` and the
120+ server acknowledges it.
121+ [RFC7541§4.2](https://datatracker.ietf.org/doc/html/rfc7541#section-4.2) leaves the table's own
122+ maximum size to the encoder, which declares it with a "dynamic table size update" command, so
123+ raising the permitted maximum doesn't change the size of the table.
124+
125+ If the new maximum is below the maximum the encoder declared, the table is resized to it and
126+ entries are evicted as needed to fit. The encoder then has to declare a size of at most the new
127+ maximum at the start of its next block, so `decode/2` returns `{:error, :missing_size_update}`
128+ for a block that doesn't start with one, per
129+ [RFC9113§4.3.1](https://www.rfc-editor.org/rfc/rfc9113.html#section-4.3.1). When the maximum
130+ changes more than once before that block, the smallest of them is the one that has to be
131+ declared.
132+
133+ ## Examples
134+
135+ decoding_context = Hex.Mint.HPAX.new(4096)
136+ Hex.Mint.HPAX.protocol_resize(decoding_context, 8192)
137+
138+ """
139+ @ doc since: "1.1.0"
140+ @ spec protocol_resize ( table ( ) , non_neg_integer ( ) ) :: table ( )
141+ defdelegate protocol_resize ( table , new_protocol_max_size ) , to: Table
142+
111143 @ doc """
112144 Decodes a header block fragment (HBF) through a given table.
113145
@@ -131,13 +163,31 @@ defmodule Hex.Mint.HPAX do
131163 def decode ( << 0b001 :: 3 , rest :: bitstring >> , % Table { } = table ) do
132164 { new_max_size , rest } = decode_integer ( rest , 5 )
133165
134- # Dynamic resizes must be less than protocol max table size
135- # https://datatracker.ietf.org/doc/html/rfc7541#section-6.3
136- if new_max_size <= table . protocol_max_table_size do
137- decode ( rest , Table . dynamic_resize ( table , new_max_size ) )
138- else
139- { :error , :protocol_error }
166+ cond do
167+ # Dynamic resizes must be less than protocol max table size
168+ # https://datatracker.ietf.org/doc/html/rfc7541#section-6.3
169+ new_max_size > table . protocol_max_table_size ->
170+ { :error , :protocol_error }
171+
172+ # The smallest maximum size the table was reduced to has to be signalled first
173+ # https://datatracker.ietf.org/doc/html/rfc7541#section-4.2
174+ is_integer ( table . required_minimum_resize ) and new_max_size > table . required_minimum_resize ->
175+ { :error , :missing_size_update }
176+
177+ true ->
178+ table = % { table | required_minimum_resize: nil }
179+ decode ( rest , Table . dynamic_resize ( table , new_max_size ) )
140180 end
181+ catch
182+ :throw , { :hpax , error } -> { :error , error }
183+ end
184+
185+ # A reduction of the maximum size below the size of the table has to be acknowledged with a
186+ # dynamic table size update at the start of the next block
187+ # https://www.rfc-editor.org/rfc/rfc9113.html#section-4.3.1
188+ def decode ( block , % Table { required_minimum_resize: required } )
189+ when is_binary ( block ) and is_integer ( required ) do
190+ { :error , :missing_size_update }
141191 end
142192
143193 def decode ( block , % Table { } = table ) when is_binary ( block ) do
@@ -300,7 +350,8 @@ defmodule Hex.Mint.HPAX do
300350 end
301351
302352 defp encode_headers ( [ { action , name , value } | rest ] , table , acc )
303- when action in @ valid_header_actions and is_binary ( name ) and is_binary ( value ) do
353+ when action in @ valid_header_actions do
354+ validate_header ( name , value )
304355 huffman? = table . huffman_encoding == :always
305356
306357 { encoded , table } =
@@ -332,6 +383,15 @@ defmodule Hex.Mint.HPAX do
332383 encode_headers ( rest , table , [ acc , encoded ] )
333384 end
334385
386+ defp validate_header ( name , value ) when is_binary ( name ) and is_binary ( value ) do
387+ :ok
388+ end
389+
390+ defp validate_header ( name , value ) do
391+ raise ArgumentError ,
392+ "expected header name/value to be strings, got: #{ inspect ( name ) } /#{ inspect ( value ) } "
393+ end
394+
335395 defp encode_indexed_header ( index ) do
336396 << 1 :: 1 , Types . encode_integer ( index , 7 ) :: bitstring >>
337397 end
0 commit comments