Skip to content

Commit 2b790ac

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 e89bfd2 commit 2b790ac

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
@@ -659,6 +659,7 @@ struct Request {
659659

660660
// for client
661661
ResponseHandler response_handler;
662+
StreamHandler stream_handler; // EXPERIMENTAL function signature may change
662663
ContentReceiverWithProgress content_receiver;
663664
Progress progress;
664665
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -1192,6 +1193,7 @@ enum class Error {
11921193
Compression,
11931194
ConnectionTimeout,
11941195
ProxyConnection,
1196+
StreamHandler,
11951197

11961198
// For internal use only
11971199
SSLPeerCouldBeClosed_,
@@ -2282,6 +2284,7 @@ inline std::string to_string(const Error error) {
22822284
case Error::Compression: return "Compression failed";
22832285
case Error::ConnectionTimeout: return "Connection timed out";
22842286
case Error::ProxyConnection: return "Proxy connection failed";
2287+
case Error::StreamHandler: return "Stream handler failed";
22852288
case Error::Unknown: return "Unknown";
22862289
default: break;
22872290
}
@@ -7889,10 +7892,12 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
78897892
}
78907893
}
78917894

7892-
if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); }
7895+
if (!req.stream_handler && !req.has_header("Accept")) {
7896+
req.set_header("Accept", "*/*");
7897+
}
78937898

78947899
if (!req.content_receiver) {
7895-
if (!req.has_header("Accept-Encoding")) {
7900+
if (!req.stream_handler && !req.has_header("Accept-Encoding")) {
78967901
std::string accept_encoding;
78977902
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
78987903
accept_encoding = "br";
@@ -7910,7 +7915,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
79107915
req.set_header("User-Agent", agent);
79117916
}
79127917
#endif
7913-
};
7918+
}
79147919

79157920
if (req.body.empty()) {
79167921
if (req.content_provider_) {
@@ -8142,10 +8147,23 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
81428147
res.status != StatusCode::NotModified_304 &&
81438148
follow_location_;
81448149

8145-
if (req.response_handler && !redirect) {
8146-
if (!req.response_handler(res)) {
8147-
error = Error::Canceled;
8148-
return false;
8150+
if (!redirect) {
8151+
if (req.response_handler) {
8152+
if (!req.response_handler(res)) {
8153+
error = Error::Canceled;
8154+
return false;
8155+
}
8156+
}
8157+
8158+
if (req.stream_handler) {
8159+
// Log early
8160+
if (logger_) { logger_(req, res); }
8161+
8162+
if (!req.stream_handler(strm)) {
8163+
error = Error::StreamHandler;
8164+
return false;
8165+
}
8166+
return true;
81498167
}
81508168
}
81518169

0 commit comments

Comments
 (0)