mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-01-19 04:52:08 +00:00
Problem with CI test on Windows without OpenSSL (#2323)
* Fix problem with 'windows without SSL` * Fix payload limit enforcement for requests without Content-Length on Windows - Enable MSG_PEEK on Windows (non-SSL builds) to detect payloads without Content-Length - Only use MSG_PEEK when payload_max_length is set to a finite value to avoid blocking - Use read_content_without_length for actual size checking to support any payload limit - Set 413 Payload Too Large status before rejecting oversized requests This fixes three test cases on Windows: - RequestWithoutContentLengthOrTransferEncoding (no payload limit) - NoContentLengthPayloadLimit (8-byte limit) - NoContentLengthExceeds10MB (10MB limit) * clang-format
This commit is contained in:
1
.github/workflows/test.yaml
vendored
1
.github/workflows/test.yaml
vendored
@@ -156,6 +156,7 @@ jobs:
|
|||||||
-DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake
|
-DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake
|
||||||
-DHTTPLIB_TEST=ON
|
-DHTTPLIB_TEST=ON
|
||||||
-DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }}
|
-DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }}
|
||||||
|
-DHTTPLIB_USE_OPENSSL_IF_AVAILABLE=${{ matrix.config.with_ssl && 'ON' || 'OFF' }}
|
||||||
-DHTTPLIB_REQUIRE_ZLIB=ON
|
-DHTTPLIB_REQUIRE_ZLIB=ON
|
||||||
-DHTTPLIB_REQUIRE_BROTLI=ON
|
-DHTTPLIB_REQUIRE_BROTLI=ON
|
||||||
-DHTTPLIB_REQUIRE_ZSTD=ON
|
-DHTTPLIB_REQUIRE_ZSTD=ON
|
||||||
|
|||||||
35
httplib.h
35
httplib.h
@@ -9078,22 +9078,29 @@ inline bool Server::read_content_core(
|
|||||||
// oversized request and fail early (causing connection close). For SSL
|
// oversized request and fail early (causing connection close). For SSL
|
||||||
// builds we cannot reliably peek the decrypted application bytes, so keep
|
// builds we cannot reliably peek the decrypted application bytes, so keep
|
||||||
// the original behaviour.
|
// the original behaviour.
|
||||||
#if !defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(_WIN32)
|
#if !defined(CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||||
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)) {
|
||||||
socket_t s = strm.socket();
|
// Only peek if payload_max_length is set to a finite value
|
||||||
if (s != INVALID_SOCKET) {
|
if (payload_max_length_ > 0 &&
|
||||||
// Peek up to payload_max_length_ + 1 bytes. If more than
|
payload_max_length_ < (std::numeric_limits<size_t>::max)()) {
|
||||||
// payload_max_length_ bytes are pending, reject the request.
|
socket_t s = strm.socket();
|
||||||
size_t to_peek =
|
if (s != INVALID_SOCKET) {
|
||||||
(payload_max_length_ > 0)
|
// Peek to check if there is any pending data
|
||||||
? (std::min)(payload_max_length_ + 1, static_cast<size_t>(4096))
|
char peekbuf[1];
|
||||||
: 1;
|
ssize_t n = ::recv(s, peekbuf, 1, MSG_PEEK);
|
||||||
std::vector<char> peekbuf(to_peek);
|
if (n > 0) {
|
||||||
ssize_t n = ::recv(s, peekbuf.data(), to_peek, MSG_PEEK);
|
// There is data, so read it with payload limit enforcement
|
||||||
if (n > 0 && static_cast<size_t>(n) > payload_max_length_) {
|
auto result = detail::read_content_without_length(
|
||||||
// Indicate failure so connection will be closed.
|
strm, payload_max_length_, out);
|
||||||
return false;
|
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