Upgrade http-parser.
This commit is contained in:
parent
039790cf26
commit
f217ce7ff7
2 changed files with 161 additions and 43 deletions
|
@ -400,6 +400,8 @@ enum http_host_state
|
|||
, s_http_host
|
||||
, s_http_host_v6
|
||||
, s_http_host_v6_end
|
||||
, s_http_host_v6_zone_start
|
||||
, s_http_host_v6_zone
|
||||
, s_http_host_port_start
|
||||
, s_http_host_port
|
||||
};
|
||||
|
@ -433,6 +435,12 @@ enum http_host_state
|
|||
(IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Verify that a char is a valid visible (printable) US-ASCII
|
||||
* character or %x80-FF
|
||||
**/
|
||||
#define IS_HEADER_CHAR(ch) \
|
||||
(ch == CR || ch == LF || ch == 9 || (ch > 31 && ch != 127))
|
||||
|
||||
#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
|
||||
|
||||
|
@ -637,6 +645,7 @@ size_t http_parser_execute (http_parser *parser,
|
|||
const char *body_mark = 0;
|
||||
const char *status_mark = 0;
|
||||
enum state p_state = (enum state) parser->state;
|
||||
const unsigned int lenient = parser->lenient_http_headers;
|
||||
|
||||
/* We're in an error state. Don't bother doing anything. */
|
||||
if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
|
||||
|
@ -957,21 +966,23 @@ reexecute:
|
|||
parser->method = (enum http_method) 0;
|
||||
parser->index = 1;
|
||||
switch (ch) {
|
||||
case 'A': parser->method = HTTP_ACL; break;
|
||||
case 'B': parser->method = HTTP_BIND; break;
|
||||
case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
|
||||
case 'D': parser->method = HTTP_DELETE; break;
|
||||
case 'G': parser->method = HTTP_GET; break;
|
||||
case 'H': parser->method = HTTP_HEAD; break;
|
||||
case 'L': parser->method = HTTP_LOCK; break;
|
||||
case 'L': parser->method = HTTP_LOCK; /* or LINK */ break;
|
||||
case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
|
||||
case 'N': parser->method = HTTP_NOTIFY; break;
|
||||
case 'O': parser->method = HTTP_OPTIONS; break;
|
||||
case 'P': parser->method = HTTP_POST;
|
||||
/* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
|
||||
break;
|
||||
case 'R': parser->method = HTTP_REPORT; break;
|
||||
case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break;
|
||||
case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
|
||||
case 'T': parser->method = HTTP_TRACE; break;
|
||||
case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
|
||||
case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break;
|
||||
default:
|
||||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
|
@ -1027,16 +1038,32 @@ reexecute:
|
|||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
}
|
||||
} else if (parser->index == 1 && parser->method == HTTP_POST) {
|
||||
if (ch == 'R') {
|
||||
parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
|
||||
} else if (ch == 'U') {
|
||||
parser->method = HTTP_PUT; /* or HTTP_PURGE */
|
||||
} else if (ch == 'A') {
|
||||
parser->method = HTTP_PATCH;
|
||||
} else {
|
||||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
} else if (parser->method == HTTP_REPORT) {
|
||||
if (parser->index == 2 && ch == 'B') {
|
||||
parser->method = HTTP_REBIND;
|
||||
} else {
|
||||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
}
|
||||
} else if (parser->index == 1) {
|
||||
if (parser->method == HTTP_POST) {
|
||||
if (ch == 'R') {
|
||||
parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
|
||||
} else if (ch == 'U') {
|
||||
parser->method = HTTP_PUT; /* or HTTP_PURGE */
|
||||
} else if (ch == 'A') {
|
||||
parser->method = HTTP_PATCH;
|
||||
} else {
|
||||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
}
|
||||
} else if (parser->method == HTTP_LOCK) {
|
||||
if (ch == 'I') {
|
||||
parser->method = HTTP_LINK;
|
||||
} else {
|
||||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
} else if (parser->index == 2) {
|
||||
if (parser->method == HTTP_PUT) {
|
||||
|
@ -1049,6 +1076,8 @@ reexecute:
|
|||
} else if (parser->method == HTTP_UNLOCK) {
|
||||
if (ch == 'S') {
|
||||
parser->method = HTTP_UNSUBSCRIBE;
|
||||
} else if(ch == 'B') {
|
||||
parser->method = HTTP_UNBIND;
|
||||
} else {
|
||||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
|
@ -1059,6 +1088,8 @@ reexecute:
|
|||
}
|
||||
} else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') {
|
||||
parser->method = HTTP_PROPPATCH;
|
||||
} else if (parser->index == 3 && parser->method == HTTP_UNLOCK && ch == 'I') {
|
||||
parser->method = HTTP_UNLINK;
|
||||
} else {
|
||||
SET_ERRNO(HPE_INVALID_METHOD);
|
||||
goto error;
|
||||
|
@ -1384,7 +1415,12 @@ reexecute:
|
|||
|| c != CONTENT_LENGTH[parser->index]) {
|
||||
parser->header_state = h_general;
|
||||
} else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
|
||||
if (parser->flags & F_CONTENTLENGTH) {
|
||||
SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
|
||||
goto error;
|
||||
}
|
||||
parser->header_state = h_content_length;
|
||||
parser->flags |= F_CONTENTLENGTH;
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -1536,6 +1572,11 @@ reexecute:
|
|||
REEXECUTE();
|
||||
}
|
||||
|
||||
if (!lenient && !IS_HEADER_CHAR(ch)) {
|
||||
SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
|
||||
goto error;
|
||||
}
|
||||
|
||||
c = LOWER(ch);
|
||||
|
||||
switch (h_state) {
|
||||
|
@ -1703,7 +1744,10 @@ reexecute:
|
|||
|
||||
case s_header_almost_done:
|
||||
{
|
||||
STRICT_CHECK(ch != LF);
|
||||
if (UNLIKELY(ch != LF)) {
|
||||
SET_ERRNO(HPE_LF_EXPECTED);
|
||||
goto error;
|
||||
}
|
||||
|
||||
UPDATE_STATE(s_header_value_lws);
|
||||
break;
|
||||
|
@ -1782,9 +1826,17 @@ reexecute:
|
|||
|
||||
if (parser->flags & F_TRAILING) {
|
||||
/* End of a chunked request */
|
||||
UPDATE_STATE(NEW_MESSAGE());
|
||||
CALLBACK_NOTIFY(message_complete);
|
||||
break;
|
||||
UPDATE_STATE(s_message_done);
|
||||
CALLBACK_NOTIFY_NOADVANCE(chunk_complete);
|
||||
REEXECUTE();
|
||||
}
|
||||
|
||||
/* Cannot use chunked encoding and a content-length header together
|
||||
per the HTTP specification. */
|
||||
if ((parser->flags & F_CHUNKED) &&
|
||||
(parser->flags & F_CONTENTLENGTH)) {
|
||||
SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
|
||||
goto error;
|
||||
}
|
||||
|
||||
UPDATE_STATE(s_headers_done);
|
||||
|
@ -1828,12 +1880,16 @@ reexecute:
|
|||
|
||||
case s_headers_done:
|
||||
{
|
||||
int hasBody;
|
||||
STRICT_CHECK(ch != LF);
|
||||
|
||||
parser->nread = 0;
|
||||
|
||||
/* Exit, the rest of the connect is in a different protocol. */
|
||||
if (parser->upgrade) {
|
||||
hasBody = parser->flags & F_CHUNKED ||
|
||||
(parser->content_length > 0 && parser->content_length != ULLONG_MAX);
|
||||
if (parser->upgrade && (parser->method == HTTP_CONNECT ||
|
||||
(parser->flags & F_SKIPBODY) || !hasBody)) {
|
||||
/* Exit, the rest of the message is in a different protocol. */
|
||||
UPDATE_STATE(NEW_MESSAGE());
|
||||
CALLBACK_NOTIFY(message_complete);
|
||||
RETURN((p - data) + 1);
|
||||
|
@ -1854,8 +1910,7 @@ reexecute:
|
|||
/* Content-Length header given and non-zero */
|
||||
UPDATE_STATE(s_body_identity);
|
||||
} else {
|
||||
if (parser->type == HTTP_REQUEST ||
|
||||
!http_message_needs_eof(parser)) {
|
||||
if (!http_message_needs_eof(parser)) {
|
||||
/* Assume content-length 0 - read the next */
|
||||
UPDATE_STATE(NEW_MESSAGE());
|
||||
CALLBACK_NOTIFY(message_complete);
|
||||
|
@ -1915,6 +1970,10 @@ reexecute:
|
|||
case s_message_done:
|
||||
UPDATE_STATE(NEW_MESSAGE());
|
||||
CALLBACK_NOTIFY(message_complete);
|
||||
if (parser->upgrade) {
|
||||
/* Exit, the rest of the message is in a different protocol. */
|
||||
RETURN((p - data) + 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case s_chunk_size_start:
|
||||
|
@ -1994,6 +2053,7 @@ reexecute:
|
|||
} else {
|
||||
UPDATE_STATE(s_chunk_data);
|
||||
}
|
||||
CALLBACK_NOTIFY(chunk_header);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -2033,6 +2093,7 @@ reexecute:
|
|||
STRICT_CHECK(ch != LF);
|
||||
parser->nread = 0;
|
||||
UPDATE_STATE(s_chunk_size_start);
|
||||
CALLBACK_NOTIFY(chunk_complete);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -2144,13 +2205,13 @@ http_parser_settings_init(http_parser_settings *settings)
|
|||
|
||||
const char *
|
||||
http_errno_name(enum http_errno err) {
|
||||
assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
|
||||
assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab));
|
||||
return http_strerror_tab[err].name;
|
||||
}
|
||||
|
||||
const char *
|
||||
http_errno_description(enum http_errno err) {
|
||||
assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
|
||||
assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab));
|
||||
return http_strerror_tab[err].description;
|
||||
}
|
||||
|
||||
|
@ -2203,6 +2264,23 @@ http_parse_host_char(enum http_host_state s, const char ch) {
|
|||
return s_http_host_v6;
|
||||
}
|
||||
|
||||
if (s == s_http_host_v6 && ch == '%') {
|
||||
return s_http_host_v6_zone_start;
|
||||
}
|
||||
break;
|
||||
|
||||
case s_http_host_v6_zone:
|
||||
if (ch == ']') {
|
||||
return s_http_host_v6_end;
|
||||
}
|
||||
|
||||
/* FALLTHROUGH */
|
||||
case s_http_host_v6_zone_start:
|
||||
/* RFC 6874 Zone ID consists of 1*( unreserved / pct-encoded) */
|
||||
if (IS_ALPHANUM(ch) || ch == '%' || ch == '.' || ch == '-' || ch == '_' ||
|
||||
ch == '~') {
|
||||
return s_http_host_v6_zone;
|
||||
}
|
||||
break;
|
||||
|
||||
case s_http_host_port:
|
||||
|
@ -2221,6 +2299,7 @@ http_parse_host_char(enum http_host_state s, const char ch) {
|
|||
|
||||
static int
|
||||
http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
|
||||
assert(u->field_set & (1 << UF_HOST));
|
||||
enum http_host_state s;
|
||||
|
||||
const char *p;
|
||||
|
@ -2252,6 +2331,11 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
|
|||
u->field_data[UF_HOST].len++;
|
||||
break;
|
||||
|
||||
case s_http_host_v6_zone_start:
|
||||
case s_http_host_v6_zone:
|
||||
u->field_data[UF_HOST].len++;
|
||||
break;
|
||||
|
||||
case s_http_host_port:
|
||||
if (s != s_http_host_port) {
|
||||
u->field_data[UF_PORT].off = p - buf;
|
||||
|
@ -2281,6 +2365,8 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
|
|||
case s_http_host_start:
|
||||
case s_http_host_v6_start:
|
||||
case s_http_host_v6:
|
||||
case s_http_host_v6_zone_start:
|
||||
case s_http_host_v6_zone:
|
||||
case s_http_host_port_start:
|
||||
case s_http_userinfo:
|
||||
case s_http_userinfo_start:
|
||||
|
@ -2292,6 +2378,11 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
http_parser_url_init(struct http_parser_url *u) {
|
||||
memset(u, 0, sizeof(*u));
|
||||
}
|
||||
|
||||
int
|
||||
http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
|
||||
struct http_parser_url *u)
|
||||
|
@ -2365,7 +2456,12 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
|
|||
|
||||
/* host must be present if there is a schema */
|
||||
/* parsing http:///toto will fail */
|
||||
if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) {
|
||||
if ((u->field_set & (1 << UF_SCHEMA)) &&
|
||||
(u->field_set & (1 << UF_HOST)) == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (u->field_set & (1 << UF_HOST)) {
|
||||
if (http_parse_host(buf, u, found_at) != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue