simplesvr.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // simplesvr.cc
  3. //
  4. // Copyright (c) 2019 Yuji Hirose. All rights reserved.
  5. // MIT License
  6. //
  7. #include <cstdio>
  8. #include <httplib.h>
  9. #include <iostream>
  10. #define SERVER_CERT_FILE "./cert.pem"
  11. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  12. using namespace httplib;
  13. using namespace std;
  14. string dump_headers(const Headers &headers) {
  15. string s;
  16. char buf[BUFSIZ];
  17. for (const auto &x : headers) {
  18. snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
  19. s += buf;
  20. }
  21. return s;
  22. }
  23. string dump_multipart_files(const MultipartFormDataMap &files) {
  24. string s;
  25. char buf[BUFSIZ];
  26. s += "--------------------------------\n";
  27. for (const auto &x : files) {
  28. const auto &name = x.first;
  29. const auto &file = x.second;
  30. snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
  31. s += buf;
  32. snprintf(buf, sizeof(buf), "filename: %s\n", file.filename.c_str());
  33. s += buf;
  34. snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str());
  35. s += buf;
  36. snprintf(buf, sizeof(buf), "text length: %lu\n", file.content.size());
  37. s += buf;
  38. s += "----------------\n";
  39. }
  40. return s;
  41. }
  42. string log(const Request &req, const Response &res) {
  43. string s;
  44. char buf[BUFSIZ];
  45. s += "================================\n";
  46. snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
  47. req.version.c_str(), req.path.c_str());
  48. s += buf;
  49. string query;
  50. for (auto it = req.params.begin(); it != req.params.end(); ++it) {
  51. const auto &x = *it;
  52. snprintf(buf, sizeof(buf), "%c%s=%s",
  53. (it == req.params.begin()) ? '?' : '&', x.first.c_str(),
  54. x.second.c_str());
  55. query += buf;
  56. }
  57. snprintf(buf, sizeof(buf), "%s\n", query.c_str());
  58. s += buf;
  59. s += dump_headers(req.headers);
  60. s += dump_multipart_files(req.files);
  61. s += "--------------------------------\n";
  62. snprintf(buf, sizeof(buf), "%d\n", res.status);
  63. s += buf;
  64. s += dump_headers(res.headers);
  65. return s;
  66. }
  67. int main(int argc, const char **argv) {
  68. if (argc > 1 && string("--help") == argv[1]) {
  69. cout << "usage: simplesvr [PORT] [DIR]" << endl;
  70. return 1;
  71. }
  72. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  73. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
  74. #else
  75. Server svr;
  76. #endif
  77. svr.Post("/multipart", [](const Request &req, Response &res) {
  78. auto body = dump_headers(req.headers) + dump_multipart_files(req.files);
  79. res.set_content(body, "text/plain");
  80. });
  81. svr.set_error_handler([](const Request & /*req*/, Response &res) {
  82. const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
  83. char buf[BUFSIZ];
  84. snprintf(buf, sizeof(buf), fmt, res.status);
  85. res.set_content(buf, "text/html");
  86. });
  87. svr.set_logger(
  88. [](const Request &req, const Response &res) { cout << log(req, res); });
  89. auto port = 8080;
  90. if (argc > 1) { port = atoi(argv[1]); }
  91. auto base_dir = "./";
  92. if (argc > 2) { base_dir = argv[2]; }
  93. if (!svr.set_mount_point("/", base_dir)) {
  94. cout << "The specified base directory doesn't exist...";
  95. return 1;
  96. }
  97. cout << "The server started at port " << port << "..." << endl;
  98. svr.listen("localhost", port);
  99. return 0;
  100. }