File: h2_frames.h

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (299 lines) | stat: -rw-r--r-- 9,449 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef AWS_HTTP_H2_FRAMES_H
#define AWS_HTTP_H2_FRAMES_H

/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/http/connection.h>
#include <aws/http/private/hpack.h>
#include <aws/http/request_response.h>

#include <aws/common/byte_buf.h>

/* Ids for each frame type (RFC-7540 6) */
enum aws_h2_frame_type {
    AWS_H2_FRAME_T_DATA = 0x00,
    AWS_H2_FRAME_T_HEADERS = 0x01,
    AWS_H2_FRAME_T_PRIORITY = 0x02,
    AWS_H2_FRAME_T_RST_STREAM = 0x03,
    AWS_H2_FRAME_T_SETTINGS = 0x04,
    AWS_H2_FRAME_T_PUSH_PROMISE = 0x05,
    AWS_H2_FRAME_T_PING = 0x06,
    AWS_H2_FRAME_T_GOAWAY = 0x07,
    AWS_H2_FRAME_T_WINDOW_UPDATE = 0x08,
    AWS_H2_FRAME_T_CONTINUATION = 0x09,
    AWS_H2_FRAME_T_UNKNOWN,
    AWS_H2_FRAME_TYPE_COUNT,
};

/* Represents flags that may be set on a frame (RFC-7540 6) */
enum aws_h2_frame_flag {
    AWS_H2_FRAME_F_ACK = 0x01,
    AWS_H2_FRAME_F_END_STREAM = 0x01,
    AWS_H2_FRAME_F_END_HEADERS = 0x04,
    AWS_H2_FRAME_F_PADDED = 0x08,
    AWS_H2_FRAME_F_PRIORITY = 0x20,
};

/* Pairs the AWS_ERROR_* to show our API user,
 * along with the AWS_HTTP2_ERR_* that should
 * be sent to the peer via RST_STREAM or GOAWAY.
 *
 * Used in place of normal error handling in functions that may result
 * in an HTTP/2 Connection Error or Stream Error.
 */
struct aws_h2err {
    enum aws_http2_error_code h2_code;
    int aws_code;
};

#define AWS_H2ERR_SUCCESS                                                                                              \
    (struct aws_h2err) { .h2_code = 0, .aws_code = 0 }

#define AWS_H2_PAYLOAD_MAX (0x00FFFFFF)       /* must fit in 3 bytes */
#define AWS_H2_WINDOW_UPDATE_MAX (0x7FFFFFFF) /* cannot use high bit */
#define AWS_H2_STREAM_ID_MAX (0x7FFFFFFF)     /* cannot use high bit */
#define AWS_H2_FRAME_PREFIX_SIZE (9)
#define AWS_H2_INIT_WINDOW_SIZE (65535) /* Defined initial window size */

/* Legal min(inclusive) and max(inclusive) for each setting */
extern const uint32_t aws_h2_settings_bounds[AWS_HTTP2_SETTINGS_END_RANGE][2];

/* Initial values for settings RFC-7540 6.5.2 */
AWS_HTTP_API
extern const uint32_t aws_h2_settings_initial[AWS_HTTP2_SETTINGS_END_RANGE];

/* This magic string must be the very first thing a client sends to the server.
 * See RFC-7540 3.5 - HTTP/2 Connection Preface.
 * Exported for tests */
AWS_HTTP_API
extern const struct aws_byte_cursor aws_h2_connection_preface_client_string;

/**
 * Present in all frames that may have set AWS_H2_FRAME_F_PRIORITY
 *
 * Encoded as:
 * +-+-------------------------------------------------------------+
 * |E|                  Stream Dependency (31)                     |
 * +-+-------------+-----------------------------------------------+
 * |   Weight (8)  |
 * +-+-------------+
 */
struct aws_h2_frame_priority_settings {
    uint32_t stream_dependency;
    bool stream_dependency_exclusive;
    uint8_t weight;
};

/**
 * A frame to be encoded.
 * (in the case of HEADERS and PUSH_PROMISE, it might turn into multiple frames due to CONTINUATION)
 */
struct aws_h2_frame {
    const struct aws_h2_frame_vtable *vtable;
    struct aws_allocator *alloc;
    struct aws_linked_list_node node;
    enum aws_h2_frame_type type;
    uint32_t stream_id;

    /* If true, frame will be sent before those with normal priority.
     * Useful for frames like PING ACK where low latency is important. */
    bool high_priority;
};

/* Used to encode a frame */
struct aws_h2_frame_encoder {
    struct aws_allocator *allocator;
    const void *logging_id;
    struct aws_hpack_encoder hpack;
    struct aws_h2_frame *current_frame;

    /* Settings for frame encoder, which is based on the settings received from peer */
    struct {
        /*  the size of the largest frame payload */
        uint32_t max_frame_size;
    } settings;

    bool has_errored;
};

typedef void aws_h2_frame_destroy_fn(struct aws_h2_frame *frame_base);
typedef int aws_h2_frame_encode_fn(
    struct aws_h2_frame *frame_base,
    struct aws_h2_frame_encoder *encoder,
    struct aws_byte_buf *output,
    bool *complete);

struct aws_h2_frame_vtable {
    aws_h2_frame_destroy_fn *destroy;
    aws_h2_frame_encode_fn *encode;
};

AWS_EXTERN_C_BEGIN

AWS_HTTP_API
const char *aws_h2_frame_type_to_str(enum aws_h2_frame_type type);

