joyent/http-parser — Issue #75: http_parser_init does not clear status_code
Author: ivosh
Created: 2012-01-13 14:37:39+00:00
State: open

We use http_parser with HTTP_BOTH flag and reuse the same parser instance for many incoming messages.
Note that messages are carried over UDP so it cannot be determined if the to-be-parsed message is a request or a response.
Also note that complete message is carried in one UDP datagram.

Apparently http parser does not clear its states properly after finished messages (our messages have no Content-Length and no body):

http_parser_init(&parser, HTTP_BOTH);

loop over messages:
size_t parsed = http_parser_execute(&parser, &settings, data, data_length);
http_parser_execute(&parser, &settings, NULL, 0); // to indicate end-of-stream

Therefore we resolved to initialise the parser every time before parsing a message:

loop over messages:
http_parser_init(&parser, HTTP_BOTH);
size_t parsed = http_parser_execute(&parser, &settings, data, data_length);

But http_parser_init does not clear all state variables of the http parser.
For example status_code remains set from the last message.

If we are using http_parser in an unsupported way, please adjust the documentation.
If this is a bug, then please add the following line at http_parser.c:1859

parser->status_code = 0;

or maybe better, bzero the whole parser structure at the beginning of http_parser_init (perhaps excluding data attribute).