Commit 4bfc4f81 authored by octal's avatar octal

Request parser now correctly ignores the crlf separating the headers from the...

Request parser now correctly ignores the crlf separating the headers from the body, which fixes #160
parent 5078760d
...@@ -315,17 +315,14 @@ namespace Private { ...@@ -315,17 +315,14 @@ namespace Private {
headerRevert.ignore(); headerRevert.ignore();
} }
if (!cursor.advance(2)) return State::Again;
revert.ignore(); revert.ignore();
return State::Next; return State::Next;
} }
State State
BodyStep::apply(StreamCursor& cursor) { BodyStep::apply(StreamCursor& cursor) {
if (message->body_.empty()) {
/* If this is the first time we are reading the body, skip the CRLF */
if (!cursor.advance(2)) return State::Again;
}
auto cl = message->headers_.tryGet<Header::ContentLength>(); auto cl = message->headers_.tryGet<Header::ContentLength>();
auto te = message->headers_.tryGet<Header::TransferEncoding>(); auto te = message->headers_.tryGet<Header::TransferEncoding>();
......
...@@ -15,3 +15,4 @@ pistache_test(typeid_test) ...@@ -15,3 +15,4 @@ pistache_test(typeid_test)
pistache_test(router_test) pistache_test(router_test)
pistache_test(cookie_test) pistache_test(cookie_test)
pistache_test(view_test) pistache_test(view_test)
pistache_test(http_parsing_test)
#include "gtest/gtest.h"
#include <pistache/http.h>
using namespace Pistache;
// @Todo: Add an easy to use fixture to inject data for parsing tests.
TEST(http_parsing_test, should_parse_http_request_in_two_packets_issue_160)
{
Http::Private::Parser<Http::Request> parser;
auto feed = [&](const char* data)
{
parser.feed(data, std::strlen(data));
};
// First, we feed the parser with a Request-Line
feed("GET /hello HTTP/1.1\r\n");
ASSERT_EQ(parser.parse(), Http::Private::State::Again);
// @Todo @Completeness We should also assert that we are in the correct step. However, the step is currently not
// exposed by the parser. Since the parser is supposed to stay "private", we could either directly expose the step
// or return it from the parse() method.
// Let's now put some headers
feed("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36\r\n");
feed("Host: localhost\r\n");
feed("Content-Length: 5\r\n");
feed("\r\n");
ASSERT_EQ(parser.parse(), Http::Private::State::Again);
// Finally, we finish the body
feed("HELLO");
ASSERT_EQ(parser.parse(), Http::Private::State::Done);
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment