main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "httplib.h"
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <fstream>
  5. #include <string>
  6. #ifndef _WIN32
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #endif
  10. using namespace httplib;
  11. namespace {
  12. // The workloads differ in which part of the write path they exercise:
  13. //
  14. // / small body set through set_content(); the response line,
  15. // the headers and the body already share a single write()
  16. // /large large body set through set_content(); the body is copied
  17. // into the header buffer before that single write().
  18. // --large-mib sets its size (and large.bin's).
  19. // /static/small.js small file served from a mount point, where the headers
  20. // and the body are two separate writes
  21. // /static/large.bin same, with the body large enough to dominate
  22. //
  23. // Bodies are generated at startup so the repository carries no fixtures.
  24. const size_t SMALL_SIZE = 1024;
  25. const size_t LARGE_SIZE_DEFAULT_MIB = 1;
  26. std::string filler(size_t n) {
  27. std::string s;
  28. s.reserve(n);
  29. while (s.size() < n) {
  30. s += "0123456789abcdef";
  31. }
  32. s.resize(n);
  33. return s;
  34. }
  35. bool write_file(const std::string &path, const std::string &content) {
  36. std::ofstream f(path.c_str(), std::ios::binary);
  37. f.write(content.data(), static_cast<std::streamsize>(content.size()));
  38. return f.good();
  39. }
  40. std::string default_dir() {
  41. const char *tmp = std::getenv("TMPDIR");
  42. std::string base = tmp && *tmp ? tmp : "/tmp";
  43. if (!base.empty() && base[base.size() - 1] == '/') {
  44. base.erase(base.size() - 1);
  45. }
  46. return base + "/cpp-httplib-bench";
  47. }
  48. bool make_dir(const std::string &path) {
  49. #ifdef _WIN32
  50. return _mkdir(path.c_str()) == 0 || errno == EEXIST;
  51. #else
  52. return ::mkdir(path.c_str(), 0755) == 0 || errno == EEXIST;
  53. #endif
  54. }
  55. void setup(Server &svr, const std::string &large, const std::string &dir) {
  56. svr.Get("/", [](const Request &, Response &res) {
  57. res.set_content("Hello World!", "text/plain");
  58. });
  59. svr.Get("/large", [&large](const Request &, Response &res) {
  60. res.set_content(large, "application/octet-stream");
  61. });
  62. svr.set_mount_point("/static", dir);
  63. }
  64. } // namespace
  65. int main(int argc, char *argv[]) {
  66. int port = 8080;
  67. std::string dir = default_dir();
  68. std::string cert;
  69. std::string key;
  70. size_t large_mib = LARGE_SIZE_DEFAULT_MIB;
  71. for (int i = 1; i < argc; i++) {
  72. auto last = i + 1 < argc;
  73. if (!std::strcmp(argv[i], "--port") && last) {
  74. port = std::atoi(argv[++i]);
  75. } else if (!std::strcmp(argv[i], "--large-mib") && last) {
  76. large_mib = static_cast<size_t>(std::atoi(argv[++i]));
  77. } else if (!std::strcmp(argv[i], "--dir") && last) {
  78. dir = argv[++i];
  79. } else if (!std::strcmp(argv[i], "--cert") && last) {
  80. cert = argv[++i];
  81. } else if (!std::strcmp(argv[i], "--key") && last) {
  82. key = argv[++i];
  83. } else {
  84. std::fprintf(stderr,
  85. "usage: %s [--port N] [--dir PATH] [--large-mib N]"
  86. " [--cert PATH --key PATH]\n",
  87. argv[0]);
  88. return 2;
  89. }
  90. }
  91. if (!make_dir(dir)) {
  92. std::fprintf(stderr, "cannot create %s\n", dir.c_str());
  93. return 1;
  94. }
  95. auto large = filler(large_mib * 1024 * 1024);
  96. if (!write_file(dir + "/small.js", filler(SMALL_SIZE)) ||
  97. !write_file(dir + "/large.bin", large)) {
  98. std::fprintf(stderr, "cannot write fixtures under %s\n", dir.c_str());
  99. return 1;
  100. }
  101. if (!cert.empty()) {
  102. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  103. SSLServer svr(cert.c_str(), key.c_str());
  104. if (!svr.is_valid()) {
  105. std::fprintf(stderr, "cannot load %s / %s\n", cert.c_str(), key.c_str());
  106. return 1;
  107. }
  108. setup(svr, large, dir);
  109. svr.listen("0.0.0.0", port);
  110. return 0;
  111. #else
  112. std::fprintf(stderr, "built without CPPHTTPLIB_OPENSSL_SUPPORT\n");
  113. return 1;
  114. #endif
  115. }
  116. Server svr;
  117. setup(svr, large, dir);
  118. svr.listen("0.0.0.0", port);
  119. }