mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-12-04 22:08:23 +00:00
Allow longer response header fields than request header fields
The client read response header fields under CPPHTTPLIB_HEADER_MAX_LENGTH (8192), the limit meant for requests a server accepts. Real servers now send longer fields: www.youtube.com returns a 13KB connection-allowlist-report-only field, so every request to it failed with Error::Read. Read response headers under a separate CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH, 32768 by default, and keep the request limit as it is.
This commit is contained in:
1 parent
a95f17cffa
commit
012a9871b1
2 files changed
+51
-8
No files matched your search
@@ -127,6 +127,10 @@
|
|||||||
#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192
|
#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH
|
||||||
|
#define CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH 32768
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef CPPHTTPLIB_HEADER_MAX_COUNT
|
#ifndef CPPHTTPLIB_HEADER_MAX_COUNT
|
||||||
#define CPPHTTPLIB_HEADER_MAX_COUNT 100
|
#define CPPHTTPLIB_HEADER_MAX_COUNT 100
|
||||||
#endif
|
#endif
|
||||||
@@ -243,6 +247,8 @@ static_assert(CPPHTTPLIB_REQUEST_URI_MAX_LENGTH > 0,
|
|||||||
"CPPHTTPLIB_REQUEST_URI_MAX_LENGTH must be positive");
|
"CPPHTTPLIB_REQUEST_URI_MAX_LENGTH must be positive");
|
||||||
static_assert(CPPHTTPLIB_HEADER_MAX_LENGTH > 0,
|
static_assert(CPPHTTPLIB_HEADER_MAX_LENGTH > 0,
|
||||||
"CPPHTTPLIB_HEADER_MAX_LENGTH must be positive");
|
"CPPHTTPLIB_HEADER_MAX_LENGTH must be positive");
|
||||||
|
static_assert(CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH > 0,
|
||||||
|
"CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH must be positive");
|
||||||
static_assert(CPPHTTPLIB_HEADER_MAX_COUNT > 0,
|
static_assert(CPPHTTPLIB_HEADER_MAX_COUNT > 0,
|
||||||
"CPPHTTPLIB_HEADER_MAX_COUNT must be positive");
|
"CPPHTTPLIB_HEADER_MAX_COUNT must be positive");
|
||||||
static_assert(CPPHTTPLIB_MAX_LINE_LENGTH > 0,
|
static_assert(CPPHTTPLIB_MAX_LINE_LENGTH > 0,
|
||||||
@@ -9224,7 +9230,8 @@ inline void erase_invalid_fields(Headers &headers) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool read_headers(Stream &strm, Headers &headers) {
|
inline bool read_headers(Stream &strm, Headers &headers,
|
||||||
|
size_t max_field_length) {
|
||||||
const auto bufsiz = 2048;
|
const auto bufsiz = 2048;
|
||||||
char buf[bufsiz];
|
char buf[bufsiz];
|
||||||
stream_line_reader line_reader(strm, buf, bufsiz);
|
stream_line_reader line_reader(strm, buf, bufsiz);
|
||||||
@@ -9249,7 +9256,7 @@ inline bool read_headers(Stream &strm, Headers &headers) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; }
|
if (line_reader.size() > max_field_length) { return false; }
|
||||||
|
|
||||||
// Check header count limit
|
// Check header count limit
|
||||||
if (header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; }
|
if (header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; }
|
||||||
@@ -9334,7 +9341,7 @@ inline bool read_response_head(Stream &strm, int &status, Headers &headers) {
|
|||||||
if (!parse_status_line(line_reader.ptr(), version, status, reason)) {
|
if (!parse_status_line(line_reader.ptr(), version, status, reason)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return read_headers(strm, headers);
|
return read_headers(strm, headers, CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool
|
inline bool
|
||||||
@@ -16221,7 +16228,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Request headers
|
// Request headers
|
||||||
if (!detail::read_headers(strm, req.headers)) {
|
if (!detail::read_headers(strm, req.headers, CPPHTTPLIB_HEADER_MAX_LENGTH)) {
|
||||||
connection_closed = true;
|
connection_closed = true;
|
||||||
res.status = StatusCode::BadRequest_400;
|
res.status = StatusCode::BadRequest_400;
|
||||||
output_error_log(Error::InvalidHeaders, &req);
|
output_error_log(Error::InvalidHeaders, &req);
|
||||||
@@ -17025,7 +17032,10 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Headers interim_headers;
|
Headers interim_headers;
|
||||||
if (!detail::read_headers(strm, interim_headers)) { return false; }
|
if (!detail::read_headers(strm, interim_headers,
|
||||||
|
CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!line_reader.getline()) { return false; } // next response line
|
if (!line_reader.getline()) { return false; } // next response line
|
||||||
|
|
||||||
if (!detail::parse_status_line(line_reader.ptr(), res.version, res.status,
|
if (!detail::parse_status_line(line_reader.ptr(), res.version, res.status,
|
||||||
@@ -17315,7 +17325,8 @@ inline ClientImpl::StreamHandle ClientImpl::open_stream_(Request &req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!read_response_line(strm, req, *handle.response) ||
|
if (!read_response_line(strm, req, *handle.response) ||
|
||||||
!detail::read_headers(strm, handle.response->headers)) {
|
!detail::read_headers(strm, handle.response->headers,
|
||||||
|
CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH)) {
|
||||||
handle.error = Error::Read;
|
handle.error = Error::Read;
|
||||||
handle.response.reset();
|
handle.response.reset();
|
||||||
return handle;
|
return handle;
|
||||||
@@ -18349,7 +18360,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
|
|||||||
// When using Expect: 100-continue, don't auto-skip `100 Continue` response
|
// When using Expect: 100-continue, don't auto-skip `100 Continue` response
|
||||||
if ((!status_line_read &&
|
if ((!status_line_read &&
|
||||||
!read_response_line(strm, req, res, !expect_100_continue)) ||
|
!read_response_line(strm, req, res, !expect_100_continue)) ||
|
||||||
!detail::read_headers(strm, res.headers)) {
|
!detail::read_headers(strm, res.headers,
|
||||||
|
CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH)) {
|
||||||
if (write_request_success) { error = Error::Read; }
|
if (write_request_success) { error = Error::Read; }
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -18366,7 +18378,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
|
|||||||
res.headers.clear();
|
res.headers.clear();
|
||||||
res.body.clear();
|
res.body.clear();
|
||||||
if (!read_response_line(strm, req, res) ||
|
if (!read_response_line(strm, req, res) ||
|
||||||
!detail::read_headers(strm, res.headers)) {
|
!detail::read_headers(strm, res.headers,
|
||||||
|
CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH)) {
|
||||||
error = Error::Read;
|
error = Error::Read;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25415,6 +25415,36 @@ TEST(SSLClientTest, UnframedBodyEndsOnlyAtCloseNotify) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
TEST(ResponseHeaderLengthTest, ClientReadsLongerFieldsThanServer) {
|
||||||
|
const auto value = std::string(CPPHTTPLIB_HEADER_MAX_LENGTH * 2, 'a');
|
||||||
|
const auto response = "HTTP/1.1 200 OK\r\n"
|
||||||
|
"X-Long: " +
|
||||||
|
value +
|
||||||
|
"\r\n"
|
||||||
|
"Content-Length: 2\r\n"
|
||||||
|
"Connection: close\r\n\r\nok";
|
||||||
|
|
||||||
|
with_single_response(response, [&](Client &cli) {
|
||||||
|
auto res = cli.Get("/");
|
||||||
|
ASSERT_TRUE(res) << to_string(res.error());
|
||||||
|
EXPECT_EQ(value, res->get_header_value("X-Long"));
|
||||||
|
});
|
||||||
|
|
||||||
|
with_single_response(response, [&](Client &cli) {
|
||||||
|
auto handle = cli.open_stream("GET", "/");
|
||||||
|
ASSERT_TRUE(handle.is_valid()) << to_string(handle.error);
|
||||||
|
EXPECT_EQ(value, handle.response->get_header_value("X-Long"));
|
||||||
|
});
|
||||||
|
|
||||||
|
std::string out;
|
||||||
|
test_raw_request("GET /hi HTTP/1.1\r\n"
|
||||||
|
"Host: localhost\r\n"
|
||||||
|
"X-Long: " +
|
||||||
|
value + "\r\n\r\n",
|
||||||
|
&out);
|
||||||
|
EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(InterimResponseTest, SkippedWithTheirHeaderFields) {
|
TEST(InterimResponseTest, SkippedWithTheirHeaderFields) {
|
||||||
const char *responses[] = {
|
const char *responses[] = {
|
||||||
"HTTP/1.1 103 Early Hints\r\n"
|
"HTTP/1.1 103 Early Hints\r\n"
|
||||||
|
|||||||
Reference in new issue
Block a user