Skip to content

Commit b18a6e8

Browse files
Add client stream handler
Introduce a new request callback, stream_handler. If set, it replaces all other content-serving mechanisms. The handler is not called in response to HEAD or CONNECT requests, or when following a redirect. Content-related default header fields are not added to the request. In conjunction with a response handler, it provides a minimal interface to implement a WebSocket client.
1 parent fc2daf9 commit b18a6e8

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

httplib.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ struct Request {
646646

647647
// for client
648648
ResponseHandler response_handler;
649+
StreamHandler stream_handler; // EXPERIMENTAL function signature may change
649650
ContentReceiverWithProgress content_receiver;
650651
Progress progress;
651652
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -1180,6 +1181,7 @@ enum class Error {
11801181
Compression,
11811182
ConnectionTimeout,
11821183
ProxyConnection,
1184+
StreamHandler,
11831185

11841186
// For internal use only
11851187
SSLPeerCouldBeClosed_,
@@ -2270,6 +2272,7 @@ inline std::string to_string(const Error error) {
22702272
case Error::Compression: return "Compression failed";
22712273
case Error::ConnectionTimeout: return "Connection timed out";
22722274
case Error::ProxyConnection: return "Proxy connection failed";
2275+
case Error::StreamHandler: return "Stream handler failed";
22732276
case Error::Unknown: return "Unknown";
22742277
default: break;
22752278
}
@@ -7825,10 +7828,12 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
78257828
}
78267829
}
78277830

7828-
if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); }
7831+
if (!req.stream_handler && !req.has_header("Accept")) {
7832+
req.set_header("Accept", "*/*");
7833+
}
78297834

78307835
if (!req.content_receiver) {
7831-
if (!req.has_header("Accept-Encoding")) {
7836+
if (!req.stream_handler && !req.has_header("Accept-Encoding")) {
78327837
std::string accept_encoding;
78337838
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
78347839
accept_encoding = "br";
@@ -7846,7 +7851,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
78467851
req.set_header("User-Agent", agent);
78477852
}
78487853
#endif
7849-
};
7854+
}
78507855

78517856
if (req.body.empty()) {
78527857
if (req.content_provider_) {
@@ -8078,10 +8083,23 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
80788083
res.status != StatusCode::NotModified_304 &&
80798084
follow_location_;
80808085

8081-
if (req.response_handler && !redirect) {
8082-
if (!req.response_handler(res)) {
8083-
error = Error::Canceled;
8084-
return false;
8086+
if (!redirect) {
8087+
if (req.response_handler) {
8088+
if (!req.response_handler(res)) {
8089+
error = Error::Canceled;
8090+
return false;
8091+
}
8092+
}
8093+
8094+
if (req.stream_handler) {
8095+
// Log early
8096+
if (logger_) { logger_(req, res); }
8097+
8098+
if (!req.stream_handler(strm)) {
8099+
error = Error::StreamHandler;
8100+
return false;
8101+
}
8102+
return true;
80858103
}
80868104
}
80878105

0 commit comments

Comments
 (0)