server.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (!svr.is_valid()) {
  59. printf("server has an error...\n");
  60. return -1;
  61. }
  62. svr.get("/", [=](const auto& /*req*/, auto& res) {
  63. res.set_redirect("/hi");
  64. });
  65. svr.get("/hi", [](const auto& /*req*/, auto& res) {
  66. res.set_content("Hello World!\n", "text/plain");
  67. });
  68. svr.get("/slow", [](const auto& /*req*/, auto& res) {
  69. using namespace std::chrono_literals;
  70. std::this_thread::sleep_for(2s);
  71. res.set_content("Slow...\n", "text/plain");
  72. });
  73. svr.get("/dump", [](const auto& req, auto& res) {
  74. res.set_content(dump_headers(req.headers), "text/plain");
  75. });
  76. svr.get("/stop", [&](const auto& /*req*/, auto& /*res*/) {
  77. svr.stop();
  78. });
  79. svr.set_error_handler([](const auto& /*req*/, auto& res) {
  80. const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
  81. char buf[BUFSIZ];
  82. snprintf(buf, sizeof(buf), fmt, res.status);
  83. res.set_content(buf, "text/html");
  84. });
  85. svr.set_logger([](const auto& req, const auto& res) {
  86. printf("%s", log(req, res).c_str());
  87. });
  88. svr.listen("localhost", 8080);
  89. return 0;
  90. }
  91. // vim: et ts=4 sw=4 cin cino={1s ff=unix