Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_HTTP_H2_FRAMES_H
2 #define AWS_HTTP_H2_FRAMES_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/connection.h>
10 #include <aws/http/private/hpack.h>
11 #include <aws/http/request_response.h>
12
13 #include <aws/common/byte_buf.h>
14
15 /* Ids for each frame type (RFC-7540 6) */
16 enum aws_h2_frame_type {
17 AWS_H2_FRAME_T_DATA = 0x00,
18 AWS_H2_FRAME_T_HEADERS = 0x01,
19 AWS_H2_FRAME_T_PRIORITY = 0x02,
20 AWS_H2_FRAME_T_RST_STREAM = 0x03,
21 AWS_H2_FRAME_T_SETTINGS = 0x04,
22 AWS_H2_FRAME_T_PUSH_PROMISE = 0x05,
23 AWS_H2_FRAME_T_PING = 0x06,
24 AWS_H2_FRAME_T_GOAWAY = 0x07,
25 AWS_H2_FRAME_T_WINDOW_UPDATE = 0x08,
26 AWS_H2_FRAME_T_CONTINUATION = 0x09,
27 AWS_H2_FRAME_T_UNKNOWN,
28 AWS_H2_FRAME_TYPE_COUNT,
29 };
30
31 /* Represents flags that may be set on a frame (RFC-7540 6) */
32 enum aws_h2_frame_flag {
33 AWS_H2_FRAME_F_ACK = 0x01,
34 AWS_H2_FRAME_F_END_STREAM = 0x01,
35 AWS_H2_FRAME_F_END_HEADERS = 0x04,
36 AWS_H2_FRAME_F_PADDED = 0x08,
37 AWS_H2_FRAME_F_PRIORITY = 0x20,
38 };
39
40 /* Pairs the AWS_ERROR_* to show our API user,
41 * along with the AWS_HTTP2_ERR_* that should
42 * be sent to the peer via RST_STREAM or GOAWAY.
43 *
44 * Used in place of normal error handling in functions that may result
45 * in an HTTP/2 Connection Error or Stream Error.
46 */
47 struct aws_h2err {
48 enum aws_http2_error_code h2_code;
49 int aws_code;
50 };
51
52 #define AWS_H2ERR_SUCCESS \
53 (struct aws_h2err) { \
54 .h2_code = 0, .aws_code = 0 \
55 }
56
57 #define AWS_H2_PAYLOAD_MAX (0x00FFFFFF) /* must fit in 3 bytes */
58 #define AWS_H2_WINDOW_UPDATE_MAX (0x7FFFFFFF) /* cannot use high bit */
59 #define AWS_H2_STREAM_ID_MAX (0x7FFFFFFF) /* cannot use high bit */
60 #define AWS_H2_FRAME_PREFIX_SIZE (9)
61 #define AWS_H2_INIT_WINDOW_SIZE (65535) /* Defined initial window size */
62
63 /* Legal min(inclusive) and max(inclusive) for each setting */
64 extern const uint32_t aws_h2_settings_bounds[AWS_HTTP2_SETTINGS_END_RANGE][2];
65
66 /* Initial values for settings RFC-7540 6.5.2 */
67 AWS_HTTP_API
68 extern const uint32_t aws_h2_settings_initial[AWS_HTTP2_SETTINGS_END_RANGE];
69
70 /* This magic string must be the very first thing a client sends to the server.
71 * See RFC-7540 3.5 - HTTP/2 Connection Preface.
72 * Exported for tests */
73 AWS_HTTP_API
74 extern const struct aws_byte_cursor aws_h2_connection_preface_client_string;
75
76 /**
77 * Present in all frames that may have set AWS_H2_FRAME_F_PRIORITY
78 *
79 * Encoded as:
80 * +-+-------------------------------------------------------------+
81 * |E| Stream Dependency (31) |
82 * +-+-------------+-----------------------------------------------+
83 * | Weight (8) |
84 * +-+-------------+
85 */
86 struct aws_h2_frame_priority_settings {
87 uint32_t stream_dependency;
88 bool stream_dependency_exclusive;
89 uint8_t weight;
90 };
91
92 /**
93 * A frame to be encoded.
94 * (in the case of HEADERS and PUSH_PROMISE, it might turn into multiple frames due to CONTINUATION)
95 */
96 struct aws_h2_frame {
97 const struct aws_h2_frame_vtable *vtable;
98 struct aws_allocator *alloc;
99 struct aws_linked_list_node node;
100 enum aws_h2_frame_type type;
101 uint32_t stream_id;
102
103 /* If true, frame will be sent before those with normal priority.
104 * Useful for frames like PING ACK where low latency is important. */
105 bool high_priority;
106 };
107
108 /* Used to encode a frame */
109 struct aws_h2_frame_encoder {
110 struct aws_allocator *allocator;
111 const void *logging_id;
112 struct aws_hpack_encoder hpack;
113 struct aws_h2_frame *current_frame;
114
115 /* Settings for frame encoder, which is based on the settings received from peer */
116 struct {
117 /* the size of the largest frame payload */
118 uint32_t max_frame_size;
119 } settings;
120
121 bool has_errored;
122 };
123
124 typedef void aws_h2_frame_destroy_fn(struct aws_h2_frame *frame_base);
125 typedef int aws_h2_frame_encode_fn(
126 struct aws_h2_frame *frame_base,
127 struct aws_h2_frame_encoder *encoder,
128 struct aws_byte_buf *output,
129 bool *complete);
130
131 struct aws_h2_frame_vtable {
132 aws_h2_frame_destroy_fn *destroy;
133 aws_h2_frame_encode_fn *encode;
134 };
135
136 AWS_EXTERN_C_BEGIN
137
138 AWS_HTTP_API
139 const char *aws_h2_frame_type_to_str(enum aws_h2_frame_type type);
140
141 AWS_HTTP_API
142 const char *aws_http2_error_code_to_str(enum aws_http2_error_code h2_error_code);
143
144 /**
145 * Specify which HTTP/2 error-code will be sent to the peer in a GOAWAY or RST_STREAM frame.
146 *
147 * The AWS_ERROR reported to the API user will be AWS_ERROR_HTTP_PROTOCOL_ERROR.
148 */
149 AWS_HTTP_API
150 struct aws_h2err aws_h2err_from_h2_code(enum aws_http2_error_code h2_error_code);
151
152 /**
153 * Specify which AWS_ERROR will be reported to the API user.
154 *
155 * The peer will be sent a GOAWAY or RST_STREAM with the INTERNAL_ERROR HTTP/2 error-code.
156 */
157 AWS_HTTP_API
158 struct aws_h2err aws_h2err_from_aws_code(int aws_error_code);
159
160 AWS_HTTP_API
161 struct aws_h2err aws_h2err_from_last_error(void);
162
163 AWS_HTTP_API
164 bool aws_h2err_success(struct aws_h2err err);
165
166 AWS_HTTP_API
167 bool aws_h2err_failed(struct aws_h2err err);
168
169 /* Raises AWS_ERROR_INVALID_ARGUMENT if stream_id is 0 or exceeds AWS_H2_MAX_STREAM_ID */
170 AWS_HTTP_API
171 int aws_h2_validate_stream_id(uint32_t stream_id);
172
173 /**
174 * The process of encoding a frame looks like:
175 * 1. Create a encoder object on the stack and initialize with aws_h2_frame_encoder_init
176 * 2. Encode the frame using aws_h2_encode_frame()
177 */
178 AWS_HTTP_API
179 int aws_h2_frame_encoder_init(
180 struct aws_h2_frame_encoder *encoder,
181 struct aws_allocator *allocator,
182 const void *logging_id);
183
184 AWS_HTTP_API
185 void aws_h2_frame_encoder_clean_up(struct aws_h2_frame_encoder *encoder);
186
187 /**
188 * Attempt to encode frame into output buffer.
189 * AWS_OP_ERR is returned if encoder encounters an unrecoverable error.
190 * frame_complete will be set true if the frame finished encoding.
191 *
192 * If frame_complete is false then we MUST call aws_h2_encode_frame() again
193 * with all the same inputs, when we have a fresh buffer (it would be illegal
194 * to encode a different frame).
195 */
196 AWS_HTTP_API
197 int aws_h2_encode_frame(
198 struct aws_h2_frame_encoder *encoder,
199 struct aws_h2_frame *frame,
200 struct aws_byte_buf *output,
201 bool *frame_complete);
202
203 /**
204 * Attempt to encode a DATA frame into the output buffer.
205 * The body_stream will be read into the available space (up to MAX_FRAME_SIZE).
206 * AWS_OP_ERR is returned if encoder encounters an unrecoverable error.
207 * body_complete will be set true if encoder reaches the end of the body_stream.
208 * body_stalled will be true if aws_input_stream_read() stopped early (didn't
209 * complete, though more space was available).
210 * body_failed will be true if the aws_input_stream was the cause of an error.
211 *
212 * Each call to this function encodes a complete DATA frame, or nothing at all,
213 * so it's always safe to encode a different frame type or the body of a different stream
214 * after calling this.
215 */
216 AWS_HTTP_API
217 int aws_h2_encode_data_frame(
218 struct aws_h2_frame_encoder *encoder,
219 uint32_t stream_id,
220 struct aws_input_stream *body_stream,
221 bool body_ends_stream,
222 uint8_t pad_length,
223 int32_t *stream_window_size_peer,
224 uint32_t *connection_window_size_peer,
225 struct aws_byte_buf *output,
226 bool *body_complete,
227 bool *body_stalled,
228 bool *body_failed);
229
230 AWS_HTTP_API
231 void aws_h2_frame_destroy(struct aws_h2_frame *frame);
232
233 /**
234 * This frame type may actually end up encoding multiple frames
235 * (HEADERS followed by 0 or more CONTINUATION frames).
236 */
237 AWS_HTTP_API
238 struct aws_h2_frame *aws_h2_frame_new_headers(
239 struct aws_allocator *allocator,
240 uint32_t stream_id,
241 const struct aws_http_headers *headers,
242 bool end_stream,
243 uint8_t pad_length,
244 const struct aws_h2_frame_priority_settings *optional_priority);
245
246 AWS_HTTP_API
247 struct aws_h2_frame *aws_h2_frame_new_priority(
248 struct aws_allocator *allocator,
249 uint32_t stream_id,
250 const struct aws_h2_frame_priority_settings *priority);
251
252 AWS_HTTP_API
253 struct aws_h2_frame *aws_h2_frame_new_rst_stream(
254 struct aws_allocator *allocator,
255 uint32_t stream_id,
256 uint32_t error_code);
257
258 AWS_HTTP_API
259 struct aws_h2_frame *aws_h2_frame_new_settings(
260 struct aws_allocator *allocator,
261 const struct aws_http2_setting *settings_array,
262 size_t num_settings,
263 bool ack);
264
265 /**
266 * This frame type may actually end up encoding multiple frames
267 * (PUSH_PROMISE followed 0 or more CONTINUATION frames).
268 */
269 AWS_HTTP_API
270 struct aws_h2_frame *aws_h2_frame_new_push_promise(
271 struct aws_allocator *allocator,
272 uint32_t stream_id,
273 uint32_t promised_stream_id,
274 const struct aws_http_headers *headers,
275 uint8_t pad_length);
276
277 AWS_HTTP_API
278 struct aws_h2_frame *aws_h2_frame_new_ping(
279 struct aws_allocator *allocator,
280 bool ack,
281 const uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE]);
282
283 AWS_HTTP_API
284 struct aws_h2_frame *aws_h2_frame_new_goaway(
285 struct aws_allocator *allocator,
286 uint32_t last_stream_id,
287 uint32_t error_code,
288 struct aws_byte_cursor debug_data);
289
290 AWS_HTTP_API
291 struct aws_h2_frame *aws_h2_frame_new_window_update(
292 struct aws_allocator *allocator,
293 uint32_t stream_id,
294 uint32_t window_size_increment);
295
296 AWS_HTTP_API void aws_h2_frame_encoder_set_setting_header_table_size(
297 struct aws_h2_frame_encoder *encoder,
298 uint32_t data);
299 AWS_HTTP_API void aws_h2_frame_encoder_set_setting_max_frame_size(struct aws_h2_frame_encoder *encoder, uint32_t data);
300
301 AWS_EXTERN_C_END
302
303 #endif /* AWS_HTTP_H2_FRAMES_H */