Debian Codegraph @

crt/aws-c-http/source/hpack.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/http/private/hpack.h>
6
7 /* #TODO test empty strings */
8
9 /* #TODO remove all OOM error handling in HTTP/2 & HPACK. make functions void if possible */
10
11 /* RFC-7540 6.5.2 */
12 const size_t s_hpack_dynamic_table_initial_size = 4096;
13 const size_t s_hpack_dynamic_table_initial_elements = 512;
14 /* TODO: shouldn't be a hardcoded max_size, it should be driven by SETTINGS_HEADER_TABLE_SIZE */
15 const size_t s_hpack_dynamic_table_max_size = 16 * 1024 * 1024;
16
17 struct aws_http_header s_static_header_table[] = {
18 #define HEADER(_index, _name) \
19 [_index] = { \
20 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_name), \
21 },
22
23 #define HEADER_WITH_VALUE(_index, _name, _value) \
24 [_index] = { \
25 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_name), \
26 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \
27 },
28
29 #include <aws/http/private/hpack_header_static_table.def>
30
31 #undef HEADER
32 #undef HEADER_WITH_VALUE
33 };
34 static const size_t s_static_header_table_size = AWS_ARRAY_SIZE(s_static_header_table);
35
36 struct aws_byte_cursor s_static_header_table_name_only[] = {
37 #define HEADER(_index, _name) [_index] = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_name),
38 #define HEADER_WITH_VALUE(_index, _name, _value) HEADER(_index, _name)
39
40 #include <aws/http/private/hpack_header_static_table.def>
41
42 #undef HEADER
43 #undef HEADER_WITH_VALUE
44 };
45
46 /* aws_http_header * -> size_t */
47 static struct aws_hash_table s_static_header_reverse_lookup;
48 /* aws_byte_cursor * -> size_t */
49 static struct aws_hash_table s_static_header_reverse_lookup_name_only;
50
51 static uint64_t s_header_hash(const void *key) {
52 const struct aws_http_header *header = key;
53
54 return aws_hash_combine(aws_hash_byte_cursor_ptr(&header->name), aws_hash_byte_cursor_ptr(&header->value));
55 }
56
57 static bool s_header_eq(const void *a, const void *b) {
58 const struct aws_http_header *left = a;
59 const struct aws_http_header *right = b;
60
61 if (!aws_byte_cursor_eq(&left->name, &right->name)) {
62 return false;
63 }
64
65 /* If the header stored in the table doesn't have a value, then it's a match */
66 return aws_byte_cursor_eq(&left->value, &right->value);
67 }
68
69 void aws_hpack_static_table_init(struct aws_allocator *allocator) {
70
71 int result = aws_hash_table_init(
72 &s_static_header_reverse_lookup,
73 allocator,
74 s_static_header_table_size - 1,
75 s_header_hash,
76 s_header_eq,
77 NULL,
78 NULL);
79 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == result);
80
81 result = aws_hash_table_init(
82 &s_static_header_reverse_lookup_name_only,
83 allocator,
84 s_static_header_table_size - 1,
85 aws_hash_byte_cursor_ptr,
86 (aws_hash_callback_eq_fn *)aws_byte_cursor_eq,
87 NULL,
88 NULL);
89 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == result);
90
91 /* Process in reverse so that name_only prefers lower indices */
92 for (size_t i = s_static_header_table_size - 1; i > 0; --i) {
93 /* the tables are created as 1-based indexing */
94 result = aws_hash_table_put(&s_static_header_reverse_lookup, &s_static_header_table[i], (void *)i, NULL);
95 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == result);
96
97 result = aws_hash_table_put(
98 &s_static_header_reverse_lookup_name_only, &s_static_header_table_name_only[i], (void *)(i), NULL);
99 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == result);
100 }
101 }
102
103 void aws_hpack_static_table_clean_up(void) {
104 aws_hash_table_clean_up(&s_static_header_reverse_lookup);
105 aws_hash_table_clean_up(&s_static_header_reverse_lookup_name_only);
106 }
107
108 #define HPACK_LOGF(level, hpack, text, ...) \
109 AWS_LOGF_##level((hpack)->log_subject, "id=%p [HPACK]: " text, (hpack)->log_id, __VA_ARGS__)
110 #define HPACK_LOG(level, hpack, text) HPACK_LOGF(level, hpack, "%s", text)
111
112 void aws_hpack_context_init(
113 struct aws_hpack_context *context,
114 struct aws_allocator *allocator,
115 enum aws_http_log_subject log_subject,
116 const void *log_id) {
117
118 AWS_ZERO_STRUCT(*context);
119 context->allocator = allocator;
120 context->log_subject = log_subject;
121 context->log_id = log_id;
122
123 /* Initialize dynamic table */
124 context->dynamic_table.max_size = s_hpack_dynamic_table_initial_size;
125 context->dynamic_table.buffer_capacity = s_hpack_dynamic_table_initial_elements;
126 context->dynamic_table.buffer =
127 aws_mem_calloc(allocator, context->dynamic_table.buffer_capacity, sizeof(struct aws_http_header));
128
129 aws_hash_table_init(
130 &context->dynamic_table.reverse_lookup,
131 allocator,
132 s_hpack_dynamic_table_initial_elements,
133 s_header_hash,
134 s_header_eq,
135 NULL,
136 NULL);
137
138 aws_hash_table_init(
139 &context->dynamic_table.reverse_lookup_name_only,
140 allocator,
141 s_hpack_dynamic_table_initial_elements,
142 aws_hash_byte_cursor_ptr,
143 (aws_hash_callback_eq_fn *)aws_byte_cursor_eq,
144 NULL,
145 NULL);
146 }
147
148 static struct aws_http_header *s_dynamic_table_get(const struct aws_hpack_context *context, size_t index);
149
150 static void s_clean_up_dynamic_table_buffer(struct aws_hpack_context *context) {
151 while (context->dynamic_table.num_elements > 0) {
152 struct aws_http_header *back = s_dynamic_table_get(context, context->dynamic_table.num_elements - 1);
153 context->dynamic_table.num_elements -= 1;
154 /* clean-up the memory we allocate for it */
155 aws_mem_release(context->allocator, back->name.ptr);
156 }
157 aws_mem_release(context->allocator, context->dynamic_table.buffer);
158 }
159
160 void aws_hpack_context_clean_up(struct aws_hpack_context *context) {
161 if (context->dynamic_table.buffer) {
162 s_clean_up_dynamic_table_buffer(context);
163 }
164 aws_hash_table_clean_up(&context->dynamic_table.reverse_lookup);
165 aws_hash_table_clean_up(&context->dynamic_table.reverse_lookup_name_only);
166 AWS_ZERO_STRUCT(*context);
167 }
168
169 size_t aws_hpack_get_header_size(const struct aws_http_header *header) {
170 return header->name.len + header->value.len + 32;
171 }
172
173 size_t aws_hpack_get_dynamic_table_num_elements(const struct aws_hpack_context *context) {
174 return context->dynamic_table.num_elements;
175 }
176
177 size_t aws_hpack_get_dynamic_table_max_size(const struct aws_hpack_context *context) {
178 return context->dynamic_table.max_size;
179 }
180
181 /*
182 * Gets the header from the dynamic table.
183 * NOTE: This function only bounds checks on the buffer size, not the number of elements.
184 */
185 static struct aws_http_header *s_dynamic_table_get(const struct aws_hpack_context *context, size_t index) {
186
187 AWS_ASSERT(index < context->dynamic_table.buffer_capacity);
188
189 return &context->dynamic_table
190 .buffer[(context->dynamic_table.index_0 + index) % context->dynamic_table.buffer_capacity];
191 }
192
193 const struct aws_http_header *aws_hpack_get_header(const struct aws_hpack_context *context, size_t index) {
194 if (index == 0 || index >= s_static_header_table_size + context->dynamic_table.num_elements) {
195 aws_raise_error(AWS_ERROR_INVALID_INDEX);
196 return NULL;
197 }
198
199 /* Check static table */
200 if (index < s_static_header_table_size) {
201 return &s_static_header_table[index];
202 }
203
204 /* Check dynamic table */
205 return s_dynamic_table_get(context, index - s_static_header_table_size);
206 }
207
208 /* TODO: remove `bool search_value`, this option has no reason to exist */
209 size_t aws_hpack_find_index(
210 const struct aws_hpack_context *context,
211 const struct aws_http_header *header,
212 bool search_value,
213 bool *found_value) {
214
215 *found_value = false;
216
217 struct aws_hash_element *elem = NULL;
218 if (search_value) {
219 /* Check name-and-value first in static table */
220 aws_hash_table_find(&s_static_header_reverse_lookup, header, &elem);
221 if (elem) {
222 /* TODO: Maybe always set found_value to true? Who cares that the value is empty if they matched? */
223 /* If an element was found, check if it has a value */
224 *found_value = ((const struct aws_http_header *)elem->key)->value.len;
225 return (size_t)elem->value;
226 }
227 /* Check name-and-value in dynamic table */
228 aws_hash_table_find(&context->dynamic_table.reverse_lookup, header, &elem);
229 if (elem) {
230 /* TODO: Maybe always set found_value to true? Who cares that the value is empty if they matched? */
231 *found_value = ((const struct aws_http_header *)elem->key)->value.len;
232 goto trans_index_from_dynamic_table;
233 }
234 }
235 /* Check the name-only table. Note, even if we search for value, when we fail in searching for name-and-value, we
236 * should also check the name only table */
237 aws_hash_table_find(&s_static_header_reverse_lookup_name_only, &header->name, &elem);
238 if (elem) {
239 return (size_t)elem->value;
240 }
241 aws_hash_table_find(&context->dynamic_table.reverse_lookup_name_only, &header->name, &elem);
242 if (elem) {
243 goto trans_index_from_dynamic_table;
244 }
245 return 0;
246
247 trans_index_from_dynamic_table:
248 AWS_ASSERT(elem);
249 size_t index;
250 const size_t absolute_index = (size_t)elem->value;
251 if (absolute_index >= context->dynamic_table.index_0) {
252 index = absolute_index - context->dynamic_table.index_0;
253 } else {
254 index = (context->dynamic_table.buffer_capacity - context->dynamic_table.index_0) + absolute_index;
255 }
256 /* Need to add the static table size to re-base indicies */
257 index += s_static_header_table_size;
258 return index;
259 }
260
261 /* Remove elements from the dynamic table until it fits in max_size bytes */
262 static int s_dynamic_table_shrink(struct aws_hpack_context *context, size_t max_size) {
263 while (context->dynamic_table.size > max_size && context->dynamic_table.num_elements > 0) {
264 struct aws_http_header *back = s_dynamic_table_get(context, context->dynamic_table.num_elements - 1);
265
266 /* "Remove" the header from the table */
267 context->dynamic_table.size -= aws_hpack_get_header_size(back);
268 context->dynamic_table.num_elements -= 1;
269
270 /* Remove old header from hash tables */
271 if (aws_hash_table_remove(&context->dynamic_table.reverse_lookup, back, NULL, NULL)) {
272 HPACK_LOG(ERROR, context, "Failed to remove header from the reverse lookup table");
273 goto error;
274 }
275
276 /* If the name-only lookup is pointing to the element we're removing, it needs to go.
277 * If not, it's pointing to a younger, sexier element. */
278 struct aws_hash_element *elem = NULL;
279 aws_hash_table_find(&context->dynamic_table.reverse_lookup_name_only, &back->name, &elem);
280 if (elem && elem->key == back) {
281 if (aws_hash_table_remove_element(&context->dynamic_table.reverse_lookup_name_only, elem)) {
282 HPACK_LOG(ERROR, context, "Failed to remove header from the reverse lookup (name-only) table");
283 goto error;
284 }
285 }
286
287 /* clean up the memory we allocated to hold the name and value string*/
288 aws_mem_release(context->allocator, back->name.ptr);
289 }
290
291 return AWS_OP_SUCCESS;
292
293 error:
294 return AWS_OP_ERR;
295 }
296
297 /*
298 * Resizes the dynamic table storage buffer to new_max_elements.
299 * Useful when inserting over capacity, or when downsizing.
300 * Do shrink first, if you want to remove elements, or memory leak will happen.
301 */
302 static int s_dynamic_table_resize_buffer(struct aws_hpack_context *context, size_t new_max_elements) {
303
304 /* Clear the old hash tables */
305 aws_hash_table_clear(&context->dynamic_table.reverse_lookup);
306 aws_hash_table_clear(&context->dynamic_table.reverse_lookup_name_only);
307
308 struct aws_http_header *new_buffer = NULL;
309
310 if (AWS_UNLIKELY(new_max_elements == 0)) {
311 /* If new buffer is of size 0, don't both initializing, just clean up the old one. */
312 goto cleanup_old_buffer;
313 }
314
315 /* Allocate the new buffer */
316 new_buffer = aws_mem_calloc(context->allocator, new_max_elements, sizeof(struct aws_http_header));
317 if (!new_buffer) {
318 return AWS_OP_ERR;
319 }
320
321 /* Don't bother copying data if old buffer was of size 0 */
322 if (AWS_UNLIKELY(context->dynamic_table.num_elements == 0)) {
323 goto reset_dyn_table_state;
324 }
325
326 /*
327 * Take a buffer that looks like this:
328 *
329 * Index 0
330 * ^
331 * +---------------------------+
332 * | Below Block | Above Block |
333 * +---------------------------+
334 * And make it look like this:
335 *
336 * Index 0
337 * ^
338 * +-------------+-------------+
339 * | Above Block | Below Block |
340 * +-------------+-------------+
341 */
342
343 /* Copy as much the above block as possible */
344 size_t above_block_size = context->dynamic_table.buffer_capacity - context->dynamic_table.index_0;
345 if (above_block_size > new_max_elements) {
346 above_block_size = new_max_elements;
347 }
348 memcpy(
349 new_buffer,
350 context->dynamic_table.buffer + context->dynamic_table.index_0,
351 above_block_size * sizeof(struct aws_http_header));
352
353 /* Copy as much of below block as possible */
354 const size_t free_blocks_available = new_max_elements - above_block_size;
355 const size_t old_blocks_to_copy = context->dynamic_table.buffer_capacity - above_block_size;
356 const size_t below_block_size = aws_min_size(free_blocks_available, old_blocks_to_copy);
357 if (below_block_size) {
358 memcpy(
359 new_buffer + above_block_size,
360 context->dynamic_table.buffer,
361 below_block_size * sizeof(struct aws_http_header));
362 }
363
364 /* Free the old memory */
365 cleanup_old_buffer:
366 aws_mem_release(context->allocator, context->dynamic_table.buffer);
367
368 /* Reset state */
369 reset_dyn_table_state:
370 if (context->dynamic_table.num_elements > new_max_elements) {
371 context->dynamic_table.num_elements = new_max_elements;
372 }
373 context->dynamic_table.buffer_capacity = new_max_elements;
374 context->dynamic_table.index_0 = 0;
375 context->dynamic_table.buffer = new_buffer;
376
377 /* Re-insert all of the reverse lookup elements */
378 for (size_t i = 0; i < context->dynamic_table.num_elements; ++i) {
379 if (aws_hash_table_put(
380 &context->dynamic_table.reverse_lookup, &context->dynamic_table.buffer[i], (void *)i, NULL)) {
381 return AWS_OP_ERR;
382 }
383 if (aws_hash_table_put(
384 &context->dynamic_table.reverse_lookup_name_only,
385 &context->dynamic_table.buffer[i].name,
386 (void *)i,
387 NULL)) {
388 return AWS_OP_ERR;
389 }
390 }
391
392 return AWS_OP_SUCCESS;
393 }
394
395 int aws_hpack_insert_header(struct aws_hpack_context *context, const struct aws_http_header *header) {
396
397 /* Don't move forward if no elements allowed in the dynamic table */
398 if (AWS_UNLIKELY(context->dynamic_table.max_size == 0)) {
399 return AWS_OP_SUCCESS;
400 }
401
402 const size_t header_size = aws_hpack_get_header_size(header);
403
404 /* If for whatever reason this new header is bigger than the total table size, burn everything to the ground. */
405 if (AWS_UNLIKELY(header_size > context->dynamic_table.max_size)) {
406 /* #TODO handle this. It's not an error. It should simply result in an empty table RFC-7541 4.4 */
407 goto error;
408 }
409
410 /* Rotate out headers until there's room for the new header (this function will return immediately if nothing needs
411 * to be evicted) */
412 if (s_dynamic_table_shrink(context, context->dynamic_table.max_size - header_size)) {
413 goto error;
414 }
415
416 /* If we're out of space in the buffer, grow it */
417 if (context->dynamic_table.num_elements == context->dynamic_table.buffer_capacity) {
418 /* If the buffer is currently of 0 size, reset it back to its initial size */
419 const size_t new_size =
420 context->dynamic_table.buffer_capacity
421 /* increase buffer by 1.5 rounded up. */
422 ? (size_t)(context->dynamic_table.buffer_capacity + ((context->dynamic_table.buffer_capacity + 1) / 2))
423 : s_hpack_dynamic_table_initial_elements;
424
425 if (s_dynamic_table_resize_buffer(context, new_size)) {
426 goto error;
427 }
428 }
429
430 /* Decrement index 0, wrapping if necessary */
431 if (context->dynamic_table.index_0 == 0) {
432 context->dynamic_table.index_0 = context->dynamic_table.buffer_capacity - 1;
433 } else {
434 context->dynamic_table.index_0--;
435 }
436
437 /* Increment num_elements */
438 context->dynamic_table.num_elements++;
439 /* Increment the size */
440 context->dynamic_table.size += header_size;
441
442 /* Put the header at the "front" of the table */
443 struct aws_http_header *table_header = s_dynamic_table_get(context, 0);
444
445 /* TODO:: We can optimize this with ring buffer. */
446 /* allocate memory for the name and value, which will be deallocated whenever the entry is evicted from the table or
447 * the table is cleaned up. We keep the pointer in the name pointer of each entry */
448 const size_t buf_memory_size = header->name.len + header->value.len;
449
450 if (buf_memory_size) {
451 uint8_t *buf_memory = aws_mem_acquire(context->allocator, buf_memory_size);
452 if (!buf_memory) {
453 return AWS_OP_ERR;
454 }
455 struct aws_byte_buf buf = aws_byte_buf_from_empty_array(buf_memory, buf_memory_size);
456 /* Copy header, then backup strings into our own allocation */
457 *table_header = *header;
458 aws_byte_buf_append_and_update(&buf, &table_header->name);
459 aws_byte_buf_append_and_update(&buf, &table_header->value);
460 } else {
461 /* if buf_memory_size is 0, no memory needed, we will insert the empty header into dynamic table */
462 *table_header = *header;
463 table_header->name.ptr = NULL;
464 table_header->value.ptr = NULL;
465 }
466 /* Write the new header to the look up tables */
467 if (aws_hash_table_put(
468 &context->dynamic_table.reverse_lookup, table_header, (void *)context->dynamic_table.index_0, NULL)) {
469 goto error;
470 }
471 /* Note that we can just blindly put here, we want to overwrite any older entry so it isn't accidentally removed. */
472 if (aws_hash_table_put(
473 &context->dynamic_table.reverse_lookup_name_only,
474 &table_header->name,
475 (void *)context->dynamic_table.index_0,
476 NULL)) {
477 goto error;
478 }
479
480 return AWS_OP_SUCCESS;
481
482 error:
483 /* Do not attempt to handle the error, if something goes wrong, close the connection */
484 return AWS_OP_ERR;
485 }
486
487 int aws_hpack_resize_dynamic_table(struct aws_hpack_context *context, size_t new_max_size) {
488
489 /* Nothing to see here! */
490 if (new_max_size == context->dynamic_table.max_size) {
491 return AWS_OP_SUCCESS;
492 }
493
494 if (new_max_size > s_hpack_dynamic_table_max_size) {
495
496 HPACK_LOGF(
497 ERROR,
498 context,
499 "New dynamic table max size %zu is greater than the supported max size (%zu)",
500 new_max_size,
501 s_hpack_dynamic_table_max_size);
502 aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
503 goto error;
504 }
505
506 /* If downsizing, remove elements until we're within the new size constraints */
507 if (s_dynamic_table_shrink(context, new_max_size)) {
508 goto error;
509 }
510
511 /* Resize the buffer to the current size */
512 if (s_dynamic_table_resize_buffer(context, context->dynamic_table.num_elements)) {
513 goto error;
514 }
515
516 /* Update the max size */
517 context->dynamic_table.max_size = new_max_size;
518
519 return AWS_OP_SUCCESS;
520
521 error:
522 return AWS_OP_ERR;
523 }