mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 06:26:02 -07:00
Update server_and_client.cc
This commit is contained in:
parent
c43c51362a
commit
c673d502b9
1 changed files with 62 additions and 17 deletions
|
@ -5,41 +5,86 @@
|
||||||
// MIT License
|
// MIT License
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <httplib.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string>
|
|
||||||
#include <httplib.h>
|
|
||||||
|
|
||||||
using namespace httplib;
|
using namespace httplib;
|
||||||
|
|
||||||
const char *HOST = "localhost";
|
std::string dump_headers(const Headers &headers) {
|
||||||
const int PORT = 1234;
|
std::string s;
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
const std::string JSON_DATA = R"({"hello": "world"})";
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void logger(const Request &req, const Response &res) {
|
||||||
|
std::string s;
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
|
s += "================================\n";
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
s += dump_headers(req.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; }
|
||||||
|
|
||||||
|
s += "\n";
|
||||||
|
|
||||||
|
std::cout << s;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
// Server
|
||||||
Server svr;
|
Server svr;
|
||||||
|
svr.set_logger(logger);
|
||||||
|
|
||||||
svr.Post("/api", [&](const Request & /*req*/, Response &res) {
|
svr.Post("/post", [&](const Request & /*req*/, Response &res) {
|
||||||
res.set_content("Hello World!", "text/plain");
|
res.set_content("POST", "text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
auto th = std::thread([&]() { svr.listen("localhost", 8080); });
|
||||||
|
|
||||||
auto se = detail::scope_exit([&] {
|
auto se = detail::scope_exit([&] {
|
||||||
svr.stop();
|
svr.stop();
|
||||||
thread.join();
|
th.join();
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.wait_until_ready();
|
svr.wait_until_ready();
|
||||||
|
|
||||||
Client cli(HOST, PORT);
|
// Client
|
||||||
|
Client cli{"localhost", 8080};
|
||||||
|
|
||||||
auto res =
|
std::string body = R"({"hello": "world"})";
|
||||||
cli.Post("/api", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
||||||
"application/json", [](uint64_t, uint64_t) { return true; });
|
|
||||||
|
|
||||||
if (res) {
|
auto res = cli.Post("/post", body, "application/json");
|
||||||
std::cout << res->body << std::endl;
|
std::cout << "--------------------------------" << std::endl;
|
||||||
}
|
std::cout << to_string(res.error()) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue