Merge e89bfd2189 into 71ba7e7b1b
This commit is contained in:
commit
70880c3f45
65
httplib.h
65
httplib.h
@ -524,6 +524,11 @@ using Progress = std::function<bool(uint64_t current, uint64_t total)>;
|
|||||||
struct Response;
|
struct Response;
|
||||||
using ResponseHandler = std::function<bool(const Response &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 {
|
struct MultipartFormData {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string content;
|
std::string content;
|
||||||
@ -712,6 +717,9 @@ struct Response {
|
|||||||
const std::string &content_type);
|
const std::string &content_type);
|
||||||
void set_file_content(const std::string &path);
|
void set_file_content(const std::string &path);
|
||||||
|
|
||||||
|
// EXPERIMENTAL callback function signature may change
|
||||||
|
void set_stream_handler(StreamHandler stream_handler);
|
||||||
|
|
||||||
Response() = default;
|
Response() = default;
|
||||||
Response(const Response &) = default;
|
Response(const Response &) = default;
|
||||||
Response &operator=(const Response &) = default;
|
Response &operator=(const Response &) = default;
|
||||||
@ -731,6 +739,8 @@ struct Response {
|
|||||||
bool content_provider_success_ = false;
|
bool content_provider_success_ = false;
|
||||||
std::string file_content_path_;
|
std::string file_content_path_;
|
||||||
std::string file_content_content_type_;
|
std::string file_content_content_type_;
|
||||||
|
// EXPERIMENTAL function signature may change
|
||||||
|
StreamHandler stream_handler_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Stream {
|
class Stream {
|
||||||
@ -5906,6 +5916,10 @@ inline void Response::set_file_content(const std::string &path) {
|
|||||||
file_content_path_ = path;
|
file_content_path_ = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void Response::set_stream_handler(StreamHandler stream_handler) {
|
||||||
|
stream_handler_ = std::move(stream_handler);
|
||||||
|
}
|
||||||
|
|
||||||
// Result implementation
|
// Result implementation
|
||||||
inline bool Result::has_request_header(const std::string &key) const {
|
inline bool Result::has_request_header(const std::string &key) const {
|
||||||
return request_headers_.find(key) != request_headers_.end();
|
return request_headers_.find(key) != request_headers_.end();
|
||||||
@ -6548,18 +6562,21 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection,
|
|||||||
res.set_header("Keep-Alive", s);
|
res.set_header("Keep-Alive", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) &&
|
if (!res.stream_handler_) {
|
||||||
!res.has_header("Content-Type")) {
|
if ((!res.body.empty() || res.content_length_ > 0 ||
|
||||||
res.set_header("Content-Type", "text/plain");
|
res.content_provider_) &&
|
||||||
}
|
!res.has_header("Content-Type")) {
|
||||||
|
res.set_header("Content-Type", "text/plain");
|
||||||
|
}
|
||||||
|
|
||||||
if (res.body.empty() && !res.content_length_ && !res.content_provider_ &&
|
if (res.body.empty() && !res.content_length_ && !res.content_provider_ &&
|
||||||
!res.has_header("Content-Length")) {
|
!res.has_header("Content-Length")) {
|
||||||
res.set_header("Content-Length", "0");
|
res.set_header("Content-Length", "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) {
|
if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) {
|
||||||
res.set_header("Accept-Ranges", "bytes");
|
res.set_header("Accept-Ranges", "bytes");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (post_routing_handler_) { post_routing_handler_(req, res); }
|
if (post_routing_handler_) { post_routing_handler_(req, res); }
|
||||||
@ -6577,16 +6594,24 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection,
|
|||||||
|
|
||||||
// Body
|
// Body
|
||||||
auto ret = true;
|
auto ret = true;
|
||||||
if (req.method != "HEAD") {
|
if (res.stream_handler_) {
|
||||||
if (!res.body.empty()) {
|
// Log early
|
||||||
if (!detail::write_data(strm, res.body.data(), res.body.size())) {
|
if (logger_) { logger_(req, res); }
|
||||||
ret = false;
|
|
||||||
}
|
return res.stream_handler_(strm);
|
||||||
} else if (res.content_provider_) {
|
} else {
|
||||||
if (write_content_with_provider(strm, req, res, boundary, content_type)) {
|
if (req.method != "HEAD") {
|
||||||
res.content_provider_success_ = true;
|
if (!res.body.empty()) {
|
||||||
} else {
|
if (!detail::write_data(strm, res.body.data(), res.body.size())) {
|
||||||
ret = false;
|
ret = false;
|
||||||
|
}
|
||||||
|
} else if (res.content_provider_) {
|
||||||
|
if (write_content_with_provider(strm, req, res, boundary,
|
||||||
|
content_type)) {
|
||||||
|
res.content_provider_success_ = true;
|
||||||
|
} else {
|
||||||
|
ret = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user