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 MultipartFiles &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 offset: %lu\n", file.offset);
  37. s += buf;
  38. snprintf(buf, sizeof(buf), "text length: %lu\n", file.length);
  39. s += buf;
  40. s += "----------------\n";
  41. }
  42. return s;
  43. }
  44. string log(const Request &req, const Response &res) {
  45. string s;
  46. char buf[BUFSIZ];
  47. s += "================================\n";
  48. snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
  49. req.version.c_str(), req.path.c_str());
  50. s += buf;
  51. string query;
  52. for (auto it = req.params.begin(); it != req.params.end(); ++it) {
  53. const auto &x = *it;
  54. snprintf(buf, sizeof(buf), "%c%s=%s",
  55. (it == req.params.begin()) ? '?' : '&', x.first.c_str(),
  56. x.second.c_str());
  57. query += buf;
  58. }
  59. snprintf(buf, sizeof(buf), "%s\n", query.c_str());
  60. s += buf;
  61. s += dump_headers(req.headers);
  62. s += dump_multipart_files(req.files);
  63. s += "--------------------------------\n";
  64. snprintf(buf, sizeof(buf), "%d\n", res.status);
  65. s += buf;
  66. s += dump_headers(res.headers);
  67. return s;
  68. }
  69. int main(int argc, const char **argv) {
  70. if (argc > 1 && string("--help") == argv[1]) {
  71. cout << "usage: simplesvr [PORT] [DIR]" << endl;
  72. return 1;
  73. }
  74. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  75. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
  76. #else
  77. Server svr;
  78. #endif
  79. svr.Post("/multipart", [](const Request &req, Response &res) {
  80. auto body = dump_headers(req.headers) + dump_multipart_files(req.files);
  81. res.set_content(body, "text/plain");
  82. });
  83. svr.set_error_handler([](const Request & /*req*/, Response &res) {
  84. const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
  85. char buf[BUFSIZ];
  86. snprintf(buf, sizeof(buf), fmt, res.status);
  87. res.set_content(buf, "text/html");
  88. });
  89. svr.set_logger(
  90. [](const Request &req, const Response &res) { cout << log(req, res); });
  91. auto port = 8080;
  92. if (argc > 1) { port = atoi(argv[1]); }
  93. auto base_dir = "./";
  94. if (argc > 2) { base_dir = argv[2]; }
  95. svr.set_base_dir(base_dir);
  96. cout << "The server started at port " << port << "...";
  97. svr.listen("localhost", port);
  98. return 0;
  99. }