Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 50 additions & 51 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ using Headers =
using Params = std::multimap<std::string, std::string>;
using Match = std::smatch;

using DownloadProgress = std::function<bool(uint64_t current, uint64_t total)>;
using UploadProgress = std::function<bool(uint64_t current, uint64_t total)>;
using DownloadProgress = std::function<bool(size_t current, size_t total)>;
using UploadProgress = std::function<bool(size_t current, size_t total)>;

struct Response;
using ResponseHandler = std::function<bool(const Response &response)>;
Expand Down Expand Up @@ -674,9 +674,8 @@ struct FormDataProvider {
};
using FormDataProviderItems = std::vector<FormDataProvider>;

using ContentReceiverWithProgress =
std::function<bool(const char *data, size_t data_length, uint64_t offset,
uint64_t total_length)>;
using ContentReceiverWithProgress = std::function<bool(
const char *data, size_t data_length, size_t offset, size_t total_length)>;

using ContentReceiver =
std::function<bool(const char *data, size_t data_length)>;
Expand Down Expand Up @@ -744,8 +743,8 @@ struct Request {
bool has_header(const std::string &key) const;
std::string get_header_value(const std::string &key, const char *def = "",
size_t id = 0) const;
uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0,
size_t id = 0) const;
size_t get_header_value_u64(const std::string &key, size_t def = 0,
size_t id = 0) const;
size_t get_header_value_count(const std::string &key) const;
void set_header(const std::string &key, const std::string &val);

Expand Down Expand Up @@ -781,8 +780,8 @@ struct Response {
bool has_header(const std::string &key) const;
std::string get_header_value(const std::string &key, const char *def = "",
size_t id = 0) const;
uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0,
size_t id = 0) const;
size_t get_header_value_u64(const std::string &key, size_t def = 0,
size_t id = 0) const;
size_t get_header_value_count(const std::string &key) const;
void set_header(const std::string &key, const std::string &val);

Expand Down Expand Up @@ -1334,8 +1333,8 @@ class Result {
std::string get_request_header_value(const std::string &key,
const char *def = "",
size_t id = 0) const;
uint64_t get_request_header_value_u64(const std::string &key,
uint64_t def = 0, size_t id = 0) const;
size_t get_request_header_value_u64(const std::string &key, size_t def = 0,
size_t id = 0) const;
size_t get_request_header_value_count(const std::string &key) const;

private:
Expand Down Expand Up @@ -2010,9 +2009,9 @@ inline bool is_numeric(const std::string &str) {
[](unsigned char c) { return std::isdigit(c); });
}

