Debian Codegraph @

crt/aws-c-http/source/hpack_encoder.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 #define HPACK_LOGF(level, encoder, text, ...) \
8 AWS_LOGF_##level(AWS_LS_HTTP_ENCODER, "id=%p [HPACK]: " text, (encoder)->log_id, __VA_ARGS__)
9 #define HPACK_LOG(level, encoder, text) HPACK_LOGF(level, encoder, "%s", text)
10
11 struct aws_huffman_symbol_coder *hpack_get_coder(void);
12
13 void aws_hpack_encoder_init(struct aws_hpack_encoder *encoder, struct aws_allocator *allocator, const void *log_id) {
14
15 AWS_ZERO_STRUCT(*encoder);
16 encoder->log_id = log_id;
17
18 aws_huffman_encoder_init(&encoder->huffman_encoder, hpack_get_coder());
19
20 aws_hpack_context_init(&encoder->context, allocator, AWS_LS_HTTP_ENCODER, log_id);
21
22 encoder->dynamic_table_size_update.pending = false;
23 encoder->dynamic_table_size_update.latest_value = SIZE_MAX;
24 encoder->dynamic_table_size_update.smallest_value = SIZE_MAX;
25 }
26
27 void aws_hpack_encoder_clean_up(struct aws_hpack_encoder *encoder) {
28 aws_hpack_context_clean_up(&encoder->context);
29 AWS_ZERO_STRUCT(*encoder);
30 }
31
32 void aws_hpack_encoder_set_huffman_mode(struct aws_hpack_encoder *encoder, enum aws_hpack_huffman_mode mode) {
33 encoder->huffman_mode = mode;
34 }
35
36 void aws_hpack_encoder_update_max_table_size(struct aws_hpack_encoder *encoder, uint32_t new_max_size) {
37
38 if (!encoder->dynamic_table_size_update.pending) {
39 encoder->dynamic_table_size_update.pending = true;
40 }
41 encoder->dynamic_table_size_update.smallest_value =
42 aws_min_size(new_max_size, encoder->dynamic_table_size_update.smallest_value);
43
44 /* TODO: don't necessarily go as high as possible. The peer said the encoder's
45 * dynamic table COULD get this big, but it's not required to.
46 * It's probably not a good idea to let the peer decide how much memory we allocate.
47 * Not sure how to cap it though... Use a hardcoded number?
48 * Match whatever SETTINGS_HEADER_TABLE_SIZE this side sends? */
49 encoder->dynamic_table_size_update.latest_value = new_max_size;
50 }
51
52 /* Return a byte with the N right-most bits masked.
53 * Ex: 2 -> 00000011 */
54 static uint8_t s_masked_right_bits_u8(uint8_t num_masked_bits) {
55 AWS_ASSERT(num_masked_bits <= 8);
56 const uint8_t cut_bits = 8 - num_masked_bits;
57 return UINT8_MAX >> cut_bits;
58 }
59
60 /* If buffer isn't big enough, grow it intelligently */
61 static int s_ensure_space(struct aws_byte_buf *output, size_t required_space) {
62 size_t available_space = output->capacity - output->len;
63 if (required_space <= available_space) {
64 return AWS_OP_SUCCESS;
65 }
66
67 /* Capacity must grow to at least this size */
68 size_t required_capacity;
69 if (aws_add_size_checked(output->len, required_space, &required_capacity)) {
70 return AWS_OP_ERR;
71 }
72
73 /* Prefer to double capacity, but if that's not enough grow to exactly required_capacity */
74 size_t double_capacity = aws_add_size_saturating(output->capacity, output->capacity);
75 size_t reserve = aws_max_size(required_capacity, double_capacity);
76 return aws_byte_buf_reserve(output, reserve);
77 }
78
79 int aws_hpack_encode_integer(
80 uint64_t integer,
81 uint8_t starting_bits,
82 uint8_t prefix_size,
83 struct aws_byte_buf *output) {
84 AWS_ASSERT(prefix_size <= 8);
85
86 const uint8_t prefix_mask = s_masked_right_bits_u8(prefix_size);
87 AWS_ASSERT((starting_bits & prefix_mask) == 0);
88
89 const size_t original_len = output->len;
90
91 if (integer < prefix_mask) {
92 /* If the integer fits inside the specified number of bits but won't be all 1's, just write it */
93
94 /* Just write out the bits we care about */
95 uint8_t first_byte = starting_bits | (uint8_t)integer;
96 if (aws_byte_buf_append_byte_dynamic(output, first_byte)) {
97 goto error;
98 }
99 } else {
100 /* Set all of the bits in the first octet to 1 */
101 uint8_t first_byte = starting_bits | prefix_mask;
102 if (aws_byte_buf_append_byte_dynamic(output, first_byte)) {
103 goto error;
104 }
105
106 integer -= prefix_mask;
107
108 const uint64_t hi_57bit_mask = UINT64_MAX - (UINT8_MAX >> 1);
109
110 do {
111 /* Take top 7 bits from the integer */
112 uint8_t this_octet = integer % 128;
113 if (integer & hi_57bit_mask) {
114 /* If there's more after this octet, set the hi bit */
115 this_octet += 128;
116 }
117
118 if (aws_byte_buf_append_byte_dynamic(output, this_octet)) {
119 goto error;
120 }
121
122 /* Remove the written bits */
123 integer >>= 7;
124 } while (integer);
125 }
126
127 return AWS_OP_SUCCESS;
128 error:
129 output->len = original_len;
130 return AWS_OP_ERR;
131 }
132
133 int aws_hpack_encode_string(
134 struct aws_hpack_encoder *encoder,
135 struct aws_byte_cursor to_encode,
136 struct aws_byte_buf *output) {
137
138 AWS_PRECONDITION(encoder);
139 AWS_PRECONDITION(aws_byte_cursor_is_valid(&to_encode));
140 AWS_PRECONDITION(output);
141
142 const size_t original_len = output->len;
143
144 /* Determine length of encoded string (and whether or not to use huffman) */
145 uint8_t use_huffman;
146 size_t str_length;
147 switch (encoder->huffman_mode) {
148 case AWS_HPACK_HUFFMAN_NEVER:
149 use_huffman = 0;
150 str_length = to_encode.len;
151 break;
152
153 case AWS_HPACK_HUFFMAN_ALWAYS:
154 use_huffman = 1;
155 str_length = aws_huffman_get_encoded_length(&encoder->huffman_encoder, to_encode);
156 break;
157
158 case AWS_HPACK_HUFFMAN_SMALLEST:
159 str_length = aws_huffman_get_encoded_length(&encoder->huffman_encoder, to_encode);
160 if (str_length < to_encode.len) {
161 use_huffman = 1;
162 } else {
163 str_length = to_encode.len;
164 use_huffman = 0;
165 }
166 break;
167
168 default:
169 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
170 goto error;
171 }
172
173 /*
174 * String literals are encoded like so (RFC-7541 5.2):
175 * H is whether or not data is huffman-encoded.
176 *
177 * 0 1 2 3 4 5 6 7
178 * +---+---+---+---+---+---+---+---+
179 * | H | String Length (7+) |
180 * +---+---------------------------+
181 * | String Data (Length octets) |
182 * +-------------------------------+
183 */
184
185 /* Encode string length */
186 uint8_t starting_bits = use_huffman << 7;
187 if (aws_hpack_encode_integer(str_length, starting_bits, 7, output)) {
188 HPACK_LOGF(ERROR, encoder, "Error encoding HPACK integer: %s", aws_error_name(aws_last_error()));
189 goto error;
190 }
191
192 /* Encode string data */
193 if (str_length > 0) {
194 if (use_huffman) {
195 /* Huffman encoder doesn't grow buffer, so we ensure it's big enough here */
196 if (s_ensure_space(output, str_length)) {
197 goto error;
198 }
199
200 if (aws_huffman_encode(&encoder->huffman_encoder, &to_encode, output)) {
201 HPACK_LOGF(ERROR, encoder, "Error from Huffman encoder: %s", aws_error_name(aws_last_error()));
202 goto error;
203 }
204
205 } else {
206 if (aws_byte_buf_append_dynamic(output, &to_encode)) {
207 goto error;
208 }
209 }
210 }
211
212 return AWS_OP_SUCCESS;
213
214 error:
215 output->len = original_len;
216 aws_huffman_encoder_reset(&encoder->huffman_encoder);
217 return AWS_OP_ERR;
218 }
219
220 /* All types that HPACK might encode/decode (RFC-7541 6 - Binary Format) */
221 enum aws_hpack_entry_type {
222 AWS_HPACK_ENTRY_INDEXED_HEADER_FIELD, /* RFC-7541 6.1 */
223 AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITH_INCREMENTAL_INDEXING, /* RFC-7541 6.2.1 */
224 AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITHOUT_INDEXING, /* RFC-7541 6.2.2 */
225 AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_NEVER_INDEXED, /* RFC-7541 6.2.3 */
226 AWS_HPACK_ENTRY_DYNAMIC_TABLE_RESIZE, /* RFC-7541 6.3 */
227 AWS_HPACK_ENTRY_TYPE_COUNT,
228 };
229
230 /**
231 * First byte each entry type looks like this (RFC-7541 6):
232 * The "xxxxx" part is the "N-bit prefix" of the entry's first encoded integer.
233 *
234 * 1xxxxxxx: Indexed Header Field Representation
235 * 01xxxxxx: Literal Header Field with Incremental Indexing
236 * 001xxxxx: Dynamic Table Size Update
237 * 0001xxxx: Literal Header Field Never Indexed
238 * 0000xxxx: Literal Header Field without Indexing
239 */
240 static const uint8_t s_hpack_entry_starting_bit_pattern[AWS_HPACK_ENTRY_TYPE_COUNT] = {
241 [AWS_HPACK_ENTRY_INDEXED_HEADER_FIELD] = 1 << 7,
242 [AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITH_INCREMENTAL_INDEXING] = 1 << 6,
243 [AWS_HPACK_ENTRY_DYNAMIC_TABLE_RESIZE] = 1 << 5,
244 [AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_NEVER_INDEXED] = 1 << 4,
245 [AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITHOUT_INDEXING] = 0 << 4,
246 };
247
248 static const uint8_t s_hpack_entry_num_prefix_bits[AWS_HPACK_ENTRY_TYPE_COUNT] = {
249 [AWS_HPACK_ENTRY_INDEXED_HEADER_FIELD] = 7,
250 [AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITH_INCREMENTAL_INDEXING] = 6,
251 [AWS_HPACK_ENTRY_DYNAMIC_TABLE_RESIZE] = 5,
252 [AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_NEVER_INDEXED] = 4,
253 [AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITHOUT_INDEXING] = 4,
254 };
255
256 static int s_convert_http_compression_to_literal_entry_type(
257 enum aws_http_header_compression compression,
258 enum aws_hpack_entry_type *out_entry_type) {
259
260 switch (compression) {
261 case AWS_HTTP_HEADER_COMPRESSION_USE_CACHE:
262 *out_entry_type = AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITH_INCREMENTAL_INDEXING;
263 return AWS_OP_SUCCESS;
264
265 case AWS_HTTP_HEADER_COMPRESSION_NO_CACHE:
266 *out_entry_type = AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITHOUT_INDEXING;
267 return AWS_OP_SUCCESS;
268
269 case AWS_HTTP_HEADER_COMPRESSION_NO_FORWARD_CACHE:
270 *out_entry_type = AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_NEVER_INDEXED;
271 return AWS_OP_SUCCESS;
272 }
273
274 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
275 }
276
277 static int s_encode_header_field(
278 struct aws_hpack_encoder *encoder,
279 const struct aws_http_header *header,
280 struct aws_byte_buf *output) {
281
282 AWS_PRECONDITION(encoder);
283 AWS_PRECONDITION(header);
284 AWS_PRECONDITION(output);
285
286 size_t original_len = output->len;
287
288 /* Search for header-field in tables */
289 bool found_indexed_value;
290 size_t header_index = aws_hpack_find_index(&encoder->context, header, true, &found_indexed_value);
291
292 if (header->compression != AWS_HTTP_HEADER_COMPRESSION_USE_CACHE) {
293 /* If user doesn't want to use indexed value, then don't use it */
294 found_indexed_value = false;
295 }
296
297 if (header_index && found_indexed_value) {
298 /* Indexed header field */
299 const enum aws_hpack_entry_type entry_type = AWS_HPACK_ENTRY_INDEXED_HEADER_FIELD;
300
301 /* encode the one index (along with the entry type), and we're done! */
302 uint8_t starting_bit_pattern = s_hpack_entry_starting_bit_pattern[entry_type];
303 uint8_t num_prefix_bits = s_hpack_entry_num_prefix_bits[entry_type];
304 if (aws_hpack_encode_integer(header_index, starting_bit_pattern, num_prefix_bits, output)) {
305 goto error;
306 }
307
308 return AWS_OP_SUCCESS;
309 }
310
311 /* Else, Literal header field... */
312
313 /* determine exactly which type of literal header-field to encode. */
314 enum aws_hpack_entry_type literal_entry_type = AWS_HPACK_ENTRY_TYPE_COUNT;
315 if (s_convert_http_compression_to_literal_entry_type(header->compression, &literal_entry_type)) {
316 goto error;
317 }
318
319 /* the entry type makes up the first few bits of the next integer we encode */
320 uint8_t starting_bit_pattern = s_hpack_entry_starting_bit_pattern[literal_entry_type];
321 uint8_t num_prefix_bits = s_hpack_entry_num_prefix_bits[literal_entry_type];
322
323 if (header_index) {
324 /* Literal header field, indexed name */
325
326 /* first encode the index of name */
327 if (aws_hpack_encode_integer(header_index, starting_bit_pattern, num_prefix_bits, output)) {
328 goto error;
329 }
330 } else {
331 /* Literal header field, new name */
332
333 /* first encode index of 0 to indicate that header-name is not indexed */
334 if (aws_hpack_encode_integer(0, starting_bit_pattern, num_prefix_bits, output)) {
335 goto error;
336 }
337
338 /* next encode header-name string */
339 if (aws_hpack_encode_string(encoder, header->name, output)) {
340 goto error;
341 }
342 }
343
344 /* then encode header-value string, and we're done encoding! */
345 if (aws_hpack_encode_string(encoder, header->value, output)) {
346 goto error;
347 }
348
349 /* if "incremental indexing" type, insert header into the dynamic table. */
350 if (AWS_HPACK_ENTRY_LITERAL_HEADER_FIELD_WITH_INCREMENTAL_INDEXING == literal_entry_type) {
351 if (aws_hpack_insert_header(&encoder->context, header)) {
352 goto error;
353 }
354 }
355
356 return AWS_OP_SUCCESS;
357 error:
358 output->len = original_len;
359 return AWS_OP_ERR;
360 }
361
362 int aws_hpack_encode_header_block(
363 struct aws_hpack_encoder *encoder,
364 const struct aws_http_headers *headers,
365 struct aws_byte_buf *output) {
366
367 /* Encode a dynamic table size update at the beginning of the first header-block
368 * following the change to the dynamic table size RFC-7541 4.2 */
369 if (encoder->dynamic_table_size_update.pending) {
370 if (encoder->dynamic_table_size_update.smallest_value != encoder->dynamic_table_size_update.latest_value) {
371 size_t smallest_update_value = encoder->dynamic_table_size_update.smallest_value;
372 HPACK_LOGF(
373 TRACE, encoder, "Encoding smallest dynamic table size update entry size: %zu", smallest_update_value);
374 if (aws_hpack_resize_dynamic_table(&encoder->context, smallest_update_value)) {
375 HPACK_LOGF(ERROR, encoder, "Dynamic table resize failed, size: %zu", smallest_update_value);
376 return AWS_OP_ERR;
377 }
378 uint8_t starting_bit_pattern = s_hpack_entry_starting_bit_pattern[AWS_HPACK_ENTRY_DYNAMIC_TABLE_RESIZE];
379 uint8_t num_prefix_bits = s_hpack_entry_num_prefix_bits[AWS_HPACK_ENTRY_DYNAMIC_TABLE_RESIZE];
380 if (aws_hpack_encode_integer(smallest_update_value, starting_bit_pattern, num_prefix_bits, output)) {
381 HPACK_LOGF(
382 ERROR,
383 encoder,
384 "Integer encoding failed for table size update entry, integer: %zu",
385 smallest_update_value);
386 return AWS_OP_ERR;
387 }
388 }
389 size_t last_update_value = encoder->dynamic_table_size_update.latest_value;
390 HPACK_LOGF(TRACE, encoder, "Encoding last dynamic table size update entry size: %zu", last_update_value);
391 if (aws_hpack_resize_dynamic_table(&encoder->context, last_update_value)) {
392 HPACK_LOGF(ERROR, encoder, "Dynamic table resize failed, size: %zu", last_update_value);
393 return AWS_OP_ERR;
394 }
395 uint8_t starting_bit_pattern = s_hpack_entry_starting_bit_pattern[AWS_HPACK_ENTRY_DYNAMIC_TABLE_RESIZE];
396 uint8_t num_prefix_bits = s_hpack_entry_num_prefix_bits[AWS_HPACK_ENTRY_DYNAMIC_TABLE_RESIZE];
397 if (aws_hpack_encode_integer(last_update_value, starting_bit_pattern, num_prefix_bits, output)) {
398 HPACK_LOGF(
399 ERROR, encoder, "Integer encoding failed for table size update entry, integer: %zu", last_update_value);
400 return AWS_OP_ERR;
401 }
402
403 encoder->dynamic_table_size_update.pending = false;
404 encoder->dynamic_table_size_update.latest_value = SIZE_MAX;
405 encoder->dynamic_table_size_update.smallest_value = SIZE_MAX;
406 }
407
408 const size_t num_headers = aws_http_headers_count(headers);
409 for (size_t i = 0; i < num_headers; ++i) {
410 struct aws_http_header header;
411 aws_http_headers_get_index(headers, i, &header);
412 if (s_encode_header_field(encoder, &header, output)) {
413 return AWS_OP_ERR;
414 }
415 }
416
417 return AWS_OP_SUCCESS;
418 }