Skip to content

Commit

Permalink
Merge pull request #262 from CrowCpp/http09_get_only
Browse files Browse the repository at this point in the history
Use only GET on HTTP/0.9
  • Loading branch information
The-EDev authored Nov 2, 2021
2 parents c45514c + 2e7d5f3 commit e48e1fa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
10 changes: 7 additions & 3 deletions include/crow/http_parser_merged.h
Original file line number Diff line number Diff line change
Expand Up @@ -1245,9 +1245,7 @@ static const int8_t unhex[256] =
case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
case 'N': parser->method = HTTP_NOTIFY; break;
case 'O': parser->method = HTTP_OPTIONS; break;
case 'P': parser->method = HTTP_POST;
/* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
break;
case 'P': parser->method = HTTP_POST; /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */ break;
case 'R': parser->method = HTTP_REPORT; break;
case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
case 'T': parser->method = HTTP_TRACE; break;
Expand Down Expand Up @@ -1404,6 +1402,12 @@ static const int8_t unhex[256] =
break;
case CROW_CR:
case CROW_LF:
if (parser->method != HTTP_GET)
{
parser->state = s_dead;
CROW_SET_ERRNO(HPE_INVALID_VERSION);
goto error;
}
parser->http_major = 0;
parser->http_minor = 9;
parser->state = (ch == CROW_CR) ?
Expand Down
4 changes: 4 additions & 0 deletions include/crow/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ namespace crow
};

int nparsed = http_parser_execute(this, &settings_, buffer, length);
if (http_errno != HPE_OK)
{
return false;
}
return nparsed == length;
}

Expand Down
34 changes: 31 additions & 3 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,34 @@ TEST_CASE("server_handling_error_request")
c.receive(asio::buffer(buf, 2048));
FAIL_CHECK();
} catch (std::exception& e) {
// std::cerr << e.what() << std::endl;
CROW_LOG_DEBUG << e.what();
}
}
app.stop();
}

TEST_CASE("server_handling_error_request_http_version")
{
static char buf[2048];
SimpleApp app;
CROW_ROUTE(app, "/")([] { return "A"; });
auto _ = async(launch::async,
[&] { app.bindaddr(LOCALHOST_ADDRESS).port(45451).run(); });
app.wait_for_server_start();
std::string sendmsg = "POST /\r\nContent-Length:3\r\nX-HeaderTest: 123\r\n\r\nA=B\r\n";
asio::io_service is;
{
asio::ip::tcp::socket c(is);
c.connect(asio::ip::tcp::endpoint(
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451));

c.send(asio::buffer(sendmsg));

try {
c.receive(asio::buffer(buf, 2048));
FAIL_CHECK();
} catch (std::exception& e) {
CROW_LOG_DEBUG << e.what();
}
}
app.stop();
Expand All @@ -483,9 +510,9 @@ TEST_CASE("multi_server")
app2.wait_for_server_start();

std::string sendmsg =
"POST /\r\nContent-Length:3\r\nX-HeaderTest: 123\r\n\r\nA=B\r\n";
asio::io_service is;
"POST / HTTP/1.0\r\nContent-Length:3\r\nX-HeaderTest: 123\r\n\r\nA=B\r\n";
{
asio::io_service is;
asio::ip::tcp::socket c(is);
c.connect(asio::ip::tcp::endpoint(
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451));
Expand All @@ -497,6 +524,7 @@ TEST_CASE("multi_server")
}

{
asio::io_service is;
asio::ip::tcp::socket c(is);
c.connect(asio::ip::tcp::endpoint(
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45452));
Expand Down

0 comments on commit e48e1fa

Please sign in to comment.