simplesvr.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // simplesvr.cc
  3. //
  4. // Copyright (c) 2013 Yuji Hirose. All rights reserved.
  5. // The Boost Software License 1.0
  6. //
  7. #include <httplib.h>
  8. #include <cstdio>
  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. {
  16. string s;
  17. char buf[BUFSIZ];
  18. for (const auto& x: headers) {
  19. snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
  20. s += buf;
  21. }
  22. return s;
  23. }
  24. string dump_multipart_files(const MultipartFiles& files)
  25. {
  26. string s;
  27. char buf[BUFSIZ];
  28. s += "--------------------------------\n";
  29. for (const auto& x: files) {
  30. const auto& name = x.first;
  31. const auto& file = x.second;
  32. snprintf(buf, sizeof(buf), "name: %s\n", name.c_str());
  33. s += buf;
  34. snprintf(buf, sizeof(buf), "filename: %s\n", file.filename.c_str());
  35. s += buf;
  36. snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str());
  37. s += buf;
  38. snprintf(buf, sizeof(buf), "text offset: %lu\n", file.offset);
  39. s += buf;
  40. snprintf(buf, sizeof(buf), "text length: %lu\n", file.length);
  41. s += buf;
  42. s += "----------------\n";
  43. }
  44. return s;
  45. }
  46. string log(const Request& req, const Response& res)
  47. {
  48. string s;
  49. char buf[BUFSIZ];
  50. s += "================================\n";
  51. snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.path.c_str(), req.version.c_str());
  52. s += buf;
  53. string query;
  54. for (auto it = req.params.begin(); it != req.params.end(); ++it) {
  55. const auto& x = *it;
  56. snprintf(buf, sizeof(buf), "%c%s=%s",
  57. (it == req.params.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str());
  58. query += buf;
  59. }
  60. snprintf(buf, sizeof(buf), "%s\n", query.c_str());
  61. s += buf;
  62. s += dump_headers(req.headers);
  63. s += dump_multipart_files(req.files);
  64. s += "--------------------------------\n";
  65. snprintf(buf, sizeof(buf), "%d\n", res.status);
  66. s += buf;
  67. s += dump_headers(res.headers);
  68. return s;
  69. }
  70. int main(int argc, const char** argv)
  71. {
  72. if (argc > 1 && string("--help") == argv[1]) {
  73. cout << "usage: simplesvr [PORT] [DIR]" << endl;
  74. return 1;
  75. }
  76. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  77. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
  78. #else
  79. Server svr;
  80. #endif
  81. svr.Post("/multipart", [](const Request& req, Response& res) {
  82. auto body =
  83. dump_headers(req.headers) +
  84. dump_multipart_files(req.files);
  85. res.set_content(body, "text/plain");
  86. });
  87. svr.set_error_handler([](const Request& /*req*/, Response& res) {
  88. const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
  89. char buf[BUFSIZ];
  90. snprintf(buf, sizeof(buf), fmt, res.status);
  91. res.set_content(buf, "text/html");
  92. });
  93. svr.set_logger([](const Request& req, const Response& res) {
  94. cout << log(req, res);
  95. });
  96. auto port = 8080;
  97. if (argc > 1) {
  98. port = atoi(argv[1]);
  99. }
  100. auto base_dir = "./";
  101. if (argc > 2) {
  102. base_dir = argv[2];
  103. }
  104. svr.set_base_dir(base_dir);
  105. cout << "The server started at port " << port << "...";
  106. svr.listen("localhost", port);
  107. return 0;
  108. }
  109. // vim: et ts=4 sw=4 cin cino={1s ff=unix