| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/http/private/hpack.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
const size_t s_hpack_dynamic_table_initial_size = 4096; |
| 13 |
const size_t s_hpack_dynamic_table_initial_elements = 512; |
| 14 |
|
| 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 |
|
| 47 |
static struct aws_hash_table s_static_header_reverse_lookup; |
| 48 |
|
| 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 |
|
| 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 |
|
| 92 |
for (size_t i = s_static_header_table_size - 1; i > 0; --i) { |
| 93 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 183 |
|
| 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 |
|
| 200 |
if (index < s_static_header_table_size) { |
| 201 |
return &s_static_header_table[index]; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
return s_dynamic_table_get(context, index - s_static_header_table_size); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 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 |
|
| 220 |
aws_hash_table_find(&s_static_header_reverse_lookup, header, &elem); |
| 221 |
if (elem) { |
| 222 |
|
| 223 |
|
| 224 |
*found_value = ((const struct aws_http_header *)elem->key)->value.len; |
| 225 |
return (size_t)elem->value; |
| 226 |
} |
| 227 |
|
| 228 |
aws_hash_table_find(&context->dynamic_table.reverse_lookup, header, &elem); |
| 229 |
if (elem) { |
| 230 |
|
| 231 |
*found_value = ((const struct aws_http_header *)elem->key)->value.len; |
| 232 |
goto trans_index_from_dynamic_table; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 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 |
|
| 257 |
index += s_static_header_table_size; |
| 258 |
return index; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 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 |
|
| 267 |
context->dynamic_table.size -= aws_hpack_get_header_size(back); |
| 268 |
context->dynamic_table.num_elements -= 1; |
| 269 |
|
| 270 |
|
| 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 |
|
| 277 |
|
| 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 |
|
| 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 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
static int s_dynamic_table_resize_buffer(struct aws_hpack_context *context, size_t new_max_elements) { |
| 303 |
|
| 304 |
|
| 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 |
|
| 312 |
goto cleanup_old_buffer; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 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 |
|
| 322 |
if (AWS_UNLIKELY(context->dynamic_table.num_elements == 0)) { |
| 323 |
goto reset_dyn_table_state; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 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 |
|
| 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 |
|
| 365 |
cleanup_old_buffer: |
| 366 |
aws_mem_release(context->allocator, context->dynamic_table.buffer); |
| 367 |
|
| 368 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 405 |
if (AWS_UNLIKELY(header_size > context->dynamic_table.max_size)) { |
| 406 |
|
| 407 |
goto error; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
if (s_dynamic_table_shrink(context, context->dynamic_table.max_size - header_size)) { |
| 413 |
goto error; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
if (context->dynamic_table.num_elements == context->dynamic_table.buffer_capacity) { |
| 418 |
|
| 419 |
const size_t new_size = |
| 420 |
context->dynamic_table.buffer_capacity |
| 421 |
|
| 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 |
|
| 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 |
|
| 438 |
context->dynamic_table.num_elements++; |
| 439 |
|
| 440 |
context->dynamic_table.size += header_size; |
| 441 |
|
| 442 |
|
| 443 |
struct aws_http_header *table_header = s_dynamic_table_get(context, 0); |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 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 |
|
| 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 |
|
| 462 |
*table_header = *header; |
| 463 |
table_header->name.ptr = NULL; |
| 464 |
table_header->value.ptr = NULL; |
| 465 |
} |
| 466 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 507 |
if (s_dynamic_table_shrink(context, new_max_size)) { |
| 508 |
goto error; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
if (s_dynamic_table_resize_buffer(context, context->dynamic_table.num_elements)) { |
| 513 |
goto error; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
context->dynamic_table.max_size = new_max_size; |
| 518 |
|
| 519 |
return AWS_OP_SUCCESS; |
| 520 |
|
| 521 |
error: |
| 522 |
return AWS_OP_ERR; |
| 523 |
} |