Debian Codegraph @

crt/aws-c-http/include/aws/http/private/h1_connection.h

raw symbols SCIP
1 #ifndef AWS_HTTP_H1_CONNECTION_H
2 #define AWS_HTTP_H1_CONNECTION_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/common/mutex.h>
10 #include <aws/http/private/connection_impl.h>
11 #include <aws/http/private/h1_encoder.h>
12 #include <aws/http/statistics.h>
13
14 #ifdef _MSC_VER
15 # pragma warning(disable : 4214) /* nonstandard extension used: bit field types other than int */
16 #endif
17
18 enum aws_h1_connection_read_state {
19 AWS_CONNECTION_READ_OPEN,
20 AWS_CONNECTION_READ_SHUTTING_DOWN,
21 AWS_CONNECTION_READ_SHUT_DOWN_COMPLETE,
22 };
23
24 struct aws_h1_connection {
25 struct aws_http_connection base;
26
27 size_t initial_stream_window_size;
28
29 /* Task responsible for sending data.
30 * As long as there is data available to send, the task will be "active" and repeatedly:
31 * 1) Encode outgoing stream data to an aws_io_message and send it up the channel.
32 * 2) Wait until the aws_io_message's write_complete callback fires.
33 * 3) Reschedule the task to run again.
34 *
35 * `thread_data.is_outgoing_stream_task_active` tells whether the task is "active".
36 *
37 * If there is no data available to write (waiting for user to add more streams or chunks),
38 * then the task stops being active. The task is made active again when the user
39 * adds more outgoing data. */
40 struct aws_channel_task outgoing_stream_task;
41
42 /* Task that removes items from `synced_data` and does their on-thread work.
43 * Runs once and wait until it's scheduled again.
44 * Any function that wants to schedule this task MUST:
45 * - acquire the synced_data.lock
46 * - check whether `synced_data.is_cross_thread_work_scheduled` was true or false.
47 * - set `synced_data.is_cross_thread_work_scheduled = true`
48 * - release synced_data.lock
49 * - ONLY IF `synced_data.is_cross_thread_work_scheduled` CHANGED from false to true:
50 * - then schedule the task
51 */
52 struct aws_channel_task cross_thread_work_task;
53
54 /* Only the event-loop thread may touch this data */
55 struct {
56 /* List of streams being worked on. */
57 struct aws_linked_list stream_list;
58
59 /* Points to the stream whose data is currently being sent.
60 * This stream is ALWAYS in the `stream_list`.
61 * HTTP pipelining is supported, so once the stream is completely written
62 * we'll start working on the next stream in the list */
63 struct aws_h1_stream *outgoing_stream;
64
65 /* Points to the stream being decoded.
66 * This stream is ALWAYS in the `stream_list`. */
67 struct aws_h1_stream *incoming_stream;
68 struct aws_h1_decoder *incoming_stream_decoder;
69
70 /* Used to encode requests and responses */
71 struct aws_h1_encoder encoder;
72
73 /**
74 * All aws_io_messages arriving in the read direction are queued here before processing.
75 * This allows the connection to receive more data than the the current HTTP-stream might allow,
76 * and process the data later when HTTP-stream's window opens or the next stream begins.
77 *
78 * The `aws_io_message.copy_mark` is used to track progress on partially processed messages.
79 * `pending_bytes` is the sum of all unprocessed bytes across all queued messages.
80 * `capacity` is the limit for how many unprocessed bytes we'd like in the queue.
81 */
82 struct {
83 struct aws_linked_list messages;
84 size_t pending_bytes;
85 size_t capacity;
86 } read_buffer;
87
88 /**
89 * The connection's current window size.
90 * We use this variable, instead of the existing `aws_channel_slot.window_size`,
91 * because that variable is not updated immediately, the channel uses a task to update it.
92 * Since we use the difference between current and desired window size when deciding
93 * how much to increment, we need the most up-to-date values possible.
94 */
95 size_t connection_window;
96
97 /* Only used by tests. Sum of window_increments issued by this slot. Resets each time it's queried */
98 size_t recent_window_increments;
99
100 struct aws_crt_statistics_http1_channel stats;
101
102 uint64_t outgoing_stream_timestamp_ns;
103 uint64_t incoming_stream_timestamp_ns;
104
105 int pending_shutdown_error_code;
106 enum aws_h1_connection_read_state read_state;
107
108 /* True when read and/or writing has stopped, whether due to errors or normal channel shutdown. */
109 bool is_writing_stopped : 1;
110
111 /* If true, the connection has upgraded to another protocol.
112 * It will pass data to adjacent channel handlers without altering it.
113 * The connection can no longer service request/response streams. */
114 bool has_switched_protocols : 1;
115
116 /* Server-only. Request-handler streams can only be created while this is true. */
117 bool can_create_request_handler_stream : 1;
118
119 /* see `outgoing_stream_task` */
120 bool is_outgoing_stream_task_active : 1;
121
122 bool is_processing_read_messages : 1;
123 } thread_data;
124
125 /* Any thread may touch this data, but the lock must be held */
126 struct {
127 struct aws_mutex lock;
128
129 /* New client streams that have not been moved to `stream_list` yet.
130 * This list is not used on servers. */
131 struct aws_linked_list new_client_stream_list;
132
133 /* If non-zero, then window_update_task is scheduled */
134 size_t window_update_size;
135
136 /* If non-zero, reason to immediately reject new streams. (ex: closing) */
137 int new_stream_error_code;
138
139 /* If true, user has called connection_close() or stream_cancel(),
140 * but the cross_thread_work_task hasn't processed it yet */
141 bool shutdown_requested;
142 int shutdown_requested_error_code;
143
144 /* See `cross_thread_work_task` */
145 bool is_cross_thread_work_task_scheduled : 1;
146
147 /* For checking status from outside the event-loop thread. */
148 bool is_open : 1;
149 } synced_data;
150 };
151
152 /* Allow tests to check current window stats */
153 struct aws_h1_window_stats {
154 size_t connection_window;
155 size_t recent_window_increments; /* Resets to 0 each time window stats are queried*/
156 size_t buffer_capacity;
157 size_t buffer_pending_bytes;
158 uint64_t stream_window;
159 bool has_incoming_stream;
160 };
161
162 AWS_EXTERN_C_BEGIN
163
164 /* The functions below are exported so they can be accessed from tests. */
165
166 AWS_HTTP_API
167 struct aws_http_connection *aws_http_connection_new_http1_1_server(
168 struct aws_allocator *allocator,
169 bool manual_window_management,
170 size_t initial_window_size,
171 const struct aws_http1_connection_options *http1_options);
172
173 AWS_HTTP_API
174 struct aws_http_connection *aws_http_connection_new_http1_1_client(
175 struct aws_allocator *allocator,
176 bool manual_window_management,
177 size_t initial_window_size,
178 const struct aws_http1_connection_options *http1_options);
179
180 /* Allow tests to check current window stats */
181 AWS_HTTP_API
182 struct aws_h1_window_stats aws_h1_connection_window_stats(struct aws_http_connection *connection_base);
183
184 AWS_EXTERN_C_END
185
186 /* DO NOT export functions below. They're only used by other .c files in this library */
187
188 /* TODO: introduce naming conventions for private header functions */
189
190 void aws_h1_connection_lock_synced_data(struct aws_h1_connection *connection);
191 void aws_h1_connection_unlock_synced_data(struct aws_h1_connection *connection);
192
193 /**
194 * Try to kick off the outgoing-stream-task.
195 * If task is already active, nothing happens.
196 * If there's nothing to do, the task will immediately stop itself.
197 * Call this whenever the user provides new outgoing data (ex: new stream, new chunk).
198 * MUST be called from the connection's event-loop thread.
199 */
200 void aws_h1_connection_try_write_outgoing_stream(struct aws_h1_connection *connection);
201
202 /**
203 * If any read messages are queued, and the downstream window is non-zero,
204 * process data and send it downstream. Then calculate the connection's
205 * desired window size and increment it if necessary.
206 *
207 * During normal operations "downstream" means the current incoming stream.
208 * If the connection has switched protocols "downstream" means the next
209 * channel handler in the read direction.
210 */
211 void aws_h1_connection_try_process_read_messages(struct aws_h1_connection *connection);
212
213 #endif /* AWS_HTTP_H1_CONNECTION_H */