Apply clangformat

This commit is contained in:
Yuji Hirose 2019-04-11 08:13:31 -04:00
parent a91a0b7dbf
commit 5d082f1da4
6 changed files with 2833 additions and 2970 deletions

View file

@ -10,22 +10,21 @@
using namespace std;
int main(void)
{
int main(void) {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
httplib::SSLClient cli("localhost", 8080);
httplib::SSLClient cli("localhost", 8080);
#else
httplib::Client cli("localhost", 8080);
httplib::Client cli("localhost", 8080);
#endif
auto res = cli.Get("/hi");
if (res) {
cout << res->status << endl;
cout << res->get_header_value("Content-Type") << endl;
cout << res->body << endl;
}
auto res = cli.Get("/hi");
if (res) {
cout << res->status << endl;
cout << res->get_header_value("Content-Type") << endl;
cout << res->body << endl;
}
return 0;
return 0;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix

View file

@ -8,15 +8,14 @@
#include <httplib.h>
using namespace httplib;
int main(void)
{
Server svr;
int main(void) {
Server svr;
svr.Get("/hi", [](const Request& /*req*/, Response& res) {
res.set_content("Hello World!", "text/plain");
});
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!", "text/plain");
});
svr.listen("localhost", 1234);
svr.listen("localhost", 1234);
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix

View file

@ -5,115 +5,111 @@
// The Boost Software License 1.0
//
#include <httplib.h>
#include <cstdio>
#include <chrono>
#include <cstdio>
#include <httplib.h>
#define SERVER_CERT_FILE "./cert.pem"
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
using namespace httplib;
std::string dump_headers(const Headers& headers)
{
std::string s;
char buf[BUFSIZ];
std::string dump_headers(const Headers &headers) {
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;
}
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;
return s;
}
std::string log(const Request& req, const Response& res)
{
std::string s;
char buf[BUFSIZ];
std::string log(const Request &req, const Response &res) {
std::string s;
char buf[BUFSIZ];
s += "================================\n";
s += "================================\n";
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.version.c_str(), req.path.c_str());
s += buf;
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
req.version.c_str(), req.path.c_str());
s += buf;
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;
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;
s += dump_headers(req.headers);
s += dump_headers(req.headers);
s += "--------------------------------\n";
s += "--------------------------------\n";
snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
s += buf;
s += dump_headers(res.headers);
s += "\n";
snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
s += buf;
s += dump_headers(res.headers);
s += "\n";
if (!res.body.empty()) {
s += res.body;
}
if (!res.body.empty()) { s += res.body; }
s += "\n";
s += "\n";
return s;
return s;
}
int main(void)
{
int main(void) {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
#else
Server svr;
Server svr;
#endif
if (!svr.is_valid()) {
printf("server has an error...\n");
return -1;
}
if (!svr.is_valid()) {
printf("server has an error...\n");
return -1;
}
svr.Get("/", [=](const Request& /*req*/, Response& res) {
res.set_redirect("/hi");
});
svr.Get("/", [=](const Request & /*req*/, Response &res) {
res.set_redirect("/hi");
});
svr.Get("/hi", [](const Request& /*req*/, Response& res) {
res.set_content("Hello World!\n", "text/plain");
});
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!\n", "text/plain");
});
svr.Get("/slow", [](const Request& /*req*/, Response& res) {
std::this_thread::sleep_for(std::chrono::seconds(2));
res.set_content("Slow...\n", "text/plain");
});
svr.Get("/slow", [](const Request & /*req*/, Response &res) {
std::this_thread::sleep_for(std::chrono::seconds(2));
res.set_content("Slow...\n", "text/plain");
});
svr.Get("/dump", [](const Request& req, Response& res) {
res.set_content(dump_headers(req.headers), "text/plain");
});
svr.Get("/dump", [](const Request &req, Response &res) {
res.set_content(dump_headers(req.headers), "text/plain");
});
svr.Get("/stop", [&](const Request& /*req*/, Response& /*res*/) {
svr.stop();
});
svr.Get("/stop",
[&](const Request & /*req*/, Response & /*res*/) { svr.stop(); });
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");
});
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");
});
svr.set_logger([](const Request& req, const Response& res) {
printf("%s", log(req, res).c_str());
});
svr.set_logger([](const Request &req, const Response &res) {
printf("%s", log(req, res).c_str());
});
svr.listen("localhost", 8080);
svr.listen("localhost", 8080);
return 0;
return 0;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix

