mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-10 09:43:51 +00:00
parent
8311e1105f
commit
986a20fb7d
15
README.md
15
README.md
@ -125,6 +125,21 @@ int main(void)
|
|||||||
res.set_content(req.body, "text/plain");
|
res.set_content(req.body, "text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If the handler takes time to finish, you can also poll the connection state
|
||||||
|
svr.Get("/task", [&](const Request& req, Response& res) {
|
||||||
|
const char * result = nullptr;
|
||||||
|
process.run(); // for example, starting an external process
|
||||||
|
while (result == nullptr) {
|
||||||
|
sleep(1);
|
||||||
|
if (req.is_connection_closed()) {
|
||||||
|
process.kill(); // kill the process
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result = process.stdout(); // != nullptr if the process finishes
|
||||||
|
}
|
||||||
|
res.set_content(result, "text/plain");
|
||||||
|
});
|
||||||
|
|
||||||
svr.Get("/stop", [&](const Request& req, Response& res) {
|
svr.Get("/stop", [&](const Request& req, Response& res) {
|
||||||
svr.stop();
|
svr.stop();
|
||||||
});
|
});
|
||||||
|
@ -628,6 +628,7 @@ struct Request {
|
|||||||
Ranges ranges;
|
Ranges ranges;
|
||||||
Match matches;
|
Match matches;
|
||||||
std::unordered_map<std::string, std::string> path_params;
|
std::unordered_map<std::string, std::string> path_params;
|
||||||
|
std::function<bool()> is_connection_closed = []() { return true; };
|
||||||
|
|
||||||
// for client
|
// for client
|
||||||
ResponseHandler response_handler;
|
ResponseHandler response_handler;
|
||||||
@ -2572,7 +2573,7 @@ inline bool is_field_content(const std::string &s) {
|
|||||||
|
|
||||||
inline bool is_field_value(const std::string &s) { return is_field_content(s); }
|
inline bool is_field_value(const std::string &s) { return is_field_content(s); }
|
||||||
|
|
||||||
}; // namespace fields
|
} // namespace fields
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
@ -7217,6 +7218,11 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup `is_connection_closed` method
|
||||||
|
req.is_connection_closed = [&]() {
|
||||||
|
return !detail::is_socket_alive(strm.socket());
|
||||||
|
};
|
||||||
|
|
||||||
// Routing
|
// Routing
|
||||||
auto routed = false;
|
auto routed = false;
|
||||||
#ifdef CPPHTTPLIB_NO_EXCEPTIONS
|
#ifdef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
|
Loading…
x
Reference in New Issue
Block a user