Debian Codegraph @

crt/aws-c-http/source/request_response.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/array_list.h>
7 #include <aws/common/mutex.h>
8 #include <aws/common/string.h>
9 #include <aws/http/private/connection_impl.h>
10 #include <aws/http/private/request_response_impl.h>
11 #include <aws/http/private/strutil.h>
12 #include <aws/http/server.h>
13 #include <aws/http/status_code.h>
14 #include <aws/io/logging.h>
15 #include <aws/io/stream.h>
16
17 #ifdef _MSC_VER
18 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
19 #endif
20
21 enum {
22 /* Initial capacity for the aws_http_message.headers array_list. */
23 AWS_HTTP_REQUEST_NUM_RESERVED_HEADERS = 16,
24 };
25
26 bool aws_http_header_name_eq(struct aws_byte_cursor name_a, struct aws_byte_cursor name_b) {
27 return aws_byte_cursor_eq_ignore_case(&name_a, &name_b);
28 }
29
30 /**
31 * -- Data Structure Notes --
32 * Headers are stored in a linear array, rather than a hash-table of arrays.
33 * The linear array was simpler to implement and may be faster due to having fewer allocations.
34 * The API has been designed so we can swap out the implementation later if desired.
35 *
36 * -- String Storage Notes --
37 * We use a single allocation to hold the name and value of each aws_http_header.
38 * We could optimize storage by using something like a string pool. If we do this, be sure to maintain
39 * the address of existing strings when adding new strings (a dynamic aws_byte_buf would not suffice).
40 */
41 struct aws_http_headers {
42 struct aws_allocator *alloc;
43 struct aws_array_list array_list; /* Contains aws_http_header */
44 struct aws_atomic_var refcount;
45 };
46
47 struct aws_http_headers *aws_http_headers_new(struct aws_allocator *allocator) {
48 AWS_PRECONDITION(allocator);
49
50 struct aws_http_headers *headers = aws_mem_calloc(allocator, 1, sizeof(struct aws_http_headers));
51 if (!headers) {
52 goto alloc_failed;
53 }
54
55 headers->alloc = allocator;
56 aws_atomic_init_int(&headers->refcount, 1);
57
58 if (aws_array_list_init_dynamic(
59 &headers->array_list, allocator, AWS_HTTP_REQUEST_NUM_RESERVED_HEADERS, sizeof(struct aws_http_header))) {
60 goto array_list_failed;
61 }
62
63 return headers;
64
65 array_list_failed:
66 aws_mem_release(headers->alloc, headers);
67 alloc_failed:
68 return NULL;
69 }
70
71 void aws_http_headers_release(struct aws_http_headers *headers) {
72 AWS_PRECONDITION(!headers || headers->alloc);
73 if (!headers) {
74 return;
75 }
76
77 size_t prev_refcount = aws_atomic_fetch_sub(&headers->refcount, 1);
78 if (prev_refcount == 1) {
79 aws_http_headers_clear(headers);
80 aws_array_list_clean_up(&headers->array_list);
81 aws_mem_release(headers->alloc, headers);
82 } else {
83 AWS_ASSERT(prev_refcount != 0);
84 }
85 }
86
87 void aws_http_headers_acquire(struct aws_http_headers *headers) {
88 AWS_PRECONDITION(headers);
89 aws_atomic_fetch_add(&headers->refcount, 1);
90 }
91
92 static int s_http_headers_add_header_impl(
93 struct aws_http_headers *headers,
94 const struct aws_http_header *header_orig,
95 bool front) {
96
97 AWS_PRECONDITION(headers);
98 AWS_PRECONDITION(header_orig);
99 AWS_PRECONDITION(aws_byte_cursor_is_valid(&header_orig->name) && aws_byte_cursor_is_valid(&header_orig->value));
100
101 struct aws_http_header header_copy = *header_orig;
102
103 if (header_copy.name.len == 0) {
104 return aws_raise_error(AWS_ERROR_HTTP_INVALID_HEADER_NAME);
105 }
106
107 /* Whitespace around header values is ignored (RFC-7230 - Section 3.2).
108 * Trim it off here, so anyone querying this value has an easier time. */
109 header_copy.value = aws_strutil_trim_http_whitespace(header_copy.value);
110
111 size_t total_len;
112 if (aws_add_size_checked(header_copy.name.len, header_copy.value.len, &total_len)) {
113 return AWS_OP_ERR;
114 }
115
116 /* Store our own copy of the strings.
117 * We put the name and value into the same allocation. */
118 uint8_t *strmem = aws_mem_acquire(headers->alloc, total_len);
119
120 struct aws_byte_buf strbuf = aws_byte_buf_from_empty_array(strmem, total_len);
121 aws_byte_buf_append_and_update(&strbuf, &header_copy.name);
122 aws_byte_buf_append_and_update(&strbuf, &header_copy.value);
123 if (front) {
124 if (aws_array_list_push_front(&headers->array_list, &header_copy)) {
125 goto error;
126 }
127 } else {
128 if (aws_array_list_push_back(&headers->array_list, &header_copy)) {
129 goto error;
130 }
131 }
132
133 return AWS_OP_SUCCESS;
134
135 error:
136 aws_mem_release(headers->alloc, strmem);
137 return AWS_OP_ERR;
138 }
139
140 int aws_http_headers_add_header(struct aws_http_headers *headers, const struct aws_http_header *header) {
141 /* Add pseudo headers to the front and not checking any violation until we send the header to the wire */
142 bool pseudo = aws_strutil_is_http_pseudo_header_name(header->name);
143 bool front = false;
144 if (pseudo && aws_http_headers_count(headers)) {
145 struct aws_http_header last_header;
146 /* TODO: instead if checking the last header, maybe we can add the pseudo headers to the end of the existing
147 * pseudo headers, which needs to insert to the middle of the array list. */
148 AWS_ZERO_STRUCT(last_header);
149 aws_http_headers_get_index(headers, aws_http_headers_count(headers) - 1, &last_header);
150 front = !aws_strutil_is_http_pseudo_header_name(last_header.name);
151 }
152 return s_http_headers_add_header_impl(headers, header, front);
153 }
154
155 int aws_http_headers_add(struct aws_http_headers *headers, struct aws_byte_cursor name, struct aws_byte_cursor value) {
156 struct aws_http_header header = {.name = name, .value = value};
157 return aws_http_headers_add_header(headers, &header);
158 }
159
160 void aws_http_headers_clear(struct aws_http_headers *headers) {
161 AWS_PRECONDITION(headers);
162
163 struct aws_http_header *header = NULL;
164 const size_t count = aws_http_headers_count(headers);
165 for (size_t i = 0; i < count; ++i) {
166 aws_array_list_get_at_ptr(&headers->array_list, (void **)&header, i);
167 AWS_ASSUME(header);
168
169 /* Storage for name & value is in the same allocation */
170 aws_mem_release(headers->alloc, header->name.ptr);
171 }
172
173 aws_array_list_clear(&headers->array_list);
174 }
175
176 /* Does not check index */
177 static void s_http_headers_erase_index(struct aws_http_headers *headers, size_t index) {
178 struct aws_http_header *header = NULL;
179 aws_array_list_get_at_ptr(&headers->array_list, (void **)&header, index);
180 AWS_ASSUME(header);
181
182 /* Storage for name & value is in the same allocation */
183 aws_mem_release(headers->alloc, header->name.ptr);
184
185 aws_array_list_erase(&headers->array_list, index);
186 }
187
188 int aws_http_headers_erase_index(struct aws_http_headers *headers, size_t index) {
189 AWS_PRECONDITION(headers);
190
191 if (index >= aws_http_headers_count(headers)) {
192 return aws_raise_error(AWS_ERROR_INVALID_INDEX);
193 }
194
195 s_http_headers_erase_index(headers, index);
196 return AWS_OP_SUCCESS;
197 }
198
199 /* Erase entries with name, stop at end_index */
200 static int s_http_headers_erase(
201 struct aws_http_headers *headers,
202 struct aws_byte_cursor name,
203 size_t start_index,
204 size_t end_index) {
205 bool erased_any = false;
206 struct aws_http_header *header = NULL;
207
208 /* Iterating in reverse is simpler */
209 for (size_t n = end_index; n > start_index; --n) {
210 const size_t i = n - 1;
211
212 aws_array_list_get_at_ptr(&headers->array_list, (void **)&header, i);
213 AWS_ASSUME(header);
214
215 if (aws_http_header_name_eq(header->name, name)) {
216 s_http_headers_erase_index(headers, i);
217 erased_any = true;
218 }
219 }
220
221 if (!erased_any) {
222 return aws_raise_error(AWS_ERROR_HTTP_HEADER_NOT_FOUND);
223 }
224
225 return AWS_OP_SUCCESS;
226 }
227
228 int aws_http_headers_erase(struct aws_http_headers *headers, struct aws_byte_cursor name) {
229 AWS_PRECONDITION(headers);
230 AWS_PRECONDITION(aws_byte_cursor_is_valid(&name));
231
232 return s_http_headers_erase(headers, name, 0, aws_http_headers_count(headers));
233 }
234
235 int aws_http_headers_erase_value(
236 struct aws_http_headers *headers,
237 struct aws_byte_cursor name,
238 struct aws_byte_cursor value) {
239
240 AWS_PRECONDITION(headers);
241 AWS_PRECONDITION(aws_byte_cursor_is_valid(&name) && aws_byte_cursor_is_valid(&value));
242
243 struct aws_http_header *header = NULL;
244 const size_t count = aws_http_headers_count(headers);
245 for (size_t i = 0; i < count; ++i) {
246 aws_array_list_get_at_ptr(&headers->array_list, (void **)&header, i);
247 AWS_ASSUME(header);
248
249 if (aws_http_header_name_eq(header->name, name) && aws_byte_cursor_eq(&header->value, &value)) {
250 s_http_headers_erase_index(headers, i);
251 return AWS_OP_SUCCESS;
252 }
253 }
254
255 return aws_raise_error(AWS_ERROR_HTTP_HEADER_NOT_FOUND);
256 }
257
258 int aws_http_headers_add_array(struct aws_http_headers *headers, const struct aws_http_header *array, size_t count) {
259 AWS_PRECONDITION(headers);
260 AWS_PRECONDITION(AWS_MEM_IS_READABLE(array, count));
261
262 const size_t orig_count = aws_http_headers_count(headers);
263
264 for (size_t i = 0; i < count; ++i) {
265 if (aws_http_headers_add_header(headers, &array[i])) {
266 goto error;
267 }
268 }
269
270 return AWS_OP_SUCCESS;
271
272 error:
273 /* Erase headers from the end until we're back to our previous state */
274 for (size_t new_count = aws_http_headers_count(headers); new_count > orig_count; --new_count) {
275 s_http_headers_erase_index(headers, new_count - 1);
276 }
277
278 return AWS_OP_ERR;
279 }
280
281 int aws_http_headers_set(struct aws_http_headers *headers, struct aws_byte_cursor name, struct aws_byte_cursor value) {
282 AWS_PRECONDITION(headers);
283 AWS_PRECONDITION(aws_byte_cursor_is_valid(&name) && aws_byte_cursor_is_valid(&value));
284
285 const size_t prev_count = aws_http_headers_count(headers);
286 bool pseudo = aws_strutil_is_http_pseudo_header_name(name);
287 const size_t start = pseudo ? 1 : 0;
288 struct aws_http_header header = {.name = name, .value = value};
289 if (s_http_headers_add_header_impl(headers, &header, pseudo)) {
290 return AWS_OP_ERR;
291 }
292 /* Erase pre-existing headers AFTER add, in case name or value was referencing their memory. */
293 s_http_headers_erase(headers, name, start, prev_count);
294 return AWS_OP_SUCCESS;
295 }
296
297 size_t aws_http_headers_count(const struct aws_http_headers *headers) {
298 AWS_PRECONDITION(headers);
299
300 return aws_array_list_length(&headers->array_list);
301 }
302
303 int aws_http_headers_get_index(
304 const struct aws_http_headers *headers,
305 size_t index,
306 struct aws_http_header *out_header) {
307
308 AWS_PRECONDITION(headers);
309 AWS_PRECONDITION(out_header);
310
311 return aws_array_list_get_at(&headers->array_list, out_header, index);
312 }
313
314 /* RFC-9110 - 5.3
315 * A recipient MAY combine multiple field lines within a field section that
316 * have the same field name into one field line, without changing the semantics
317 * of the message, by appending each subsequent field line value to the initial
318 * field line value in order, separated by a comma (",") and optional whitespace
319 * (OWS, defined in Section 5.6.3). For consistency, use comma SP. */
320 AWS_HTTP_API
321 struct aws_string *aws_http_headers_get_all(const struct aws_http_headers *headers, struct aws_byte_cursor name) {
322
323 AWS_PRECONDITION(headers);
324 AWS_PRECONDITION(aws_byte_cursor_is_valid(&name));
325
326 struct aws_string *value_str = NULL;
327
328 const struct aws_byte_cursor separator = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(", ");
329
330 struct aws_byte_buf value_builder;
331 aws_byte_buf_init(&value_builder, headers->alloc, 0);
332 bool found = false;
333 struct aws_http_header *header = NULL;
334 const size_t count = aws_http_headers_count(headers);
335 for (size_t i = 0; i < count; ++i) {
336 aws_array_list_get_at_ptr(&headers->array_list, (void **)&header, i);
337 if (aws_http_header_name_eq(name, header->name)) {
338 if (!found) {
339 found = true;
340 } else {
341 aws_byte_buf_append_dynamic(&value_builder, &separator);
342 }
343 aws_byte_buf_append_dynamic(&value_builder, &header->value);
344 }
345 }
346
347 if (found) {
348 value_str = aws_string_new_from_buf(headers->alloc, &value_builder);
349 } else {
350 aws_raise_error(AWS_ERROR_HTTP_HEADER_NOT_FOUND);
351 }
352
353 aws_byte_buf_clean_up(&value_builder);
354 return value_str;
355 }
356
357 int aws_http_headers_get(
358 const struct aws_http_headers *headers,
359 struct aws_byte_cursor name,
360 struct aws_byte_cursor *out_value) {
361
362 AWS_PRECONDITION(headers);
363 AWS_PRECONDITION(out_value);
364 AWS_PRECONDITION(aws_byte_cursor_is_valid(&name));
365
366 struct aws_http_header *header = NULL;
367 const size_t count = aws_http_headers_count(headers);
368 for (size_t i = 0; i < count; ++i) {
369 aws_array_list_get_at_ptr(&headers->array_list, (void **)&header, i);
370 AWS_ASSUME(header);
371
372 if (aws_http_header_name_eq(header->name, name)) {
373 *out_value = header->value;
374 return AWS_OP_SUCCESS;
375 }
376 }
377
378 return aws_raise_error(AWS_ERROR_HTTP_HEADER_NOT_FOUND);
379 }
380
381 bool aws_http_headers_has(const struct aws_http_headers *headers, struct aws_byte_cursor name) {
382
383 struct aws_byte_cursor out_value;
384 if (aws_http_headers_get(headers, name, &out_value)) {
385 return false;
386 }
387 return true;
388 }
389
390 int aws_http2_headers_get_request_method(
391 const struct aws_http_headers *h2_headers,
392 struct aws_byte_cursor *out_method) {
393 return aws_http_headers_get(h2_headers, aws_http_header_method, out_method);
394 }
395
396 int aws_http2_headers_get_request_scheme(
397 const struct aws_http_headers *h2_headers,
398 struct aws_byte_cursor *out_scheme) {
399 return aws_http_headers_get(h2_headers, aws_http_header_scheme, out_scheme);
400 }
401
402 int aws_http2_headers_get_request_authority(
403 const struct aws_http_headers *h2_headers,
404 struct aws_byte_cursor *out_authority) {
405 return aws_http_headers_get(h2_headers, aws_http_header_authority, out_authority);
406 }
407
408 int aws_http2_headers_get_request_path(const struct aws_http_headers *h2_headers, struct aws_byte_cursor *out_path) {
409 return aws_http_headers_get(h2_headers, aws_http_header_path, out_path);
410 }
411
412 int aws_http2_headers_get_response_status(const struct aws_http_headers *h2_headers, int *out_status_code) {
413 struct aws_byte_cursor status_code_cur;
414 int return_code = aws_http_headers_get(h2_headers, aws_http_header_status, &status_code_cur);
415 if (return_code == AWS_OP_SUCCESS) {
416 uint64_t code_val_u64;
417 if (aws_byte_cursor_utf8_parse_u64(status_code_cur, &code_val_u64)) {
418 return AWS_OP_ERR;
419 }
420 *out_status_code = (int)code_val_u64;
421 }
422 return return_code;
423 }
424
425 int aws_http2_headers_set_request_method(struct aws_http_headers *h2_headers, struct aws_byte_cursor method) {
426 return aws_http_headers_set(h2_headers, aws_http_header_method, method);
427 }
428
429 int aws_http2_headers_set_request_scheme(struct aws_http_headers *h2_headers, struct aws_byte_cursor scheme) {
430 return aws_http_headers_set(h2_headers, aws_http_header_scheme, scheme);
431 }
432
433 int aws_http2_headers_set_request_authority(struct aws_http_headers *h2_headers, struct aws_byte_cursor authority) {
434 return aws_http_headers_set(h2_headers, aws_http_header_authority, authority);
435 }
436
437 int aws_http2_headers_set_request_path(struct aws_http_headers *h2_headers, struct aws_byte_cursor path) {
438 return aws_http_headers_set(h2_headers, aws_http_header_path, path);
439 }
440
441 int aws_http2_headers_set_response_status(struct aws_http_headers *h2_headers, int status_code) {
442 /* Status code must fit in 3 digits */
443 if (status_code < 0 || status_code > 999) {
444 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
445 }
446 char status_code_str[4] = "000";
447 snprintf(status_code_str, sizeof(status_code_str), "%03d", status_code);
448 struct aws_byte_cursor status_code_cur = aws_byte_cursor_from_c_str(status_code_str);
449 return aws_http_headers_set(h2_headers, aws_http_header_status, status_code_cur);
450 }
451
452 struct aws_http_message {
453 struct aws_allocator *allocator;
454 struct aws_http_headers *headers;
455 struct aws_input_stream *body_stream;
456 struct aws_atomic_var refcount;
457 enum aws_http_version http_version;
458
459 /* Data specific to the request or response subclasses */
460 union {
461 struct aws_http_message_request_data {
462 struct aws_string *method;
463 struct aws_string *path;
464 } request;
465 struct aws_http_message_response_data {
466 int status;
467 } response;
468 } subclass_data;
469
470 struct aws_http_message_request_data *request_data;
471 struct aws_http_message_response_data *response_data;
472 };
473
474 static int s_set_string_from_cursor(
475 struct aws_string **dst,
476 struct aws_byte_cursor cursor,
477 struct aws_allocator *alloc) {
478
479 AWS_PRECONDITION(dst);
480
481 /* If the cursor is empty, set dst to NULL */
482 struct aws_string *new_str;
483 if (cursor.len) {
484 new_str = aws_string_new_from_cursor(alloc, &cursor);
485 if (!new_str) {
486 return AWS_OP_ERR;
487 }
488 } else {
489 new_str = NULL;
490 }
491
492 /* Replace existing value */
493 aws_string_destroy(*dst);
494
495 *dst = new_str;
496 return AWS_OP_SUCCESS;
497 }
498 static struct aws_http_message *s_message_new_common(
499 struct aws_allocator *allocator,
500 struct aws_http_headers *existing_headers) {
501
502 /* allocation cannot fail */
503 struct aws_http_message *message = aws_mem_calloc(allocator, 1, sizeof(struct aws_http_message));
504
505 message->allocator = allocator;
506 aws_atomic_init_int(&message->refcount, 1);
507
508 if (existing_headers) {
509 message->headers = existing_headers;
510 aws_http_headers_acquire(message->headers);
511 } else {
512 message->headers = aws_http_headers_new(allocator);
513 if (!message->headers) {
514 goto error;
515 }
516 }
517
518 return message;
519 error:
520 aws_http_message_destroy(message);
521 return NULL;
522 }
523
524 static struct aws_http_message *s_message_new_request_common(
525 struct aws_allocator *allocator,
526 struct aws_http_headers *existing_headers,
527 enum aws_http_version version) {
528
529 struct aws_http_message *message = s_message_new_common(allocator, existing_headers);
530 if (message) {
531 message->request_data = &message->subclass_data.request;
532 message->http_version = version;
533 }
534 return message;
535 }
536
537 struct aws_http_message *aws_http_message_new_request_with_headers(
538 struct aws_allocator *allocator,
539 struct aws_http_headers *existing_headers) {
540
541 AWS_PRECONDITION(allocator);
542 AWS_PRECONDITION(existing_headers);
543
544 return s_message_new_request_common(allocator, existing_headers, AWS_HTTP_VERSION_1_1);
545 }
546
547 struct aws_http_message *aws_http_message_new_request(struct aws_allocator *allocator) {
548 AWS_PRECONDITION(allocator);
549 return s_message_new_request_common(allocator, NULL, AWS_HTTP_VERSION_1_1);
550 }
551
552 struct aws_http_message *aws_http2_message_new_request(struct aws_allocator *allocator) {
553 AWS_PRECONDITION(allocator);
554 return s_message_new_request_common(allocator, NULL, AWS_HTTP_VERSION_2);
555 }
556
557 static struct aws_http_message *s_http_message_new_response_common(
558 struct aws_allocator *allocator,
559 enum aws_http_version version) {
560 AWS_PRECONDITION(allocator);
561
562 struct aws_http_message *message = s_message_new_common(allocator, NULL);
563 if (message) {
564 message->response_data = &message->subclass_data.response;
565 message->response_data->status = AWS_HTTP_STATUS_CODE_UNKNOWN;
566 message->http_version = version;
567 }
568 return message;
569 }
570
571 struct aws_http_message *aws_http_message_new_response(struct aws_allocator *allocator) {
572 AWS_PRECONDITION(allocator);
573 return s_http_message_new_response_common(allocator, AWS_HTTP_VERSION_1_1);
574 }
575
576 struct aws_http_message *aws_http2_message_new_response(struct aws_allocator *allocator) {
577 AWS_PRECONDITION(allocator);
578 return s_http_message_new_response_common(allocator, AWS_HTTP_VERSION_2);
579 }
580
581 void aws_http_message_destroy(struct aws_http_message *message) {
582 aws_http_message_release(message);
583 }
584
585 struct aws_http_message *aws_http_message_release(struct aws_http_message *message) {
586 /* Note that release() may also be used by new() functions to clean up if something goes wrong */
587 AWS_PRECONDITION(!message || message->allocator);
588 if (!message) {
589 return NULL;
590 }
591
592 size_t prev_refcount = aws_atomic_fetch_sub(&message->refcount, 1);
593 if (prev_refcount == 1) {
594 if (message->request_data) {
595 aws_string_destroy(message->request_data->method);
596 aws_string_destroy(message->request_data->path);
597 }
598
599 aws_http_headers_release(message->headers);
600 aws_input_stream_release(message->body_stream);
601 aws_mem_release(message->allocator, message);
602 } else {
603 AWS_ASSERT(prev_refcount != 0);
604 }
605
606 return NULL;
607 }
608
609 struct aws_http_message *aws_http_message_acquire(struct aws_http_message *message) {
610 if (message != NULL) {
611 aws_atomic_fetch_add(&message->refcount, 1);
612 }
613
614 return message;
615 }
616
617 bool aws_http_message_is_request(const struct aws_http_message *message) {
618 AWS_PRECONDITION(message);
619 return message->request_data;
620 }
621
622 bool aws_http_message_is_response(const struct aws_http_message *message) {
623 AWS_PRECONDITION(message);
624 return message->response_data;
625 }
626
627 enum aws_http_version aws_http_message_get_protocol_version(const struct aws_http_message *message) {
628 AWS_PRECONDITION(message);
629 return message->http_version;
630 }
631
632 int aws_http_message_set_request_method(struct aws_http_message *request_message, struct aws_byte_cursor method) {
633 AWS_PRECONDITION(request_message);
634 AWS_PRECONDITION(aws_byte_cursor_is_valid(&method));
635 AWS_PRECONDITION(request_message->request_data);
636
637 if (request_message->request_data) {
638 switch (request_message->http_version) {
639 case AWS_HTTP_VERSION_1_1:
640 return s_set_string_from_cursor(
641 &request_message->request_data->method, method, request_message->allocator);
642 case AWS_HTTP_VERSION_2:
643 return aws_http2_headers_set_request_method(request_message->headers, method);
644 default:
645 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
646 }
647 }
648 return aws_raise_error(AWS_ERROR_INVALID_STATE);
649 }
650
651 int aws_http_message_get_request_method(
652 const struct aws_http_message *request_message,
653 struct aws_byte_cursor *out_method) {
654
655 AWS_PRECONDITION(request_message);
656 AWS_PRECONDITION(out_method);
657 AWS_PRECONDITION(request_message->request_data);
658 int error = AWS_ERROR_HTTP_DATA_NOT_AVAILABLE;
659 if (request_message->request_data) {
660 switch (request_message->http_version) {
661 case AWS_HTTP_VERSION_1_1:
662 if (request_message->request_data->method) {
663 *out_method = aws_byte_cursor_from_string(request_message->request_data->method);
664 return AWS_OP_SUCCESS;
665 }
666 break;
667 case AWS_HTTP_VERSION_2:
668 return aws_http2_headers_get_request_method(request_message->headers, out_method);
669 default:
670 error = AWS_ERROR_UNIMPLEMENTED;
671 }
672 }
673
674 AWS_ZERO_STRUCT(*out_method);
675 return aws_raise_error(error);
676 }
677
678 int aws_http_message_set_request_path(struct aws_http_message *request_message, struct aws_byte_cursor path) {
679 AWS_PRECONDITION(request_message);
680 AWS_PRECONDITION(aws_byte_cursor_is_valid(&path));
681 AWS_PRECONDITION(request_message->request_data);
682
683 if (request_message->request_data) {
684 switch (request_message->http_version) {
685 case AWS_HTTP_VERSION_1_1:
686 return s_set_string_from_cursor(&request_message->request_data->path, path, request_message->allocator);
687 case AWS_HTTP_VERSION_2:
688 return aws_http2_headers_set_request_path(request_message->headers, path);
689 default:
690 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
691 }
692 }
693
694 return aws_raise_error(AWS_ERROR_INVALID_STATE);
695 }
696
697 int aws_http_message_get_request_path(
698 const struct aws_http_message *request_message,
699 struct aws_byte_cursor *out_path) {
700
701 AWS_PRECONDITION(request_message);
702 AWS_PRECONDITION(out_path);
703 AWS_PRECONDITION(request_message->request_data);
704
705 if (request_message->request_data) {
706 switch (request_message->http_version) {
707 case AWS_HTTP_VERSION_1_1:
708 if (request_message->request_data->path) {
709 *out_path = aws_byte_cursor_from_string(request_message->request_data->path);
710 return AWS_OP_SUCCESS;
711 }
712 break;
713 case AWS_HTTP_VERSION_2:
714 return aws_http2_headers_get_request_path(request_message->headers, out_path);
715 default:
716 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
717 }
718 }
719
720 AWS_ZERO_STRUCT(*out_path);
721 return aws_raise_error(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE);
722 }
723
724 int aws_http_message_get_response_status(const struct aws_http_message *response_message, int *out_status_code) {
725 AWS_PRECONDITION(response_message);
726 AWS_PRECONDITION(out_status_code);
727 AWS_PRECONDITION(response_message->response_data);
728
729 *out_status_code = AWS_HTTP_STATUS_CODE_UNKNOWN;
730
731 if (response_message->response_data) {
732 switch (response_message->http_version) {
733 case AWS_HTTP_VERSION_1_1:
734 if (response_message->response_data->status != AWS_HTTP_STATUS_CODE_UNKNOWN) {
735 *out_status_code = response_message->response_data->status;
736 return AWS_OP_SUCCESS;
737 }
738 break;
739 case AWS_HTTP_VERSION_2:
740 return aws_http2_headers_get_response_status(response_message->headers, out_status_code);
741 default:
742 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
743 }
744 }
745
746 return aws_raise_error(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE);
747 }
748
749 int aws_http_message_set_response_status(struct aws_http_message *response_message, int status_code) {
750 AWS_PRECONDITION(response_message);
751 AWS_PRECONDITION(response_message->response_data);
752
753 if (response_message->response_data) {
754 /* Status code must be printable with exactly 3 digits */
755 if (status_code >= 0 && status_code <= 999) {
756 switch (response_message->http_version) {
757 case AWS_HTTP_VERSION_1_1:
758 response_message->response_data->status = status_code;
759 return AWS_OP_SUCCESS;
760 case AWS_HTTP_VERSION_2:
761 return aws_http2_headers_set_response_status(response_message->headers, status_code);
762 default:
763 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
764 }
765 }
766
767 return aws_raise_error(AWS_ERROR_HTTP_INVALID_STATUS_CODE);
768 }
769
770 return aws_raise_error(AWS_ERROR_INVALID_STATE);
771 }
772
773 void aws_http_message_set_body_stream(struct aws_http_message *message, struct aws_input_stream *body_stream) {
774 AWS_PRECONDITION(message);
775 /* release previous stream, if any */
776 aws_input_stream_release(message->body_stream);
777
778 message->body_stream = body_stream;
779 if (message->body_stream) {
780 aws_input_stream_acquire(message->body_stream);
781 }
782 }
783
784 int aws_http1_stream_write_chunk(struct aws_http_stream *http1_stream, const struct aws_http1_chunk_options *options) {
785 AWS_PRECONDITION(http1_stream);
786 AWS_PRECONDITION(http1_stream->vtable);
787 AWS_PRECONDITION(options);
788 if (!http1_stream->vtable->http1_write_chunk) {
789 AWS_LOGF_TRACE(
790 AWS_LS_HTTP_STREAM,
791 "id=%p: HTTP/1 stream only function invoked on other stream, ignoring call.",
792 (void *)http1_stream);
793 return aws_raise_error(AWS_ERROR_INVALID_STATE);
794 }
795
796 return http1_stream->vtable->http1_write_chunk(http1_stream, options);
797 }
798
799 int aws_http_stream_write_data(
800 struct aws_http_stream *stream,
801 const struct aws_http_stream_write_data_options *options) {
802 AWS_PRECONDITION(stream);
803 AWS_PRECONDITION(stream->vtable);
804 AWS_PRECONDITION(stream->vtable->write_data);
805 AWS_PRECONDITION(options);
806
807 return stream->vtable->write_data(stream, options);
808 }
809
810 int aws_http2_stream_write_data(
811 struct aws_http_stream *http2_stream,
812 const struct aws_http2_stream_write_data_options *options) {
813 AWS_PRECONDITION(http2_stream);
814 AWS_PRECONDITION(http2_stream->vtable);
815 AWS_PRECONDITION(http2_stream->vtable->write_data);
816 AWS_PRECONDITION(options);
817
818 return http2_stream->vtable->write_data(http2_stream, (const struct aws_http_stream_write_data_options *)options);
819 }
820
821 int aws_http1_stream_add_chunked_trailer(
822 struct aws_http_stream *http1_stream,
823 const struct aws_http_headers *trailing_headers) {
824 AWS_PRECONDITION(http1_stream);
825 AWS_PRECONDITION(http1_stream->vtable);
826 AWS_PRECONDITION(trailing_headers);
827 if (!http1_stream->vtable->http1_add_trailer) {
828 AWS_LOGF_TRACE(
829 AWS_LS_HTTP_STREAM,
830 "id=%p: HTTP/1 stream only function invoked on other stream, ignoring call.",
831 (void *)http1_stream);
832 return aws_raise_error(AWS_ERROR_INVALID_STATE);
833 }
834
835 return http1_stream->vtable->http1_add_trailer(http1_stream, trailing_headers);
836 }
837
838 struct aws_input_stream *aws_http_message_get_body_stream(const struct aws_http_message *message) {
839 AWS_PRECONDITION(message);
840 return message->body_stream;
841 }
842
843 struct aws_http_headers *aws_http_message_get_headers(const struct aws_http_message *message) {
844 AWS_PRECONDITION(message);
845 return message->headers;
846 }
847
848 const struct aws_http_headers *aws_http_message_get_const_headers(const struct aws_http_message *message) {
849 AWS_PRECONDITION(message);
850 return message->headers;
851 }
852
853 int aws_http_message_add_header(struct aws_http_message *message, struct aws_http_header header) {
854 return aws_http_headers_add(message->headers, header.name, header.value);
855 }
856
857 int aws_http_message_add_header_array(
858 struct aws_http_message *message,
859 const struct aws_http_header *headers,
860 size_t num_headers) {
861
862 return aws_http_headers_add_array(message->headers, headers, num_headers);
863 }
864
865 int aws_http_message_erase_header(struct aws_http_message *message, size_t index) {
866 return aws_http_headers_erase_index(message->headers, index);
867 }
868
869 size_t aws_http_message_get_header_count(const struct aws_http_message *message) {
870 return aws_http_headers_count(message->headers);
871 }
872
873 int aws_http_message_get_header(
874 const struct aws_http_message *message,
875 struct aws_http_header *out_header,
876 size_t index) {
877
878 return aws_http_headers_get_index(message->headers, index, out_header);
879 }
880
881 AWS_FUTURE_T_POINTER_WITH_RELEASE_IMPLEMENTATION(
882 aws_future_http_message,
883 struct aws_http_message,
884 aws_http_message_release)
885
886 struct aws_http_stream *aws_http_connection_make_request(
887 struct aws_http_connection *client_connection,
888 const struct aws_http_make_request_options *options) {
889
890 AWS_PRECONDITION(client_connection);
891 AWS_PRECONDITION(aws_http_connection_is_client(client_connection));
892 AWS_PRECONDITION(options);
893 if (options->self_size == 0 || !options->request || !aws_http_message_is_request(options->request)) {
894
895 AWS_LOGF_ERROR(
896 AWS_LS_HTTP_CONNECTION,
897 "id=%p: Cannot create client request, options are invalid.",
898 (void *)client_connection);
899 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
900 return NULL;
901 }
902
903 /* Connection owns stream, and must outlive stream */
904 aws_http_connection_acquire(client_connection);
905
906 struct aws_http_stream *stream = client_connection->vtable->make_request(client_connection, options);
907 if (!stream) {
908 aws_http_connection_release(client_connection);
909 return NULL;
910 }
911
912 return stream;
913 }
914
915 struct aws_http_message *aws_http2_message_new_from_http1(
916 struct aws_allocator *alloc,
917 const struct aws_http_message *http1_msg) {
918
919 struct aws_http_headers *old_headers = aws_http_message_get_headers(http1_msg);
920 struct aws_http_header header_iter;
921 struct aws_byte_buf lower_name_buf;
922 AWS_ZERO_STRUCT(lower_name_buf);
923 struct aws_http_message *message = aws_http_message_is_request(http1_msg) ? aws_http2_message_new_request(alloc)
924 : aws_http2_message_new_response(alloc);
925 if (!message) {
926 return NULL;
927 }
928 struct aws_http_headers *copied_headers = message->headers;
929 AWS_LOGF_TRACE(AWS_LS_HTTP_GENERAL, "Creating HTTP/2 message from HTTP/1 message id: %p", (void *)http1_msg);
930
931 /* Set pseudo headers from HTTP/1.1 message */
932 if (aws_http_message_is_request(http1_msg)) {
933 struct aws_byte_cursor method;
934 if (aws_http_message_get_request_method(http1_msg, &method)) {
935 AWS_LOGF_ERROR(
936 AWS_LS_HTTP_GENERAL,
937 "Failed to create HTTP/2 message from HTTP/1 message, ip: %p, due to no method found.",
938 (void *)http1_msg);
939 /* error will happen when the request is invalid */
940 aws_raise_error(AWS_ERROR_HTTP_INVALID_METHOD);
941 goto error;
942 }
943 /* Use add instead of set method to avoid push front to the array list */
944 if (aws_http_headers_add(copied_headers, aws_http_header_method, method)) {
945 goto error;
946 }
947 AWS_LOGF_TRACE(
948 AWS_LS_HTTP_GENERAL,
949 "Added header to new HTTP/2 header - \"%.*s\": \"%.*s\" ",
950 (int)aws_http_header_method.len,
951 aws_http_header_method.ptr,
952 (int)method.len,
953 method.ptr);
954 /**
955 * we set a default value, "https", for now.
956 * TODO: as we support prior knowledge, we may also want to support http?
957 */
958 struct aws_byte_cursor scheme_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("https");
959 if (aws_http_headers_add(copied_headers, aws_http_header_scheme, scheme_cursor)) {
960 goto error;
961 }
962 AWS_LOGF_TRACE(
963 AWS_LS_HTTP_GENERAL,
964 "Added header to new HTTP/2 header - \"%.*s\": \"%.*s\" ",
965 (int)aws_http_header_scheme.len,
966 aws_http_header_scheme.ptr,
967 (int)scheme_cursor.len,
968 scheme_cursor.ptr);
969
970 /**
971 * An intermediary that forwards a request over HTTP/2 MUST construct an ":authority" pseudo-header field
972 * using the authority information from the control data of the original request. (RFC=9113 8.3.1)
973 */
974 struct aws_byte_cursor host_value;
975 AWS_ZERO_STRUCT(host_value);
976 if (aws_http_headers_get(http1_msg->headers, aws_byte_cursor_from_c_str("host"), &host_value) ==
977 AWS_OP_SUCCESS) {
978 if (aws_http_headers_add(copied_headers, aws_http_header_authority, host_value)) {
979 goto error;
980 }
981 AWS_LOGF_TRACE(
982 AWS_LS_HTTP_GENERAL,
983 "Added header to new HTTP/2 header - \"%.*s\": \"%.*s\" ",
984 (int)aws_http_header_authority.len,
985 aws_http_header_authority.ptr,
986 (int)host_value.len,
987 host_value.ptr);
988 }
989 /* TODO: If the host headers is missing, the target URI could be the other source of the authority
990 * information
991 */
992
993 struct aws_byte_cursor path_cursor;
994 if (aws_http_message_get_request_path(http1_msg, &path_cursor)) {
995 AWS_LOGF_ERROR(
996 AWS_LS_HTTP_GENERAL,
997 "Failed to create HTTP/2 message from HTTP/1 message, ip: %p, due to no path found.",
998 (void *)http1_msg);
999 aws_raise_error(AWS_ERROR_HTTP_INVALID_PATH);
1000 goto error;
1001 }
1002 if (aws_http_headers_add(copied_headers, aws_http_header_path, path_cursor)) {
1003 goto error;
1004 }
1005 AWS_LOGF_TRACE(
1006 AWS_LS_HTTP_GENERAL,
1007 "Added header to new HTTP/2 header - \"%.*s\": \"%.*s\" ",
1008 (int)aws_http_header_path.len,
1009 aws_http_header_path.ptr,
1010 (int)path_cursor.len,
1011 path_cursor.ptr);
1012 } else {
1013 int status = 0;
1014 if (aws_http_message_get_response_status(http1_msg, &status)) {
1015 AWS_LOGF_ERROR(
1016 AWS_LS_HTTP_GENERAL,
1017 "Failed to create HTTP/2 response message from HTTP/1 response message, ip: %p, due to no status "
1018 "found.",
1019 (void *)http1_msg);
1020 /* error will happen when the request is invalid */
1021 aws_raise_error(AWS_ERROR_HTTP_INVALID_STATUS_CODE);
1022 goto error;
1023 }
1024 if (aws_http2_headers_set_response_status(copied_headers, status)) {
1025 goto error;
1026 }
1027 AWS_LOGF_TRACE(
1028 AWS_LS_HTTP_GENERAL,
1029 "Added header to new HTTP/2 header - \"%.*s\": \"%d\" ",
1030 (int)aws_http_header_status.len,
1031 aws_http_header_status.ptr,
1032 status);
1033 }
1034
1035 if (aws_byte_buf_init(&lower_name_buf, alloc, 256)) {
1036 goto error;
1037 }
1038 for (size_t iter = 0; iter < aws_http_headers_count(old_headers); iter++) {
1039 aws_byte_buf_reset(&lower_name_buf, false);
1040 bool copy_header = true;
1041 /* name should be converted to lower case */
1042 if (aws_http_headers_get_index(old_headers, iter, &header_iter)) {
1043 goto error;
1044 }
1045 /* append lower case name to the buffer */
1046 aws_byte_buf_append_with_lookup(&lower_name_buf, &header_iter.name, aws_lookup_table_to_lower_get());
1047 struct aws_byte_cursor lower_name_cursor = aws_byte_cursor_from_buf(&lower_name_buf);
1048 enum aws_http_header_name name_enum = aws_http_lowercase_str_to_header_name(lower_name_cursor);
1049 switch (name_enum) {
1050 /**
1051 * An intermediary transforming an HTTP/1.x message to HTTP/2 MUST remove connection-specific header
1052 * fields as discussed in Section 7.6.1 of [HTTP]. (RFC=9113 8.2.2)
1053 */
1054 case AWS_HTTP_HEADER_CONNECTION:
1055 case AWS_HTTP_HEADER_TRANSFER_ENCODING:
1056 case AWS_HTTP_HEADER_UPGRADE:
1057 case AWS_HTTP_HEADER_KEEP_ALIVE:
1058 case AWS_HTTP_HEADER_PROXY_CONNECTION:
1059 /* Host has been converted to :authority pseudo header, skip it as well. */
1060 case AWS_HTTP_HEADER_HOST:
1061 AWS_LOGF_TRACE(
1062 AWS_LS_HTTP_GENERAL,
1063 "Skip connection-specific headers - \"%.*s\" ",
1064 (int)lower_name_cursor.len,
1065 lower_name_cursor.ptr);
1066 copy_header = false;
1067 break;
1068
1069 default:
1070 break;
1071 }
1072 if (copy_header) {
1073 if (aws_http_headers_add(copied_headers, lower_name_cursor, header_iter.value)) {
1074 goto error;
1075 }
1076 AWS_LOGF_TRACE(
1077 AWS_LS_HTTP_GENERAL,
1078 "Added header to new HTTP/2 header - \"%.*s\": \"%.*s\" ",
1079 (int)lower_name_cursor.len,
1080 lower_name_cursor.ptr,
1081 (int)header_iter.value.len,
1082 header_iter.value.ptr);
1083 }
1084 }
1085 aws_byte_buf_clean_up(&lower_name_buf);
1086 aws_http_message_set_body_stream(message, aws_http_message_get_body_stream(http1_msg));
1087
1088 return message;
1089 error:
1090 aws_http_message_release(message);
1091 aws_byte_buf_clean_up(&lower_name_buf);
1092 return NULL;
1093 }
1094
1095 int aws_http_stream_activate(struct aws_http_stream *stream) {
1096 AWS_PRECONDITION(stream);
1097 AWS_PRECONDITION(stream->vtable);
1098 AWS_PRECONDITION(stream->vtable->activate);
1099 /* make sure it's actually a client calling us. This is always a programmer bug, so just assert and die. */
1100 AWS_PRECONDITION(aws_http_connection_is_client(stream->owning_connection));
1101
1102 return stream->vtable->activate(stream);
1103 }
1104
1105 struct aws_http_stream *aws_http_stream_new_server_request_handler(
1106 const struct aws_http_request_handler_options *options) {
1107 AWS_PRECONDITION(options);
1108 if (options->self_size == 0 || !options->server_connection ||
1109 !aws_http_connection_is_server(options->server_connection)) {
1110
1111 AWS_LOGF_ERROR(
1112 AWS_LS_HTTP_CONNECTION,
1113 "id=%p: Cannot create server request handler stream, options are invalid.",
1114 (void *)options->server_connection);
1115 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1116 return NULL;
1117 }
1118
1119 return options->server_connection->vtable->new_server_request_handler_stream(options);
1120 }
1121
1122 int aws_http_stream_send_response(struct aws_http_stream *stream, struct aws_http_message *response) {
1123 AWS_PRECONDITION(stream);
1124 AWS_PRECONDITION(response);
1125 AWS_PRECONDITION(aws_http_message_is_response(response));
1126 return stream->owning_connection->vtable->stream_send_response(stream, response);
1127 }
1128
1129 struct aws_http_stream *aws_http_stream_acquire(struct aws_http_stream *stream) {
1130 AWS_PRECONDITION(stream);
1131
1132 size_t prev_refcount = aws_atomic_fetch_add(&stream->refcount, 1);
1133 AWS_LOGF_TRACE(
1134 AWS_LS_HTTP_STREAM, "id=%p: Stream refcount acquired, %zu remaining.", (void *)stream, prev_refcount + 1);
1135 return stream;
1136 }
1137
1138 void aws_http_stream_release(struct aws_http_stream *stream) {
1139 if (!stream) {
1140 return;
1141 }
1142
1143 size_t prev_refcount = aws_atomic_fetch_sub(&stream->refcount, 1);
1144 if (prev_refcount == 1) {
1145 AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Final stream refcount released.", (void *)stream);
1146
1147 void *user_data = stream->user_data;
1148 aws_http_on_stream_destroy_fn *on_destroy_callback = stream->on_destroy;
1149
1150 struct aws_http_connection *owning_connection = stream->owning_connection;
1151 stream->vtable->destroy(stream);
1152
1153 if (on_destroy_callback) {
1154 /* info user that destroy completed. */
1155 on_destroy_callback(user_data);
1156 }
1157 /* Connection needed to outlive stream, but it's free to go now */
1158 aws_http_connection_release(owning_connection);
1159 } else {
1160 AWS_ASSERT(prev_refcount != 0);
1161 AWS_LOGF_TRACE(
1162 AWS_LS_HTTP_STREAM, "id=%p: Stream refcount released, %zu remaining.", (void *)stream, prev_refcount - 1);
1163 }
1164 }
1165
1166 struct aws_http_connection *aws_http_stream_get_connection(const struct aws_http_stream *stream) {
1167 AWS_ASSERT(stream);
1168 return stream->owning_connection;
1169 }
1170
1171 int aws_http_stream_get_incoming_response_status(const struct aws_http_stream *stream, int *out_status) {
1172 AWS_ASSERT(stream && stream->client_data);
1173
1174 if (stream->client_data->response_status == (int)AWS_HTTP_STATUS_CODE_UNKNOWN) {
1175 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Status code not yet received.", (void *)stream);
1176 return aws_raise_error(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE);
1177 }
1178
1179 *out_status = stream->client_data->response_status;
1180 return AWS_OP_SUCCESS;
1181 }
1182
1183 int aws_http_stream_get_incoming_request_method(
1184 const struct aws_http_stream *stream,
1185 struct aws_byte_cursor *out_method) {
1186 AWS_ASSERT(stream && stream->server_data);
1187
1188 if (!stream->server_data->request_method_str.ptr) {
1189 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Request method not yet received.", (void *)stream);
1190 return aws_raise_error(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE);
1191 }
1192
1193 *out_method = stream->server_data->request_method_str;
1194 return AWS_OP_SUCCESS;
1195 }
1196
1197 int aws_http_stream_get_incoming_request_uri(const struct aws_http_stream *stream, struct aws_byte_cursor *out_uri) {
1198 AWS_ASSERT(stream && stream->server_data);
1199
1200 if (!stream->server_data->request_path.ptr) {
1201 AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Request URI not yet received.", (void *)stream);
1202 return aws_raise_error(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE);
1203 }
1204
1205 *out_uri = stream->server_data->request_path;
1206 return AWS_OP_SUCCESS;
1207 }
1208
1209 void aws_http_stream_update_window(struct aws_http_stream *stream, size_t increment_size) {
1210 stream->vtable->update_window(stream, increment_size);
1211 }
1212
1213 uint32_t aws_http_stream_get_id(const struct aws_http_stream *stream) {
1214 return stream->id;
1215 }
1216
1217 void aws_http_stream_cancel(struct aws_http_stream *stream, int error_code) {
1218 stream->vtable->cancel(stream, error_code);
1219 }
1220
1221 void aws_http_stream_cancel_default_error(struct aws_http_stream *stream) {
1222 stream->vtable->cancel(stream, AWS_ERROR_HTTP_STREAM_CANCELLED);
1223 }
1224
1225 int aws_http2_stream_reset(struct aws_http_stream *http2_stream, uint32_t http2_error) {
1226 AWS_PRECONDITION(http2_stream);
1227 AWS_PRECONDITION(http2_stream->vtable);
1228 if (!http2_stream->vtable->http2_reset_stream) {
1229 AWS_LOGF_TRACE(
1230 AWS_LS_HTTP_STREAM,
1231 "id=%p: HTTP/2 stream only function invoked on other stream, ignoring call.",
1232 (void *)http2_stream);
1233 return aws_raise_error(AWS_ERROR_INVALID_STATE);
1234 }
1235 return http2_stream->vtable->http2_reset_stream(http2_stream, http2_error);
1236 }
1237
1238 int aws_http2_stream_get_received_reset_error_code(struct aws_http_stream *http2_stream, uint32_t *out_http2_error) {
1239 AWS_PRECONDITION(http2_stream);
1240 AWS_PRECONDITION(http2_stream->vtable);
1241 AWS_PRECONDITION(out_http2_error);
1242 if (!http2_stream->vtable->http2_get_received_error_code) {
1243 AWS_LOGF_TRACE(
1244 AWS_LS_HTTP_STREAM,
1245 "id=%p: HTTP/2 stream only function invoked on other stream, ignoring call.",
1246 (void *)http2_stream);
1247 return aws_raise_error(AWS_ERROR_INVALID_STATE);
1248 }
1249 return http2_stream->vtable->http2_get_received_error_code(http2_stream, out_http2_error);
1250 }
1251
1252 int aws_http2_stream_get_sent_reset_error_code(struct aws_http_stream *http2_stream, uint32_t *out_http2_error) {
1253 AWS_PRECONDITION(http2_stream);
1254 AWS_PRECONDITION(http2_stream->vtable);
1255 AWS_PRECONDITION(out_http2_error);
1256 if (!http2_stream->vtable->http2_get_sent_error_code) {
1257 AWS_LOGF_TRACE(
1258 AWS_LS_HTTP_STREAM,
1259 "id=%p: HTTP/2 stream only function invoked on other stream, ignoring call.",
1260 (void *)http2_stream);
1261 return aws_raise_error(AWS_ERROR_INVALID_STATE);
1262 }
1263 return http2_stream->vtable->http2_get_sent_error_code(http2_stream, out_http2_error);
1264 }