|
|
@ -26,12 +26,6 @@ |
|
|
|
#include <limits.h> |
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
/* parser->flags */ |
|
|
|
#define EATING 0x01 |
|
|
|
#define ERROR 0x02 |
|
|
|
#define CHUNKED 0x04 |
|
|
|
#define EAT_FOREVER 0x10 |
|
|
|
|
|
|
|
static int unhex[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
|
|
|
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
|
|
|
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
|
|
@ -41,14 +35,12 @@ static int unhex[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
|
|
|
,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
|
|
|
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
|
|
|
}; |
|
|
|
#define TRUE 1 |
|
|
|
#define FALSE 0 |
|
|
|
#define MIN(a,b) (a < b ? a : b) |
|
|
|
#define NULL (void*)(0) |
|
|
|
|
|
|
|
#undef MIN |
|
|
|
#define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
|
|
|
|
|
|
#undef NULL |
|
|
|
#define NULL ((void*)(0)) |
|
|
|
|
|
|
|
#define MAX_FIELD_SIZE (80*1024) |
|
|
|
#define MAX_FIELD_SIZE 80*1024 |
|
|
|
|
|
|
|
#define REMAINING (unsigned long)(pe - p) |
|
|
|
#define CALLBACK(FOR) \ |
|
|
@ -56,24 +48,20 @@ do { \ |
|
|
|
if (parser->FOR##_mark) { \ |
|
|
|
parser->FOR##_size += p - parser->FOR##_mark; \ |
|
|
|
if (parser->FOR##_size > MAX_FIELD_SIZE) { \ |
|
|
|
parser->flags |= ERROR; \ |
|
|
|
return; \ |
|
|
|
parser->error = TRUE; \ |
|
|
|
return 0; \ |
|
|
|
} \ |
|
|
|
if (parser->on_##FOR) { \ |
|
|
|
callback_return_value = parser->on_##FOR(parser, \ |
|
|
|
parser->FOR##_mark, \ |
|
|
|
p - parser->FOR##_mark); \ |
|
|
|
} \ |
|
|
|
if (callback_return_value != 0) { \ |
|
|
|
parser->flags |= ERROR; \ |
|
|
|
return; \ |
|
|
|
} \ |
|
|
|
} \ |
|
|
|
} while(0) |
|
|
|
|
|
|
|
#define RESET_PARSER(parser) \ |
|
|
|
parser->chunk_size = 0; \ |
|
|
|
parser->flags = 0; \ |
|
|
|
parser->eating = 0; \ |
|
|
|
parser->header_field_mark = NULL; \ |
|
|
|
parser->header_value_mark = NULL; \ |
|
|
|
parser->query_string_mark = NULL; \ |
|
|
@ -82,10 +70,12 @@ do { \ |
|
|
|
parser->fragment_mark = NULL; \ |
|
|
|
parser->status_code = 0; \ |
|
|
|
parser->method = 0; \ |
|
|
|
parser->version = HTTP_VERSION_OTHER; \ |
|
|
|
parser->transfer_encoding = HTTP_IDENTITY; \ |
|
|
|
parser->version_major = 0; \ |
|
|
|
parser->version_minor = 0; \ |
|
|
|
parser->keep_alive = -1; \ |
|
|
|
parser->content_length = -1; \ |
|
|
|
parser->body_read = 0 |
|
|
|
parser->content_length = 0; \ |
|
|
|
parser->body_read = 0; |
|
|
|
|
|
|
|
#define END_REQUEST \ |
|
|
|
do { \ |
|
|
@ -107,12 +97,12 @@ do { \ |
|
|
|
parser->body_read += tmp; \ |
|
|
|
parser->chunk_size -= tmp; \ |
|
|
|
if (0 == parser->chunk_size) { \ |
|
|
|
parser->flags &= ~EATING; \ |
|
|
|
if (!(parser->flags & CHUNKED)) { \ |
|
|
|
parser->eating = FALSE; \ |
|
|
|
if (parser->transfer_encoding == HTTP_IDENTITY) { \ |
|
|
|
END_REQUEST; \ |
|
|
|
} \ |
|
|
|
} else { \ |
|
|
|
parser->flags |= EATING; \ |
|
|
|
parser->eating = TRUE; \ |
|
|
|
} \ |
|
|
|
} \ |
|
|
|
} while (0) |
|
|
@ -152,36 +142,60 @@ do { \ |
|
|
|
|
|
|
|
action header_field { |
|
|
|
CALLBACK(header_field); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
parser->header_field_mark = NULL; |
|
|
|
parser->header_field_size = 0; |
|
|
|
} |
|
|
|
|
|
|
|
action header_value { |
|
|
|
CALLBACK(header_value); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
parser->header_value_mark = NULL; |
|
|
|
parser->header_value_size = 0; |
|
|
|
} |
|
|
|
|
|
|
|
action request_uri { |
|
|
|
CALLBACK(uri); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
parser->uri_mark = NULL; |
|
|
|
parser->uri_size = 0; |
|
|
|
} |
|
|
|
|
|
|
|
action fragment { |
|
|
|
CALLBACK(fragment); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
parser->fragment_mark = NULL; |
|
|
|
parser->fragment_size = 0; |
|
|
|
} |
|
|
|
|
|
|
|
action query_string { |
|
|
|
CALLBACK(query_string); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
parser->query_string_mark = NULL; |
|
|
|
parser->query_string_size = 0; |
|
|
|
} |
|
|
|
|
|
|
|
action request_path { |
|
|
|
CALLBACK(path); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
parser->path_mark = NULL; |
|
|
|
parser->path_size = 0; |
|
|
|
} |
|
|
@ -190,8 +204,8 @@ do { \ |
|
|
|
if(parser->on_headers_complete) { |
|
|
|
callback_return_value = parser->on_headers_complete(parser); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->flags |= ERROR; |
|
|
|
return; |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -200,17 +214,16 @@ do { \ |
|
|
|
if(parser->on_message_begin) { |
|
|
|
callback_return_value = parser->on_message_begin(parser); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->flags |= ERROR; |
|
|
|
return; |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
action content_length { |
|
|
|
if (parser->content_length == -1) parser->content_length = 0; |
|
|
|
if (parser->content_length > INT_MAX) { |
|
|
|
parser->flags |= ERROR; |
|
|
|
return; |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
parser->content_length *= 10; |
|
|
|
parser->content_length += *p - '0'; |
|
|
@ -221,14 +234,21 @@ do { \ |
|
|
|
parser->status_code += *p - '0'; |
|
|
|
} |
|
|
|
|
|
|
|
action use_chunked_encoding { parser->flags |= CHUNKED; } |
|
|
|
action use_identity_encoding { parser->transfer_encoding = HTTP_IDENTITY; } |
|
|
|
action use_chunked_encoding { parser->transfer_encoding = HTTP_CHUNKED; } |
|
|
|
|
|
|
|
action set_keep_alive { parser->keep_alive = TRUE; } |
|
|
|
action set_not_keep_alive { parser->keep_alive = FALSE; } |
|
|
|
|
|
|
|
action set_keep_alive { parser->keep_alive = 1; } |
|
|
|
action set_not_keep_alive { parser->keep_alive = 0; } |
|
|
|
action version_major { |
|
|
|
parser->version_major *= 10; |
|
|
|
parser->version_major += *p - '0'; |
|
|
|
} |
|
|
|
|
|
|
|
action version_11 { parser->version = HTTP_VERSION_11; } |
|
|
|
action version_10 { parser->version = HTTP_VERSION_10; } |
|
|
|
action version_09 { parser->version = HTTP_VERSION_09; } |
|
|
|
action version_minor { |
|
|
|
parser->version_minor *= 10; |
|
|
|
parser->version_minor += *p - '0'; |
|
|
|
} |
|
|
|
|
|
|
|
action add_to_chunk_size { |
|
|
|
parser->chunk_size *= 16; |
|
|
@ -238,8 +258,8 @@ do { \ |
|
|
|
action skip_chunk_data { |
|
|
|
SKIP_BODY(MIN(parser->chunk_size, REMAINING)); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->flags |= ERROR; |
|
|
|
return; |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
fhold; |
|
|
@ -260,32 +280,18 @@ do { \ |
|
|
|
} |
|
|
|
|
|
|
|
action body_logic { |
|
|
|
if (parser->flags & CHUNKED) { |
|
|
|
if (parser->transfer_encoding == HTTP_CHUNKED) { |
|
|
|
fnext ChunkedBody; |
|
|
|
} else { |
|
|
|
/* this is pretty stupid. i'd prefer to combine this with |
|
|
|
* skip_chunk_data */ |
|
|
|
if (parser->content_length < 0) { |
|
|
|
/* If we didn't get a content length; if not keep-alive |
|
|
|
* just read body until EOF */ |
|
|
|
if (!http_parser_should_keep_alive(parser)) { |
|
|
|
parser->flags |= EAT_FOREVER; |
|
|
|
parser->chunk_size = REMAINING; |
|
|
|
} else { |
|
|
|
/* Otherwise, if keep-alive, then assume the message |
|
|
|
* has no body. */ |
|
|
|
parser->chunk_size = parser->content_length = 0; |
|
|
|
} |
|
|
|
} else { |
|
|
|
/* this is pretty stupid. i'd prefer to combine this with skip_chunk_data */ |
|
|
|
parser->chunk_size = parser->content_length; |
|
|
|
} |
|
|
|
p += 1; |
|
|
|
|
|
|
|
SKIP_BODY(MIN(REMAINING, parser->chunk_size)); |
|
|
|
SKIP_BODY(MIN(REMAINING, parser->content_length)); |
|
|
|
|
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->flags |= ERROR; |
|
|
|
return; |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
fhold; |
|
|
@ -336,11 +342,7 @@ do { \ |
|
|
|
| "UNLOCK" %{ parser->method = HTTP_UNLOCK; } |
|
|
|
); # Not allowing extension methods |
|
|
|
|
|
|
|
HTTP_Version = "HTTP/" ( "1.1" %version_11 |
|
|
|
| "1.0" %version_10 |
|
|
|
| "0.9" %version_09 |
|
|
|
| (digit "." digit) |
|
|
|
); |
|
|
|
HTTP_Version = "HTTP/" digit $version_major "." digit $version_minor; |
|
|
|
|
|
|
|
scheme = ( alpha | digit | "+" | "-" | "." )* ; |
|
|
|
absolute_uri = (scheme ":" (uchar | reserved )*); |
|
|
@ -367,7 +369,7 @@ do { \ |
|
|
|
| "close"i %set_not_keep_alive |
|
|
|
) |
|
|
|
) |
|
|
|
| ("Transfer-Encoding"i hsep "chunked"i %use_chunked_encoding) |
|
|
|
| ("Transfer-Encoding"i %use_chunked_encoding hsep "identity" %use_identity_encoding) |
|
|
|
| (Field_Name hsep Field_Value) |
|
|
|
) :> CRLF; |
|
|
|
|
|
|
@ -385,11 +387,11 @@ do { \ |
|
|
|
chunk_ext_val = token*; |
|
|
|
chunk_ext_name = token*; |
|
|
|
chunk_extension = ( ";" " "* chunk_ext_name ("=" chunk_ext_val)? )*; |
|
|
|
last_chunk = "0"+ ( chunk_extension | " "+) CRLF; |
|
|
|
chunk_size = (xdigit* [1-9a-fA-F] xdigit* ) $add_to_chunk_size; |
|
|
|
last_chunk = "0"+ chunk_extension CRLF; |
|
|
|
chunk_size = (xdigit* [1-9a-fA-F] xdigit*) $add_to_chunk_size; |
|
|
|
chunk_end = CRLF; |
|
|
|
chunk_body = any >skip_chunk_data; |
|
|
|
chunk_begin = chunk_size ( chunk_extension | " "+ ) CRLF; |
|
|
|
chunk_begin = chunk_size chunk_extension CRLF; |
|
|
|
chunk = chunk_begin chunk_body chunk_end; |
|
|
|
ChunkedBody := chunk* last_chunk trailing_headers CRLF @end_chunked_body; |
|
|
|
|
|
|
@ -419,6 +421,7 @@ http_parser_init (http_parser *parser, enum http_parser_type type) |
|
|
|
%% write init; |
|
|
|
parser->cs = cs; |
|
|
|
parser->type = type; |
|
|
|
parser->error = 0; |
|
|
|
|
|
|
|
parser->on_message_begin = NULL; |
|
|
|
parser->on_path = NULL; |
|
|
@ -435,39 +438,23 @@ http_parser_init (http_parser *parser, enum http_parser_type type) |
|
|
|
} |
|
|
|
|
|
|
|
/** exec **/ |
|
|
|
void |
|
|
|
size_t |
|
|
|
http_parser_execute (http_parser *parser, const char *buffer, size_t len) |
|
|
|
{ |
|
|
|
size_t tmp; // REMOVE ME this is extremely hacky |
|
|
|
int callback_return_value = 0; |
|
|
|
const char *p, *pe, *eof; |
|
|
|
const char *p, *pe; |
|
|
|
int cs = parser->cs; |
|
|
|
|
|
|
|
p = buffer; |
|
|
|
pe = buffer+len; |
|
|
|
eof = len ? NULL : pe; |
|
|
|
|
|
|
|
if (parser->flags & EAT_FOREVER) { |
|
|
|
if (len == 0) { |
|
|
|
if (parser->on_message_complete) { |
|
|
|
callback_return_value = parser->on_message_complete(parser); |
|
|
|
if (callback_return_value != 0) parser->flags |= ERROR; |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (parser->on_body) { |
|
|
|
callback_return_value = parser->on_body(parser, p, len); |
|
|
|
if (callback_return_value != 0) parser->flags |= ERROR; |
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (0 < parser->chunk_size && (parser->flags & EATING)) { |
|
|
|
if (0 < parser->chunk_size && parser->eating) { |
|
|
|
/* eat body */ |
|
|
|
SKIP_BODY(MIN(len, parser->chunk_size)); |
|
|
|
if (callback_return_value != 0) { |
|
|
|
parser->flags |= ERROR; |
|
|
|
return; |
|
|
|
parser->error = TRUE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -490,11 +477,26 @@ http_parser_execute (http_parser *parser, const char *buffer, size_t len) |
|
|
|
CALLBACK(uri); |
|
|
|
|
|
|
|
assert(p <= pe && "buffer overflow after parsing execute"); |
|
|
|
return(p - buffer); |
|
|
|
} |
|
|
|
|
|
|
|
int |
|
|
|
http_parser_has_error (http_parser *parser) |
|
|
|
{ |
|
|
|
if (parser->flags & ERROR) return 1; |
|
|
|
if (parser->error) return TRUE; |
|
|
|
return parser->cs == http_parser_error; |
|
|
|
} |
|
|
|
|
|
|
|
int |
|
|
|
http_parser_should_keep_alive (http_parser *parser) |
|
|
|
{ |
|
|
|
if (parser->keep_alive == -1) |
|
|
|
if (parser->version_major == 1) |
|
|
|
return (parser->version_minor != 0); |
|
|
|
else if (parser->version_major == 0) |
|
|
|
return FALSE; |
|
|
|
else |
|
|
|
return TRUE; |
|
|
|
else |
|
|
|
return parser->keep_alive; |
|
|
|
} |
|
|
|