mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-12 06:01:40 +00:00
Apply clangformat
This commit is contained in:
parent
a91a0b7dbf
commit
5d082f1da4
@ -10,8 +10,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(void)
|
int main(void) {
|
||||||
{
|
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
httplib::SSLClient cli("localhost", 8080);
|
httplib::SSLClient cli("localhost", 8080);
|
||||||
#else
|
#else
|
||||||
|
@ -8,11 +8,10 @@
|
|||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
using namespace httplib;
|
using namespace httplib;
|
||||||
|
|
||||||
int main(void)
|
int main(void) {
|
||||||
{
|
|
||||||
Server svr;
|
Server svr;
|
||||||
|
|
||||||
svr.Get("/hi", [](const Request& /*req*/, Response& res) {
|
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
|
||||||
res.set_content("Hello World!", "text/plain");
|
res.set_content("Hello World!", "text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -5,22 +5,21 @@
|
|||||||
// The Boost Software License 1.0
|
// The Boost Software License 1.0
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <httplib.h>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <httplib.h>
|
||||||
|
|
||||||
#define SERVER_CERT_FILE "./cert.pem"
|
#define SERVER_CERT_FILE "./cert.pem"
|
||||||
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
|
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
|
||||||
|
|
||||||
using namespace httplib;
|
using namespace httplib;
|
||||||
|
|
||||||
std::string dump_headers(const Headers& headers)
|
std::string dump_headers(const Headers &headers) {
|
||||||
{
|
|
||||||
std::string s;
|
std::string s;
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
for (auto it = headers.begin(); it != headers.end(); ++it) {
|
for (auto it = headers.begin(); it != headers.end(); ++it) {
|
||||||
const auto& x = *it;
|
const auto &x = *it;
|
||||||
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
|
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
|
||||||
s += buf;
|
s += buf;
|
||||||
}
|
}
|
||||||
@ -28,21 +27,22 @@ std::string dump_headers(const Headers& headers)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string log(const Request& req, const Response& res)
|
std::string log(const Request &req, const Response &res) {
|
||||||
{
|
|
||||||
std::string s;
|
std::string s;
|
||||||
char buf[BUFSIZ];
|
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());
|
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
|
||||||
|
req.version.c_str(), req.path.c_str());
|
||||||
s += buf;
|
s += buf;
|
||||||
|
|
||||||
std::string query;
|
std::string query;
|
||||||
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
|
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
|
||||||
const auto& x = *it;
|
const auto &x = *it;
|
||||||
snprintf(buf, sizeof(buf), "%c%s=%s",
|
snprintf(buf, sizeof(buf), "%c%s=%s",
|
||||||
(it == req.params.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str());
|
(it == req.params.begin()) ? '?' : '&', x.first.c_str(),
|
||||||
|
x.second.c_str());
|
||||||
query += buf;
|
query += buf;
|
||||||
}
|
}
|
||||||
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
|
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
|
||||||
@ -57,17 +57,14 @@ std::string log(const Request& req, const Response& res)
|
|||||||
s += dump_headers(res.headers);
|
s += dump_headers(res.headers);
|
||||||
s += "\n";
|
s += "\n";
|
||||||
|
|
||||||
if (!res.body.empty()) {
|
if (!res.body.empty()) { s += res.body; }
|
||||||
s += res.body;
|
|
||||||
}
|
|
||||||
|
|
||||||
s += "\n";
|
s += "\n";
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void) {
|
||||||
{
|
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
||||||
#else
|
#else
|
||||||
@ -79,35 +76,34 @@ int main(void)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
svr.Get("/", [=](const Request& /*req*/, Response& res) {
|
svr.Get("/", [=](const Request & /*req*/, Response &res) {
|
||||||
res.set_redirect("/hi");
|
res.set_redirect("/hi");
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.Get("/hi", [](const Request& /*req*/, Response& res) {
|
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
|
||||||
res.set_content("Hello World!\n", "text/plain");
|
res.set_content("Hello World!\n", "text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.Get("/slow", [](const Request& /*req*/, Response& res) {
|
svr.Get("/slow", [](const Request & /*req*/, Response &res) {
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||||
res.set_content("Slow...\n", "text/plain");
|
res.set_content("Slow...\n", "text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.Get("/dump", [](const Request& req, Response& res) {
|
svr.Get("/dump", [](const Request &req, Response &res) {
|
||||||
res.set_content(dump_headers(req.headers), "text/plain");
|
res.set_content(dump_headers(req.headers), "text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.Get("/stop", [&](const Request& /*req*/, Response& /*res*/) {
|
svr.Get("/stop",
|
||||||
svr.stop();
|
[&](const Request & /*req*/, Response & /*res*/) { svr.stop(); });
|
||||||
});
|
|
||||||
|
|
||||||
svr.set_error_handler([](const Request& /*req*/, Response& res) {
|
svr.set_error_handler([](const Request & /*req*/, Response &res) {
|
||||||
const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
|
const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
snprintf(buf, sizeof(buf), fmt, res.status);
|
snprintf(buf, sizeof(buf), fmt, res.status);
|
||||||
res.set_content(buf, "text/html");
|
res.set_content(buf, "text/html");
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.set_logger([](const Request& req, const Response& res) {
|
svr.set_logger([](const Request &req, const Response &res) {
|
||||||
printf("%s", log(req, res).c_str());
|
printf("%s", log(req, res).c_str());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
// The Boost Software License 1.0
|
// The Boost Software License 1.0
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <httplib.h>
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <httplib.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define SERVER_CERT_FILE "./cert.pem"
|
#define SERVER_CERT_FILE "./cert.pem"
|
||||||
@ -15,12 +15,11 @@
|
|||||||
using namespace httplib;
|
using namespace httplib;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
string dump_headers(const Headers& headers)
|
string dump_headers(const Headers &headers) {
|
||||||
{
|
|
||||||
string s;
|
string s;
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
for (const auto& x: headers) {
|
for (const auto &x : headers) {
|
||||||
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
|
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
|
||||||
s += buf;
|
s += buf;
|
||||||
}
|
}
|
||||||
@ -28,16 +27,15 @@ string dump_headers(const Headers& headers)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
string dump_multipart_files(const MultipartFiles& files)
|
string dump_multipart_files(const MultipartFiles &files) {
|
||||||
{
|
|
||||||
string s;
|
string s;
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
s += "--------------------------------\n";
|
s += "--------------------------------\n";
|
||||||
|
|
||||||
for (const auto& x: files) {
|
for (const auto &x : files) {
|
||||||
const auto& name = x.first;
|
const auto &name = x.first;
|
||||||
const auto& file = x.second;
|
const auto &file = x.second;
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
|
snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
|
||||||
s += buf;
|
s += buf;
|
||||||
@ -60,21 +58,22 @@ string dump_multipart_files(const MultipartFiles& files)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
string log(const Request& req, const Response& res)
|
string log(const Request &req, const Response &res) {
|
||||||
{
|
|
||||||
string s;
|
string s;
|
||||||
char buf[BUFSIZ];
|
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());
|
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
|
||||||
|
req.version.c_str(), req.path.c_str());
|
||||||
s += buf;
|
s += buf;
|
||||||
|
|
||||||
string query;
|
string query;
|
||||||
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
|
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
|
||||||
const auto& x = *it;
|
const auto &x = *it;
|
||||||
snprintf(buf, sizeof(buf), "%c%s=%s",
|
snprintf(buf, sizeof(buf), "%c%s=%s",
|
||||||
(it == req.params.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str());
|
(it == req.params.begin()) ? '?' : '&', x.first.c_str(),
|
||||||
|
x.second.c_str());
|
||||||
query += buf;
|
query += buf;
|
||||||
}
|
}
|
||||||
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
|
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
|
||||||
@ -92,8 +91,7 @@ string log(const Request& req, const Response& res)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char** argv)
|
int main(int argc, const char **argv) {
|
||||||
{
|
|
||||||
if (argc > 1 && string("--help") == argv[1]) {
|
if (argc > 1 && string("--help") == argv[1]) {
|
||||||
cout << "usage: simplesvr [PORT] [DIR]" << endl;
|
cout << "usage: simplesvr [PORT] [DIR]" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
@ -105,34 +103,27 @@ int main(int argc, const char** argv)
|
|||||||
Server svr;
|
Server svr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
svr.Post("/multipart", [](const Request& req, Response& res) {
|
svr.Post("/multipart", [](const Request &req, Response &res) {
|
||||||
auto body =
|
auto body = dump_headers(req.headers) + dump_multipart_files(req.files);
|
||||||
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) {
|
svr.set_error_handler([](const Request & /*req*/, Response &res) {
|
||||||
const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
|
const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
snprintf(buf, sizeof(buf), fmt, res.status);
|
snprintf(buf, sizeof(buf), fmt, res.status);
|
||||||
res.set_content(buf, "text/html");
|
res.set_content(buf, "text/html");
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.set_logger([](const Request& req, const Response& res) {
|
svr.set_logger(
|
||||||
cout << log(req, res);
|
[](const Request &req, const Response &res) { cout << log(req, res); });
|
||||||
});
|
|
||||||
|
|
||||||
auto port = 8080;
|
auto port = 8080;
|
||||||
if (argc > 1) {
|
if (argc > 1) { port = atoi(argv[1]); }
|
||||||
port = atoi(argv[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto base_dir = "./";
|
auto base_dir = "./";
|
||||||
if (argc > 2) {
|
if (argc > 2) { base_dir = argv[2]; }
|
||||||
base_dir = argv[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
svr.set_base_dir(base_dir);
|
svr.set_base_dir(base_dir);
|
||||||
|
|
||||||
|
592
test/test.cc
592
test/test.cc
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user