server.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // sample.cc
  3. //
  4. // Copyright (c) 2012 Yuji Hirose. All rights reserved.
  5. // The Boost Software License 1.0
  6. //
  7. #include <httplib.h>
  8. #include <cstdio>
  9. #include <chrono>
  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. {
  15. std::string s;
  16. char buf[BUFSIZ];
  17. for (auto it = headers.begin(); it != headers.end(); ++it) {
  18. const auto& x = *it;
  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. std::string log(const Request& req, const Response& res)
  25. {
  26. std::string s;
  27. char buf[BUFSIZ];
  28. s += "================================\n";
  29. snprintf(buf, sizeof(buf), "%s %s", req.method.c_str(), req.path.c_str());
  30. s += buf;
  31. std::string query;
  32. for (auto it = req.params.begin(); it != req.params.end(); ++it) {
  33. const auto& x = *it;
  34. snprintf(buf, sizeof(buf), "%c%s=%s",
  35. (it == req.params.begin()) ? '?' : '&', x.first.c_str(), 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\n", res.status);
  43. s += buf;
  44. s += dump_headers(res.headers);
  45. if (!res.body.empty()) {
  46. s += res.body;
  47. }
  48. s += "\n";
  49. return s;
  50. }
  51. int main(void)
  52. {
  53. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  54. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
  55. #else
  56. Server svr;
  57. #endif
  58. svr.get("/", [=](const auto& /*req*/, auto& res) {
  59. res.set_redirect("/hi");
  60. });
  61. svr.get("/hi", [](const auto& /*req*/, auto& res) {
  62. res.set_content("Hello World!\n", "text/plain");
  63. });
  64. svr.get("/slow", [](const auto& /*req*/, auto& res) {
  65. using namespace std::chrono_literals;
  66. std::this_thread::sleep_for(2s);
  67. res.set_content("Slow...\n", "text/plain");
  68. });
  69. svr.get("/dump", [](const auto& req, auto& res) {
  70. res.set_content(dump_headers(req.headers), "text/plain");
  71. });
  72. svr.get("/stop", [&](const auto& /*req*/, auto& /*res*/) {
  73. svr.stop();
  74. });
  75. svr.set_error_handler([](const auto& /*req*/, auto& 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 auto& req, const auto& res) {
  82. printf("%s", log(req, res).c_str());
  83. });
  84. svr.listen("localhost", 8080);
  85. return 0;
  86. }
  87. // vim: et ts=4 sw=4 cin cino={1s ff=unix