This commit is contained in:
yhirose 2017-12-05 19:19:07 -05:00
parent ea9c8ee46b
commit bb8a1df7a3
4 changed files with 238 additions and 12 deletions

View file

@ -28,6 +28,38 @@ string dump_headers(const MultiMap& headers)
return s;
}
string dump_multipart_files(const MultipartFiles& files)
{
string s;
char buf[BUFSIZ];
s += "--------------------------------\n";
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), "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), "text offset: %lu\n", file.offset);
s += buf;
snprintf(buf, sizeof(buf), "text length: %lu\n", file.length);
s += buf;
s += "----------------\n";
}
return s;
}
string log(const Request& req, const Response& res)
{
string s;
@ -49,6 +81,7 @@ string log(const Request& req, const Response& res)
s += buf;
s += dump_headers(req.headers);
s += dump_multipart_files(req.files);
s += "--------------------------------\n";
@ -72,7 +105,15 @@ int main(int argc, const char** argv)
Server svr;
#endif
svr.set_error_handler([](const auto& req, auto& res) {
svr.post("/multipart", [](const auto& req, auto& res) {
auto body =
dump_headers(req.headers) +
dump_multipart_files(req.files);
res.set_content(body, "text/plain");
});
svr.set_error_handler([](const auto& /*req*/, auto& res) {
const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), fmt, res.status);
@ -83,7 +124,7 @@ int main(int argc, const char** argv)
cout << log(req, res);
});
auto port = 80;
auto port = 8080;
if (argc > 1) {
port = atoi(argv[1]);
}