| 1 | #ifndef AWS_HTTP_WEBSOCKET_H |
| 2 | #define AWS_HTTP_WEBSOCKET_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | |
| 8 | #include <aws/http/http.h> |
| 9 | |
| 10 | AWS_PUSH_SANE_WARNING_LEVEL |
| 11 | |
| 12 | struct aws_http_header; |
| 13 | struct aws_http_message; |
| 14 | |
| 15 | /* TODO: Document lifetime stuff */ |
| 16 | /* TODO: Document CLOSE frame behavior (when auto-sent during close, when auto-closed) */ |
| 17 | /* TODO: Accept payload as aws_input_stream */ |
| 18 | |
| 19 | /** |
| 20 | * A websocket connection. |
| 21 | */ |
| 22 | struct aws_websocket; |
| 23 | |
| 24 | /** |
| 25 | * Opcode describing the type of a websocket frame. |
| 26 | * RFC-6455 Section 5.2 |
| 27 | */ |
| 28 | enum aws_websocket_opcode { |
| 29 | AWS_WEBSOCKET_OPCODE_CONTINUATION = 0x0, |
| 30 | AWS_WEBSOCKET_OPCODE_TEXT = 0x1, |
| 31 | AWS_WEBSOCKET_OPCODE_BINARY = 0x2, |
| 32 | AWS_WEBSOCKET_OPCODE_CLOSE = 0x8, |
| 33 | AWS_WEBSOCKET_OPCODE_PING = 0x9, |
| 34 | AWS_WEBSOCKET_OPCODE_PONG = 0xA, |
| 35 | }; |
| 36 | |
| 37 | #define AWS_WEBSOCKET_MAX_PAYLOAD_LENGTH 0x7FFFFFFFFFFFFFFF |
| 38 | #define AWS_WEBSOCKET_MAX_HANDSHAKE_KEY_LENGTH 25 |
| 39 | #define AWS_WEBSOCKET_CLOSE_TIMEOUT 1000000000 // nanos -> 1 sec |
| 40 | |
| 41 | /** |
| 42 | * Data passed to the websocket on_connection_setup callback. |
| 43 | * |
| 44 | * An error_code of zero indicates that setup was completely successful. |
| 45 | * You own the websocket pointer now and must call aws_websocket_release() when you are done with it. |
| 46 | * You can inspect the response headers, if you're interested. |
| 47 | * |
| 48 | * A non-zero error_code indicates that setup failed. |
| 49 | * The websocket pointer will be NULL. |
| 50 | * If the server sent a response, you can inspect its status-code, headers, and body, |
| 51 | * but this data will NULL if setup failed before a full response could be received. |
| 52 | * If you wish to persist data from the response make a deep copy. |
| 53 | * The response data becomes invalid once the callback completes. |
| 54 | */ |
| 55 | struct aws_websocket_on_connection_setup_data { |
| 56 | int error_code; |
| 57 | struct aws_websocket *websocket; |
| 58 | const int *handshake_response_status; |
| 59 | const struct aws_http_header *handshake_response_header_array; |
| 60 | size_t num_handshake_response_headers; |
| 61 | const struct aws_byte_cursor *handshake_response_body; |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Called when websocket setup is complete. |
| 66 | * Called exactly once on the websocket's event-loop thread. |
| 67 | * See `aws_websocket_on_connection_setup_data`. |
| 68 | */ |
| 69 | typedef void( |
| 70 | aws_websocket_on_connection_setup_fn)(const struct aws_websocket_on_connection_setup_data *setup, void *user_data); |
| 71 | |
| 72 | /** |
| 73 | * Called when the websocket has finished shutting down. |
| 74 | * Called once on the websocket's event-loop thread if setup succeeded. |
| 75 | * If setup failed, this is never called. |
| 76 | */ |
| 77 | typedef void(aws_websocket_on_connection_shutdown_fn)(struct aws_websocket *websocket, int error_code, void *user_data); |
| 78 | |
| 79 | /** |
| 80 | * Data about an incoming frame. |
| 81 | * See RFC-6455 Section 5.2. |
| 82 | */ |
| 83 | struct aws_websocket_incoming_frame { |
| 84 | uint64_t payload_length; |
| 85 | uint8_t opcode; |
| 86 | bool fin; |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * Called when a new frame arrives. |
| 91 | * Invoked once per frame on the websocket's event-loop thread. |
| 92 | * Each incoming-frame-begin call will eventually be followed by an incoming-frame-complete call, |
| 93 | * before the next frame begins and before the websocket shuts down. |
| 94 | * |
| 95 | * Return true to proceed normally. If false is returned, the websocket will read no further data, |
| 96 | * the frame will complete with an error-code, and the connection will close. |
| 97 | */ |
| 98 | typedef bool(aws_websocket_on_incoming_frame_begin_fn)( |
| 99 | struct aws_websocket *websocket, |
| 100 | const struct aws_websocket_incoming_frame *frame, |
| 101 | void *user_data); |
| 102 | |
| 103 | /** |
| 104 | * Called repeatedly as payload data arrives. |
| 105 | * Invoked 0 or more times on the websocket's event-loop thread. |
| 106 | * Payload data will not be valid after this call, so copy if necessary. |
| 107 | * The payload data is always unmasked at this point. |
| 108 | * |
| 109 | * NOTE: If you created the websocket with `manual_window_management` set true, you must maintain the read window. |
| 110 | * Whenever the read window reaches 0, you will stop receiving anything. |
| 111 | * The websocket's `initial_window_size` determines the starting size of the read window. |
| 112 | * The read window shrinks as you receive the payload from "data" frames (TEXT, BINARY, and CONTINUATION). |
| 113 | * Use aws_websocket_increment_read_window() to increment the window again and keep frames flowing. |
| 114 | * Maintain a larger window to keep up high throughput. |
| 115 | * You only need to worry about the payload from "data" frames. |
| 116 | * The websocket automatically increments the window to account for any |
| 117 | * other incoming bytes, including other parts of a frame (opcode, payload-length, etc) |
| 118 | * and the payload of other frame types (PING, PONG, CLOSE). |
| 119 | * |
| 120 | * Return true to proceed normally. If false is returned, the websocket will read no further data, |
| 121 | * the frame will complete with an error-code, and the connection will close. |
| 122 | */ |
| 123 | typedef bool(aws_websocket_on_incoming_frame_payload_fn)( |
| 124 | struct aws_websocket *websocket, |
| 125 | const struct aws_websocket_incoming_frame *frame, |
| 126 | struct aws_byte_cursor data, |
| 127 | void *user_data); |
| 128 | |
| 129 | /** |
| 130 | * Called when done processing an incoming frame. |
| 131 | * If error_code is non-zero, an error occurred and the payload may not have been completely received. |
| 132 | * Invoked once per frame on the websocket's event-loop thread. |
| 133 | * |
| 134 | * Return true to proceed normally. If false is returned, the websocket will read no further data |
| 135 | * and the connection will close. |
| 136 | */ |
| 137 | typedef bool(aws_websocket_on_incoming_frame_complete_fn)( |
| 138 | struct aws_websocket *websocket, |
| 139 | const struct aws_websocket_incoming_frame *frame, |
| 140 | int error_code, |
| 141 | void *user_data); |
| 142 | |
| 143 | /** |
| 144 | * Options for creating a websocket client connection. |
| 145 | */ |
| 146 | struct aws_websocket_client_connection_options { |
| 147 | /** |
| 148 | * Required. |
| 149 | * Must outlive the connection. |
| 150 | */ |
| 151 | struct aws_allocator *allocator; |
| 152 | |
| 153 | /** |
| 154 | * Required. |
| 155 | * The connection keeps the bootstrap alive via ref-counting. |
| 156 | */ |
| 157 | struct aws_client_bootstrap *bootstrap; |
| 158 | |
| 159 | /** |
| 160 | * Required. |
| 161 | * aws_websocket_client_connect() makes a copy. |
| 162 | */ |
| 163 | const struct aws_socket_options *socket_options; |
| 164 | |
| 165 | /** |
| 166 | * Optional. |
| 167 | * aws_websocket_client_connect() deep-copies all contents, |
| 168 | * and keeps the `aws_tls_ctx` alive via ref-counting. |
| 169 | */ |
| 170 | const struct aws_tls_connection_options *tls_options; |
| 171 | |
| 172 | /** |
| 173 | * Optional |
| 174 | * Configuration options related to http proxy usage. |
| 175 | */ |
| 176 | const struct aws_http_proxy_options *proxy_options; |
| 177 | |
| 178 | /** |
| 179 | * Required. |
| 180 | * aws_websocket_client_connect() makes a copy. |
| 181 | */ |
| 182 | struct aws_byte_cursor host; |
| 183 | |
| 184 | /** |
| 185 | * Optional. |
| 186 | * Defaults to 443 if tls_options is present, 80 if it is not. |
| 187 | */ |
| 188 | uint32_t port; |
| 189 | |
| 190 | /** |
| 191 | * Required. |
| 192 | * The request will be kept alive via ref-counting until the handshake completes. |
| 193 | * Suggestion: create via aws_http_message_new_websocket_handshake_request() |
| 194 | * |
| 195 | * The method MUST be set to GET. |
| 196 | * The following headers are required (replace values in []): |
| 197 | * |
| 198 | * Host: [server.example.com] |
| 199 | * Upgrade: websocket |
| 200 | * Connection: Upgrade |
| 201 | * Sec-WebSocket-Key: [dGhlIHNhbXBsZSBub25jZQ==] |
| 202 | * Sec-WebSocket-Version: 13 |
| 203 | * |
| 204 | * Sec-Websocket-Key should be a random 16 bytes value, Base64 encoded. |
| 205 | */ |
| 206 | struct aws_http_message *handshake_request; |
| 207 | |
| 208 | /** |
| 209 | * Initial size of the websocket's read window. |
| 210 | * Ignored unless `manual_window_management` is true. |
| 211 | * Set to 0 to prevent any incoming websocket frames until aws_websocket_increment_read_window() is called. |
| 212 | */ |
| 213 | size_t initial_window_size; |
| 214 | |
| 215 | /** |
| 216 | * User data for callbacks. |
| 217 | * Optional. |
| 218 | */ |
| 219 | void *user_data; |
| 220 | |
| 221 | /** |
| 222 | * Called when connect completes. |
| 223 | * Required. |
| 224 | * If unsuccessful, error_code will be set, connection will be NULL, |
| 225 | * and the on_connection_shutdown callback will never be called. |
| 226 | * If successful, the user is now responsible for the websocket and must |
| 227 | * call aws_websocket_release() when they are done with it. |
| 228 | */ |
| 229 | aws_websocket_on_connection_setup_fn *on_connection_setup; |
| 230 | |
| 231 | /** |
| 232 | * Called when connection has finished shutting down. |
| 233 | * Optional. |
| 234 | * Never called if `on_connection_setup` reported failure. |
| 235 | * Note that the connection is not completely done until `on_connection_shutdown` has been called |
| 236 | * AND aws_websocket_release() has been called. |
| 237 | */ |
| 238 | aws_websocket_on_connection_shutdown_fn *on_connection_shutdown; |
| 239 | |
| 240 | /** |
| 241 | * Called when each new frame arrives. |
| 242 | * Optional. |
| 243 | * See `aws_websocket_on_incoming_frame_begin_fn`. |
| 244 | */ |
| 245 | aws_websocket_on_incoming_frame_begin_fn *on_incoming_frame_begin; |
| 246 | |
| 247 | /** |
| 248 | * Called repeatedly as payload data arrives. |
| 249 | * Optional. |
| 250 | * See `aws_websocket_on_incoming_frame_payload_fn`. |
| 251 | */ |
| 252 | aws_websocket_on_incoming_frame_payload_fn *on_incoming_frame_payload; |
| 253 | |
| 254 | /** |
| 255 | * Called when done processing an incoming frame. |
| 256 | * Optional. |
| 257 | * See `aws_websocket_on_incoming_frame_complete_fn`. |
| 258 | */ |
| 259 | aws_websocket_on_incoming_frame_complete_fn *on_incoming_frame_complete; |
| 260 | |
| 261 | /** |
| 262 | * Set to true to manually manage the read window size. |
| 263 | * |
| 264 | * If this is false, no backpressure is applied and frames will arrive as fast as possible. |
| 265 | * |
| 266 | * If this is true, then whenever the read window reaches 0 you will stop receiving anything. |
| 267 | * The websocket's `initial_window_size` determines the starting size of the read window. |
| 268 | * The read window shrinks as you receive the payload from "data" frames (TEXT, BINARY, and CONTINUATION). |
| 269 | * Use aws_websocket_increment_read_window() to increment the window again and keep frames flowing. |
| 270 | * Maintain a larger window to keep up high throughput. |
| 271 | * You only need to worry about the payload from "data" frames. |
| 272 | * The websocket automatically increments the window to account for any |
| 273 | * other incoming bytes, including other parts of a frame (opcode, payload-length, etc) |
| 274 | * and the payload of other frame types (PING, PONG, CLOSE). |
| 275 | */ |
| 276 | bool manual_window_management; |
| 277 | |
| 278 | /** |
| 279 | * Optional |
| 280 | * If set, requests that a specific event loop be used to seat the connection, rather than the next one |
| 281 | * in the event loop group. Useful for serializing all io and external events related to a client onto |
| 282 | * a single thread. |
| 283 | */ |
| 284 | struct aws_event_loop *requested_event_loop; |
| 285 | |
| 286 | /** |
| 287 | * Optional |
| 288 | * Host resolution override that allows the user to override DNS behavior for this particular connection. |
| 289 | */ |
| 290 | const struct aws_host_resolution_config *host_resolution_config; |
| 291 | }; |
| 292 | |
| 293 | /** |
| 294 | * Called repeatedly as the websocket's payload is streamed out. |
| 295 | * The user should write payload data to out_buf, up to available capacity. |
| 296 | * The websocket will mask this data for you, if necessary. |
| 297 | * Invoked repeatedly on the websocket's event-loop thread. |
| 298 | * |
| 299 | * Return true to proceed normally. If false is returned, the websocket will send no further data, |
| 300 | * the frame will complete with an error-code, and the connection will close. |
| 301 | */ |
| 302 | typedef bool(aws_websocket_stream_outgoing_payload_fn)( |
| 303 | struct aws_websocket *websocket, |
| 304 | struct aws_byte_buf *out_buf, |
| 305 | void *user_data); |
| 306 | |
| 307 | /** |
| 308 | * Called when a aws_websocket_send_frame() operation completes. |
| 309 | * error_code will be zero if the operation was successful. |
| 310 | * "Success" does not guarantee that the peer actually received or processed the frame. |
| 311 | * Invoked exactly once per sent frame on the websocket's event-loop thread. |
| 312 | */ |
| 313 | typedef void( |
| 314 | aws_websocket_outgoing_frame_complete_fn)(struct aws_websocket *websocket, int error_code, void *user_data); |
| 315 | |
| 316 | /** |
| 317 | * Options for sending a websocket frame. |
| 318 | * This structure is copied immediately by aws_websocket_send(). |
| 319 | * For descriptions of opcode, fin, and payload_length see in RFC-6455 Section 5.2. |
| 320 | */ |
| 321 | struct aws_websocket_send_frame_options { |
| 322 | /** |
| 323 | * Size of payload to be sent via `stream_outgoing_payload` callback. |
| 324 | */ |
| 325 | uint64_t payload_length; |
| 326 | |
| 327 | /** |
| 328 | * User data passed to callbacks. |
| 329 | */ |
| 330 | void *user_data; |
| 331 | |
| 332 | /** |
| 333 | * Callback for sending payload data. |
| 334 | * See `aws_websocket_stream_outgoing_payload_fn`. |
| 335 | * Required if `payload_length` is non-zero. |
| 336 | */ |
| 337 | aws_websocket_stream_outgoing_payload_fn *stream_outgoing_payload; |
| 338 | |
| 339 | /** |
| 340 | * Callback for completion of send operation. |
| 341 | * See `aws_websocket_outgoing_frame_complete_fn`. |
| 342 | * Optional. |
| 343 | */ |
| 344 | aws_websocket_outgoing_frame_complete_fn *on_complete; |
| 345 | |
| 346 | /** |
| 347 | * Frame type. |
| 348 | * `aws_websocket_opcode` enum provides standard values. |
| 349 | */ |
| 350 | uint8_t opcode; |
| 351 | |
| 352 | /** |
| 353 | * Indicates that this is the final fragment in a message. The first fragment MAY also be the final fragment. |
| 354 | */ |
| 355 | bool fin; |
| 356 | }; |
| 357 | |
| 358 | AWS_EXTERN_C_BEGIN |
| 359 | |
| 360 | /** |
| 361 | * Return true if opcode is for a data frame, false if opcode if for a control frame. |
| 362 | */ |
| 363 | AWS_HTTP_API |
| 364 | bool aws_websocket_is_data_frame(uint8_t opcode); |
| 365 | |
| 366 | /** |
| 367 | * Asynchronously establish a client websocket connection. |
| 368 | * The on_connection_setup callback is invoked when the operation has finished creating a connection, or failed. |
| 369 | */ |
| 370 | AWS_HTTP_API |
| 371 | int aws_websocket_client_connect(const struct aws_websocket_client_connection_options *options); |
| 372 | |
| 373 | /** |
| 374 | * Increment the websocket's ref-count, preventing it from being destroyed. |
| 375 | * @return Always returns the same pointer that is passed in. |
| 376 | */ |
| 377 | AWS_HTTP_API |
| 378 | struct aws_websocket *aws_websocket_acquire(struct aws_websocket *websocket); |
| 379 | |
| 380 | /** |
| 381 | * Decrement the websocket's ref-count. |
| 382 | * When the ref-count reaches zero, the connection will shut down, if it hasn't already. |
| 383 | * Users must release the websocket when they are done with it. |
| 384 | * The websocket's memory cannot be reclaimed until this is done. |
| 385 | * Callbacks may continue firing after this is called, with "shutdown" being the final callback. |
| 386 | * This function may be called from any thread. |
| 387 | * |
| 388 | * It is safe to pass NULL, nothing will happen. |
| 389 | */ |
| 390 | AWS_HTTP_API |
| 391 | void aws_websocket_release(struct aws_websocket *websocket); |
| 392 | |
| 393 | /** |
| 394 | * Close the websocket connection. |
| 395 | * It is safe to call this, even if the connection is already closed or closing. |
| 396 | * The websocket will attempt to send a CLOSE frame during normal shutdown. |
| 397 | * If `free_scarce_resources_immediately` is true, the connection will be torn down as quickly as possible. |
| 398 | * This function may be called from any thread. |
| 399 | */ |
| 400 | AWS_HTTP_API |
| 401 | void aws_websocket_close(struct aws_websocket *websocket, bool free_scarce_resources_immediately); |
| 402 | |
| 403 | /** |
| 404 | * Send a websocket frame. |
| 405 | * The `options` struct is copied. |
| 406 | * A callback will be invoked when the operation completes. |
| 407 | * This function may be called from any thread. |
| 408 | */ |
| 409 | AWS_HTTP_API |
| 410 | int aws_websocket_send_frame(struct aws_websocket *websocket, const struct aws_websocket_send_frame_options *options); |
| 411 | |
| 412 | /** |
| 413 | * Manually increment the read window to keep frames flowing. |
| 414 | * |
| 415 | * If the websocket was created with `manual_window_management` set true, |
| 416 | * then whenever the read window reaches 0 you will stop receiving data. |
| 417 | * The websocket's `initial_window_size` determines the starting size of the read window. |
| 418 | * The read window shrinks as you receive the payload from "data" frames (TEXT, BINARY, and CONTINUATION). |
| 419 | * Use aws_websocket_increment_read_window() to increment the window again and keep frames flowing. |
| 420 | * Maintain a larger window to keep up high throughput. |
| 421 | * You only need to worry about the payload from "data" frames. |
| 422 | * The websocket automatically increments the window to account for any |
| 423 | * other incoming bytes, including other parts of a frame (opcode, payload-length, etc) |
| 424 | * and the payload of other frame types (PING, PONG, CLOSE). |
| 425 | * |
| 426 | * If the websocket was created with `manual_window_management` set false, this function does nothing. |
| 427 | * |
| 428 | * This function may be called from any thread. |
| 429 | */ |
| 430 | AWS_HTTP_API |
| 431 | void aws_websocket_increment_read_window(struct aws_websocket *websocket, size_t size); |
| 432 | |
| 433 | /** |
| 434 | * Convert the websocket into a mid-channel handler. |
| 435 | * The websocket will stop being usable via its public API and become just another handler in the channel. |
| 436 | * The caller will likely install a channel handler to the right. |
| 437 | * This must not be called in the middle of an incoming frame (between "frame begin" and "frame complete" callbacks). |
| 438 | * This MUST be called from the websocket's thread. |
| 439 | * |
| 440 | * If successful: |
| 441 | * - Other than aws_websocket_release(), all calls to aws_websocket_x() functions are ignored. |
| 442 | * - The websocket will no longer invoke any "incoming frame" callbacks. |
| 443 | * - aws_io_messages written by a downstream handler will be wrapped in binary data frames and sent upstream. |
| 444 | * The data may be split/combined as it is sent along. |
| 445 | * - aws_io_messages read from upstream handlers will be scanned for binary data frames. |
| 446 | * The payloads of these frames will be sent downstream. |
| 447 | * The payloads may be split/combined as they are sent along. |
| 448 | * - An incoming close frame will automatically result in channel-shutdown. |
| 449 | * - aws_websocket_release() must still be called or the websocket and its channel will never be cleaned up. |
| 450 | * - The websocket will still invoke its "on connection shutdown" callback when channel shutdown completes. |
| 451 | * |
| 452 | * If unsuccessful, NULL is returned and the websocket is unchanged. |
| 453 | */ |
| 454 | AWS_HTTP_API |
| 455 | int aws_websocket_convert_to_midchannel_handler(struct aws_websocket *websocket); |
| 456 | |
| 457 | /** |
| 458 | * Returns the websocket's underlying I/O channel. |
| 459 | */ |
| 460 | AWS_HTTP_API |
| 461 | struct aws_channel *aws_websocket_get_channel(const struct aws_websocket *websocket); |
| 462 | |
| 463 | /** |
| 464 | * Generate value for a Sec-WebSocket-Key header and write it into `dst` buffer. |
| 465 | * The buffer should have at least AWS_WEBSOCKET_MAX_HANDSHAKE_KEY_LENGTH space available. |
| 466 | * |
| 467 | * This value is the base64 encoding of a random 16-byte value. |
| 468 | * RFC-6455 Section 4.1 |
| 469 | */ |
| 470 | AWS_HTTP_API |
| 471 | int aws_websocket_random_handshake_key(struct aws_byte_buf *dst); |
| 472 | |
| 473 | /** |
| 474 | * Create request with all required fields for a websocket upgrade request. |
| 475 | * The method and path are set, and the the following headers are added: |
| 476 | * |
| 477 | * Host: <host> |
| 478 | * Upgrade: websocket |
| 479 | * Connection: Upgrade |
| 480 | * Sec-WebSocket-Key: <base64 encoding of 16 random bytes> |
| 481 | * Sec-WebSocket-Version: 13 |
| 482 | */ |
| 483 | AWS_HTTP_API |
| 484 | struct aws_http_message *aws_http_message_new_websocket_handshake_request( |
| 485 | struct aws_allocator *allocator, |
| 486 | struct aws_byte_cursor path, |
| 487 | struct aws_byte_cursor host); |
| 488 | |
| 489 | AWS_EXTERN_C_END |
| 490 | AWS_POP_SANE_WARNING_LEVEL |
| 491 | |
| 492 | #endif /* AWS_HTTP_WEBSOCKET_H */ |