mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-12-05 22:18:27 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f59c57662 | ||
|
|
d81a932f2a | ||
|
|
5fd66e46df | ||
|
|
c438858d92 | ||
|
|
6c2324ee59 | ||
|
|
da4a0a6f33 | ||
|
|
4e6226d7af | ||
|
|
3091751b5d | ||
|
|
a4697bd29d | ||
|
|
852a66bcec | ||
|
|
012a9871b1 | ||
|
|
a95f17cffa |
No files matched your search
@@ -4,7 +4,7 @@ langs = ["en", "ja"]
|
|||||||
|
|
||||||
[site]
|
[site]
|
||||||
title = "cpp-httplib"
|
title = "cpp-httplib"
|
||||||
version = "0.69.0"
|
version = "0.70.0"
|
||||||
hostname = "https://yhirose.github.io"
|
hostname = "https://yhirose.github.io"
|
||||||
base_path = "/cpp-httplib"
|
base_path = "/cpp-httplib"
|
||||||
footer_message = "© 2026 Yuji Hirose. All rights reserved."
|
footer_message = "© 2026 Yuji Hirose. All rights reserved."
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
#ifndef CPPHTTPLIB_HTTPLIB_H
|
#ifndef CPPHTTPLIB_HTTPLIB_H
|
||||||
#define CPPHTTPLIB_HTTPLIB_H
|
#define CPPHTTPLIB_HTTPLIB_H
|
||||||
|
|
||||||
#define CPPHTTPLIB_VERSION "0.69.0"
|
#define CPPHTTPLIB_VERSION "0.70.0"
|
||||||
#define CPPHTTPLIB_VERSION_NUM "0x004500"
|
#define CPPHTTPLIB_VERSION_NUM "0x004600"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
|
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
|
||||||
@@ -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,
|
||||||
@@ -3892,7 +3898,7 @@ struct FileStat {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
struct _stat st_;
|
struct _stat64 st_;
|
||||||
#else
|
#else
|
||||||
struct stat st_;
|
struct stat st_;
|
||||||
#endif
|
#endif
|
||||||
@@ -4181,6 +4187,10 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool is_valid_ = false;
|
bool is_valid_ = false;
|
||||||
|
// RFC 1952 2.2: a gzip body is a series of members. After one ends, the next
|
||||||
|
// starts with ID1 ID2, and anything else is ignored.
|
||||||
|
enum class State { Inflating, MemberEnded, SawId1, Ignoring };
|
||||||
|
State state_ = State::Inflating;
|
||||||
z_stream strm_;
|
z_stream strm_;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
@@ -6458,7 +6468,7 @@ inline bool is_path_within_base(const std::string &resolved_path,
|
|||||||
inline FileStat::FileStat(const std::string &path) {
|
inline FileStat::FileStat(const std::string &path) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
auto wpath = u8string_to_wstring(path.c_str());
|
auto wpath = u8string_to_wstring(path.c_str());
|
||||||
ret_ = _wstat(wpath.c_str(), &st_);
|
ret_ = _wstat64(wpath.c_str(), &st_);
|
||||||
#else
|
#else
|
||||||
ret_ = stat(path.c_str(), &st_);
|
ret_ = stat(path.c_str(), &st_);
|
||||||
#endif
|
#endif
|
||||||
@@ -8883,6 +8893,8 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
|||||||
Callback callback) {
|
Callback callback) {
|
||||||
assert(is_valid_);
|
assert(is_valid_);
|
||||||
|
|
||||||
|
if (state_ == State::Ignoring) { return true; }
|
||||||
|
|
||||||
auto ret = Z_OK;
|
auto ret = Z_OK;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -8901,6 +8913,37 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
|||||||
strm_.avail_out = static_cast<uInt>(buff.size());
|
strm_.avail_out = static_cast<uInt>(buff.size());
|
||||||
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
|
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
|
||||||
|
|
||||||
|
if (state_ != State::Inflating) {
|
||||||
|
static const Bytef id[] = {0x1f, 0x8b};
|
||||||
|
auto i = state_ == State::MemberEnded ? 0 : 1;
|
||||||
|
if (*strm_.next_in != id[i]) {
|
||||||
|
state_ = State::Ignoring;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
strm_.next_in++;
|
||||||
|
strm_.avail_in--;
|
||||||
|
if (i == 0) {
|
||||||
|
state_ = State::SawId1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The ID bytes may have come split across calls, so feed them from id
|
||||||
|
inflateReset(&strm_);
|
||||||
|
auto next_in = strm_.next_in;
|
||||||
|
auto avail_in = strm_.avail_in;
|
||||||
|
strm_.next_in = const_cast<Bytef *>(id);
|
||||||
|
strm_.avail_in = sizeof(id);
|
||||||
|
ret = inflate(&strm_, Z_NO_FLUSH);
|
||||||
|
strm_.next_in = next_in;
|
||||||
|
strm_.avail_in = avail_in;
|
||||||
|
state_ = State::Inflating;
|
||||||
|
if (ret != Z_OK) {
|
||||||
|
inflateEnd(&strm_);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
ret = inflate(&strm_, Z_NO_FLUSH);
|
ret = inflate(&strm_, Z_NO_FLUSH);
|
||||||
|
|
||||||
assert(ret != Z_STREAM_ERROR);
|
assert(ret != Z_STREAM_ERROR);
|
||||||
@@ -8913,6 +8956,11 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
|||||||
if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
|
if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ret == Z_STREAM_END) {
|
||||||
|
state_ = State::MemberEnded;
|
||||||
|
ret = Z_OK;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret != Z_OK && ret != Z_STREAM_END) { return false; }
|
if (ret != Z_OK && ret != Z_STREAM_END) { return false; }
|
||||||
@@ -9224,7 +9272,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 +9298,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 +9383,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
|
||||||
@@ -10509,14 +10558,17 @@ public:
|
|||||||
|
|
||||||
it = params.find("filename*");
|
it = params.find("filename*");
|
||||||
if (it != params.end()) {
|
if (it != params.end()) {
|
||||||
// RFC 5987: only UTF-8 encoding is allowed
|
// RFC 8187: UTF-8'[language]'value-chars
|
||||||
const auto &val = it->second;
|
const auto &val = it->second;
|
||||||
constexpr const char utf8_prefix[] = "UTF-8''";
|
constexpr const char utf8_prefix[] = "UTF-8'";
|
||||||
constexpr size_t prefix_len = str_len(utf8_prefix);
|
constexpr size_t prefix_len = str_len(utf8_prefix);
|
||||||
if (val.size() > prefix_len &&
|
const auto value_pos = start_with_case_ignore(val, utf8_prefix)
|
||||||
start_with_case_ignore(val, utf8_prefix)) {
|
? val.find('\'', prefix_len)
|
||||||
|
: std::string::npos;
|
||||||
|
if (value_pos != std::string::npos &&
|
||||||
|
value_pos + 1 < val.size()) {
|
||||||
file_.filename = decode_path_component(
|
file_.filename = decode_path_component(
|
||||||
val.substr(prefix_len)); // override...
|
val.substr(value_pos + 1)); // override...
|
||||||
} else {
|
} else {
|
||||||
is_valid_ = false;
|
is_valid_ = false;
|
||||||
return false;
|
return false;
|
||||||
@@ -10994,7 +11046,7 @@ inline bool range_error(Request &req, Response &res) {
|
|||||||
ssize_t content_len = static_cast<ssize_t>(
|
ssize_t content_len = static_cast<ssize_t>(
|
||||||
res.content_length_ ? res.content_length_ : res.body.size());
|
res.content_length_ ? res.content_length_ : res.body.size());
|
||||||
|
|
||||||
std::vector<std::pair<ssize_t, ssize_t>> processed_ranges;
|
Ranges satisfiable_ranges;
|
||||||
size_t overwrapping_count = 0;
|
size_t overwrapping_count = 0;
|
||||||
|
|
||||||
// NOTE: The following Range check is based on '14.2. Range' in RFC 9110
|
// NOTE: The following Range check is based on '14.2. Range' in RFC 9110
|
||||||
@@ -11007,7 +11059,9 @@ inline bool range_error(Request &req, Response &res) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &r : req.ranges) {
|
satisfiable_ranges.reserve(req.ranges.size());
|
||||||
|
|
||||||
|
for (auto r : req.ranges) {
|
||||||
auto &first_pos = r.first;
|
auto &first_pos = r.first;
|
||||||
auto &last_pos = r.second;
|
auto &last_pos = r.second;
|
||||||
|
|
||||||
@@ -11036,25 +11090,28 @@ inline bool range_error(Request &req, Response &res) {
|
|||||||
last_pos = content_len - 1;
|
last_pos = content_len - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Range must be within content length
|
// Skip an unsatisfiable range (RFC 9110 14.1.1)
|
||||||
if (!(0 <= first_pos && first_pos <= last_pos &&
|
if (!(0 <= first_pos && first_pos <= last_pos &&
|
||||||
last_pos <= content_len - 1)) {
|
last_pos <= content_len - 1)) {
|
||||||
return true;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request must not have more than two overlapping ranges
|
// Request must not have more than two overlapping ranges
|
||||||
for (const auto &processed_range : processed_ranges) {
|
for (const auto &prev : satisfiable_ranges) {
|
||||||
if (!(last_pos < processed_range.first ||
|
if (!(last_pos < prev.first || first_pos > prev.second)) {
|
||||||
first_pos > processed_range.second)) {
|
|
||||||
overwrapping_count++;
|
overwrapping_count++;
|
||||||
if (overwrapping_count > 2) { return true; }
|
if (overwrapping_count > 2) { return true; }
|
||||||
break; // Only count once per range
|
break; // Only count once per range
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
processed_ranges.emplace_back(first_pos, last_pos);
|
satisfiable_ranges.push_back(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 416 only when no range is satisfiable (RFC 9110 15.5.17)
|
||||||
|
if (satisfiable_ranges.empty()) { return true; }
|
||||||
|
req.ranges = std::move(satisfiable_ranges);
|
||||||
|
|
||||||
// After validation, coalesce overlapping ranges as per RFC 9110
|
// After validation, coalesce overlapping ranges as per RFC 9110
|
||||||
coalesce_ranges(req.ranges, static_cast<size_t>(content_len));
|
coalesce_ranges(req.ranges, static_cast<size_t>(content_len));
|
||||||
}
|
}
|
||||||
@@ -15636,9 +15693,9 @@ inline bool Server::check_if_range(Request &req, const std::string &etag,
|
|||||||
// Weak ETags are not valid for If-Range (RFC 9110 Section 13.1.5)
|
// Weak ETags are not valid for If-Range (RFC 9110 Section 13.1.5)
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// HTTP-date comparison
|
// HTTP-date must exactly match Last-Modified (RFC 9110 Section 13.1.5)
|
||||||
auto t = detail::parse_http_date(val);
|
auto t = detail::parse_http_date(val);
|
||||||
return (t != static_cast<time_t>(-1) && mtime <= t);
|
return (t != static_cast<time_t>(-1) && mtime == t);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -16043,8 +16100,9 @@ inline void Server::apply_ranges(const Request &req, Response &res,
|
|||||||
"multipart/byteranges; boundary=" + boundary);
|
"multipart/byteranges; boundary=" + boundary);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto type =
|
auto type = compress_ && !detail::is_partial_content(req, res)
|
||||||
compress_ ? detail::encoding_type(req, res) : detail::EncodingType::None;
|
? detail::encoding_type(req, res)
|
||||||
|
: detail::EncodingType::None;
|
||||||
|
|
||||||
if (res.body.empty()) {
|
if (res.body.empty()) {
|
||||||
if (res.content_length_ > 0 && req.ranges.size() == 1 &&
|
if (res.content_length_ > 0 && req.ranges.size() == 1 &&
|
||||||
@@ -16216,7 +16274,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);
|
||||||
@@ -17020,7 +17078,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,
|
||||||
@@ -17310,7 +17371,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;
|
||||||
@@ -17401,6 +17463,14 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf,
|
|||||||
constexpr size_t kDecompressionBufferSize = 8192;
|
constexpr size_t kDecompressionBufferSize = 8192;
|
||||||
char compressed_buf[kDecompressionBufferSize];
|
char compressed_buf[kDecompressionBufferSize];
|
||||||
|
|
||||||
|
auto limit = body_reader_.payload_max_length;
|
||||||
|
auto counted = decompressed_bytes_read_;
|
||||||
|
if (limit == 0) {
|
||||||
|
// The stream is unbounded, but one read holds all it inflates at once
|
||||||
|
limit = CPPHTTPLIB_PAYLOAD_MAX_LENGTH;
|
||||||
|
counted = 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto n = detail::read_body_content(stream_, body_reader_, compressed_buf,
|
auto n = detail::read_body_content(stream_, body_reader_, compressed_buf,
|
||||||
sizeof(compressed_buf));
|
sizeof(compressed_buf));
|
||||||
@@ -17412,10 +17482,7 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf,
|
|||||||
compressed_buf, static_cast<size_t>(n),
|
compressed_buf, static_cast<size_t>(n),
|
||||||
[&](const char *data, size_t data_len) {
|
[&](const char *data, size_t data_len) {
|
||||||
decompress_buffer_.append(data, data_len);
|
decompress_buffer_.append(data, data_len);
|
||||||
auto limit = body_reader_.payload_max_length;
|
exceeded = limit > 0 && counted + decompress_buffer_.size() > limit;
|
||||||
exceeded =
|
|
||||||
limit > 0 &&
|
|
||||||
decompressed_bytes_read_ + decompress_buffer_.size() > limit;
|
|
||||||
return !exceeded;
|
return !exceeded;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -17725,10 +17792,12 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
|
|||||||
|
|
||||||
auto path = std::move(next_path) + next_query;
|
auto path = std::move(next_path) + next_query;
|
||||||
|
|
||||||
// A Unix socket client sends "Host: localhost", so a Location naming
|
// Host names are case-insensitive (RFC 3986 3.2.2). A Unix socket client
|
||||||
// localhost names this client too.
|
// sends "Host: localhost", so a Location naming localhost names this client
|
||||||
auto is_same_host = next_host == host_ ||
|
// too.
|
||||||
(address_family_ == AF_UNIX && next_host == "localhost");
|
auto is_same_host = detail::case_ignore::equal(next_host, host_) ||
|
||||||
|
(address_family_ == AF_UNIX &&
|
||||||
|
detail::case_ignore::equal(next_host, "localhost"));
|
||||||
|
|
||||||
// Same host redirect - use current client
|
// Same host redirect - use current client
|
||||||
if (next_scheme == scheme && is_same_host && next_port == port_) {
|
if (next_scheme == scheme && is_same_host && next_port == port_) {
|
||||||
@@ -18344,7 +18413,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;
|
||||||
}
|
}
|
||||||
@@ -18361,7 +18431,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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,12 +13,14 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
|
|||||||
BROTLI_DIR = /usr/local/opt/brotli
|
BROTLI_DIR = /usr/local/opt/brotli
|
||||||
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
||||||
|
|
||||||
FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer websocket_fuzzer sse_parser_fuzzer server_mount_fuzzer
|
FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer websocket_fuzzer sse_parser_fuzzer server_mount_fuzzer chunked_decoder_fuzzer
|
||||||
|
|
||||||
# Runs all the tests and also fuzz tests against seed corpus.
|
# Runs all the tests and also fuzz tests against seed corpus.
|
||||||
all : $(FUZZERS)
|
all : $(FUZZERS)
|
||||||
./server_fuzzer corpus/*
|
./server_fuzzer corpus/*
|
||||||
./server_mount_fuzzer corpus_mount/*
|
./server_mount_fuzzer corpus_mount/*
|
||||||
|
./multipart_parser_fuzzer corpus_multipart/*
|
||||||
|
./chunked_decoder_fuzzer corpus_chunked/*
|
||||||
|
|
||||||
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
||||||
server_fuzzer : server_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
server_fuzzer : server_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
||||||
@@ -38,6 +40,7 @@ url_parser_fuzzer : url_parser_fuzzer.cc ../../httplib.h
|
|||||||
|
|
||||||
multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h
|
multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h
|
||||||
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
zip -q -r multipart_parser_fuzzer_seed_corpus.zip corpus_multipart
|
||||||
|
|
||||||
websocket_fuzzer : websocket_fuzzer.cc ../../httplib.h
|
websocket_fuzzer : websocket_fuzzer.cc ../../httplib.h
|
||||||
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
@@ -49,5 +52,9 @@ server_mount_fuzzer : server_mount_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
|||||||
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
zip -q -r server_mount_fuzzer_seed_corpus.zip corpus_mount
|
zip -q -r server_mount_fuzzer_seed_corpus.zip corpus_mount
|
||||||
|
|
||||||
|
chunked_decoder_fuzzer : chunked_decoder_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
zip -q -r chunked_decoder_fuzzer_seed_corpus.zip corpus_chunked
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "fuzzed_stream.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void decode(const uint8_t *data, size_t size, size_t payload_max_length) {
|
||||||
|
FuzzedStream strm(data, size);
|
||||||
|
httplib::Response res;
|
||||||
|
httplib::detail::read_content_chunked(
|
||||||
|
strm, res, payload_max_length,
|
||||||
|
[](const char *, size_t, uint64_t, uint64_t) { return true; });
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
// The first decode initializes function-local statics, which in a persistent
|
||||||
|
// fuzzer would otherwise show up as unstable coverage on the first input only.
|
||||||
|
extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
|
||||||
|
static const char warm_up[] =
|
||||||
|
"5;ext=1\r\nhello\r\n0\r\nX-Trailer: 1\r\nExpires: 0\r\n\r\n";
|
||||||
|
decode(reinterpret_cast<const uint8_t *>(warm_up), sizeof(warm_up) - 1, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
|
if (size < 2) { return 0; }
|
||||||
|
|
||||||
|
// The first byte selects the payload limit, 0 meaning none
|
||||||
|
static const size_t max_lens[] = {0, 1, 16, 4096,
|
||||||
|
CPPHTTPLIB_PAYLOAD_MAX_LENGTH};
|
||||||
|
auto n = sizeof(max_lens) / sizeof(max_lens[0]);
|
||||||
|
decode(data + 1, size - 1, max_lens[data[0] % n]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
crlf="\x0d\x0a"
|
||||||
|
last_chunk="0\x0d\x0a\x0d\x0a"
|
||||||
|
ext=";"
|
||||||
|
ext_kv=";name=value"
|
||||||
|
ext_quoted=";name=\"a;b\""
|
||||||
|
hex_max="ffffffffffffffff"
|
||||||
|
hex_over="10000000000000000"
|
||||||
|
trailer="X-Trailer: 1\x0d\x0a"
|
||||||
|
expires="Expires: 0\x0d\x0a"
|
||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,4 @@
|
|||||||
|
10
|
||||||
|
0123456789abcdef
|
||||||
|
0
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
10
|
||||||
|
0123456789abcdef
|
||||||
|
1
|
||||||
|
X
|
||||||
|
0
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
5
|
||||||
|
hello
|
||||||
|
0
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
5
|
||||||
|
hello
|
||||||
|
0
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
|
|||||||
|
1234567890123456--1234567890123456
|
||||||
|
Content-Disposition: form-data; name="f"
|
||||||
|
|
||||||
|
v
|
||||||
|
--1234567890123456--
|
||||||
Binary file not shown.
@@ -0,0 +1,5 @@
|
|||||||
|
BOUND--BOUND
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
|
||||||
|
text contains --BOUND here
|
||||||
|
--BOUND--
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
XYZ123--XYZ123
|
||||||
|
Content-Disposition: form-data; name="t"
|
||||||
|
|
||||||
|
--XYZ123 not after CRLF
|
||||||
|
|
||||||
|
--XYZ12
|
||||||
|
|
||||||
|
-XYZ123
|
||||||
|
|
||||||
|
--xyz123
|
||||||
|
|
||||||
|
--XYZ_123␍ tail
|
||||||
|
--XYZ123--
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
cdBad--cdBad
|
||||||
|
Content-Disposition: form-data; filename="noname.txt"
|
||||||
|
|
||||||
|
missing name
|
||||||
|
--cdBad
|
||||||
|
Content-Disposition: form-data; name="x"; filename*=ISO-8859-1''abc
|
||||||
|
|
||||||
|
bad charset
|
||||||
|
--cdBad
|
||||||
|
Bad Header Without Colon
|
||||||
|
|
||||||
|
z
|
||||||
|
--cdBad--
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
cdVar--cdVar
|
||||||
|
content-disposition:FORM-DATA;name=bare
|
||||||
|
|
||||||
|
1
|
||||||
|
--cdVar
|
||||||
|
Content-Disposition: form-data; name="semi;colon"; filename="a\"b.txt"
|
||||||
|
|
||||||
|
2
|
||||||
|
--cdVar
|
||||||
|
Content-Disposition: form-data; name="u"; filename="x.txt"; filename*=UTF-8''%E2%82%AC%20rates.txt
|
||||||
|
|
||||||
|
3
|
||||||
|
--cdVar
|
||||||
|
Content-Disposition: form-data; name="dup"; name="dup"; name="other"
|
||||||
|
|
||||||
|
4
|
||||||
|
--cdVar
|
||||||
|
Content-Disposition: attachment; name="ignored"
|
||||||
|
CONTENT-TYPE: text/html
|
||||||
|
|
||||||
|
5
|
||||||
|
--cdVar--
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
empty--empty
|
||||||
|
|
||||||
|
|
||||||
|
--empty
|
||||||
|
Content-Disposition: form-data; name="e"
|
||||||
|
|
||||||
|
|
||||||
|
--empty
|
||||||
|
|
||||||
|
|
||||||
|
--empty--
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
BOUND--BOUND
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
X-Custom: test
|
||||||
|
|
||||||
|
val
|
||||||
|
--BOUND--
|
||||||
Binary file not shown.
@@ -0,0 +1,9 @@
|
|||||||
|
pad--pad
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
|
||||||
|
ok
|
||||||
|
--pad
|
||||||
|
Content-Disposition: form-data; name="b"
|
||||||
|
|
||||||
|
x
|
||||||
|
--pad--
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
BOUND
|
||||||
|
--BOUND
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
|
||||||
|
val
|
||||||
|
--BOUND--
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
unclosed--unclosed
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
|
||||||
|
first
|
||||||
|
--unclosed
|
||||||
|
Content-Disposition: form-data; name="b"
|
||||||
|
|
||||||
|
truncated content with no final delimiter
|
||||||
|
--unclo
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
BOUND--BOUND
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
|
||||||
|
val
|
||||||
|
--BOUND--
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
nocrlf--nocrlfContent-Disposition: form-data; name="x"
|
||||||
|
|
||||||
|
data
|
||||||
|
--nocrlf--
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
hdrEnd--hdrEnd
|
||||||
|
Content-Disposition: form-data; name="h"
|
||||||
|
Content-Type: text/plain
|
||||||
|
content right after headers--hdrEnd--
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
BOUND--BOUND
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
|
||||||
|
1
|
||||||
|
--BOUND
|
||||||
|
Content-Disposition: form-data; name="b"
|
||||||
|
|
||||||
|
2
|
||||||
|
--BOUND--
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
BOUND--BOUND
|
||||||
|
Content-Type: text/plain
|
||||||
|
|
||||||
|
orphaned data
|
||||||
|
--BOUND--
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
sev3ral--sev3ral
|
||||||
|
Content-Disposition: form-data; name="a"
|
||||||
|
|
||||||
|
alpha
|
||||||
|
--sev3ral
|
||||||
|
Content-Disposition: form-data; name="b"
|
||||||
|
|
||||||
|
bravo
|
||||||
|
second line
|
||||||
|
--sev3ral
|
||||||
|
Content-Disposition: form-data; name="c"
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{"k":[1,2,3]}
|
||||||
|
--sev3ral--
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
AaB03x--AaB03x
|
||||||
|
Content-Disposition: form-data; name="field1"
|
||||||
|
|
||||||
|
value1
|
||||||
|
--AaB03x--
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
BOUND--BOUND
|
||||||
|
Content-Disposition: form-data; name="user"
|
||||||
|
|
||||||
|
Alice
|
||||||
|
--BOUND--
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
BOUND--BOUND
|
||||||
|
Content-Disposition : form-data ; name = "a"
|
||||||
|
|
||||||
|
val
|
||||||
|
--BOUND--
|
||||||
+282
-5
@@ -347,6 +347,9 @@ TEST_F(UnixSocketTest, RedirectStaysOnSocket) {
|
|||||||
// An absolute Location naming the Host this client sends
|
// An absolute Location naming the Host this client sends
|
||||||
res.set_redirect("http://localhost" + pattern_);
|
res.set_redirect("http://localhost" + pattern_);
|
||||||
});
|
});
|
||||||
|
svr.Get("/upper", [&](const httplib::Request &, httplib::Response &res) {
|
||||||
|
res.set_redirect("http://LOCALHOST" + pattern_);
|
||||||
|
});
|
||||||
svr.Get("/cross", [&](const httplib::Request &, httplib::Response &res) {
|
svr.Get("/cross", [&](const httplib::Request &, httplib::Response &res) {
|
||||||
res.set_redirect("http://" + other_path + pattern_);
|
res.set_redirect("http://" + other_path + pattern_);
|
||||||
});
|
});
|
||||||
@@ -399,6 +402,13 @@ TEST_F(UnixSocketTest, RedirectStaysOnSocket) {
|
|||||||
EXPECT_EQ(content_, res->body);
|
EXPECT_EQ(content_, res->body);
|
||||||
EXPECT_EQ("Bearer token", res->get_header_value("X-Auth"));
|
EXPECT_EQ("Bearer token", res->get_header_value("X-Auth"));
|
||||||
|
|
||||||
|
// in any case
|
||||||
|
res = cli.Get("/upper");
|
||||||
|
ASSERT_TRUE(res) << "error: " << res.error();
|
||||||
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
|
EXPECT_EQ(content_, res->body);
|
||||||
|
EXPECT_EQ("Bearer token", res->get_header_value("X-Auth"));
|
||||||
|
|
||||||
// A host the caller mapped to a socket goes there
|
// A host the caller mapped to a socket goes there
|
||||||
cli.set_direct_hostname_addr_map({{"mapped.test", other_path}});
|
cli.set_direct_hostname_addr_map({{"mapped.test", other_path}});
|
||||||
res = cli.Get("/mapped");
|
res = cli.Get("/mapped");
|
||||||
@@ -4744,6 +4754,45 @@ TEST(RedirectToSamePort, ForwardCookie) {
|
|||||||
EXPECT_EQ("session_id=SECRET", captured_cookie);
|
EXPECT_EQ("session_id=SECRET", captured_cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(RedirectToSamePort, HostNameIsCaseInsensitive) {
|
||||||
|
// "LOCALHOST" names the same host as "localhost" (RFC 3986 3.2.2), so the
|
||||||
|
// redirect stays on this origin and keeps its credentials and cookies.
|
||||||
|
Server svr;
|
||||||
|
std::string captured_authorization;
|
||||||
|
std::string captured_cookie;
|
||||||
|
int port = 0;
|
||||||
|
svr.Get("/redir", [&](const Request & /*req*/, Response &res) {
|
||||||
|
res.set_redirect("http://LOCALHOST:" + std::to_string(port) + "/target",
|
||||||
|
302);
|
||||||
|
});
|
||||||
|
svr.Get("/target", [&](const Request &req, Response &res) {
|
||||||
|
captured_authorization = req.get_header_value("Authorization");
|
||||||
|
captured_cookie = req.get_header_value("Cookie");
|
||||||
|
res.set_content("OK", "text/plain");
|
||||||
|
});
|
||||||
|
|
||||||
|
port = svr.bind_to_any_port(HOST);
|
||||||
|
auto thread = std::thread([&]() { svr.listen_after_bind(); });
|
||||||
|
auto se = detail::scope_exit([&] {
|
||||||
|
svr.stop();
|
||||||
|
thread.join();
|
||||||
|
ASSERT_FALSE(svr.is_running());
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.wait_until_ready();
|
||||||
|
|
||||||
|
Client cli(HOST, port);
|
||||||
|
cli.set_follow_location(true);
|
||||||
|
cli.set_basic_auth("admin", "secret");
|
||||||
|
|
||||||
|
Headers headers = {{"Cookie", "session_id=SECRET"}};
|
||||||
|
auto res = cli.Get("/redir", headers);
|
||||||
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
|
EXPECT_EQ("Basic YWRtaW46c2VjcmV0", captured_authorization);
|
||||||
|
EXPECT_EQ("session_id=SECRET", captured_cookie);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(RedirectTest, DoNotCarryRequestParamsToLocation) {
|
TEST(RedirectTest, DoNotCarryRequestParamsToLocation) {
|
||||||
// The Location alone names the next target, on any origin.
|
// The Location alone names the next target, on any origin.
|
||||||
Server target_svr;
|
Server target_svr;
|
||||||
@@ -9392,6 +9441,20 @@ TEST_F(ServerTest, GetWithRange5) {
|
|||||||
EXPECT_EQ(std::string("abcdef"), res->body);
|
EXPECT_EQ(std::string("abcdef"), res->body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
||||||
|
TEST_F(ServerTest, GetWithRangeAcceptEncoding) {
|
||||||
|
cli_.set_decompress(false);
|
||||||
|
auto res = cli_.Get("/with-range", Headers{{"Range", "bytes=1-3"},
|
||||||
|
{"Accept-Encoding", "gzip"}});
|
||||||
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
|
EXPECT_EQ("bytes 1-3/7", res->get_header_value("Content-Range"));
|
||||||
|
EXPECT_EQ("3", res->get_header_value("Content-Length"));
|
||||||
|
EXPECT_EQ("bcd", res->body);
|
||||||
|
EXPECT_FALSE(res->has_header("Content-Encoding"));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) {
|
TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) {
|
||||||
auto res =
|
auto res =
|
||||||
cli_.Get("/with-range", Headers{{make_range_header({{10000, 20000}})}});
|
cli_.Get("/with-range", Headers{{make_range_header({{10000, 20000}})}});
|
||||||
@@ -9410,10 +9473,47 @@ TEST_F(ServerTest, GetWithRangeMultipart) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
|
TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
|
||||||
|
// RFC 9110 14.1.1: the set is satisfiable when any of its ranges is, and the
|
||||||
|
// unsatisfiable ones are left out of the response.
|
||||||
auto res = cli_.Get("/with-range",
|
auto res = cli_.Get("/with-range",
|
||||||
Headers{{make_range_header({{-1, 2}, {10000, 30000}})}});
|
Headers{make_range_header({{-1, 2}, {10000, 30000}}),
|
||||||
|
{"Accept-Encoding", ""}});
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
|
EXPECT_EQ("2", res->get_header_value("Content-Length"));
|
||||||
|
EXPECT_EQ("bytes 5-6/7", res->get_header_value("Content-Range"));
|
||||||
|
EXPECT_EQ(std::string("fg"), res->body);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, GetWithRangeMultipartSkipsUnsatisfiableRange) {
|
||||||
|
auto res =
|
||||||
|
cli_.Get("/with-range",
|
||||||
|
Headers{{make_range_header({{1, 2}, {10000, 30000}, {4, 5}})}});
|
||||||
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
|
EXPECT_EQ("267", res->get_header_value("Content-Length"));
|
||||||
|
EXPECT_EQ(false, res->has_header("Content-Range"));
|
||||||
|
EXPECT_EQ(267U, res->body.size());
|
||||||
|
EXPECT_TRUE(res->body.find("bc\r\n") != std::string::npos);
|
||||||
|
EXPECT_TRUE(res->body.find("ef\r\n") != std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, GetWithRangeMultipartNoneSatisfiable) {
|
||||||
|
for (auto range : {"bytes=7-, -0", "bytes=10000-30000, 7-8"}) {
|
||||||
|
auto res = cli_.Get("/with-range", Headers{{"Range", range}});
|
||||||
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status) << range;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, GetStreamedWithRangeSkipsUnsatisfiableRange) {
|
||||||
|
auto res =
|
||||||
|
cli_.Get("/streamed-with-range", Headers{{"Range", "bytes=7-, 2-3"}});
|
||||||
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
|
EXPECT_EQ("2", res->get_header_value("Content-Length"));
|
||||||
|
EXPECT_EQ("bytes 2-3/7", res->get_header_value("Content-Range"));
|
||||||
|
EXPECT_EQ(std::string("cd"), res->body);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
|
TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
|
||||||
@@ -9959,6 +10059,50 @@ TEST(GzipDecompressor, DeflateDecompressionTrailingBytes) {
|
|||||||
ASSERT_EQ(original_text, decompressed_data);
|
ASSERT_EQ(original_text, decompressed_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(GzipDecompressor, MultipleMembers) {
|
||||||
|
auto gzip = [](const std::string &data) {
|
||||||
|
std::string out;
|
||||||
|
httplib::detail::gzip_compressor compressor;
|
||||||
|
compressor.compress(data.data(), data.size(), /*last=*/true,
|
||||||
|
[&](const char *chunk, size_t n) {
|
||||||
|
out.append(chunk, n);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
return out;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Decompresses `data` handed over in two calls, the first `split` bytes long
|
||||||
|
auto decompress = [](const std::string &data, size_t split,
|
||||||
|
std::string &out) {
|
||||||
|
httplib::detail::gzip_decompressor decompressor;
|
||||||
|
auto callback = [&](const char *chunk, size_t n) {
|
||||||
|
out.append(chunk, n);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
return decompressor.decompress(data.data(), split, callback) &&
|
||||||
|
decompressor.decompress(data.data() + split, data.size() - split,
|
||||||
|
callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
// RFC 1952 2.2: a gzip body is a series of members, an empty one included.
|
||||||
|
// Bytes after the last member that start no other one are still ignored.
|
||||||
|
const auto members = gzip("Hello, ") + gzip("") + gzip("World!");
|
||||||
|
for (const auto &body : {members, members + std::string(8, '\0')}) {
|
||||||
|
for (size_t split = 0; split <= body.size(); split++) {
|
||||||
|
std::string out;
|
||||||
|
ASSERT_TRUE(decompress(body, split, out)) << "split at " << split;
|
||||||
|
EXPECT_EQ("Hello, World!", out) << "split at " << split;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A broken later member fails like a broken first one
|
||||||
|
auto broken = gzip("Hello, ") + gzip("World!");
|
||||||
|
auto crc_pos = broken.size() - 8;
|
||||||
|
broken[crc_pos] = static_cast<char>(broken[crc_pos] ^ 0x01);
|
||||||
|
std::string out;
|
||||||
|
EXPECT_FALSE(decompress(broken, broken.size(), out));
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
|
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
|
||||||
@@ -16834,6 +16978,43 @@ TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
|
||||||
|
// A zero limit leaves a stream unbounded, but a body of a few bytes must not
|
||||||
|
// inflate past the default limit in a single read
|
||||||
|
TEST(ClientVulnerabilityTest, BrotliBombInStreamWithoutPayloadLimit) {
|
||||||
|
std::string bomb(1024, '\0');
|
||||||
|
{
|
||||||
|
std::string body(CPPHTTPLIB_PAYLOAD_MAX_LENGTH + 1024 * 1024, 'A');
|
||||||
|
auto size = bomb.size();
|
||||||
|
ASSERT_TRUE(BrotliEncoderCompress(
|
||||||
|
5, BROTLI_DEFAULT_WINDOW, BROTLI_MODE_GENERIC, body.size(),
|
||||||
|
reinterpret_cast<const uint8_t *>(body.data()), &size,
|
||||||
|
reinterpret_cast<uint8_t *>(&bomb[0])));
|
||||||
|
bomb.resize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Server svr;
|
||||||
|
svr.Get("/", [&](const Request &, Response &res) {
|
||||||
|
res.set_header("Content-Encoding", "br");
|
||||||
|
res.set_content(bomb, "text/plain");
|
||||||
|
});
|
||||||
|
auto port = svr.bind_to_any_port(HOST);
|
||||||
|
std::thread t([&] { svr.listen_after_bind(); });
|
||||||
|
auto se = detail::scope_exit([&] {
|
||||||
|
svr.stop();
|
||||||
|
t.join();
|
||||||
|
});
|
||||||
|
svr.wait_until_ready();
|
||||||
|
|
||||||
|
Client cli(HOST, port);
|
||||||
|
cli.set_payload_max_length(0);
|
||||||
|
auto result = stream::Get(cli, "/");
|
||||||
|
ASSERT_TRUE(result.is_valid());
|
||||||
|
while (result.next()) {}
|
||||||
|
EXPECT_EQ(Error::ExceedMaxPayloadSize, result.read_error());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST(HostAndPortPropertiesTest, NoSSL) {
|
TEST(HostAndPortPropertiesTest, NoSSL) {
|
||||||
httplib::Client cli("www.google.com", 1234);
|
httplib::Client cli("www.google.com", 1234);
|
||||||
ASSERT_EQ("www.google.com", cli.host());
|
ASSERT_EQ("www.google.com", cli.host());
|
||||||
@@ -20407,6 +20588,50 @@ TEST(MultipartFormDataTest, AlternateFilenameLongValueAndCaseInsensitive) {
|
|||||||
ASSERT_TRUE(send_request(1, req));
|
ASSERT_TRUE(send_request(1, req));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(MultipartFormDataTest, AlternateFilenameWithLanguageTag) {
|
||||||
|
auto handled = false;
|
||||||
|
|
||||||
|
Server svr;
|
||||||
|
svr.Post("/test", [&](const Request &req, Response &res) {
|
||||||
|
ASSERT_EQ("A.txt", req.form.get_file("file1").filename);
|
||||||
|
ASSERT_EQ("na\xC3\xAFve.txt", req.form.get_file("file2").filename);
|
||||||
|
|
||||||
|
res.set_content("ok", "text/plain");
|
||||||
|
handled = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
thread t = thread([&] { svr.listen(HOST, PORT); });
|
||||||
|
auto se = detail::scope_exit([&] {
|
||||||
|
svr.stop();
|
||||||
|
t.join();
|
||||||
|
ASSERT_FALSE(svr.is_running());
|
||||||
|
ASSERT_TRUE(handled);
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.wait_until_ready();
|
||||||
|
|
||||||
|
std::string body = "----------\r\n"
|
||||||
|
"Content-Disposition: form-data; name=\"file1\"; "
|
||||||
|
"filename*=UTF-8'en'%41.txt\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"hello\r\n"
|
||||||
|
"----------\r\n"
|
||||||
|
"Content-Disposition: form-data; name=\"file2\"; "
|
||||||
|
"filename*=utf-8'fr-CA'na%C3%AFve.txt\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"bonjour\r\n"
|
||||||
|
"------------\r\n";
|
||||||
|
|
||||||
|
auto req = "POST /test HTTP/1.1\r\n"
|
||||||
|
"Host: localhost\r\n"
|
||||||
|
"Connection: close\r\n"
|
||||||
|
"Content-Type: multipart/form-data;boundary=--------\r\n"
|
||||||
|
"Content-Length: " +
|
||||||
|
std::to_string(body.size()) + "\r\n\r\n" + body;
|
||||||
|
|
||||||
|
ASSERT_TRUE(send_request(1, req));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) {
|
TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) {
|
||||||
auto handled = false;
|
auto handled = false;
|
||||||
|
|
||||||
@@ -25378,6 +25603,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"
|
||||||
@@ -27033,13 +27288,35 @@ TEST(ETagTest, IfRangeWithDate) {
|
|||||||
EXPECT_EQ(200, res3->status);
|
EXPECT_EQ(200, res3->status);
|
||||||
EXPECT_EQ(content, res3->body);
|
EXPECT_EQ(content, res3->body);
|
||||||
|
|
||||||
// Range request with future If-Range date: should get 206
|
// Range request with future If-Range date: should get 200 (full content)
|
||||||
Headers h4 = {{"Range", "bytes=0-4"},
|
Headers h4 = {{"Range", "bytes=0-4"},
|
||||||
{"If-Range", "Sun, 01 Jan 2099 00:00:00 GMT"}};
|
{"If-Range", "Sun, 01 Jan 2099 00:00:00 GMT"}};
|
||||||
auto res4 = cli.Get("/static/if_range_date_testfile.txt", h4);
|
auto res4 = cli.Get("/static/if_range_date_testfile.txt", h4);
|
||||||
ASSERT_TRUE(res4);
|
ASSERT_TRUE(res4);
|
||||||
EXPECT_EQ(206, res4->status);
|
EXPECT_EQ(200, res4->status);
|
||||||
EXPECT_EQ("ABCDE", res4->body);
|
EXPECT_EQ(content, res4->body);
|
||||||
|
|
||||||
|
#if defined(__APPLE__) || defined(__linux__)
|
||||||
|
// File replaced by one with an older mtime: should get 200 (full content)
|
||||||
|
const std::string replaced = "abcdefghij9876543210";
|
||||||
|
{
|
||||||
|
std::ofstream ofs(fname);
|
||||||
|
ofs << replaced;
|
||||||
|
ASSERT_TRUE(ofs.good());
|
||||||
|
}
|
||||||
|
struct timeval times[2];
|
||||||
|
times[0].tv_sec = time(nullptr);
|
||||||
|
times[0].tv_usec = 0;
|
||||||
|
times[1].tv_sec = time(nullptr) - 3600;
|
||||||
|
times[1].tv_usec = 0;
|
||||||
|
ASSERT_EQ(0, utimes(fname, times));
|
||||||
|
|
||||||
|
Headers h5 = {{"Range", "bytes=5-9"}, {"If-Range", last_modified}};
|
||||||
|
auto res5 = cli.Get("/static/if_range_date_testfile.txt", h5);
|
||||||
|
ASSERT_TRUE(res5);
|
||||||
|
EXPECT_EQ(200, res5->status);
|
||||||
|
EXPECT_EQ(replaced, res5->body);
|
||||||
|
#endif
|
||||||
|
|
||||||
svr.stop();
|
svr.stop();
|
||||||
t.join();
|
t.join();
|
||||||
|
|||||||
Reference in new issue
Block a user