| 1 | #ifndef AWS_HTTP_SERVER_H |
| 2 | #define AWS_HTTP_SERVER_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/http/http.h> |
| 10 | |
| 11 | AWS_PUSH_SANE_WARNING_LEVEL |
| 12 | |
| 13 | struct aws_http_connection; |
| 14 | struct aws_server_bootstrap; |
| 15 | struct aws_socket_options; |
| 16 | struct aws_tls_connection_options; |
| 17 | /** |
| 18 | * A listening socket which accepts incoming HTTP connections, |
| 19 | * creating a server-side aws_http_connection to handle each one. |
| 20 | */ |
| 21 | struct aws_http_server; |
| 22 | struct aws_http_stream; |
| 23 | |
| 24 | typedef void(aws_http_server_on_incoming_connection_fn)( |
| 25 | struct aws_http_server *server, |
| 26 | struct aws_http_connection *connection, |
| 27 | int error_code, |
| 28 | void *user_data); |
| 29 | |
| 30 | typedef void(aws_http_server_on_destroy_fn)(void *user_data); |
| 31 | |
| 32 | /** |
| 33 | * Options for creating an HTTP server. |
| 34 | * Initialize with AWS_HTTP_SERVER_OPTIONS_INIT to set default values. |
| 35 | */ |
| 36 | struct aws_http_server_options { |
| 37 | /** |
| 38 | * The sizeof() this struct, used for versioning. |
| 39 | * Set by AWS_HTTP_SERVER_OPTIONS_INIT. |
| 40 | */ |
| 41 | size_t self_size; |
| 42 | |
| 43 | /** |
| 44 | * Required. |
| 45 | * Must outlive server. |
| 46 | */ |
| 47 | struct aws_allocator *allocator; |
| 48 | |
| 49 | /** |
| 50 | * Required. |
| 51 | * Must outlive server. |
| 52 | */ |
| 53 | struct aws_server_bootstrap *bootstrap; |
| 54 | |
| 55 | /** |
| 56 | * Required. |
| 57 | * Server makes copy. |
| 58 | */ |
| 59 | struct aws_socket_endpoint *endpoint; |
| 60 | |
| 61 | /** |
| 62 | * Required. |
| 63 | * Server makes a copy. |
| 64 | */ |
| 65 | struct aws_socket_options *socket_options; |
| 66 | |
| 67 | /** |
| 68 | * Optional. |
| 69 | * Server copies all contents except the `aws_tls_ctx`, which must outlive the server. |
| 70 | */ |
| 71 | struct aws_tls_connection_options *tls_options; |
| 72 | |
| 73 | /** |
| 74 | * Initial window size for incoming connections. |
| 75 | * Optional. |
| 76 | * A default size is set by AWS_HTTP_SERVER_OPTIONS_INIT. |
| 77 | */ |
| 78 | size_t initial_window_size; |
| 79 | |
| 80 | /** |
| 81 | * User data passed to callbacks. |
| 82 | * Optional. |
| 83 | */ |
| 84 | void *server_user_data; |
| 85 | |
| 86 | /** |
| 87 | * Invoked when an incoming connection has been set up, or when setup has failed. |
| 88 | * Required. |
| 89 | * If setup succeeds, the user must call aws_http_connection_configure_server(). |
| 90 | */ |
| 91 | aws_http_server_on_incoming_connection_fn *on_incoming_connection; |
| 92 | |
| 93 | /** |
| 94 | * Invoked when the server finishes the destroy operation. |
| 95 | * Optional. |
| 96 | */ |
| 97 | aws_http_server_on_destroy_fn *on_destroy_complete; |
| 98 | |
| 99 | /** |
| 100 | * Set to true to manually manage the read window size. |
| 101 | * |
| 102 | * If this is false, the connection will maintain a constant window size. |
| 103 | * |
| 104 | * If this is true, the caller must manually increment the window size using aws_http_stream_update_window(). |
| 105 | * If the window is not incremented, it will shrink by the amount of body data received. If the window size |
| 106 | * reaches 0, no further data will be received. |
| 107 | **/ |
| 108 | bool manual_window_management; |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * Initializes aws_http_server_options with default values. |
| 113 | */ |
| 114 | #define AWS_HTTP_SERVER_OPTIONS_INIT \ |
| 115 | { \ |
| 116 | .self_size = sizeof(struct aws_http_server_options), \ |
| 117 | .initial_window_size = SIZE_MAX, \ |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Invoked at the start of an incoming request. |
| 122 | * To process the request, the user must create a request handler stream and return it to the connection. |
| 123 | * If NULL is returned, the request will not be processed and the last error will be reported as the reason for failure. |
| 124 | */ |
| 125 | typedef struct aws_http_stream *(aws_http_on_incoming_request_fn)(struct aws_http_connection *connection, |
| 126 | void *user_data); |
| 127 | |
| 128 | typedef void(aws_http_on_server_connection_shutdown_fn)( |
| 129 | struct aws_http_connection *connection, |
| 130 | int error_code, |
| 131 | void *connection_user_data); |
| 132 | |
| 133 | /** |
| 134 | * Options for configuring a server-side aws_http_connection. |
| 135 | * Initialized with AWS_HTTP_SERVER_CONNECTION_OPTIONS_INIT to set default values. |
| 136 | */ |
| 137 | struct aws_http_server_connection_options { |
| 138 | /** |
| 139 | * The sizeof() this struct, used for versioning. |
| 140 | * Set by AWS_HTTP_SERVER_CONNECTION_OPTIONS_INIT. |
| 141 | */ |
| 142 | size_t self_size; |
| 143 | |
| 144 | /** |
| 145 | * User data specific to this connection. |
| 146 | * Optional. |
| 147 | */ |
| 148 | void *connection_user_data; |
| 149 | |
| 150 | /** |
| 151 | * Invoked at the start of an incoming request. |
| 152 | * Required. |
| 153 | * The user must create a request handler stream and return it to the connection. |
| 154 | * See `aws_http_on_incoming_request_fn`. |
| 155 | */ |
| 156 | aws_http_on_incoming_request_fn *on_incoming_request; |
| 157 | |
| 158 | /** |
| 159 | * Invoked when the connection is shut down. |
| 160 | * Optional. |
| 161 | */ |
| 162 | aws_http_on_server_connection_shutdown_fn *on_shutdown; |
| 163 | }; |
| 164 | |
| 165 | /** |
| 166 | * Initializes aws_http_server_connection_options with default values. |
| 167 | */ |
| 168 | #define AWS_HTTP_SERVER_CONNECTION_OPTIONS_INIT \ |
| 169 | { \ |
| 170 | .self_size = sizeof(struct aws_http_server_connection_options), \ |
| 171 | } |
| 172 | |
| 173 | AWS_EXTERN_C_BEGIN |
| 174 | |
| 175 | /** |
| 176 | * Create server, a listening socket that accepts incoming connections. |
| 177 | */ |
| 178 | AWS_HTTP_API |
| 179 | struct aws_http_server *aws_http_server_new(const struct aws_http_server_options *options); |
| 180 | |
| 181 | /** |
| 182 | * Release the server. It will close the listening socket and all the connections existing in the server. |
| 183 | * The on_destroy_complete will be invoked when the destroy operation completes |
| 184 | */ |
| 185 | AWS_HTTP_API |
| 186 | void aws_http_server_release(struct aws_http_server *server); |
| 187 | |
| 188 | /** |
| 189 | * Configure a server connection. |
| 190 | * This must be called from the server's on_incoming_connection callback. |
| 191 | */ |
| 192 | AWS_HTTP_API |
| 193 | int aws_http_connection_configure_server( |
| 194 | struct aws_http_connection *connection, |
| 195 | const struct aws_http_server_connection_options *options); |
| 196 | |
| 197 | /** |
| 198 | * Returns true if this is a server connection. |
| 199 | */ |
| 200 | AWS_HTTP_API |
| 201 | bool aws_http_connection_is_server(const struct aws_http_connection *connection); |
| 202 | |
| 203 | /** |
| 204 | * Returns the local listener endpoint of the HTTP server. Only valid as long as the server remains valid. |
| 205 | */ |
| 206 | AWS_HTTP_API |
| 207 | const struct aws_socket_endpoint *aws_http_server_get_listener_endpoint(const struct aws_http_server *server); |
| 208 | |
| 209 | AWS_EXTERN_C_END |
| 210 | AWS_POP_SANE_WARNING_LEVEL |
| 211 | |
| 212 | #endif /* AWS_HTTP_SERVER_H */ |