Debian Codegraph @

crt/aws-c-http/source/h1_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
6 #include <aws/common/string.h>
7 #include <aws/http/private/h1_decoder.h>
8 #include <aws/http/private/strutil.h>
9 #include <aws/http/status_code.h>
10 #include <aws/io/logging.h>
11
12 AWS_STATIC_STRING_FROM_LITERAL(s_transfer_coding_chunked, "chunked");
13 AWS_STATIC_STRING_FROM_LITERAL(s_transfer_coding_compress, "compress");
14 AWS_STATIC_STRING_FROM_LITERAL(s_transfer_coding_x_compress, "x-compress");
15 AWS_STATIC_STRING_FROM_LITERAL(s_transfer_coding_deflate, "deflate");
16 AWS_STATIC_STRING_FROM_LITERAL(s_transfer_coding_gzip, "gzip");
17 AWS_STATIC_STRING_FROM_LITERAL(s_transfer_coding_x_gzip, "x-gzip");
18
19 /* Decoder runs a state machine.
20 * Each state consumes data until it sets the next state.
21 * A common state is the "line state", which handles consuming one line ending in CRLF
22 * and feeding the line to a linestate_fn, which should process data and set the next state.
23 */
24 typedef int(state_fn)(struct aws_h1_decoder *decoder, struct aws_byte_cursor *input);
25 typedef int(linestate_fn)(struct aws_h1_decoder *decoder, struct aws_byte_cursor input);
26
27 struct aws_h1_decoder {
28 /* Implementation data. */
29 struct aws_allocator *alloc;
30 struct aws_byte_buf scratch_space;
31 state_fn *run_state;
32 linestate_fn *process_line;
33 int transfer_encoding;
34 uint64_t content_processed;
35 uint64_t content_length;
36 uint64_t chunk_processed;
37 uint64_t chunk_size;
38 bool doing_trailers;
39 bool is_done;
40 bool body_headers_ignored;
41 bool body_headers_forbidden;
42 bool content_length_received;
43
44 enum aws_http_header_block header_block;
45 const void *logging_id;
46
47 /* User callbacks and settings. */
48 struct aws_h1_decoder_vtable vtable;
49 bool is_decoding_requests;
50 void *user_data;
51 };
52
53 static int s_linestate_request(struct aws_h1_decoder *decoder, struct aws_byte_cursor input);
54 static int s_linestate_response(struct aws_h1_decoder *decoder, struct aws_byte_cursor input);
55 static int s_linestate_header(struct aws_h1_decoder *decoder, struct aws_byte_cursor input);
56 static int s_linestate_chunk_size(struct aws_h1_decoder *decoder, struct aws_byte_cursor input);
57
58 static bool s_scan_for_crlf(struct aws_h1_decoder *decoder, struct aws_byte_cursor input, size_t *bytes_processed) {
59 AWS_ASSERT(input.len > 0);
60
61 /* In a loop, scan for "\n", then look one char back for "\r" */
62 uint8_t *ptr = input.ptr;
63 uint8_t *end = input.ptr + input.len;
64 while (ptr != end) {
65 uint8_t *newline = (uint8_t *)memchr(ptr, '\n', end - ptr);
66 if (!newline) {
67 break;
68 }
69
70 uint8_t prev_char;
71 if (newline == input.ptr) {
72 /* If "\n" is first character check scratch_space for previous character */
73 if (decoder->scratch_space.len > 0) {
74 prev_char = decoder->scratch_space.buffer[decoder->scratch_space.len - 1];
75 } else {
76 prev_char = 0;
77 }
78 } else {
79 prev_char = *(newline - 1);
80 }
81
82 if (prev_char == '\r') {
83 *bytes_processed = 1 + (newline - input.ptr);
84 return true;
85 }
86
87 ptr = newline + 1;
88 }
89
90 *bytes_processed = input.len;
91 return false;
92 }
93
94 /* This state consumes an entire line, then calls a linestate_fn to process the line. */
95 static int s_state_getline(struct aws_h1_decoder *decoder, struct aws_byte_cursor *input) {
96 /* If preceding runs of this state failed to find CRLF, their data is stored in the scratch_space
97 * and new data needs to be combined with the old data for processing. */
98 bool has_prev_data = decoder->scratch_space.len;
99
100 size_t line_length = 0;
101 bool found_crlf = s_scan_for_crlf(decoder, *input, &line_length);
102
103 /* Found end of line! Run the line processor on it */
104 struct aws_byte_cursor line = aws_byte_cursor_advance(input, line_length);
105
106 bool use_scratch = !found_crlf | has_prev_data;
107 if (AWS_UNLIKELY(use_scratch)) {
108 if (aws_byte_buf_append_dynamic(&decoder->scratch_space, &line)) {
109 AWS_LOGF_ERROR(
110 AWS_LS_HTTP_STREAM,
111 "id=%p: Internal buffer write failed with error code %d (%s)",
112 decoder->logging_id,
113 aws_last_error(),
114 aws_error_name(aws_last_error()));
115
116 return AWS_OP_ERR;
117 }
118 /* Line is actually the entire scratch buffer now */
119 line = aws_byte_cursor_from_buf(&decoder->scratch_space);
120 }
121
122 if (AWS_LIKELY(found_crlf)) {
123 /* Backup so "\r\n" is not included. */
124 /* RFC-7230 section 3 Message Format */
125 AWS_ASSERT(line.len >= 2);
126 line.len -= 2;
127
128 return decoder->process_line(decoder, line);
129 }
130
131 /* Didn't find crlf, we'll continue scanning when more data comes in */
132 return AWS_OP_SUCCESS;
133 }
134
135 static int s_cursor_split_impl(
136 struct aws_byte_cursor input,
137 char split_on,
138 struct aws_byte_cursor *cursor_array,
139 size_t num_cursors,
140 bool error_if_more_splits_possible) {
141
142 struct aws_byte_cursor split;
143 AWS_ZERO_STRUCT(split);
144 for (size_t i = 0; i < num_cursors; ++i) {
145 if (!aws_byte_cursor_next_split(&input, split_on, &split)) {
146 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
147 }
148 cursor_array[i] = split;
149 }
150
151 if (error_if_more_splits_possible) {
152 if (aws_byte_cursor_next_split(&input, split_on, &split)) {
153 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
154 }
155 } else {
156 /* Otherwise, the last cursor will contain the remainder of the string */
157 struct aws_byte_cursor *last_cursor = &cursor_array[num_cursors - 1];
158 last_cursor->len = (input.ptr + input.len) - last_cursor->ptr;
159 }
160
161 return AWS_OP_SUCCESS;
162 }
163
164 /* Final cursor contains remainder of input. */
165 static int s_cursor_split_first_n_times(
166 struct aws_byte_cursor input,
167 char split_on,
168 struct aws_byte_cursor *cursor_array,
169 size_t num_cursors) {
170
171 return s_cursor_split_impl(input, split_on, cursor_array, num_cursors, false);
172 }
173
174 /* Error if input could have been split more times */
175 static int s_cursor_split_exactly_n_times(
176 struct aws_byte_cursor input,
177 char split_on,
178 struct aws_byte_cursor *cursor_array,
179 size_t num_cursors) {
180
181 return s_cursor_split_impl(input, split_on, cursor_array, num_cursors, true);
182 }
183
184 static void s_set_state(struct aws_h1_decoder *decoder, state_fn *state) {
185 decoder->scratch_space.len = 0;
186 decoder->run_state = state;
187 decoder->process_line = NULL;
188 }
189
190 /* Set next state to capture a full line, then call the specified linestate_fn on it */
191 static void s_set_line_state(struct aws_h1_decoder *decoder, linestate_fn *line_processor) {
192 s_set_state(decoder, s_state_getline);
193 decoder->process_line = line_processor;
194 }
195
196 static int s_mark_done(struct aws_h1_decoder *decoder) {
197 decoder->is_done = true;
198
199 return decoder->vtable.on_done(decoder->user_data);
200 }
201
202 /* Reset state, in preparation for processing a new message */
203 static void s_reset_state(struct aws_h1_decoder *decoder) {
204 if (decoder->is_decoding_requests) {
205 s_set_line_state(decoder, s_linestate_request);
206 } else {
207 s_set_line_state(decoder, s_linestate_response);
208 }
209
210 decoder->transfer_encoding = 0;
211 decoder->content_processed = 0;
212 decoder->content_length = 0;
213 decoder->chunk_processed = 0;
214 decoder->chunk_size = 0;
215 decoder->doing_trailers = false;
216 decoder->is_done = false;
217 decoder->body_headers_ignored = false;
218 decoder->body_headers_forbidden = false;
219 decoder->content_length_received = false;
220 /* set to normal by default */
221 decoder->header_block = AWS_HTTP_HEADER_BLOCK_MAIN;
222 }
223
224 static int s_state_unchunked_body(struct aws_h1_decoder *decoder, struct aws_byte_cursor *input) {
225
226 size_t processed_bytes = 0;
227 AWS_FATAL_ASSERT(decoder->content_processed < decoder->content_length); /* shouldn't be possible */
228
229 if (input->len > (decoder->content_length - decoder->content_processed)) {
230 processed_bytes = (size_t)(decoder->content_length - decoder->content_processed);
231 } else {
232 processed_bytes = input->len;
233 }
234
235 decoder->content_processed += processed_bytes;
236
237 bool finished = decoder->content_processed == decoder->content_length;
238 struct aws_byte_cursor body = aws_byte_cursor_advance(input, processed_bytes);
239 int err = decoder->vtable.on_body(&body, finished, decoder->user_data);
240 if (err) {
241 return AWS_OP_ERR;
242 }
243
244 if (AWS_LIKELY(finished)) {
245 err = s_mark_done(decoder);
246 if (err) {
247 return AWS_OP_ERR;
248 }
249 }
250
251 return AWS_OP_SUCCESS;
252 }
253
254 static int s_linestate_chunk_terminator(struct aws_h1_decoder *decoder, struct aws_byte_cursor input) {
255
256 /* Expecting CRLF at end of each chunk */
257 /* RFC-7230 section 4.1 Chunked Transfer Encoding */
258 if (AWS_UNLIKELY(input.len != 0)) {
259 AWS_LOGF_ERROR(
260 AWS_LS_HTTP_STREAM, "id=%p: Incoming chunk is invalid, does not end with CRLF.", decoder->logging_id);
261 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
262 }
263
264 s_set_line_state(decoder, s_linestate_chunk_size);
265
266 return AWS_OP_SUCCESS;
267 }
268
269 static int s_state_chunk(struct aws_h1_decoder *decoder, struct aws_byte_cursor *input) {
270 size_t processed_bytes = 0;
271 AWS_ASSERT(decoder->chunk_processed < decoder->chunk_size);
272
273 if (input->len > (decoder->chunk_size - decoder->chunk_processed)) {
274 processed_bytes = (size_t)(decoder->chunk_size - decoder->chunk_processed);
275 } else {
276 processed_bytes = input->len;
277 }
278
279 decoder->chunk_processed += processed_bytes;
280
281 bool finished = decoder->chunk_processed == decoder->chunk_size;
282 struct aws_byte_cursor body = aws_byte_cursor_advance(input, processed_bytes);
283 int err = decoder->vtable.on_body(&body, false, decoder->user_data);
284 if (err) {
285 return AWS_OP_ERR;
286 }
287
288 if (AWS_LIKELY(finished)) {
289 s_set_line_state(decoder, s_linestate_chunk_terminator);
290 }
291
292 return AWS_OP_SUCCESS;
293 }
294
295 static int s_linestate_chunk_size(struct aws_h1_decoder *decoder, struct aws_byte_cursor input) {
296 struct aws_byte_cursor size;
297 AWS_ZERO_STRUCT(size);
298 if (!aws_byte_cursor_next_split(&input, ';', &size)) {
299 AWS_LOGF_ERROR(
300 AWS_LS_HTTP_STREAM, "id=%p: Incoming chunk is invalid, first line is malformed.", decoder->logging_id);
301 AWS_LOGF_DEBUG(
302 AWS_LS_HTTP_STREAM,
303 "id=%p: Bad chunk line is: '" PRInSTR "'",
304 decoder->logging_id,
305 AWS_BYTE_CURSOR_PRI(input));
306
307 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
308 }
309
310 int err = aws_byte_cursor_utf8_parse_u64_hex(size, &decoder->chunk_size);
311 if (err) {
312 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Failed to parse size of incoming chunk.", decoder->logging_id);
313 AWS_LOGF_DEBUG(
314 AWS_LS_HTTP_STREAM,
315 "id=%p: Bad chunk size is: '" PRInSTR "'",
316 decoder->logging_id,
317 AWS_BYTE_CURSOR_PRI(size));
318
319 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
320 }
321 decoder->chunk_processed = 0;
322
323 /* Empty chunk signifies all chunks have been read. */
324 if (AWS_UNLIKELY(decoder->chunk_size == 0)) {
325 struct aws_byte_cursor cursor;
326 cursor.ptr = NULL;
327 cursor.len = 0;
328 err = decoder->vtable.on_body(&cursor, true, decoder->user_data);
329 if (err) {
330 return AWS_OP_ERR;
331 }
332
333 /* Expected empty newline and end of message. */
334 decoder->doing_trailers = true;
335 s_set_line_state(decoder, s_linestate_header);
336 return AWS_OP_SUCCESS;
337 }
338
339 /* Skip all chunk extensions, as they are optional. */
340 /* RFC-7230 section 4.1.1 Chunk Extensions */
341
342 s_set_state(decoder, s_state_chunk);
343
344 return AWS_OP_SUCCESS;
345 }
346
347 static int s_linestate_header(struct aws_h1_decoder *decoder, struct aws_byte_cursor input) {
348 int err;
349
350 /* The \r\n was just processed by `s_state_getline`. */
351 /* Empty line signifies end of headers, and beginning of body or end of trailers. */
352 /* RFC-7230 section 3 Message Format */
353 if (input.len == 0) {
354 if (AWS_LIKELY(!decoder->doing_trailers)) {
355 if (decoder->body_headers_ignored) {
356 err = s_mark_done(decoder);
357 if (err) {
358 return AWS_OP_ERR;
359 }
360 } else if (decoder->transfer_encoding & AWS_HTTP_TRANSFER_ENCODING_CHUNKED) {
361 s_set_line_state(decoder, s_linestate_chunk_size);
362 } else if (decoder->content_length > 0) {
363 s_set_state(decoder, s_state_unchunked_body);
364 } else {
365 err = s_mark_done(decoder);
366 if (err) {
367 return AWS_OP_ERR;
368 }
369 }
370 } else {
371 /* Empty line means end of message. */
372 err = s_mark_done(decoder);
373 if (err) {
374 return AWS_OP_ERR;
375 }
376 }
377
378 return AWS_OP_SUCCESS;
379 }
380
381 /* Each header field consists of a case-insensitive field name followed by a colon (":"),
382 * optional leading whitespace, the field value, and optional trailing whitespace.
383 * RFC-7230 3.2 */
384 struct aws_byte_cursor splits[2];
385 err = s_cursor_split_first_n_times(input, ':', splits, 2); /* value may contain more colons */
386 if (err) {
387 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Invalid incoming header, missing colon.", decoder->logging_id);
388 AWS_LOGF_DEBUG(
389 AWS_LS_HTTP_STREAM, "id=%p: Bad header is: '" PRInSTR "'", decoder->logging_id, AWS_BYTE_CURSOR_PRI(input));
390 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
391 }
392
393 struct aws_byte_cursor name = splits[0];
394 if (!aws_strutil_is_http_token(name)) {
395 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Invalid incoming header, bad name.", decoder->logging_id);
396 AWS_LOGF_DEBUG(
397 AWS_LS_HTTP_STREAM, "id=%p: Bad header is: '" PRInSTR "'", decoder->logging_id, AWS_BYTE_CURSOR_PRI(input));
398 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
399 }
400
401 struct aws_byte_cursor value = aws_strutil_trim_http_whitespace(splits[1]);
402 if (!aws_strutil_is_http_field_value(value)) {
403 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Invalid incoming header, bad value.", decoder->logging_id);
404 AWS_LOGF_DEBUG(
405 AWS_LS_HTTP_STREAM, "id=%p: Bad header is: '" PRInSTR "'", decoder->logging_id, AWS_BYTE_CURSOR_PRI(input));
406 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
407 }
408
409 struct aws_h1_decoded_header header;
410 header.name = aws_http_str_to_header_name(name);
411 header.name_data = name;
412 header.value_data = value;
413 header.data = input;
414
415 switch (header.name) {
416 case AWS_HTTP_HEADER_CONTENT_LENGTH:
417 if (decoder->content_length_received) {
418 AWS_LOGF_ERROR(
419 AWS_LS_HTTP_STREAM,
420 "id=%p: Multiple incoming headers for content-length received. This is illegal.",
421 decoder->logging_id);
422 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
423 }
424
425 if (decoder->transfer_encoding) {
426 AWS_LOGF_ERROR(
427 AWS_LS_HTTP_STREAM,
428 "id=%p: Incoming headers for both content-length and transfer-encoding received. This is illegal.",
429 decoder->logging_id);
430 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
431 }
432
433 if (aws_byte_cursor_utf8_parse_u64(header.value_data, &decoder->content_length)) {
434 AWS_LOGF_ERROR(
435 AWS_LS_HTTP_STREAM,
436 "id=%p: Incoming content-length header has invalid value.",
437 decoder->logging_id);
438 AWS_LOGF_DEBUG(
439 AWS_LS_HTTP_STREAM,
440 "id=%p: Bad content-length value is: '" PRInSTR "'",
441 decoder->logging_id,
442 AWS_BYTE_CURSOR_PRI(header.value_data));
443 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
444 }
445
446 if (decoder->body_headers_forbidden && decoder->content_length != 0) {
447 AWS_LOGF_ERROR(
448 AWS_LS_HTTP_STREAM,
449 "id=%p: Incoming headers for content-length received, but it is illegal for this message to have a "
450 "body",
451 decoder->logging_id);
452 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
453 }
454
455 decoder->content_length_received = true;
456 break;
457
458 case AWS_HTTP_HEADER_TRANSFER_ENCODING: {
459 if (decoder->content_length_received) {
460 AWS_LOGF_ERROR(
461 AWS_LS_HTTP_STREAM,
462 "id=%p: Incoming headers for both content-length and transfer-encoding received. This is illegal.",
463 decoder->logging_id);
464 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
465 }
466
467 if (decoder->body_headers_forbidden) {
468 AWS_LOGF_ERROR(
469 AWS_LS_HTTP_STREAM,
470 "id=%p: Incoming headers for transfer-encoding received, but it is illegal for this message to "
471 "have a body",
472 decoder->logging_id);
473 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
474 }
475 /* RFC-7230 section 3.3.1 Transfer-Encoding */
476 /* RFC-7230 section 4.2 Compression Codings */
477
478 /* Note that it's possible for multiple Transfer-Encoding headers to exist, in which case the values
479 * should be appended with those from any previously encountered Transfer-Encoding headers. */
480 struct aws_byte_cursor split;
481 AWS_ZERO_STRUCT(split);
482 while (aws_byte_cursor_next_split(&header.value_data, ',', &split)) {
483 struct aws_byte_cursor coding = aws_strutil_trim_http_whitespace(split);
484
485 /* A sender MUST NOT apply chunked more than once to a message body.
486 * If any transfer coding other than chunked is applied to a request payload body, the sender MUST
487 * apply chunked as the final transfer coding to ensure that the message is properly framed.
488 * RFC-7230 3.3.1 */
489 if (decoder->transfer_encoding & AWS_HTTP_TRANSFER_ENCODING_CHUNKED) {
490 AWS_LOGF_ERROR(
491 AWS_LS_HTTP_STREAM,
492 "id=%p: Incoming transfer-encoding header lists a coding after 'chunked', this is illegal.",
493 decoder->logging_id);
494 AWS_LOGF_DEBUG(
495 AWS_LS_HTTP_STREAM,
496 "id=%p: Misplaced coding is '" PRInSTR "'",
497 decoder->logging_id,
498 AWS_BYTE_CURSOR_PRI(coding));
499 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
500 }
501
502 if (aws_string_eq_byte_cursor_ignore_case(s_transfer_coding_chunked, &coding)) {
503 decoder->transfer_encoding |= AWS_HTTP_TRANSFER_ENCODING_CHUNKED;
504
505 } else if (
506 aws_string_eq_byte_cursor_ignore_case(s_transfer_coding_compress, &coding) ||
507 aws_string_eq_byte_cursor_ignore_case(s_transfer_coding_x_compress, &coding)) {
508 /* A recipient SHOULD consider "x-compress" to be equivalent to "compress". RFC-7230 4.2.1 */
509 decoder->transfer_encoding |= AWS_HTTP_TRANSFER_ENCODING_DEPRECATED_COMPRESS;
510
511 } else if (aws_string_eq_byte_cursor_ignore_case(s_transfer_coding_deflate, &coding)) {
512 decoder->transfer_encoding |= AWS_HTTP_TRANSFER_ENCODING_DEFLATE;
513
514 } else if (
515 aws_string_eq_byte_cursor_ignore_case(s_transfer_coding_gzip, &coding) ||
516 aws_string_eq_byte_cursor_ignore_case(s_transfer_coding_x_gzip, &coding)) {
517 /* A recipient SHOULD consider "x-gzip" to be equivalent to "gzip". RFC-7230 4.2.3 */
518 decoder->transfer_encoding |= AWS_HTTP_TRANSFER_ENCODING_GZIP;
519
520 } else if (coding.len > 0) {
521 AWS_LOGF_ERROR(
522 AWS_LS_HTTP_STREAM,
523 "id=%p: Incoming transfer-encoding header lists unrecognized coding.",
524 decoder->logging_id);
525 AWS_LOGF_DEBUG(
526 AWS_LS_HTTP_STREAM,
527 "id=%p: Unrecognized coding is: '" PRInSTR "'",
528 decoder->logging_id,
529 AWS_BYTE_CURSOR_PRI(coding));
530 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
531 } else {
532 AWS_LOGF_ERROR(
533 AWS_LS_HTTP_STREAM,
534 "id=%p: Incoming transfer-encoding header has invalid blank entry.",
535 decoder->logging_id);
536 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
537 }
538 }
539
540 /* TODO: deal with body of indeterminate length, marking it as successful when connection is closed:
541 *
542 * A response that has neither chunked transfer coding nor Content-Length is terminated by closure of
543 * the connection and, thus, is considered complete regardless of the number of message body octets
544 * received, provided that the header section was received intact.
545 * RFC-7230 3.4 */
546 } break;
547
548 default:
549 break;
550 }
551
552 err = decoder->vtable.on_header(&header, decoder->user_data);
553 if (err) {
554 return AWS_OP_ERR;
555 }
556
557 s_set_line_state(decoder, s_linestate_header);
558
559 return AWS_OP_SUCCESS;
560 }
561
562 static int s_linestate_request(struct aws_h1_decoder *decoder, struct aws_byte_cursor input) {
563 struct aws_byte_cursor cursors[3];
564 int err = s_cursor_split_exactly_n_times(input, ' ', cursors, 3); /* extra spaces not allowed */
565 if (err) {
566 AWS_LOGF_ERROR(
567 AWS_LS_HTTP_STREAM, "id=%p: Incoming request line has wrong number of spaces.", decoder->logging_id);
568 AWS_LOGF_DEBUG(
569 AWS_LS_HTTP_STREAM,
570 "id=%p: Bad request line is: '" PRInSTR "'",
571 decoder->logging_id,
572 AWS_BYTE_CURSOR_PRI(input));
573 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
574 }
575
576 for (size_t i = 0; i < AWS_ARRAY_SIZE(cursors); ++i) {
577 if (cursors[i].len == 0) {
578 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Incoming request line has empty values.", decoder->logging_id);
579 AWS_LOGF_DEBUG(
580 AWS_LS_HTTP_STREAM,
581 "id=%p: Bad request line is: '" PRInSTR "'",
582 decoder->logging_id,
583 AWS_BYTE_CURSOR_PRI(input));
584 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
585 }
586 }
587
588 struct aws_byte_cursor method = cursors[0];
589 struct aws_byte_cursor uri = cursors[1];
590 struct aws_byte_cursor version = cursors[2];
591
592 if (!aws_strutil_is_http_token(method)) {
593 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Incoming request has invalid method.", decoder->logging_id);
594 AWS_LOGF_DEBUG(
595 AWS_LS_HTTP_STREAM,
596 "id=%p: Bad request line is: '" PRInSTR "'",
597 decoder->logging_id,
598 AWS_BYTE_CURSOR_PRI(input));
599 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
600 }
601
602 if (!aws_strutil_is_http_request_target(uri)) {
603 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Incoming request has invalid path.", decoder->logging_id);
604 AWS_LOGF_DEBUG(
605 AWS_LS_HTTP_STREAM,
606 "id=%p: Bad request line is: '" PRInSTR "'",
607 decoder->logging_id,
608 AWS_BYTE_CURSOR_PRI(input));
609 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
610 }
611
612 struct aws_byte_cursor version_expected = aws_http_version_to_str(AWS_HTTP_VERSION_1_1);
613 if (!aws_byte_cursor_eq(&version, &version_expected)) {
614 AWS_LOGF_ERROR(
615 AWS_LS_HTTP_STREAM, "id=%p: Incoming request uses unsupported HTTP version.", decoder->logging_id);
616 AWS_LOGF_DEBUG(
617 AWS_LS_HTTP_STREAM,
618 "id=%p: Unsupported version is: '" PRInSTR "'",
619 decoder->logging_id,
620 AWS_BYTE_CURSOR_PRI(version));
621 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
622 }
623
624 err = decoder->vtable.on_request(aws_http_str_to_method(method), &method, &uri, decoder->user_data);
625 if (err) {
626 return AWS_OP_ERR;
627 }
628
629 s_set_line_state(decoder, s_linestate_header);
630
631 return AWS_OP_SUCCESS;
632 }
633
634 static bool s_check_info_response_status_code(int code_val) {
635 return code_val >= 100 && code_val < 200;
636 }
637
638 static int s_linestate_response(struct aws_h1_decoder *decoder, struct aws_byte_cursor input) {
639 struct aws_byte_cursor cursors[3];
640 int err = s_cursor_split_first_n_times(input, ' ', cursors, 3); /* phrase may contain spaces */
641 if (err) {
642 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Incoming response status line is invalid.", decoder->logging_id);
643 AWS_LOGF_DEBUG(
644 AWS_LS_HTTP_STREAM,
645 "id=%p: Bad status line is: '" PRInSTR "'",
646 decoder->logging_id,
647 AWS_BYTE_CURSOR_PRI(input));
648 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
649 }
650
651 struct aws_byte_cursor version = cursors[0];
652 struct aws_byte_cursor code = cursors[1];
653 struct aws_byte_cursor phrase = cursors[2];
654
655 struct aws_byte_cursor version_1_1_expected = aws_http_version_to_str(AWS_HTTP_VERSION_1_1);
656 struct aws_byte_cursor version_1_0_expected = aws_http_version_to_str(AWS_HTTP_VERSION_1_0);
657 if (!aws_byte_cursor_eq(&version, &version_1_1_expected) && !aws_byte_cursor_eq(&version, &version_1_0_expected)) {
658 AWS_LOGF_ERROR(
659 AWS_LS_HTTP_STREAM, "id=%p: Incoming response uses unsupported HTTP version.", decoder->logging_id);
660 AWS_LOGF_DEBUG(
661 AWS_LS_HTTP_STREAM,
662 "id=%p: Unsupported version is: '" PRInSTR "'",
663 decoder->logging_id,
664 AWS_BYTE_CURSOR_PRI(version));
665 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
666 }
667
668 /* Validate phrase */
669 if (!aws_strutil_is_http_reason_phrase(phrase)) {
670 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Incoming response has invalid reason phrase.", decoder->logging_id);
671 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
672 }
673
674 /* Status-code is a 3-digit integer. RFC7230 section 3.1.2 */
675 uint64_t code_val_u64;
676 err = aws_byte_cursor_utf8_parse_u64(code, &code_val_u64);
677 if (err || code.len != 3 || code_val_u64 > 999) {
678 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Incoming response has invalid status code.", decoder->logging_id);
679 AWS_LOGF_DEBUG(
680 AWS_LS_HTTP_STREAM,
681 "id=%p: Bad status code is: '" PRInSTR "'",
682 decoder->logging_id,
683 AWS_BYTE_CURSOR_PRI(code));
684 return aws_raise_error(AWS_ERROR_HTTP_PROTOCOL_ERROR);
685 }
686
687 int code_val = (int)code_val_u64;
688
689 /* RFC-7230 section 3.3 Message Body */
690 decoder->body_headers_ignored |= code_val == AWS_HTTP_STATUS_CODE_304_NOT_MODIFIED;
691 decoder->body_headers_forbidden = code_val == AWS_HTTP_STATUS_CODE_204_NO_CONTENT || code_val / 100 == 1;
692
693 if (s_check_info_response_status_code(code_val)) {
694 decoder->header_block = AWS_HTTP_HEADER_BLOCK_INFORMATIONAL;
695 }
696
697 err = decoder->vtable.on_response(code_val, decoder->user_data);
698 if (err) {
699 return AWS_OP_ERR;
700 }
701
702 s_set_line_state(decoder, s_linestate_header);
703 return AWS_OP_SUCCESS;
704 }
705
706 struct aws_h1_decoder *aws_h1_decoder_new(struct aws_h1_decoder_params *params) {
707 AWS_ASSERT(params);
708
709 struct aws_h1_decoder *decoder = aws_mem_acquire(params->alloc, sizeof(struct aws_h1_decoder));
710 if (!decoder) {
711 return NULL;
712 }
713 AWS_ZERO_STRUCT(*decoder);
714
715 decoder->alloc = params->alloc;
716 decoder->user_data = params->user_data;
717 decoder->vtable = params->vtable;
718 decoder->is_decoding_requests = params->is_decoding_requests;
719
720 aws_byte_buf_init(&decoder->scratch_space, params->alloc, params->scratch_space_initial_size);
721
722 s_reset_state(decoder);
723
724 return decoder;
725 }
726
727 void aws_h1_decoder_destroy(struct aws_h1_decoder *decoder) {
728 if (!decoder) {
729 return;
730 }
731 aws_byte_buf_clean_up(&decoder->scratch_space);
732 aws_mem_release(decoder->alloc, decoder);
733 }
734
735 int aws_h1_decode(struct aws_h1_decoder *decoder, struct aws_byte_cursor *data) {
736 AWS_ASSERT(decoder);
737 AWS_ASSERT(data);
738
739 struct aws_byte_cursor backup = *data;
740
741 while (data->len && !decoder->is_done) {
742 int err = decoder->run_state(decoder, data);
743 if (err) {
744 /* Reset the data param to how we found it */
745 *data = backup;
746 return AWS_OP_ERR;
747 }
748 }
749
750 if (decoder->is_done) {
751 s_reset_state(decoder);
752 }
753
754 return AWS_OP_SUCCESS;
755 }
756
757 int aws_h1_decoder_get_encoding_flags(const struct aws_h1_decoder *decoder) {
758 return decoder->transfer_encoding;
759 }
760
761 uint64_t aws_h1_decoder_get_content_length(const struct aws_h1_decoder *decoder) {
762 return decoder->content_length;
763 }
764
765 bool aws_h1_decoder_get_body_headers_ignored(const struct aws_h1_decoder *decoder) {
766 return decoder->body_headers_ignored;
767 }
768
769 enum aws_http_header_block aws_h1_decoder_get_header_block(const struct aws_h1_decoder *decoder) {
770 return decoder->header_block;
771 }
772
773 void aws_h1_decoder_set_logging_id(struct aws_h1_decoder *decoder, const void *id) {
774 decoder->logging_id = id;
775 }
776
777 void aws_h1_decoder_set_body_headers_ignored(struct aws_h1_decoder *decoder, bool body_headers_ignored) {
778 decoder->body_headers_ignored = body_headers_ignored;
779 }