simplesvr.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_formdata(const MultipartFormData &form) {
  24. string s;
  25. char buf[BUFSIZ];
  26. s += "--------------------------------\n";
  27. for (const auto &x : form.fields) {
  28. const auto &name = x.first;
  29. const auto &field = x.second;
  30. snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
  31. s += buf;
  32. snprintf(buf, sizeof(buf), "text length: %zu\n", field.content.size());
  33. s += buf;
  34. s += "----------------\n";
  35. }
  36. for (const auto &x : form.files) {
  37. const auto &name = x.first;
  38. const auto &file = x.second;
  39. snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
  40. s += buf;
  41. snprintf(buf, sizeof(buf), "filename: %s\n", file.filename.c_str());
  42. s += buf;
  43. snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str());
  44. s += buf;
  45. snprintf(buf, sizeof(buf), "text length: %zu\n", file.content.size());
  46. s += buf;
  47. s += "----------------\n";
  48. }
  49. return s;
  50. }
  51. string log(const Request &req, const Response &res) {
  52. string s;
  53. char buf[BUFSIZ];
  54. s += "================================\n";
  55. snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
  56. req.version.c_str(), req.path.c_str());
  57. s += buf;
  58. string query;
  59. for (auto it = req.params.begin(); it != req.params.end(); ++it) {
  60. const auto &x = *it;
  61. snprintf(buf, sizeof(buf), "%c%s=%s",
  62. (it == req.params.begin()) ? '?' : '&', x.first.c_str(),
  63. x.second.c_str());
  64. query += buf;
  65. }
  66. snprintf(buf, sizeof(buf), "%s\n", query.c_str());
  67. s += buf;
  68. s += dump_headers(req.headers);
  69. s += dump_multipart_formdata(req.form);
  70. s += "--------------------------------\n";
  71. snprintf(buf, sizeof(buf), "%d\n", res.status);
  72. s += buf;
  73. s += dump_headers(res.headers);
  74. return s;
  75. }
  76. int main(int argc, const char **argv) {
  77. if (argc > 1 && string("--help") == argv[1]) {
  78. cout << "usage: simplesvr [PORT] [DIR]" << endl;
  79. return 1;
  80. }
  81. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  82. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
  83. #else
  84. Server svr;
  85. #endif
  86. svr.Post("/multipart", [](const Request &req, Response &res) {
  87. auto body = dump_headers(req.headers) + dump_multipart_formdata(req.form);
  88. res.set_content(body, "text/plain");
  89. });
  90. svr.set_error_handler([](const Request & /*req*/, Response &res) {
  91. const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
  92. char buf[BUFSIZ];
  93. snprintf(buf, sizeof(buf), fmt, res.status);
  94. res.set_content(buf, "text/html");
  95. });
  96. svr.set_logger(
  97. [](const Request &req, const Response &res) { cout << log(req, res); });
  98. auto port = 8080;
  99. if (argc > 1) { port = atoi(argv[1]); }
  100. auto base_dir = "./";
  101. if (argc > 2) { base_dir = argv[2]; }
  102. if (!svr.set_mount_point("/", base_dir)) {
  103. cout << "The specified base directory doesn't exist...";
  104. return 1;
  105. }
  106. cout << "The server started at port " << port << "..." << endl;
  107. svr.listen("localhost", port);
  108. return 0;
  109. }