| 1 | #ifndef AWS_HTTP_H1_STREAM_H |
| 2 | #define AWS_HTTP_H1_STREAM_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/private/h1_encoder.h> |
| 9 | #include <aws/http/private/http_impl.h> |
| 10 | #include <aws/http/private/request_response_impl.h> |
| 11 | #include <aws/io/channel.h> |
| 12 | |
| 13 | #ifdef _MSC_VER |
| 14 | # pragma warning(disable : 4214) /* nonstandard extension used: bit field types other than int */ |
| 15 | #endif |
| 16 | |
| 17 | /* Simple view of stream's state. |
| 18 | * Used to determine whether it's safe for a user to call functions that alter state. */ |
| 19 | enum aws_h1_stream_api_state { |
| 20 | AWS_H1_STREAM_API_STATE_INIT, |
| 21 | AWS_H1_STREAM_API_STATE_ACTIVE, |
| 22 | AWS_H1_STREAM_API_STATE_COMPLETE, |
| 23 | }; |
| 24 | |
| 25 | struct aws_h1_data_write { |
| 26 | struct aws_allocator *allocator; |
| 27 | struct aws_input_stream *data; |
| 28 | aws_http_stream_write_complete_fn *on_complete; |
| 29 | void *user_data; |
| 30 | struct aws_linked_list_node node; |
| 31 | bool is_end_stream; |
| 32 | }; |
| 33 | |
| 34 | struct aws_h1_stream { |
| 35 | struct aws_http_stream base; |
| 36 | |
| 37 | struct aws_linked_list_node node; |
| 38 | |
| 39 | /* Task that removes items from `synced_data` and does their on-thread work. |
| 40 | * Runs once and wait until it's scheduled again. |
| 41 | * Any function that wants to schedule this task MUST: |
| 42 | * - acquire the synced_data.lock |
| 43 | * - check whether `synced_data.is_cross_thread_work_scheduled` was true or false. |
| 44 | * - set `synced_data.is_cross_thread_work_scheduled = true` |
| 45 | * - release synced_data.lock |
| 46 | * - ONLY IF `synced_data.is_cross_thread_work_scheduled` CHANGED from false to true: |
| 47 | * - increment the stream's refcount, to keep stream alive until task runs |
| 48 | * - schedule the task |
| 49 | */ |
| 50 | struct aws_channel_task cross_thread_work_task; |
| 51 | |
| 52 | /* Whether the stream is using manual data writes instead of input_stream */ |
| 53 | bool using_manual_data_writes : 1; |
| 54 | |
| 55 | struct { |
| 56 | /* Message (derived from outgoing request or response) to be submitted to encoder */ |
| 57 | struct aws_h1_encoder_message encoder_message; |
| 58 | |
| 59 | bool is_outgoing_message_done; |
| 60 | |
| 61 | bool is_incoming_message_done; |
| 62 | bool is_incoming_head_done; |
| 63 | |
| 64 | /* If true, this is the last stream the connection should process. |
| 65 | * See RFC-7230 Section 6: Connection Management. */ |
| 66 | bool is_final_stream; |
| 67 | |
| 68 | /* Buffer for incoming data that needs to stick around. */ |
| 69 | struct aws_byte_buf incoming_storage_buf; |
| 70 | |
| 71 | /* List of `struct aws_h1_chunk`, used for chunked encoding. |
| 72 | * Encoder completes/frees/pops front chunk when it's done sending. */ |
| 73 | struct aws_linked_list pending_chunk_list; |
| 74 | |
| 75 | struct aws_h1_encoder_message message; |
| 76 | |
| 77 | /* Size of stream's flow-control window. |
| 78 | * Only body data (not headers, etc) counts against the stream's flow-control window. */ |
| 79 | uint64_t stream_window; |
| 80 | |
| 81 | /* List of `struct aws_h1_data_write` which have been moved from synced_data for processing */ |
| 82 | struct aws_linked_list pending_data_write_list; |
| 83 | |
| 84 | /* Whether the final data write (with is_end_stream=true) has been received */ |
| 85 | bool has_final_data_write : 1; |
| 86 | |
| 87 | /* Whether a "request handler" stream has a response to send. |
| 88 | * Has mirror variable in synced_data */ |
| 89 | bool has_outgoing_response : 1; |
| 90 | } thread_data; |
| 91 | |
| 92 | /* Any thread may touch this data, but the connection's lock must be held. |
| 93 | * Sharing a lock is fine because it's rare for an HTTP/1 connection |
| 94 | * to have more than one stream at a time. */ |
| 95 | struct { |
| 96 | /* Outgoing response on "request handler" stream which has been submitted by user, |
| 97 | * but hasn't yet moved to thread_data.encoder_message. */ |
| 98 | struct aws_h1_encoder_message pending_outgoing_response; |
| 99 | |
| 100 | /* List of `struct aws_h1_chunk` which have been submitted by user, |
| 101 | * but haven't yet moved to thread_data.encoder_message.pending_chunk_list where the encoder will find them. */ |
| 102 | struct aws_linked_list pending_chunk_list; |
| 103 | |
| 104 | /* List of `struct aws_h1_data_write` which have been submitted by user, |
| 105 | * but haven't yet moved to thread_data.pending_data_write_list where the encoder will find them. */ |
| 106 | struct aws_linked_list pending_data_write_list; |
| 107 | |
| 108 | /* trailing headers which have been submitted by user, |
| 109 | * but haven't yet moved to thread_data.encoder_message where the encoder will find them. */ |
| 110 | struct aws_h1_trailer *pending_trailer; |
| 111 | |
| 112 | enum aws_h1_stream_api_state api_state; |
| 113 | |
| 114 | /* Sum of all aws_http_stream_update_window() calls that haven't yet moved to thread_data.stream_window */ |
| 115 | uint64_t pending_window_update; |
| 116 | |
| 117 | /* See `cross_thread_work_task` */ |
| 118 | bool is_cross_thread_work_task_scheduled : 1; |
| 119 | |
| 120 | /* Whether a "request handler" stream has a response to send. |
| 121 | * Has mirror variable in thread_data */ |
| 122 | bool has_outgoing_response : 1; |
| 123 | |
| 124 | /* Whether the outgoing message is using chunked encoding */ |
| 125 | bool using_chunked_encoding : 1; |
| 126 | |
| 127 | /* Whether the final 0 length chunk has already been sent */ |
| 128 | bool has_final_chunk : 1; |
| 129 | |
| 130 | /* Whether the final data write (with is_end_stream=true) has been received */ |
| 131 | bool has_final_data_write : 1; |
| 132 | |
| 133 | /* Whether the chunked trailer has already been sent */ |
| 134 | bool has_added_trailer : 1; |
| 135 | } synced_data; |
| 136 | }; |
| 137 | |
| 138 | /* DO NOT export functions below. They're only used by other .c files in this library */ |
| 139 | |
| 140 | struct aws_h1_stream *aws_h1_stream_new_request( |
| 141 | struct aws_http_connection *client_connection, |
| 142 | const struct aws_http_make_request_options *options); |
| 143 | |
| 144 | struct aws_h1_stream *aws_h1_stream_new_request_handler(const struct aws_http_request_handler_options *options); |
| 145 | |
| 146 | int aws_h1_stream_activate(struct aws_http_stream *stream); |
| 147 | void aws_h1_stream_cancel(struct aws_http_stream *stream, int error_code); |
| 148 | |
| 149 | int aws_h1_stream_send_response(struct aws_h1_stream *stream, struct aws_http_message *response); |
| 150 | |
| 151 | void aws_h1_data_write_complete_and_destroy( |
| 152 | struct aws_h1_data_write *data_write, |
| 153 | struct aws_http_stream *http_stream, |
| 154 | int error_code); |
| 155 | |
| 156 | #endif /* AWS_HTTP_H1_STREAM_H */ |