mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-18 11:09:39 +00:00
Fix problem with PayloadMaxLengthTest.NoContentLengthPayloadLimit
This commit is contained in:
1 parent
a188913b02
commit
f69737a838
1 file changed
+27
-28
@@ -9855,41 +9855,40 @@ inline bool Server::read_content_core(
|
|||||||
// are true (no Transfer-Encoding and no Content-Length), then the message
|
// are true (no Transfer-Encoding and no Content-Length), then the message
|
||||||
// body length is zero (no message body is present).
|
// body length is zero (no message body is present).
|
||||||
//
|
//
|
||||||
// For non-SSL builds, peek into the socket to detect clients that send a
|
// For non-SSL builds, detect clients that send a body without a
|
||||||
// body without a Content-Length header (raw HTTP over TCP). If there is
|
// Content-Length header (raw HTTP over TCP). Check both the stream's
|
||||||
// pending data that exceeds the configured payload limit, treat this as an
|
// internal read buffer (data already read from the socket during header
|
||||||
// oversized request and fail early (causing connection close). For SSL
|
// parsing) and the socket itself for pending data. If data is found and
|
||||||
// builds we cannot reliably peek the decrypted application bytes, so keep
|
// exceeds the configured payload limit, reject with 413.
|
||||||
// the original behaviour.
|
// For SSL builds we cannot reliably peek the decrypted application bytes,
|
||||||
|
// so keep the original behaviour.
|
||||||
#if !defined(CPPHTTPLIB_SSL_ENABLED)
|
#if !defined(CPPHTTPLIB_SSL_ENABLED)
|
||||||
if (!req.has_header("Content-Length") &&
|
if (!req.has_header("Content-Length") &&
|
||||||
!detail::is_chunked_transfer_encoding(req.headers)) {
|
!detail::is_chunked_transfer_encoding(req.headers)) {
|
||||||
// Only peek if payload_max_length is set to a finite value
|
// Only check if payload_max_length is set to a finite value
|
||||||
if (payload_max_length_ > 0 &&
|
if (payload_max_length_ > 0 &&
|
||||||
payload_max_length_ < (std::numeric_limits<size_t>::max)()) {
|
payload_max_length_ < (std::numeric_limits<size_t>::max)()) {
|
||||||
socket_t s = strm.socket();
|
// Check if there is data already buffered in the stream (read during
|
||||||
if (s != INVALID_SOCKET) {
|
// header parsing) or pending on the socket. Use a non-blocking socket
|
||||||
// Use a non-blocking check to see if there is any pending data.
|
// check to avoid deadlock when the client sends no body.
|
||||||
// A blocking recv(MSG_PEEK) would deadlock when the client is
|
bool has_data = strm.is_readable();
|
||||||
// waiting for the response (e.g. POST with Connection: close and
|
if (!has_data) {
|
||||||
// no body).
|
socket_t s = strm.socket();
|
||||||
if (detail::select_read(s, 0, 0) > 0) {
|
if (s != INVALID_SOCKET) {
|
||||||
char peekbuf[1];
|
has_data = detail::select_read(s, 0, 0) > 0;
|
||||||
ssize_t n = ::recv(s, peekbuf, 1, MSG_PEEK);
|
|
||||||
if (n > 0) {
|
|
||||||
// There is data, so read it with payload limit enforcement
|
|
||||||
auto result = detail::read_content_without_length(
|
|
||||||
strm, payload_max_length_, out);
|
|
||||||
if (result == detail::ReadContentResult::PayloadTooLarge) {
|
|
||||||
res.status = StatusCode::PayloadTooLarge_413;
|
|
||||||
return false;
|
|
||||||
} else if (result != detail::ReadContentResult::Success) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (has_data) {
|
||||||
|
auto result =
|
||||||
|
detail::read_content_without_length(strm, payload_max_length_, out);
|
||||||
|
if (result == detail::ReadContentResult::PayloadTooLarge) {
|
||||||
|
res.status = StatusCode::PayloadTooLarge_413;
|
||||||
|
return false;
|
||||||
|
} else if (result != detail::ReadContentResult::Success) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user