AWS_HTTP_API
const char *aws_http2_error_code_to_str(enum aws_http2_error_code h2_error_code);

/**
 * Specify which HTTP/2 error-code will be sent to the peer in a GOAWAY or RST_STREAM frame.
 *
 * The AWS_ERROR reported to the API user will be AWS_ERROR_HTTP_PROTOCOL_ERROR.
 */
AWS_HTTP_API
struct aws_h2err aws_h2err_from_h2_code(enum aws_http2_error_code h2_error_code);

/**
 * Specify which AWS_ERROR will be reported to the API user.
 *
 * The peer will be sent a GOAWAY or RST_STREAM with the INTERNAL_ERROR HTTP/2 error-code.
 */
AWS_HTTP_API
struct aws_h2err aws_h2err_from_aws_code(int aws_error_code);

AWS_HTTP_API
struct aws_h2err aws_h2err_from_last_error(void);

AWS_HTTP_API
bool aws_h2err_success(struct aws_h2err err);

AWS_HTTP_API
bool aws_h2err_failed(struct aws_h2err err);

/* Raises AWS_ERROR_INVALID_ARGUMENT if stream_id is 0 or exceeds AWS_H2_MAX_STREAM_ID */
AWS_HTTP_API
int aws_h2_validate_stream_id(uint32_t stream_id);

/**
 * The process of encoding a frame looks like:
 * 1. Create a encoder object on the stack and initialize with aws_h2_frame_encoder_init
 * 2. Encode the frame using aws_h2_encode_frame()
 */
AWS_HTTP_API
int aws_h2_frame_encoder_init(
    struct aws_h2_frame_encoder *encoder,
    struct aws_allocator *allocator,
    const void *logging_id);

AWS_HTTP_API
void aws_h2_frame_encoder_clean_up(struct aws_h2_frame_encoder *encoder);

/**
 * Attempt to encode frame into output buffer.
 * AWS_OP_ERR is returned if encoder encounters an unrecoverable error.
 * frame_complete will be set true if the frame finished encoding.
 *
 * If frame_complete is false then we MUST call aws_h2_encode_frame() again
 * with all the same inputs, when we have a fresh buffer (it would be illegal
 * to encode a different frame).
 */
AWS_HTTP_API
int aws_h2_encode_frame(
    struct aws_h2_frame_encoder *encoder,
    struct aws_h2_frame *frame,
    struct aws_byte_buf *output,
    bool *frame_complete);

/**
 * Attempt to encode a DATA frame into the output buffer.
 * The body_stream will be read into the available space (up to MAX_FRAME_SIZE).
 * AWS_OP_ERR is returned if encoder encounters an unrecoverable error.
 * body_complete will be set true if encoder reaches the end of the body_stream.
 * body_stalled will be true if aws_input_stream_read() stopped early (didn't
 * complete, though more space was available).
 *
 * Each call to this function encodes a complete DATA frame, or nothing at all,
 * so it's always safe to encode a different frame type or the body of a different stream
 * after calling this.
 */
AWS_HTTP_API
int aws_h2_encode_data_frame(
    struct aws_h2_frame_encoder *encoder,
    uint32_t stream_id,
    struct aws_input_stream *body_stream,
    bool body_ends_stream,
    uint8_t pad_length,
    int32_t *stream_window_size_peer,
    size_t *connection_window_size_peer,
    struct aws_byte_buf *output,
    bool *body_complete,
    bool *body_stalled);

AWS_HTTP_API
void aws_h2_frame_destroy(struct aws_h2_frame *frame);

/**
 * This frame type may actually end up encoding multiple frames
 * (HEADERS followed by 0 or more CONTINUATION frames).
 */
AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_headers(
    struct aws_allocator *allocator,
    uint32_t stream_id,
    const struct aws_http_headers *headers,
    bool end_stream,
    uint8_t pad_length,
    const struct aws_h2_frame_priority_settings *optional_priority);

AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_priority(
    struct aws_allocator *allocator,
    uint32_t stream_id,
    const struct aws_h2_frame_priority_settings *priority);

AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_rst_stream(
    struct aws_allocator *allocator,
    uint32_t stream_id,
    uint32_t error_code);

AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_settings(
    struct aws_allocator *allocator,
    const struct aws_http2_setting *settings_array,
    size_t num_settings,
    bool ack);

/**
 * This frame type may actually end up encoding multiple frames
 * (PUSH_PROMISE followed 0 or more CONTINUATION frames).
 */
AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_push_promise(
    struct aws_allocator *allocator,
    uint32_t stream_id,
    uint32_t promised_stream_id,
    const struct aws_http_headers *headers,
    uint8_t pad_length);

AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_ping(
    struct aws_allocator *allocator,
    bool ack,
    const uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE]);

AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_goaway(
    struct aws_allocator *allocator,
    uint32_t last_stream_id,
    uint32_t error_code,
    struct aws_byte_cursor debug_data);

AWS_HTTP_API
struct aws_h2_frame *aws_h2_frame_new_window_update(
    struct aws_allocator *allocator,
    uint32_t stream_id,
    uint32_t window_size_increment);

AWS_HTTP_API void aws_h2_frame_encoder_set_setting_header_table_size(
    struct aws_h2_frame_encoder *encoder,
    uint32_t data);
AWS_HTTP_API void aws_h2_frame_encoder_set_setting_max_frame_size(struct aws_h2_frame_encoder *encoder, uint32_t data);

AWS_EXTERN_C_END

#endif /* AWS_HTTP_H2_FRAMES_H */