mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 01:08:27 +00:00
Refactoring.
This commit is contained in:
parent
f91cc98b89
commit
aa75fbb5f9
7 changed files with 100 additions and 79 deletions
|
@ -7,14 +7,23 @@
|
|||
|
||||
#include <httplib.h>
|
||||
#include <cstdio>
|
||||
#include <signal.h>
|
||||
|
||||
template<typename Fn> void signal(int sig, Fn fn)
|
||||
#ifdef _WIN32
|
||||
#define snprintf sprintf_s
|
||||
#endif
|
||||
|
||||
std::string dump_headers(const httplib::MultiMap& headers)
|
||||
{
|
||||
static std::function<void ()> signal_handler_;
|
||||
struct SignalHandler { static void fn(int sig) { signal_handler_(); } };
|
||||
signal_handler_ = fn;
|
||||
signal(sig, SignalHandler::fn);
|
||||
std::string s;
|
||||
char buf[BUFSIZ];
|
||||
|
||||
for (auto it = headers.begin(); it != headers.end(); ++it) {
|
||||
const auto& x = *it;
|
||||
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
|
||||
s += buf;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string log(const httplib::Connection& c)
|
||||
|
@ -40,13 +49,13 @@ std::string log(const httplib::Connection& c)
|
|||
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
|
||||
s += buf;
|
||||
|
||||
s += httplib::dump_headers(req.headers);
|
||||
s += dump_headers(req.headers);
|
||||
|
||||
s += "--------------------------------\n";
|
||||
|
||||
snprintf(buf, sizeof(buf), "%d\n", res.status);
|
||||
s += buf;
|
||||
s += httplib::dump_headers(res.headers);
|
||||
s += dump_headers(res.headers);
|
||||
|
||||
if (!res.body.empty()) {
|
||||
s += res.body;
|
||||
|
@ -57,13 +66,6 @@ std::string log(const httplib::Connection& c)
|
|||
return s;
|
||||
}
|
||||
|
||||
inline void error_handler(httplib::Connection& c)
|
||||
{
|
||||
char buf[BUFSIZ];
|
||||
snprintf(buf, sizeof(buf), "Error Status: %d\r\n", c.response.status);
|
||||
c.response.set_content(buf);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
using namespace httplib;
|
||||
|
@ -77,21 +79,24 @@ int main(void)
|
|||
});
|
||||
|
||||
svr.get("/hi", [](Connection& c) {
|
||||
c.response.set_content("Hello World!");
|
||||
c.response.set_content("Hello World!", "text/plain");
|
||||
});
|
||||
|
||||
svr.get("/dump", [](Connection& c) {
|
||||
c.response.set_content(httplib::dump_headers(c.request.headers));
|
||||
c.response.set_content(dump_headers(c.request.headers), "text/plain");
|
||||
});
|
||||
|
||||
svr.error(error_handler);
|
||||
svr.set_error_handler([](httplib::Connection& c) {
|
||||
char buf[BUFSIZ];
|
||||
snprintf(buf, sizeof(buf), "<p>Error Status: <span style='color:red;'>%d</span></p>", c.response.status);
|
||||
c.response.body = buf;
|
||||
c.response.set_header("Content-Type", "text/html");
|
||||
});
|
||||
|
||||
svr.set_logger([](const Connection& c) {
|
||||
printf("%s", log(c).c_str());
|
||||
});
|
||||
|
||||
signal(SIGINT, [&]() { svr.stop(); });
|
||||
|
||||
svr.run();
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue