"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/http-parser/http_parser.c" between
tcpflow-1.4.5.tar.gz and tcpflow-1.5.0.tar.gz

About: tcpflow is a TCP/IP packet demultiplexer that captures data transmitted as part of TCP connections (flows), and stores the data in a way that is convenient for protocol analysis and debugging.

http_parser.c  (tcpflow-1.4.5):http_parser.c  (tcpflow-1.5.0)
skipping to change at line 59 skipping to change at line 59
#ifndef ELEM_AT #ifndef ELEM_AT
# define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v)) # define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v))
#endif #endif
#define SET_ERRNO(e) \ #define SET_ERRNO(e) \
do { \ do { \
parser->http_errno = (e); \ parser->http_errno = (e); \
} while(0) } while(0)
#define CURRENT_STATE() p_state
#define UPDATE_STATE(V) p_state = (enum state) (V);
#define RETURN(V) \
do { \
parser->state = CURRENT_STATE(); \
return (V); \
} while (0);
#define REEXECUTE() \
goto reexecute; \
#ifdef __GNUC__
# define LIKELY(X) __builtin_expect(!!(X), 1)
# define UNLIKELY(X) __builtin_expect(!!(X), 0)
#else
# define LIKELY(X) (X)
# define UNLIKELY(X) (X)
#endif
/* Run the notify callback FOR, returning ER if it fails */ /* Run the notify callback FOR, returning ER if it fails */
#define CALLBACK_NOTIFY_(FOR, ER) \ #define CALLBACK_NOTIFY_(FOR, ER) \
do { \ do { \
assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
\ \
if (settings->on_##FOR) { \ if (LIKELY(settings->on_##FOR)) { \
if (0 != settings->on_##FOR(parser)) { \ parser->state = CURRENT_STATE(); \
if (UNLIKELY(0 != settings->on_##FOR(parser))) { \
SET_ERRNO(HPE_CB_##FOR); \ SET_ERRNO(HPE_CB_##FOR); \
} \ } \
UPDATE_STATE(parser->state); \
\ \
/* We either errored above or got paused; get out */ \ /* We either errored above or got paused; get out */ \
if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \ if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \
return (ER); \ return (ER); \
} \ } \
} \ } \
} while (0) } while (0)
/* Run the notify callback FOR and consume the current byte */ /* Run the notify callback FOR and consume the current byte */
#define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1) #define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1)
/* Run the notify callback FOR and don't consume the current byte */ /* Run the notify callback FOR and don't consume the current byte */
#define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data) #define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data)
/* Run data callback FOR with LEN bytes, returning ER if it fails */ /* Run data callback FOR with LEN bytes, returning ER if it fails */
#define CALLBACK_DATA_(FOR, LEN, ER) \ #define CALLBACK_DATA_(FOR, LEN, ER) \
do { \ do { \
assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
\ \
if (FOR##_mark) { \ if (FOR##_mark) { \
if (settings->on_##FOR) { \ if (LIKELY(settings->on_##FOR)) { \
if (0 != settings->on_##FOR(parser, FOR##_mark, (LEN))) { \ parser->state = CURRENT_STATE(); \
if (UNLIKELY(0 != \
settings->on_##FOR(parser, FOR##_mark, (LEN)))) { \
SET_ERRNO(HPE_CB_##FOR); \ SET_ERRNO(HPE_CB_##FOR); \
} \ } \
UPDATE_STATE(parser->state); \
\ \
/* We either errored above or got paused; get out */ \ /* We either errored above or got paused; get out */ \
if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \ if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \
return (ER); \ return (ER); \
} \ } \
} \ } \
FOR##_mark = NULL; \ FOR##_mark = NULL; \
} \ } \
} while (0) } while (0)
/* Run the data callback FOR and consume the current byte */ /* Run the data callback FOR and consume the current byte */
#define CALLBACK_DATA(FOR) \ #define CALLBACK_DATA(FOR) \
CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1) CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1)
skipping to change at line 118 skipping to change at line 141
CALLBACK_DATA_(FOR, p - FOR##_mark, p - data) CALLBACK_DATA_(FOR, p - FOR##_mark, p - data)
/* Set the mark FOR; non-destructive if mark is already set */ /* Set the mark FOR; non-destructive if mark is already set */
#define MARK(FOR) \ #define MARK(FOR) \
do { \ do { \
if (!FOR##_mark) { \ if (!FOR##_mark) { \
FOR##_mark = p; \ FOR##_mark = p; \
} \ } \
} while (0) } while (0)
/* Don't allow the total size of the HTTP headers (including the status
* line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect
* embedders against denial-of-service attacks where the attacker feeds
* us a never-ending header that the embedder keeps buffering.
*
* This check is arguably the responsibility of embedders but we're doing
* it on the embedder's behalf because most won't bother and this way we
* make the web a little safer. HTTP_MAX_HEADER_SIZE is still far bigger
* than any reasonable request or response so this should never affect
* day-to-day operation.
*/
#define COUNT_HEADER_SIZE(V) \
do { \
parser->nread += (V); \
if (UNLIKELY(parser->nread > (HTTP_MAX_HEADER_SIZE))) { \
SET_ERRNO(HPE_HEADER_OVERFLOW); \
goto error; \
} \
} while (0)
#define PROXY_CONNECTION "proxy-connection" #define PROXY_CONNECTION "proxy-connection"
#define CONNECTION "connection" #define CONNECTION "connection"
#define CONTENT_LENGTH "content-length" #define CONTENT_LENGTH "content-length"
#define TRANSFER_ENCODING "transfer-encoding" #define TRANSFER_ENCODING "transfer-encoding"
#define UPGRADE "upgrade" #define UPGRADE "upgrade"
#define CHUNKED "chunked" #define CHUNKED "chunked"
#define KEEP_ALIVE "keep-alive" #define KEEP_ALIVE "keep-alive"
#define CLOSE "close" #define CLOSE "close"
static const char *method_strings[] = static const char *method_strings[] =
skipping to change at line 238 skipping to change at line 281
enum state enum state
{ s_dead = 1 /* important that this is > 0 */ { s_dead = 1 /* important that this is > 0 */
, s_start_req_or_res , s_start_req_or_res
, s_res_or_resp_H , s_res_or_resp_H
, s_start_res , s_start_res
, s_res_H , s_res_H
, s_res_HT , s_res_HT
, s_res_HTT , s_res_HTT
, s_res_HTTP , s_res_HTTP
, s_res_first_http_major
, s_res_http_major , s_res_http_major
, s_res_first_http_minor , s_res_http_dot
, s_res_http_minor , s_res_http_minor
, s_res_http_end
, s_res_first_status_code , s_res_first_status_code
, s_res_status_code , s_res_status_code
, s_res_status_start , s_res_status_start
, s_res_status , s_res_status
, s_res_line_almost_done , s_res_line_almost_done
, s_start_req , s_start_req
, s_req_method , s_req_method
, s_req_spaces_before_url , s_req_spaces_before_url
skipping to change at line 268 skipping to change at line 311
, s_req_path , s_req_path
, s_req_query_string_start , s_req_query_string_start
, s_req_query_string , s_req_query_string
, s_req_fragment_start , s_req_fragment_start
, s_req_fragment , s_req_fragment
, s_req_http_start , s_req_http_start
, s_req_http_H , s_req_http_H
, s_req_http_HT , s_req_http_HT
, s_req_http_HTT , s_req_http_HTT
, s_req_http_HTTP , s_req_http_HTTP
, s_req_first_http_major
, s_req_http_major , s_req_http_major
, s_req_first_http_minor , s_req_http_dot
, s_req_http_minor , s_req_http_minor
, s_req_http_end
, s_req_line_almost_done , s_req_line_almost_done
, s_header_field_start , s_header_field_start
, s_header_field , s_header_field
, s_header_value_discard_ws , s_header_value_discard_ws
, s_header_value_discard_ws_almost_done , s_header_value_discard_ws_almost_done
, s_header_value_discard_lws , s_header_value_discard_lws
, s_header_value_start , s_header_value_start
, s_header_value , s_header_value
, s_header_value_lws , s_header_value_lws
skipping to change at line 328 skipping to change at line 371
, h_matching_content_length , h_matching_content_length
, h_matching_transfer_encoding , h_matching_transfer_encoding
, h_matching_upgrade , h_matching_upgrade
, h_connection , h_connection
, h_content_length , h_content_length
, h_transfer_encoding , h_transfer_encoding
, h_upgrade , h_upgrade
, h_matching_transfer_encoding_chunked , h_matching_transfer_encoding_chunked
, h_matching_connection_token_start
, h_matching_connection_keep_alive , h_matching_connection_keep_alive
, h_matching_connection_close , h_matching_connection_close
, h_matching_connection_upgrade
, h_matching_connection_token
, h_transfer_encoding_chunked , h_transfer_encoding_chunked
, h_connection_keep_alive , h_connection_keep_alive
, h_connection_close , h_connection_close
, h_connection_upgrade
}; };
enum http_host_state enum http_host_state
{ {
s_http_host_dead = 1 s_http_host_dead = 1
, s_http_userinfo_start , s_http_userinfo_start
, s_http_userinfo , s_http_userinfo
, s_http_host_start , s_http_host_start
, s_http_host_v6_start , s_http_host_v6_start
, s_http_host , s_http_host
, s_http_host_v6 , s_http_host_v6
, s_http_host_v6_end , 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_start
, s_http_host_port , s_http_host_port
}; };
/* Macros for character classes; depends on strict-mode */ /* Macros for character classes; depends on strict-mode */
#define CR '\r' #define CR '\r'
#define LF '\n' #define LF '\n'
#define LOWER(c) (unsigned char)(c | 0x20) #define LOWER(c) (unsigned char)(c | 0x20)
#define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z') #define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z')
#define IS_NUM(c) ((c) >= '0' && (c) <= '9') #define IS_NUM(c) ((c) >= '0' && (c) <= '9')
#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) #define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c))
#define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f')) #define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f'))
#define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \ #define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \
(c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \ (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \
(c) == ')') (c) == ')')
#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \ #define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \
(c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \ (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \
(c) == '$' || (c) == ',') (c) == '$' || (c) == ',')
#define STRICT_TOKEN(c) (tokens[(unsigned char)c])
#if HTTP_PARSER_STRICT #if HTTP_PARSER_STRICT
#define TOKEN(c) (tokens[(unsigned char)c]) #define TOKEN(c) (tokens[(unsigned char)c])
#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c)) #define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c))
#define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-') #define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-')
#else #else
#define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c]) #define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c])
#define IS_URL_CHAR(c) \ #define IS_URL_CHAR(c) \
(BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80)) (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80))
#define IS_HOST_CHAR(c) \ #define IS_HOST_CHAR(c) \
(IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_') (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
#endif #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 || ((unsigned char)ch > 31 && ch != 127))
#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res) #define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
#if HTTP_PARSER_STRICT #if HTTP_PARSER_STRICT
# define STRICT_CHECK(cond) \ # define STRICT_CHECK(cond) \
do { \ do { \
if (cond) { \ if (cond) { \
SET_ERRNO(HPE_STRICT); \ SET_ERRNO(HPE_STRICT); \
goto error; \ goto error; \
} \ } \
} while (0) } while (0)
skipping to change at line 577 skipping to change at line 635
size_t len) size_t len)
{ {
char c, ch; char c, ch;
int8_t unhex_val; int8_t unhex_val;
const char *p = data; const char *p = data;
const char *header_field_mark = 0; const char *header_field_mark = 0;
const char *header_value_mark = 0; const char *header_value_mark = 0;
const char *url_mark = 0; const char *url_mark = 0;
const char *body_mark = 0; const char *body_mark = 0;
const char *status_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. */ /* We're in an error state. Don't bother doing anything. */
if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
return 0; return 0;
} }
if (len == 0) { if (len == 0) {
switch (parser->state) { switch (CURRENT_STATE()) {
case s_body_identity_eof: case s_body_identity_eof:
/* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if
* we got paused. * we got paused.
*/ */
CALLBACK_NOTIFY_NOADVANCE(message_complete); CALLBACK_NOTIFY_NOADVANCE(message_complete);
return 0; return 0;
case s_dead: case s_dead:
case s_start_req_or_res: case s_start_req_or_res:
case s_start_res: case s_start_res:
case s_start_req: case s_start_req:
return 0; return 0;
default: default:
SET_ERRNO(HPE_INVALID_EOF_STATE); SET_ERRNO(HPE_INVALID_EOF_STATE);
return 1; return 1;
} }
} }
if (parser->state == s_header_field) if (CURRENT_STATE() == s_header_field)
header_field_mark = data; header_field_mark = data;
if (parser->state == s_header_value) if (CURRENT_STATE() == s_header_value)
header_value_mark = data; header_value_mark = data;
switch (parser->state) { switch (CURRENT_STATE()) {
case s_req_path: case s_req_path:
case s_req_schema: case s_req_schema:
case s_req_schema_slash: case s_req_schema_slash:
case s_req_schema_slash_slash: case s_req_schema_slash_slash:
case s_req_server_start: case s_req_server_start:
case s_req_server: case s_req_server:
case s_req_server_with_at: case s_req_server_with_at:
case s_req_query_string_start: case s_req_query_string_start:
case s_req_query_string: case s_req_query_string:
case s_req_fragment_start: case s_req_fragment_start:
case s_req_fragment: case s_req_fragment:
url_mark = data; url_mark = data;
break; break;
case s_res_status: case s_res_status:
status_mark = data; status_mark = data;
break; break;
default:
break;
} }
for (p=data; p != data + len; p++) { for (p=data; p != data + len; p++) {
ch = *p; ch = *p;
if (PARSING_HEADER(parser->state)) { if (PARSING_HEADER(CURRENT_STATE()))
++parser->nread; COUNT_HEADER_SIZE(1);
/* Don't allow the total size of the HTTP headers (including the status
* line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect
* embedders against denial-of-service attacks where the attacker feeds
* us a never-ending header that the embedder keeps buffering.
*
* This check is arguably the responsibility of embedders but we're doing
* it on the embedder's behalf because most won't bother and this way we
* make the web a little safer. HTTP_MAX_HEADER_SIZE is still far bigger
* than any reasonable request or response so this should never affect
* day-to-day operation.
*/
if (parser->nread > (HTTP_MAX_HEADER_SIZE)) {
SET_ERRNO(HPE_HEADER_OVERFLOW);
goto error;
}
}
reexecute_byte: reexecute:
switch (parser->state) { switch (CURRENT_STATE()) {
case s_dead: case s_dead:
/* this state is used after a 'Connection: close' message /* this state is used after a 'Connection: close' message
* the parser will error out if it reads another message * the parser will error out if it reads another message
*/ */
if (ch == CR || ch == LF) if (LIKELY(ch == CR || ch == LF))
break; break;
SET_ERRNO(HPE_CLOSED_CONNECTION); SET_ERRNO(HPE_CLOSED_CONNECTION);
goto error; goto error;
case s_start_req_or_res: case s_start_req_or_res:
{ {
if (ch == CR || ch == LF) if (ch == CR || ch == LF)
break; break;
parser->flags = 0; parser->flags = 0;
parser->content_length = ULLONG_MAX; parser->content_length = ULLONG_MAX;
if (ch == 'H') { if (ch == 'H') {
parser->state = s_res_or_resp_H; UPDATE_STATE(s_res_or_resp_H);
CALLBACK_NOTIFY(message_begin); CALLBACK_NOTIFY(message_begin);
} else { } else {
parser->type = HTTP_REQUEST; parser->type = HTTP_REQUEST;
parser->state = s_start_req; UPDATE_STATE(s_start_req);
goto reexecute_byte; REEXECUTE();
} }
break; break;
} }
case s_res_or_resp_H: case s_res_or_resp_H:
if (ch == 'T') { if (ch == 'T') {
parser->type = HTTP_RESPONSE; parser->type = HTTP_RESPONSE;
parser->state = s_res_HT; UPDATE_STATE(s_res_HT);
} else { } else {
if (ch != 'E') { if (UNLIKELY(ch != 'E')) {
SET_ERRNO(HPE_INVALID_CONSTANT); SET_ERRNO(HPE_INVALID_CONSTANT);
goto error; goto error;
} }
parser->type = HTTP_REQUEST; parser->type = HTTP_REQUEST;
parser->method = HTTP_HEAD; parser->method = HTTP_HEAD;
parser->index = 2; parser->index = 2;
parser->state = s_req_method; UPDATE_STATE(s_req_method);
} }
break; break;
case s_start_res: case s_start_res:
{ {
parser->flags = 0; parser->flags = 0;
parser->content_length = ULLONG_MAX; parser->content_length = ULLONG_MAX;
switch (ch) { switch (ch) {
case 'H': case 'H':
parser->state = s_res_H; UPDATE_STATE(s_res_H);
break; break;
case CR: case CR:
case LF: case LF:
break; break;
default: default:
SET_ERRNO(HPE_INVALID_CONSTANT); SET_ERRNO(HPE_INVALID_CONSTANT);
goto error; goto error;
} }
CALLBACK_NOTIFY(message_begin); CALLBACK_NOTIFY(message_begin);
break; break;
} }
case s_res_H: case s_res_H:
STRICT_CHECK(ch != 'T'); STRICT_CHECK(ch != 'T');
parser->state = s_res_HT; UPDATE_STATE(s_res_HT);
break; break;
case s_res_HT: case s_res_HT:
STRICT_CHECK(ch != 'T'); STRICT_CHECK(ch != 'T');
parser->state = s_res_HTT; UPDATE_STATE(s_res_HTT);
break; break;
case s_res_HTT: case s_res_HTT:
STRICT_CHECK(ch != 'P'); STRICT_CHECK(ch != 'P');
parser->state = s_res_HTTP; UPDATE_STATE(s_res_HTTP);
break; break;
case s_res_HTTP: case s_res_HTTP:
STRICT_CHECK(ch != '/'); STRICT_CHECK(ch != '/');
parser->state = s_res_first_http_major; UPDATE_STATE(s_res_http_major);
break; break;
case s_res_first_http_major: case s_res_http_major:
if (ch < '0' || ch > '9') { if (UNLIKELY(!IS_NUM(ch))) {
SET_ERRNO(HPE_INVALID_VERSION); SET_ERRNO(HPE_INVALID_VERSION);
goto error; goto error;
} }
parser->http_major = ch - '0'; parser->http_major = ch - '0';
parser->state = s_res_http_major; UPDATE_STATE(s_res_http_dot);
break; break;
/* major HTTP version or dot */ case s_res_http_dot:
case s_res_http_major:
{ {
if (ch == '.') { if (UNLIKELY(ch != '.')) {
parser->state = s_res_first_http_minor;
break;
}
if (!IS_NUM(ch)) {
SET_ERRNO(HPE_INVALID_VERSION);
goto error;
}
parser->http_major *= 10;
parser->http_major += ch - '0';
if (parser->http_major > 999) {
SET_ERRNO(HPE_INVALID_VERSION); SET_ERRNO(HPE_INVALID_VERSION);
goto error; goto error;
} }
UPDATE_STATE(s_res_http_minor);
break; break;
} }
/* first digit of minor HTTP version */ case s_res_http_minor:
case s_res_first_http_minor: if (UNLIKELY(!IS_NUM(ch))) {
if (!IS_NUM(ch)) {
SET_ERRNO(HPE_INVALID_VERSION); SET_ERRNO(HPE_INVALID_VERSION);
goto error; goto error;
} }
parser->http_minor = ch - '0'; parser->http_minor = ch - '0';
parser->state = s_res_http_minor; UPDATE_STATE(s_res_http_end);
break; break;
/* minor HTTP version or end of request line */ case s_res_http_end:
case s_res_http_minor:
{ {
if (ch == ' ') { if (UNLIKELY(ch != ' ')) {
parser->state = s_res_first_status_code;
break;
}
if (!IS_NUM(ch)) {
SET_ERRNO(HPE_INVALID_VERSION);
goto error;
}
parser->http_minor *= 10;
parser->http_minor += ch - '0';
if (parser->http_minor > 999) {
SET_ERRNO(HPE_INVALID_VERSION); SET_ERRNO(HPE_INVALID_VERSION);
goto error; goto error;
} }
UPDATE_STATE(s_res_first_status_code);
break; break;
} }
case s_res_first_status_code: case s_res_first_status_code:
{ {
if (!IS_NUM(ch)) { if (!IS_NUM(ch)) {
if (ch == ' ') { if (ch == ' ') {
break; break;
} }
SET_ERRNO(HPE_INVALID_STATUS); SET_ERRNO(HPE_INVALID_STATUS);
goto error; goto error;
} }
parser->status_code = ch - '0'; parser->status_code = ch - '0';
parser->state = s_res_status_code; UPDATE_STATE(s_res_status_code);
break; break;
} }
case s_res_status_code: case s_res_status_code:
{ {
if (!IS_NUM(ch)) { if (!IS_NUM(ch)) {
switch (ch) { switch (ch) {
case ' ': case ' ':
parser->state = s_res_status_start; UPDATE_STATE(s_res_status_start);
break; break;
case CR: case CR:
parser->state = s_res_line_almost_done;
break;
case LF: case LF:
parser->state = s_header_field_start; UPDATE_STATE(s_res_status_start);
REEXECUTE();
break; break;
default: default:
SET_ERRNO(HPE_INVALID_STATUS); SET_ERRNO(HPE_INVALID_STATUS);
goto error; goto error;
} }
break; break;
} }
parser->status_code *= 10; parser->status_code *= 10;
parser->status_code += ch - '0'; parser->status_code += ch - '0';
if (parser->status_code > 999) { if (UNLIKELY(parser->status_code > 999)) {
SET_ERRNO(HPE_INVALID_STATUS); SET_ERRNO(HPE_INVALID_STATUS);
goto error; goto error;
} }
break; break;
} }
case s_res_status_start: case s_res_status_start:
{ {
if (ch == CR) {
parser->state = s_res_line_almost_done;
break;
}
if (ch == LF) {
parser->state = s_header_field_start;
break;
}
MARK(status); MARK(status);
parser->state = s_res_status; UPDATE_STATE(s_res_status);
parser->index = 0; parser->index = 0;
if (ch == CR || ch == LF)
REEXECUTE();
break; break;
} }
case s_res_status: case s_res_status:
if (ch == CR) { if (ch == CR) {
parser->state = s_res_line_almost_done; UPDATE_STATE(s_res_line_almost_done);
CALLBACK_DATA(status); CALLBACK_DATA(status);
break; break;
} }
if (ch == LF) { if (ch == LF) {
parser->state = s_header_field_start; UPDATE_STATE(s_header_field_start);
CALLBACK_DATA(status); CALLBACK_DATA(status);
break; break;
} }
break; break;
case s_res_line_almost_done: case s_res_line_almost_done:
STRICT_CHECK(ch != LF); STRICT_CHECK(ch != LF);
parser->state = s_header_field_start; UPDATE_STATE(s_header_field_start);
break; break;
case s_start_req: case s_start_req:
{ {
if (ch == CR || ch == LF) if (ch == CR || ch == LF)
break; break;
parser->flags = 0; parser->flags = 0;
parser->content_length = ULLONG_MAX; parser->content_length = ULLONG_MAX;
if (!IS_ALPHA(ch)) { if (UNLIKELY(!IS_ALPHA(ch))) {
SET_ERRNO(HPE_INVALID_METHOD); SET_ERRNO(HPE_INVALID_METHOD);
goto error; goto error;
} }
parser->method = (enum http_method) 0; parser->method = (enum http_method) 0;
parser->index = 1; parser->index = 1;
switch (ch) { 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 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break ;
case 'D': parser->method = HTTP_DELETE; break; case 'D': parser->method = HTTP_DELETE; break;
case 'G': parser->method = HTTP_GET; break; case 'G': parser->method = HTTP_GET; break;
case 'H': parser->method = HTTP_HEAD; 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, case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE,
M-SEARCH */ break; M-SEARCH, MKCALENDAR */ break;
case 'N': parser->method = HTTP_NOTIFY; break; case 'N': parser->method = HTTP_NOTIFY; break;
case 'O': parser->method = HTTP_OPTIONS; break; case 'O': parser->method = HTTP_OPTIONS; break;
case 'P': parser->method = HTTP_POST; case 'P': parser->method = HTTP_POST;
/* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */ /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
break; 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 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
case 'T': parser->method = HTTP_TRACE; 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, UNL INK */ break;
default: default:
SET_ERRNO(HPE_INVALID_METHOD); SET_ERRNO(HPE_INVALID_METHOD);
goto error; goto error;
} }
parser->state = s_req_method; UPDATE_STATE(s_req_method);
CALLBACK_NOTIFY(message_begin); CALLBACK_NOTIFY(message_begin);
break; break;
} }
case s_req_method: case s_req_method:
{ {
const char *matcher; const char *matcher;
if (ch == '\0') { if (UNLIKELY(ch == '\0')) {
SET_ERRNO(HPE_INVALID_METHOD); SET_ERRNO(HPE_INVALID_METHOD);
goto error; goto error;
} }
matcher = method_strings[parser->method]; matcher = method_strings[parser->method];
if (ch == ' ' && matcher[parser->index] == '\0') { if (ch == ' ' && matcher[parser->index] == '\0') {
parser->state = s_req_spaces_before_url; UPDATE_STATE(s_req_spaces_before_url);
} else if (ch == matcher[parser->index]) { } else if (ch == matcher[parser->index]) {
; /* nada */ ; /* nada */
} else if (parser->method == HTTP_CONNECT) { } else if ((ch >= 'A' && ch <= 'Z') || ch == '-') {
if (parser->index == 1 && ch == 'H') {
parser->method = HTTP_CHECKOUT; switch (parser->method << 16 | parser->index << 8 | ch) {
} else if (parser->index == 2 && ch == 'P') { #define XX(meth, pos, ch, new_meth) \
parser->method = HTTP_COPY; case (HTTP_##meth << 16 | pos << 8 | ch): \
} else { parser->method = HTTP_##new_meth; break;
SET_ERRNO(HPE_INVALID_METHOD);
goto error; XX(POST, 1, 'U', PUT)
} XX(POST, 1, 'A', PATCH)
} else if (parser->method == HTTP_MKCOL) { XX(POST, 1, 'R', PROPFIND)
if (parser->index == 1 && ch == 'O') { XX(PUT, 2, 'R', PURGE)
parser->method = HTTP_MOVE; XX(CONNECT, 1, 'H', CHECKOUT)
} else if (parser->index == 1 && ch == 'E') { XX(CONNECT, 2, 'P', COPY)
parser->method = HTTP_MERGE; XX(MKCOL, 1, 'O', MOVE)
} else if (parser->index == 1 && ch == '-') { XX(MKCOL, 1, 'E', MERGE)
parser->method = HTTP_MSEARCH; XX(MKCOL, 1, '-', MSEARCH)
} else if (parser->index == 2 && ch == 'A') { XX(MKCOL, 2, 'A', MKACTIVITY)
parser->method = HTTP_MKACTIVITY; XX(MKCOL, 3, 'A', MKCALENDAR)
} else { XX(SUBSCRIBE, 1, 'E', SEARCH)
SET_ERRNO(HPE_INVALID_METHOD); XX(REPORT, 2, 'B', REBIND)
goto error; XX(PROPFIND, 4, 'P', PROPPATCH)
} XX(LOCK, 1, 'I', LINK)
} else if (parser->method == HTTP_SUBSCRIBE) { XX(UNLOCK, 2, 'S', UNSUBSCRIBE)
if (parser->index == 1 && ch == 'E') { XX(UNLOCK, 2, 'B', UNBIND)
parser->method = HTTP_SEARCH; XX(UNLOCK, 3, 'I', UNLINK)
} else { #undef XX
SET_ERRNO(HPE_INVALID_METHOD); default:
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->index == 2) {
if (parser->method == HTTP_PUT) {
if (ch == 'R') {
parser->method = HTTP_PURGE;
} else {
SET_ERRNO(HPE_INVALID_METHOD);
goto error;
}
} else if (parser->method == HTTP_UNLOCK) {
if (ch == 'S') {
parser->method = HTTP_UNSUBSCRIBE;
} else {
SET_ERRNO(HPE_INVALID_METHOD); SET_ERRNO(HPE_INVALID_METHOD);
goto error; goto error;
}
} else {
SET_ERRNO(HPE_INVALID_METHOD);
goto error;
} }
} else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch =
= 'P') {
parser->method = HTTP_PROPPATCH;
} else { } else {
SET_ERRNO(HPE_INVALID_METHOD); SET_ERRNO(HPE_INVALID_METHOD);
goto error; goto error;
} }
++parser->index; ++parser->index;
break; break;
} }
case s_req_spaces_before_url: case s_req_spaces_before_url:
{ {
if (ch == ' ') break; if (ch == ' ') break;
MARK(url); MARK(url);
if (parser->method == HTTP_CONNECT) { if (parser->method == HTTP_CONNECT) {
parser->state = s_req_server_start; UPDATE_STATE(s_req_server_start);
} }
parser->state = parse_url_char((enum state)parser->state, ch); UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch));
if (parser->state == s_dead) { if (UNLIKELY(CURRENT_STATE() == s_dead)) {
SET_ERRNO(HPE_INVALID_URL); SET_ERRNO(HPE_INVALID_URL);
goto error; goto error;
} }
break; break;
} }
case s_req_schema: case s_req_schema:
case s_req_schema_slash: case s_req_schema_slash:
case s_req_schema_slash_slash: case s_req_schema_slash_slash:
case s_req_server_start: case s_req_server_start:
{ {
switch (ch) { switch (ch) {
/* No whitespace allowed here */ /* No whitespace allowed here */
case ' ': case ' ':
case CR: case CR:
case LF: case LF:
SET_ERRNO(HPE_INVALID_URL); SET_ERRNO(HPE_INVALID_URL);
goto error; goto error;
default: default:
parser->state = parse_url_char((enum state)parser->state, ch); UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch));
if (parser->state == s_dead) { if (UNLIKELY(CURRENT_STATE() == s_dead)) {
SET_ERRNO(HPE_INVALID_URL); SET_ERRNO(HPE_INVALID_URL);
goto error; goto error;
} }
} }
break; break;
} }
case s_req_server: case s_req_server:
case s_req_server_with_at: case s_req_server_with_at:
case s_req_path: case s_req_path:
case s_req_query_string_start: case s_req_query_string_start:
case s_req_query_string: case s_req_query_string:
case s_req_fragment_start: case s_req_fragment_start:
case s_req_fragment: case s_req_fragment:
{ {
switch (ch) { switch (ch) {
case ' ': case ' ':
parser->state = s_req_http_start; UPDATE_STATE(s_req_http_start);
CALLBACK_DATA(url); CALLBACK_DATA(url);
break; break;
case CR: case CR:
case LF: case LF:
parser->http_major = 0; parser->http_major = 0;
parser->http_minor = 9; parser->http_minor = 9;
parser->state = (ch == CR) ? UPDATE_STATE((ch == CR) ?
s_req_line_almost_done : s_req_line_almost_done :
s_header_field_start; s_header_field_start);
CALLBACK_DATA(url); CALLBACK_DATA(url);
break; break;
default: default:
parser->state = parse_url_char((enum state)parser->state, ch); UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch));
if (parser->state == s_dead) { if (UNLIKELY(CURRENT_STATE() == s_dead)) {
SET_ERRNO(HPE_INVALID_URL); SET_ERRNO(HPE_INVALID_URL);
goto error; goto error;
} }
} }
break; break;
} }
case s_req_http_start: case s_req_http_start:
switch (ch) { switch (ch) {
case 'H': case 'H':
parser->state = s_req_http_H; UPDATE_STATE(s_req_http_H);
break; break;
case ' ': case ' ':
break; break;
default: default:
SET_ERRNO(HPE_INVALID_CONSTANT); SET_ERRNO(HPE_INVALID_CONSTANT);
goto error; goto error;
} }
break; break;
case s_req_http_H: case s_req_http_H:
STRICT_CHECK(ch != 'T'); STRICT_CHECK(ch != 'T');
parser->state = s_req_http_HT; UPDATE_STATE(s_req_http_HT);
break; break;
case s_req_http_HT: case s_req_http_HT:
STRICT_CHECK(ch != 'T'); STRICT_CHECK(ch != 'T');
parser->state = s_req_http_HTT; UPDATE_STATE(s_req_http_HTT);
break; break;
case s_req_http_HTT: case s_req_http_HTT:
STRICT_CHECK(ch != 'P'); STRICT_CHECK(ch != 'P');
parser->state = s_req_http_HTTP; UPDATE_STATE(s_req_http_HTTP);
break; break;
case s_req_http_HTTP: case s_req_http_HTTP:
STRICT_CHECK(ch != '/'); STRICT_CHECK(ch != '/');
parser->state = s_req_first_http_major; UPDATE_STATE(s_req_http_major);
break; break;
/* first digit of major HTTP version */ case s_req_http_major:
case s_req_first_http_major: if (UNLIKELY(!IS_NUM(ch))) {
if (ch < '1' || ch > '9') {
SET_ERRNO(HPE_INVALID_VERSION); SET_ERRNO(HPE_INVALID_VERSION);
goto error; goto error;
} }
parser->http_major = ch - '0'; parser->http_major = ch - '0';
parser->state = s_req_http_major; UPDATE_STATE(s_req_http_dot);
break; break;
/* major HTTP version or dot */ case s_req_http_dot:
case s_req_http_major:
{ {
if (ch == '.') { if (UNLIKELY(ch != '.')) {
parser->state = s_req_first_http_minor;
break;
}
if (!IS_NUM(ch)) {
SET_ERRNO(HPE_INVALID_VERSION);
goto error;
}
parser->http_major *= 10;
parser->http_major += ch - '0';
if (parser->http_major > 999) {
SET_ERRNO(HPE_INVALID_VERSION); SET_ERRNO(HPE_INVALID_VERSION);
goto error; goto error;
} }
UPDATE_STATE(s_req_http_minor);
break; break;
} }
/* first digit of minor HTTP version */ case s_req_http_minor:
case s_req_first_http_minor: if (UNLIKELY(!IS_NUM(ch))) {
if (!IS_NUM(ch)) {
SET_ERRNO(HPE_INVALID_VERSION); SET_ERRNO(HPE_INVALID_VERSION);
goto error; goto error;
} }
parser->http_minor = ch - '0'; parser->http_minor = ch - '0';
parser->state = s_req_http_minor; UPDATE_STATE(s_req_http_end);
break; break;
/* minor HTTP version or end of request line */ case s_req_http_end:
case s_req_http_minor:
{ {
if (ch == CR) { if (ch == CR) {
parser->state = s_req_line_almost_done; UPDATE_STATE(s_req_line_almost_done);
break; break;
} }
if (ch == LF) { if (ch == LF) {
parser->state = s_header_field_start; UPDATE_STATE(s_header_field_start);
break; break;
} }
/* XXX allow spaces after digit? */ SET_ERRNO(HPE_INVALID_VERSION);
goto error;
if (!IS_NUM(ch)) {
SET_ERRNO(HPE_INVALID_VERSION);
goto error;
}
parser->http_minor *= 10;
parser->http_minor += ch - '0';
if (parser->http_minor > 999) {
SET_ERRNO(HPE_INVALID_VERSION);
goto error;
}
break; break;
} }
/* end of request line */ /* end of request line */
case s_req_line_almost_done: case s_req_line_almost_done:
{ {
if (ch != LF) { if (UNLIKELY(ch != LF)) {
SET_ERRNO(HPE_LF_EXPECTED); SET_ERRNO(HPE_LF_EXPECTED);
goto error; goto error;
} }
parser->state = s_header_field_start; UPDATE_STATE(s_header_field_start);
break; break;
} }
case s_header_field_start: case s_header_field_start:
{ {
if (ch == CR) { if (ch == CR) {
parser->state = s_headers_almost_done; UPDATE_STATE(s_headers_almost_done);
break; break;
} }
if (ch == LF) { if (ch == LF) {
/* they might be just sending \n instead of \r\n so this would be /* they might be just sending \n instead of \r\n so this would be
* the second \n to denote the end of headers*/ * the second \n to denote the end of headers*/
parser->state = s_headers_almost_done; UPDATE_STATE(s_headers_almost_done);
goto reexecute_byte; REEXECUTE();
} }
c = TOKEN(ch); c = TOKEN(ch);
if (!c) { if (UNLIKELY(!c)) {
SET_ERRNO(HPE_INVALID_HEADER_TOKEN); SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
goto error; goto error;
} }
MARK(header_field); MARK(header_field);
parser->index = 0; parser->index = 0;
parser->state = s_header_field; UPDATE_STATE(s_header_field);
switch (c) { switch (c) {
case 'c': case 'c':
parser->header_state = h_C; parser->header_state = h_C;
break; break;
case 'p': case 'p':
parser->header_state = h_matching_proxy_connection; parser->header_state = h_matching_proxy_connection;
break; break;
skipping to change at line 1266 skipping to change at line 1220
default: default:
parser->header_state = h_general; parser->header_state = h_general;
break; break;
} }
break; break;
} }
case s_header_field: case s_header_field:
{ {
c = TOKEN(ch); const char* start = p;
for (; p != data + len; p++) {
ch = *p;
c = TOKEN(ch);
if (!c)
break;
if (c) {
switch (parser->header_state) { switch (parser->header_state) {
case h_general: case h_general:
break; break;
case h_C: case h_C:
parser->index++; parser->index++;
parser->header_state = (c == 'o' ? h_CO : h_general); parser->header_state = (c == 'o' ? h_CO : h_general);
break; break;
case h_CO: case h_CO:
skipping to change at line 1369 skipping to change at line 1328
case h_content_length: case h_content_length:
case h_transfer_encoding: case h_transfer_encoding:
case h_upgrade: case h_upgrade:
if (ch != ' ') parser->header_state = h_general; if (ch != ' ') parser->header_state = h_general;
break; break;
default: default:
assert(0 && "Unknown header_state"); assert(0 && "Unknown header_state");
break; break;
} }
break;
} }
if (ch == ':') { COUNT_HEADER_SIZE(p - start);
parser->state = s_header_value_discard_ws;
CALLBACK_DATA(header_field);
break;
}
if (ch == CR) { if (p == data + len) {
parser->state = s_header_almost_done; --p;
CALLBACK_DATA(header_field);
break; break;
} }
if (ch == LF) { if (ch == ':') {
parser->state = s_header_field_start; UPDATE_STATE(s_header_value_discard_ws);
CALLBACK_DATA(header_field); CALLBACK_DATA(header_field);
break; break;
} }
SET_ERRNO(HPE_INVALID_HEADER_TOKEN); SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
goto error; goto error;
} }
case s_header_value_discard_ws: case s_header_value_discard_ws:
if (ch == ' ' || ch == '\t') break; if (ch == ' ' || ch == '\t') break;
if (ch == CR) { if (ch == CR) {
parser->state = s_header_value_discard_ws_almost_done; UPDATE_STATE(s_header_value_discard_ws_almost_done);
break; break;
} }
if (ch == LF) { if (ch == LF) {
parser->state = s_header_value_discard_lws; UPDATE_STATE(s_header_value_discard_lws);
break; break;
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
case s_header_value_start: case s_header_value_start:
{ {
MARK(header_value); MARK(header_value);
parser->state = s_header_value; UPDATE_STATE(s_header_value);
parser->index = 0; parser->index = 0;
c = LOWER(ch); c = LOWER(ch);
switch (parser->header_state) { switch (parser->header_state) {
case h_upgrade: case h_upgrade:
parser->flags |= F_UPGRADE; parser->flags |= F_UPGRADE;
parser->header_state = h_general; parser->header_state = h_general;
break; break;
case h_transfer_encoding: case h_transfer_encoding:
/* looking for 'Transfer-Encoding: chunked' */ /* looking for 'Transfer-Encoding: chunked' */
if ('c' == c) { if ('c' == c) {
parser->header_state = h_matching_transfer_encoding_chunked; parser->header_state = h_matching_transfer_encoding_chunked;
} else { } else {
parser->header_state = h_general; parser->header_state = h_general;
} }
break; break;
case h_content_length: case h_content_length:
if (!IS_NUM(ch)) { if (UNLIKELY(!IS_NUM(ch))) {
SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
goto error; goto error;
} }
if (parser->flags & F_CONTENTLENGTH) {
SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
goto error;
}
parser->flags |= F_CONTENTLENGTH;
parser->content_length = ch - '0'; parser->content_length = ch - '0';
break; break;
case h_connection: case h_connection:
/* looking for 'Connection: keep-alive' */ /* looking for 'Connection: keep-alive' */
if (c == 'k') { if (c == 'k') {
parser->header_state = h_matching_connection_keep_alive; parser->header_state = h_matching_connection_keep_alive;
/* looking for 'Connection: close' */ /* looking for 'Connection: close' */
} else if (c == 'c') { } else if (c == 'c') {
parser->header_state = h_matching_connection_close; parser->header_state = h_matching_connection_close;
} else if (c == 'u') {
parser->header_state = h_matching_connection_upgrade;
} else { } else {
parser->header_state = h_general; parser->header_state = h_matching_connection_token;
} }
break; break;
/* Multi-value `Connection` header */
case h_matching_connection_token_start:
break;
default: default:
parser->header_state = h_general; parser->header_state = h_general;
break; break;
} }
break; break;
} }
case s_header_value: case s_header_value:
{ {
const char* start = p;
enum header_states h_state = (enum header_states) parser->header_state;
for (; p != data + len; p++) {
ch = *p;
if (ch == CR) {
UPDATE_STATE(s_header_almost_done);
parser->header_state = h_state;
CALLBACK_DATA(header_value);
break;
}
if (ch == CR) { if (ch == LF) {
parser->state = s_header_almost_done; UPDATE_STATE(s_header_almost_done);
CALLBACK_DATA(header_value); COUNT_HEADER_SIZE(p - start);
break; parser->header_state = h_state;
} CALLBACK_DATA_NOADVANCE(header_value);
REEXECUTE();
}
if (ch == LF) { if (!lenient && !IS_HEADER_CHAR(ch)) {
parser->state = s_header_almost_done; SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
CALLBACK_DATA_NOADVANCE(header_value); goto error;
goto reexecute_byte; }
}
c = LOWER(ch); c = LOWER(ch);
switch (parser->header_state) { switch (h_state) {
case h_general: case h_general:
break; {
const char* p_cr;
const char* p_lf;
size_t limit = data + len - p;
limit = MIN(limit, HTTP_MAX_HEADER_SIZE);
p_cr = (const char*) memchr(p, CR, limit);
p_lf = (const char*) memchr(p, LF, limit);
if (p_cr != NULL) {
if (p_lf != NULL && p_cr >= p_lf)
p = p_lf;
else
p = p_cr;
} else if (UNLIKELY(p_lf != NULL)) {
p = p_lf;
} else {
p = data + len;
}
--p;
case h_connection: break;
case h_transfer_encoding: }
assert(0 && "Shouldn't get here.");
break;
case h_content_length: case h_connection:
{ case h_transfer_encoding:
uint64_t t; assert(0 && "Shouldn't get here.");
break;
if (ch == ' ') break; case h_content_length:
{
uint64_t t;
if (!IS_NUM(ch)) { if (ch == ' ') break;
SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
goto error;
}
t = parser->content_length; if (UNLIKELY(!IS_NUM(ch))) {
t *= 10; SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
t += ch - '0'; parser->header_state = h_state;
goto error;
}
/* Overflow? Test against a conservative limit for simplicity. */ t = parser->content_length;
if ((ULLONG_MAX - 10) / 10 < parser->content_length) { t *= 10;
SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); t += ch - '0';
goto error;
/* Overflow? Test against a conservative limit for simplicity. */
if (UNLIKELY((ULLONG_MAX - 10) / 10 < parser->content_length)) {
SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
parser->header_state = h_state;
goto error;
}
parser->content_length = t;
break;
} }
parser->content_length = t; /* Transfer-Encoding: chunked */
break; case h_matching_transfer_encoding_chunked:
} parser->index++;
if (parser->index > sizeof(CHUNKED)-1
|| c != CHUNKED[parser->index]) {
h_state = h_general;
} else if (parser->index == sizeof(CHUNKED)-2) {
h_state = h_transfer_encoding_chunked;
}
break;
/* Transfer-Encoding: chunked */ case h_matching_connection_token_start:
case h_matching_transfer_encoding_chunked: /* looking for 'Connection: keep-alive' */
parser->index++; if (c == 'k') {
if (parser->index > sizeof(CHUNKED)-1 h_state = h_matching_connection_keep_alive;
|| c != CHUNKED[parser->index]) { /* looking for 'Connection: close' */
parser->header_state = h_general; } else if (c == 'c') {
} else if (parser->index == sizeof(CHUNKED)-2) { h_state = h_matching_connection_close;
parser->header_state = h_transfer_encoding_chunked; } else if (c == 'u') {
} h_state = h_matching_connection_upgrade;
break; } else if (STRICT_TOKEN(c)) {
h_state = h_matching_connection_token;
} else if (c == ' ' || c == '\t') {
/* Skip lws */
} else {
h_state = h_general;
}
break;
/* looking for 'Connection: keep-alive' */ /* looking for 'Connection: keep-alive' */
case h_matching_connection_keep_alive: case h_matching_connection_keep_alive:
parser->index++; parser->index++;
if (parser->index > sizeof(KEEP_ALIVE)-1 if (parser->index > sizeof(KEEP_ALIVE)-1
|| c != KEEP_ALIVE[parser->index]) { || c != KEEP_ALIVE[parser->index]) {
parser->header_state = h_general; h_state = h_matching_connection_token;
} else if (parser->index == sizeof(KEEP_ALIVE)-2) { } else if (parser->index == sizeof(KEEP_ALIVE)-2) {
parser->header_state = h_connection_keep_alive; h_state = h_connection_keep_alive;
} }
break; break;
/* looking for 'Connection: close' */ /* looking for 'Connection: close' */
case h_matching_connection_close: case h_matching_connection_close:
parser->index++; parser->index++;
if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) { if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index])
parser->header_state = h_general; {
} else if (parser->index == sizeof(CLOSE)-2) { h_state = h_matching_connection_token;
parser->header_state = h_connection_close; } else if (parser->index == sizeof(CLOSE)-2) {
} h_state = h_connection_close;
break; }
break;
case h_transfer_encoding_chunked: /* looking for 'Connection: upgrade' */
case h_connection_keep_alive: case h_matching_connection_upgrade:
case h_connection_close: parser->index++;
if (ch != ' ') parser->header_state = h_general; if (parser->index > sizeof(UPGRADE) - 1 ||
break; c != UPGRADE[parser->index]) {
h_state = h_matching_connection_token;
} else if (parser->index == sizeof(UPGRADE)-2) {
h_state = h_connection_upgrade;
}
break;
default: case h_matching_connection_token:
parser->state = s_header_value; if (ch == ',') {
parser->header_state = h_general; h_state = h_matching_connection_token_start;
break; parser->index = 0;
}
break;
case h_transfer_encoding_chunked:
if (ch != ' ') h_state = h_general;
break;
case h_connection_keep_alive:
case h_connection_close:
case h_connection_upgrade:
if (ch == ',') {
if (h_state == h_connection_keep_alive) {
parser->flags |= F_CONNECTION_KEEP_ALIVE;
} else if (h_state == h_connection_close) {
parser->flags |= F_CONNECTION_CLOSE;
} else if (h_state == h_connection_upgrade) {
parser->flags |= F_CONNECTION_UPGRADE;
}
h_state = h_matching_connection_token_start;
parser->index = 0;
} else if (ch != ' ') {
h_state = h_matching_connection_token;
}
break;
default:
UPDATE_STATE(s_header_value);
h_state = h_general;
break;
}
} }
parser->header_state = h_state;
COUNT_HEADER_SIZE(p - start);
if (p == data + len)
--p;
break; break;
} }
case s_header_almost_done: case s_header_almost_done:
{ {
STRICT_CHECK(ch != LF); if (UNLIKELY(ch != LF)) {
SET_ERRNO(HPE_LF_EXPECTED);
goto error;
}
parser->state = s_header_value_lws; UPDATE_STATE(s_header_value_lws);
break; break;
} }
case s_header_value_lws: case s_header_value_lws:
{ {
if (ch == ' ' || ch == '\t') { if (ch == ' ' || ch == '\t') {
parser->state = s_header_value_start; UPDATE_STATE(s_header_value_start);
goto reexecute_byte; REEXECUTE();
} }
/* finished the header */ /* finished the header */
switch (parser->header_state) { switch (parser->header_state) {
case h_connection_keep_alive: case h_connection_keep_alive:
parser->flags |= F_CONNECTION_KEEP_ALIVE; parser->flags |= F_CONNECTION_KEEP_ALIVE;
break; break;
case h_connection_close: case h_connection_close:
parser->flags |= F_CONNECTION_CLOSE; parser->flags |= F_CONNECTION_CLOSE;
break; break;
case h_transfer_encoding_chunked: case h_transfer_encoding_chunked:
parser->flags |= F_CHUNKED; parser->flags |= F_CHUNKED;
break; break;
case h_connection_upgrade:
parser->flags |= F_CONNECTION_UPGRADE;
break;
default: default:
break; break;
} }
parser->state = s_header_field_start; UPDATE_STATE(s_header_field_start);
goto reexecute_byte; REEXECUTE();
} }
case s_header_value_discard_ws_almost_done: case s_header_value_discard_ws_almost_done:
{ {
STRICT_CHECK(ch != LF); STRICT_CHECK(ch != LF);
parser->state = s_header_value_discard_lws; UPDATE_STATE(s_header_value_discard_lws);
break; break;
} }
case s_header_value_discard_lws: case s_header_value_discard_lws:
{ {
if (ch == ' ' || ch == '\t') { if (ch == ' ' || ch == '\t') {
parser->state = s_header_value_discard_ws; UPDATE_STATE(s_header_value_discard_ws);
break; break;
} else { } else {
switch (parser->header_state) {
case h_connection_keep_alive:
parser->flags |= F_CONNECTION_KEEP_ALIVE;
break;
case h_connection_close:
parser->flags |= F_CONNECTION_CLOSE;
break;
case h_connection_upgrade:
parser->flags |= F_CONNECTION_UPGRADE;
break;
case h_transfer_encoding_chunked:
parser->flags |= F_CHUNKED;
break;
default:
break;
}
/* header value was empty */ /* header value was empty */
MARK(header_value); MARK(header_value);
parser->state = s_header_field_start; UPDATE_STATE(s_header_field_start);
CALLBACK_DATA_NOADVANCE(header_value); CALLBACK_DATA_NOADVANCE(header_value);
goto reexecute_byte; REEXECUTE();
} }
} }
case s_headers_almost_done: case s_headers_almost_done:
{ {
STRICT_CHECK(ch != LF); STRICT_CHECK(ch != LF);
if (parser->flags & F_TRAILING) { if (parser->flags & F_TRAILING) {
/* End of a chunked request */ /* End of a chunked request */
parser->state = NEW_MESSAGE(); UPDATE_STATE(s_message_done);
CALLBACK_NOTIFY(message_complete); CALLBACK_NOTIFY_NOADVANCE(chunk_complete);
break; 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;
} }
parser->state = s_headers_done; UPDATE_STATE(s_headers_done);
/* Set this here so that on_headers_complete() callbacks can see it */ /* Set this here so that on_headers_complete() callbacks can see it */
parser->upgrade = if ((parser->flags & F_UPGRADE) &&
(parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT); (parser->flags & F_CONNECTION_UPGRADE)) {
/* For responses, "Upgrade: foo" and "Connection: upgrade" are
* mandatory only when it is a 101 Switching Protocols response,
* otherwise it is purely informational, to announce support.
*/
parser->upgrade =
(parser->type == HTTP_REQUEST || parser->status_code == 101);
} else {
parser->upgrade = (parser->method == HTTP_CONNECT);
}
/* Here we call the headers_complete callback. This is somewhat /* Here we call the headers_complete callback. This is somewhat
* different than other callbacks because if the user returns 1, we * different than other callbacks because if the user returns 1, we
* will interpret that as saying that this message has no body. This * will interpret that as saying that this message has no body. This
* is needed for the annoying case of recieving a response to a HEAD * is needed for the annoying case of recieving a response to a HEAD
* request. * request.
* *
* We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
* we have to simulate it by handling a change in errno below. * we have to simulate it by handling a change in errno below.
*/ */
if (settings->on_headers_complete) { if (settings->on_headers_complete) {
switch (settings->on_headers_complete(parser)) { switch (settings->on_headers_complete(parser)) {
case 0: case 0:
break; break;
case 2:
parser->upgrade = 1;
/* FALLTHROUGH */
case 1: case 1:
parser->flags |= F_SKIPBODY; parser->flags |= F_SKIPBODY;
break; break;
default: default:
SET_ERRNO(HPE_CB_headers_complete); SET_ERRNO(HPE_CB_headers_complete);
return p - data; /* Error */ RETURN(p - data); /* Error */
} }
} }
if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
return p - data; RETURN(p - data);
} }
goto reexecute_byte; REEXECUTE();
} }
case s_headers_done: case s_headers_done:
{ {
int hasBody;
STRICT_CHECK(ch != LF); STRICT_CHECK(ch != LF);
parser->nread = 0; parser->nread = 0;
/* Exit, the rest of the connect is in a different protocol. */ hasBody = parser->flags & F_CHUNKED ||
if (parser->upgrade) { (parser->content_length > 0 && parser->content_length != ULLONG_MAX);
parser->state = NEW_MESSAGE(); 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); CALLBACK_NOTIFY(message_complete);
return (p - data) + 1; RETURN((p - data) + 1);
} }
if (parser->flags & F_SKIPBODY) { if (parser->flags & F_SKIPBODY) {
parser->state = NEW_MESSAGE(); UPDATE_STATE(NEW_MESSAGE());
CALLBACK_NOTIFY(message_complete); CALLBACK_NOTIFY(message_complete);
} else if (parser->flags & F_CHUNKED) { } else if (parser->flags & F_CHUNKED) {
/* chunked encoding - ignore Content-Length header */ /* chunked encoding - ignore Content-Length header */
parser->state = s_chunk_size_start; UPDATE_STATE(s_chunk_size_start);
} else { } else {
if (parser->content_length == 0) { if (parser->content_length == 0) {
/* Content-Length header given but zero: Content-Length: 0\r\n */ /* Content-Length header given but zero: Content-Length: 0\r\n */
parser->state = NEW_MESSAGE(); UPDATE_STATE(NEW_MESSAGE());
CALLBACK_NOTIFY(message_complete); CALLBACK_NOTIFY(message_complete);
} else if (parser->content_length != ULLONG_MAX) { } else if (parser->content_length != ULLONG_MAX) {
/* Content-Length header given and non-zero */ /* Content-Length header given and non-zero */
parser->state = s_body_identity; UPDATE_STATE(s_body_identity);
} else { } else {
if (parser->type == HTTP_REQUEST || if (!http_message_needs_eof(parser)) {
!http_message_needs_eof(parser)) {
/* Assume content-length 0 - read the next */ /* Assume content-length 0 - read the next */
parser->state = NEW_MESSAGE(); UPDATE_STATE(NEW_MESSAGE());
CALLBACK_NOTIFY(message_complete); CALLBACK_NOTIFY(message_complete);
} else { } else {
/* Read body until EOF */ /* Read body until EOF */
parser->state = s_body_identity_eof; UPDATE_STATE(s_body_identity_eof);
} }
} }
} }
break; break;
} }
case s_body_identity: case s_body_identity:
{ {
uint64_t to_read = MIN(parser->content_length, uint64_t to_read = MIN(parser->content_length,
skipping to change at line 1722 skipping to change at line 1828
/* The difference between advancing content_length and p is because /* The difference between advancing content_length and p is because
* the latter will automaticaly advance on the next loop iteration. * the latter will automaticaly advance on the next loop iteration.
* Further, if content_length ends up at 0, we want to see the last * Further, if content_length ends up at 0, we want to see the last
* byte again for our message complete callback. * byte again for our message complete callback.
*/ */
MARK(body); MARK(body);
parser->content_length -= to_read; parser->content_length -= to_read;
p += to_read - 1; p += to_read - 1;
if (parser->content_length == 0) { if (parser->content_length == 0) {
parser->state = s_message_done; UPDATE_STATE(s_message_done);
/* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte. /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte.
* *
* The alternative to doing this is to wait for the next byte to * The alternative to doing this is to wait for the next byte to
* trigger the data callback, just as in every other case. The * trigger the data callback, just as in every other case. The
* problem with this is that this makes it difficult for the test * problem with this is that this makes it difficult for the test
* harness to distinguish between complete-on-EOF and * harness to distinguish between complete-on-EOF and
* complete-on-length. It's not clear that this distinction is * complete-on-length. It's not clear that this distinction is
* important for applications, but let's keep it for now. * important for applications, but let's keep it for now.
*/ */
CALLBACK_DATA_(body, p - body_mark + 1, p - data); CALLBACK_DATA_(body, p - body_mark + 1, p - data);
goto reexecute_byte; REEXECUTE();
} }
break; break;
} }
/* read until EOF */ /* read until EOF */
case s_body_identity_eof: case s_body_identity_eof:
MARK(body); MARK(body);
p = data + len - 1; p = data + len - 1;
break; break;
case s_message_done: case s_message_done:
parser->state = NEW_MESSAGE(); UPDATE_STATE(NEW_MESSAGE());
CALLBACK_NOTIFY(message_complete); CALLBACK_NOTIFY(message_complete);
if (parser->upgrade) {
/* Exit, the rest of the message is in a different protocol. */
RETURN((p - data) + 1);
}
break; break;
case s_chunk_size_start: case s_chunk_size_start:
{ {
assert(parser->nread == 1); assert(parser->nread == 1);
assert(parser->flags & F_CHUNKED); assert(parser->flags & F_CHUNKED);
unhex_val = unhex[(unsigned char)ch]; unhex_val = unhex[(unsigned char)ch];
if (unhex_val == -1) { if (UNLIKELY(unhex_val == -1)) {
SET_ERRNO(HPE_INVALID_CHUNK_SIZE); SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
goto error; goto error;
} }
parser->content_length = unhex_val; parser->content_length = unhex_val;
parser->state = s_chunk_size; UPDATE_STATE(s_chunk_size);
break; break;
} }
case s_chunk_size: case s_chunk_size:
{ {
uint64_t t; uint64_t t;
assert(parser->flags & F_CHUNKED); assert(parser->flags & F_CHUNKED);
if (ch == CR) { if (ch == CR) {
parser->state = s_chunk_size_almost_done; UPDATE_STATE(s_chunk_size_almost_done);
break; break;
} }
unhex_val = unhex[(unsigned char)ch]; unhex_val = unhex[(unsigned char)ch];
if (unhex_val == -1) { if (unhex_val == -1) {
if (ch == ';' || ch == ' ') { if (ch == ';' || ch == ' ') {
parser->state = s_chunk_parameters; UPDATE_STATE(s_chunk_parameters);
break; break;
} }
SET_ERRNO(HPE_INVALID_CHUNK_SIZE); SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
goto error; goto error;
} }
t = parser->content_length; t = parser->content_length;
t *= 16; t *= 16;
t += unhex_val; t += unhex_val;
/* Overflow? Test against a conservative limit for simplicity. */ /* Overflow? Test against a conservative limit for simplicity. */
if ((ULLONG_MAX - 16) / 16 < parser->content_length) { if (UNLIKELY((ULLONG_MAX - 16) / 16 < parser->content_length)) {
SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
goto error; goto error;
} }
parser->content_length = t; parser->content_length = t;
break; break;
} }
case s_chunk_parameters: case s_chunk_parameters:
{ {
assert(parser->flags & F_CHUNKED); assert(parser->flags & F_CHUNKED);
/* just ignore this shit. TODO check for overflow */ /* just ignore this shit. TODO check for overflow */
if (ch == CR) { if (ch == CR) {
parser->state = s_chunk_size_almost_done; UPDATE_STATE(s_chunk_size_almost_done);
break; break;
} }
break; break;
} }
case s_chunk_size_almost_done: case s_chunk_size_almost_done:
{ {
assert(parser->flags & F_CHUNKED); assert(parser->flags & F_CHUNKED);
STRICT_CHECK(ch != LF); STRICT_CHECK(ch != LF);
parser->nread = 0; parser->nread = 0;
if (parser->content_length == 0) { if (parser->content_length == 0) {
parser->flags |= F_TRAILING; parser->flags |= F_TRAILING;
parser->state = s_header_field_start; UPDATE_STATE(s_header_field_start);
} else { } else {
parser->state = s_chunk_data; UPDATE_STATE(s_chunk_data);
} }
CALLBACK_NOTIFY(chunk_header);
break; break;
} }
case s_chunk_data: case s_chunk_data:
{ {
uint64_t to_read = MIN(parser->content_length, uint64_t to_read = MIN(parser->content_length,
(uint64_t) ((data + len) - p)); (uint64_t) ((data + len) - p));
assert(parser->flags & F_CHUNKED); assert(parser->flags & F_CHUNKED);
assert(parser->content_length != 0 assert(parser->content_length != 0
&& parser->content_length != ULLONG_MAX); && parser->content_length != ULLONG_MAX);
/* See the explanation in s_body_identity for why the content /* See the explanation in s_body_identity for why the content
* length and data pointers are managed this way. * length and data pointers are managed this way.
*/ */
MARK(body); MARK(body);
parser->content_length -= to_read; parser->content_length -= to_read;
p += to_read - 1; p += to_read - 1;
if (parser->content_length == 0) { if (parser->content_length == 0) {
parser->state = s_chunk_data_almost_done; UPDATE_STATE(s_chunk_data_almost_done);
} }
break; break;
} }
case s_chunk_data_almost_done: case s_chunk_data_almost_done:
assert(parser->flags & F_CHUNKED); assert(parser->flags & F_CHUNKED);
assert(parser->content_length == 0); assert(parser->content_length == 0);
STRICT_CHECK(ch != CR); STRICT_CHECK(ch != CR);
parser->state = s_chunk_data_done; UPDATE_STATE(s_chunk_data_done);
CALLBACK_DATA(body); CALLBACK_DATA(body);
break; break;
case s_chunk_data_done: case s_chunk_data_done:
assert(parser->flags & F_CHUNKED); assert(parser->flags & F_CHUNKED);
STRICT_CHECK(ch != LF); STRICT_CHECK(ch != LF);
parser->nread = 0; parser->nread = 0;
parser->state = s_chunk_size_start; UPDATE_STATE(s_chunk_size_start);
CALLBACK_NOTIFY(chunk_complete);
break; break;
default: default:
assert(0 && "unhandled state"); assert(0 && "unhandled state");
SET_ERRNO(HPE_INVALID_INTERNAL_STATE); SET_ERRNO(HPE_INVALID_INTERNAL_STATE);
goto error; goto error;
} }
} }
/* Run callbacks for any marks that we have leftover after we ran our of /* Run callbacks for any marks that we have leftover after we ran our of
skipping to change at line 1899 skipping to change at line 2011
(url_mark ? 1 : 0) + (url_mark ? 1 : 0) +
(body_mark ? 1 : 0) + (body_mark ? 1 : 0) +
(status_mark ? 1 : 0)) <= 1); (status_mark ? 1 : 0)) <= 1);
CALLBACK_DATA_NOADVANCE(header_field); CALLBACK_DATA_NOADVANCE(header_field);
CALLBACK_DATA_NOADVANCE(header_value); CALLBACK_DATA_NOADVANCE(header_value);
CALLBACK_DATA_NOADVANCE(url); CALLBACK_DATA_NOADVANCE(url);
CALLBACK_DATA_NOADVANCE(body); CALLBACK_DATA_NOADVANCE(body);
CALLBACK_DATA_NOADVANCE(status); CALLBACK_DATA_NOADVANCE(status);
return len; RETURN(len);
error: error:
if (HTTP_PARSER_ERRNO(parser) == HPE_OK) { if (HTTP_PARSER_ERRNO(parser) == HPE_OK) {
SET_ERRNO(HPE_UNKNOWN); SET_ERRNO(HPE_UNKNOWN);
} }
return (p - data); RETURN(p - data);
} }
/* Does the parser need to see an EOF to find the end of the message? */ /* Does the parser need to see an EOF to find the end of the message? */
int int
http_message_needs_eof (const http_parser *parser) http_message_needs_eof (const http_parser *parser)
{ {
if (parser->type == HTTP_REQUEST) { if (parser->type == HTTP_REQUEST) {
return 0; return 0;
} }
skipping to change at line 1967 skipping to change at line 2079
http_parser_init (http_parser *parser, enum http_parser_type t) http_parser_init (http_parser *parser, enum http_parser_type t)
{ {
void *data = parser->data; /* preserve application data */ void *data = parser->data; /* preserve application data */
memset(parser, 0, sizeof(*parser)); memset(parser, 0, sizeof(*parser));
parser->data = data; parser->data = data;
parser->type = t; parser->type = t;
parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_sta rt_res : s_start_req_or_res)); parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_sta rt_res : s_start_req_or_res));
parser->http_errno = HPE_OK; parser->http_errno = HPE_OK;
} }
void
http_parser_settings_init(http_parser_settings *settings)
{
memset(settings, 0, sizeof(*settings));
}
const char * const char *
http_errno_name(enum http_errno err) { 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; return http_strerror_tab[err].name;
} }
const char * const char *
http_errno_description(enum http_errno err) { 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; return http_strerror_tab[err].description;
} }
static enum http_host_state static enum http_host_state
http_parse_host_char(enum http_host_state s, const char ch) { http_parse_host_char(enum http_host_state s, const char ch) {
switch(s) { switch(s) {
case s_http_userinfo: case s_http_userinfo:
case s_http_userinfo_start: case s_http_userinfo_start:
if (ch == '@') { if (ch == '@') {
return s_http_host_start; return s_http_host_start;
skipping to change at line 2028 skipping to change at line 2146
if (ch == ']') { if (ch == ']') {
return s_http_host_v6_end; return s_http_host_v6_end;
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
case s_http_host_v6_start: case s_http_host_v6_start:
if (IS_HEX(ch) || ch == ':' || ch == '.') { if (IS_HEX(ch) || ch == ':' || ch == '.') {
return s_http_host_v6; 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; break;
case s_http_host_port: case s_http_host_port:
case s_http_host_port_start: case s_http_host_port_start:
if (IS_NUM(ch)) { if (IS_NUM(ch)) {
return s_http_host_port; return s_http_host_port;
} }
break; break;
skipping to change at line 2051 skipping to change at line 2186
return s_http_host_dead; return s_http_host_dead;
} }
static int static int
http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
enum http_host_state s; enum http_host_state s;
const char *p; const char *p;
size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len; size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
assert(u->field_set & (1 << UF_HOST));
u->field_data[UF_HOST].len = 0; u->field_data[UF_HOST].len = 0;
s = found_at ? s_http_userinfo_start : s_http_host_start; s = found_at ? s_http_userinfo_start : s_http_host_start;
for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) { for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
enum http_host_state new_s = http_parse_host_char(s, *p); enum http_host_state new_s = http_parse_host_char(s, *p);
if (new_s == s_http_host_dead) { if (new_s == s_http_host_dead) {
return 1; return 1;
} }
skipping to change at line 2077 skipping to change at line 2214
u->field_data[UF_HOST].len++; u->field_data[UF_HOST].len++;
break; break;
case s_http_host_v6: case s_http_host_v6:
if (s != s_http_host_v6) { if (s != s_http_host_v6) {
u->field_data[UF_HOST].off = p - buf; u->field_data[UF_HOST].off = p - buf;
} }
u->field_data[UF_HOST].len++; u->field_data[UF_HOST].len++;
break; 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: case s_http_host_port:
if (s != s_http_host_port) { if (s != s_http_host_port) {
u->field_data[UF_PORT].off = p - buf; u->field_data[UF_PORT].off = p - buf;
u->field_data[UF_PORT].len = 0; u->field_data[UF_PORT].len = 0;
u->field_set |= (1 << UF_PORT); u->field_set |= (1 << UF_PORT);
} }
u->field_data[UF_PORT].len++; u->field_data[UF_PORT].len++;
break; break;
case s_http_userinfo: case s_http_userinfo:
skipping to change at line 2106 skipping to change at line 2248
break; break;
} }
s = new_s; s = new_s;
} }
/* Make sure we don't end somewhere unexpected */ /* Make sure we don't end somewhere unexpected */
switch (s) { switch (s) {
case s_http_host_start: case s_http_host_start:
case s_http_host_v6_start: case s_http_host_v6_start:
case s_http_host_v6: 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_host_port_start:
case s_http_userinfo: case s_http_userinfo:
case s_http_userinfo_start: case s_http_userinfo_start:
return 1; return 1;
default: default:
break; break;
} }
return 0; return 0;
} }
void
http_parser_url_init(struct http_parser_url *u) {
memset(u, 0, sizeof(*u));
}
int int
http_parser_parse_url(const char *buf, size_t buflen, int is_connect, http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
struct http_parser_url *u) struct http_parser_url *u)
{ {
enum state s; enum state s;
const char *p; const char *p;
enum http_parser_url_fields uf, old_uf; enum http_parser_url_fields uf, old_uf;
int found_at = 0; int found_at = 0;
u->port = u->field_set = 0; u->port = u->field_set = 0;
s = is_connect ? s_req_server_start : s_req_spaces_before_url; s = is_connect ? s_req_server_start : s_req_spaces_before_url;
uf = old_uf = UF_MAX; old_uf = UF_MAX;
for (p = buf; p < buf + buflen; p++) { for (p = buf; p < buf + buflen; p++) {
s = parse_url_char(s, *p); s = parse_url_char(s, *p);
/* Figure out the next field that we're operating on */ /* Figure out the next field that we're operating on */
switch (s) { switch (s) {
case s_dead: case s_dead:
return 1; return 1;
/* Skip delimeters */ /* Skip delimeters */
skipping to change at line 2153 skipping to change at line 2302
case s_req_fragment_start: case s_req_fragment_start:
continue; continue;
case s_req_schema: case s_req_schema:
uf = UF_SCHEMA; uf = UF_SCHEMA;
break; break;
case s_req_server_with_at: case s_req_server_with_at:
found_at = 1; found_at = 1;
/* FALLTROUGH */ /* FALLTHROUGH */
case s_req_server: case s_req_server:
uf = UF_HOST; uf = UF_HOST;
break; break;
case s_req_path: case s_req_path:
uf = UF_PATH; uf = UF_PATH;
break; break;
case s_req_query_string: case s_req_query_string:
uf = UF_QUERY; uf = UF_QUERY;
skipping to change at line 2190 skipping to change at line 2339
u->field_data[uf].off = p - buf; u->field_data[uf].off = p - buf;
u->field_data[uf].len = 1; u->field_data[uf].len = 1;
u->field_set |= (1 << uf); u->field_set |= (1 << uf);
old_uf = uf; old_uf = uf;
} }
/* host must be present if there is a schema */ /* host must be present if there is a schema */
/* parsing http:///toto will fail */ /* 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) { if (http_parse_host(buf, u, found_at) != 0) {
return 1; return 1;
} }
} }
/* CONNECT requests can only contain "hostname:port" */ /* CONNECT requests can only contain "hostname:port" */
if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) { if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) {
return 1; return 1;
} }
 End of changes. 188 change blocks. 
382 lines changed or deleted 536 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)