This commit is contained in:
Florian Albrechtskirchinger 2025-02-21 06:09:27 +01:00 committed by GitHub
commit 9da2ae1ab2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -524,6 +524,11 @@ using Progress = std::function<bool(uint64_t current, uint64_t total)>;
struct Response;
using ResponseHandler = std::function<bool(const Response &response)>;
class Stream;
// Note: do not replace 'std::function<bool(Stream &strm)>' with StreamHandler;
// signature is not final
using StreamHandler = std::function<bool(Stream &strm)>;
struct MultipartFormData {
std::string name;
std::string content;
@ -641,6 +646,7 @@ struct Request {
// for client
ResponseHandler response_handler;
StreamHandler stream_handler; // EXPERIMENTAL function signature may change
ContentReceiverWithProgress content_receiver;
Progress progress;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@ -1170,6 +1176,7 @@ enum class Error {
Compression,
ConnectionTimeout,
ProxyConnection,
StreamHandler,
// For internal use only
SSLPeerCouldBeClosed_,
@ -2260,6 +2267,7 @@ inline std::string to_string(const Error error) {
case Error::Compression: return "Compression failed";
case Error::ConnectionTimeout: return "Connection timed out";
case Error::ProxyConnection: return "Proxy connection failed";
case Error::StreamHandler: return "Stream handler failed";
case Error::Unknown: return "Unknown";
default: break;
}
@ -7800,10 +7808,12 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
}
}
if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); }
if (!req.stream_handler && !req.has_header("Accept")) {
req.set_header("Accept", "*/*");
}
if (!req.content_receiver) {
if (!req.has_header("Accept-Encoding")) {
if (!req.stream_handler && !req.has_header("Accept-Encoding")) {
std::string accept_encoding;
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
accept_encoding = "br";
@ -7821,7 +7831,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
req.set_header("User-Agent", agent);
}
#endif
};
}
if (req.body.empty()) {
if (req.content_provider_) {
@ -8053,10 +8063,23 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
res.status != StatusCode::NotModified_304 &&
follow_location_;
if (req.response_handler && !redirect) {
if (!req.response_handler(res)) {
error = Error::Canceled;
return false;
if (!redirect) {
if (req.response_handler) {
if (!req.response_handler(res)) {
error = Error::Canceled;
return false;
}
}
if (req.stream_handler) {
// Log early
if (logger_) { logger_(req, res); }
if (!req.stream_handler(strm)) {
error = Error::StreamHandler;
return false;
}
return true;
}
}