Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_HTTP_H2_DECODER_H
2 #define AWS_HTTP_H2_DECODER_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/http_impl.h>
11
12 /* Decoder design goals:
13 * - Minimize state tracking and verification required by user.
14 * For example, we have _begin()/_i()/_end() callbacks when something happens N times.
15 * The _begin() and _end() callbacks tell the user when to transition states.
16 * Without them the user needs to be like, oh, I was doing X but now I'm doing Y,
17 * so I guess I need to end X and start Y.
18
19 * - A callback should result in 1 distinct action.
20 * For example, we have distinct callbacks for `on_ping()` and `on_ping_ack()`.
21 * We COULD have had just one `on_ping(bool ack)` callback, but since user must
22 * take two complete different actions based on the ACK, we opted for two callbacks.
23 */
24
25 /* Return a failed aws_h2err from any callback to stop the decoder and cause a Connection Error */
26 struct aws_h2_decoder_vtable {
27 /* For HEADERS header-block: _begin() is called, then 0+ _i() calls, then _end().
28 * No other decoder callbacks will occur in this time.
29 * If something is malformed, no further _i() calls occur, and it is reported in _end() */
30 struct aws_h2err (*on_headers_begin)(uint32_t stream_id, void *userdata);
31 struct aws_h2err (*on_headers_i)(
32 uint32_t stream_id,
33 const struct aws_http_header *header,
34 enum aws_http_header_name name_enum,
35 enum aws_http_header_block block_type,
36 void *userdata);
37 struct aws_h2err (
38 *on_headers_end)(uint32_t stream_id, bool malformed, enum aws_http_header_block block_type, void *userdata);
39
40 /* For PUSH_PROMISE header-block: _begin() is called, then 0+ _i() calls, then _end().
41 * No other decoder callbacks will occur in this time.
42 * If something is malformed, no further _i() calls occur, and it is reported in _end() */
43 struct aws_h2err (*on_push_promise_begin)(uint32_t stream_id, uint32_t promised_stream_id, void *userdata);
44 struct aws_h2err (*on_push_promise_i)(
45 uint32_t stream_id,
46 const struct aws_http_header *header,
47 enum aws_http_header_name name_enum,
48 void *userdata);
49 struct aws_h2err (*on_push_promise_end)(uint32_t stream_id, bool malformed, void *userdata);
50
51 /* For DATA frame: _begin() is called, then 0+ _i() calls, then _end().
52 * No other decoder callbacks will occur in this time */
53 struct aws_h2err (*on_data_begin)(
54 uint32_t stream_id,
55 uint32_t payload_len, /* Whole payload length including padding and padding length */
56 uint32_t total_padding_bytes, /* The length of padding and the byte for padding length */
57 bool end_stream,
58 void *userdata);
59 struct aws_h2err (*on_data_i)(uint32_t stream_id, struct aws_byte_cursor data, void *userdata);
60 struct aws_h2err (*on_data_end)(uint32_t stream_id, void *userdata);
61
62 /* Called at end of DATA frame containing the END_STREAM flag.
63 * OR called at end of header-block which began with HEADERS frame containing the END_STREAM flag */
64 struct aws_h2err (*on_end_stream)(uint32_t stream_id, void *userdata);
65
66 /* Called once for RST_STREAM frame */
67 struct aws_h2err (*on_rst_stream)(uint32_t stream_id, uint32_t error_code, void *userdata);
68
69 /* Called once For PING frame with ACK flag set */
70 struct aws_h2err (*on_ping_ack)(uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE], void *userdata);
71
72 /* Called once for PING frame (no ACK flag set)*/
73 struct aws_h2err (*on_ping)(uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE], void *userdata);
74
75 /* Called once for SETTINGS frame with ACK flag */
76 struct aws_h2err (*on_settings_ack)(void *userdata);
77
78 /* Called once for SETTINGS frame, without ACK flag */
79 struct aws_h2err (
80 *on_settings)(const struct aws_http2_setting *settings_array, size_t num_settings, void *userdata);
81
82 /* Called once for GOAWAY frame */
83 struct aws_h2err (
84 *on_goaway)(uint32_t last_stream, uint32_t error_code, struct aws_byte_cursor debug_data, void *userdata);
85
86 /* Called once for WINDOW_UPDATE frame */
87 struct aws_h2err (*on_window_update)(uint32_t stream_id, uint32_t window_size_increment, void *userdata);
88 };
89
90 /**
91 * Structure used to initialize an `aws_h2_decoder`.
92 */
93 struct aws_h2_decoder_params {
94 struct aws_allocator *alloc;
95 const struct aws_h2_decoder_vtable *vtable;
96 void *userdata;
97 const void *logging_id;
98 bool is_server;
99
100 /* If true, do not expect the connection preface and immediately accept any frame type.
101 * Only set this when testing the decoder itself */
102 bool skip_connection_preface;
103 };
104
105 struct aws_h2_decoder;
106
107 AWS_EXTERN_C_BEGIN
108
109 AWS_HTTP_API struct aws_h2_decoder *aws_h2_decoder_new(struct aws_h2_decoder_params *params);
110 AWS_HTTP_API void aws_h2_decoder_destroy(struct aws_h2_decoder *decoder);
111
112 /* If failed aws_h2err returned, it is a Connection Error */
113 AWS_HTTP_API struct aws_h2err aws_h2_decode(struct aws_h2_decoder *decoder, struct aws_byte_cursor *data);
114
115 AWS_HTTP_API void aws_h2_decoder_set_setting_header_table_size(struct aws_h2_decoder *decoder, uint32_t data);
116 AWS_HTTP_API void aws_h2_decoder_set_setting_enable_push(struct aws_h2_decoder *decoder, uint32_t data);
117 AWS_HTTP_API void aws_h2_decoder_set_setting_max_frame_size(struct aws_h2_decoder *decoder, uint32_t data);
118
119 AWS_EXTERN_C_END
120
121 #endif /* AWS_HTTP_H2_DECODER_H */