cpp-httplib/example/server.cc

114 lines
2.7 KiB
C++
Raw Normal View History

2012-10-02 17:39:13 -07:00
//
// sample.cc
//
2019-06-27 18:48:57 -07:00
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
2012-10-02 17:39:13 -07:00
//
2017-12-16 17:07:58 -07:00
#include <chrono>
2019-04-11 05:13:31 -07:00
#include <cstdio>
#include <httplib.h>
2012-10-02 17:39:13 -07:00
2017-04-21 20:00:00 -07:00
#define SERVER_CERT_FILE "./cert.pem"
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
2012-10-12 13:09:39 -07:00
using namespace httplib;
2012-10-03 17:11:22 -07:00
2019-04-11 05:13:31 -07:00
std::string dump_headers(const Headers &headers) {
std::string s;
char buf[BUFSIZ];
2012-10-03 17:11:22 -07:00
2019-04-11 05:13:31 -07:00
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;
}
2012-10-03 17:11:22 -07:00
2019-04-11 05:13:31 -07:00
return s;
2012-10-02 17:39:13 -07:00
}
2019-04-11 05:13:31 -07:00
std::string log(const Request &req, const Response &res) {
std::string s;
char buf[BUFSIZ];
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
s += "================================\n";
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
req.version.c_str(), req.path.c_str());
s += buf;
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
std::string query;
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%c%s=%s",
(it == req.params.begin()) ? '?' : '&', x.first.c_str(),
x.second.c_str());
query += buf;
}
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
s += buf;
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
s += dump_headers(req.headers);
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
s += "--------------------------------\n";
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
s += buf;
s += dump_headers(res.headers);
s += "\n";
2017-04-21 20:00:00 -07:00
2019-04-11 05:13:31 -07:00
if (!res.body.empty()) { s += res.body; }
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
s += "\n";
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
return s;
2012-10-02 17:39:13 -07:00
}
2019-04-11 05:13:31 -07:00
int main(void) {
2017-04-21 20:00:00 -07:00
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2019-04-11 05:13:31 -07:00
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
2017-04-21 20:00:00 -07:00
#else
2019-04-11 05:13:31 -07:00
Server svr;
2017-04-21 20:00:00 -07:00
#endif
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
if (!svr.is_valid()) {
printf("server has an error...\n");
return -1;
}
2019-04-11 05:13:31 -07:00
svr.Get("/", [=](const Request & /*req*/, Response &res) {
res.set_redirect("/hi");
});
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!\n", "text/plain");
});
2017-12-16 17:07:58 -07:00
2019-04-11 05:13:31 -07:00
svr.Get("/slow", [](const Request & /*req*/, Response &res) {
std::this_thread::sleep_for(std::chrono::seconds(2));
res.set_content("Slow...\n", "text/plain");
});
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
svr.Get("/dump", [](const Request &req, Response &res) {
res.set_content(dump_headers(req.headers), "text/plain");
});
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
svr.Get("/stop",
[&](const Request & /*req*/, Response & /*res*/) { svr.stop(); });
2012-10-11 20:52:34 -07:00
2019-04-11 05:13:31 -07:00
svr.set_error_handler([](const Request & /*req*/, Response &res) {
const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), fmt, res.status);
res.set_content(buf, "text/html");
});
2012-10-02 19:37:14 -07:00
2019-04-11 05:13:31 -07:00
svr.set_logger([](const Request &req, const Response &res) {
printf("%s", log(req, res).c_str());
});
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
svr.listen("localhost", 8080);
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
return 0;
2012-10-02 17:39:13 -07:00
}