Debian Codegraph @

crt/aws-c-http/source/h2_decoder.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/h2_decoder.h>
6
7 #include <aws/http/private/hpack.h>
8 #include <aws/http/private/strutil.h>
9
10 #include <aws/common/string.h>
11 #include <aws/http/status_code.h>
12 #include <aws/io/logging.h>
13
14 #include <inttypes.h>
15
16 #ifdef _MSC_VER
17 # pragma warning(disable : 4204) /* Declared initializers */
18 #endif
19
20 /***********************************************************************************************************************
21 * Constants
22 **********************************************************************************************************************/
23
24 /* The scratch buffers data for states with bytes_required > 0. Must be big enough for largest state */
25 static const size_t s_scratch_space_size = 9;
26
27 /* Stream ids & dependencies should only write the bottom 31 bits */
28 static const uint32_t s_31_bit_mask = UINT32_MAX >> 1;
29
30 /* initial size for cookie buffer, buffer will grow if needed */
31 static const size_t s_decoder_cookie_buffer_initial_size = 512;
32
33 #define DECODER_LOGF(level, decoder, text, ...) \
34 AWS_LOGF_##level(AWS_LS_HTTP_DECODER, "id=%p " text, (decoder)->logging_id, __VA_ARGS__)
35 #define DECODER_LOG(level, decoder, text) DECODER_LOGF(level, decoder, "%s", text)
36
37 #define DECODER_CALL_VTABLE(decoder, fn) \
38 do { \
39 if ((decoder)->vtable->fn) { \
40 DECODER_LOG(TRACE, decoder, "Invoking callback " #fn); \
41 struct aws_h2err vtable_err = (decoder)->vtable->fn((decoder)->userdata); \
42 if (aws_h2err_failed(vtable_err)) { \
43 DECODER_LOGF( \
44 ERROR, \
45 decoder, \
46 "Error from callback " #fn ", %s->%s", \
47 aws_http2_error_code_to_str(vtable_err.h2_code), \
48 aws_error_name(vtable_err.aws_code)); \
49 return vtable_err; \
50 } \
51 } \
52 } while (false)
53 #define DECODER_CALL_VTABLE_ARGS(decoder, fn, ...) \
54 do { \
55 if ((decoder)->vtable->fn) { \
56 DECODER_LOG(TRACE, decoder, "Invoking callback " #fn); \
57 struct aws_h2err vtable_err = (decoder)->vtable->fn(__VA_ARGS__, (decoder)->userdata); \
58 if (aws_h2err_failed(vtable_err)) { \
59 DECODER_LOGF( \
60 ERROR, \
61 decoder, \
62 "Error from callback " #fn ", %s->%s", \
63 aws_http2_error_code_to_str(vtable_err.h2_code), \
64 aws_error_name(vtable_err.aws_code)); \
65 return vtable_err; \
66 } \
67 } \
68 } while (false)
69 #define DECODER_CALL_VTABLE_STREAM(decoder, fn) \
70 DECODER_CALL_VTABLE_ARGS(decoder, fn, (decoder)->frame_in_progress.stream_id)
71 #define DECODER_CALL_VTABLE_STREAM_ARGS(decoder, fn, ...) \
72 DECODER_CALL_VTABLE_ARGS(decoder, fn, (decoder)->frame_in_progress.stream_id, __VA_ARGS__)
73
74 /* for storing things in array without worrying about the specific values of the other AWS_HTTP_HEADER_XYZ enums */
75 enum pseudoheader_name {
76 PSEUDOHEADER_UNKNOWN = -1, /* Unrecognized value */
77
78 /* Request pseudo-headers */
79 PSEUDOHEADER_METHOD,
80 PSEUDOHEADER_SCHEME,
81 PSEUDOHEADER_AUTHORITY,
82 PSEUDOHEADER_PATH,
83 /* Response pseudo-headers */
84 PSEUDOHEADER_STATUS,
85
86 PSEUDOHEADER_COUNT, /* Number of valid enums */
87 };
88
89 static const struct aws_byte_cursor *s_pseudoheader_name_to_cursor[PSEUDOHEADER_COUNT] = {
90 [PSEUDOHEADER_METHOD] = &aws_http_header_method,
91 [PSEUDOHEADER_SCHEME] = &aws_http_header_scheme,
92 [PSEUDOHEADER_AUTHORITY] = &aws_http_header_authority,
93 [PSEUDOHEADER_PATH] = &aws_http_header_path,
94 [PSEUDOHEADER_STATUS] = &aws_http_header_status,
95 };
96
97 static const enum aws_http_header_name s_pseudoheader_to_header_name[PSEUDOHEADER_COUNT] = {
98 [PSEUDOHEADER_METHOD] = AWS_HTTP_HEADER_METHOD,
99 [PSEUDOHEADER_SCHEME] = AWS_HTTP_HEADER_SCHEME,
100 [PSEUDOHEADER_AUTHORITY] = AWS_HTTP_HEADER_AUTHORITY,
101 [PSEUDOHEADER_PATH] = AWS_HTTP_HEADER_PATH,
102 [PSEUDOHEADER_STATUS] = AWS_HTTP_HEADER_STATUS,
103 };
104
105 static enum pseudoheader_name s_header_to_pseudoheader_name(enum aws_http_header_name name) {
106 /* The compiled switch statement is actually faster than array lookup with bounds-checking.
107 * (the lookup arrays above don't need to do bounds-checking) */
108 switch (name) {
109 case AWS_HTTP_HEADER_METHOD:
110 return PSEUDOHEADER_METHOD;
111 case AWS_HTTP_HEADER_SCHEME:
112 return PSEUDOHEADER_SCHEME;
113 case AWS_HTTP_HEADER_AUTHORITY:
114 return PSEUDOHEADER_AUTHORITY;
115 case AWS_HTTP_HEADER_PATH:
116 return PSEUDOHEADER_PATH;
117 case AWS_HTTP_HEADER_STATUS:
118 return PSEUDOHEADER_STATUS;
119 default:
120 return PSEUDOHEADER_UNKNOWN;
121 }
122 }
123
124 /***********************************************************************************************************************
125 * State Machine
126 **********************************************************************************************************************/
127
128 typedef struct aws_h2err(state_fn)(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input);
129 struct h2_decoder_state {
130 state_fn *fn;
131 uint32_t bytes_required;
132 const char *name;
133 };
134
135 #define DEFINE_STATE(_name, _bytes_required) \
136 static state_fn s_state_fn_##_name; \
137 enum { s_state_##_name##_requires_##_bytes_required##_bytes = _bytes_required }; \
138 static const struct h2_decoder_state s_state_##_name = { \
139 .fn = s_state_fn_##_name, \
140 .bytes_required = s_state_##_name##_requires_##_bytes_required##_bytes, \
141 .name = #_name, \
142 }
143
144 /* Common states */
145 DEFINE_STATE(prefix, 9);
146 DEFINE_STATE(padding_len, 1);
147 DEFINE_STATE(padding, 0);
148
149 DEFINE_STATE(priority_block, 5);
150
151 DEFINE_STATE(header_block_loop, 0);
152 DEFINE_STATE(header_block_entry, 1); /* requires 1 byte, but may consume more */
153
154 /* Frame-specific states */
155 DEFINE_STATE(frame_data, 0);
156 DEFINE_STATE(frame_headers, 0);
157 DEFINE_STATE(frame_priority, 0);
158 DEFINE_STATE(frame_rst_stream, 4);
159 DEFINE_STATE(frame_settings_begin, 0);
160 DEFINE_STATE(frame_settings_loop, 0);
161 DEFINE_STATE(frame_settings_i, 6);
162 DEFINE_STATE(frame_push_promise, 4);
163 DEFINE_STATE(frame_ping, 8);
164 DEFINE_STATE(frame_goaway, 8);
165 DEFINE_STATE(frame_goaway_debug_data, 0);
166 DEFINE_STATE(frame_window_update, 4);
167 DEFINE_STATE(frame_continuation, 0);
168 DEFINE_STATE(frame_unknown, 0);
169
170 /* States that have nothing to do with frames */
171 DEFINE_STATE(connection_preface_string, 1); /* requires 1 byte but may consume more */
172
173 /* Helper for states that need to transition to frame-type states */
174 static const struct h2_decoder_state *s_state_frames[AWS_H2_FRAME_TYPE_COUNT] = {
175 [AWS_H2_FRAME_T_DATA] = &s_state_frame_data,
176 [AWS_H2_FRAME_T_HEADERS] = &s_state_frame_headers,
177 [AWS_H2_FRAME_T_PRIORITY] = &s_state_frame_priority,
178 [AWS_H2_FRAME_T_RST_STREAM] = &s_state_frame_rst_stream,
179 [AWS_H2_FRAME_T_SETTINGS] = &s_state_frame_settings_begin,
180 [AWS_H2_FRAME_T_PUSH_PROMISE] = &s_state_frame_push_promise,
181 [AWS_H2_FRAME_T_PING] = &s_state_frame_ping,
182 [AWS_H2_FRAME_T_GOAWAY] = &s_state_frame_goaway,
183 [AWS_H2_FRAME_T_WINDOW_UPDATE] = &s_state_frame_window_update,
184 [AWS_H2_FRAME_T_CONTINUATION] = &s_state_frame_continuation,
185 [AWS_H2_FRAME_T_UNKNOWN] = &s_state_frame_unknown,
186 };
187
188 /***********************************************************************************************************************
189 * Struct
190 **********************************************************************************************************************/
191
192 struct aws_h2_decoder {
193 /* Implementation data. */
194 struct aws_allocator *alloc;
195 const void *logging_id;
196 struct aws_hpack_decoder hpack;
197 bool is_server;
198 struct aws_byte_buf scratch;
199 const struct h2_decoder_state *state;
200 bool state_changed;
201
202 /* HTTP/2 connection preface must be first thing received (RFC-7540 3.5):
203 * Server must receive (client must send): magic string, then SETTINGS frame.
204 * Client must receive (server must send): SETTINGS frame. */
205 bool connection_preface_complete;
206
207 /* Cursor over the canonical client connection preface string */
208 struct aws_byte_cursor connection_preface_cursor;
209
210 /* Frame-in-progress */
211 struct aws_frame_in_progress {
212 enum aws_h2_frame_type type;
213 uint32_t stream_id;
214 uint32_t payload_len;
215 uint8_t padding_len;
216
217 struct {
218 bool ack;
219 bool end_stream;
220 bool end_headers;
221 bool priority;
222 } flags;
223 } frame_in_progress;
224
225 /* GOAWAY buffer */
226 struct aws_goaway_in_progress {
227 uint32_t last_stream;
228 uint32_t error_code;
229 /* Buffer of the received debug data in the latest goaway frame */
230 struct aws_byte_buf debug_data;
231 } goaway_in_progress;
232
233 /* A header-block starts with a HEADERS or PUSH_PROMISE frame, followed by 0 or more CONTINUATION frames.
234 * It's an error for any other frame-type or stream ID to arrive while a header-block is in progress.
235 * The header-block ends when a frame has the END_HEADERS flag set. (RFC-7540 4.3) */
236 struct aws_header_block_in_progress {
237 /* If 0, then no header-block in progress */
238 uint32_t stream_id;
239
240 /* Whether these are informational (1xx), normal, or trailing headers */
241 enum aws_http_header_block block_type;
242
243 /* Buffer up pseudo-headers and deliver them once they're all validated */
244 struct aws_string *pseudoheader_values[PSEUDOHEADER_COUNT];
245 enum aws_http_header_compression pseudoheader_compression[PSEUDOHEADER_COUNT];
246
247 /* All pseudo-header fields MUST appear in the header block before regular header fields. */
248 bool pseudoheaders_done;
249
250 /* T: PUSH_PROMISE header-block
251 * F: HEADERS header-block */
252 bool is_push_promise;
253
254 /* If frame that starts header-block has END_STREAM flag,
255 * then frame that ends header-block also ends the stream. */
256 bool ends_stream;
257
258 /* True if something occurs that makes the header-block malformed (ex: invalid header name).
259 * A malformed header-block is not a connection error, it's a Stream Error (RFC-7540 5.4.2).
260 * We continue decoding and report that it's malformed in on_headers_end(). */
261 bool malformed;
262
263 bool body_headers_forbidden;
264
265 /* Buffer up cookie header fields to concatenate separate ones */
266 struct aws_byte_buf cookies;
267 /* If separate cookie fields have different compression types, the concatenated cookie uses the strictest type.
268 */
269 enum aws_http_header_compression cookie_header_compression_type;
270 } header_block_in_progress;
271
272 /* Settings for decoder, which is based on the settings sent to the peer and ACKed by peer */
273 struct {
274 /* enable/disable server push */
275 uint32_t enable_push;
276 /* the size of the largest frame payload */
277 uint32_t max_frame_size;
278 } settings;
279
280 struct aws_array_list settings_buffer_list;
281
282 /* User callbacks and settings. */
283 const struct aws_h2_decoder_vtable *vtable;
284 void *userdata;
285
286 /* If this is set to true, decode may no longer be called */
287 bool has_errored;
288 };
289
290 /***********************************************************************************************************************/
291
292 struct aws_h2_decoder *aws_h2_decoder_new(struct aws_h2_decoder_params *params) {
293 AWS_PRECONDITION(params);
294 AWS_PRECONDITION(params->alloc);
295 AWS_PRECONDITION(params->vtable);
296
297 struct aws_h2_decoder *decoder = NULL;
298 void *scratch_buf = NULL;
299
300 void *allocation = aws_mem_acquire_many(
301 params->alloc, 2, &decoder, sizeof(struct aws_h2_decoder), &scratch_buf, s_scratch_space_size);
302 if (!allocation) {
303 goto error;
304 }
305
306 AWS_ZERO_STRUCT(*decoder);
307 decoder->alloc = params->alloc;
308 decoder->vtable = params->vtable;
309 decoder->userdata = params->userdata;
310 decoder->logging_id = params->logging_id;
311 decoder->is_server = params->is_server;
312 decoder->connection_preface_complete = params->skip_connection_preface;
313
314 decoder->scratch = aws_byte_buf_from_empty_array(scratch_buf, s_scratch_space_size);
315
316 aws_hpack_decoder_init(&decoder->hpack, params->alloc, decoder);
317
318 if (decoder->is_server && !params->skip_connection_preface) {
319 decoder->state = &s_state_connection_preface_string;
320 decoder->connection_preface_cursor = aws_h2_connection_preface_client_string;
321 } else {
322 decoder->state = &s_state_prefix;
323 }
324
325 decoder->settings.enable_push = aws_h2_settings_initial[AWS_HTTP2_SETTINGS_ENABLE_PUSH];
326 decoder->settings.max_frame_size = aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE];
327
328 if (aws_array_list_init_dynamic(
329 &decoder->settings_buffer_list, decoder->alloc, 0, sizeof(struct aws_http2_setting))) {
330 goto error;
331 }
332
333 if (aws_byte_buf_init(
334 &decoder->header_block_in_progress.cookies, decoder->alloc, s_decoder_cookie_buffer_initial_size)) {
335 goto error;
336 }
337
338 return decoder;
339
340 error:
341 if (decoder) {
342 aws_hpack_decoder_clean_up(&decoder->hpack);
343 aws_array_list_clean_up(&decoder->settings_buffer_list);
344 aws_byte_buf_clean_up(&decoder->header_block_in_progress.cookies);
345 }
346 aws_mem_release(params->alloc, allocation);
347 return NULL;
348 }
349
350 static void s_reset_header_block_in_progress(struct aws_h2_decoder *decoder) {
351 for (size_t i = 0; i < PSEUDOHEADER_COUNT; ++i) {
352 aws_string_destroy(decoder->header_block_in_progress.pseudoheader_values[i]);
353 }
354 struct aws_byte_buf cookie_backup = decoder->header_block_in_progress.cookies;
355 AWS_ZERO_STRUCT(decoder->header_block_in_progress);
356 decoder->header_block_in_progress.cookies = cookie_backup;
357 aws_byte_buf_reset(&decoder->header_block_in_progress.cookies, false);
358 }
359
360 void aws_h2_decoder_destroy(struct aws_h2_decoder *decoder) {
361 if (!decoder) {
362 return;
363 }
364 aws_array_list_clean_up(&decoder->settings_buffer_list);
365 aws_hpack_decoder_clean_up(&decoder->hpack);
366 s_reset_header_block_in_progress(decoder);
367 aws_byte_buf_clean_up(&decoder->header_block_in_progress.cookies);
368 aws_byte_buf_clean_up(&decoder->goaway_in_progress.debug_data);
369 aws_mem_release(decoder->alloc, decoder);
370 }
371
372 struct aws_h2err aws_h2_decode(struct aws_h2_decoder *decoder, struct aws_byte_cursor *data) {
373 AWS_PRECONDITION(decoder);
374 AWS_PRECONDITION(data);
375
376 AWS_FATAL_ASSERT(!decoder->has_errored);
377
378 struct aws_h2err err = AWS_H2ERR_SUCCESS;
379
380 /* Run decoder state machine until we're no longer changing states.
381 * We don't simply loop `while(data->len)` because some states consume no data,
382 * and these states should run even when there is no data left. */
383 do {
384 decoder->state_changed = false;
385
386 const uint32_t bytes_required = decoder->state->bytes_required;
387 AWS_ASSERT(bytes_required <= decoder->scratch.capacity);
388 const char *current_state_name = decoder->state->name;
389 const size_t prev_data_len = data->len;
390 (void)prev_data_len;
391
392 if (!decoder->scratch.len && data->len >= bytes_required) {
393 /* Easy case, there is no scratch and we have enough data, so just send it to the state */
394
395 DECODER_LOGF(TRACE, decoder, "Running state '%s' with %zu bytes available", current_state_name, data->len);
396
397 err = decoder->state->fn(decoder, data);
398 if (aws_h2err_failed(err)) {
399 goto handle_error;
400 }
401
402 AWS_ASSERT(prev_data_len - data->len >= bytes_required && "Decoder state requested more data than it used");
403 } else {
404 /* Otherwise, state requires a minimum amount of data and we have to use the scratch */
405 size_t bytes_to_read = bytes_required - decoder->scratch.len;
406 bool will_finish_state = true;
407
408 if (bytes_to_read > data->len) {
409 /* Not enough in this cursor, need to read as much as possible and then come back */
410 bytes_to_read = data->len;
411 will_finish_state = false;
412 }
413
414 if (AWS_LIKELY(bytes_to_read)) {
415 /* Read the appropriate number of bytes into scratch */
416 struct aws_byte_cursor to_read = aws_byte_cursor_advance(data, bytes_to_read);
417 bool succ = aws_byte_buf_write_from_whole_cursor(&decoder->scratch, to_read);
418 AWS_ASSERT(succ);
419 (void)succ;
420 }
421
422 /* If we have the correct number of bytes, call the state */
423 if (will_finish_state) {
424
425 DECODER_LOGF(TRACE, decoder, "Running state '%s' (using scratch)", current_state_name);
426
427 struct aws_byte_cursor state_data = aws_byte_cursor_from_buf(&decoder->scratch);
428 err = decoder->state->fn(decoder, &state_data);
429 if (aws_h2err_failed(err)) {
430 goto handle_error;
431 }
432
433 AWS_ASSERT(state_data.len == 0 && "Decoder state requested more data than it used");
434 } else {
435 DECODER_LOGF(
436 TRACE,
437 decoder,
438 "State '%s' requires %" PRIu32 " bytes, but only %zu available, trying again later",
439 current_state_name,
440 bytes_required,
441 decoder->scratch.len);
442 }
443 }
444 } while (decoder->state_changed);
445
446 return AWS_H2ERR_SUCCESS;
447
448 handle_error:
449 decoder->has_errored = true;
450 return err;
451 }
452
453 /***********************************************************************************************************************
454 * State functions
455 **********************************************************************************************************************/
456
457 static struct aws_h2err s_decoder_switch_state(struct aws_h2_decoder *decoder, const struct h2_decoder_state *state) {
458 /* Ensure payload is big enough to enter next state.
459 * If this fails, then the payload length we received is too small for this frame type.
460 * (ex: a RST_STREAM frame with < 4 bytes) */
461 if (decoder->frame_in_progress.payload_len < state->bytes_required) {
462 DECODER_LOGF(
463 ERROR, decoder, "%s payload is too small", aws_h2_frame_type_to_str(decoder->frame_in_progress.type));
464 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_FRAME_SIZE_ERROR);
465 }
466
467 DECODER_LOGF(TRACE, decoder, "Moving from state '%s' to '%s'", decoder->state->name, state->name);
468 decoder->scratch.len = 0;
469 decoder->state = state;
470 decoder->state_changed = true;
471 return AWS_H2ERR_SUCCESS;
472 }
473
474 static struct aws_h2err s_decoder_switch_to_frame_state(struct aws_h2_decoder *decoder) {
475 AWS_ASSERT(decoder->frame_in_progress.type < AWS_H2_FRAME_TYPE_COUNT);
476 return s_decoder_switch_state(decoder, s_state_frames[decoder->frame_in_progress.type]);
477 }
478
479 static struct aws_h2err s_decoder_reset_state(struct aws_h2_decoder *decoder) {
480 /* Ensure we've consumed all payload (and padding) when state machine finishes this frame.
481 * If this fails, the payload length we received is too large for this frame type.
482 * (ex: a RST_STREAM frame with > 4 bytes) */
483 if (decoder->frame_in_progress.payload_len > 0 || decoder->frame_in_progress.padding_len > 0) {
484 DECODER_LOGF(
485 ERROR, decoder, "%s frame payload is too large", aws_h2_frame_type_to_str(decoder->frame_in_progress.type));
486 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_FRAME_SIZE_ERROR);
487 }
488
489 DECODER_LOGF(TRACE, decoder, "%s frame complete", aws_h2_frame_type_to_str(decoder->frame_in_progress.type));
490
491 decoder->scratch.len = 0;
492 decoder->state = &s_state_prefix;
493 decoder->state_changed = true;
494
495 AWS_ZERO_STRUCT(decoder->frame_in_progress);
496 return AWS_H2ERR_SUCCESS;
497 }
498
499 /* Returns as much of the current frame's payload as possible, and updates payload_len */
500 static struct aws_byte_cursor s_decoder_get_payload(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
501
502 struct aws_byte_cursor result;
503
504 const uint32_t remaining_length = decoder->frame_in_progress.payload_len;
505 if (input->len < remaining_length) {
506 AWS_ASSERT(input->len <= UINT32_MAX);
507 result = aws_byte_cursor_advance(input, input->len);
508 } else {
509 result = aws_byte_cursor_advance(input, remaining_length);
510 }
511
512 decoder->frame_in_progress.payload_len -= (uint32_t)result.len;
513
514 return result;
515 }
516
517 /* clang-format off */
518
519 /* Mask of flags supported by each frame type.
520 * Frames not listed have mask of 0, which means all flags will be ignored. */
521 static const uint8_t s_acceptable_flags_for_frame[AWS_H2_FRAME_TYPE_COUNT] = {
522 [AWS_H2_FRAME_T_DATA] = AWS_H2_FRAME_F_END_STREAM | AWS_H2_FRAME_F_PADDED,
523 [AWS_H2_FRAME_T_HEADERS] = AWS_H2_FRAME_F_END_STREAM | AWS_H2_FRAME_F_END_HEADERS |
524 AWS_H2_FRAME_F_PADDED | AWS_H2_FRAME_F_PRIORITY,
525 [AWS_H2_FRAME_T_PRIORITY] = 0,
526 [AWS_H2_FRAME_T_RST_STREAM] = 0,
527 [AWS_H2_FRAME_T_SETTINGS] = AWS_H2_FRAME_F_ACK,
528 [AWS_H2_FRAME_T_PUSH_PROMISE] = AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PADDED,
529 [AWS_H2_FRAME_T_PING] = AWS_H2_FRAME_F_ACK,
530 [AWS_H2_FRAME_T_GOAWAY] = 0,
531 [AWS_H2_FRAME_T_WINDOW_UPDATE] = 0,
532 [AWS_H2_FRAME_T_CONTINUATION] = AWS_H2_FRAME_F_END_HEADERS,
533 [AWS_H2_FRAME_T_UNKNOWN] = 0,
534 };
535
536 enum stream_id_rules {
537 STREAM_ID_REQUIRED,
538 STREAM_ID_FORBIDDEN,
539 STREAM_ID_EITHER_WAY,
540 };
541
542 /* Frame-types generally either require a stream-id, or require that it be zero. */
543 static const enum stream_id_rules s_stream_id_rules_for_frame[AWS_H2_FRAME_TYPE_COUNT] = {
544 [AWS_H2_FRAME_T_DATA] = STREAM_ID_REQUIRED,
545 [AWS_H2_FRAME_T_HEADERS] = STREAM_ID_REQUIRED,
546 [AWS_H2_FRAME_T_PRIORITY] = STREAM_ID_REQUIRED,
547 [AWS_H2_FRAME_T_RST_STREAM] = STREAM_ID_REQUIRED,
548 [AWS_H2_FRAME_T_SETTINGS] = STREAM_ID_FORBIDDEN,
549 [AWS_H2_FRAME_T_PUSH_PROMISE] = STREAM_ID_REQUIRED,
550 [AWS_H2_FRAME_T_PING] = STREAM_ID_FORBIDDEN,
551 [AWS_H2_FRAME_T_GOAWAY] = STREAM_ID_FORBIDDEN,
552 [AWS_H2_FRAME_T_WINDOW_UPDATE] = STREAM_ID_EITHER_WAY, /* WINDOW_UPDATE is special and can do either */
553 [AWS_H2_FRAME_T_CONTINUATION] = STREAM_ID_REQUIRED,
554 [AWS_H2_FRAME_T_UNKNOWN] = STREAM_ID_EITHER_WAY, /* Everything in an UNKNOWN frame type is ignored */
555 };
556 /* clang-format on */
557
558 /* All frames begin with a fixed 9-octet header followed by a variable-length payload. (RFC-7540 4.1)
559 * This function processes everything preceding Frame Payload in the following diagram:
560 * +-----------------------------------------------+
561 * | Length (24) |
562 * +---------------+---------------+---------------+
563 * | Type (8) | Flags (8) |
564 * +-+-------------+---------------+-------------------------------+
565 * |R| Stream Identifier (31) |
566 * +=+=============================================================+
567 * | Frame Payload (0...) ...
568 * +---------------------------------------------------------------+
569 */
570 static struct aws_h2err s_state_fn_prefix(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
571
572 AWS_ASSERT(input->len >= s_state_prefix_requires_9_bytes);
573
574 struct aws_frame_in_progress *frame = &decoder->frame_in_progress;
575 uint8_t raw_type = 0;
576 uint8_t raw_flags = 0;
577
578 /* Read the raw values from the first 9 bytes */
579 bool all_read = true;
580 all_read &= aws_byte_cursor_read_be24(input, &frame->payload_len);
581 all_read &= aws_byte_cursor_read_u8(input, &raw_type);
582 all_read &= aws_byte_cursor_read_u8(input, &raw_flags);
583 all_read &= aws_byte_cursor_read_be32(input, &frame->stream_id);
584 AWS_ASSERT(all_read);
585 (void)all_read;
586
587 /* Validate frame type */
588 frame->type = raw_type < AWS_H2_FRAME_T_UNKNOWN ? raw_type : AWS_H2_FRAME_T_UNKNOWN;
589
590 /* Validate the frame's flags
591 * Flags that have no defined semantics for a particular frame type MUST be ignored (RFC-7540 4.1) */
592 const uint8_t flags = raw_flags & s_acceptable_flags_for_frame[decoder->frame_in_progress.type];
593
594 bool is_padded = flags & AWS_H2_FRAME_F_PADDED;
595 decoder->frame_in_progress.flags.ack = flags & AWS_H2_FRAME_F_ACK;
596 decoder->frame_in_progress.flags.end_stream = flags & AWS_H2_FRAME_F_END_STREAM;
597 decoder->frame_in_progress.flags.end_headers = flags & AWS_H2_FRAME_F_END_HEADERS;
598 decoder->frame_in_progress.flags.priority =
599 flags & AWS_H2_FRAME_F_PRIORITY || decoder->frame_in_progress.type == AWS_H2_FRAME_T_PRIORITY;
600
601 /* Connection preface requires that SETTINGS be sent first (RFC-7540 3.5).
602 * This should be the first error we check for, so that a connection sending
603 * total garbage data is likely to trigger this PROTOCOL_ERROR */
604 if (!decoder->connection_preface_complete) {
605 if (frame->type == AWS_H2_FRAME_T_SETTINGS && !frame->flags.ack) {
606 DECODER_LOG(TRACE, decoder, "Connection preface satisfied.");
607 decoder->connection_preface_complete = true;
608 } else {
609 DECODER_LOG(ERROR, decoder, "First frame must be SETTINGS");
610 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
611 }
612 }
613
614 /* Validate the frame's stream ID. */
615
616 /* Reserved bit (1st bit) MUST be ignored when receiving (RFC-7540 4.1) */
617 frame->stream_id &= s_31_bit_mask;
618
619 /* Some frame types require a stream ID, some frame types require that stream ID be zero. */
620 const enum stream_id_rules stream_id_rules = s_stream_id_rules_for_frame[frame->type];
621 if (frame->stream_id) {
622 if (stream_id_rules == STREAM_ID_FORBIDDEN) {
623 DECODER_LOGF(ERROR, decoder, "Stream ID for %s frame must be 0.", aws_h2_frame_type_to_str(frame->type));
624 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
625 }
626 } else {
627 if (stream_id_rules == STREAM_ID_REQUIRED) {
628 DECODER_LOGF(ERROR, decoder, "Stream ID for %s frame cannot be 0.", aws_h2_frame_type_to_str(frame->type));
629 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
630 }
631 }
632
633 /* A header-block starts with a HEADERS or PUSH_PROMISE frame, followed by 0 or more CONTINUATION frames.
634 * It's an error for any other frame-type or stream ID to arrive while a header-block is in progress.
635 * (RFC-7540 4.3) */
636 if (frame->type == AWS_H2_FRAME_T_CONTINUATION) {
637 if (decoder->header_block_in_progress.stream_id != frame->stream_id) {
638 DECODER_LOG(ERROR, decoder, "Unexpected CONTINUATION frame.");
639 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
640 }
641 } else {
642 if (decoder->header_block_in_progress.stream_id) {
643 DECODER_LOG(ERROR, decoder, "Expected CONTINUATION frame.");
644 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
645 }
646 }
647
648 /* Validate payload length. */
649 uint32_t max_frame_size = decoder->settings.max_frame_size;
650 if (frame->payload_len > max_frame_size) {
651 DECODER_LOGF(
652 ERROR,
653 decoder,
654 "Decoder's max frame size is %" PRIu32 ", but frame of size %" PRIu32 " was received.",
655 max_frame_size,
656 frame->payload_len);
657 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_FRAME_SIZE_ERROR);
658 }
659
660 DECODER_LOGF(
661 TRACE,
662 decoder,
663 "Done decoding frame prefix (type=%s stream-id=%" PRIu32 " payload-len=%" PRIu32 "), moving on to payload",
664 aws_h2_frame_type_to_str(frame->type),
665 frame->stream_id,
666 frame->payload_len);
667
668 if (is_padded) {
669 /* Read padding length if necessary */
670 return s_decoder_switch_state(decoder, &s_state_padding_len);
671 }
672 if (decoder->frame_in_progress.type == AWS_H2_FRAME_T_DATA) {
673 /* We invoke the on_data_begin here to report the whole payload size */
674 DECODER_CALL_VTABLE_STREAM_ARGS(
675 decoder, on_data_begin, frame->payload_len, 0 /*padding_len*/, frame->flags.end_stream);
676 }
677 if (decoder->frame_in_progress.flags.priority) {
678 /* Read the stream dependency and weight if PRIORITY is set */
679 return s_decoder_switch_state(decoder, &s_state_priority_block);
680 }
681
682 /* Set the state to the appropriate frame's state */
683 return s_decoder_switch_to_frame_state(decoder);
684 }
685
686 /* Frames that support padding, and have the PADDED flag set, begin with a 1-byte Pad Length.
687 * (Actual padding comes later at the very end of the frame)
688 * +---------------+
689 * |Pad Length? (8)|
690 * +---------------+
691 */
692 static struct aws_h2err s_state_fn_padding_len(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
693
694 AWS_ASSERT(input->len >= s_state_padding_len_requires_1_bytes);
695
696 struct aws_frame_in_progress *frame = &decoder->frame_in_progress;
697 /* Read the padding length */
698 bool succ = aws_byte_cursor_read_u8(input, &frame->padding_len);
699 AWS_ASSERT(succ);
700 (void)succ;
701
702 /* Adjust payload size so it doesn't include padding (or the 1-byte padding length) */
703 uint32_t reduce_payload = s_state_padding_len_requires_1_bytes + frame->padding_len;
704 if (reduce_payload > decoder->frame_in_progress.payload_len) {
705 DECODER_LOG(ERROR, decoder, "Padding length exceeds payload length");
706 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
707 }
708
709 if (frame->type == AWS_H2_FRAME_T_DATA) {
710 /* We invoke the on_data_begin here to report the whole payload size and the padding size */
711 DECODER_CALL_VTABLE_STREAM_ARGS(
712 decoder, on_data_begin, frame->payload_len, frame->padding_len + 1, frame->flags.end_stream);
713 }
714
715 frame->payload_len -= reduce_payload;
716
717 DECODER_LOGF(TRACE, decoder, "Padding length of frame: %" PRIu32, frame->padding_len);
718 if (frame->flags.priority) {
719 /* Read the stream dependency and weight if PRIORITY is set */
720 return s_decoder_switch_state(decoder, &s_state_priority_block);
721 }
722
723 /* Set the state to the appropriate frame's state */
724 return s_decoder_switch_to_frame_state(decoder);
725 }
726
727 static struct aws_h2err s_state_fn_padding(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
728
729 const uint8_t remaining_len = decoder->frame_in_progress.padding_len;
730 const uint8_t consuming_len = input->len < remaining_len ? (uint8_t)input->len : remaining_len;
731 aws_byte_cursor_advance(input, consuming_len);
732 decoder->frame_in_progress.padding_len -= consuming_len;
733
734 if (remaining_len == consuming_len) {
735 /* Done with the frame! */
736 return s_decoder_reset_state(decoder);
737 }
738
739 return AWS_H2ERR_SUCCESS;
740 }
741
742 /* Shared code for:
743 * PRIORITY frame (RFC-7540 6.3)
744 * Start of HEADERS frame IF the priority flag is set (RFC-7540 6.2)
745 * +-+-------------+-----------------------------------------------+
746 * |E| Stream Dependency (31) |
747 * +-+-------------+-----------------------------------------------+
748 * | Weight (8) |
749 * +-+-------------+-----------------------------------------------+
750 */
751 static struct aws_h2err s_state_fn_priority_block(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
752
753 AWS_ASSERT(input->len >= s_state_priority_block_requires_5_bytes);
754
755 /* #NOTE: throw priority data on the GROUND. They make us hecka vulnerable to DDoS and stuff.
756 * https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-9513
757 */
758 aws_byte_cursor_advance(input, s_state_priority_block_requires_5_bytes);
759
760 decoder->frame_in_progress.payload_len -= s_state_priority_block_requires_5_bytes;
761
762 return s_decoder_switch_to_frame_state(decoder);
763 }
764
765 static struct aws_h2err s_state_fn_frame_data(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
766
767 const struct aws_byte_cursor body_data = s_decoder_get_payload(decoder, input);
768
769 if (body_data.len) {
770 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_data_i, body_data);
771 }
772
773 if (decoder->frame_in_progress.payload_len == 0) {
774 DECODER_CALL_VTABLE_STREAM(decoder, on_data_end);
775 /* If frame had END_STREAM flag, alert user now */
776 if (decoder->frame_in_progress.flags.end_stream) {
777 DECODER_CALL_VTABLE_STREAM(decoder, on_end_stream);
778 }
779
780 /* Process padding if necessary, otherwise we're done! */
781 return s_decoder_switch_state(decoder, &s_state_padding);
782 }
783
784 return AWS_H2ERR_SUCCESS;
785 }
786 static struct aws_h2err s_state_fn_frame_headers(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
787 (void)input;
788
789 /* Start header-block and alert the user */
790 decoder->header_block_in_progress.stream_id = decoder->frame_in_progress.stream_id;
791 decoder->header_block_in_progress.is_push_promise = false;
792 decoder->header_block_in_progress.ends_stream = decoder->frame_in_progress.flags.end_stream;
793
794 DECODER_CALL_VTABLE_STREAM(decoder, on_headers_begin);
795
796 /* Read the header-block fragment */
797 return s_decoder_switch_state(decoder, &s_state_header_block_loop);
798 }
799 static struct aws_h2err s_state_fn_frame_priority(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
800 (void)input;
801
802 /* We already processed this data in the shared priority_block state, so we're done! */
803 return s_decoder_reset_state(decoder);
804 }
805
806 /* RST_STREAM is just a 4-byte error code.
807 * +---------------------------------------------------------------+
808 * | Error Code (32) |
809 * +---------------------------------------------------------------+
810 */
811 static struct aws_h2err s_state_fn_frame_rst_stream(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
812
813 AWS_ASSERT(input->len >= s_state_frame_rst_stream_requires_4_bytes);
814
815 uint32_t error_code = 0;
816 bool succ = aws_byte_cursor_read_be32(input, &error_code);
817 AWS_ASSERT(succ);
818 (void)succ;
819
820 decoder->frame_in_progress.payload_len -= s_state_frame_rst_stream_requires_4_bytes;
821
822 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_rst_stream, error_code);
823
824 return s_decoder_reset_state(decoder);
825 }
826
827 /* A SETTINGS frame may contain any number of 6-byte entries.
828 * This state consumes no data, but sends us into the appropriate next state */
829 static struct aws_h2err s_state_fn_frame_settings_begin(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
830 (void)input;
831
832 /* If ack is set, report and we're done */
833 if (decoder->frame_in_progress.flags.ack) {
834 /* Receipt of a SETTINGS frame with the ACK flag set and a length field value other
835 * than 0 MUST be treated as a connection error of type FRAME_SIZE_ERROR */
836 if (decoder->frame_in_progress.payload_len) {
837 DECODER_LOGF(
838 ERROR,
839 decoder,
840 "SETTINGS ACK frame received, but it has non-0 payload length %" PRIu32,
841 decoder->frame_in_progress.payload_len);
842 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_FRAME_SIZE_ERROR);
843 }
844
845 DECODER_CALL_VTABLE(decoder, on_settings_ack);
846 return s_decoder_reset_state(decoder);
847 }
848
849 if (decoder->frame_in_progress.payload_len % s_state_frame_settings_i_requires_6_bytes != 0) {
850 /* A SETTINGS frame with a length other than a multiple of 6 octets MUST be
851 * treated as a connection error (Section 5.4.1) of type FRAME_SIZE_ERROR */
852 DECODER_LOGF(
853 ERROR,
854 decoder,
855 "Settings frame payload length is %" PRIu32 ", but it must be divisible by %" PRIu32,
856 decoder->frame_in_progress.payload_len,
857 s_state_frame_settings_i_requires_6_bytes);
858 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_FRAME_SIZE_ERROR);
859 }
860
861 /* Enter looping states until all entries are consumed. */
862 return s_decoder_switch_state(decoder, &s_state_frame_settings_loop);
863 }
864
865 /* Check if we're done consuming settings */
866 static struct aws_h2err s_state_fn_frame_settings_loop(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
867 (void)input;
868
869 if (decoder->frame_in_progress.payload_len == 0) {
870 /* Huzzah, done with the frame, fire the callback */
871 struct aws_array_list *buffer = &decoder->settings_buffer_list;
872 DECODER_CALL_VTABLE_ARGS(
873 decoder, on_settings, buffer->data, aws_array_list_length(&decoder->settings_buffer_list));
874 /* clean up the buffer */
875 aws_array_list_clear(&decoder->settings_buffer_list);
876 return s_decoder_reset_state(decoder);
877 }
878
879 return s_decoder_switch_state(decoder, &s_state_frame_settings_i);
880 }
881
882 /* Each run through this state consumes one 6-byte setting.
883 * There may be multiple settings in a SETTINGS frame.
884 * +-------------------------------+
885 * | Identifier (16) |
886 * +-------------------------------+-------------------------------+
887 * | Value (32) |
888 * +---------------------------------------------------------------+
889 */
890 static struct aws_h2err s_state_fn_frame_settings_i(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
891
892 AWS_ASSERT(input->len >= s_state_frame_settings_i_requires_6_bytes);
893
894 uint16_t id = 0;
895 uint32_t value = 0;
896
897 bool succ = aws_byte_cursor_read_be16(input, &id);
898 AWS_ASSERT(succ);
899 (void)succ;
900
901 succ = aws_byte_cursor_read_be32(input, &value);
902 AWS_ASSERT(succ);
903 (void)succ;
904
905 /* An endpoint that receives a SETTINGS frame with any unknown or unsupported identifier MUST ignore that setting.
906 * RFC-7540 6.5.2 */
907 if (id >= AWS_HTTP2_SETTINGS_BEGIN_RANGE && id < AWS_HTTP2_SETTINGS_END_RANGE) {
908 /* check the value meets the settings bounds */
909 if (value < aws_h2_settings_bounds[id][0] || value > aws_h2_settings_bounds[id][1]) {
910 DECODER_LOGF(
911 ERROR, decoder, "A value of SETTING frame is invalid, id: %" PRIu16 ", value: %" PRIu32, id, value);
912 if (id == AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE) {
913 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_FLOW_CONTROL_ERROR);
914 } else {
915 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
916 }
917 }
918 struct aws_http2_setting setting;
919 setting.id = id;
920 setting.value = value;
921 /* array_list will keep a copy of setting, it is fine to be a local variable */
922 if (aws_array_list_push_back(&decoder->settings_buffer_list, &setting)) {
923 DECODER_LOGF(ERROR, decoder, "Writing setting to buffer failed, %s", aws_error_name(aws_last_error()));
924 return aws_h2err_from_last_error();
925 }
926 }
927
928 /* Update payload len */
929 decoder->frame_in_progress.payload_len -= s_state_frame_settings_i_requires_6_bytes;
930
931 return s_decoder_switch_state(decoder, &s_state_frame_settings_loop);
932 }
933
934 /* Read 4-byte Promised Stream ID
935 * The rest of the frame is just like HEADERS, so move on to shared states...
936 * +-+-------------------------------------------------------------+
937 * |R| Promised Stream ID (31) |
938 * +-+-----------------------------+-------------------------------+
939 */
940 static struct aws_h2err s_state_fn_frame_push_promise(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
941
942 if (decoder->settings.enable_push == 0) {
943 /* treat the receipt of a PUSH_PROMISE frame as a connection error of type PROTOCOL_ERROR.(RFC-7540 6.5.2) */
944 DECODER_LOG(ERROR, decoder, "PUSH_PROMISE is invalid, the seting for enable push is 0");
945 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
946 }
947
948 AWS_ASSERT(input->len >= s_state_frame_push_promise_requires_4_bytes);
949
950 uint32_t promised_stream_id = 0;
951 bool succ = aws_byte_cursor_read_be32(input, &promised_stream_id);
952 AWS_ASSERT(succ);
953 (void)succ;
954
955 decoder->frame_in_progress.payload_len -= s_state_frame_push_promise_requires_4_bytes;
956
957 /* Reserved bit (top bit) must be ignored when receiving (RFC-7540 4.1) */
958 promised_stream_id &= s_31_bit_mask;
959
960 /* Promised stream ID must not be 0 (RFC-7540 6.6).
961 * Promised stream ID (server-initiated) must be even-numbered (RFC-7540 5.1.1). */
962 if ((promised_stream_id == 0) || (promised_stream_id % 2) != 0) {
963 DECODER_LOGF(ERROR, decoder, "PUSH_PROMISE is promising invalid stream ID %" PRIu32, promised_stream_id);
964 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
965 }
966
967 /* Server cannot receive PUSH_PROMISE frames */
968 if (decoder->is_server) {
969 DECODER_LOG(ERROR, decoder, "Server cannot receive PUSH_PROMISE frames");
970 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
971 }
972
973 /* Start header-block and alert the user. */
974 decoder->header_block_in_progress.stream_id = decoder->frame_in_progress.stream_id;
975 decoder->header_block_in_progress.is_push_promise = true;
976 decoder->header_block_in_progress.ends_stream = false;
977
978 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_push_promise_begin, promised_stream_id);
979
980 /* Read the header-block fragment */
981 return s_decoder_switch_state(decoder, &s_state_header_block_loop);
982 }
983
984 /* PING frame is just 8-bytes of opaque data.
985 * +---------------------------------------------------------------+
986 * | |
987 * | Opaque Data (64) |
988 * | |
989 * +---------------------------------------------------------------+
990 */
991 static struct aws_h2err s_state_fn_frame_ping(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
992
993 AWS_ASSERT(input->len >= s_state_frame_ping_requires_8_bytes);
994
995 uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE] = {0};
996 bool succ = aws_byte_cursor_read(input, &opaque_data, AWS_HTTP2_PING_DATA_SIZE);
997 AWS_ASSERT(succ);
998 (void)succ;
999
1000 decoder->frame_in_progress.payload_len -= s_state_frame_ping_requires_8_bytes;
1001
1002 if (decoder->frame_in_progress.flags.ack) {
1003 DECODER_CALL_VTABLE_ARGS(decoder, on_ping_ack, opaque_data);
1004 } else {
1005 DECODER_CALL_VTABLE_ARGS(decoder, on_ping, opaque_data);
1006 }
1007
1008 return s_decoder_reset_state(decoder);
1009 }
1010
1011 /* Read first 8 bytes of GOAWAY.
1012 * This may be followed by N bytes of debug data.
1013 * +-+-------------------------------------------------------------+
1014 * |R| Last-Stream-ID (31) |
1015 * +-+-------------------------------------------------------------+
1016 * | Error Code (32) |
1017 * +---------------------------------------------------------------+
1018 */
1019 static struct aws_h2err s_state_fn_frame_goaway(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
1020
1021 AWS_ASSERT(input->len >= s_state_frame_goaway_requires_8_bytes);
1022
1023 uint32_t last_stream = 0;
1024 uint32_t error_code = AWS_HTTP2_ERR_NO_ERROR;
1025
1026 bool succ = aws_byte_cursor_read_be32(input, &last_stream);
1027 AWS_ASSERT(succ);
1028 (void)succ;
1029
1030 last_stream &= s_31_bit_mask;
1031
1032 succ = aws_byte_cursor_read_be32(input, &error_code);
1033 AWS_ASSERT(succ);
1034 (void)succ;
1035
1036 decoder->frame_in_progress.payload_len -= s_state_frame_goaway_requires_8_bytes;
1037 uint32_t debug_data_length = decoder->frame_in_progress.payload_len;
1038 /* Received new GOAWAY, clean up the previous one. Buffer it up and invoke the callback once the debug data decoded
1039 * fully. */
1040 decoder->goaway_in_progress.error_code = error_code;
1041 decoder->goaway_in_progress.last_stream = last_stream;
1042 int init_result = aws_byte_buf_init(&decoder->goaway_in_progress.debug_data, decoder->alloc, debug_data_length);
1043 AWS_ASSERT(init_result == 0);
1044 (void)init_result;
1045
1046 return s_decoder_switch_state(decoder, &s_state_frame_goaway_debug_data);
1047 }
1048
1049 /* Optional remainder of GOAWAY frame.
1050 * +---------------------------------------------------------------+
1051 * | Additional Debug Data (*) |
1052 * +---------------------------------------------------------------+
1053 */
1054 static struct aws_h2err s_state_fn_frame_goaway_debug_data(
1055 struct aws_h2_decoder *decoder,
1056 struct aws_byte_cursor *input) {
1057
1058 struct aws_byte_cursor debug_data = s_decoder_get_payload(decoder, input);
1059 if (debug_data.len > 0) {
1060 /* As we initialized the buffer to the size of debug data, we can safely append here */
1061 aws_byte_buf_append(&decoder->goaway_in_progress.debug_data, &debug_data);
1062 }
1063
1064 /* If this is the last data in the frame, reset decoder */
1065 if (decoder->frame_in_progress.payload_len == 0) {
1066 struct aws_byte_cursor debug_cursor = aws_byte_cursor_from_buf(&decoder->goaway_in_progress.debug_data);
1067
1068 DECODER_CALL_VTABLE_ARGS(
1069 decoder,
1070 on_goaway,
1071 decoder->goaway_in_progress.last_stream,
1072 decoder->goaway_in_progress.error_code,
1073 debug_cursor);
1074 aws_byte_buf_clean_up(&decoder->goaway_in_progress.debug_data);
1075 return s_decoder_reset_state(decoder);
1076 }
1077
1078 return AWS_H2ERR_SUCCESS;
1079 }
1080
1081 /* WINDOW_UPDATE frame.
1082 * +-+-------------------------------------------------------------+
1083 * |R| Window Size Increment (31) |
1084 * +-+-------------------------------------------------------------+
1085 */
1086 static struct aws_h2err s_state_fn_frame_window_update(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
1087
1088 AWS_ASSERT(input->len >= s_state_frame_window_update_requires_4_bytes);
1089
1090 uint32_t window_increment = 0;
1091 bool succ = aws_byte_cursor_read_be32(input, &window_increment);
1092 AWS_ASSERT(succ);
1093 (void)succ;
1094
1095 decoder->frame_in_progress.payload_len -= s_state_frame_window_update_requires_4_bytes;
1096
1097 window_increment &= s_31_bit_mask;
1098
1099 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_window_update, window_increment);
1100
1101 return s_decoder_reset_state(decoder);
1102 }
1103
1104 /* CONTINUATION is a lot like HEADERS, so it uses shared states. */
1105 static struct aws_h2err s_state_fn_frame_continuation(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
1106 (void)input;
1107
1108 /* Read the header-block fragment */
1109 return s_decoder_switch_state(decoder, &s_state_header_block_loop);
1110 }
1111
1112 /* Implementations MUST ignore and discard any frame that has a type that is unknown. */
1113 static struct aws_h2err s_state_fn_frame_unknown(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
1114
1115 /* Read all data possible, and throw it on the floor */
1116 s_decoder_get_payload(decoder, input);
1117
1118 /* If there's no more data expected, end the frame */
1119 if (decoder->frame_in_progress.payload_len == 0) {
1120 return s_decoder_reset_state(decoder);
1121 }
1122
1123 return AWS_H2ERR_SUCCESS;
1124 }
1125
1126 /* Perform analysis that can't be done until all pseudo-headers are received.
1127 * Then deliver buffered pseudoheaders via callback */
1128 static struct aws_h2err s_flush_pseudoheaders(struct aws_h2_decoder *decoder) {
1129 struct aws_header_block_in_progress *current_block = &decoder->header_block_in_progress;
1130
1131 if (current_block->malformed) {
1132 goto already_malformed;
1133 }
1134
1135 if (current_block->pseudoheaders_done) {
1136 return AWS_H2ERR_SUCCESS;
1137 }
1138 current_block->pseudoheaders_done = true;
1139
1140 /* s_process_header_field() already checked that we're not mixing request & response pseudoheaders */
1141 bool has_request_pseudoheaders = false;
1142 for (int i = PSEUDOHEADER_METHOD; i <= PSEUDOHEADER_PATH; ++i) {
1143 if (current_block->pseudoheader_values[i] != NULL) {
1144 has_request_pseudoheaders = true;
1145 break;
1146 }
1147 }
1148
1149 bool has_response_pseudoheaders = current_block->pseudoheader_values[PSEUDOHEADER_STATUS] != NULL;
1150
1151 if (current_block->is_push_promise && !has_request_pseudoheaders) {
1152 DECODER_LOG(ERROR, decoder, "PUSH_PROMISE is missing :method");
1153 goto malformed;
1154 }
1155
1156 if (has_request_pseudoheaders) {
1157 /* Request header-block. */
1158 current_block->block_type = AWS_HTTP_HEADER_BLOCK_MAIN;
1159
1160 } else if (has_response_pseudoheaders) {
1161 /* Response header block. */
1162
1163 /* Determine whether this is an Informational (1xx) response */
1164 struct aws_byte_cursor status_value =
1165 aws_byte_cursor_from_string(current_block->pseudoheader_values[PSEUDOHEADER_STATUS]);
1166 uint64_t status_code;
1167 if (status_value.len != 3 || aws_byte_cursor_utf8_parse_u64(status_value, &status_code)) {
1168 DECODER_LOG(ERROR, decoder, ":status header has invalid value");
1169 DECODER_LOGF(DEBUG, decoder, "Bad :status value is '" PRInSTR "'", AWS_BYTE_CURSOR_PRI(status_value));
1170 goto malformed;
1171 }
1172
1173 if (status_code / 100 == 1) {
1174 current_block->block_type = AWS_HTTP_HEADER_BLOCK_INFORMATIONAL;
1175
1176 if (current_block->ends_stream) {
1177 /* Informational headers do not constitute a full response (RFC-7540 8.1) */
1178 DECODER_LOG(ERROR, decoder, "Informational (1xx) response cannot END_STREAM");
1179 goto malformed;
1180 }
1181 current_block->body_headers_forbidden = true;
1182 } else {
1183 current_block->block_type = AWS_HTTP_HEADER_BLOCK_MAIN;
1184 }
1185 /**
1186 * RFC-9110 8.6.
1187 * A server MUST NOT send a Content-Length header field in any response with a status code of 1xx
1188 * (Informational) or 204 (No Content).
1189 */
1190 current_block->body_headers_forbidden |= status_code == AWS_HTTP_STATUS_CODE_204_NO_CONTENT;
1191
1192 } else {
1193 /* Trailing header block. */
1194 if (!current_block->ends_stream) {
1195 DECODER_LOG(ERROR, decoder, "HEADERS appear to be trailer, but lack END_STREAM");
1196 goto malformed;
1197 }
1198
1199 current_block->block_type = AWS_HTTP_HEADER_BLOCK_TRAILING;
1200 }
1201
1202 /* #TODO RFC-7540 8.1.2.3 & 8.3 Validate request has correct pseudoheaders. Note different rules for CONNECT */
1203 /* #TODO validate pseudoheader values. each one has its own special rules */
1204
1205 /* Finally, deliver header-fields via callback */
1206 for (size_t i = 0; i < PSEUDOHEADER_COUNT; ++i) {
1207 const struct aws_string *value_string = current_block->pseudoheader_values[i];
1208 if (value_string) {
1209
1210 struct aws_http_header header_field = {
1211 .name = *s_pseudoheader_name_to_cursor[i],
1212 .value = aws_byte_cursor_from_string(value_string),
1213 .compression = current_block->pseudoheader_compression[i],
1214 };
1215
1216 enum aws_http_header_name name_enum = s_pseudoheader_to_header_name[i];
1217
1218 if (current_block->is_push_promise) {
1219 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_push_promise_i, &header_field, name_enum);
1220 } else {
1221 DECODER_CALL_VTABLE_STREAM_ARGS(
1222 decoder, on_headers_i, &header_field, name_enum, current_block->block_type);
1223 }
1224 }
1225 }
1226
1227 return AWS_H2ERR_SUCCESS;
1228
1229 malformed:
1230 /* A malformed header-block is not a connection error, it's a Stream Error (RFC-7540 5.4.2).
1231 * We continue decoding and report that it's malformed in on_headers_end(). */
1232 current_block->malformed = true;
1233 return AWS_H2ERR_SUCCESS;
1234 already_malformed:
1235 return AWS_H2ERR_SUCCESS;
1236 }
1237
1238 /* Process single header-field.
1239 * If it's invalid, mark the header-block as malformed.
1240 * If it's valid, and header-block is not malformed, deliver via callback. */
1241 static struct aws_h2err s_process_header_field(
1242 struct aws_h2_decoder *decoder,
1243 const struct aws_http_header *header_field) {
1244
1245 struct aws_header_block_in_progress *current_block = &decoder->header_block_in_progress;
1246 if (current_block->malformed) {
1247 goto already_malformed;
1248 }
1249
1250 const struct aws_byte_cursor name = header_field->name;
1251 if (name.len == 0) {
1252 DECODER_LOG(ERROR, decoder, "Header name is blank");
1253 goto malformed;
1254 }
1255
1256 enum aws_http_header_name name_enum = aws_http_lowercase_str_to_header_name(name);
1257
1258 bool is_pseudoheader = name.ptr[0] == ':';
1259 if (is_pseudoheader) {
1260 if (current_block->pseudoheaders_done) {
1261 /* Note: being careful not to leak possibly sensitive data except at DEBUG level and lower */
1262 DECODER_LOG(ERROR, decoder, "Pseudo-headers must appear before regular fields.");
1263 DECODER_LOGF(DEBUG, decoder, "Misplaced pseudo-header is '" PRInSTR "'", AWS_BYTE_CURSOR_PRI(name));
1264 goto malformed;
1265 }
1266
1267 enum pseudoheader_name pseudoheader_enum = s_header_to_pseudoheader_name(name_enum);
1268 if (pseudoheader_enum == PSEUDOHEADER_UNKNOWN) {
1269 DECODER_LOG(ERROR, decoder, "Unrecognized pseudo-header");
1270 DECODER_LOGF(DEBUG, decoder, "Unrecognized pseudo-header is '" PRInSTR "'", AWS_BYTE_CURSOR_PRI(name));
1271 goto malformed;
1272 }
1273
1274 /* Ensure request pseudo-headers vs response pseudoheaders were sent appropriately.
1275 * This also ensures that request and response pseudoheaders aren't being mixed. */
1276 bool expect_request_pseudoheader = decoder->is_server || current_block->is_push_promise;
1277 bool is_request_pseudoheader = pseudoheader_enum != PSEUDOHEADER_STATUS;
1278 if (expect_request_pseudoheader != is_request_pseudoheader) {
1279 DECODER_LOGF(
1280 ERROR, /* ok to log name of recognized pseudo-header at ERROR level */
1281 decoder,
1282 "'" PRInSTR "' pseudo-header cannot be in %s header-block to %s",
1283 AWS_BYTE_CURSOR_PRI(name),
1284 current_block->is_push_promise ? "PUSH_PROMISE" : "HEADERS",
1285 decoder->is_server ? "server" : "client");
1286 goto malformed;
1287 }
1288
1289 /* Protect against duplicates. */
1290 if (current_block->pseudoheader_values[pseudoheader_enum] != NULL) {
1291 /* ok to log name of recognized pseudo-header at ERROR level */
1292 DECODER_LOGF(
1293 ERROR, decoder, "'" PRInSTR "' pseudo-header occurred multiple times", AWS_BYTE_CURSOR_PRI(name));
1294 goto malformed;
1295 }
1296
1297 /* Buffer up pseudo-headers, we'll deliver them later once they're all validated. */
1298 current_block->pseudoheader_compression[pseudoheader_enum] = header_field->compression;
1299 current_block->pseudoheader_values[pseudoheader_enum] =
1300 aws_string_new_from_cursor(decoder->alloc, &header_field->value);
1301 if (!current_block->pseudoheader_values[pseudoheader_enum]) {
1302 return aws_h2err_from_last_error();
1303 }
1304
1305 } else { /* Else regular header-field. */
1306
1307 /* Regular header-fields come after pseudo-headers, so make sure pseudo-headers are flushed */
1308 if (!current_block->pseudoheaders_done) {
1309 struct aws_h2err err = s_flush_pseudoheaders(decoder);
1310 if (aws_h2err_failed(err)) {
1311 return err;
1312 }
1313
1314 /* might have realized that header-block is malformed during flush */
1315 if (current_block->malformed) {
1316 goto already_malformed;
1317 }
1318 }
1319
1320 /* Validate header name (not necessary if string already matched against a known enum) */
1321 if (name_enum == AWS_HTTP_HEADER_UNKNOWN) {
1322 if (!aws_strutil_is_lowercase_http_token(name)) {
1323 DECODER_LOG(ERROR, decoder, "Header name contains invalid characters");
1324 DECODER_LOGF(DEBUG, decoder, "Bad header name is '" PRInSTR "'", AWS_BYTE_CURSOR_PRI(name));
1325 goto malformed;
1326 }
1327 }
1328
1329 /* #TODO Validate characters used in header_field->value */
1330
1331 switch (name_enum) {
1332 case AWS_HTTP_HEADER_COOKIE:
1333 /* for a header cookie, we will not fire callback until we concatenate them all, let's store it at the
1334 * buffer */
1335 if (header_field->compression > current_block->cookie_header_compression_type) {
1336 current_block->cookie_header_compression_type = header_field->compression;
1337 }
1338
1339 if (current_block->cookies.len) {
1340 /* add a delimiter */
1341 struct aws_byte_cursor delimiter = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("; ");
1342 if (aws_byte_buf_append_dynamic(&current_block->cookies, &delimiter)) {
1343 return aws_h2err_from_last_error();
1344 }
1345 }
1346 if (aws_byte_buf_append_dynamic(&current_block->cookies, &header_field->value)) {
1347 return aws_h2err_from_last_error();
1348 }
1349 /* Early return */
1350 return AWS_H2ERR_SUCCESS;
1351 case AWS_HTTP_HEADER_TRANSFER_ENCODING:
1352 case AWS_HTTP_HEADER_UPGRADE:
1353 case AWS_HTTP_HEADER_KEEP_ALIVE:
1354 case AWS_HTTP_HEADER_PROXY_CONNECTION: {
1355 /* connection-specific header field are treated as malformed (RFC9113 8.2.2) */
1356 DECODER_LOGF(
1357 ERROR,
1358 decoder,
1359 "Connection-specific header ('" PRInSTR "') found, not allowed in HTTP/2",
1360 AWS_BYTE_CURSOR_PRI(name));
1361 goto malformed;
1362 } break;
1363
1364 case AWS_HTTP_HEADER_CONTENT_LENGTH:
1365 if (current_block->body_headers_forbidden) {
1366 /* The content-length are forbidden */
1367 DECODER_LOG(ERROR, decoder, "Unexpected Content-Length header found");
1368 goto malformed;
1369 }
1370 break;
1371 default:
1372 break;
1373 }
1374 /* Deliver header-field via callback */
1375 if (current_block->is_push_promise) {
1376 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_push_promise_i, header_field, name_enum);
1377 } else {
1378 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_headers_i, header_field, name_enum, current_block->block_type);
1379 }
1380 }
1381
1382 return AWS_H2ERR_SUCCESS;
1383
1384 malformed:
1385 /* A malformed header-block is not a connection error, it's a Stream Error (RFC-7540 5.4.2).
1386 * We continue decoding and report that it's malformed in on_headers_end(). */
1387 current_block->malformed = true;
1388 return AWS_H2ERR_SUCCESS;
1389 already_malformed:
1390 return AWS_H2ERR_SUCCESS;
1391 }
1392
1393 static struct aws_h2err s_flush_cookie_header(struct aws_h2_decoder *decoder) {
1394 struct aws_header_block_in_progress *current_block = &decoder->header_block_in_progress;
1395 if (current_block->malformed) {
1396 return AWS_H2ERR_SUCCESS;
1397 }
1398 if (current_block->cookies.len == 0) {
1399 /* Nothing to flush */
1400 return AWS_H2ERR_SUCCESS;
1401 }
1402 struct aws_http_header concatenated_cookie;
1403 struct aws_byte_cursor header_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("cookie");
1404 concatenated_cookie.name = header_name;
1405 concatenated_cookie.value = aws_byte_cursor_from_buf(&current_block->cookies);
1406 concatenated_cookie.compression = current_block->cookie_header_compression_type;
1407 if (current_block->is_push_promise) {
1408 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_push_promise_i, &concatenated_cookie, AWS_HTTP_HEADER_COOKIE);
1409 } else {
1410 DECODER_CALL_VTABLE_STREAM_ARGS(
1411 decoder, on_headers_i, &concatenated_cookie, AWS_HTTP_HEADER_COOKIE, current_block->block_type);
1412 }
1413 return AWS_H2ERR_SUCCESS;
1414 }
1415
1416 /* This state checks whether we've consumed the current frame's entire header-block fragment.
1417 * We revisit this state after each entry is decoded.
1418 * This state consumes no data. */
1419 static struct aws_h2err s_state_fn_header_block_loop(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
1420 (void)input;
1421
1422 /* If we're out of payload data, handle frame complete */
1423 if (decoder->frame_in_progress.payload_len == 0) {
1424
1425 /* If this is the end of the header-block, invoke callback and clear header_block_in_progress */
1426 if (decoder->frame_in_progress.flags.end_headers) {
1427 /* Ensure pseudo-headers have been flushed */
1428 struct aws_h2err err = s_flush_pseudoheaders(decoder);
1429 if (aws_h2err_failed(err)) {
1430 return err;
1431 }
1432 /* flush the concatenated cookie header */
1433 err = s_flush_cookie_header(decoder);
1434 if (aws_h2err_failed(err)) {
1435 return err;
1436 }
1437
1438 bool malformed = decoder->header_block_in_progress.malformed;
1439 DECODER_LOGF(TRACE, decoder, "Done decoding header-block, malformed=%d", malformed);
1440
1441 if (decoder->header_block_in_progress.is_push_promise) {
1442 DECODER_CALL_VTABLE_STREAM_ARGS(decoder, on_push_promise_end, malformed);
1443 } else {
1444 DECODER_CALL_VTABLE_STREAM_ARGS(
1445 decoder, on_headers_end, malformed, decoder->header_block_in_progress.block_type);
1446 }
1447
1448 /* If header-block began with END_STREAM flag, alert user now */
1449 if (decoder->header_block_in_progress.ends_stream) {
1450 DECODER_CALL_VTABLE_STREAM(decoder, on_end_stream);
1451 }
1452
1453 s_reset_header_block_in_progress(decoder);
1454
1455 } else {
1456 DECODER_LOG(TRACE, decoder, "Done decoding header-block fragment, expecting CONTINUATION frames");
1457 }
1458
1459 /* Finish this frame */
1460 return s_decoder_switch_state(decoder, &s_state_padding);
1461 }
1462
1463 DECODER_LOGF(
1464 TRACE,
1465 decoder,
1466 "Decoding header-block entry, %" PRIu32 " bytes remaining in payload",
1467 decoder->frame_in_progress.payload_len);
1468
1469 return s_decoder_switch_state(decoder, &s_state_header_block_entry);
1470 }
1471
1472 /* We stay in this state until a single "entry" is decoded from the header-block fragment.
1473 * Then we return to the header_block_loop state */
1474 static struct aws_h2err s_state_fn_header_block_entry(struct aws_h2_decoder *decoder, struct aws_byte_cursor *input) {
1475 /* This state requires at least 1 byte, but will likely consume more */
1476 AWS_ASSERT(input->len >= s_state_header_block_entry_requires_1_bytes);
1477
1478 /* Feed header-block fragment to HPACK decoder.
1479 * Don't let decoder consume anything beyond payload_len. */
1480 struct aws_byte_cursor fragment = *input;
1481 if (fragment.len > decoder->frame_in_progress.payload_len) {
1482 fragment.len = decoder->frame_in_progress.payload_len;
1483 }
1484
1485 const size_t prev_fragment_len = fragment.len;
1486
1487 struct aws_hpack_decode_result result;
1488 if (aws_hpack_decode(&decoder->hpack, &fragment, &result)) {
1489 DECODER_LOGF(ERROR, decoder, "Error decoding header-block fragment: %s", aws_error_name(aws_last_error()));
1490
1491 /* Any possible error from HPACK decoder (except OOM) is treated as a COMPRESSION error. */
1492 if (aws_last_error() == AWS_ERROR_OOM) {
1493 return aws_h2err_from_last_error();
1494 } else {
1495 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_COMPRESSION_ERROR);
1496 }
1497 }
1498
1499 /* HPACK decoder returns when it reaches the end of an entry, or when it's consumed the whole fragment.
1500 * Update input & payload_len to reflect the number of bytes consumed. */
1501 const size_t bytes_consumed = prev_fragment_len - fragment.len;
1502 aws_byte_cursor_advance(input, bytes_consumed);
1503 decoder->frame_in_progress.payload_len -= (uint32_t)bytes_consumed;
1504
1505 if (result.type == AWS_HPACK_DECODE_T_ONGOING) {
1506 /* HPACK decoder hasn't finished entry */
1507
1508 if (decoder->frame_in_progress.payload_len > 0) {
1509 /* More payload is coming. Remain in state until it arrives */
1510 DECODER_LOG(TRACE, decoder, "Header-block entry partially decoded, waiting for more data.");
1511 return AWS_H2ERR_SUCCESS;
1512 }
1513
1514 if (decoder->frame_in_progress.flags.end_headers) {
1515 /* Reached end of the frame's payload, and this frame ends the header-block.
1516 * Error if we ended up with a partially decoded entry. */
1517 DECODER_LOG(ERROR, decoder, "Compression error: incomplete entry at end of header-block");
1518 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_COMPRESSION_ERROR);
1519 }
1520
1521 /* Reached end of this frame's payload, but CONTINUATION frames are expected to arrive.
1522 * We'll resume decoding this entry when we get them. */
1523 DECODER_LOG(TRACE, decoder, "Header-block entry partially decoded, resumes in CONTINUATION frame");
1524 return s_decoder_switch_state(decoder, &s_state_header_block_loop);
1525 }
1526
1527 /* Finished decoding HPACK entry! */
1528
1529 /* #TODO Enforces dynamic table resize rules from RFC-7541 4.2
1530 * If dynamic table size changed via SETTINGS frame, next header-block must start with DYNAMIC_TABLE_RESIZE entry.
1531 * Is it illegal to receive a resize entry at other times? */
1532
1533 /* #TODO The TE header field ... MUST NOT contain any value other than "trailers" */
1534
1535 if (result.type == AWS_HPACK_DECODE_T_HEADER_FIELD) {
1536 const struct aws_http_header *header_field = &result.data.header_field;
1537
1538 DECODER_LOGF(
1539 TRACE,
1540 decoder,
1541 "Decoded header field: \"" PRInSTR ": " PRInSTR "\"",
1542 AWS_BYTE_CURSOR_PRI(header_field->name),
1543 AWS_BYTE_CURSOR_PRI(header_field->value));
1544
1545 struct aws_h2err err = s_process_header_field(decoder, header_field);
1546 if (aws_h2err_failed(err)) {
1547 return err;
1548 }
1549 }
1550
1551 return s_decoder_switch_state(decoder, &s_state_header_block_loop);
1552 }
1553
1554 /* The first thing a client sends on a connection is a 24 byte magic string (RFC-7540 3.5).
1555 * Note that this state doesn't "require" the full 24 bytes, it runs as data arrives.
1556 * This avoids hanging if < 24 bytes rolled in. */
1557 static struct aws_h2err s_state_fn_connection_preface_string(
1558 struct aws_h2_decoder *decoder,
1559 struct aws_byte_cursor *input) {
1560 size_t remaining_len = decoder->connection_preface_cursor.len;
1561 size_t consuming_len = input->len < remaining_len ? input->len : remaining_len;
1562
1563 struct aws_byte_cursor expected = aws_byte_cursor_advance(&decoder->connection_preface_cursor, consuming_len);
1564
1565 struct aws_byte_cursor received = aws_byte_cursor_advance(input, consuming_len);
1566
1567 if (!aws_byte_cursor_eq(&expected, &received)) {
1568 DECODER_LOG(ERROR, decoder, "Client connection preface is invalid");
1569 return aws_h2err_from_h2_code(AWS_HTTP2_ERR_PROTOCOL_ERROR);
1570 }
1571
1572 if (decoder->connection_preface_cursor.len == 0) {
1573 /* Done receiving connection preface string, proceed to decoding normal frames. */
1574 return s_decoder_reset_state(decoder);
1575 }
1576
1577 /* Remain in state until more data arrives */
1578 return AWS_H2ERR_SUCCESS;
1579 }
1580
1581 void aws_h2_decoder_set_setting_header_table_size(struct aws_h2_decoder *decoder, uint32_t data) {
1582 /* Set the protocol_max_size_setting for hpack. */
1583 aws_hpack_decoder_update_max_table_size(&decoder->hpack, data);
1584 }
1585
1586 void aws_h2_decoder_set_setting_enable_push(struct aws_h2_decoder *decoder, uint32_t data) {
1587 decoder->settings.enable_push = data;
1588 }
1589
1590 void aws_h2_decoder_set_setting_max_frame_size(struct aws_h2_decoder *decoder, uint32_t data) {
1591 decoder->settings.max_frame_size = data;
1592 }