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
|
||||
#endif
|
||||
|
||||
#ifndef CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH
|
||||
#define CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH 32768
|
||||
#endif
|
||||
|
||||
#ifndef CPPHTTPLIB_HEADER_MAX_COUNT
|
||||
#define CPPHTTPLIB_HEADER_MAX_COUNT 100
|
||||
#endif
|
||||
@@ -243,6 +247,8 @@ static_assert(CPPHTTPLIB_REQUEST_URI_MAX_LENGTH > 0,
|
||||
"CPPHTTPLIB_REQUEST_URI_MAX_LENGTH must be positive");
|
||||
static_assert(CPPHTTPLIB_HEADER_MAX_LENGTH > 0,
|
||||
"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,
|
||||
"CPPHTTPLIB_HEADER_MAX_COUNT must be positive");
|
||||
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;
|
||||
char buf[bufsiz];
|
||||
stream_line_reader line_reader(strm, buf, bufsiz);
|
||||
@@ -9249,7 +9256,7 @@ inline bool read_headers(Stream &strm, Headers &headers) {
|
||||
#endif
|
||||
}
|
||||
|
||||
if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; }
|
||||
if (line_reader.size() > max_field_length) { return false; }
|
||||
|
||||
// Check header count limit
|
||||
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)) {
|
||||
return true;
|
||||
}
|
||||
return read_headers(strm, headers);
|
||||
return read_headers(strm, headers, CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH);
|
||||
}
|
||||
|
||||
inline bool
|
||||
@@ -16221,7 +16228,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
}
|
||||
|
||||
// Request headers
|
||||
if (!detail::read_headers(strm, req.headers)) {
|
||||
if (!detail::read_headers(strm, req.headers, CPPHTTPLIB_HEADER_MAX_LENGTH)) {
|
||||
connection_closed = true;
|
||||
res.status = StatusCode::BadRequest_400;
|
||||
output_error_log(Error::InvalidHeaders, &req);
|
||||
@@ -17025,7 +17032,10 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
|
||||
return false;
|
||||
}
|
||||
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 (!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) ||
|
||||
!detail::read_headers(strm, handle.response->headers)) {
|
||||
!detail::read_headers(strm, handle.response->headers,
|
||||
CPPHTTPLIB_RESPONSE_HEADER_MAX_LENGTH)) {
|
||||
handle.error = Error::Read;
|
||||
handle.response.reset();
|
||||
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
|
||||
if ((!status_line_read &&
|
||||
!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; }
|
||||
return false;
|
||||
}
|
||||
@@ -18366,7 +18378,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
|
||||
res.headers.clear();
|
||||
res.body.clear();
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user