Commit 88a3cb51 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

Bump llhttp to 4.0.0

parent 40679cf6
# llhttp # llhttp
[![CI](https://github.com/nodejs/llhttp/workflows/CI/badge.svg)](https://github.com/nodejs/llhttp/actions?query=workflow%3ACI)
Port of [http_parser][0] to [llparse][1]. Port of [http_parser][0] to [llparse][1].
...@@ -31,7 +32,7 @@ So far llhttp outperforms http_parser: ...@@ -31,7 +32,7 @@ So far llhttp outperforms http_parser:
| | input size | bandwidth | reqs/sec | time | | | input size | bandwidth | reqs/sec | time |
|:----------------|-----------:|-------------:|-----------:|--------:| |:----------------|-----------:|-------------:|-----------:|--------:|
| **llhttp** | 8192.00 mb | 1777.24 mb/s | 3583799.39 ops/sec | 4.61 s | | **llhttp** | 8192.00 mb | 1777.24 mb/s | 3583799.39 req/sec | 4.61 s |
| **http_parser** | 8192.00 mb | 694.66 mb/s | 1406180.33 req/sec | 11.79 s | | **http_parser** | 8192.00 mb | 694.66 mb/s | 1406180.33 req/sec | 11.79 s |
llhttp is faster by approximately **156%**. llhttp is faster by approximately **156%**.
...@@ -92,6 +93,11 @@ if (err == HPE_OK) { ...@@ -92,6 +93,11 @@ if (err == HPE_OK) {
--- ---
### Bindings to other languages
* Python: [pallas/pyllhttp][8]
* Ruby: [metabahn/llhttp][9]
#### LICENSE #### LICENSE
This software is licensed under the MIT License. This software is licensed under the MIT License.
...@@ -125,3 +131,5 @@ USE OR OTHER DEALINGS IN THE SOFTWARE. ...@@ -125,3 +131,5 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
[5]: https://llvm.org/docs/LangRef.html#call-instruction [5]: https://llvm.org/docs/LangRef.html#call-instruction
[6]: https://clang.llvm.org/ [6]: https://clang.llvm.org/
[7]: https://github.com/nodejs/node [7]: https://github.com/nodejs/node
[8]: https://github.com/pallas/pyllhttp
[9]: https://github.com/metabahn/llhttp
#ifndef INCLUDE_LLHTTP_H_ #ifndef INCLUDE_LLHTTP_H_
#define INCLUDE_LLHTTP_H_ #define INCLUDE_LLHTTP_H_
#define LLHTTP_VERSION_MAJOR 2 #define LLHTTP_VERSION_MAJOR 4
#define LLHTTP_VERSION_MINOR 2 #define LLHTTP_VERSION_MINOR 0
#define LLHTTP_VERSION_PATCH 0 #define LLHTTP_VERSION_PATCH 0
#ifndef LLHTTP_STRICT_MODE #ifndef LLHTTP_STRICT_MODE
...@@ -33,10 +33,11 @@ struct llhttp__internal_s { ...@@ -33,10 +33,11 @@ struct llhttp__internal_s {
uint8_t http_major; uint8_t http_major;
uint8_t http_minor; uint8_t http_minor;
uint8_t header_state; uint8_t header_state;
uint16_t flags; uint8_t lenient_flags;
uint8_t upgrade; uint8_t upgrade;
uint16_t status_code;
uint8_t finish; uint8_t finish;
uint16_t flags;
uint16_t status_code;
void* settings; void* settings;
}; };
...@@ -91,11 +92,16 @@ enum llhttp_flags { ...@@ -91,11 +92,16 @@ enum llhttp_flags {
F_CONTENT_LENGTH = 0x20, F_CONTENT_LENGTH = 0x20,
F_SKIPBODY = 0x40, F_SKIPBODY = 0x40,
F_TRAILING = 0x80, F_TRAILING = 0x80,
F_LENIENT = 0x100,
F_TRANSFER_ENCODING = 0x200 F_TRANSFER_ENCODING = 0x200
}; };
typedef enum llhttp_flags llhttp_flags_t; typedef enum llhttp_flags llhttp_flags_t;
enum llhttp_lenient_flags {
LENIENT_HEADERS = 0x1,
LENIENT_CHUNKED_LENGTH = 0x2
};
typedef enum llhttp_lenient_flags llhttp_lenient_flags_t;
enum llhttp_type { enum llhttp_type {
HTTP_BOTH = 0, HTTP_BOTH = 0,
HTTP_REQUEST = 1, HTTP_REQUEST = 1,
...@@ -286,6 +292,11 @@ struct llhttp_settings_s { ...@@ -286,6 +292,11 @@ struct llhttp_settings_s {
*/ */
llhttp_cb on_chunk_header; llhttp_cb on_chunk_header;
llhttp_cb on_chunk_complete; llhttp_cb on_chunk_complete;
llhttp_cb on_url_complete;
llhttp_cb on_status_complete;
llhttp_cb on_header_field_complete;
llhttp_cb on_header_value_complete;
}; };
/* Initialize the parser with specific type and user settings. /* Initialize the parser with specific type and user settings.
...@@ -297,6 +308,11 @@ struct llhttp_settings_s { ...@@ -297,6 +308,11 @@ struct llhttp_settings_s {
void llhttp_init(llhttp_t* parser, llhttp_type_t type, void llhttp_init(llhttp_t* parser, llhttp_type_t type,
const llhttp_settings_t* settings); const llhttp_settings_t* settings);
/* Reset an already initialized parser back to the start state, preserving the
* existing parser type, callback settings, user data, and lenient flags.
*/
void llhttp_reset(llhttp_t* parser);
/* Initialize the settings object */ /* Initialize the settings object */
void llhttp_settings_init(llhttp_settings_t* settings); void llhttp_settings_init(llhttp_settings_t* settings);
...@@ -400,7 +416,20 @@ const char* llhttp_method_name(llhttp_method_t method); ...@@ -400,7 +416,20 @@ const char* llhttp_method_name(llhttp_method_t method);
* *
* **(USE AT YOUR OWN RISK)** * **(USE AT YOUR OWN RISK)**
*/ */
void llhttp_set_lenient(llhttp_t* parser, int enabled); void llhttp_set_lenient_headers(llhttp_t* parser, int enabled);
/* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
* `Content-Length` headers (disabled by default).
*
* Normally `llhttp` would error when `Transfer-Encoding` is present in
* conjunction with `Content-Length`. This error is important to prevent HTTP
* request smuggling, but may be less desirable for small number of cases
* involving legacy servers.
*
* **(USE AT YOUR OWN RISK)**
*/
void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#define CALLBACK_MAYBE(PARSER, NAME, ...) \ #define CALLBACK_MAYBE(PARSER, NAME, ...) \
do { \ do { \
llhttp_settings_t* settings; \ const llhttp_settings_t* settings; \
settings = (llhttp_settings_t*) (PARSER)->settings; \ settings = (const llhttp_settings_t*) (PARSER)->settings; \
if (settings == NULL || settings->NAME == NULL) { \ if (settings == NULL || settings->NAME == NULL) { \
err = 0; \ err = 0; \
break; \ break; \
...@@ -24,6 +24,21 @@ void llhttp_init(llhttp_t* parser, llhttp_type_t type, ...@@ -24,6 +24,21 @@ void llhttp_init(llhttp_t* parser, llhttp_type_t type,
} }
void llhttp_reset(llhttp_t* parser) {
llhttp_type_t type = parser->type;
const llhttp_settings_t* settings = parser->settings;
void* data = parser->data;
uint8_t lenient_flags = parser->lenient_flags;
llhttp__internal_init(parser);
parser->type = type;
parser->settings = (void*) settings;
parser->data = data;
parser->lenient_flags = lenient_flags;
}
llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len) { llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len) {
return llhttp__internal_execute(parser, data, data + len); return llhttp__internal_execute(parser, data, data + len);
} }
...@@ -127,11 +142,19 @@ const char* llhttp_method_name(llhttp_method_t method) { ...@@ -127,11 +142,19 @@ const char* llhttp_method_name(llhttp_method_t method) {
} }
void llhttp_set_lenient(llhttp_t* parser, int enabled) { void llhttp_set_lenient_headers(llhttp_t* parser, int enabled) {
if (enabled) { if (enabled) {
parser->flags |= F_LENIENT; parser->lenient_flags |= LENIENT_HEADERS;
} else { } else {
parser->flags &= ~F_LENIENT; parser->lenient_flags &= ~LENIENT_HEADERS;
}
}
void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled) {
if (enabled) {
parser->lenient_flags |= LENIENT_CHUNKED_LENGTH;
} else {
parser->lenient_flags &= ~LENIENT_CHUNKED_LENGTH;
} }
} }
...@@ -153,6 +176,13 @@ int llhttp__on_url(llhttp_t* s, const char* p, const char* endp) { ...@@ -153,6 +176,13 @@ int llhttp__on_url(llhttp_t* s, const char* p, const char* endp) {
} }
int llhttp__on_url_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_url_complete, s);
return err;
}
int llhttp__on_status(llhttp_t* s, const char* p, const char* endp) { int llhttp__on_status(llhttp_t* s, const char* p, const char* endp) {
int err; int err;
CALLBACK_MAYBE(s, on_status, s, p, endp - p); CALLBACK_MAYBE(s, on_status, s, p, endp - p);
...@@ -160,6 +190,13 @@ int llhttp__on_status(llhttp_t* s, const char* p, const char* endp) { ...@@ -160,6 +190,13 @@ int llhttp__on_status(llhttp_t* s, const char* p, const char* endp) {
} }
int llhttp__on_status_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_status_complete, s);
return err;
}
int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) { int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) {
int err; int err;
CALLBACK_MAYBE(s, on_header_field, s, p, endp - p); CALLBACK_MAYBE(s, on_header_field, s, p, endp - p);
...@@ -167,6 +204,13 @@ int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) { ...@@ -167,6 +204,13 @@ int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) {
} }
int llhttp__on_header_field_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_header_field_complete, s);
return err;
}
int llhttp__on_header_value(llhttp_t* s, const char* p, const char* endp) { int llhttp__on_header_value(llhttp_t* s, const char* p, const char* endp) {
int err; int err;
CALLBACK_MAYBE(s, on_header_value, s, p, endp - p); CALLBACK_MAYBE(s, on_header_value, s, p, endp - p);
...@@ -174,6 +218,13 @@ int llhttp__on_header_value(llhttp_t* s, const char* p, const char* endp) { ...@@ -174,6 +218,13 @@ int llhttp__on_header_value(llhttp_t* s, const char* p, const char* endp) {
} }
int llhttp__on_header_value_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_header_value_complete, s);
return err;
}
int llhttp__on_headers_complete(llhttp_t* s, const char* p, const char* endp) { int llhttp__on_headers_complete(llhttp_t* s, const char* p, const char* endp) {
int err; int err;
CALLBACK_MAYBE(s, on_headers_complete, s); CALLBACK_MAYBE(s, on_headers_complete, s);
......
...@@ -51,7 +51,8 @@ int llhttp__after_headers_complete(llhttp_t* parser, const char* p, ...@@ -51,7 +51,8 @@ int llhttp__after_headers_complete(llhttp_t* parser, const char* p,
/* chunked encoding - ignore Content-Length header, prepare for a chunk */ /* chunked encoding - ignore Content-Length header, prepare for a chunk */
return 2; return 2;
} else if (parser->flags & F_TRANSFER_ENCODING) { } else if (parser->flags & F_TRANSFER_ENCODING) {
if (parser->type == HTTP_REQUEST && (parser->flags & F_LENIENT) == 0) { if (parser->type == HTTP_REQUEST &&
(parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0) {
/* RFC 7230 3.3.3 */ /* RFC 7230 3.3.3 */
/* If a Transfer-Encoding header field /* If a Transfer-Encoding header field
...@@ -97,9 +98,7 @@ int llhttp__after_message_complete(llhttp_t* parser, const char* p, ...@@ -97,9 +98,7 @@ int llhttp__after_message_complete(llhttp_t* parser, const char* p,
should_keep_alive = llhttp_should_keep_alive(parser); should_keep_alive = llhttp_should_keep_alive(parser);
parser->finish = HTTP_FINISH_SAFE; parser->finish = HTTP_FINISH_SAFE;
parser->flags = 0;
/* Keep `F_LENIENT` flag between messages, but reset every other flag */
parser->flags &= F_LENIENT;
/* NOTE: this is ignored in loose parsing mode */ /* NOTE: this is ignored in loose parsing mode */
return should_keep_alive; return should_keep_alive;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment