Debian Codegraph @

third_party/python/aiohttp/vendor/llhttp/src/native/api.h

raw
1 #ifndef INCLUDE_LLHTTP_API_H_
2 #define INCLUDE_LLHTTP_API_H_
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 #include <stddef.h>
7
8 #if defined(__wasm__)
9 #define LLHTTP_EXPORT __attribute__((visibility("default")))
10 #elif defined(_WIN32)
11 #define LLHTTP_EXPORT __declspec(dllexport)
12 #else
13 #define LLHTTP_EXPORT
14 #endif
15
16 typedef llhttp__internal_t llhttp_t;
17 typedef struct llhttp_settings_s llhttp_settings_t;
18
19 typedef int (*llhttp_data_cb)(llhttp_t*, const char *at, size_t length);
20 typedef int (*llhttp_cb)(llhttp_t*);
21
22 struct llhttp_settings_s {
23 /* Possible return values 0, -1, `HPE_PAUSED` */
24 llhttp_cb on_message_begin;
25
26 /* Possible return values 0, -1, HPE_USER */
27 llhttp_data_cb on_url;
28 llhttp_data_cb on_status;
29 llhttp_data_cb on_method;
30 llhttp_data_cb on_version;
31 llhttp_data_cb on_header_field;
32 llhttp_data_cb on_header_value;
33 llhttp_data_cb on_chunk_extension_name;
34 llhttp_data_cb on_chunk_extension_value;
35
36 /* Possible return values:
37 * 0 - Proceed normally
38 * 1 - Assume that request/response has no body, and proceed to parsing the
39 * next message
40 * 2 - Assume absence of body (as above) and make `llhttp_execute()` return
41 * `HPE_PAUSED_UPGRADE`
42 * -1 - Error
43 * `HPE_PAUSED`
44 */
45 llhttp_cb on_headers_complete;
46
47 /* Possible return values 0, -1, HPE_USER */
48 llhttp_data_cb on_body;
49
50 /* Possible return values 0, -1, `HPE_PAUSED` */
51 llhttp_cb on_message_complete;
52 llhttp_cb on_url_complete;
53 llhttp_cb on_status_complete;
54 llhttp_cb on_method_complete;
55 llhttp_cb on_version_complete;
56 llhttp_cb on_header_field_complete;
57 llhttp_cb on_header_value_complete;
58 llhttp_cb on_chunk_extension_name_complete;
59 llhttp_cb on_chunk_extension_value_complete;
60
61 /* When on_chunk_header is called, the current chunk length is stored
62 * in parser->content_length.
63 * Possible return values 0, -1, `HPE_PAUSED`
64 */
65 llhttp_cb on_chunk_header;
66 llhttp_cb on_chunk_complete;
67 llhttp_cb on_reset;
68 };
69
70 /* Initialize the parser with specific type and user settings.
71 *
72 * NOTE: lifetime of `settings` has to be at least the same as the lifetime of
73 * the `parser` here. In practice, `settings` has to be either a static
74 * variable or be allocated with `malloc`, `new`, etc.
75 */
76 LLHTTP_EXPORT
77 void llhttp_init(llhttp_t* parser, llhttp_type_t type,
78 const llhttp_settings_t* settings);
79
80 LLHTTP_EXPORT
81 llhttp_t* llhttp_alloc(llhttp_type_t type);
82
83 LLHTTP_EXPORT
84 void llhttp_free(llhttp_t* parser);
85
86 LLHTTP_EXPORT
87 uint8_t llhttp_get_type(llhttp_t* parser);
88
89 LLHTTP_EXPORT
90 uint8_t llhttp_get_http_major(llhttp_t* parser);
91
92 LLHTTP_EXPORT
93 uint8_t llhttp_get_http_minor(llhttp_t* parser);
94
95 LLHTTP_EXPORT
96 uint8_t llhttp_get_method(llhttp_t* parser);
97
98 LLHTTP_EXPORT
99 int llhttp_get_status_code(llhttp_t* parser);
100
101 LLHTTP_EXPORT
102 uint8_t llhttp_get_upgrade(llhttp_t* parser);
103
104 /* Reset an already initialized parser back to the start state, preserving the
105 * existing parser type, callback settings, user data, and lenient flags.
106 */
107 LLHTTP_EXPORT
108 void llhttp_reset(llhttp_t* parser);
109
110 /* Initialize the settings object */
111 LLHTTP_EXPORT
112 void llhttp_settings_init(llhttp_settings_t* settings);
113
114 /* Parse full or partial request/response, invoking user callbacks along the
115 * way.
116 *
117 * If any of `llhttp_data_cb` returns errno not equal to `HPE_OK` - the parsing
118 * interrupts, and such errno is returned from `llhttp_execute()`. If
119 * `HPE_PAUSED` was used as a errno, the execution can be resumed with
120 * `llhttp_resume()` call.
121 *
122 * In a special case of CONNECT/Upgrade request/response `HPE_PAUSED_UPGRADE`
123 * is returned after fully parsing the request/response. If the user wishes to
124 * continue parsing, they need to invoke `llhttp_resume_after_upgrade()`.
125 *
126 * NOTE: if this function ever returns a non-pause type error, it will continue
127 * to return the same error upon each successive call up until `llhttp_init()`
128 * is called.
129 */
130 LLHTTP_EXPORT
131 llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
132
133 /* This method should be called when the other side has no further bytes to
134 * send (e.g. shutdown of readable side of the TCP connection.)
135 *
136 * Requests without `Content-Length` and other messages might require treating
137 * all incoming bytes as the part of the body, up to the last byte of the
138 * connection. This method will invoke `on_message_complete()` callback if the
139 * request was terminated safely. Otherwise a error code would be returned.
140 */
141 LLHTTP_EXPORT
142 llhttp_errno_t llhttp_finish(llhttp_t* parser);
143
144 /* Returns `1` if the incoming message is parsed until the last byte, and has
145 * to be completed by calling `llhttp_finish()` on EOF
146 */
147 LLHTTP_EXPORT
148 int llhttp_message_needs_eof(const llhttp_t* parser);
149
150 /* Returns `1` if there might be any other messages following the last that was
151 * successfully parsed.
152 */
153 LLHTTP_EXPORT
154 int llhttp_should_keep_alive(const llhttp_t* parser);
155
156 /* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
157 * appropriate error reason.
158 *
159 * Important: do not call this from user callbacks! User callbacks must return
160 * `HPE_PAUSED` if pausing is required.
161 */
162 LLHTTP_EXPORT
163 void llhttp_pause(llhttp_t* parser);
164
165 /* Might be called to resume the execution after the pause in user's callback.
166 * See `llhttp_execute()` above for details.
167 *
168 * Call this only if `llhttp_execute()` returns `HPE_PAUSED`.
169 */
170 LLHTTP_EXPORT
171 void llhttp_resume(llhttp_t* parser);
172
173 /* Might be called to resume the execution after the pause in user's callback.
174 * See `llhttp_execute()` above for details.
175 *
176 * Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`
177 */
178 LLHTTP_EXPORT
179 void llhttp_resume_after_upgrade(llhttp_t* parser);
180
181 /* Returns the latest return error */
182 LLHTTP_EXPORT
183 llhttp_errno_t llhttp_get_errno(const llhttp_t* parser);
184
185 /* Returns the verbal explanation of the latest returned error.
186 *
187 * Note: User callback should set error reason when returning the error. See
188 * `llhttp_set_error_reason()` for details.
189 */
190 LLHTTP_EXPORT
191 const char* llhttp_get_error_reason(const llhttp_t* parser);
192
193 /* Assign verbal description to the returned error. Must be called in user
194 * callbacks right before returning the errno.
195 *
196 * Note: `HPE_USER` error code might be useful in user callbacks.
197 */
198 LLHTTP_EXPORT
199 void llhttp_set_error_reason(llhttp_t* parser, const char* reason);
200
201 /* Returns the pointer to the last parsed byte before the returned error. The
202 * pointer is relative to the `data` argument of `llhttp_execute()`.
203 *
204 * Note: this method might be useful for counting the number of parsed bytes.
205 */
206 LLHTTP_EXPORT
207 const char* llhttp_get_error_pos(const llhttp_t* parser);
208
209 /* Returns textual name of error code */
210 LLHTTP_EXPORT
211 const char* llhttp_errno_name(llhttp_errno_t err);
212
213 /* Returns textual name of HTTP method */
214 LLHTTP_EXPORT
215 const char* llhttp_method_name(llhttp_method_t method);
216
217 /* Returns textual name of HTTP status */
218 LLHTTP_EXPORT
219 const char* llhttp_status_name(llhttp_status_t status);
220
221 /* Enables/disables lenient header value parsing (disabled by default).
222 *
223 * Lenient parsing disables header value token checks, extending llhttp's
224 * protocol support to highly non-compliant clients/server. No
225 * `HPE_INVALID_HEADER_TOKEN` will be raised for incorrect header values when
226 * lenient parsing is "on".
227 *
228 * **Enabling this flag can pose a security issue since you will be exposed to
229 * request smuggling attacks. USE WITH CAUTION!**
230 */
231 LLHTTP_EXPORT
232 void llhttp_set_lenient_headers(llhttp_t* parser, int enabled);
233
234
235 /* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
236 * `Content-Length` headers (disabled by default).
237 *
238 * Normally `llhttp` would error when `Transfer-Encoding` is present in
239 * conjunction with `Content-Length`. This error is important to prevent HTTP
240 * request smuggling, but may be less desirable for small number of cases
241 * involving legacy servers.
242 *
243 * **Enabling this flag can pose a security issue since you will be exposed to
244 * request smuggling attacks. USE WITH CAUTION!**
245 */
246 LLHTTP_EXPORT
247 void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
248
249
250 /* Enables/disables lenient handling of `Connection: close` and HTTP/1.0
251 * requests responses.
252 *
253 * Normally `llhttp` would error on (in strict mode) or discard (in loose mode)
254 * the HTTP request/response after the request/response with `Connection: close`
255 * and `Content-Length`. This is important to prevent cache poisoning attacks,
256 * but might interact badly with outdated and insecure clients. With this flag
257 * the extra request/response will be parsed normally.
258 *
259 * **Enabling this flag can pose a security issue since you will be exposed to
260 * poisoning attacks. USE WITH CAUTION!**
261 */
262 LLHTTP_EXPORT
263 void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled);
264
265 /* Enables/disables lenient handling of `Transfer-Encoding` header.
266 *
267 * Normally `llhttp` would error when a `Transfer-Encoding` has `chunked` value
268 * and another value after it (either in a single header or in multiple
269 * headers whose value are internally joined using `, `).
270 * This is mandated by the spec to reliably determine request body size and thus
271 * avoid request smuggling.
272 * With this flag the extra value will be parsed normally.
273 *
274 * **Enabling this flag can pose a security issue since you will be exposed to
275 * request smuggling attacks. USE WITH CAUTION!**
276 */
277 LLHTTP_EXPORT
278 void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled);
279
280 /* Enables/disables lenient handling of HTTP version.
281 *
282 * Normally `llhttp` would error when the HTTP version in the request or status line
283 * is not `0.9`, `1.0`, `1.1` or `2.0`.
284 * With this flag the invalid value will be parsed normally.
285 *
286 * **Enabling this flag can pose a security issue since you will allow unsupported
287 * HTTP versions. USE WITH CAUTION!**
288 */
289 LLHTTP_EXPORT
290 void llhttp_set_lenient_version(llhttp_t* parser, int enabled);
291
292 /* Enables/disables lenient handling of additional data received after a message ends
293 * and keep-alive is disabled.
294 *
295 * Normally `llhttp` would error when additional unexpected data is received if the message
296 * contains the `Connection` header with `close` value.
297 * With this flag the extra data will discarded without throwing an error.
298 *
299 * **Enabling this flag can pose a security issue since you will be exposed to
300 * poisoning attacks. USE WITH CAUTION!**
301 */
302 LLHTTP_EXPORT
303 void llhttp_set_lenient_data_after_close(llhttp_t* parser, int enabled);
304
305 /* Enables/disables lenient handling of incomplete CRLF sequences.
306 *
307 * Normally `llhttp` would error when a CR is not followed by LF when terminating the
308 * request line, the status line, the headers or a chunk header.
309 * With this flag only a CR is required to terminate such sections.
310 *
311 * **Enabling this flag can pose a security issue since you will be exposed to
312 * request smuggling attacks. USE WITH CAUTION!**
313 */
314 LLHTTP_EXPORT
315 void llhttp_set_lenient_optional_lf_after_cr(llhttp_t* parser, int enabled);
316
317 /*
318 * Enables/disables lenient handling of line separators.
319 *
320 * Normally `llhttp` would error when a LF is not preceded by CR when terminating the
321 * request line, the status line, the headers, a chunk header or a chunk data.
322 * With this flag only a LF is required to terminate such sections.
323 *
324 * **Enabling this flag can pose a security issue since you will be exposed to
325 * request smuggling attacks. USE WITH CAUTION!**
326 */
327 LLHTTP_EXPORT
328 void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled);
329
330 /* Enables/disables lenient handling of chunks not separated via CRLF.
331 *
332 * Normally `llhttp` would error when after a chunk data a CRLF is missing before
333 * starting a new chunk.
334 * With this flag the new chunk can start immediately after the previous one.
335 *
336 * **Enabling this flag can pose a security issue since you will be exposed to
337 * request smuggling attacks. USE WITH CAUTION!**
338 */
339 LLHTTP_EXPORT
340 void llhttp_set_lenient_optional_crlf_after_chunk(llhttp_t* parser, int enabled);
341
342 /* Enables/disables lenient handling of spaces after chunk size.
343 *
344 * Normally `llhttp` would error when after a chunk size is followed by one or more
345 * spaces are present instead of a CRLF or `;`.
346 * With this flag this check is disabled.
347 *
348 * **Enabling this flag can pose a security issue since you will be exposed to
349 * request smuggling attacks. USE WITH CAUTION!**
350 */
351 LLHTTP_EXPORT
352 void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled);
353
354 #ifdef __cplusplus
355 } /* extern "C" */
356 #endif
357 #endif /* INCLUDE_LLHTTP_API_H_ */