Debian Codegraph @

crt/aws-c-http/source/connection.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/http/private/connection_impl.h>
7 #include <aws/http/private/connection_monitor.h>
8
9 #include <aws/http/private/h1_connection.h>
10 #include <aws/http/private/h2_connection.h>
11
12 #include <aws/http/private/proxy_impl.h>
13
14 #include <aws/common/hash_table.h>
15 #include <aws/common/mutex.h>
16 #include <aws/common/string.h>
17 #include <aws/http/request_response.h>
18 #include <aws/io/channel_bootstrap.h>
19 #include <aws/io/future.h>
20 #include <aws/io/logging.h>
21 #include <aws/io/socket.h>
22 #include <aws/io/socket_channel_handler.h>
23 #include <aws/io/tls_channel_handler.h>
24
25 #ifdef _MSC_VER
26 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
27 # pragma warning(disable : 4232) /* function pointer to dll symbol */
28 #endif
29
30 static struct aws_http_connection_system_vtable s_default_system_vtable = {
31 .aws_client_bootstrap_new_socket_channel = aws_client_bootstrap_new_socket_channel,
32 };
33
34 static const struct aws_http_connection_system_vtable *s_system_vtable_ptr = &s_default_system_vtable;
35
36 void aws_http_client_bootstrap_destroy(struct aws_http_client_bootstrap *bootstrap) {
37 /* During allocating, the underlying stuctures should be allocated with the bootstrap by aws_mem_acquire_many. Thus,
38 * we only need to clean up the first pointer which is the bootstrap */
39 if (bootstrap->alpn_string_map) {
40 aws_hash_table_clean_up(bootstrap->alpn_string_map);
41 }
42 aws_mem_release(bootstrap->alloc, bootstrap);
43 }
44
45 void aws_http_connection_set_system_vtable(const struct aws_http_connection_system_vtable *system_vtable) {
46 s_system_vtable_ptr = system_vtable;
47 }
48
49 AWS_STATIC_STRING_FROM_LITERAL(s_alpn_protocol_http_1_1, "http/1.1");
50 AWS_STATIC_STRING_FROM_LITERAL(s_alpn_protocol_http_2, "h2");
51
52 struct aws_http_server {
53 struct aws_allocator *alloc;
54 struct aws_server_bootstrap *bootstrap;
55 bool is_using_tls;
56 bool manual_window_management;
57 size_t initial_window_size;
58 void *user_data;
59 aws_http_server_on_incoming_connection_fn *on_incoming_connection;
60 aws_http_server_on_destroy_fn *on_destroy_complete;
61 struct aws_socket *socket;
62 struct aws_future_void *setup_future;
63
64 /* Any thread may touch this data, but the lock must be held */
65 struct {
66 struct aws_mutex lock;
67 bool is_shutting_down;
68 struct aws_hash_table channel_to_connection_map;
69 } synced_data;
70 };
71
72 static void s_server_lock_synced_data(struct aws_http_server *server) {
73 int err = aws_mutex_lock(&server->synced_data.lock);
74 AWS_ASSERT(!err);
75 (void)err;
76 }
77
78 static void s_server_unlock_synced_data(struct aws_http_server *server) {
79 int err = aws_mutex_unlock(&server->synced_data.lock);
80 AWS_ASSERT(!err);
81 (void)err;
82 }
83
84 /* Determine the http-version, create appropriate type of connection, and insert it into the channel. */
85 struct aws_http_connection *aws_http_connection_new_channel_handler(
86 struct aws_allocator *alloc,
87 struct aws_channel *channel,
88 bool is_server,
89 bool is_using_tls,
90 bool manual_window_management,
91 bool prior_knowledge_http2,
92 size_t initial_window_size,
93 const struct aws_hash_table *alpn_string_map,
94 const struct aws_http1_connection_options *http1_options,
95 const struct aws_http2_connection_options *http2_options,
96 void *connection_user_data) {
97
98 struct aws_channel_slot *connection_slot = NULL;
99 struct aws_http_connection *connection = NULL;
100
101 /* Create slot for connection. */
102 connection_slot = aws_channel_slot_new(channel);
103 if (!connection_slot) {
104 AWS_LOGF_ERROR(
105 AWS_LS_HTTP_CONNECTION,
106 "static: Failed to create slot in channel %p, error %d (%s).",
107 (void *)channel,
108 aws_last_error(),
109 aws_error_name(aws_last_error()));
110
111 goto error;
112 }
113
114 int err = aws_channel_slot_insert_end(channel, connection_slot);
115 if (err) {
116 AWS_LOGF_ERROR(
117 AWS_LS_HTTP_CONNECTION,
118 "static: Failed to insert slot into channel %p, error %d (%s).",
119 (void *)channel,
120 aws_last_error(),
121 aws_error_name(aws_last_error()));
122 goto error;
123 }
124
125 /* Determine HTTP version */
126 enum aws_http_version version = AWS_HTTP_VERSION_1_1;
127
128 if (is_using_tls) {
129 /* Query TLS channel handler (immediately to left in the channel) for negotiated ALPN protocol */
130 if (!connection_slot->adj_left || !connection_slot->adj_left->handler) {
131 aws_raise_error(AWS_ERROR_INVALID_STATE);
132 AWS_LOGF_ERROR(
133 AWS_LS_HTTP_CONNECTION, "static: Failed to find TLS handler in channel %p.", (void *)channel);
134 goto error;
135 }
136
137 struct aws_channel_slot *tls_slot = connection_slot->adj_left;
138 struct aws_channel_handler *tls_handler = tls_slot->handler;
139 struct aws_byte_buf protocol = aws_tls_handler_protocol(tls_handler);
140 if (protocol.len) {
141 bool customized = false;
142 if (alpn_string_map) {
143 customized = true;
144 struct aws_string *negotiated_result = aws_string_new_from_buf(alloc, &protocol);
145 struct aws_hash_element *found = NULL;
146 aws_hash_table_find(alpn_string_map, (void *)negotiated_result, &found);
147 if (found) {
148 version = (enum aws_http_version)(size_t)found->value;
149 AWS_LOGF_DEBUG(
150 AWS_LS_HTTP_CONNECTION,
151 "static: Customized ALPN protocol " PRInSTR " used. " PRInSTR " client connection established.",
152 AWS_BYTE_BUF_PRI(protocol),
153 AWS_BYTE_CURSOR_PRI(aws_http_version_to_str(version)));
154 } else {
155 AWS_LOGF_ERROR(
156 AWS_LS_HTTP_CONNECTION,
157 "static: Customized ALPN protocol " PRInSTR
158 " used. However the it's not found in the ALPN map provided.",
159 AWS_BYTE_BUF_PRI(protocol));
160 version = AWS_HTTP_VERSION_UNKNOWN;
161 }
162 aws_string_destroy(negotiated_result);
163 }
164 if (customized) {
165 /* Do nothing */
166 } else if (aws_string_eq_byte_buf(s_alpn_protocol_http_1_1, &protocol)) {
167 version = AWS_HTTP_VERSION_1_1;
168 } else if (aws_string_eq_byte_buf(s_alpn_protocol_http_2, &protocol)) {
169 version = AWS_HTTP_VERSION_2;
170 } else {
171 AWS_LOGF_WARN(AWS_LS_HTTP_CONNECTION, "static: Unrecognized ALPN protocol. Assuming HTTP/1.1");
172 AWS_LOGF_DEBUG(
173 AWS_LS_HTTP_CONNECTION, "static: Unrecognized ALPN protocol " PRInSTR, AWS_BYTE_BUF_PRI(protocol));
174
175 version = AWS_HTTP_VERSION_1_1;
176 }
177 }
178 } else {
179 if (prior_knowledge_http2) {
180 AWS_LOGF_TRACE(AWS_LS_HTTP_CONNECTION, "Using prior knowledge to start HTTP/2 connection");
181 version = AWS_HTTP_VERSION_2;
182 }
183 }
184
185 /* Create connection/handler */
186 switch (version) {
187 case AWS_HTTP_VERSION_1_1:
188 if (is_server) {
189 connection = aws_http_connection_new_http1_1_server(
190 alloc, manual_window_management, initial_window_size, http1_options);
191 } else {
192 connection = aws_http_connection_new_http1_1_client(
193 alloc, manual_window_management, initial_window_size, http1_options);
194 }
195 break;
196 case AWS_HTTP_VERSION_2:
197 if (is_server) {
198 connection = aws_http_connection_new_http2_server(
199 alloc, manual_window_management, initial_window_size, http2_options);
200 } else {
201 connection = aws_http_connection_new_http2_client(
202 alloc, manual_window_management, initial_window_size, http2_options);
203 }
204 break;
205 default:
206 AWS_LOGF_ERROR(
207 AWS_LS_HTTP_CONNECTION,
208 "static: Unsupported version " PRInSTR,
209 AWS_BYTE_CURSOR_PRI(aws_http_version_to_str(version)));
210
211 aws_raise_error(AWS_ERROR_HTTP_UNSUPPORTED_PROTOCOL);
212 goto error;
213 }
214
215 if (!connection) {
216 AWS_LOGF_ERROR(
217 AWS_LS_HTTP_CONNECTION,
218 "static: Failed to create " PRInSTR " %s connection object, error %d (%s).",
219 AWS_BYTE_CURSOR_PRI(aws_http_version_to_str(version)),
220 is_server ? "server" : "client",
221 aws_last_error(),
222 aws_error_name(aws_last_error()));
223
224 goto error;
225 }
226 connection->user_data = connection_user_data;
227
228 /* Connect handler and slot */
229 if (aws_channel_slot_set_handler(connection_slot, &connection->channel_handler)) {
230 AWS_LOGF_ERROR(
231 AWS_LS_HTTP_CONNECTION,
232 "static: Failed to set HTTP handler into slot on channel %p, error %d (%s).",
233 (void *)channel,
234 aws_last_error(),
235 aws_error_name(aws_last_error()));
236
237 goto error;
238 }
239
240 /* Success! Inform connection that installation is complete */
241 connection->vtable->on_channel_handler_installed(&connection->channel_handler, connection_slot);
242
243 return connection;
244
245 error:
246 if (connection_slot) {
247 if (!connection_slot->handler && connection) {
248 aws_channel_handler_destroy(&connection->channel_handler);
249 }
250
251 aws_channel_slot_remove(connection_slot);
252 }
253
254 return NULL;
255 }
256
257 void aws_http_connection_close(struct aws_http_connection *connection) {
258 AWS_ASSERT(connection);
259 connection->vtable->close(connection);
260 }
261
262 void aws_http_connection_stop_new_requests(struct aws_http_connection *connection) {
263 AWS_ASSERT(connection);
264 connection->vtable->stop_new_requests(connection);
265 }
266
267 bool aws_http_connection_is_open(const struct aws_http_connection *connection) {
268 AWS_ASSERT(connection);
269 return connection->vtable->is_open(connection);
270 }
271
272 bool aws_http_connection_new_requests_allowed(const struct aws_http_connection *connection) {
273 AWS_ASSERT(connection);
274 return connection->vtable->new_requests_allowed(connection);
275 }
276
277 bool aws_http_connection_is_client(const struct aws_http_connection *connection) {
278 return connection->client_data;
279 }
280
281 bool aws_http_connection_is_server(const struct aws_http_connection *connection) {
282 return connection->server_data;
283 }
284
285 int aws_http2_connection_change_settings(
286 struct aws_http_connection *http2_connection,
287 const struct aws_http2_setting *settings_array,
288 size_t num_settings,
289 aws_http2_on_change_settings_complete_fn *on_completed,
290 void *user_data) {
291 AWS_ASSERT(http2_connection);
292 AWS_PRECONDITION(http2_connection->vtable);
293 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
294 return http2_connection->vtable->change_settings(
295 http2_connection, settings_array, num_settings, on_completed, user_data);
296 }
297
298 int aws_http2_connection_ping(
299 struct aws_http_connection *http2_connection,
300 const struct aws_byte_cursor *optional_opaque_data,
301 aws_http2_on_ping_complete_fn *on_ack,
302 void *user_data) {
303 AWS_ASSERT(http2_connection);
304 AWS_PRECONDITION(http2_connection->vtable);
305 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
306 return http2_connection->vtable->send_ping(http2_connection, optional_opaque_data, on_ack, user_data);
307 }
308
309 void aws_http2_connection_send_goaway(
310 struct aws_http_connection *http2_connection,
311 uint32_t http2_error,
312 bool allow_more_streams,
313 const struct aws_byte_cursor *optional_debug_data) {
314 AWS_ASSERT(http2_connection);
315 AWS_PRECONDITION(http2_connection->vtable);
316 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
317 http2_connection->vtable->send_goaway(http2_connection, http2_error, allow_more_streams, optional_debug_data);
318 }
319
320 int aws_http2_connection_get_sent_goaway(
321 struct aws_http_connection *http2_connection,
322 uint32_t *out_http2_error,
323 uint32_t *out_last_stream_id) {
324 AWS_ASSERT(http2_connection);
325 AWS_PRECONDITION(out_http2_error);
326 AWS_PRECONDITION(out_last_stream_id);
327 AWS_PRECONDITION(http2_connection->vtable);
328 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
329 return http2_connection->vtable->get_sent_goaway(http2_connection, out_http2_error, out_last_stream_id);
330 }
331
332 int aws_http2_connection_get_received_goaway(
333 struct aws_http_connection *http2_connection,
334 uint32_t *out_http2_error,
335 uint32_t *out_last_stream_id) {
336 AWS_ASSERT(http2_connection);
337 AWS_PRECONDITION(out_http2_error);
338 AWS_PRECONDITION(out_last_stream_id);
339 AWS_PRECONDITION(http2_connection->vtable);
340 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
341 return http2_connection->vtable->get_received_goaway(http2_connection, out_http2_error, out_last_stream_id);
342 }
343
344 void aws_http2_connection_get_local_settings(
345 const struct aws_http_connection *http2_connection,
346 struct aws_http2_setting out_settings[AWS_HTTP2_SETTINGS_COUNT]) {
347 AWS_ASSERT(http2_connection);
348 AWS_PRECONDITION(http2_connection->vtable);
349 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
350 http2_connection->vtable->get_local_settings(http2_connection, out_settings);
351 }
352
353 void aws_http2_connection_get_remote_settings(
354 const struct aws_http_connection *http2_connection,
355 struct aws_http2_setting out_settings[AWS_HTTP2_SETTINGS_COUNT]) {
356 AWS_ASSERT(http2_connection);
357 AWS_PRECONDITION(http2_connection->vtable);
358 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
359 http2_connection->vtable->get_remote_settings(http2_connection, out_settings);
360 }
361
362 void aws_http2_connection_update_window(struct aws_http_connection *http2_connection, uint32_t increment_size) {
363 AWS_ASSERT(http2_connection);
364 AWS_PRECONDITION(http2_connection->vtable);
365 AWS_FATAL_ASSERT(http2_connection->http_version == AWS_HTTP_VERSION_2);
366 http2_connection->vtable->update_window(http2_connection, increment_size);
367 }
368
369 struct aws_channel *aws_http_connection_get_channel(struct aws_http_connection *connection) {
370 AWS_ASSERT(connection);
371 return connection->channel_slot->channel;
372 }
373
374 const struct aws_socket_endpoint *aws_http_connection_get_remote_endpoint(
375 const struct aws_http_connection *connection) {
376 AWS_ASSERT(connection);
377 struct aws_channel *channel = connection->channel_slot->channel;
378 /* The first slot for an HTTP connection is always socket */
379 struct aws_channel_slot *socket_slot = aws_channel_get_first_slot(channel);
380 const struct aws_socket *socket = aws_socket_handler_get_socket(socket_slot->handler);
381 return &socket->remote_endpoint;
382 }
383
384 int aws_http_alpn_map_init(struct aws_allocator *allocator, struct aws_hash_table *map) {
385 AWS_ASSERT(allocator);
386 AWS_ASSERT(map);
387 int result = aws_hash_table_init(
388 map,
389 allocator,
390 5 /* initial size */,
391 aws_hash_string,
392 aws_hash_callback_string_eq,
393 aws_hash_callback_string_destroy,
394 NULL);
395 if (result) {
396 /* OOM will crash */
397 int error_code = aws_last_error();
398 AWS_LOGF_ERROR(
399 AWS_LS_HTTP_CONNECTION,
400 "Failed to initialize ALPN map with error code %d (%s)",
401 error_code,
402 aws_error_name(error_code));
403 }
404 return result;
405 }
406
407 void aws_http_connection_acquire(struct aws_http_connection *connection) {
408 AWS_ASSERT(connection);
409 aws_atomic_fetch_add(&connection->refcount, 1);
410 }
411
412 void aws_http_connection_release(struct aws_http_connection *connection) {
413 if (!connection) {
414 return;
415 }
416 size_t prev_refcount = aws_atomic_fetch_sub(&connection->refcount, 1);
417 if (prev_refcount == 1) {
418 AWS_LOGF_TRACE(
419 AWS_LS_HTTP_CONNECTION,
420 "id=%p: Final connection refcount released, shut down if necessary.",
421 (void *)connection);
422
423 /* Channel might already be shut down, but make sure */
424 aws_channel_shutdown(connection->channel_slot->channel, AWS_ERROR_SUCCESS);
425
426 /* When the channel's refcount reaches 0, it destroys its slots/handlers, which will destroy the connection */
427 aws_channel_release_hold(connection->channel_slot->channel);
428 } else {
429 AWS_FATAL_ASSERT(prev_refcount != 0);
430 AWS_LOGF_TRACE(
431 AWS_LS_HTTP_CONNECTION,
432 "id=%p: Connection refcount released, %zu remaining.",
433 (void *)connection,
434 prev_refcount - 1);
435 }
436 }
437
438 /* At this point, the server bootstrapper has accepted an incoming connection from a client and set up a channel.
439 * Now we need to create an aws_http_connection and insert it into the channel as a channel-handler.
440 * Note: Be careful not to access server->socket until lock is acquired to avoid race conditions */
441 static void s_server_bootstrap_on_accept_channel_setup(
442 struct aws_server_bootstrap *bootstrap,
443 int error_code,
444 struct aws_channel *channel,
445 void *user_data) {
446
447 (void)bootstrap;
448 AWS_ASSERT(user_data);
449 struct aws_http_server *server = user_data;
450 bool user_cb_invoked = false;
451 struct aws_http_connection *connection = NULL;
452 if (error_code) {
453 AWS_LOGF_ERROR(
454 AWS_LS_HTTP_SERVER,
455 "%p: Incoming connection failed with error code %d (%s)",
456 (void *)server,
457 error_code,
458 aws_error_name(error_code));
459
460 goto error;
461 }
462 /* Create connection */
463 /* TODO: expose http1/2 options to server API */
464 struct aws_http1_connection_options http1_options;
465 AWS_ZERO_STRUCT(http1_options);
466 struct aws_http2_connection_options http2_options;
467 AWS_ZERO_STRUCT(http2_options);
468 connection = aws_http_connection_new_channel_handler(
469 server->alloc,
470 channel,
471 true,
472 server->is_using_tls,
473 server->manual_window_management,
474 false, /* prior_knowledge_http2 */
475 server->initial_window_size,
476 NULL, /* alpn_string_map */
477 &http1_options,
478 &http2_options,
479 NULL /* connection_user_data */);
480 if (!connection) {
481 AWS_LOGF_ERROR(
482 AWS_LS_HTTP_SERVER,
483 "%p: Failed to create connection object, error %d (%s).",
484 (void *)server,
485 aws_last_error(),
486 aws_error_name(aws_last_error()));
487
488 goto error;
489 }
490
491 int put_err = 0;
492 /* BEGIN CRITICAL SECTION */
493 s_server_lock_synced_data(server);
494 if (server->synced_data.is_shutting_down) {
495 error_code = AWS_ERROR_HTTP_CONNECTION_CLOSED;
496 }
497 if (!error_code) {
498 put_err = aws_hash_table_put(&server->synced_data.channel_to_connection_map, channel, connection, NULL);
499 }
500 s_server_unlock_synced_data(server);
501 /* END CRITICAL SECTION */
502 if (error_code) {
503 AWS_LOGF_ERROR(
504 AWS_ERROR_HTTP_SERVER_CLOSED,
505 "id=%p: Incoming connection failed. The server is shutting down.",
506 (void *)server);
507 goto error;
508 }
509
510 if (put_err) {
511 AWS_LOGF_ERROR(
512 AWS_LS_HTTP_SERVER,
513 "%p: %s:%u: Failed to store connection object, error %d (%s).",
514 (void *)server,
515 server->socket->local_endpoint.address,
516 server->socket->local_endpoint.port,
517 aws_last_error(),
518 aws_error_name(aws_last_error()));
519
520 goto error;
521 }
522
523 /* Tell user of successful connection. */
524 AWS_LOGF_INFO(
525 AWS_LS_HTTP_CONNECTION,
526 "id=%p: " PRInSTR " server connection established at %p %s:%u.",
527 (void *)connection,
528 AWS_BYTE_CURSOR_PRI(aws_http_version_to_str(connection->http_version)),
529 (void *)server,
530 server->socket->local_endpoint.address,
531 server->socket->local_endpoint.port);
532
533 server->on_incoming_connection(server, connection, AWS_ERROR_SUCCESS, server->user_data);
534 user_cb_invoked = true;
535
536 /* If user failed to configure the server during callback, shut down the channel. */
537 if (!connection->server_data->on_incoming_request) {
538 AWS_LOGF_ERROR(
539 AWS_LS_HTTP_CONNECTION,
540 "id=%p: Caller failed to invoke aws_http_connection_configure_server() during on_incoming_connection "
541 "callback, closing connection.",
542 (void *)connection);
543
544 aws_raise_error(AWS_ERROR_HTTP_REACTION_REQUIRED);
545 goto error;
546 }
547 return;
548
549 error:
550
551 if (!error_code) {
552 error_code = aws_last_error();
553 }
554
555 if (!user_cb_invoked) {
556 server->on_incoming_connection(server, NULL, error_code, server->user_data);
557 }
558
559 if (channel) {
560 aws_channel_shutdown(channel, error_code);
561 }
562
563 if (connection) {
564 /* release the ref count for the user side */
565 aws_http_connection_release(connection);
566 }
567 }
568
569 /* clean the server memory up */
570 static void s_http_server_clean_up(struct aws_http_server *server) {
571 if (!server) {
572 return;
573 }
574
575 aws_server_bootstrap_release(server->bootstrap);
576
577 /* invoke the user callback */
578 if (server->on_destroy_complete) {
579 server->on_destroy_complete(server->user_data);
580 }
581 aws_hash_table_clean_up(&server->synced_data.channel_to_connection_map);
582 aws_mutex_clean_up(&server->synced_data.lock);
583 aws_future_void_release(server->setup_future);
584 aws_mem_release(server->alloc, server);
585 }
586
587 /* At this point, the channel for a server connection has completed shutdown, but hasn't been destroyed yet. */
588 static void s_server_bootstrap_on_accept_channel_shutdown(
589 struct aws_server_bootstrap *bootstrap,
590 int error_code,
591 struct aws_channel *channel,
592 void *user_data) {
593
594 (void)bootstrap;
595 AWS_ASSERT(user_data);
596 struct aws_http_server *server = user_data;
597
598 /* Figure out which connection this was, and remove that entry from the map.
599 * It won't be in the map if something went wrong while setting up the connection. */
600 struct aws_hash_element map_elem;
601 int was_present;
602
603 /* BEGIN CRITICAL SECTION */
604 s_server_lock_synced_data(server);
605 int remove_err =
606 aws_hash_table_remove(&server->synced_data.channel_to_connection_map, channel, &map_elem, &was_present);
607 s_server_unlock_synced_data(server);
608 /* END CRITICAL SECTION */
609
610 if (!remove_err && was_present) {
611 struct aws_http_connection *connection = map_elem.value;
612 AWS_LOGF_INFO(AWS_LS_HTTP_CONNECTION, "id=%p: Server connection shut down.", (void *)connection);
613 /* Tell user about shutdown */
614 if (connection->server_data->on_shutdown) {
615 connection->server_data->on_shutdown(connection, error_code, connection->user_data);
616 }
617 }
618 }
619
620 /* the server listener has finished the destroy process, no existing connections
621 * finally safe to clean the server up */
622 static void s_server_bootstrap_on_server_listener_destroy(struct aws_server_bootstrap *bootstrap, void *user_data) {
623 (void)bootstrap;
624 AWS_ASSERT(user_data);
625 struct aws_http_server *server = user_data;
626 s_http_server_clean_up(server);
627 }
628
629 /* the server listener has finished setup. We released the socket if error_code is not 0. */
630 static void s_server_bootstrap_on_server_listener_setup(
631 struct aws_server_bootstrap *bootstrap,
632 int error_code,
633 void *user_data) {
634 (void)bootstrap;
635 struct aws_http_server *server = user_data;
636 if (error_code) {
637 aws_future_void_set_error(server->setup_future, error_code);
638 } else {
639 aws_future_void_set_result(server->setup_future);
640 }
641 }
642
643 struct aws_http_server *aws_http_server_new(const struct aws_http_server_options *options) {
644 aws_http_fatal_assert_library_initialized();
645
646 struct aws_http_server *server = NULL;
647
648 if (!options || options->self_size == 0 || !options->allocator || !options->bootstrap || !options->socket_options ||
649 !options->on_incoming_connection || !options->endpoint) {
650
651 AWS_LOGF_ERROR(AWS_LS_HTTP_SERVER, "static: Invalid options, cannot create server.");
652 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
653 /* nothing to clean up */
654 return NULL;
655 }
656
657 server = aws_mem_calloc(options->allocator, 1, sizeof(struct aws_http_server));
658 if (!server) {
659 /* nothing to clean up */
660 return NULL;
661 }
662
663 server->alloc = options->allocator;
664 server->bootstrap = aws_server_bootstrap_acquire(options->bootstrap);
665 server->is_using_tls = options->tls_options != NULL;
666 server->initial_window_size = options->initial_window_size;
667 server->user_data = options->server_user_data;
668 server->on_incoming_connection = options->on_incoming_connection;
669 server->on_destroy_complete = options->on_destroy_complete;
670 server->manual_window_management = options->manual_window_management;
671
672 int err = aws_mutex_init(&server->synced_data.lock);
673 if (err) {
674 AWS_LOGF_ERROR(
675 AWS_LS_HTTP_SERVER, "static: Failed to initialize mutex, error %d (%s).", err, aws_error_name(err));
676 goto server_error;
677 }
678 err = aws_hash_table_init(
679 &server->synced_data.channel_to_connection_map, server->alloc, 16, aws_hash_ptr, aws_ptr_eq, NULL, NULL);
680 if (err) {
681 AWS_LOGF_ERROR(
682 AWS_LS_HTTP_SERVER,
683 "static: Cannot create server, error %d (%s).",
684 aws_last_error(),
685 aws_error_name(aws_last_error()));
686 goto server_error;
687 }
688 /* Protect against callbacks firing before server->socket is set */
689 s_server_lock_synced_data(server);
690 if (options->tls_options) {
691 server->is_using_tls = true;
692 }
693
694 server->setup_future = aws_future_void_new(options->allocator);
695
696 struct aws_server_socket_channel_bootstrap_options bootstrap_options = {
697 .enable_read_back_pressure = options->manual_window_management,
698 .tls_options = options->tls_options,
699 .bootstrap = options->bootstrap,
700 .socket_options = options->socket_options,
701 .incoming_callback = s_server_bootstrap_on_accept_channel_setup,
702 .shutdown_callback = s_server_bootstrap_on_accept_channel_shutdown,
703 .destroy_callback = s_server_bootstrap_on_server_listener_destroy,
704 .setup_callback = s_server_bootstrap_on_server_listener_setup,
705 .host_name = options->endpoint->address,
706 .port = options->endpoint->port,
707 .user_data = server,
708 };
709
710 int listen_error = AWS_OP_SUCCESS;
711
712 /*
713 * WARNING & TODO!!!!
714 * aws_server_bootstrap_new_socket_listener has async callback, we would block here waiting for
715 * setup complete. aws-c-http library need to be updated with a proper async API.
716 */
717 server->socket = aws_server_bootstrap_new_socket_listener(&bootstrap_options);
718 // if server setup properly, waiting for setup callback
719 if (server->socket) {
720 aws_future_void_wait(server->setup_future, UINT64_MAX /*timeout*/);
721 listen_error = aws_future_void_get_error(server->setup_future);
722 } else {
723 listen_error = aws_last_error();
724 }
725 s_server_unlock_synced_data(server);
726
727 if (listen_error) {
728 AWS_LOGF_ERROR(
729 AWS_LS_HTTP_SERVER,
730 "static: Failed creating new socket listener, error %d (%s). Cannot create server.",
731 listen_error,
732 aws_error_name(listen_error));
733
734 aws_raise_error(listen_error);
735 goto server_error;
736 }
737
738 AWS_LOGF_INFO(
739 AWS_LS_HTTP_SERVER,
740 "%p %s:%u: Server setup complete, listening for incoming connections.",
741 (void *)server,
742 server->socket->local_endpoint.address,
743 server->socket->local_endpoint.port);
744
745 return server;
746
747 server_error:
748 s_http_server_clean_up(server);
749 return NULL;
750 }
751
752 void aws_http_server_release(struct aws_http_server *server) {
753 if (!server) {
754 return;
755 }
756 bool already_shutting_down = false;
757 /* BEGIN CRITICAL SECTION */
758 s_server_lock_synced_data(server);
759 if (server->synced_data.is_shutting_down) {
760 already_shutting_down = true;
761 } else {
762 server->synced_data.is_shutting_down = true;
763 }
764 if (!already_shutting_down) {
765 /* shutdown all existing channels */
766 for (struct aws_hash_iter iter = aws_hash_iter_begin(&server->synced_data.channel_to_connection_map);
767 !aws_hash_iter_done(&iter);
768 aws_hash_iter_next(&iter)) {
769 struct aws_channel *channel = (struct aws_channel *)iter.element.key;
770 aws_channel_shutdown(channel, AWS_ERROR_HTTP_CONNECTION_CLOSED);
771 }
772 }
773 s_server_unlock_synced_data(server);
774 /* END CRITICAL SECTION */
775
776 if (already_shutting_down) {
777 /* The service is already shutting down, not shutting it down again */
778 AWS_LOGF_TRACE(AWS_LS_HTTP_SERVER, "id=%p: The server is already shutting down", (void *)server);
779 return;
780 }
781
782 /* stop listening, clean up the socket, after all existing connections finish shutting down, the
783 * s_server_bootstrap_on_server_listener_destroy will be invoked, clean up of the server will be there */
784 AWS_LOGF_INFO(
785 AWS_LS_HTTP_SERVER,
786 "%p %s:%u: Shutting down the server.",
787 (void *)server,
788 server->socket->local_endpoint.address,
789 server->socket->local_endpoint.port);
790
791 aws_server_bootstrap_destroy_socket_listener(server->bootstrap, server->socket);
792
793 /* wait for connections to finish shutting down
794 * clean up will be called from eventloop */
795 }
796
797 const struct aws_socket_endpoint *aws_http_server_get_listener_endpoint(const struct aws_http_server *server) {
798 AWS_FATAL_ASSERT(server);
799
800 return &server->socket->local_endpoint;
801 }
802
803 /* At this point, the channel bootstrapper has established a connection to the server and set up a channel.
804 * Now we need to create the aws_http_connection and insert it into the channel as a channel-handler. */
805 static void s_client_bootstrap_on_channel_setup(
806 struct aws_client_bootstrap *channel_bootstrap,
807 int error_code,
808 struct aws_channel *channel,
809 void *user_data) {
810
811 (void)channel_bootstrap;
812 AWS_ASSERT(user_data);
813 struct aws_http_client_bootstrap *http_bootstrap = user_data;
814
815 /* Contract for setup callbacks is: channel is NULL if error_code is non-zero. */
816 AWS_FATAL_ASSERT((error_code != 0) == (channel == NULL));
817
818 if (error_code) {
819 AWS_LOGF_ERROR(
820 AWS_LS_HTTP_CONNECTION,
821 "static: Client connection failed with error %d (%s).",
822 error_code,
823 aws_error_name(error_code));
824
825 /* Immediately tell user of failed connection.
826 * No channel exists, so there will be no channel_shutdown callback. */
827 http_bootstrap->on_setup(NULL, error_code, http_bootstrap->user_data);
828
829 /* Clean up the http_bootstrap, it has no more work to do. */
830 aws_http_client_bootstrap_destroy(http_bootstrap);
831 return;
832 }
833
834 AWS_LOGF_TRACE(AWS_LS_HTTP_CONNECTION, "static: Socket connected, creating client connection object.");
835
836 http_bootstrap->connection = aws_http_connection_new_channel_handler(
837 http_bootstrap->alloc,
838 channel,
839 false,
840 http_bootstrap->is_using_tls,
841 http_bootstrap->stream_manual_window_management,
842 http_bootstrap->prior_knowledge_http2,
843 http_bootstrap->initial_window_size,
844 http_bootstrap->alpn_string_map,
845 &http_bootstrap->http1_options,
846 &http_bootstrap->http2_options,
847 http_bootstrap->user_data);
848 if (!http_bootstrap->connection) {
849 AWS_LOGF_ERROR(
850 AWS_LS_HTTP_CONNECTION,
851 "static: Failed to create the client connection object, error %d (%s).",
852 aws_last_error(),
853 aws_error_name(aws_last_error()));
854
855 goto error;
856 }
857
858 if (aws_http_connection_monitoring_options_is_valid(&http_bootstrap->monitoring_options)) {
859 /*
860 * On creation we validate monitoring options, if they exist, and fail if they're not
861 * valid. So at this point, is_valid() functions as an is-monitoring-on? check. A false
862 * value here is not an error, it's just not enabled.
863 */
864 struct aws_crt_statistics_handler *http_connection_monitor =
865 aws_crt_statistics_handler_new_http_connection_monitor(
866 http_bootstrap->alloc, &http_bootstrap->monitoring_options);
867 if (http_connection_monitor == NULL) {
868 goto error;
869 }
870
871 aws_channel_set_statistics_handler(channel, http_connection_monitor);
872 }
873
874 http_bootstrap->connection->proxy_request_transform = http_bootstrap->proxy_request_transform;
875 http_bootstrap->connection->client_data->response_first_byte_timeout_ms =
876 http_bootstrap->response_first_byte_timeout_ms;
877
878 AWS_LOGF_INFO(
879 AWS_LS_HTTP_CONNECTION,
880 "id=%p: " PRInSTR " client connection established.",
881 (void *)http_bootstrap->connection,
882 AWS_BYTE_CURSOR_PRI(aws_http_version_to_str(http_bootstrap->connection->http_version)));
883
884 /* Tell user of successful connection.
885 * Then clear the on_setup callback so that we know it's been called */
886 http_bootstrap->on_setup(http_bootstrap->connection, AWS_ERROR_SUCCESS, http_bootstrap->user_data);
887 http_bootstrap->on_setup = NULL;
888
889 return;
890
891 error:
892 /* Something went wrong. Invoke channel shutdown. Then wait for channel shutdown to complete
893 * before informing the user that setup failed and cleaning up the http_bootstrap.*/
894 aws_channel_shutdown(channel, aws_last_error());
895 }
896
897 /* At this point, the channel for a client connection has completed its shutdown */
898 static void s_client_bootstrap_on_channel_shutdown(
899 struct aws_client_bootstrap *channel_bootstrap,
900 int error_code,
901 struct aws_channel *channel,
902 void *user_data) {
903
904 (void)channel_bootstrap;
905 (void)channel;
906
907 AWS_ASSERT(user_data);
908 struct aws_http_client_bootstrap *http_bootstrap = user_data;
909
910 /* If on_setup hasn't been called yet, inform user of failed setup.
911 * If on_setup was already called, inform user that it's shut down now. */
912 if (http_bootstrap->on_setup) {
913 /* make super duper sure that failed setup receives a non-zero error_code */
914 if (error_code == 0) {
915 error_code = AWS_ERROR_UNKNOWN;
916 }
917
918 AWS_LOGF_ERROR(
919 AWS_LS_HTTP_CONNECTION,
920 "static: Client setup failed with error %d (%s).",
921 error_code,
922 aws_error_name(error_code));
923
924 http_bootstrap->on_setup(NULL, error_code, http_bootstrap->user_data);
925
926 } else if (http_bootstrap->on_shutdown) {
927 AWS_LOGF_INFO(
928 AWS_LS_HTTP_CONNECTION,
929 "%p: Client shutdown completed with error %d (%s).",
930 (void *)http_bootstrap->connection,
931 error_code,
932 aws_error_name(error_code));
933
934 http_bootstrap->on_shutdown(http_bootstrap->connection, error_code, http_bootstrap->user_data);
935 }
936
937 /* Clean up bootstrapper */
938 aws_http_client_bootstrap_destroy(http_bootstrap);
939 }
940
941 int s_validate_http_client_connection_options(const struct aws_http_client_connection_options *options) {
942 if (options->self_size == 0) {
943 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: Invalid connection options, self size not initialized");
944 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
945 }
946
947 if (!options->allocator) {
948 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: Invalid connection options, no allocator supplied");
949 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
950 }
951
952 if (options->host_name.len == 0) {
953 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: Invalid connection options, empty host name.");
954 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
955 }
956
957 if (!options->socket_options) {
958 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: Invalid connection options, socket options are null.");
959 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
960 }
961
962 if (!options->on_setup) {
963 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: Invalid connection options, setup callback is null");
964 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
965 }
966
967 /* http2_options cannot be NULL here, calling function adds them if they were missing */
968 if (options->http2_options->num_initial_settings > 0 && !options->http2_options->initial_settings_array) {
969 AWS_LOGF_ERROR(
970 AWS_LS_HTTP_CONNECTION,
971 "static: Invalid connection options, h2 settings count is non-zero but settings array is null");
972 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
973 }
974
975 if (options->monitoring_options && !aws_http_connection_monitoring_options_is_valid(options->monitoring_options)) {
976 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: Invalid connection options, invalid monitoring options");
977 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
978 }
979
980 if (options->prior_knowledge_http2 && options->tls_options) {
981 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: HTTP/2 prior knowledge only works with cleartext TCP.");
982 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
983 }
984
985 return AWS_OP_SUCCESS;
986 }
987
988 struct s_copy_alpn_string_map_context {
989 struct aws_hash_table *map;
990 struct aws_allocator *allocator;
991 };
992
993 /* put every item into the source to make a deep copy of the map */
994 static int s_copy_alpn_string_map(void *context, struct aws_hash_element *item) {
995 struct s_copy_alpn_string_map_context *func_context = context;
996 struct aws_hash_table *dest = func_context->map;
997 /* make a deep copy of the string and hash map will own the copy */
998 struct aws_string *key_copy = aws_string_new_from_string(func_context->allocator, item->key);
999 int was_created;
1000 if (aws_hash_table_put(dest, key_copy, item->value, &was_created)) {
1001 int error_code = aws_last_error();
1002 AWS_LOGF_ERROR(
1003 AWS_LS_HTTP_CONNECTION,
1004 "Failed to copy ALPN map with error code %d (%s)",
1005 error_code,
1006 aws_error_name(error_code));
1007 /* failed to put into the table, we need to clean up the copy ourselves */
1008 aws_string_destroy(key_copy);
1009 /* return error to stop iteration */
1010 return AWS_COMMON_HASH_TABLE_ITER_ERROR;
1011 }
1012 if (!was_created) {
1013 /* no new entry created, clean up the copy ourselves */
1014 aws_string_destroy(key_copy);
1015 }
1016 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
1017 }
1018
1019 int aws_http_alpn_map_init_copy(
1020 struct aws_allocator *allocator,
1021 struct aws_hash_table *dest,
1022 struct aws_hash_table *src) {
1023 if (!src) {
1024 AWS_ZERO_STRUCT(*dest);
1025 return AWS_OP_SUCCESS;
1026 }
1027 if (!src->p_impl) {
1028 AWS_ZERO_STRUCT(*dest);
1029 return AWS_OP_SUCCESS;
1030 }
1031
1032 if (aws_http_alpn_map_init(allocator, dest)) {
1033 return AWS_OP_ERR;
1034 }
1035 struct s_copy_alpn_string_map_context context;
1036 context.allocator = allocator;
1037 context.map = dest;
1038 /* make a deep copy of the map */
1039 if (aws_hash_table_foreach(src, s_copy_alpn_string_map, &context)) {
1040 int error_code = aws_last_error();
1041 AWS_LOGF_ERROR(
1042 AWS_LS_HTTP_CONNECTION,
1043 "Failed to copy ALPN map with error code %d (%s)",
1044 error_code,
1045 aws_error_name(error_code));
1046 aws_hash_table_clean_up(dest);
1047 return AWS_OP_ERR;
1048 }
1049 return AWS_OP_SUCCESS;
1050 }
1051
1052 int aws_http_client_connect_internal(
1053 const struct aws_http_client_connection_options *orig_options,
1054 aws_http_proxy_request_transform_fn *proxy_request_transform) {
1055
1056 if (!orig_options) {
1057 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: http connection options are null.");
1058 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1059 }
1060 struct aws_http_client_bootstrap *http_bootstrap = NULL;
1061 struct aws_string *host_name = NULL;
1062 int err = 0;
1063
1064 /* make copy of options, and add defaults for missing optional structs */
1065 struct aws_http_client_connection_options options = *orig_options;
1066 struct aws_http1_connection_options default_http1_options;
1067 AWS_ZERO_STRUCT(default_http1_options);
1068 if (options.http1_options == NULL) {
1069 options.http1_options = &default_http1_options;
1070 }
1071
1072 struct aws_http2_connection_options default_http2_options;
1073 AWS_ZERO_STRUCT(default_http2_options);
1074 if (options.http2_options == NULL) {
1075 options.http2_options = &default_http2_options;
1076 }
1077
1078 /* validate options */
1079 if (s_validate_http_client_connection_options(&options)) {
1080 goto error;
1081 }
1082
1083 AWS_FATAL_ASSERT(options.proxy_options == NULL);
1084
1085 /* bootstrap_new() functions requires a null-terminated c-str */
1086 host_name = aws_string_new_from_cursor(options.allocator, &options.host_name);
1087 if (!host_name) {
1088 goto error;
1089 }
1090
1091 struct aws_http2_setting *setting_array = NULL;
1092 struct aws_hash_table *alpn_string_map = NULL;
1093 aws_mem_acquire_many(
1094 options.allocator,
1095 3,
1096 &http_bootstrap,
1097 sizeof(struct aws_http_client_bootstrap),
1098 &setting_array,
1099 options.http2_options->num_initial_settings * sizeof(struct aws_http2_setting),
1100 &alpn_string_map,
1101 sizeof(struct aws_hash_table));
1102
1103 AWS_ZERO_STRUCT(*http_bootstrap);
1104
1105 http_bootstrap->alloc = options.allocator;
1106 http_bootstrap->is_using_tls = options.tls_options != NULL;
1107 http_bootstrap->stream_manual_window_management = options.manual_window_management;
1108 http_bootstrap->prior_knowledge_http2 = options.prior_knowledge_http2;
1109 http_bootstrap->initial_window_size = options.initial_window_size;
1110 http_bootstrap->user_data = options.user_data;
1111 http_bootstrap->on_setup = options.on_setup;
1112 http_bootstrap->on_shutdown = options.on_shutdown;
1113 http_bootstrap->proxy_request_transform = proxy_request_transform;
1114 http_bootstrap->http1_options = *options.http1_options;
1115 http_bootstrap->http2_options = *options.http2_options;
1116 http_bootstrap->response_first_byte_timeout_ms = options.response_first_byte_timeout_ms;
1117
1118 /* keep a copy of the settings array if it's not NULL */
1119 if (options.http2_options->num_initial_settings > 0) {
1120 memcpy(
1121 setting_array,
1122 options.http2_options->initial_settings_array,
1123 options.http2_options->num_initial_settings * sizeof(struct aws_http2_setting));
1124 http_bootstrap->http2_options.initial_settings_array = setting_array;
1125 }
1126
1127 if (options.alpn_string_map) {
1128 if (aws_http_alpn_map_init_copy(options.allocator, alpn_string_map, options.alpn_string_map)) {
1129 goto error;
1130 }
1131 http_bootstrap->alpn_string_map = alpn_string_map;
1132 }
1133
1134 if (options.monitoring_options) {
1135 http_bootstrap->monitoring_options = *options.monitoring_options;
1136 }
1137
1138 AWS_LOGF_TRACE(
1139 AWS_LS_HTTP_CONNECTION,
1140 "static: attempting to initialize a new client channel to %s:%u",
1141 aws_string_c_str(host_name),
1142 options.port);
1143
1144 struct aws_socket_channel_bootstrap_options channel_options = {
1145 .bootstrap = options.bootstrap,
1146 .host_name = aws_string_c_str(host_name),
1147 .port = options.port,
1148 .socket_options = options.socket_options,
1149 .tls_options = options.tls_options,
1150 .setup_callback = s_client_bootstrap_on_channel_setup,
1151 .shutdown_callback = s_client_bootstrap_on_channel_shutdown,
1152 .enable_read_back_pressure = options.manual_window_management,
1153 .user_data = http_bootstrap,
1154 .requested_event_loop = options.requested_event_loop,
1155 .host_resolution_override_config = options.host_resolution_config,
1156 };
1157
1158 err = s_system_vtable_ptr->aws_client_bootstrap_new_socket_channel(&channel_options);
1159
1160 if (err) {
1161 AWS_LOGF_ERROR(
1162 AWS_LS_HTTP_CONNECTION,
1163 "static: Failed to initiate socket channel for new client connection, error %d (%s).",
1164 aws_last_error(),
1165 aws_error_name(aws_last_error()));
1166
1167 goto error;
1168 }
1169
1170 aws_string_destroy(host_name);
1171 return AWS_OP_SUCCESS;
1172
1173 error:
1174 if (http_bootstrap) {
1175 aws_http_client_bootstrap_destroy(http_bootstrap);
1176 }
1177
1178 if (host_name) {
1179 aws_string_destroy(host_name);
1180 }
1181
1182 return AWS_OP_ERR;
1183 }
1184
1185 int aws_http_client_connect(const struct aws_http_client_connection_options *options) {
1186 aws_http_fatal_assert_library_initialized();
1187 if (options->prior_knowledge_http2 && options->tls_options) {
1188 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "static: HTTP/2 prior knowledge only works with cleartext TCP.");
1189 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1190 }
1191
1192 if (options->proxy_options != NULL) {
1193 return aws_http_client_connect_via_proxy(options);
1194 } else {
1195 if (!options->proxy_ev_settings || options->proxy_ev_settings->env_var_type != AWS_HPEV_ENABLE) {
1196 return aws_http_client_connect_internal(options, NULL);
1197 } else {
1198 /* Proxy through envrionment variable is enabled */
1199 return aws_http_client_connect_via_proxy(options);
1200 }
1201 }
1202 }
1203
1204 enum aws_http_version aws_http_connection_get_version(const struct aws_http_connection *connection) {
1205 return connection->http_version;
1206 }
1207
1208 int aws_http_connection_configure_server(
1209 struct aws_http_connection *connection,
1210 const struct aws_http_server_connection_options *options) {
1211
1212 if (!connection || !options || !options->on_incoming_request) {
1213 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "id=%p: Invalid server configuration options.", (void *)connection);
1214 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1215 }
1216
1217 if (!connection->server_data) {
1218 AWS_LOGF_WARN(
1219 AWS_LS_HTTP_CONNECTION,
1220 "id=%p: Server-only function invoked on client, ignoring call.",
1221 (void *)connection);
1222 return aws_raise_error(AWS_ERROR_INVALID_STATE);
1223 }
1224 if (connection->server_data->on_incoming_request) {
1225 AWS_LOGF_WARN(
1226 AWS_LS_HTTP_CONNECTION, "id=%p: Connection is already configured, ignoring call.", (void *)connection);
1227 return aws_raise_error(AWS_ERROR_INVALID_STATE);
1228 }
1229
1230 connection->user_data = options->connection_user_data;
1231 connection->server_data->on_incoming_request = options->on_incoming_request;
1232 connection->server_data->on_shutdown = options->on_shutdown;
1233
1234 return AWS_OP_SUCCESS;
1235 }
1236
1237 /* Stream IDs are only 31 bits [5.1.1] */
1238 static const uint32_t MAX_STREAM_ID = UINT32_MAX >> 1;
1239
1240 uint32_t aws_http_connection_get_next_stream_id(struct aws_http_connection *connection) {
1241
1242 uint32_t next_id = connection->next_stream_id;
1243
1244 if (AWS_UNLIKELY(next_id > MAX_STREAM_ID)) {
1245 AWS_LOGF_INFO(AWS_LS_HTTP_CONNECTION, "id=%p: All available stream ids are gone", (void *)connection);
1246
1247 next_id = 0;
1248 aws_raise_error(AWS_ERROR_HTTP_STREAM_IDS_EXHAUSTED);
1249 } else {
1250 connection->next_stream_id += 2;
1251 }
1252 return next_id;
1253 }