for clarity, replace is_request_stream with enum http_parser_type

version0.2
Ryan 16 years ago
parent 9906cb69e6
commit 5214fb2524

@ -62,10 +62,12 @@ typedef int (*http_cb) (http_parser*);
#define HTTP_IDENTITY 0x01 #define HTTP_IDENTITY 0x01
#define HTTP_CHUNKED 0x02 #define HTTP_CHUNKED 0x02
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE };
struct http_parser { struct http_parser {
/** PRIVATE **/ /** PRIVATE **/
int cs; int cs;
int is_request_stream; enum http_parser_type type;
size_t chunk_size; size_t chunk_size;
unsigned eating:1; unsigned eating:1;
@ -112,7 +114,7 @@ struct http_parser {
* it will be parsing requests or responses. Set the second argument to 1 * it will be parsing requests or responses. Set the second argument to 1
* for requests; 0 for responses. * 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); size_t http_parser_execute (http_parser *parser, const char *data, size_t len);

@ -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 { action end_chunked_body {
END_REQUEST END_REQUEST
//fnext main; //fnext main;
if(parser->is_request_stream) { if(parser->type == HTTP_REQUEST) {
fnext Requests; fnext Requests;
} else { } else {
fnext Responses; 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*; Requests := Request*;
Responses := Response*; Responses := Response*;
#main := (Requests when { parser->is_request_stream })?
# | (Responses when { !parser->is_request_stream })?
# ;
main := any >{ main := any >{
fhold; fhold;
if(parser->is_request_stream) { if(parser->type == HTTP_REQUEST) {
fgoto Requests; fgoto Requests;
} else { } else {
fgoto Responses; 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)); memset(parser, 0, sizeof(struct http_parser));
int cs = 0; int cs = 0;
%% write init; %% write init;
parser->cs = cs; parser->cs = cs;
parser->is_request_stream = is_request_stream; parser->type = type;
RESET_PARSER(parser); RESET_PARSER(parser);
} }
/** exec **/ /** 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; const char *p, *pe;
int cs = parser->cs; int cs = parser->cs;

@ -399,11 +399,11 @@ int begin_message (http_parser *_)
} }
void void
parser_init (void) parser_init (enum http_parser_type type)
{ {
num_messages = 0; num_messages = 0;
http_parser_init(&parser, TRUE); http_parser_init(&parser, type);
memset(&messages, 0, sizeof messages); memset(&messages, 0, sizeof messages);
@ -458,7 +458,7 @@ parse_messages (int message_count, const struct message *input_messages[])
// Parse the stream // Parse the stream
size_t traversed = 0; size_t traversed = 0;
parser_init(); parser_init(HTTP_REQUEST);
traversed = http_parser_execute(&parser, total, length); traversed = http_parser_execute(&parser, total, length);
@ -475,7 +475,7 @@ void
test_request (const struct message *message) test_request (const struct message *message)
{ {
size_t traversed = 0; size_t traversed = 0;
parser_init(); parser_init(HTTP_REQUEST);
traversed = http_parser_execute(&parser, message->raw, strlen(message->raw)); traversed = http_parser_execute(&parser, message->raw, strlen(message->raw));
assert(!http_parser_has_error(&parser)); assert(!http_parser_has_error(&parser));
@ -488,7 +488,7 @@ void
test_error (const char *buf) test_error (const char *buf)
{ {
size_t traversed = 0; size_t traversed = 0;
parser_init(); parser_init(HTTP_REQUEST);
traversed = http_parser_execute(&parser, buf, strlen(buf)); 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); strcat(total, r3->raw);
size_t traversed = 0; size_t traversed = 0;
parser_init(); parser_init(HTTP_REQUEST);
traversed = http_parser_execute(&parser, total, strlen(total)); 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; ops += 1;
parser_init(); parser_init(HTTP_REQUEST);
int buf1_len = i; int buf1_len = i;
strncpy(buf1, total, buf1_len); strncpy(buf1, total, buf1_len);

Loading…
Cancel
Save