Skip to content

Commit 7e2c65d

Browse files
committed
release: 5.0.0
1 parent 8787895 commit 7e2c65d

5 files changed

Lines changed: 1147 additions & 671 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# llhttp
2+
[![CI](https://github.com/nodejs/llhttp/workflows/CI/badge.svg)](https://github.com/nodejs/llhttp/actions?query=workflow%3ACI)
23

34
Port of [http_parser][0] to [llparse][1].
45

@@ -31,7 +32,7 @@ So far llhttp outperforms http_parser:
3132

3233
| | input size | bandwidth | reqs/sec | time |
3334
|:----------------|-----------:|-------------:|-----------:|--------:|
34-
| **llhttp** | 8192.00 mb | 1777.24 mb/s | 3583799.39 ops/sec | 4.61 s |
35+
| **llhttp** | 8192.00 mb | 1777.24 mb/s | 3583799.39 req/sec | 4.61 s |
3536
| **http_parser** | 8192.00 mb | 694.66 mb/s | 1406180.33 req/sec | 11.79 s |
3637

3738
llhttp is faster by approximately **156%**.
@@ -92,6 +93,11 @@ if (err == HPE_OK) {
9293
9394
---
9495
96+
### Bindings to other languages
97+
98+
* Python: [pallas/pyllhttp][8]
99+
* Ruby: [metabahn/llhttp][9]
100+
95101
#### LICENSE
96102
97103
This software is licensed under the MIT License.
@@ -125,3 +131,5 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
125131
[5]: https://llvm.org/docs/LangRef.html#call-instruction
126132
[6]: https://clang.llvm.org/
127133
[7]: https://github.com/nodejs/node
134+
[8]: https://github.com/pallas/pyllhttp
135+
[9]: https://github.com/metabahn/llhttp

include/llhttp.h

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef INCLUDE_LLHTTP_H_
22
#define INCLUDE_LLHTTP_H_
33

4-
#define LLHTTP_VERSION_MAJOR 2
5-
#define LLHTTP_VERSION_MINOR 2
6-
#define LLHTTP_VERSION_PATCH 1
4+
#define LLHTTP_VERSION_MAJOR 5
5+
#define LLHTTP_VERSION_MINOR 0
6+
#define LLHTTP_VERSION_PATCH 0
77

88
#ifndef LLHTTP_STRICT_MODE
99
# define LLHTTP_STRICT_MODE 0
@@ -33,10 +33,11 @@ struct llhttp__internal_s {
3333
uint8_t http_major;
3434
uint8_t http_minor;
3535
uint8_t header_state;
36-
uint16_t flags;
36+
uint8_t lenient_flags;
3737
uint8_t upgrade;
38-
uint16_t status_code;
3938
uint8_t finish;
39+
uint16_t flags;
40+
uint16_t status_code;
4041
void* settings;
4142
};
4243

@@ -91,11 +92,17 @@ enum llhttp_flags {
9192
F_CONTENT_LENGTH = 0x20,
9293
F_SKIPBODY = 0x40,
9394
F_TRAILING = 0x80,
94-
F_LENIENT = 0x100,
9595
F_TRANSFER_ENCODING = 0x200
9696
};
9797
typedef enum llhttp_flags llhttp_flags_t;
9898

99+
enum llhttp_lenient_flags {
100+
LENIENT_HEADERS = 0x1,
101+
LENIENT_CHUNKED_LENGTH = 0x2,
102+
LENIENT_KEEP_ALIVE = 0x4
103+
};
104+
typedef enum llhttp_lenient_flags llhttp_lenient_flags_t;
105+
99106
enum llhttp_type {
100107
HTTP_BOTH = 0,
101108
HTTP_REQUEST = 1,
@@ -249,6 +256,12 @@ extern "C" {
249256
#endif
250257
#include <stddef.h>
251258

259+
#if defined(__wasm__)
260+
#define LLHTTP_EXPORT __attribute__((visibility("default")))
261+
#else
262+
#define LLHTTP_EXPORT
263+
#endif
264+
252265
typedef llhttp__internal_t llhttp_t;
253266
typedef struct llhttp_settings_s llhttp_settings_t;
254267

@@ -286,6 +299,11 @@ struct llhttp_settings_s {
286299
*/
287300
llhttp_cb on_chunk_header;
288301
llhttp_cb on_chunk_complete;
302+
303+
llhttp_cb on_url_complete;
304+
llhttp_cb on_status_complete;
305+
llhttp_cb on_header_field_complete;
306+
llhttp_cb on_header_value_complete;
289307
};
290308

291309
/* Initialize the parser with specific type and user settings.
@@ -294,10 +312,46 @@ struct llhttp_settings_s {
294312
* the `parser` here. In practice, `settings` has to be either a static
295313
* variable or be allocated with `malloc`, `new`, etc.
296314
*/
315+
LLHTTP_EXPORT
297316
void llhttp_init(llhttp_t* parser, llhttp_type_t type,
298317
const llhttp_settings_t* settings);
299318

319+
#if defined(__wasm__)
320+
321+
LLHTTP_EXPORT
322+
llhttp_t* llhttp_alloc(llhttp_type_t type);
323+
324+
LLHTTP_EXPORT
325+
void llhttp_free(llhttp_t* parser);
326+
327+
LLHTTP_EXPORT
328+
uint8_t llhttp_get_type(llhttp_t* parser);
329+
330+
LLHTTP_EXPORT
331+
uint8_t llhttp_get_http_major(llhttp_t* parser);
332+
333+
LLHTTP_EXPORT
334+
uint8_t llhttp_get_http_minor(llhttp_t* parser);
335+
336+
LLHTTP_EXPORT
337+
uint8_t llhttp_get_method(llhttp_t* parser);
338+
339+
LLHTTP_EXPORT
340+
int llhttp_get_status_code(llhttp_t* parser);
341+
342+
LLHTTP_EXPORT
343+
uint8_t llhttp_get_upgrade(llhttp_t* parser);
344+
345+
#endif // defined(__wasm__)
346+
347+
/* Reset an already initialized parser back to the start state, preserving the
348+
* existing parser type, callback settings, user data, and lenient flags.
349+
*/
350+
LLHTTP_EXPORT
351+
void llhttp_reset(llhttp_t* parser);
352+
300353
/* Initialize the settings object */
354+
LLHTTP_EXPORT
301355
void llhttp_settings_init(llhttp_settings_t* settings);
302356

303357
/* Parse full or partial request/response, invoking user callbacks along the
@@ -316,6 +370,7 @@ void llhttp_settings_init(llhttp_settings_t* settings);
316370
* to return the same error upon each successive call up until `llhttp_init()`
317371
* is called.
318372
*/
373+
LLHTTP_EXPORT
319374
llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
320375

321376
/* This method should be called when the other side has no further bytes to
@@ -326,16 +381,19 @@ llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
326381
* connection. This method will invoke `on_message_complete()` callback if the
327382
* request was terminated safely. Otherwise a error code would be returned.
328383
*/
384+
LLHTTP_EXPORT
329385
llhttp_errno_t llhttp_finish(llhttp_t* parser);
330386

331387
/* Returns `1` if the incoming message is parsed until the last byte, and has
332388
* to be completed by calling `llhttp_finish()` on EOF
333389
*/
390+
LLHTTP_EXPORT
334391
int llhttp_message_needs_eof(const llhttp_t* parser);
335392

336393
/* Returns `1` if there might be any other messages following the last that was
337394
* successfully parsed.
338395
*/
396+
LLHTTP_EXPORT
339397
int llhttp_should_keep_alive(const llhttp_t* parser);
340398

341399
/* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
@@ -344,50 +402,59 @@ int llhttp_should_keep_alive(const llhttp_t* parser);
344402
* Important: do not call this from user callbacks! User callbacks must return
345403
* `HPE_PAUSED` if pausing is required.
346404
*/
405+
LLHTTP_EXPORT
347406
void llhttp_pause(llhttp_t* parser);
348407

349408
/* Might be called to resume the execution after the pause in user's callback.
350409
* See `llhttp_execute()` above for details.
351410
*
352411
* Call this only if `llhttp_execute()` returns `HPE_PAUSED`.
353412
*/
413+
LLHTTP_EXPORT
354414
void llhttp_resume(llhttp_t* parser);
355415

356416
/* Might be called to resume the execution after the pause in user's callback.
357417
* See `llhttp_execute()` above for details.
358418
*
359419
* Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`
360420
*/
421+
LLHTTP_EXPORT
361422
void llhttp_resume_after_upgrade(llhttp_t* parser);
362423

363424
/* Returns the latest return error */
425+
LLHTTP_EXPORT
364426
llhttp_errno_t llhttp_get_errno(const llhttp_t* parser);
365427

366428
/* Returns the verbal explanation of the latest returned error.
367429
*
368430
* Note: User callback should set error reason when returning the error. See
369431
* `llhttp_set_error_reason()` for details.
370432
*/
433+
LLHTTP_EXPORT
371434
const char* llhttp_get_error_reason(const llhttp_t* parser);
372435

373436
/* Assign verbal description to the returned error. Must be called in user
374437
* callbacks right before returning the errno.
375438
*
376439
* Note: `HPE_USER` error code might be useful in user callbacks.
377440
*/
441+
LLHTTP_EXPORT
378442
void llhttp_set_error_reason(llhttp_t* parser, const char* reason);
379443

380444
/* Returns the pointer to the last parsed byte before the returned error. The
381445
* pointer is relative to the `data` argument of `llhttp_execute()`.
382446
*
383447
* Note: this method might be useful for counting the number of parsed bytes.
384448
*/
449+
LLHTTP_EXPORT
385450
const char* llhttp_get_error_pos(const llhttp_t* parser);
386451

387452
/* Returns textual name of error code */
453+
LLHTTP_EXPORT
388454
const char* llhttp_errno_name(llhttp_errno_t err);
389455

390456
/* Returns textual name of HTTP method */
457+
LLHTTP_EXPORT
391458
const char* llhttp_method_name(llhttp_method_t method);
392459

393460

@@ -400,7 +467,36 @@ const char* llhttp_method_name(llhttp_method_t method);
400467
*
401468
* **(USE AT YOUR OWN RISK)**
402469
*/
403-
void llhttp_set_lenient(llhttp_t* parser, int enabled);
470+
LLHTTP_EXPORT
471+
void llhttp_set_lenient_headers(llhttp_t* parser, int enabled);
472+
473+
474+
/* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
475+
* `Content-Length` headers (disabled by default).
476+
*
477+
* Normally `llhttp` would error when `Transfer-Encoding` is present in
478+
* conjunction with `Content-Length`. This error is important to prevent HTTP
479+
* request smuggling, but may be less desirable for small number of cases
480+
* involving legacy servers.
481+
*
482+
* **(USE AT YOUR OWN RISK)**
483+
*/
484+
LLHTTP_EXPORT
485+
void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
486+
487+
488+
/* Enables/disables lenient handling of `Connection: close` and HTTP/1.0
489+
* requests responses.
490+
*
491+
* Normally `llhttp` would error on (in strict mode) or discard (in loose mode)
492+
* the HTTP request/response after the request/response with `Connection: close`
493+
* and `Content-Length`. This is important to prevent cache poisoning attacks,
494+
* but might interact badly with outdated and insecure clients. With this flag
495+
* the extra request/response will be parsed normally.
496+
*
497+
* **(USE AT YOUR OWN RISK)**
498+
*/
499+
void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled);
404500

405501
#ifdef __cplusplus
406502
} /* extern "C" */

0 commit comments

Comments
 (0)