Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_HTTP_H1_ENCODER_H
2 #define AWS_HTTP_H1_ENCODER_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/http/private/http_impl.h>
9 #include <aws/http/private/request_response_impl.h>
10
11 struct aws_h1_data_write;
12
13 struct aws_h1_chunk {
14 struct aws_allocator *allocator;
15 struct aws_input_stream *data;
16 uint64_t data_size;
17 aws_http1_stream_write_chunk_complete_fn *on_complete;
18 void *user_data;
19 struct aws_linked_list_node node;
20 /* Buffer containing pre-encoded start line: chunk-size [chunk-ext] CRLF */
21 struct aws_byte_buf chunk_line;
22 };
23
24 struct aws_h1_trailer {
25 struct aws_allocator *allocator;
26 struct aws_byte_buf trailer_data;
27 };
28
29 /**
30 * Message to be submitted to encoder.
31 * Contains data necessary for encoder to write an outgoing request or response.
32 */
33 struct aws_h1_encoder_message {
34 /* Upon creation, the "head" (everything preceding body) is buffered here. */
35 struct aws_byte_buf outgoing_head_buf;
36 /* Single stream used for unchunked body */
37 struct aws_input_stream *body;
38
39 /* Pointer to list of `struct aws_h1_chunk`, used for chunked encoding.
40 * List is owned by aws_h1_stream.
41 * Encoder completes/frees/pops front chunk when it's done sending.
42 * If list goes empty, encoder waits for more chunks to arrive.
43 * A chunk with data_size=0 means "final chunk" */
44 struct aws_linked_list *pending_chunk_list;
45
46 /* Pointer to chunked_trailer, used for chunked_trailer. */
47 struct aws_h1_trailer *trailer;
48
49 /* Pointer to list of `struct aws_h1_data_write`, used for manual data writes with Content-Length.
50 * List is owned by aws_h1_stream. */
51 struct aws_linked_list *pending_data_write_list;
52
53 /* Current data write being processed (for manual data writes with Content-Length) */
54 struct aws_h1_data_write *current_data_write;
55
56 /* If non-zero, length of unchunked body to send */
57 uint64_t content_length;
58 bool has_connection_close_header;
59 bool has_chunked_encoding_header;
60 bool has_manual_data_writes;
61 };
62
63 enum aws_h1_encoder_state {
64 AWS_H1_ENCODER_STATE_INIT,
65 AWS_H1_ENCODER_STATE_HEAD,
66 /* Write streaming body, without chunked encoding, because Content-Length is known */
67 AWS_H1_ENCODER_STATE_UNCHUNKED_BODY_STREAM,
68 /* Write streaming body, with chunked encoding, because Content-Length is unknown */
69 AWS_H1_ENCODER_STATE_CHUNKED_BODY_STREAM,
70 AWS_H1_ENCODER_STATE_CHUNKED_BODY_STREAM_LAST_CHUNK,
71 /* The rest of the _CHUNK_ states support the write_chunk() API (body stream not provided up front) */
72 AWS_H1_ENCODER_STATE_CHUNK_NEXT,
73 AWS_H1_ENCODER_STATE_CHUNK_LINE,
74 AWS_H1_ENCODER_STATE_CHUNK_BODY,
75 AWS_H1_ENCODER_STATE_CHUNK_END,
76 AWS_H1_ENCODER_STATE_CHUNK_TRAILER,
77 /* The _DATA_WRITE_ states support the write_data() API (manual data writes with Content-Length) */
78 AWS_H1_ENCODER_STATE_DATA_WRITE_NEXT,
79 AWS_H1_ENCODER_STATE_DATA_WRITE_BODY,
80 AWS_H1_ENCODER_STATE_DONE,
81 };
82
83 struct aws_h1_encoder {
84 struct aws_allocator *allocator;
85
86 enum aws_h1_encoder_state state;
87 /* Current message being encoded */
88 struct aws_h1_encoder_message *message;
89 /* Used by some states to track progress. Reset to 0 whenever state changes */
90 uint64_t progress_bytes;
91 /* Current chunk */
92 struct aws_h1_chunk *current_chunk;
93 /* Number of chunks sent, just used for logging */
94 uint64_t chunk_count;
95 /* Encoder logs with this stream ptr as the ID, and passes this ptr to the chunk_complete callback */
96 struct aws_http_stream *current_stream;
97 };
98
99 struct aws_h1_chunk *aws_h1_chunk_new(struct aws_allocator *allocator, const struct aws_http1_chunk_options *options);
100 struct aws_h1_trailer *aws_h1_trailer_new(
101 struct aws_allocator *allocator,
102 const struct aws_http_headers *trailing_headers);
103
104 void aws_h1_trailer_destroy(struct aws_h1_trailer *trailer);
105
106 /* Just destroy the chunk (don't fire callback) */
107 void aws_h1_chunk_destroy(struct aws_h1_chunk *chunk);
108
109 /* Destroy chunk and fire its completion callback */
110 void aws_h1_chunk_complete_and_destroy(struct aws_h1_chunk *chunk, struct aws_http_stream *http_stream, int error_code);
111
112 AWS_EXTERN_C_BEGIN
113
114 /* Validate request and cache any info the encoder will need later in the "encoder message". */
115 AWS_HTTP_API
116 int aws_h1_encoder_message_init_from_request(
117 struct aws_h1_encoder_message *message,
118 struct aws_allocator *allocator,
119 const struct aws_http_message *request,
120 struct aws_linked_list *pending_chunk_list,
121 struct aws_linked_list *pending_data_write_list,
122 bool use_manual_data_writes);
123
124 int aws_h1_encoder_message_init_from_response(
125 struct aws_h1_encoder_message *message,
126 struct aws_allocator *allocator,
127 const struct aws_http_message *response,
128 bool body_headers_ignored,
129 struct aws_linked_list *pending_chunk_list);
130
131 AWS_HTTP_API
132 void aws_h1_encoder_message_clean_up(struct aws_h1_encoder_message *message);
133
134 AWS_HTTP_API
135 void aws_h1_encoder_init(struct aws_h1_encoder *encoder, struct aws_allocator *allocator);
136
137 AWS_HTTP_API
138 void aws_h1_encoder_clean_up(struct aws_h1_encoder *encoder);
139
140 AWS_HTTP_API
141 int aws_h1_encoder_start_message(
142 struct aws_h1_encoder *encoder,
143 struct aws_h1_encoder_message *message,
144 struct aws_http_stream *stream);
145
146 AWS_HTTP_API
147 int aws_h1_encoder_process(struct aws_h1_encoder *encoder, struct aws_byte_buf *out_buf);
148
149 AWS_HTTP_API
150 bool aws_h1_encoder_is_message_in_progress(const struct aws_h1_encoder *encoder);
151
152 /* Return true if the encoder is stuck waiting for more chunks to be added to the current message */
153 AWS_HTTP_API
154 bool aws_h1_encoder_is_waiting_for_chunks(const struct aws_h1_encoder *encoder);
155
156 /* Return true if the encoder is stuck waiting for more data writes to be added to the current message */
157 AWS_HTTP_API
158 bool aws_h1_encoder_is_waiting_for_data_writes(const struct aws_h1_encoder *encoder);
159
160 AWS_EXTERN_C_END
161
162 #endif /* AWS_HTTP_H1_ENCODER_H */