server.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 %s", req.method.c_str(), req.path.c_str(), req.version.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 %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()) {
  47. s += res.body;
  48. }
  49. s += "\n";
  50. return s;
  51. }
  52. int main(void)
  53. {
  54. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  55. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
  56. #else
  57. Server svr;
  58. #endif
  59. if (!svr.is_valid()) {
  60. printf("server has an error...\n");
  61. return -1;
  62. }
  63. svr.Get("/", [=](const auto& /*req*/, auto& res) {
  64. res.set_redirect("/hi");
  65. });
  66. svr.Get("/hi", [](const auto& /*req*/, auto& res) {
  67. res.set_content("Hello World!\n", "text/plain");
  68. });
  69. svr.Get("/slow", [](const auto& /*req*/, auto& res) {
  70. using namespace std::chrono_literals;
  71. std::this_thread::sleep_for(2s);
  72. res.set_content("Slow...\n", "text/plain");
  73. });
  74. svr.Get("/dump", [](const auto& req, auto& res) {
  75. res.set_content(dump_headers(req.headers), "text/plain");
  76. });
  77. svr.Get("/stop", [&](const auto& /*req*/, auto& /*res*/) {
  78. svr.stop();
  79. });
  80. svr.set_error_handler([](const auto& /*req*/, auto& res) {
  81. const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
  82. char buf[BUFSIZ];
  83. snprintf(buf, sizeof(buf), fmt, res.status);
  84. res.set_content(buf, "text/html");
  85. });
  86. svr.set_logger([](const auto& req, const auto& res) {
  87. printf("%s", log(req, res).c_str());
  88. });
  89. svr.listen("localhost", 8080);
  90. return 0;
  91. }
  92. // vim: et ts=4 sw=4 cin cino={1s ff=unix