Merge pull request #1827 from nghttp2/bump-llhttp

Bump llhttp
This commit is contained in:
Tatsuhiro Tsujikawa 2022-11-09 18:48:52 +09:00 committed by GitHub
commit e91de24be6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 5197 additions and 1789 deletions

View File

@ -150,13 +150,26 @@ constexpr llhttp_settings_t htp_hooks = {
htp_msg_begincb, // llhttp_cb on_message_begin;
nullptr, // llhttp_data_cb on_url;
htp_statuscb, // llhttp_data_cb on_status;
nullptr, // llhttp_data_cb on_method;
nullptr, // llhttp_data_cb on_version;
htp_hdr_keycb, // llhttp_data_cb on_header_field;
htp_hdr_valcb, // llhttp_data_cb on_header_value;
nullptr, // llhttp_data_cb on_chunk_extension_name;
nullptr, // llhttp_data_cb on_chunk_extension_value;
htp_hdrs_completecb, // llhttp_cb on_headers_complete;
htp_body_cb, // llhttp_data_cb on_body;
htp_msg_completecb, // llhttp_cb on_message_complete;
nullptr, // llhttp_cb on_chunk_header
nullptr, // llhttp_cb on_chunk_complete
nullptr, // llhttp_cb on_url_complete;
nullptr, // llhttp_cb on_status_complete;
nullptr, // llhttp_cb on_method_complete;
nullptr, // llhttp_cb on_version_complete;
nullptr, // llhttp_cb on_header_field_complete;
nullptr, // llhttp_cb on_header_value_complete;
nullptr, // llhttp_cb on_chunk_extension_name_complete;
nullptr, // llhttp_cb on_chunk_extension_value_complete;
nullptr, // llhttp_cb on_chunk_header;
nullptr, // llhttp_cb on_chunk_complete;
nullptr, // llhttp_cb on_reset;
};
} // namespace

View File

@ -426,13 +426,26 @@ constexpr llhttp_settings_t htp_hooks = {
htp_msg_begincb, // llhttp_cb on_message_begin;
nullptr, // llhttp_data_cb on_url;
nullptr, // llhttp_data_cb on_status;
nullptr, // llhttp_data_cb on_method;
nullptr, // llhttp_data_cb on_version;
nullptr, // llhttp_data_cb on_header_field;
nullptr, // llhttp_data_cb on_header_value;
nullptr, // llhttp_data_cb on_chunk_extension_name;
nullptr, // llhttp_data_cb on_chunk_extension_value;
nullptr, // llhttp_cb on_headers_complete;
nullptr, // llhttp_data_cb on_body;
htp_msg_completecb, // llhttp_cb on_message_complete;
nullptr, // llhttp_cb on_chunk_header
nullptr, // llhttp_cb on_chunk_complete
nullptr, // llhttp_cb on_url_complete;
nullptr, // llhttp_cb on_status_complete;
nullptr, // llhttp_cb on_method_complete;
nullptr, // llhttp_cb on_version_complete;
nullptr, // llhttp_cb on_header_field_complete;
nullptr, // llhttp_cb on_header_value_complete;
nullptr, // llhttp_cb on_chunk_extension_name_complete;
nullptr, // llhttp_cb on_chunk_extension_value_complete;
nullptr, // llhttp_cb on_chunk_header;
nullptr, // llhttp_cb on_chunk_complete;
nullptr, // llhttp_cb on_reset;
};
} // namespace

View File

@ -345,13 +345,26 @@ constexpr llhttp_settings_t htp_hooks = {
nullptr, // llhttp_cb on_message_begin;
nullptr, // llhttp_data_cb on_url;
nullptr, // llhttp_data_cb on_status;
nullptr, // llhttp_data_cb on_method;
nullptr, // llhttp_data_cb on_version;
nullptr, // llhttp_data_cb on_header_field;
nullptr, // llhttp_data_cb on_header_value;
nullptr, // llhttp_data_cb on_chunk_extension_name;
nullptr, // llhttp_data_cb on_chunk_extension_value;
htp_hdrs_completecb, // llhttp_cb on_headers_complete;
nullptr, // llhttp_data_cb on_body;
nullptr, // llhttp_cb on_message_complete;
nullptr, // llhttp_cb on_chunk_header
nullptr, // llhttp_cb on_chunk_complete
nullptr, // llhttp_cb on_url_complete;
nullptr, // llhttp_cb on_status_complete;
nullptr, // llhttp_cb on_method_complete;
nullptr, // llhttp_cb on_version_complete;
nullptr, // llhttp_cb on_header_field_complete;
nullptr, // llhttp_cb on_header_value_complete;
nullptr, // llhttp_cb on_chunk_extension_name_complete;
nullptr, // llhttp_cb on_chunk_extension_value_complete;
nullptr, // llhttp_cb on_chunk_header;
nullptr, // llhttp_cb on_chunk_complete;
nullptr, // llhttp_cb on_reset;
};
} // namespace

View File

