Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_HTTP_H2_STREAM_H
2 #define AWS_HTTP_H2_STREAM_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/private/h2_frames.h>
10 #include <aws/http/private/request_response_impl.h>
11
12 #include <aws/common/mutex.h>
13 #include <aws/io/channel.h>
14
15 #include <inttypes.h>
16
17 #define AWS_H2_STREAM_LOGF(level, stream, text, ...) \
18 AWS_LOGF_##level( \
19 AWS_LS_HTTP_STREAM, \
20 "id=%" PRIu32 " connection=%p state=%s: " text, \
21 (stream)->base.id, \
22 (void *)(stream)->base.owning_connection, \
23 aws_h2_stream_state_to_str((stream)->thread_data.state), \
24 __VA_ARGS__)
25 #define AWS_H2_STREAM_LOG(level, stream, text) AWS_H2_STREAM_LOGF(level, (stream), "%s", (text))
26
27 enum aws_h2_stream_state {
28 /* Initial state, before anything sent or received. */
29 AWS_H2_STREAM_STATE_IDLE,
30 /* (server-only) stream-id was reserved via PUSH_PROMISE on another stream,
31 * but HEADERS for this stream have not been sent yet */
32 AWS_H2_STREAM_STATE_RESERVED_LOCAL,
33 /* (client-only) stream-id was reserved via PUSH_PROMISE on another stream,
34 * but HEADERS for this stream have not been received yet */
35 AWS_H2_STREAM_STATE_RESERVED_REMOTE,
36 /* Neither side is done sending their message. */
37 AWS_H2_STREAM_STATE_OPEN,
38 /* This side is done sending message (END_STREAM), but peer is not done. */
39 AWS_H2_STREAM_STATE_HALF_CLOSED_LOCAL,
40 /* Peer is done sending message (END_STREAM), but this side is not done */
41 AWS_H2_STREAM_STATE_HALF_CLOSED_REMOTE,
42 /* Both sides done sending message (END_STREAM),
43 * or either side has sent RST_STREAM */
44 AWS_H2_STREAM_STATE_CLOSED,
45
46 AWS_H2_STREAM_STATE_COUNT,
47 };
48
49 /* simplified stream state for API implementation */
50 enum aws_h2_stream_api_state {
51 AWS_H2_STREAM_API_STATE_INIT,
52 AWS_H2_STREAM_API_STATE_ACTIVE,
53 AWS_H2_STREAM_API_STATE_COMPLETE,
54 };
55
56 /* Indicates the state of the body of the HTTP/2 stream */
57 enum aws_h2_stream_body_state {
58 AWS_H2_STREAM_BODY_STATE_NONE, /* Has no body for the HTTP/2 stream */
59 AWS_H2_STREAM_BODY_STATE_WAITING_WRITES, /* Has no active body, but waiting for more to be
60 write */
61 AWS_H2_STREAM_BODY_STATE_ONGOING, /* Has active ongoing body */
62 };
63
64 /* represents a write operation, which will be turned into a data frame */
65 struct aws_h2_stream_data_write {
66 struct aws_linked_list_node node;
67 struct aws_input_stream *data_stream;
68 aws_http2_stream_write_data_complete_fn *on_complete;
69 void *user_data;
70 bool end_stream;
71 };
72
73 struct aws_h2_stream {
74 struct aws_http_stream base;
75
76 struct aws_linked_list_node node;
77 struct aws_channel_task cross_thread_work_task;
78 /* The threshold to send out a window update frame.
79 * When the window_size_self is less than the threshold, client will starts the sending of WINDOW_UPDATE frame
80 * to keep flow continues.
81 */
82 int32_t window_size_threshold_to_send_update;
83 aws_http2_on_remote_end_stream_fn *on_h2_remote_end_stream;
84
85 /* Only the event-loop thread may touch this data */
86 struct {
87 enum aws_h2_stream_state state;
88 /* The remote window size.
89 * RFC-9113 6.9.2: The settings can shrink the exiting flow-control windows to negative values.
90 **/
91 int32_t window_size_peer;
92 /* The local window size.
93 * RFC-9113 6.9.2: The settings can shrink the exiting flow-control windows to negative values.
94 **/
95 int32_t window_size_self;
96
97 /**
98 * The size to increment the window_size_self pending to be sent.
99 * Allow the pending_window_update_size to exceed the max window size.
100 * The client will send the WINDOW_UPDATE frame to the server only valid.
101 * If the pending_window_update_size is too large, we will leave the excess to send it out later.
102 **/
103 uint64_t pending_window_update_size_self;
104
105 struct aws_http_message *outgoing_message;
106 /* All queued writes. If the message provides a body stream, it will be first in this list
107 * This list can drain, which results in the stream being put to sleep (moved to waiting_streams_list in
108 * h2_connection). */
109 struct aws_linked_list outgoing_writes; /* aws_http2_stream_data_write */
110 bool received_main_headers;
111
112 bool content_length_received;
113 /* Set if incoming message has content-length header */
114 uint64_t incoming_content_length;
115 /* The total length of payload of data frame received */
116 uint64_t incoming_data_length;
117 /* Indicates that the stream is currently in the waiting_streams_list and is
118 * asleep. When stream needs to be awaken, moving the stream back to the outgoing_streams_list and set this bool
119 * to false */
120 bool waiting_for_writes;
121 /* Set when s_stream_complete has been called. Prevents double-completion
122 * (e.g. GOAWAY completes stream, then pending cancel cross-thread task fires) */
123 bool is_complete;
124 } thread_data;
125
126 /* Any thread may touch this data, but the lock must be held (unless it's an atomic) */
127 struct {
128 struct aws_mutex lock;
129
130 bool is_cross_thread_work_task_scheduled;
131
132 /* The window_update value for `thread_data.window_size_self` that haven't applied yet */
133 uint64_t pending_window_update_size_self;
134
135 /* The combined aws_http2_error_code user wanted to send to remote peer via rst_stream and internal aws error
136 * code we want to inform user about. */
137 struct aws_h2err reset_error;
138 bool reset_called;
139 bool manual_write_ended;
140
141 /* Simplified stream state. */
142 enum aws_h2_stream_api_state api_state;
143
144 /* any data streams sent manually via aws_http2_stream_write_data */
145 struct aws_linked_list pending_write_list; /* aws_h2_stream_pending_data */
146 } synced_data;
147 bool manual_write;
148
149 /* Store the sent reset HTTP/2 error code, set to -1, if none has sent so far */
150 int64_t sent_reset_error_code;
151
152 /* Store the received reset HTTP/2 error code, set to -1, if none has received so far */
153 int64_t received_reset_error_code;
154 };
155
156 const char *aws_h2_stream_state_to_str(enum aws_h2_stream_state state);
157
158 struct aws_h2_stream *aws_h2_stream_new_request(
159 struct aws_http_connection *client_connection,
160 const struct aws_http_make_request_options *options);
161
162 enum aws_h2_stream_state aws_h2_stream_get_state(const struct aws_h2_stream *stream);
163
164 /**
165 * When the flow control windows changed with SETTING and WINDOW_UPDATE frame receive.
166 * The SETTINGS and remote WINDOW_UPDATE frame should cause flow control error if window size is illegal.
167 * But client controls the window to be legal for the user.
168 **/
169 struct aws_h2err aws_h2_stream_window_size_change_direct(struct aws_h2_stream *stream, int32_t size_changed, bool self);
170
171 /* Connection is ready to send frames from stream now */
172 int aws_h2_stream_on_activated(struct aws_h2_stream *stream, enum aws_h2_stream_body_state *body_state);
173
174 /* Completes stream for one reason or another, clean up any pending writes/resources. */
175 void aws_h2_stream_complete(struct aws_h2_stream *stream, int error_code);
176
177 /* Connection is ready to send data from stream now.
178 * Stream may complete itself during this call.
179 * data_encode_status: see `aws_h2_data_encode_status`
180 */
181 int aws_h2_stream_encode_data_frame(
182 struct aws_h2_stream *stream,
183 struct aws_h2_frame_encoder *encoder,
184 struct aws_byte_buf *output,
185 int *data_encode_status);
186
187 struct aws_h2err aws_h2_stream_on_decoder_headers_begin(struct aws_h2_stream *stream);
188
189 struct aws_h2err aws_h2_stream_on_decoder_headers_i(
190 struct aws_h2_stream *stream,
191 const struct aws_http_header *header,
192 enum aws_http_header_name name_enum,
193 enum aws_http_header_block block_type);
194
195 struct aws_h2err aws_h2_stream_on_decoder_headers_end(
196 struct aws_h2_stream *stream,
197 bool malformed,
198 enum aws_http_header_block block_type);
199
200 struct aws_h2err aws_h2_stream_on_decoder_push_promise(struct aws_h2_stream *stream, uint32_t promised_stream_id);
201 struct aws_h2err aws_h2_stream_on_decoder_data_begin(
202 struct aws_h2_stream *stream,
203 uint32_t payload_len,
204 uint32_t total_padding_bytes,
205 bool end_stream);
206 struct aws_h2err aws_h2_stream_on_decoder_data_i(struct aws_h2_stream *stream, struct aws_byte_cursor data);
207 struct aws_h2err aws_h2_stream_on_decoder_window_update(
208 struct aws_h2_stream *stream,
209 uint32_t window_size_increment,
210 bool *window_resume);
211 struct aws_h2err aws_h2_stream_on_decoder_end_stream(struct aws_h2_stream *stream);
212 struct aws_h2err aws_h2_stream_on_decoder_rst_stream(struct aws_h2_stream *stream, uint32_t h2_error_code);
213
214 int aws_h2_stream_activate(struct aws_http_stream *stream);
215
216 #endif /* AWS_HTTP_H2_STREAM_H */