View file

@ -5,8 +5,8 @@
// The Boost Software License 1.0
//
#include <httplib.h>
#include <cstdio>
#include <httplib.h>
#include <iostream>
#define SERVER_CERT_FILE "./cert.pem"
@ -15,132 +15,123 @@
using namespace httplib;
using namespace std;
string dump_headers(const Headers& headers)
{
string s;
char buf[BUFSIZ];
string dump_headers(const Headers &headers) {
string s;
char buf[BUFSIZ];
for (const auto& x: headers) {
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
s += buf;
}
for (const auto &x : headers) {
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
s += buf;
}
return s;
return s;
}
string dump_multipart_files(const MultipartFiles& files)
{
string s;
char buf[BUFSIZ];
string dump_multipart_files(const MultipartFiles &files) {
string s;
char buf[BUFSIZ];
s += "--------------------------------\n";
s += "--------------------------------\n";
for (const auto& x: files) {
const auto& name = x.first;
const auto& file = x.second;
for (const auto &x : files) {
const auto &name = x.first;
const auto &file = x.second;
snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
s += buf;
snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
s += buf;
snprintf(buf, sizeof(buf), "filename: %s\n", file.filename.c_str());
s += buf;
snprintf(buf, sizeof(buf), "filename: %s\n", file.filename.c_str());
s += buf;
snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str());
s += buf;
snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str());
s += buf;
snprintf(buf, sizeof(buf), "text offset: %lu\n", file.offset);
s += buf;
snprintf(buf, sizeof(buf), "text offset: %lu\n", file.offset);
s += buf;
snprintf(buf, sizeof(buf), "text length: %lu\n", file.length);
s += buf;
snprintf(buf, sizeof(buf), "text length: %lu\n", file.length);
s += buf;
s += "----------------\n";
}
s += "----------------\n";
}
return s;
return s;
}
string log(const Request& req, const Response& res)
{
string s;
char buf[BUFSIZ];
string log(const Request &req, const Response &res) {
string s;
char buf[BUFSIZ];
s += "================================\n";
s += "================================\n";
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.version.c_str(), req.path.c_str());
s += buf;
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
req.version.c_str(), req.path.c_str());
s += buf;
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;
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;
s += dump_headers(req.headers);
s += dump_multipart_files(req.files);
s += dump_headers(req.headers);
s += dump_multipart_files(req.files);
s += "--------------------------------\n";
s += "--------------------------------\n";
snprintf(buf, sizeof(buf), "%d\n", res.status);
s += buf;
s += dump_headers(res.headers);
snprintf(buf, sizeof(buf), "%d\n", res.status);
s += buf;
s += dump_headers(res.headers);
return s;
return s;
}
int main(int argc, const char** argv)
{
if (argc > 1 && string("--help") == argv[1]) {
cout << "usage: simplesvr [PORT] [DIR]" << endl;
return 1;
}
int main(int argc, const char **argv) {
if (argc > 1 && string("--help") == argv[1]) {
cout << "usage: simplesvr [PORT] [DIR]" << endl;
return 1;
}
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
#else
Server svr;
Server svr;
#endif
svr.Post("/multipart", [](const Request& req, Response& res) {
auto body =
dump_headers(req.headers) +
dump_multipart_files(req.files);
svr.Post("/multipart", [](const Request &req, Response &res) {
auto body = dump_headers(req.headers) + dump_multipart_files(req.files);
res.set_content(body, "text/plain");
});
res.set_content(body, "text/plain");
});
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");
});
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");
});
svr.set_logger([](const Request& req, const Response& res) {
cout << log(req, res);
});
svr.set_logger(
[](const Request &req, const Response &res) { cout << log(req, res); });
auto port = 8080;
if (argc > 1) {
port = atoi(argv[1]);
}
auto port = 8080;
if (argc > 1) { port = atoi(argv[1]); }
auto base_dir = "./";
if (argc > 2) {
base_dir = argv[2];
}
auto base_dir = "./";
if (argc > 2) { base_dir = argv[2]; }
svr.set_base_dir(base_dir);
svr.set_base_dir(base_dir);
cout << "The server started at port " << port << "...";
cout << "The server started at port " << port << "...";
svr.listen("localhost", port);
svr.listen("localhost", port);
return 0;
return 0;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix

3636
httplib.h

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff