Skip to content

Commit a268d65

Browse files
authored
Fix check for URI length to prevent incorrect HTTP 414 errors (#2046)
1 parent b397c76 commit a268d65

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

httplib.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7234,21 +7234,21 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
72347234
#endif
72357235
#endif
72367236

7237-
// Check if the request URI doesn't exceed the limit
7238-
if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) {
7239-
Headers dummy;
7240-
detail::read_headers(strm, dummy);
7241-
res.status = StatusCode::UriTooLong_414;
7242-
return write_response(strm, close_connection, req, res);
7243-
}
7244-
72457237
// Request line and headers
72467238
if (!parse_request_line(line_reader.ptr(), req) ||
72477239
!detail::read_headers(strm, req.headers)) {
72487240
res.status = StatusCode::BadRequest_400;
72497241
return write_response(strm, close_connection, req, res);
72507242
}
72517243

7244+
// Check if the request URI doesn't exceed the limit
7245+
if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) {
7246+
Headers dummy;
7247+
detail::read_headers(strm, dummy);
7248+
res.status = StatusCode::UriTooLong_414;
7249+
return write_response(strm, close_connection, req, res);
7250+
}
7251+
72527252
if (req.get_header_value("Connection") == "close") {
72537253
connection_closed = true;
72547254
}

test/test.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3541,7 +3541,7 @@ TEST_F(ServerTest, LongRequest) {
35413541

35423542
TEST_F(ServerTest, TooLongRequest) {
35433543
std::string request;
3544-
for (size_t i = 0; i < 545; i++) {
3544+
for (size_t i = 0; i < 546; i++) {
35453545
request += "/TooLongRequest";
35463546
}
35473547
request += "_NG";
@@ -3552,6 +3552,17 @@ TEST_F(ServerTest, TooLongRequest) {
35523552
EXPECT_EQ(StatusCode::UriTooLong_414, res->status);
35533553
}
35543554

3555+
TEST_F(ServerTest, AlmostTooLongRequest) {
3556+
// test for #2046 - URI length check shouldn't include other content on req line
3557+
// URI is max URI length, minus 14 other chars in req line (GET, space, leading /, space, HTTP/1.1)
3558+
std::string request = "/" + string(CPPHTTPLIB_REQUEST_URI_MAX_LENGTH - 14, 'A');
3559+
3560+
auto res = cli_.Get(request.c_str());
3561+
3562+
ASSERT_TRUE(res);
3563+
EXPECT_EQ(StatusCode::NotFound_404, res->status);
3564+
}
3565+
35553566
TEST_F(ServerTest, LongHeader) {
35563567
Request req;
35573568
req.method = "GET";

0 commit comments

Comments
 (0)