cpp-httplib/example/simplesvr.cc

133 lines
3 KiB
C++
Raw Normal View History

2013-07-04 19:08:49 -07:00
//
// simplesvr.cc
//
2019-06-27 18:48:57 -07:00
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
2013-07-04 19:08:49 -07:00
//
#include <cstdio>
2019-04-11 05:13:31 -07:00
#include <httplib.h>
2013-07-04 19:08:49 -07:00
#include <iostream>
2017-04-21 20:00:00 -07:00
#define SERVER_CERT_FILE "./cert.pem"
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
2013-07-04 19:08:49 -07:00
using namespace httplib;
using namespace std;
2019-04-11 05:13:31 -07:00
string dump_headers(const Headers &headers) {
string s;
char buf[BUFSIZ];
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
for (const auto &x : headers) {
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
s += buf;
}
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
return s;
2013-07-04 19:08:49 -07:00
}
2019-04-11 05:13:31 -07:00
string dump_multipart_files(const MultipartFiles &files) {
string s;
char buf[BUFSIZ];
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
s += "--------------------------------\n";
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
for (const auto &x : files) {
const auto &name = x.first;
const auto &file = x.second;
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
s += buf;
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
snprintf(buf, sizeof(buf), "filename: %s\n", file.filename.c_str());
s += buf;
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str());
s += buf;
2017-12-05 17:19:07 -07:00
2019-12-03 08:30:07 -07:00
snprintf(buf, sizeof(buf), "text length: %lu\n", file.content.size());
2019-04-11 05:13:31 -07:00
s += buf;
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
s += "----------------\n";
}
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
return s;
2017-12-05 17:19:07 -07:00
}
2019-04-11 05:13:31 -07:00
string log(const Request &req, const Response &res) {
string s;
char buf[BUFSIZ];
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
s += "================================\n";
2013-07-04 19:08:49 -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;
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
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;
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
s += dump_headers(req.headers);
s += dump_multipart_files(req.files);
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
s += "--------------------------------\n";
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
snprintf(buf, sizeof(buf), "%d\n", res.status);
s += buf;
s += dump_headers(res.headers);
2017-04-21 20:00:00 -07:00
2019-04-11 05:13:31 -07:00
return s;
2013-07-04 19:08:49 -07:00
}
2019-04-11 05:13:31 -07:00
int main(int argc, const char **argv) {
if (argc > 1 && string("--help") == argv[1]) {
cout << "usage: simplesvr [PORT] [DIR]" << endl;
return 1;
}
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
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
svr.Post("/multipart", [](const Request &req, Response &res) {
auto body = dump_headers(req.headers) + dump_multipart_files(req.files);
2017-12-05 17:19:07 -07:00
2019-04-11 05:13:31 -07:00
res.set_content(body, "text/plain");
});
2017-12-05 17:19:07 -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");
});
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
svr.set_logger(
[](const Request &req, const Response &res) { cout << log(req, res); });
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
auto port = 8080;
if (argc > 1) { port = atoi(argv[1]); }
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
auto base_dir = "./";
if (argc > 2) { base_dir = argv[2]; }
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
svr.set_base_dir(base_dir);
2019-04-11 05:13:31 -07:00
cout << "The server started at port " << port << "...";
2019-04-11 05:13:31 -07:00
svr.listen("localhost", port);
2013-07-04 19:08:49 -07:00
2019-04-11 05:13:31 -07:00
return 0;
2013-07-04 19:08:49 -07:00
}