Debian Codegraph @

crt/aws-c-http/include/aws/http/private/hpack.h

raw symbols SCIP
1 #ifndef AWS_HTTP_HPACK_H
2 #define AWS_HTTP_HPACK_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8 #include <aws/http/request_response.h>
9
10 #include <aws/common/hash_table.h>
11 #include <aws/compression/huffman.h>
12
13 /**
14 * Result of aws_hpack_decode() call.
15 * If a complete entry has not been decoded yet, type is ONGOING.
16 * Otherwise, type informs which data to look at.
17 */
18
19 enum aws_hpack_decode_type {
20 AWS_HPACK_DECODE_T_ONGOING,
21 AWS_HPACK_DECODE_T_HEADER_FIELD,
22 AWS_HPACK_DECODE_T_DYNAMIC_TABLE_RESIZE,
23 };
24
25 struct aws_hpack_decode_result {
26 enum aws_hpack_decode_type type;
27
28 union {
29 /* If type is AWS_HPACK_DECODE_T_HEADER_FIELD */
30 struct aws_http_header header_field;
31
32 /* If type is AWS_HPACK_DECODE_T_DYNAMIC_TABLE_RESIZE */
33 size_t dynamic_table_resize;
34 } data;
35 };
36
37 /**
38 * Controls whether non-indexed strings will use Huffman encoding.
39 * In SMALLEST mode, strings will only be sent with Huffman encoding if it makes them smaller.
40 *
41 * Note: This does not control compression via "indexing",
42 * for that, see `aws_http_header_compression`.
43 * This only controls how string values are encoded when they're not already in a table.
44 */
45 enum aws_hpack_huffman_mode {
46 AWS_HPACK_HUFFMAN_SMALLEST,
47 AWS_HPACK_HUFFMAN_NEVER,
48 AWS_HPACK_HUFFMAN_ALWAYS,
49 };
50
51 /**
52 * Maintains the dynamic table.
53 * Insertion is backwards, indexing is forwards
54 */
55 struct aws_hpack_context {
56 struct aws_allocator *allocator;
57
58 enum aws_http_log_subject log_subject;
59 const void *log_id;
60
61 struct {
62 /* Array of headers, pointers to memory we alloced, which needs to be cleaned up whenever we move an entry out
63 */
64 struct aws_http_header *buffer;
65 size_t buffer_capacity; /* Number of http_headers that can fit in buffer */
66
67 size_t num_elements;
68 size_t index_0;
69
70 /* Size in bytes, according to [4.1] */
71 size_t size;
72 size_t max_size;
73
74 /* aws_http_header * -> size_t */
75 struct aws_hash_table reverse_lookup;
76 /* aws_byte_cursor * -> size_t */
77 struct aws_hash_table reverse_lookup_name_only;
78 } dynamic_table;
79 };
80
81 /**
82 * Encodes outgoing headers.
83 */
84 struct aws_hpack_encoder {
85 const void *log_id;
86
87 struct aws_huffman_encoder huffman_encoder;
88 enum aws_hpack_huffman_mode huffman_mode;
89
90 struct aws_hpack_context context;
91
92 struct {
93 size_t latest_value;
94 size_t smallest_value;
95 bool pending;
96 } dynamic_table_size_update;
97 };
98
99 /**
100 * Decodes incoming headers
101 */
102 struct aws_hpack_decoder {
103 const void *log_id;
104
105 struct aws_huffman_decoder huffman_decoder;
106
107 struct aws_hpack_context context;
108
109 /* TODO: check the new (RFC 9113 - 4.3.1) to make sure we did it right */
110 /* SETTINGS_HEADER_TABLE_SIZE from http2 */
111 size_t dynamic_table_protocol_max_size_setting;
112
113 /* PRO TIP: Don't union progress_integer and progress_string together, since string_decode calls integer_decode */
114 struct hpack_progress_integer {
115 enum {
116 HPACK_INTEGER_STATE_INIT,
117 HPACK_INTEGER_STATE_VALUE,
118 } state;
119 uint8_t bit_count;
120 } progress_integer;
121
122 struct hpack_progress_string {
123 enum {
124 HPACK_STRING_STATE_INIT,
125 HPACK_STRING_STATE_LENGTH,
126 HPACK_STRING_STATE_VALUE,
127 } state;
128 bool use_huffman;
129 uint64_t length;
130 } progress_string;
131
132 struct hpack_progress_entry {
133 enum {
134 HPACK_ENTRY_STATE_INIT,
135 /* Indexed header field: just 1 state. read index, find name and value at index */
136 HPACK_ENTRY_STATE_INDEXED,
137 /* Literal header field: name may be indexed OR literal, value is always literal */
138 HPACK_ENTRY_STATE_LITERAL_BEGIN,
139 HPACK_ENTRY_STATE_LITERAL_NAME_STRING,
140 HPACK_ENTRY_STATE_LITERAL_VALUE_STRING,
141 /* Dynamic table resize: just 1 state. read new size */
142 HPACK_ENTRY_STATE_DYNAMIC_TABLE_RESIZE,
143 /* Done */
144 HPACK_ENTRY_STATE_COMPLETE,
145 } state;
146
147 union {
148 struct {
149 uint64_t index;
150 } indexed;
151
152 struct hpack_progress_literal {
153 uint8_t prefix_size;
154 enum aws_http_header_compression compression;
155 uint64_t name_index;
156 size_t name_length;
157 } literal;
158
159 struct {
160 uint64_t size;
161 } dynamic_table_resize;
162 } u;
163
164 enum aws_hpack_decode_type type;
165
166 /* Scratch holds header name and value while decoding */
167 struct aws_byte_buf scratch;
168 } progress_entry;
169 };
170
171 AWS_EXTERN_C_BEGIN
172
173 /* Library-level init and shutdown */
174 void aws_hpack_static_table_init(struct aws_allocator *allocator);
175 void aws_hpack_static_table_clean_up(void);
176
177 AWS_HTTP_API
178 void aws_hpack_context_init(
179 struct aws_hpack_context *aws_hpack_context,
180 struct aws_allocator *allocator,
181 enum aws_http_log_subject log_subject,
182 const void *log_id);
183
184 AWS_HTTP_API
185 void aws_hpack_context_clean_up(struct aws_hpack_context *context);
186
187 /* Returns the hpack size of a header (name.len + value.len + 32) [4.1] */
188 AWS_HTTP_API
189 size_t aws_hpack_get_header_size(const struct aws_http_header *header);
190
191 /* Returns the number of elements in dynamic table now */
192 AWS_HTTP_API
193 size_t aws_hpack_get_dynamic_table_num_elements(const struct aws_hpack_context *context);
194
195 size_t aws_hpack_get_dynamic_table_max_size(const struct aws_hpack_context *context);
196
197 AWS_HTTP_API
198 const struct aws_http_header *aws_hpack_get_header(const struct aws_hpack_context *context, size_t index);
199
200 /* A return value of 0 indicates that the header wasn't found */
201 AWS_HTTP_API
202 size_t aws_hpack_find_index(
203 const struct aws_hpack_context *context,
204 const struct aws_http_header *header,
205 bool search_value,
206 bool *found_value);
207
208 AWS_HTTP_API
209 int aws_hpack_insert_header(struct aws_hpack_context *context, const struct aws_http_header *header);
210
211 /**
212 * Set the max size of the dynamic table (in octets). The size of each header is name.len + value.len + 32 [4.1].
213 */
214 AWS_HTTP_API
215 int aws_hpack_resize_dynamic_table(struct aws_hpack_context *context, size_t new_max_size);
216
217 AWS_HTTP_API
218 void aws_hpack_encoder_init(struct aws_hpack_encoder *encoder, struct aws_allocator *allocator, const void *log_id);
219
220 AWS_HTTP_API
221 void aws_hpack_encoder_clean_up(struct aws_hpack_encoder *encoder);
222
223 /* Call this after receiving SETTINGS_HEADER_TABLE_SIZE from peer and sending the ACK.
224 * The hpack-encoder remembers all size updates, and makes sure to encode the proper
225 * number of Dynamic Table Size Updates the next time a header block is sent. */
226 AWS_HTTP_API
227 void aws_hpack_encoder_update_max_table_size(struct aws_hpack_encoder *encoder, uint32_t new_max_size);
228
229 AWS_HTTP_API
230 void aws_hpack_encoder_set_huffman_mode(struct aws_hpack_encoder *encoder, enum aws_hpack_huffman_mode mode);
231
232 /**
233 * Encode header-block into the output.
234 * This function will mutate hpack, so an error means hpack can no longer be used.
235 * Note that output will be dynamically resized if it's too short.
236 */
237 AWS_HTTP_API
238 int aws_hpack_encode_header_block(
239 struct aws_hpack_encoder *encoder,
240 const struct aws_http_headers *headers,
241 struct aws_byte_buf *output);
242
243 AWS_HTTP_API
244 void aws_hpack_decoder_init(struct aws_hpack_decoder *decoder, struct aws_allocator *allocator, const void *log_id);
245
246 AWS_HTTP_API
247 void aws_hpack_decoder_clean_up(struct aws_hpack_decoder *decoder);
248
249 /* Call this after sending SETTINGS_HEADER_TABLE_SIZE and receiving ACK from the peer.
250 * The hpack-decoder remembers all size updates, and makes sure that the peer
251 * sends the appropriate Dynamic Table Size Updates in the next header block we receive. */
252 AWS_HTTP_API
253 void aws_hpack_decoder_update_max_table_size(struct aws_hpack_decoder *decoder, uint32_t new_max_size);
254
255 /**
256 * Decode the next entry in the header-block-fragment.
257 * If result->type is ONGOING, then call decode() again with more data to resume decoding.
258 * Otherwise, type is either a HEADER_FIELD or a DYNAMIC_TABLE_RESIZE.
259 *
260 * If an error occurs, the decoder is broken and decode() must not be called again.
261 */
262 AWS_HTTP_API
263 int aws_hpack_decode(
264 struct aws_hpack_decoder *decoder,
265 struct aws_byte_cursor *to_decode,
266 struct aws_hpack_decode_result *result);
267
268 /*******************************************************************************
269 * Private functions for encoder/decoder, but public for testing purposes
270 ******************************************************************************/
271
272 /* Output will be dynamically resized if it's too short */
273 AWS_HTTP_API
274 int aws_hpack_encode_integer(uint64_t integer, uint8_t starting_bits, uint8_t prefix_size, struct aws_byte_buf *output);
275
276 /* Output will be dynamically resized if it's too short */
277 AWS_HTTP_API
278 int aws_hpack_encode_string(
279 struct aws_hpack_encoder *encoder,
280 struct aws_byte_cursor to_encode,
281 struct aws_byte_buf *output);
282
283 AWS_HTTP_API
284 int aws_hpack_decode_integer(
285 struct aws_hpack_decoder *decoder,
286 struct aws_byte_cursor *to_decode,
287 uint8_t prefix_size,
288 uint64_t *integer,
289 bool *complete);
290
291 AWS_HTTP_API
292 int aws_hpack_decode_string(
293 struct aws_hpack_decoder *decoder,
294 struct aws_byte_cursor *to_decode,
295 struct aws_byte_buf *output,
296 bool *complete);
297
298 AWS_EXTERN_C_END
299
300 #endif /* AWS_HTTP_HPACK_H */