-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
59 lines (47 loc) · 1.65 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "dependencies/httplib.h"
#include "logger.h"
#include <chrono>
using namespace std;
using namespace httplib;
static string timePointToString(const chrono::system_clock::time_point& tp) {
time_t time = chrono::system_clock::to_time_t(tp);
tm* tm = std::localtime(&time);
ostringstream oss;
oss << put_time(tm, "%Y-%m-%d %H:%M:%S");
return oss.str();
}
static string stringify_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;
}
return s;
}
static string log(const Request& req, const Response& res) {
string s;
string query;
char buf[BUFSIZ];
s += "\n===========================================================================================\n";
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
req.version.c_str(), req.path.c_str());
s += buf;
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 += stringify_headers(req.headers);
//s += dump_multipart_files(req.files); method removed / disabled
snprintf(buf, sizeof(buf), "%d\n", res.status);
s += buf;
s += stringify_headers(res.headers);
s += "\n===========================================================================================\n";
logger::ltf(s);
return s;
}