@ -266,13 +266,26 @@ constexpr llhttp_settings_t htp_hooks = {
htp_msg_begincb, // llhttp_cb on_message_begin;
nullptr, // llhttp_data_cb on_url;
nullptr, // llhttp_data_cb on_status;
nullptr, // llhttp_data_cb on_method;
nullptr, // llhttp_data_cb on_version;
htp_hdr_keycb, // llhttp_data_cb on_header_field;
htp_hdr_valcb, // llhttp_data_cb on_header_value;
nullptr, // llhttp_data_cb on_chunk_extension_name;
nullptr, // llhttp_data_cb on_chunk_extension_value;
htp_hdrs_completecb, // llhttp_cb on_headers_complete;
htp_bodycb, // llhttp_data_cb on_body;
htp_msg_completecb, // llhttp_cb on_message_complete;
nullptr, // llhttp_cb on_chunk_header
nullptr, // llhttp_cb on_chunk_complete
nullptr, // llhttp_cb on_url_complete;
nullptr, // llhttp_cb on_status_complete;
nullptr, // llhttp_cb on_method_complete;
nullptr, // llhttp_cb on_version_complete;
nullptr, // llhttp_cb on_header_field_complete;
nullptr, // llhttp_cb on_header_value_complete;
nullptr, // llhttp_cb on_chunk_extension_name_complete;
nullptr, // llhttp_cb on_chunk_extension_value_complete;
nullptr, // llhttp_cb on_chunk_header;
nullptr, // llhttp_cb on_chunk_complete;
nullptr, // llhttp_cb on_reset;
};
} // namespace

View File

@ -66,13 +66,26 @@ constexpr llhttp_settings_t htp_hooks = {
htp_msg_begin, // llhttp_cb on_message_begin;
htp_uricb, // llhttp_data_cb on_url;
nullptr, // llhttp_data_cb on_status;
nullptr, // llhttp_data_cb on_method;
nullptr, // llhttp_data_cb on_version;
htp_hdr_keycb, // llhttp_data_cb on_header_field;
htp_hdr_valcb, // llhttp_data_cb on_header_value;
nullptr, // llhttp_data_cb on_chunk_extension_name;
nullptr, // llhttp_data_cb on_chunk_extension_value;
htp_hdrs_completecb, // llhttp_cb on_headers_complete;
htp_bodycb, // llhttp_data_cb on_body;
htp_msg_completecb, // llhttp_cb on_message_complete;
nullptr, // llhttp_cb on_url_complete;
nullptr, // llhttp_cb on_status_complete;
nullptr, // llhttp_cb on_method_complete;
nullptr, // llhttp_cb on_version_complete;
nullptr, // llhttp_cb on_header_field_complete;
nullptr, // llhttp_cb on_header_value_complete;
nullptr, // llhttp_cb on_chunk_extension_name_complete;
nullptr, // llhttp_cb on_chunk_extension_value_complete;
nullptr, // llhttp_cb on_chunk_header;
nullptr, // llhttp_cb on_chunk_complete;
nullptr, // llhttp_cb on_reset;
};
} // namespace

View File

