1
1
#ifndef INCLUDE_LLHTTP_H_
2
2
#define INCLUDE_LLHTTP_H_
3
3
4
- #define LLHTTP_VERSION_MAJOR 2
4
+ #define LLHTTP_VERSION_MAJOR 5
5
5
#define LLHTTP_VERSION_MINOR 1
6
- #define LLHTTP_VERSION_PATCH 3
6
+ #define LLHTTP_VERSION_PATCH 0
7
7
8
8
#ifndef LLHTTP_STRICT_MODE
9
9
# define LLHTTP_STRICT_MODE 0
@@ -33,10 +33,11 @@ struct llhttp__internal_s {
33
33
uint8_t http_major ;
34
34
uint8_t http_minor ;
35
35
uint8_t header_state ;
36
- uint16_t flags ;
36
+ uint8_t lenient_flags ;
37
37
uint8_t upgrade ;
38
- uint16_t status_code ;
39
38
uint8_t finish ;
39
+ uint16_t flags ;
40
+ uint16_t status_code ;
40
41
void * settings ;
41
42
};
42
43
@@ -78,7 +79,8 @@ enum llhttp_errno {
78
79
HPE_CB_CHUNK_COMPLETE = 20 ,
79
80
HPE_PAUSED = 21 ,
80
81
HPE_PAUSED_UPGRADE = 22 ,
81
- HPE_USER = 23
82
+ HPE_PAUSED_H2_UPGRADE = 23 ,
83
+ HPE_USER = 24
82
84
};
83
85
typedef enum llhttp_errno llhttp_errno_t ;
84
86
@@ -91,11 +93,17 @@ enum llhttp_flags {
91
93
F_CONTENT_LENGTH = 0x20 ,
92
94
F_SKIPBODY = 0x40 ,
93
95
F_TRAILING = 0x80 ,
94
- F_LENIENT = 0x100 ,
95
96
F_TRANSFER_ENCODING = 0x200
96
97
};
97
98
typedef enum llhttp_flags llhttp_flags_t ;
98
99
100
+ enum llhttp_lenient_flags {
101
+ LENIENT_HEADERS = 0x1 ,
102
+ LENIENT_CHUNKED_LENGTH = 0x2 ,
103
+ LENIENT_KEEP_ALIVE = 0x4
104
+ };
105
+ typedef enum llhttp_lenient_flags llhttp_lenient_flags_t ;
106
+
99
107
enum llhttp_type {
100
108
HTTP_BOTH = 0 ,
101
109
HTTP_REQUEST = 1 ,
@@ -145,7 +153,18 @@ enum llhttp_method {
145
153
HTTP_LINK = 31 ,
146
154
HTTP_UNLINK = 32 ,
147
155
HTTP_SOURCE = 33 ,
148
- HTTP_PRI = 34
156
+ HTTP_PRI = 34 ,
157
+ HTTP_DESCRIBE = 35 ,
158
+ HTTP_ANNOUNCE = 36 ,
159
+ HTTP_SETUP = 37 ,
160
+ HTTP_PLAY = 38 ,
161
+ HTTP_PAUSE = 39 ,
162
+ HTTP_TEARDOWN = 40 ,
163
+ HTTP_GET_PARAMETER = 41 ,
164
+ HTTP_SET_PARAMETER = 42 ,
165
+ HTTP_REDIRECT = 43 ,
166
+ HTTP_RECORD = 44 ,
167
+ HTTP_FLUSH = 45
149
168
};
150
169
typedef enum llhttp_method llhttp_method_t ;
151
170
@@ -173,7 +192,8 @@ typedef enum llhttp_method llhttp_method_t;
173
192
XX(20, CB_CHUNK_COMPLETE, CB_CHUNK_COMPLETE) \
174
193
XX(21, PAUSED, PAUSED) \
175
194
XX(22, PAUSED_UPGRADE, PAUSED_UPGRADE) \
176
- XX(23, USER, USER) \
195
+ XX(23, PAUSED_H2_UPGRADE, PAUSED_H2_UPGRADE) \
196
+ XX(24, USER, USER) \
177
197
178
198
179
199
#define HTTP_METHOD_MAP (XX ) \
@@ -212,6 +232,17 @@ typedef enum llhttp_method llhttp_method_t;
212
232
XX(32, UNLINK, UNLINK) \
213
233
XX(33, SOURCE, SOURCE) \
214
234
XX(34, PRI, PRI) \
235
+ XX(35, DESCRIBE, DESCRIBE) \
236
+ XX(36, ANNOUNCE, ANNOUNCE) \
237
+ XX(37, SETUP, SETUP) \
238
+ XX(38, PLAY, PLAY) \
239
+ XX(39, PAUSE, PAUSE) \
240
+ XX(40, TEARDOWN, TEARDOWN) \
241
+ XX(41, GET_PARAMETER, GET_PARAMETER) \
242
+ XX(42, SET_PARAMETER, SET_PARAMETER) \
243
+ XX(43, REDIRECT, REDIRECT) \
244
+ XX(44, RECORD, RECORD) \
245
+ XX(45, FLUSH, FLUSH) \
215
246
216
247
217
248
@@ -227,6 +258,12 @@ extern "C" {
227
258
#endif
228
259
#include <stddef.h>
229
260
261
+ #if defined(__wasm__ )
262
+ #define LLHTTP_EXPORT __attribute__((visibility("default")))
263
+ #else
264
+ #define LLHTTP_EXPORT
265
+ #endif
266
+
230
267
typedef llhttp__internal_t llhttp_t ;
231
268
typedef struct llhttp_settings_s llhttp_settings_t ;
232
269
@@ -264,13 +301,59 @@ struct llhttp_settings_s {
264
301
*/
265
302
llhttp_cb on_chunk_header ;
266
303
llhttp_cb on_chunk_complete ;
304
+
305
+ llhttp_cb on_url_complete ;
306
+ llhttp_cb on_status_complete ;
307
+ llhttp_cb on_header_field_complete ;
308
+ llhttp_cb on_header_value_complete ;
267
309
};
268
310
269
- /* Initialize the parser with specific type and user settings */
311
+ /* Initialize the parser with specific type and user settings.
312
+ *
313
+ * NOTE: lifetime of `settings` has to be at least the same as the lifetime of
314
+ * the `parser` here. In practice, `settings` has to be either a static
315
+ * variable or be allocated with `malloc`, `new`, etc.
316
+ */
317
+ LLHTTP_EXPORT
270
318
void llhttp_init (llhttp_t * parser , llhttp_type_t type ,
271
319
const llhttp_settings_t * settings );
272
320
321
+ #if defined(__wasm__ )
322
+
323
+ LLHTTP_EXPORT
324
+ llhttp_t * llhttp_alloc (llhttp_type_t type );
325
+
326
+ LLHTTP_EXPORT
327
+ void llhttp_free (llhttp_t * parser );
328
+
329
+ LLHTTP_EXPORT
330
+ uint8_t llhttp_get_type (llhttp_t * parser );
331
+
332
+ LLHTTP_EXPORT
333
+ uint8_t llhttp_get_http_major (llhttp_t * parser );
334
+
335
+ LLHTTP_EXPORT
336
+ uint8_t llhttp_get_http_minor (llhttp_t * parser );
337
+
338
+ LLHTTP_EXPORT
339
+ uint8_t llhttp_get_method (llhttp_t * parser );
340
+
341
+ LLHTTP_EXPORT
342
+ int llhttp_get_status_code (llhttp_t * parser );
343
+
344
+ LLHTTP_EXPORT
345
+ uint8_t llhttp_get_upgrade (llhttp_t * parser );
346
+
347
+ #endif // defined(__wasm__)
348
+
349
+ /* Reset an already initialized parser back to the start state, preserving the
350
+ * existing parser type, callback settings, user data, and lenient flags.
351
+ */
352
+ LLHTTP_EXPORT
353
+ void llhttp_reset (llhttp_t * parser );
354
+
273
355
/* Initialize the settings object */
356
+ LLHTTP_EXPORT
274
357
void llhttp_settings_init (llhttp_settings_t * settings );
275
358
276
359
/* Parse full or partial request/response, invoking user callbacks along the
@@ -289,6 +372,7 @@ void llhttp_settings_init(llhttp_settings_t* settings);
289
372
* to return the same error upon each successive call up until `llhttp_init()`
290
373
* is called.
291
374
*/
375
+ LLHTTP_EXPORT
292
376
llhttp_errno_t llhttp_execute (llhttp_t * parser , const char * data , size_t len );
293
377
294
378
/* This method should be called when the other side has no further bytes to
@@ -299,16 +383,19 @@ llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
299
383
* connection. This method will invoke `on_message_complete()` callback if the
300
384
* request was terminated safely. Otherwise a error code would be returned.
301
385
*/
386
+ LLHTTP_EXPORT
302
387
llhttp_errno_t llhttp_finish (llhttp_t * parser );
303
388
304
389
/* Returns `1` if the incoming message is parsed until the last byte, and has
305
390
* to be completed by calling `llhttp_finish()` on EOF
306
391
*/
392
+ LLHTTP_EXPORT
307
393
int llhttp_message_needs_eof (const llhttp_t * parser );
308
394
309
395
/* Returns `1` if there might be any other messages following the last that was
310
396
* successfully parsed.
311
397
*/
398
+ LLHTTP_EXPORT
312
399
int llhttp_should_keep_alive (const llhttp_t * parser );
313
400
314
401
/* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
@@ -317,50 +404,59 @@ int llhttp_should_keep_alive(const llhttp_t* parser);
317
404
* Important: do not call this from user callbacks! User callbacks must return
318
405
* `HPE_PAUSED` if pausing is required.
319
406
*/
407
+ LLHTTP_EXPORT
320
408
void llhttp_pause (llhttp_t * parser );
321
409
322
410
/* Might be called to resume the execution after the pause in user's callback.
323
411
* See `llhttp_execute()` above for details.
324
412
*
325
413
* Call this only if `llhttp_execute()` returns `HPE_PAUSED`.
326
414
*/
415
+ LLHTTP_EXPORT
327
416
void llhttp_resume (llhttp_t * parser );
328
417
329
418
/* Might be called to resume the execution after the pause in user's callback.
330
419
* See `llhttp_execute()` above for details.
331
420
*
332
421
* Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`
333
422
*/
423
+ LLHTTP_EXPORT
334
424
void llhttp_resume_after_upgrade (llhttp_t * parser );
335
425
336
426
/* Returns the latest return error */
427
+ LLHTTP_EXPORT
337
428
llhttp_errno_t llhttp_get_errno (const llhttp_t * parser );
338
429
339
430
/* Returns the verbal explanation of the latest returned error.
340
431
*
341
432
* Note: User callback should set error reason when returning the error. See
342
433
* `llhttp_set_error_reason()` for details.
343
434
*/
435
+ LLHTTP_EXPORT
344
436
const char * llhttp_get_error_reason (const llhttp_t * parser );
345
437
346
438
/* Assign verbal description to the returned error. Must be called in user
347
439
* callbacks right before returning the errno.
348
440
*
349
441
* Note: `HPE_USER` error code might be useful in user callbacks.
350
442
*/
443
+ LLHTTP_EXPORT
351
444
void llhttp_set_error_reason (llhttp_t * parser , const char * reason );
352
445
353
446
/* Returns the pointer to the last parsed byte before the returned error. The
354
447
* pointer is relative to the `data` argument of `llhttp_execute()`.
355
448
*
356
449
* Note: this method might be useful for counting the number of parsed bytes.
357
450
*/
451
+ LLHTTP_EXPORT
358
452
const char * llhttp_get_error_pos (const llhttp_t * parser );
359
453
360
454
/* Returns textual name of error code */
455
+ LLHTTP_EXPORT
361
456
const char * llhttp_errno_name (llhttp_errno_t err );
362
457
363
458
/* Returns textual name of HTTP method */
459
+ LLHTTP_EXPORT
364
460
const char * llhttp_method_name (llhttp_method_t method );
365
461
366
462
@@ -373,7 +469,36 @@ const char* llhttp_method_name(llhttp_method_t method);
373
469
*
374
470
* **(USE AT YOUR OWN RISK)**
375
471
*/
376
- void llhttp_set_lenient (llhttp_t * parser , int enabled );
472
+ LLHTTP_EXPORT
473
+ void llhttp_set_lenient_headers (llhttp_t * parser , int enabled );
474
+
475
+
476
+ /* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
477
+ * `Content-Length` headers (disabled by default).
478
+ *
479
+ * Normally `llhttp` would error when `Transfer-Encoding` is present in
480
+ * conjunction with `Content-Length`. This error is important to prevent HTTP
481
+ * request smuggling, but may be less desirable for small number of cases
482
+ * involving legacy servers.
483
+ *
484
+ * **(USE AT YOUR OWN RISK)**
485
+ */
486
+ LLHTTP_EXPORT
487
+ void llhttp_set_lenient_chunked_length (llhttp_t * parser , int enabled );
488
+
489
+
490
+ /* Enables/disables lenient handling of `Connection: close` and HTTP/1.0
491
+ * requests responses.
492
+ *
493
+ * Normally `llhttp` would error on (in strict mode) or discard (in loose mode)
494
+ * the HTTP request/response after the request/response with `Connection: close`
495
+ * and `Content-Length`. This is important to prevent cache poisoning attacks,
496
+ * but might interact badly with outdated and insecure clients. With this flag
497
+ * the extra request/response will be parsed normally.
498
+ *
499
+ * **(USE AT YOUR OWN RISK)**
500
+ */
501
+ void llhttp_set_lenient_keep_alive (llhttp_t * parser , int enabled );
377
502
378
503
#ifdef __cplusplus
379
504
} /* extern "C" */
0 commit comments