AndreLouisCaron/httpxx — Issue #1: Impossible to parse pipelined HTTP 1.1 headers
Author: pezcode
Created: 2012-02-21 23:11:42+00:00
State: open

When parsing multiple HTTP 1.0 headers in one buffer, feed() returns as soon as the first message was parsed (which is the behaviour I actually need).

However, if I just replace every "HTTP/1.0" with "HTTP/1.1" in the headers, http-parser doesn't return after the first message. It calls on_message_complete but doesn't return like it does for HTTP 1.0 headers.

That way, when using httpx, it's impossible to parse all 3 headers, without having to resort to feeding it single bytes in a loop and checking if the message is complete everytime.

Maybe the simplest solution would be to return non-0 in Message::on_message_complete to abort parsing, copy over needed structures and reset the http-parser struct.

See the sample code below. It outputs all 3 headers no problem. Replace every 1.0 with 1.1 and it outputs one header with body "HELLOWorld".

const char HEADER_TEST[] =

"GET /get_funky_content_length_body_hello HTTP/1.0\r\n"
"conTENT-Length: 5\r\n"
"\r\n"
"HELLO"

"GET /get_one_header_no_body HTTP/1.0\r\n"
"\r\n"

"POST /post_identity_body_world?q=search#hey HTTP/1.0\r\n"
"Accept: */*\r\n"
"Transfer-Encoding: identity\r\n"
"Content-Length: 5\r\n"
"\r\n"
"World"
;

http::Request request;

const char* buf = HEADER_TEST;
size_t size = sizeof(HEADER_TEST) - 1;

int header = 1;

while(size > 0)
{

size_t read = request.feed(buf, size);
if(read == 0)
{
std::cout << "ERROR" << std::endl;
break;
}

size -= read;
buf += read;

std::cout << "Header " << header << '\n';
std::cout << " Read: " << read << " bytes" << '\n';
std::cout << " Body: " << request.body().length() << " bytes" << '\n';
std::cout << " Body: " << request.body() << '\n';

request.clear();
header++;
}