server.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // sample.cc
  3. //
  4. // Copyright (c) 2019 Yuji Hirose. All rights reserved.
  5. // MIT License
  6. //
  7. #include <chrono>
  8. #include <cstdio>
  9. #include <httplib.h>
  10. #define SERVER_CERT_FILE "./cert.pem"
  11. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  12. using namespace httplib;
  13. std::string dump_headers(const Headers &headers) {
  14. std::string s;
  15. char buf[BUFSIZ];
  16. for (auto it = headers.begin(); it != headers.end(); ++it) {
  17. const auto &x = *it;
  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. std::string log(const Request &req, const Response &res) {
  24. std::string s;
  25. char buf[BUFSIZ];
  26. s += "================================\n";
  27. snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
  28. req.version.c_str(), req.path.c_str());
  29. s += buf;
  30. std::string query;
  31. for (auto it = req.params.begin(); it != req.params.end(); ++it) {
  32. const auto &x = *it;
  33. snprintf(buf, sizeof(buf), "%c%s=%s",
  34. (it == req.params.begin()) ? '?' : '&', x.first.c_str(),
  35. x.second.c_str());
  36. query += buf;
  37. }
  38. snprintf(buf, sizeof(buf), "%s\n", query.c_str());
  39. s += buf;
  40. s += dump_headers(req.headers);
  41. s += "--------------------------------\n";
  42. snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
  43. s += buf;
  44. s += dump_headers(res.headers);
  45. s += "\n";
  46. if (!res.body.empty()) { s += res.body; }
  47. s += "\n";
  48. return s;
  49. }
  50. int main(void) {
  51. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  52. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
  53. #else
  54. Server svr;
  55. #endif
  56. if (!svr.is_valid()) {
  57. printf("server has an error...\n");
  58. return -1;
  59. }
  60. svr.Get("/", [=](const Request & /*req*/, Response &res) {
  61. res.set_redirect("/hi");
  62. });
  63. svr.Get("/hi", [](const Request & /*req*/, Response &res) {
  64. res.set_content("Hello World!\n", "text/plain");
  65. });
  66. svr.Get("/slow", [](const Request & /*req*/, Response &res) {
  67. std::this_thread::sleep_for(std::chrono::seconds(2));
  68. res.set_content("Slow...\n", "text/plain");
  69. });
  70. svr.Get("/dump", [](const Request &req, Response &res) {
  71. res.set_content(dump_headers(req.headers), "text/plain");
  72. });
  73. svr.Get("/stop",
  74. [&](const Request & /*req*/, Response & /*res*/) { svr.stop(); });
  75. svr.set_error_handler([](const Request & /*req*/, Response &res) {
  76. const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
  77. char buf[BUFSIZ];
  78. snprintf(buf, sizeof(buf), fmt, res.status);
  79. res.set_content(buf, "text/html");
  80. });
  81. svr.set_logger([](const Request &req, const Response &res) {
  82. printf("%s", log(req, res).c_str());
  83. });
  84. svr.listen("localhost", 8080);
  85. return 0;
  86. }