Debian Codegraph @

crt/aws-c-http/source/http.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/hash_table.h>
7 #include <aws/compression/compression.h>
8 #include <aws/http/private/hpack.h>
9 #include <aws/http/private/http_impl.h>
10 #include <aws/http/status_code.h>
11 #include <aws/io/logging.h>
12
13 #include <ctype.h>
14
15 #define AWS_DEFINE_ERROR_INFO_HTTP(CODE, STR) [(CODE) - 0x0800] = AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-c-http")
16
17 /* clang-format off */
18 static struct aws_error_info s_errors[] = {
19 AWS_DEFINE_ERROR_INFO_HTTP(
20 AWS_ERROR_HTTP_UNKNOWN,
21 "Encountered an unknown error."),
22 AWS_DEFINE_ERROR_INFO_HTTP(
23 AWS_ERROR_HTTP_HEADER_NOT_FOUND,
24 "The specified header was not found"),
25 AWS_DEFINE_ERROR_INFO_HTTP(
26 AWS_ERROR_HTTP_INVALID_HEADER_FIELD,
27 "Invalid header field, including a forbidden header field."),
28 AWS_DEFINE_ERROR_INFO_HTTP(
29 AWS_ERROR_HTTP_INVALID_HEADER_NAME,
30 "Invalid header name."),
31 AWS_DEFINE_ERROR_INFO_HTTP(
32 AWS_ERROR_HTTP_INVALID_HEADER_VALUE,
33 "Invalid header value."),
34 AWS_DEFINE_ERROR_INFO_HTTP(
35 AWS_ERROR_HTTP_INVALID_METHOD,
36 "Method is invalid."),
37 AWS_DEFINE_ERROR_INFO_HTTP(
38 AWS_ERROR_HTTP_INVALID_PATH,
39 "Path is invalid."),
40 AWS_DEFINE_ERROR_INFO_HTTP(
41 AWS_ERROR_HTTP_INVALID_STATUS_CODE,
42 "Status code is invalid."),
43 AWS_DEFINE_ERROR_INFO_HTTP(
44 AWS_ERROR_HTTP_MISSING_BODY_STREAM,
45 "Given the provided headers (ex: Content-Length), a body is expected."),
46 AWS_DEFINE_ERROR_INFO_HTTP(
47 AWS_ERROR_HTTP_INVALID_BODY_STREAM,
48 "A body stream provided, but the message does not allow body (ex: response for HEAD Request and 304 response)"),
49 AWS_DEFINE_ERROR_INFO_HTTP(
50 AWS_ERROR_HTTP_CONNECTION_CLOSED,
51 "The connection has closed or is closing."),
52 AWS_DEFINE_ERROR_INFO_HTTP(
53 AWS_ERROR_HTTP_SWITCHED_PROTOCOLS,
54 "The connection has switched protocols."),
55 AWS_DEFINE_ERROR_INFO_HTTP(
56 AWS_ERROR_HTTP_UNSUPPORTED_PROTOCOL,
57 "An unsupported protocol was encountered."),
58 AWS_DEFINE_ERROR_INFO_HTTP(
59 AWS_ERROR_HTTP_REACTION_REQUIRED,
60 "A necessary function was not invoked from a user callback."),
61 AWS_DEFINE_ERROR_INFO_HTTP(
62 AWS_ERROR_HTTP_DATA_NOT_AVAILABLE,
63 "This data is not yet available."),
64 AWS_DEFINE_ERROR_INFO_HTTP(
65 AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT,
66 "Amount of data streamed out does not match the previously declared length."),
67 AWS_DEFINE_ERROR_INFO_HTTP(
68 AWS_ERROR_HTTP_CALLBACK_FAILURE,
69 "A callback has reported failure."),
70 AWS_DEFINE_ERROR_INFO_HTTP(
71 AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE,
72 "Failed to upgrade HTTP connection to Websocket."),
73 AWS_DEFINE_ERROR_INFO_HTTP(
74 AWS_ERROR_HTTP_WEBSOCKET_CLOSE_FRAME_SENT,
75 "Websocket has sent CLOSE frame, no more data will be sent."),
76 AWS_DEFINE_ERROR_INFO_HTTP(
77 AWS_ERROR_HTTP_WEBSOCKET_IS_MIDCHANNEL_HANDLER,
78 "Operation cannot be performed because websocket has been converted to a midchannel handler."),
79 AWS_DEFINE_ERROR_INFO_HTTP(
80 AWS_ERROR_HTTP_CONNECTION_MANAGER_INVALID_STATE_FOR_ACQUIRE,
81 "Acquire called after the connection manager's ref count has reached zero"),
82 AWS_DEFINE_ERROR_INFO_HTTP(
83 AWS_ERROR_HTTP_CONNECTION_MANAGER_VENDED_CONNECTION_UNDERFLOW,
84 "Release called when the connection manager's vended connection count was zero"),
85 AWS_DEFINE_ERROR_INFO_HTTP(
86 AWS_ERROR_HTTP_SERVER_CLOSED,
87 "The http server is closed, no more connections will be accepted"),
88 AWS_DEFINE_ERROR_INFO_HTTP(
89 AWS_ERROR_HTTP_PROXY_CONNECT_FAILED,
90 "Proxy-based connection establishment failed because the CONNECT call failed"),
91 AWS_DEFINE_ERROR_INFO_HTTP(
92 AWS_ERROR_HTTP_CONNECTION_MANAGER_SHUTTING_DOWN,
93 "Connection acquisition failed because connection manager is shutting down"),
94 AWS_DEFINE_ERROR_INFO_HTTP(
95 AWS_ERROR_HTTP_CHANNEL_THROUGHPUT_FAILURE,
96 "Http connection channel shut down due to failure to meet throughput minimum"),
97 AWS_DEFINE_ERROR_INFO_HTTP(
98 AWS_ERROR_HTTP_PROTOCOL_ERROR,
99 "Protocol rules violated by peer"),
100 AWS_DEFINE_ERROR_INFO_HTTP(
101 AWS_ERROR_HTTP_STREAM_IDS_EXHAUSTED,
102 "Connection exhausted all possible HTTP-stream IDs. Establish a new connection for new streams."),
103 AWS_DEFINE_ERROR_INFO_HTTP(
104 AWS_ERROR_HTTP_GOAWAY_RECEIVED,
105 "Peer sent GOAWAY to initiate connection shutdown. Establish a new connection to retry the HTTP-streams."),
106 AWS_DEFINE_ERROR_INFO_HTTP(
107 AWS_ERROR_HTTP_RST_STREAM_RECEIVED,
108 "Peer sent RST_STREAM to terminate HTTP-stream."),
109 AWS_DEFINE_ERROR_INFO_HTTP(
110 AWS_ERROR_HTTP_RST_STREAM_SENT,
111 "RST_STREAM has sent from local implementation and HTTP-stream has been terminated."),
112 AWS_DEFINE_ERROR_INFO_HTTP(
113 AWS_ERROR_HTTP_STREAM_NOT_ACTIVATED,
114 "HTTP-stream must be activated before use."),
115 AWS_DEFINE_ERROR_INFO_HTTP(
116 AWS_ERROR_HTTP_STREAM_HAS_COMPLETED,
117 "HTTP-stream has completed, action cannot be performed."),
118 AWS_DEFINE_ERROR_INFO_HTTP(
119 AWS_ERROR_HTTP_PROXY_STRATEGY_NTLM_CHALLENGE_TOKEN_MISSING,
120 "NTLM Proxy strategy was initiated without a challenge token"),
121 AWS_DEFINE_ERROR_INFO_HTTP(
122 AWS_ERROR_HTTP_PROXY_STRATEGY_TOKEN_RETRIEVAL_FAILURE,
123 "Failure in user code while retrieving proxy auth token"),
124 AWS_DEFINE_ERROR_INFO_HTTP(
125 AWS_ERROR_HTTP_PROXY_CONNECT_FAILED_RETRYABLE,
126 "Proxy connection attempt failed but the negotiation could be continued on a new connection"),
127 AWS_DEFINE_ERROR_INFO_HTTP(
128 AWS_ERROR_HTTP_PROTOCOL_SWITCH_FAILURE,
129 "Internal state failure prevent connection from switching protocols"),
130 AWS_DEFINE_ERROR_INFO_HTTP(
131 AWS_ERROR_HTTP_MAX_CONCURRENT_STREAMS_EXCEEDED,
132 "Max concurrent stream reached"),
133 AWS_DEFINE_ERROR_INFO_HTTP(
134 AWS_ERROR_HTTP_STREAM_MANAGER_SHUTTING_DOWN,
135 "Stream acquisition failed because stream manager is shutting down"),
136 AWS_DEFINE_ERROR_INFO_HTTP(
137 AWS_ERROR_HTTP_STREAM_MANAGER_CONNECTION_ACQUIRE_FAILURE,
138 "Stream acquisition failed because stream manager failed to acquire a connection"),
139 AWS_DEFINE_ERROR_INFO_HTTP(
140 AWS_ERROR_HTTP_STREAM_MANAGER_UNEXPECTED_HTTP_VERSION,
141 "Stream acquisition failed because stream manager got an unexpected version of HTTP connection"),
142 AWS_DEFINE_ERROR_INFO_HTTP(
143 AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR,
144 "Websocket protocol rules violated by peer"),
145 AWS_DEFINE_ERROR_INFO_HTTP(
146 AWS_ERROR_HTTP_MANUAL_WRITE_NOT_ENABLED,
147 "Manual write failed because manual writes are not enabled."),
148 AWS_DEFINE_ERROR_INFO_HTTP(
149 AWS_ERROR_HTTP_MANUAL_WRITE_HAS_COMPLETED,
150 "Manual write failed because manual writes are already completed."),
151 AWS_DEFINE_ERROR_INFO_HTTP(
152 AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT,
153 "Timed out waiting for first byte of HTTP response, after sending the full request."),
154 AWS_DEFINE_ERROR_INFO_HTTP(
155 AWS_ERROR_HTTP_CONNECTION_MANAGER_ACQUISITION_TIMEOUT,
156 "Connection Manager failed to acquire a connection within the defined timeout."),
157 AWS_DEFINE_ERROR_INFO_HTTP(
158 AWS_ERROR_HTTP_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED,
159 "Max pending acquisitions reached"),
160 AWS_DEFINE_ERROR_INFO_HTTP(
161 AWS_ERROR_HTTP_STREAM_CANCELLED,
162 "In-flight HTTP-stream has been cancelled by user."),
163 };
164 /* clang-format on */
165
166 static struct aws_error_info_list s_error_list = {
167 .error_list = s_errors,
168 .count = AWS_ARRAY_SIZE(s_errors),
169 };
170
171 static struct aws_log_subject_info s_log_subject_infos[] = {
172 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_GENERAL, "http", "Misc HTTP logging"),
173 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_CONNECTION, "http-connection", "HTTP client or server connection"),
174 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_ENCODER, "http-encoder", "HTTP data encoder"),
175 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_DECODER, "http-decoder", "HTTP data decoder"),
176 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_SERVER, "http-server", "HTTP server socket listening for incoming connections"),
177 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_STREAM, "http-stream", "HTTP request-response exchange"),
178 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_CONNECTION_MANAGER, "connection-manager", "HTTP connection manager"),
179 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_STREAM_MANAGER, "http2-stream-manager", "HTTP/2 stream manager"),
180 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_WEBSOCKET, "websocket", "Websocket"),
181 DEFINE_LOG_SUBJECT_INFO(AWS_LS_HTTP_WEBSOCKET_SETUP, "websocket-setup", "Websocket setup"),
182 DEFINE_LOG_SUBJECT_INFO(
183 AWS_LS_HTTP_PROXY_NEGOTIATION,
184 "proxy-negotiation",
185 "Negotiating an http connection with a proxy server"),
186 };
187
188 static struct aws_log_subject_info_list s_log_subject_list = {
189 .subject_list = s_log_subject_infos,
190 .count = AWS_ARRAY_SIZE(s_log_subject_infos),
191 };
192
193 struct aws_enum_value {
194 struct aws_allocator *allocator;
195 int value;
196 };
197
198 static void s_destroy_enum_value(void *value) {
199 struct aws_enum_value *enum_value = value;
200 aws_mem_release(enum_value->allocator, enum_value);
201 }
202
203 /**
204 * Given array of aws_byte_cursors, init hashtable where...
205 * Key is aws_byte_cursor* (pointing into cursor from array) and comparisons are case-insensitive.
206 * Value is the array index cast to a void*.
207 */
208 static void s_init_str_to_enum_hash_table(
209 struct aws_hash_table *table,
210 struct aws_allocator *alloc,
211 struct aws_byte_cursor *str_array,
212 int start_index,
213 int end_index,
214 bool ignore_case) {
215
216 int err = aws_hash_table_init(
217 table,
218 alloc,
219 end_index - start_index,
220 ignore_case ? aws_hash_byte_cursor_ptr_ignore_case : aws_hash_byte_cursor_ptr,
221 (aws_hash_callback_eq_fn *)(ignore_case ? aws_byte_cursor_eq_ignore_case : aws_byte_cursor_eq),
222 NULL,
223 s_destroy_enum_value);
224 AWS_FATAL_ASSERT(!err);
225
226 for (int i = start_index; i < end_index; ++i) {
227 int was_created = 0;
228 struct aws_enum_value *enum_value = aws_mem_calloc(alloc, 1, sizeof(struct aws_enum_value));
229 AWS_FATAL_ASSERT(enum_value);
230 enum_value->allocator = alloc;
231 enum_value->value = i;
232
233 AWS_FATAL_ASSERT(str_array[i].ptr && "Missing enum string");
234 err = aws_hash_table_put(table, &str_array[i], (void *)enum_value, &was_created);
235 AWS_FATAL_ASSERT(!err && was_created);
236 }
237 }
238
239 /**
240 * Given key, get value from table initialized by s_init_str_to_enum_hash_table().
241 * Returns -1 if key not found.
242 */
243 static int s_find_in_str_to_enum_hash_table(const struct aws_hash_table *table, struct aws_byte_cursor *key) {
244 struct aws_hash_element *elem;
245 aws_hash_table_find(table, key, &elem);
246 if (elem) {
247 struct aws_enum_value *enum_value = elem->value;
248 return enum_value->value;
249 }
250 return -1;
251 }
252
253 /* METHODS */
254 static struct aws_hash_table s_method_str_to_enum; /* for string -> enum lookup */
255 static struct aws_byte_cursor s_method_enum_to_str[AWS_HTTP_METHOD_COUNT]; /* for enum -> string lookup */
256
257 static void s_methods_init(struct aws_allocator *alloc) {
258 s_method_enum_to_str[AWS_HTTP_METHOD_GET] = aws_http_method_get;
259 s_method_enum_to_str[AWS_HTTP_METHOD_HEAD] = aws_http_method_head;
260 s_method_enum_to_str[AWS_HTTP_METHOD_CONNECT] = aws_http_method_connect;
261
262 s_init_str_to_enum_hash_table(
263 &s_method_str_to_enum,
264 alloc,
265 s_method_enum_to_str,
266 AWS_HTTP_METHOD_UNKNOWN + 1,
267 AWS_HTTP_METHOD_COUNT,
268 false /* DO NOT ignore case of method */);
269 }
270
271 static void s_methods_clean_up(void) {
272 aws_hash_table_clean_up(&s_method_str_to_enum);
273 }
274
275 enum aws_http_method aws_http_str_to_method(struct aws_byte_cursor cursor) {
276 int method = s_find_in_str_to_enum_hash_table(&s_method_str_to_enum, &cursor);
277 if (method >= 0) {
278 return (enum aws_http_method)method;
279 }
280 return AWS_HTTP_METHOD_UNKNOWN;
281 }
282
283 /* VERSIONS */
284 static struct aws_byte_cursor s_version_enum_to_str[AWS_HTTP_HEADER_COUNT]; /* for enum -> string lookup */
285
286 static void s_versions_init(struct aws_allocator *alloc) {
287 (void)alloc;
288 s_version_enum_to_str[AWS_HTTP_VERSION_UNKNOWN] = aws_byte_cursor_from_c_str("Unknown");
289 s_version_enum_to_str[AWS_HTTP_VERSION_1_0] = aws_byte_cursor_from_c_str("HTTP/1.0");
290 s_version_enum_to_str[AWS_HTTP_VERSION_1_1] = aws_byte_cursor_from_c_str("HTTP/1.1");
291 s_version_enum_to_str[AWS_HTTP_VERSION_2] = aws_byte_cursor_from_c_str("HTTP/2");
292 }
293
294 static void s_versions_clean_up(void) {}
295
296 struct aws_byte_cursor aws_http_version_to_str(enum aws_http_version version) {
297 if ((int)version < AWS_HTTP_VERSION_UNKNOWN || (int)version >= AWS_HTTP_VERSION_COUNT) {
298 version = AWS_HTTP_VERSION_UNKNOWN;
299 }
300
301 return s_version_enum_to_str[version];
302 }
303
304 /* HEADERS */
305 static struct aws_hash_table s_header_str_to_enum; /* for case-insensitive string -> enum lookup */
306 static struct aws_hash_table s_lowercase_header_str_to_enum; /* for case-sensitive string -> enum lookup */
307 static struct aws_byte_cursor s_header_enum_to_str[AWS_HTTP_HEADER_COUNT]; /* for enum -> string lookup */
308
309 static void s_headers_init(struct aws_allocator *alloc) {
310 s_header_enum_to_str[AWS_HTTP_HEADER_METHOD] = aws_byte_cursor_from_c_str(":method");
311 s_header_enum_to_str[AWS_HTTP_HEADER_SCHEME] = aws_byte_cursor_from_c_str(":scheme");
312 s_header_enum_to_str[AWS_HTTP_HEADER_AUTHORITY] = aws_byte_cursor_from_c_str(":authority");
313 s_header_enum_to_str[AWS_HTTP_HEADER_PATH] = aws_byte_cursor_from_c_str(":path");
314 s_header_enum_to_str[AWS_HTTP_HEADER_STATUS] = aws_byte_cursor_from_c_str(":status");
315 s_header_enum_to_str[AWS_HTTP_HEADER_COOKIE] = aws_byte_cursor_from_c_str("cookie");
316 s_header_enum_to_str[AWS_HTTP_HEADER_SET_COOKIE] = aws_byte_cursor_from_c_str("set-cookie");
317 s_header_enum_to_str[AWS_HTTP_HEADER_HOST] = aws_byte_cursor_from_c_str("host");
318 s_header_enum_to_str[AWS_HTTP_HEADER_CONNECTION] = aws_byte_cursor_from_c_str("connection");
319 s_header_enum_to_str[AWS_HTTP_HEADER_CONTENT_LENGTH] = aws_byte_cursor_from_c_str("content-length");
320 s_header_enum_to_str[AWS_HTTP_HEADER_EXPECT] = aws_byte_cursor_from_c_str("expect");
321 s_header_enum_to_str[AWS_HTTP_HEADER_TRANSFER_ENCODING] = aws_byte_cursor_from_c_str("transfer-encoding");
322 s_header_enum_to_str[AWS_HTTP_HEADER_CACHE_CONTROL] = aws_byte_cursor_from_c_str("cache-control");
323 s_header_enum_to_str[AWS_HTTP_HEADER_MAX_FORWARDS] = aws_byte_cursor_from_c_str("max-forwards");
324 s_header_enum_to_str[AWS_HTTP_HEADER_PRAGMA] = aws_byte_cursor_from_c_str("pragma");
325 s_header_enum_to_str[AWS_HTTP_HEADER_RANGE] = aws_byte_cursor_from_c_str("range");
326 s_header_enum_to_str[AWS_HTTP_HEADER_TE] = aws_byte_cursor_from_c_str("te");
327 s_header_enum_to_str[AWS_HTTP_HEADER_CONTENT_ENCODING] = aws_byte_cursor_from_c_str("content-encoding");
328 s_header_enum_to_str[AWS_HTTP_HEADER_CONTENT_TYPE] = aws_byte_cursor_from_c_str("content-type");
329 s_header_enum_to_str[AWS_HTTP_HEADER_CONTENT_RANGE] = aws_byte_cursor_from_c_str("content-range");
330 s_header_enum_to_str[AWS_HTTP_HEADER_TRAILER] = aws_byte_cursor_from_c_str("trailer");
331 s_header_enum_to_str[AWS_HTTP_HEADER_WWW_AUTHENTICATE] = aws_byte_cursor_from_c_str("www-authenticate");
332 s_header_enum_to_str[AWS_HTTP_HEADER_AUTHORIZATION] = aws_byte_cursor_from_c_str("authorization");
333 s_header_enum_to_str[AWS_HTTP_HEADER_PROXY_AUTHENTICATE] = aws_byte_cursor_from_c_str("proxy-authenticate");
334 s_header_enum_to_str[AWS_HTTP_HEADER_PROXY_AUTHORIZATION] = aws_byte_cursor_from_c_str("proxy-authorization");
335 s_header_enum_to_str[AWS_HTTP_HEADER_AGE] = aws_byte_cursor_from_c_str("age");
336 s_header_enum_to_str[AWS_HTTP_HEADER_EXPIRES] = aws_byte_cursor_from_c_str("expires");
337 s_header_enum_to_str[AWS_HTTP_HEADER_DATE] = aws_byte_cursor_from_c_str("date");
338 s_header_enum_to_str[AWS_HTTP_HEADER_LOCATION] = aws_byte_cursor_from_c_str("location");
339 s_header_enum_to_str[AWS_HTTP_HEADER_RETRY_AFTER] = aws_byte_cursor_from_c_str("retry-after");
340 s_header_enum_to_str[AWS_HTTP_HEADER_VARY] = aws_byte_cursor_from_c_str("vary");
341 s_header_enum_to_str[AWS_HTTP_HEADER_WARNING] = aws_byte_cursor_from_c_str("warning");
342 s_header_enum_to_str[AWS_HTTP_HEADER_UPGRADE] = aws_byte_cursor_from_c_str("upgrade");
343 s_header_enum_to_str[AWS_HTTP_HEADER_KEEP_ALIVE] = aws_byte_cursor_from_c_str("keep-alive");
344 s_header_enum_to_str[AWS_HTTP_HEADER_PROXY_CONNECTION] = aws_byte_cursor_from_c_str("proxy-connection");
345 s_header_enum_to_str[AWS_HTTP_HEADER_SIGNING_SECURITY_TOKEN] = aws_byte_cursor_from_c_str("x-amz-security-token");
346 s_header_enum_to_str[AWS_HTTP_HEADER_SIGNING_S3SESSION_TOKEN] = aws_byte_cursor_from_c_str("x-amz-s3session-token");
347
348 s_init_str_to_enum_hash_table(
349 &s_header_str_to_enum,
350 alloc,
351 s_header_enum_to_str,
352 AWS_HTTP_HEADER_UNKNOWN + 1,
353 AWS_HTTP_HEADER_COUNT,
354 true /* ignore case */);
355
356 s_init_str_to_enum_hash_table(
357 &s_lowercase_header_str_to_enum,
358 alloc,
359 s_header_enum_to_str,
360 AWS_HTTP_HEADER_UNKNOWN + 1,
361 AWS_HTTP_HEADER_COUNT,
362 false /* ignore case */);
363 }
364
365 static void s_headers_clean_up(void) {
366 aws_hash_table_clean_up(&s_header_str_to_enum);
367 aws_hash_table_clean_up(&s_lowercase_header_str_to_enum);
368 }
369
370 enum aws_http_header_name aws_http_str_to_header_name(struct aws_byte_cursor cursor) {
371 int header = s_find_in_str_to_enum_hash_table(&s_header_str_to_enum, &cursor);
372 if (header >= 0) {
373 return (enum aws_http_header_name)header;
374 }
375 return AWS_HTTP_HEADER_UNKNOWN;
376 }
377
378 enum aws_http_header_name aws_http_lowercase_str_to_header_name(struct aws_byte_cursor cursor) {
379 int header = s_find_in_str_to_enum_hash_table(&s_lowercase_header_str_to_enum, &cursor);
380 if (header >= 0) {
381 return (enum aws_http_header_name)header;
382 }
383 return AWS_HTTP_HEADER_UNKNOWN;
384 }
385
386 /* STATUS */
387 const char *aws_http_status_text(int status_code) {
388 /**
389 * Data from Internet Assigned Numbers Authority (IANA):
390 * https://www.iana.org/assignments/http-status-codes/http-status-codes.txt
391 */
392 switch (status_code) {
393 case AWS_HTTP_STATUS_CODE_100_CONTINUE:
394 return "Continue";
395 case AWS_HTTP_STATUS_CODE_101_SWITCHING_PROTOCOLS:
396 return "Switching Protocols";
397 case AWS_HTTP_STATUS_CODE_102_PROCESSING:
398 return "Processing";
399 case AWS_HTTP_STATUS_CODE_103_EARLY_HINTS:
400 return "Early Hints";
401 case AWS_HTTP_STATUS_CODE_200_OK:
402 return "OK";
403 case AWS_HTTP_STATUS_CODE_201_CREATED:
404 return "Created";
405 case AWS_HTTP_STATUS_CODE_202_ACCEPTED:
406 return "Accepted";
407 case AWS_HTTP_STATUS_CODE_203_NON_AUTHORITATIVE_INFORMATION:
408 return "Non-Authoritative Information";
409 case AWS_HTTP_STATUS_CODE_204_NO_CONTENT:
410 return "No Content";
411 case AWS_HTTP_STATUS_CODE_205_RESET_CONTENT:
412 return "Reset Content";
413 case AWS_HTTP_STATUS_CODE_206_PARTIAL_CONTENT:
414 return "Partial Content";
415 case AWS_HTTP_STATUS_CODE_207_MULTI_STATUS:
416 return "Multi-Status";
417 case AWS_HTTP_STATUS_CODE_208_ALREADY_REPORTED:
418 return "Already Reported";
419 case AWS_HTTP_STATUS_CODE_226_IM_USED:
420 return "IM Used";
421 case AWS_HTTP_STATUS_CODE_300_MULTIPLE_CHOICES:
422 return "Multiple Choices";
423 case AWS_HTTP_STATUS_CODE_301_MOVED_PERMANENTLY:
424 return "Moved Permanently";
425 case AWS_HTTP_STATUS_CODE_302_FOUND:
426 return "Found";
427 case AWS_HTTP_STATUS_CODE_303_SEE_OTHER:
428 return "See Other";
429 case AWS_HTTP_STATUS_CODE_304_NOT_MODIFIED:
430 return "Not Modified";
431 case AWS_HTTP_STATUS_CODE_305_USE_PROXY:
432 return "Use Proxy";
433 case AWS_HTTP_STATUS_CODE_307_TEMPORARY_REDIRECT:
434 return "Temporary Redirect";
435 case AWS_HTTP_STATUS_CODE_308_PERMANENT_REDIRECT:
436 return "Permanent Redirect";
437 case AWS_HTTP_STATUS_CODE_400_BAD_REQUEST:
438 return "Bad Request";
439 case AWS_HTTP_STATUS_CODE_401_UNAUTHORIZED:
440 return "Unauthorized";
441 case AWS_HTTP_STATUS_CODE_402_PAYMENT_REQUIRED:
442 return "Payment Required";
443 case AWS_HTTP_STATUS_CODE_403_FORBIDDEN:
444 return "Forbidden";
445 case AWS_HTTP_STATUS_CODE_404_NOT_FOUND:
446 return "Not Found";
447 case AWS_HTTP_STATUS_CODE_405_METHOD_NOT_ALLOWED:
448 return "Method Not Allowed";
449 case AWS_HTTP_STATUS_CODE_406_NOT_ACCEPTABLE:
450 return "Not Acceptable";
451 case AWS_HTTP_STATUS_CODE_407_PROXY_AUTHENTICATION_REQUIRED:
452 return "Proxy Authentication Required";
453 case AWS_HTTP_STATUS_CODE_408_REQUEST_TIMEOUT:
454 return "Request Timeout";
455 case AWS_HTTP_STATUS_CODE_409_CONFLICT:
456 return "Conflict";
457 case AWS_HTTP_STATUS_CODE_410_GONE:
458 return "Gone";
459 case AWS_HTTP_STATUS_CODE_411_LENGTH_REQUIRED:
460 return "Length Required";
461 case AWS_HTTP_STATUS_CODE_412_PRECONDITION_FAILED:
462 return "Precondition Failed";
463 case AWS_HTTP_STATUS_CODE_413_REQUEST_ENTITY_TOO_LARGE:
464 return "Payload Too Large";
465 case AWS_HTTP_STATUS_CODE_414_REQUEST_URI_TOO_LONG:
466 return "URI Too Long";
467 case AWS_HTTP_STATUS_CODE_415_UNSUPPORTED_MEDIA_TYPE:
468 return "Unsupported Media Type";
469 case AWS_HTTP_STATUS_CODE_416_REQUESTED_RANGE_NOT_SATISFIABLE:
470 return "Range Not Satisfiable";
471 case AWS_HTTP_STATUS_CODE_417_EXPECTATION_FAILED:
472 return "Expectation Failed";
473 case AWS_HTTP_STATUS_CODE_421_MISDIRECTED_REQUEST:
474 return "Misdirected Request";
475 case AWS_HTTP_STATUS_CODE_422_UNPROCESSABLE_ENTITY:
476 return "Unprocessable Entity";
477 case AWS_HTTP_STATUS_CODE_423_LOCKED:
478 return "Locked";
479 case AWS_HTTP_STATUS_CODE_424_FAILED_DEPENDENCY:
480 return "Failed Dependency";
481 case AWS_HTTP_STATUS_CODE_425_TOO_EARLY:
482 return "Too Early";
483 case AWS_HTTP_STATUS_CODE_426_UPGRADE_REQUIRED:
484 return "Upgrade Required";
485 case AWS_HTTP_STATUS_CODE_428_PRECONDITION_REQUIRED:
486 return "Precondition Required";
487 case AWS_HTTP_STATUS_CODE_429_TOO_MANY_REQUESTS:
488 return "Too Many Requests";
489 case AWS_HTTP_STATUS_CODE_431_REQUEST_HEADER_FIELDS_TOO_LARGE:
490 return "Request Header Fields Too Large";
491 case AWS_HTTP_STATUS_CODE_451_UNAVAILABLE_FOR_LEGAL_REASON:
492 return "Unavailable For Legal Reasons";
493 case AWS_HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR:
494 return "Internal Server Error";
495 case AWS_HTTP_STATUS_CODE_501_NOT_IMPLEMENTED:
496 return "Not Implemented";
497 case AWS_HTTP_STATUS_CODE_502_BAD_GATEWAY:
498 return "Bad Gateway";
499 case AWS_HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE:
500 return "Service Unavailable";
501 case AWS_HTTP_STATUS_CODE_504_GATEWAY_TIMEOUT:
502 return "Gateway Timeout";
503 case AWS_HTTP_STATUS_CODE_505_HTTP_VERSION_NOT_SUPPORTED:
504 return "HTTP Version Not Supported";
505 case AWS_HTTP_STATUS_CODE_506_VARIANT_ALSO_NEGOTIATES:
506 return "Variant Also Negotiates";
507 case AWS_HTTP_STATUS_CODE_507_INSUFFICIENT_STORAGE:
508 return "Insufficient Storage";
509 case AWS_HTTP_STATUS_CODE_508_LOOP_DETECTED:
510 return "Loop Detected";
511 case AWS_HTTP_STATUS_CODE_510_NOT_EXTENDED:
512 return "Not Extended";
513 case AWS_HTTP_STATUS_CODE_511_NETWORK_AUTHENTICATION_REQUIRED:
514 return "Network Authentication Required";
515 default:
516 return "";
517 }
518 }
519
520 static bool s_library_initialized = false;
521 void aws_http_library_init(struct aws_allocator *alloc) {
522 if (s_library_initialized) {
523 return;
524 }
525 s_library_initialized = true;
526
527 aws_io_library_init(alloc);
528 aws_compression_library_init(alloc);
529 aws_register_error_info(&s_error_list);
530 aws_register_log_subject_info_list(&s_log_subject_list);
531 s_methods_init(alloc);
532 s_headers_init(alloc);
533 s_versions_init(alloc);
534 aws_hpack_static_table_init(alloc);
535 }
536
537 void aws_http_library_clean_up(void) {
538 if (!s_library_initialized) {
539 return;
540 }
541 s_library_initialized = false;
542
543 aws_thread_join_all_managed();
544 aws_unregister_error_info(&s_error_list);
545 aws_unregister_log_subject_info_list(&s_log_subject_list);
546 s_methods_clean_up();
547 s_headers_clean_up();
548 s_versions_clean_up();
549 aws_hpack_static_table_clean_up();
550 aws_compression_library_clean_up();
551 aws_io_library_clean_up();
552 }
553
554 void aws_http_fatal_assert_library_initialized(void) {
555 if (!s_library_initialized) {
556 AWS_LOGF_FATAL(
557 AWS_LS_HTTP_GENERAL,
558 "aws_http_library_init() must be called before using any functionality in aws-c-http.");
559
560 AWS_FATAL_ASSERT(s_library_initialized);
561 }
562 }
563
564 /*
565 * This might need to get updated with more http error codes based on consensus.
566 */
567 bool aws_http_error_code_is_retryable(int error_code) {
568 switch (error_code) {
569 case AWS_ERROR_HTTP_CONNECTION_CLOSED:
570 case AWS_ERROR_HTTP_SERVER_CLOSED:
571 case AWS_ERROR_HTTP_PROXY_CONNECT_FAILED_RETRYABLE:
572 return true;
573 }
574 return aws_io_error_code_is_retryable(error_code);
575 }
576
577 const struct aws_byte_cursor aws_http_method_get = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET");
578 const struct aws_byte_cursor aws_http_method_head = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HEAD");
579 const struct aws_byte_cursor aws_http_method_post = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST");
580 const struct aws_byte_cursor aws_http_method_put = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PUT");
581 const struct aws_byte_cursor aws_http_method_delete = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("DELETE");
582 const struct aws_byte_cursor aws_http_method_connect = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CONNECT");
583 const struct aws_byte_cursor aws_http_method_options = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("OPTIONS");
584
585 const struct aws_byte_cursor aws_http_header_method = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(":method");
586 const struct aws_byte_cursor aws_http_header_scheme = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(":scheme");
587 const struct aws_byte_cursor aws_http_header_authority = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(":authority");
588 const struct aws_byte_cursor aws_http_header_path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(":path");
589 const struct aws_byte_cursor aws_http_header_status = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(":status");
590
591 const struct aws_byte_cursor aws_http_scheme_http = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("http");
592 const struct aws_byte_cursor aws_http_scheme_https = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("https");