diff --git a/http_parser.h b/http_parser.h index 4cc4638..f4d6942 100644 --- a/http_parser.h +++ b/http_parser.h @@ -62,10 +62,12 @@ typedef int (*http_cb) (http_parser*); #define HTTP_IDENTITY 0x01 #define HTTP_CHUNKED 0x02 +enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE }; + struct http_parser { /** PRIVATE **/ int cs; - int is_request_stream; + enum http_parser_type type; size_t chunk_size; unsigned eating:1; @@ -112,7 +114,7 @@ struct http_parser { * it will be parsing requests or responses. Set the second argument to 1 * for requests; 0 for responses. */ -void http_parser_init (http_parser *parser, int is_request_stream); +void http_parser_init (http_parser *parser, enum http_parser_type); size_t http_parser_execute (http_parser *parser, const char *data, size_t len); diff --git a/http_parser.rl b/http_parser.rl index 6f8d435..820ecd6 100644 --- a/http_parser.rl +++ b/http_parser.rl @@ -170,7 +170,7 @@ static int unhex[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 action end_chunked_body { END_REQUEST //fnext main; - if(parser->is_request_stream) { + if(parser->type == HTTP_REQUEST) { fnext Requests; } else { fnext Responses; @@ -293,12 +293,9 @@ static int unhex[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 Requests := Request*; Responses := Response*; - #main := (Requests when { parser->is_request_stream })? - # | (Responses when { !parser->is_request_stream })? - # ; main := any >{ fhold; - if(parser->is_request_stream) { + if(parser->type == HTTP_REQUEST) { fgoto Requests; } else { fgoto Responses; @@ -327,20 +324,22 @@ skip_body(const char **p, http_parser *parser, size_t nskip) { } } -void http_parser_init(http_parser *parser, int is_request_stream) +void +http_parser_init (http_parser *parser, enum http_parser_type type) { memset(parser, 0, sizeof(struct http_parser)); int cs = 0; %% write init; parser->cs = cs; - parser->is_request_stream = is_request_stream; + parser->type = type; RESET_PARSER(parser); } /** exec **/ -size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len) +size_t +http_parser_execute(http_parser *parser, const char *buffer, size_t len) { const char *p, *pe; int cs = parser->cs; diff --git a/test.c b/test.c index cd60596..d6ae602 100644 --- a/test.c +++ b/test.c @@ -399,11 +399,11 @@ int begin_message (http_parser *_) } void -parser_init (void) +parser_init (enum http_parser_type type) { num_messages = 0; - http_parser_init(&parser, TRUE); + http_parser_init(&parser, type); memset(&messages, 0, sizeof messages); @@ -458,7 +458,7 @@ parse_messages (int message_count, const struct message *input_messages[]) // Parse the stream size_t traversed = 0; - parser_init(); + parser_init(HTTP_REQUEST); traversed = http_parser_execute(&parser, total, length); @@ -475,7 +475,7 @@ void test_request (const struct message *message) { size_t traversed = 0; - parser_init(); + parser_init(HTTP_REQUEST); traversed = http_parser_execute(&parser, message->raw, strlen(message->raw)); assert(!http_parser_has_error(&parser)); @@ -488,7 +488,7 @@ void test_error (const char *buf) { size_t traversed = 0; - parser_init(); + parser_init(HTTP_REQUEST); traversed = http_parser_execute(&parser, buf, strlen(buf)); @@ -511,7 +511,7 @@ test_multiple3 (const struct message *r1, const struct message *r2, const struct strcat(total, r3->raw); size_t traversed = 0; - parser_init(); + parser_init(HTTP_REQUEST); traversed = http_parser_execute(&parser, total, strlen(total)); @@ -553,7 +553,7 @@ test_scan (const struct message *r1, const struct message *r2, const struct mess } ops += 1; - parser_init(); + parser_init(HTTP_REQUEST); int buf1_len = i; strncpy(buf1, total, buf1_len);