Skip to content

Commit

Permalink
Merging in node/llhttp#fix/nodejsgh-69
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamkennery committed Nov 20, 2020
1 parent 948f515 commit 829f562
Show file tree
Hide file tree
Showing 5 changed files with 3,753 additions and 2,447 deletions.
62 changes: 54 additions & 8 deletions deps/llhttp/include/llhttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define INCLUDE_LLHTTP_H_

#define LLHTTP_VERSION_MAJOR 2
#define LLHTTP_VERSION_MINOR 1
#define LLHTTP_VERSION_PATCH 3
#define LLHTTP_VERSION_MINOR 2
#define LLHTTP_VERSION_PATCH 0

#ifndef LLHTTP_STRICT_MODE
# define LLHTTP_STRICT_MODE 0
Expand Down Expand Up @@ -33,10 +33,11 @@ struct llhttp__internal_s {
uint8_t http_major;
uint8_t http_minor;
uint8_t header_state;
uint16_t flags;
uint8_t lenient_flags;
uint8_t upgrade;
uint16_t status_code;
uint8_t finish;
uint16_t flags;
uint16_t status_code;
void* settings;
};

Expand Down Expand Up @@ -91,11 +92,16 @@ enum llhttp_flags {
F_CONTENT_LENGTH = 0x20,
F_SKIPBODY = 0x40,
F_TRAILING = 0x80,
F_LENIENT = 0x100,
F_TRANSFER_ENCODING = 0x200
};
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 {
HTTP_BOTH = 0,
HTTP_REQUEST = 1,
Expand Down Expand Up @@ -145,7 +151,18 @@ enum llhttp_method {
HTTP_LINK = 31,
HTTP_UNLINK = 32,
HTTP_SOURCE = 33,
HTTP_PRI = 34
HTTP_PRI = 34,
HTTP_DESCRIBE = 35,
HTTP_ANNOUNCE = 36,
HTTP_SETUP = 37,
HTTP_PLAY = 38,
HTTP_PAUSE = 39,
HTTP_TEARDOWN = 40,
HTTP_GET_PARAMETER = 41,
HTTP_SET_PARAMETER = 42,
HTTP_REDIRECT = 43,
HTTP_RECORD = 44,
HTTP_FLUSH = 45
};
typedef enum llhttp_method llhttp_method_t;

Expand Down Expand Up @@ -212,6 +229,17 @@ typedef enum llhttp_method llhttp_method_t;
XX(32, UNLINK, UNLINK) \
XX(33, SOURCE, SOURCE) \
XX(34, PRI, PRI) \
XX(35, DESCRIBE, DESCRIBE) \
XX(36, ANNOUNCE, ANNOUNCE) \
XX(37, SETUP, SETUP) \
XX(38, PLAY, PLAY) \
XX(39, PAUSE, PAUSE) \
XX(40, TEARDOWN, TEARDOWN) \
XX(41, GET_PARAMETER, GET_PARAMETER) \
XX(42, SET_PARAMETER, SET_PARAMETER) \
XX(43, REDIRECT, REDIRECT) \
XX(44, RECORD, RECORD) \
XX(45, FLUSH, FLUSH) \



Expand Down Expand Up @@ -266,7 +294,12 @@ struct llhttp_settings_s {
llhttp_cb on_chunk_complete;
};

/* Initialize the parser with specific type and user settings */
/* Initialize the parser with specific type and user settings.
*
* NOTE: lifetime of `settings` has to be at least the same as the lifetime of
* the `parser` here. In practice, `settings` has to be either a static
* variable or be allocated with `malloc`, `new`, etc.
*/
void llhttp_init(llhttp_t* parser, llhttp_type_t type,
const llhttp_settings_t* settings);

Expand Down Expand Up @@ -373,7 +406,20 @@ const char* llhttp_method_name(llhttp_method_t method);
*
* **(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
} /* extern "C" */
Expand Down
14 changes: 11 additions & 3 deletions deps/llhttp/src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,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) {
parser->flags |= F_LENIENT;
parser->lenient_flags |= LENIENT_HEADERS;
} 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;
}
}

Expand Down
7 changes: 3 additions & 4 deletions deps/llhttp/src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
return 2;
} 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 */

/* If a Transfer-Encoding header field
Expand Down Expand Up @@ -97,9 +98,7 @@ int llhttp__after_message_complete(llhttp_t* parser, const char* p,

should_keep_alive = llhttp_should_keep_alive(parser);
parser->finish = HTTP_FINISH_SAFE;

/* Keep `F_LENIENT` flag between messages, but reset every other flag */
parser->flags &= F_LENIENT;
parser->flags = 0;

/* NOTE: this is ignored in loose parsing mode */
return should_keep_alive;
Expand Down
Loading

0 comments on commit 829f562

Please sign in to comment.