inline uint64_t get_header_value_u64(const Headers &headers,
const std::string &key, uint64_t def,
size_t id, bool &is_invalid_value) {
inline size_t get_header_value_u64(const Headers &headers,
const std::string &key, size_t def,
size_t id, bool &is_invalid_value) {
is_invalid_value = false;
auto rng = headers.equal_range(key);
auto it = rng.first;
Expand All @@ -2027,22 +2026,22 @@ inline uint64_t get_header_value_u64(const Headers &headers,
return def;
}

inline uint64_t get_header_value_u64(const Headers &headers,
const std::string &key, uint64_t def,
size_t id) {
inline size_t get_header_value_u64(const Headers &headers,
const std::string &key, size_t def,
size_t id) {
bool dummy = false;
return get_header_value_u64(headers, key, def, id, dummy);
}

} // namespace detail

inline uint64_t Request::get_header_value_u64(const std::string &key,
uint64_t def, size_t id) const {
inline size_t Request::get_header_value_u64(const std::string &key, size_t def,
size_t id) const {
return detail::get_header_value_u64(headers, key, def, id);
}

inline uint64_t Response::get_header_value_u64(const std::string &key,
uint64_t def, size_t id) const {
inline size_t Response::get_header_value_u64(const std::string &key, size_t def,
size_t id) const {
return detail::get_header_value_u64(headers, key, def, id);
}

Expand Down Expand Up @@ -2228,9 +2227,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) {
return os;
}

inline uint64_t Result::get_request_header_value_u64(const std::string &key,
uint64_t def,
size_t id) const {
inline size_t Result::get_request_header_value_u64(const std::string &key,
size_t def,
size_t id) const {
return detail::get_header_value_u64(request_headers_, key, def, id);
}

Expand Down Expand Up @@ -4617,19 +4616,19 @@ inline bool read_headers(Stream &strm, Headers &headers) {
return true;
}

inline bool read_content_with_length(Stream &strm, uint64_t len,
inline bool read_content_with_length(Stream &strm, size_t len,
DownloadProgress progress,
ContentReceiverWithProgress out) {
char buf[CPPHTTPLIB_RECV_BUFSIZ];

uint64_t r = 0;
size_t r = 0;
while (r < len) {
auto read_len = static_cast<size_t>(len - r);
auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ));
if (n <= 0) { return false; }

if (!out(buf, static_cast<size_t>(n), r, len)) { return false; }
r += static_cast<uint64_t>(n);
r += static_cast<size_t>(n);

if (progress) {
if (!progress(r, len)) { return false; }
Expand All @@ -4639,14 +4638,14 @@ inline bool read_content_with_length(Stream &strm, uint64_t len,
return true;
}

inline void skip_content_with_length(Stream &strm, uint64_t len) {
inline void skip_content_with_length(Stream &strm, size_t len) {
char buf[CPPHTTPLIB_RECV_BUFSIZ];
uint64_t r = 0;
size_t r = 0;
while (r < len) {
auto read_len = static_cast<size_t>(len - r);
auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ));
if (n <= 0) { return; }
r += static_cast<uint64_t>(n);
r += static_cast<size_t>(n);
}
}

Expand All @@ -4660,22 +4659,22 @@ inline ReadContentResult
read_content_without_length(Stream &strm, size_t payload_max_length,
ContentReceiverWithProgress out) {
char buf[CPPHTTPLIB_RECV_BUFSIZ];
uint64_t r = 0;
size_t r = 0;
for (;;) {
auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ);
if (n == 0) { return ReadContentResult::Success; }
if (n < 0) { return ReadContentResult::Error; }

// Check if adding this data would exceed the payload limit
if (r > payload_max_length ||
payload_max_length - r < static_cast<uint64_t>(n)) {
payload_max_length - r < static_cast<size_t>(n)) {
return ReadContentResult::PayloadTooLarge;
}

if (!out(buf, static_cast<size_t>(n), r, 0)) {
return ReadContentResult::Error;
}
r += static_cast<uint64_t>(n);
r += static_cast<size_t>(n);
}

return ReadContentResult::Success;
Expand All @@ -4693,7 +4692,7 @@ inline ReadContentResult read_content_chunked(Stream &strm, T &x,
if (!line_reader.getline()) { return ReadContentResult::Error; }

unsigned long chunk_len;
uint64_t total_len = 0;
size_t total_len = 0;
while (true) {
char *end_ptr;

Expand Down Expand Up @@ -4845,7 +4844,7 @@ bool prepare_content_receiver(T &x, int &status,
if (decompressor) {
if (decompressor->is_valid()) {
ContentReceiverWithProgress out = [&](const char *buf, size_t n,
uint64_t off, uint64_t len) {
size_t off, size_t len) {
return decompressor->decompress(buf, n,
[&](const char *buf2, size_t n2) {
return receiver(buf2, n2, off, len);
Expand All @@ -4859,8 +4858,8 @@ bool prepare_content_receiver(T &x, int &status,
}
}

ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off,
uint64_t len) {
ContentReceiverWithProgress out = [&](const char *buf, size_t n, size_t off,
size_t len) {
return receiver(buf, n, off, len);
};
return callback(std::move(out));
Expand Down Expand Up @@ -4899,9 +4898,9 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
}
} else {
auto is_invalid_value = false;
auto len = get_header_value_u64(
x.headers, "Content-Length",
(std::numeric_limits<uint64_t>::max)(), 0, is_invalid_value);
auto len = get_header_value_u64(x.headers, "Content-Length",
(std::numeric_limits<size_t>::max)(),
0, is_invalid_value);

if (is_invalid_value) {
ret = false;
Expand Down Expand Up @@ -7568,13 +7567,13 @@ inline bool Server::read_content_core(
}

multipart_form_data_parser.set_boundary(std::move(boundary));
out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) {
out = [&](const char *buf, size_t n, size_t /*off*/, size_t /*len*/) {
return multipart_form_data_parser.parse(buf, n, multipart_header,
multipart_receiver);
};
} else {
out = [receiver](const char *buf, size_t n, uint64_t /*off*/,
uint64_t /*len*/) { return receiver(buf, n); };
out = [receiver](const char *buf, size_t n, size_t /*off*/,
size_t /*len*/) { return receiver(buf, n); };
}

if (req.method == "DELETE" && !req.has_header("Content-Length")) {
Expand Down Expand Up @@ -9093,21 +9092,21 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
auto out =
req.content_receiver
? static_cast<ContentReceiverWithProgress>(
[&](const char *buf, size_t n, uint64_t off, uint64_t len) {
[&](const char *buf, size_t n, size_t off, size_t len) {
if (redirect) { return true; }
auto ret = req.content_receiver(buf, n, off, len);
if (!ret) { error = Error::Canceled; }
return ret;
})
: static_cast<ContentReceiverWithProgress>(
[&](const char *buf, size_t n, uint64_t /*off*/,
uint64_t /*len*/) {
[&](const char *buf, size_t n, size_t /*off*/,
size_t /*len*/) {
assert(res.body.size() + n <= res.body.max_size());
res.body.append(buf, n);
return true;
});

auto progress = [&](uint64_t current, uint64_t total) {
auto progress = [&](size_t current, size_t total) {
if (!req.download_progress || redirect) { return true; }
auto ret = req.download_progress(current, total);
if (!ret) { error = Error::Canceled; }
Expand Down Expand Up @@ -9258,7 +9257,7 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers,
req.response_handler = std::move(response_handler);
req.content_receiver =
[content_receiver](const char *data, size_t data_length,
uint64_t /*offset*/, uint64_t /*total_length*/) {
size_t /*offset*/, size_t /*total_length*/) {
return content_receiver(data, data_length);
};
req.download_progress = std::move(progress);
Expand Down Expand Up @@ -9448,7 +9447,7 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers,
req.body = body;
req.content_receiver =
[content_receiver](const char *data, size_t data_length,
uint64_t /*offset*/, uint64_t /*total_length*/) {
size_t /*offset*/, size_t /*total_length*/) {
return content_receiver(data, data_length);
};
req.download_progress = std::move(progress);
Expand Down Expand Up @@ -9600,7 +9599,7 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers,
req.body = body;
req.content_receiver =
[content_receiver](const char *data, size_t data_length,
uint64_t /*offset*/, uint64_t /*total_length*/) {
size_t /*offset*/, size_t /*total_length*/) {
return content_receiver(data, data_length);
};
req.download_progress = std::move(progress);
Expand Down Expand Up @@ -9755,7 +9754,7 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers,
req.body = body;
req.content_receiver =
[content_receiver](const char *data, size_t data_length,
uint64_t /*offset*/, uint64_t /*total_length*/) {
size_t /*offset*/, size_t /*total_length*/) {
return content_receiver(data, data_length);
};
req.download_progress = std::move(progress);
Expand Down
Loading