@ -92,6 +92,239 @@ if (err == HPE_OK) {
```
For more information on API usage, please refer to [src/native/api.h](https://github.com/nodejs/llhttp/blob/main/src/native/api.h).
## API
### llhttp_settings_t
The settings object contains a list of callbacks that the parser will invoke.
The following callbacks can return `0` (proceed normally), `-1` (error) or `HPE_PAUSED` (pause the parser):
* `on_message_begin`: Invoked when a new request/response starts.
* `on_message_complete`: Invoked when a request/response has been completedly parsed.
* `on_url_complete`: Invoked after the URL has been parsed.
* `on_method_complete`: Invoked after the HTTP method has been parsed.
* `on_version_complete`: Invoked after the HTTP version has been parsed.
* `on_status_complete`: Invoked after the status code has been parsed.
* `on_header_field_complete`: Invoked after a header name has been parsed.
* `on_header_value_complete`: Invoked after a header value has been parsed.
* `on_chunk_header`: Invoked after a new chunk is started. The current chunk length is stored in `parser->content_length`.
* `on_chunk_extension_name_complete`: Invoked after a chunk extension name is started.
* `on_chunk_extension_value_complete`: Invoked after a chunk extension value is started.
* `on_chunk_complete`: Invoked after a new chunk is received.
* `on_reset`: Invoked after `on_message_complete` and before `on_message_begin` when a new message
is received on the same parser. This is not invoked for the first message of the parser.
The following callbacks can return `0` (proceed normally), `-1` (error) or `HPE_USER` (error from the callback):
* `on_url`: Invoked when another character of the URL is received.
* `on_status`: Invoked when another character of the status is received.
* `on_method`: Invoked when another character of the method is received.
When parser is created with `HTTP_BOTH` and the input is a response, this also invoked for the sequence `HTTP/`
of the first message.
* `on_version`: Invoked when another character of the version is received.
* `on_header_field`: Invoked when another character of a header name is received.
* `on_header_value`: Invoked when another character of a header value is received.
* `on_chunk_extension_name`: Invoked when another character of a chunk extension name is received.
* `on_chunk_extension_value`: Invoked when another character of a extension value is received.
The callback `on_headers_complete`, invoked when headers are completed, can return:
* `0`: Proceed normally.
* `1`: Assume that request/response has no body, and proceed to parsing the next message.
* `2`: Assume absence of body (as above) and make `llhttp_execute()` return `HPE_PAUSED_UPGRADE`.
* `-1`: Error
* `HPE_PAUSED`: Pause the parser.
### `void llhttp_init(llhttp_t* parser, llhttp_type_t type, const llhttp_settings_t* settings)`
Initialize the parser with specific type and user settings.
### `uint8_t llhttp_get_type(llhttp_t* parser)`
Returns the type of the parser.
### `uint8_t llhttp_get_http_major(llhttp_t* parser)`
Returns the major version of the HTTP protocol of the current request/response.
### `uint8_t llhttp_get_http_minor(llhttp_t* parser)`
Returns the minor version of the HTTP protocol of the current request/response.
### `uint8_t llhttp_get_method(llhttp_t* parser)`
Returns the method of the current request.
### `int llhttp_get_status_code(llhttp_t* parser)`
Returns the method of the current response.
### `uint8_t llhttp_get_upgrade(llhttp_t* parser)`
Returns `1` if request includes the `Connection: upgrade` header.
### `void llhttp_reset(llhttp_t* parser)`
Reset an already initialized parser back to the start state, preserving the
existing parser type, callback settings, user data, and lenient flags.
### `void llhttp_settings_init(llhttp_settings_t* settings)`
Initialize the settings object.
### `llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len)`
Parse full or partial request/response, invoking user callbacks along the way.
If any of `llhttp_data_cb` returns errno not equal to `HPE_OK` - the parsing interrupts,
and such errno is returned from `llhttp_execute()`. If `HPE_PAUSED` was used as a errno,
the execution can be resumed with `llhttp_resume()` call.
In a special case of CONNECT/Upgrade request/response `HPE_PAUSED_UPGRADE` is returned
after fully parsing the request/response. If the user wishes to continue parsing,
they need to invoke `llhttp_resume_after_upgrade()`.
**if this function ever returns a non-pause type error, it will continue to return
the same error upon each successive call up until `llhttp_init()` is called.**
### `llhttp_errno_t llhttp_finish(llhttp_t* parser)`
This method should be called when the other side has no further bytes to
send (e.g. shutdown of readable side of the TCP connection.)
Requests without `Content-Length` and other messages might require treating
all incoming bytes as the part of the body, up to the last byte of the
connection.
This method will invoke `on_message_complete()` callback if the
request was terminated safely. Otherwise a error code would be returned.
### `int llhttp_message_needs_eof(const llhttp_t* parser)`
Returns `1` if the incoming message is parsed until the last byte, and has to be completed by calling `llhttp_finish()` on EOF.
### `int llhttp_should_keep_alive(const llhttp_t* parser)`
Returns `1` if there might be any other messages following the last that was
successfully parsed.
### `void llhttp_pause(llhttp_t* parser)`
Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
appropriate error reason.
**Do not call this from user callbacks! User callbacks must return
`HPE_PAUSED` if pausing is required.**
### `void llhttp_resume(llhttp_t* parser)`
Might be called to resume the execution after the pause in user's callback.
See `llhttp_execute()` above for details.
**Call this only if `llhttp_execute()` returns `HPE_PAUSED`.**
### `void llhttp_resume_after_upgrade(llhttp_t* parser)`
Might be called to resume the execution after the pause in user's callback.
See `llhttp_execute()` above for details.
**Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`**
### `llhttp_errno_t llhttp_get_errno(const llhttp_t* parser)`
Returns the latest error.
### `const char* llhttp_get_error_reason(const llhttp_t* parser)`
Returns the verbal explanation of the latest returned error.
**User callback should set error reason when returning the error. See
`llhttp_set_error_reason()` for details.**
### `void llhttp_set_error_reason(llhttp_t* parser, const char* reason)`
Assign verbal description to the returned error. Must be called in user
callbacks right before returning the errno.
**`HPE_USER` error code might be useful in user callbacks.**
### `const char* llhttp_get_error_pos(const llhttp_t* parser)`
Returns the pointer to the last parsed byte before the returned error. The
pointer is relative to the `data` argument of `llhttp_execute()`.
**This method might be useful for counting the number of parsed bytes.**
### `const char* llhttp_errno_name(llhttp_errno_t err)`
Returns textual name of error code.
### `const char* llhttp_method_name(llhttp_method_t method)`
Returns textual name of HTTP method.
### `const char* llhttp_status_name(llhttp_status_t status)`
Returns textual name of HTTP status.
### `void llhttp_set_lenient_headers(llhttp_t* parser, int enabled)`
Enables/disables lenient header value parsing (disabled by default).
Lenient parsing disables header value token checks, extending llhttp's
protocol support to highly non-compliant clients/server.
No `HPE_INVALID_HEADER_TOKEN` will be raised for incorrect header values when
lenient parsing is "on".
**USE AT YOUR OWN RISK!**
### `void llhttp_set_lenient_chunked_length(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_keep_alive(llhttp_t* parser, int enabled)`
Enables/disables lenient handling of `Connection: close` and HTTP/1.0
requests responses.
Normally `llhttp` would error on (in strict mode) or discard (in loose mode)
the HTTP request/response after the request/response with `Connection: close`
and `Content-Length`.
This is important to prevent cache poisoning attacks,
but might interact badly with outdated and insecure clients.
With this flag the extra request/response will be parsed normally.
**USE AT YOUR OWN RISK!**
### `void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled)`
Enables/disables lenient handling of `Transfer-Encoding` header.
Normally `llhttp` would error when a `Transfer-Encoding` has `chunked` value
and another value after it (either in a single header or in multiple
headers whose value are internally joined using `, `).
This is mandated by the spec to reliably determine request body size and thus
avoid request smuggling.
With this flag the extra value will be parsed normally.
**USE AT YOUR OWN RISK!**
## Build Instructions
Make sure you have [Node.js](https://nodejs.org/), npm and npx installed. Then under project directory run:
@ -105,8 +338,10 @@ make
### Bindings to other languages
* Lua: [MunifTanjim/llhttp.lua][11]
* Python: [pallas/pyllhttp][8]
* Ruby: [metabahn/llhttp][9]
* Rust: [JackLiar/rust-llhttp][10]
### Using with CMake
@ -114,7 +349,7 @@ If you want to use this library in a CMake project you can use the snippet below
```
FetchContent_Declare(llhttp
URL "https://github.com/nodejs/llhttp/releases/download/v6.0.5/llhttp-release-v6.0.5.tar.gz") # Using version 6.0.5
URL "https://github.com/nodejs/llhttp/archive/refs/tags/v6.0.5.tar.gz") # Using version 6.0.5
FetchContent_MakeAvailable(llhttp)
@ -178,3 +413,5 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
[7]: https://github.com/nodejs/node
[8]: https://github.com/pallas/pyllhttp
[9]: https://github.com/metabahn/llhttp
[10]: https://github.com/JackLiar/rust-llhttp
[11]: https://github.com/MunifTanjim/llhttp.lua

View File

@ -1,9 +1,9 @@
#ifndef INCLUDE_LLHTTP_H_
#define INCLUDE_LLHTTP_H_
#define LLHTTP_VERSION_MAJOR 6
#define LLHTTP_VERSION_MINOR 0
#define LLHTTP_VERSION_PATCH 7
#define LLHTTP_VERSION_MAJOR 8
#define LLHTTP_VERSION_MINOR 1
#define LLHTTP_VERSION_PATCH 0
#ifndef LLHTTP_STRICT_MODE
# define LLHTTP_STRICT_MODE 0
@ -38,6 +38,7 @@ struct llhttp__internal_s {
uint8_t finish;
uint16_t flags;
uint16_t status_code;
uint8_t initial_message_completed;
void* settings;
};
@ -62,6 +63,7 @@ enum llhttp_errno {
HPE_CR_EXPECTED = 25,
HPE_LF_EXPECTED = 3,
HPE_UNEXPECTED_CONTENT_LENGTH = 4,
HPE_UNEXPECTED_SPACE = 30,
HPE_CLOSED_CONNECTION = 5,
HPE_INVALID_METHOD = 6,
HPE_INVALID_URL = 7,
@ -81,7 +83,16 @@ enum llhttp_errno {
HPE_PAUSED = 21,
HPE_PAUSED_UPGRADE = 22,
HPE_PAUSED_H2_UPGRADE = 23,
HPE_USER = 24
HPE_USER = 24,
HPE_CB_URL_COMPLETE = 26,
HPE_CB_STATUS_COMPLETE = 27,
HPE_CB_METHOD_COMPLETE = 32,
HPE_CB_VERSION_COMPLETE = 33,
HPE_CB_HEADER_FIELD_COMPLETE = 28,
HPE_CB_HEADER_VALUE_COMPLETE = 29,
HPE_CB_CHUNK_EXTENSION_NAME_COMPLETE = 34,
HPE_CB_CHUNK_EXTENSION_VALUE_COMPLETE = 35,
HPE_CB_RESET = 31
};
typedef enum llhttp_errno llhttp_errno_t;
@ -102,7 +113,8 @@ enum llhttp_lenient_flags {
LENIENT_HEADERS = 0x1,
LENIENT_CHUNKED_LENGTH = 0x2,
LENIENT_KEEP_ALIVE = 0x4,
LENIENT_TRANSFER_ENCODING = 0x8
LENIENT_TRANSFER_ENCODING = 0x8,
LENIENT_VERSION = 0x10
};
typedef enum llhttp_lenient_flags llhttp_lenient_flags_t;
@ -170,6 +182,109 @@ enum llhttp_method {
};
typedef enum llhttp_method llhttp_method_t;
enum llhttp_status {
HTTP_STATUS_CONTINUE = 100,
HTTP_STATUS_SWITCHING_PROTOCOLS = 101,
HTTP_STATUS_PROCESSING = 102,
HTTP_STATUS_EARLY_HINTS = 103,
HTTP_STATUS_RESPONSE_IS_STALE = 110,
HTTP_STATUS_REVALIDATION_FAILED = 111,
HTTP_STATUS_DISCONNECTED_OPERATION = 112,
HTTP_STATUS_HEURISTIC_EXPIRATION = 113,
HTTP_STATUS_MISCELLANEOUS_WARNING = 199,
HTTP_STATUS_OK = 200,
HTTP_STATUS_CREATED = 201,
HTTP_STATUS_ACCEPTED = 202,
HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,
HTTP_STATUS_NO_CONTENT = 204,
HTTP_STATUS_RESET_CONTENT = 205,
HTTP_STATUS_PARTIAL_CONTENT = 206,
HTTP_STATUS_MULTI_STATUS = 207,
HTTP_STATUS_ALREADY_REPORTED = 208,
HTTP_STATUS_TRANSFORMATION_APPLIED = 214,
HTTP_STATUS_IM_USED = 226,
HTTP_STATUS_MISCELLANEOUS_PERSISTENT_WARNING = 299,
HTTP_STATUS_MULTIPLE_CHOICES = 300,
HTTP_STATUS_MOVED_PERMANENTLY = 301,
HTTP_STATUS_FOUND = 302,
HTTP_STATUS_SEE_OTHER = 303,
HTTP_STATUS_NOT_MODIFIED = 304,
HTTP_STATUS_USE_PROXY = 305,
HTTP_STATUS_SWITCH_PROXY = 306,
HTTP_STATUS_TEMPORARY_REDIRECT = 307,
HTTP_STATUS_PERMANENT_REDIRECT = 308,
HTTP_STATUS_BAD_REQUEST = 400,
HTTP_STATUS_UNAUTHORIZED = 401,
HTTP_STATUS_PAYMENT_REQUIRED = 402,
HTTP_STATUS_FORBIDDEN = 403,
HTTP_STATUS_NOT_FOUND = 404,
HTTP_STATUS_METHOD_NOT_ALLOWED = 405,
HTTP_STATUS_NOT_ACCEPTABLE = 406,
HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,
HTTP_STATUS_REQUEST_TIMEOUT = 408,
HTTP_STATUS_CONFLICT = 409,
HTTP_STATUS_GONE = 410,
HTTP_STATUS_LENGTH_REQUIRED = 411,
HTTP_STATUS_PRECONDITION_FAILED = 412,
HTTP_STATUS_PAYLOAD_TOO_LARGE = 413,
HTTP_STATUS_URI_TOO_LONG = 414,
HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
HTTP_STATUS_RANGE_NOT_SATISFIABLE = 416,
HTTP_STATUS_EXPECTATION_FAILED = 417,
HTTP_STATUS_IM_A_TEAPOT = 418,
HTTP_STATUS_PAGE_EXPIRED = 419,
HTTP_STATUS_ENHANCE_YOUR_CALM = 420,
HTTP_STATUS_MISDIRECTED_REQUEST = 421,
HTTP_STATUS_UNPROCESSABLE_ENTITY = 422,
HTTP_STATUS_LOCKED = 423,
HTTP_STATUS_FAILED_DEPENDENCY = 424,
HTTP_STATUS_TOO_EARLY = 425,
HTTP_STATUS_UPGRADE_REQUIRED = 426,
HTTP_STATUS_PRECONDITION_REQUIRED = 428,
HTTP_STATUS_TOO_MANY_REQUESTS = 429,
HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL = 430,
HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
HTTP_STATUS_LOGIN_TIMEOUT = 440,
HTTP_STATUS_NO_RESPONSE = 444,
HTTP_STATUS_RETRY_WITH = 449,
HTTP_STATUS_BLOCKED_BY_PARENTAL_CONTROL = 450,
HTTP_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
HTTP_STATUS_CLIENT_CLOSED_LOAD_BALANCED_REQUEST = 460,
HTTP_STATUS_INVALID_X_FORWARDED_FOR = 463,
HTTP_STATUS_REQUEST_HEADER_TOO_LARGE = 494,
HTTP_STATUS_SSL_CERTIFICATE_ERROR = 495,
HTTP_STATUS_SSL_CERTIFICATE_REQUIRED = 496,
HTTP_STATUS_HTTP_REQUEST_SENT_TO_HTTPS_PORT = 497,
HTTP_STATUS_INVALID_TOKEN = 498,
HTTP_STATUS_CLIENT_CLOSED_REQUEST = 499,
HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
HTTP_STATUS_NOT_IMPLEMENTED = 501,
HTTP_STATUS_BAD_GATEWAY = 502,
HTTP_STATUS_SERVICE_UNAVAILABLE = 503,
HTTP_STATUS_GATEWAY_TIMEOUT = 504,
HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,
HTTP_STATUS_VARIANT_ALSO_NEGOTIATES = 506,
HTTP_STATUS_INSUFFICIENT_STORAGE = 507,
HTTP_STATUS_LOOP_DETECTED = 508,
HTTP_STATUS_BANDWIDTH_LIMIT_EXCEEDED = 509,
HTTP_STATUS_NOT_EXTENDED = 510,
HTTP_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511,
HTTP_STATUS_WEB_SERVER_UNKNOWN_ERROR = 520,
HTTP_STATUS_WEB_SERVER_IS_DOWN = 521,
HTTP_STATUS_CONNECTION_TIMEOUT = 522,
HTTP_STATUS_ORIGIN_IS_UNREACHABLE = 523,
HTTP_STATUS_TIMEOUT_OCCURED = 524,
HTTP_STATUS_SSL_HANDSHAKE_FAILED = 525,
HTTP_STATUS_INVALID_SSL_CERTIFICATE = 526,
HTTP_STATUS_RAILGUN_ERROR = 527,
HTTP_STATUS_SITE_IS_OVERLOADED = 529,
HTTP_STATUS_SITE_IS_FROZEN = 530,
HTTP_STATUS_IDENTITY_PROVIDER_AUTHENTICATION_ERROR = 561,
HTTP_STATUS_NETWORK_READ_TIMEOUT = 598,
HTTP_STATUS_NETWORK_CONNECT_TIMEOUT = 599
};
typedef enum llhttp_status llhttp_status_t;
#define HTTP_ERRNO_MAP(XX) \
XX(0, OK, OK) \
XX(1, INTERNAL, INTERNAL) \
@ -177,6 +292,7 @@ typedef enum llhttp_method llhttp_method_t;
XX(25, CR_EXPECTED, CR_EXPECTED) \
XX(3, LF_EXPECTED, LF_EXPECTED) \
XX(4, UNEXPECTED_CONTENT_LENGTH, UNEXPECTED_CONTENT_LENGTH) \
XX(30, UNEXPECTED_SPACE, UNEXPECTED_SPACE) \
XX(5, CLOSED_CONNECTION, CLOSED_CONNECTION) \
XX(6, INVALID_METHOD, INVALID_METHOD) \
XX(7, INVALID_URL, INVALID_URL) \
@ -197,6 +313,15 @@ typedef enum llhttp_method llhttp_method_t;
XX(22, PAUSED_UPGRADE, PAUSED_UPGRADE) \
XX(23, PAUSED_H2_UPGRADE, PAUSED_H2_UPGRADE) \
XX(24, USER, USER) \
XX(26, CB_URL_COMPLETE, CB_URL_COMPLETE) \
XX(27, CB_STATUS_COMPLETE, CB_STATUS_COMPLETE) \
XX(32, CB_METHOD_COMPLETE, CB_METHOD_COMPLETE) \
XX(33, CB_VERSION_COMPLETE, CB_VERSION_COMPLETE) \
XX(28, CB_HEADER_FIELD_COMPLETE, CB_HEADER_FIELD_COMPLETE) \
XX(29, CB_HEADER_VALUE_COMPLETE, CB_HEADER_VALUE_COMPLETE) \
XX(34, CB_CHUNK_EXTENSION_NAME_COMPLETE, CB_CHUNK_EXTENSION_NAME_COMPLETE) \
XX(35, CB_CHUNK_EXTENSION_VALUE_COMPLETE, CB_CHUNK_EXTENSION_VALUE_COMPLETE) \
XX(31, CB_RESET, CB_RESET) \
#define HTTP_METHOD_MAP(XX) \
@ -302,6 +427,108 @@ typedef enum llhttp_method llhttp_method_t;
XX(45, FLUSH, FLUSH) \
#define HTTP_STATUS_MAP(XX) \
XX(100, CONTINUE, CONTINUE) \
XX(101, SWITCHING_PROTOCOLS, SWITCHING_PROTOCOLS) \
XX(102, PROCESSING, PROCESSING) \
XX(103, EARLY_HINTS, EARLY_HINTS) \
XX(110, RESPONSE_IS_STALE, RESPONSE_IS_STALE) \
XX(111, REVALIDATION_FAILED, REVALIDATION_FAILED) \
XX(112, DISCONNECTED_OPERATION, DISCONNECTED_OPERATION) \
XX(113, HEURISTIC_EXPIRATION, HEURISTIC_EXPIRATION) \
XX(199, MISCELLANEOUS_WARNING, MISCELLANEOUS_WARNING) \
XX(200, OK, OK) \
XX(201, CREATED, CREATED) \
XX(202, ACCEPTED, ACCEPTED) \
XX(203, NON_AUTHORITATIVE_INFORMATION, NON_AUTHORITATIVE_INFORMATION) \
XX(204, NO_CONTENT, NO_CONTENT) \
XX(205, RESET_CONTENT, RESET_CONTENT) \
XX(206, PARTIAL_CONTENT, PARTIAL_CONTENT) \
XX(207, MULTI_STATUS, MULTI_STATUS) \
XX(208, ALREADY_REPORTED, ALREADY_REPORTED) \
XX(214, TRANSFORMATION_APPLIED, TRANSFORMATION_APPLIED) \
XX(226, IM_USED, IM_USED) \
XX(299, MISCELLANEOUS_PERSISTENT_WARNING, MISCELLANEOUS_PERSISTENT_WARNING) \
XX(300, MULTIPLE_CHOICES, MULTIPLE_CHOICES) \
XX(301, MOVED_PERMANENTLY, MOVED_PERMANENTLY) \
XX(302, FOUND, FOUND) \
XX(303, SEE_OTHER, SEE_OTHER) \
XX(304, NOT_MODIFIED, NOT_MODIFIED) \
XX(305, USE_PROXY, USE_PROXY) \
XX(306, SWITCH_PROXY, SWITCH_PROXY) \
XX(307, TEMPORARY_REDIRECT, TEMPORARY_REDIRECT) \
XX(308, PERMANENT_REDIRECT, PERMANENT_REDIRECT) \
XX(400, BAD_REQUEST, BAD_REQUEST) \
XX(401, UNAUTHORIZED, UNAUTHORIZED) \
XX(402, PAYMENT_REQUIRED, PAYMENT_REQUIRED) \
XX(403, FORBIDDEN, FORBIDDEN) \
XX(404, NOT_FOUND, NOT_FOUND) \
XX(405, METHOD_NOT_ALLOWED, METHOD_NOT_ALLOWED) \
XX(406, NOT_ACCEPTABLE, NOT_ACCEPTABLE) \
XX(407, PROXY_AUTHENTICATION_REQUIRED, PROXY_AUTHENTICATION_REQUIRED) \
XX(408, REQUEST_TIMEOUT, REQUEST_TIMEOUT) \
XX(409, CONFLICT, CONFLICT) \
XX(410, GONE, GONE) \
XX(411, LENGTH_REQUIRED, LENGTH_REQUIRED) \
XX(412, PRECONDITION_FAILED, PRECONDITION_FAILED) \
XX(413, PAYLOAD_TOO_LARGE, PAYLOAD_TOO_LARGE) \
XX(414, URI_TOO_LONG, URI_TOO_LONG) \
XX(415, UNSUPPORTED_MEDIA_TYPE, UNSUPPORTED_MEDIA_TYPE) \
XX(416, RANGE_NOT_SATISFIABLE, RANGE_NOT_SATISFIABLE) \
XX(417, EXPECTATION_FAILED, EXPECTATION_FAILED) \
XX(418, IM_A_TEAPOT, IM_A_TEAPOT) \
XX(419, PAGE_EXPIRED, PAGE_EXPIRED) \
XX(420, ENHANCE_YOUR_CALM, ENHANCE_YOUR_CALM) \
XX(421, MISDIRECTED_REQUEST, MISDIRECTED_REQUEST) \
XX(422, UNPROCESSABLE_ENTITY, UNPROCESSABLE_ENTITY) \
XX(423, LOCKED, LOCKED) \
XX(424, FAILED_DEPENDENCY, FAILED_DEPENDENCY) \
XX(425, TOO_EARLY, TOO_EARLY) \
XX(426, UPGRADE_REQUIRED, UPGRADE_REQUIRED) \
XX(428, PRECONDITION_REQUIRED, PRECONDITION_REQUIRED) \
XX(429, TOO_MANY_REQUESTS, TOO_MANY_REQUESTS) \
XX(430, REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL, REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL) \
XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, REQUEST_HEADER_FIELDS_TOO_LARGE) \
XX(440, LOGIN_TIMEOUT, LOGIN_TIMEOUT) \
XX(444, NO_RESPONSE, NO_RESPONSE) \
XX(449, RETRY_WITH, RETRY_WITH) \
XX(450, BLOCKED_BY_PARENTAL_CONTROL, BLOCKED_BY_PARENTAL_CONTROL) \
XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, UNAVAILABLE_FOR_LEGAL_REASONS) \
XX(460, CLIENT_CLOSED_LOAD_BALANCED_REQUEST, CLIENT_CLOSED_LOAD_BALANCED_REQUEST) \
XX(463, INVALID_X_FORWARDED_FOR, INVALID_X_FORWARDED_FOR) \
XX(494, REQUEST_HEADER_TOO_LARGE, REQUEST_HEADER_TOO_LARGE) \
XX(495, SSL_CERTIFICATE_ERROR, SSL_CERTIFICATE_ERROR) \
XX(496, SSL_CERTIFICATE_REQUIRED, SSL_CERTIFICATE_REQUIRED) \
XX(497, HTTP_REQUEST_SENT_TO_HTTPS_PORT, HTTP_REQUEST_SENT_TO_HTTPS_PORT) \
XX(498, INVALID_TOKEN, INVALID_TOKEN) \
XX(499, CLIENT_CLOSED_REQUEST, CLIENT_CLOSED_REQUEST) \
XX(500, INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR) \
XX(501, NOT_IMPLEMENTED, NOT_IMPLEMENTED) \
XX(502, BAD_GATEWAY, BAD_GATEWAY) \
XX(503, SERVICE_UNAVAILABLE, SERVICE_UNAVAILABLE) \
XX(504, GATEWAY_TIMEOUT, GATEWAY_TIMEOUT) \
XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP_VERSION_NOT_SUPPORTED) \
XX(506, VARIANT_ALSO_NEGOTIATES, VARIANT_ALSO_NEGOTIATES) \
XX(507, INSUFFICIENT_STORAGE, INSUFFICIENT_STORAGE) \
XX(508, LOOP_DETECTED, LOOP_DETECTED) \
XX(509, BANDWIDTH_LIMIT_EXCEEDED, BANDWIDTH_LIMIT_EXCEEDED) \
XX(510, NOT_EXTENDED, NOT_EXTENDED) \
XX(511, NETWORK_AUTHENTICATION_REQUIRED, NETWORK_AUTHENTICATION_REQUIRED) \
XX(520, WEB_SERVER_UNKNOWN_ERROR, WEB_SERVER_UNKNOWN_ERROR) \
XX(521, WEB_SERVER_IS_DOWN, WEB_SERVER_IS_DOWN) \
XX(522, CONNECTION_TIMEOUT, CONNECTION_TIMEOUT) \
XX(523, ORIGIN_IS_UNREACHABLE, ORIGIN_IS_UNREACHABLE) \
XX(524, TIMEOUT_OCCURED, TIMEOUT_OCCURED) \
XX(525, SSL_HANDSHAKE_FAILED, SSL_HANDSHAKE_FAILED) \
XX(526, INVALID_SSL_CERTIFICATE, INVALID_SSL_CERTIFICATE) \
XX(527, RAILGUN_ERROR, RAILGUN_ERROR) \
XX(529, SITE_IS_OVERLOADED, SITE_IS_OVERLOADED) \
XX(530, SITE_IS_FROZEN, SITE_IS_FROZEN) \
XX(561, IDENTITY_PROVIDER_AUTHENTICATION_ERROR, IDENTITY_PROVIDER_AUTHENTICATION_ERROR) \
XX(598, NETWORK_READ_TIMEOUT, NETWORK_READ_TIMEOUT) \
XX(599, NETWORK_CONNECT_TIMEOUT, NETWORK_CONNECT_TIMEOUT) \
#ifdef __cplusplus
} /* extern "C" */
#endif
@ -333,8 +560,12 @@ struct llhttp_settings_s {
/* Possible return values 0, -1, HPE_USER */
llhttp_data_cb on_url;
llhttp_data_cb on_status;
llhttp_data_cb on_method;
llhttp_data_cb on_version;
llhttp_data_cb on_header_field;
llhttp_data_cb on_header_value;
llhttp_data_cb on_chunk_extension_name;
llhttp_data_cb on_chunk_extension_value;
/* Possible return values:
* 0 - Proceed normally
@ -352,6 +583,14 @@ struct llhttp_settings_s {
/* Possible return values 0, -1, `HPE_PAUSED` */
llhttp_cb on_message_complete;
llhttp_cb on_url_complete;
llhttp_cb on_status_complete;
llhttp_cb on_method_complete;
llhttp_cb on_version_complete;
llhttp_cb on_header_field_complete;
llhttp_cb on_header_value_complete;
llhttp_cb on_chunk_extension_name_complete;
llhttp_cb on_chunk_extension_value_complete;
/* When on_chunk_header is called, the current chunk length is stored
* in parser->content_length.
@ -359,12 +598,7 @@ struct llhttp_settings_s {
*/
llhttp_cb on_chunk_header;
llhttp_cb on_chunk_complete;
/* Information-only callbacks, return value is ignored */
llhttp_cb on_url_complete;
llhttp_cb on_status_complete;
llhttp_cb on_header_field_complete;
llhttp_cb on_header_value_complete;
llhttp_cb on_reset;
};
/* Initialize the parser with specific type and user settings.
@ -514,6 +748,9 @@ const char* llhttp_errno_name(llhttp_errno_t err);
LLHTTP_EXPORT
const char* llhttp_method_name(llhttp_method_t method);
/* Returns textual name of HTTP status */
LLHTTP_EXPORT
const char* llhttp_status_name(llhttp_status_t status);
/* Enables/disables lenient header value parsing (disabled by default).
*
@ -553,6 +790,7 @@ void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
*
* **(USE AT YOUR OWN RISK)**
*/
LLHTTP_EXPORT
void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled);
/* Enables/disables lenient handling of `Transfer-Encoding` header.
@ -566,6 +804,7 @@ void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled);
*
* **(USE AT YOUR OWN RISK)**
*/
LLHTTP_EXPORT
void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled);
#ifdef __cplusplus

View File

@ -60,13 +60,26 @@ const llhttp_settings_t wasm_settings = {
wasm_on_message_begin,
wasm_on_url,
wasm_on_status,
NULL,
NULL,
wasm_on_header_field,
wasm_on_header_value,
NULL,
NULL,
wasm_on_headers_complete_wrap,
wasm_on_body,
wasm_on_message_complete,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
@ -80,6 +93,8 @@ void llhttp_free(llhttp_t* parser) {
free(parser);
}
#endif // defined(__wasm__)
/* Some getters required to get stuff from the parser */
uint8_t llhttp_get_type(llhttp_t* parser) {
@ -106,8 +121,6 @@ uint8_t llhttp_get_upgrade(llhttp_t* parser) {
return parser->upgrade;
}
#endif // defined(__wasm__)
void llhttp_reset(llhttp_t* parser) {
llhttp_type_t type = parser->type;
@ -226,6 +239,15 @@ const char* llhttp_method_name(llhttp_method_t method) {
#undef HTTP_METHOD_GEN
}
const char* llhttp_status_name(llhttp_status_t status) {
#define HTTP_STATUS_GEN(NUM, NAME, STRING) case HTTP_STATUS_##NAME: return #STRING;
switch (status) {
HTTP_STATUS_MAP(HTTP_STATUS_GEN)
default: abort();
}
#undef HTTP_STATUS_GEN
}
void llhttp_set_lenient_headers(llhttp_t* parser, int enabled) {
if (enabled) {
@ -299,6 +321,34 @@ int llhttp__on_status_complete(llhttp_t* s, const char* p, const char* endp) {
}
int llhttp__on_method(llhttp_t* s, const char* p, const char* endp) {
int err;
SPAN_CALLBACK_MAYBE(s, on_method, p, endp - p);
return err;
}
int llhttp__on_method_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_method_complete);
return err;
}
int llhttp__on_version(llhttp_t* s, const char* p, const char* endp) {
int err;
SPAN_CALLBACK_MAYBE(s, on_version, p, endp - p);
return err;
}
int llhttp__on_version_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_version_complete);
return err;
}
int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) {
int err;
SPAN_CALLBACK_MAYBE(s, on_header_field, p, endp - p);
@ -355,6 +405,34 @@ int llhttp__on_chunk_header(llhttp_t* s, const char* p, const char* endp) {
}
int llhttp__on_chunk_extension_name(llhttp_t* s, const char* p, const char* endp) {
int err;
SPAN_CALLBACK_MAYBE(s, on_chunk_extension_name, p, endp - p);
return err;
}
int llhttp__on_chunk_extension_name_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_chunk_extension_name_complete);
return err;
}
int llhttp__on_chunk_extension_value(llhttp_t* s, const char* p, const char* endp) {
int err;
SPAN_CALLBACK_MAYBE(s, on_chunk_extension_value, p, endp - p);
return err;
}
int llhttp__on_chunk_extension_value_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_chunk_extension_value_complete);
return err;
}
int llhttp__on_chunk_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_chunk_complete);
@ -362,6 +440,13 @@ int llhttp__on_chunk_complete(llhttp_t* s, const char* p, const char* endp) {
}
int llhttp__on_reset(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_reset);
return err;
}
/* Private */

File diff suppressed because it is too large Load Diff