main.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // main.cc
  3. //
  4. // Copyright (c) 2025 Yuji Hirose. All rights reserved.
  5. // MIT License
  6. //
  7. #include <atomic>
  8. #include <chrono>
  9. #include <ctime>
  10. #include <format>
  11. #include <iomanip>
  12. #include <iostream>
  13. #include <signal.h>
  14. #include <sstream>
  15. #include <httplib.h>
  16. using namespace httplib;
  17. const auto SERVER_NAME =
  18. std::format("cpp-httplib-server/{}", CPPHTTPLIB_VERSION);
  19. Server svr;
  20. void signal_handler(int signal) {
  21. if (signal == SIGINT || signal == SIGTERM) {
  22. std::cout << "\nReceived signal, shutting down gracefully...\n";
  23. svr.stop();
  24. }
  25. }
  26. std::string get_time_format() {
  27. auto now = std::chrono::system_clock::now();
  28. auto time_t = std::chrono::system_clock::to_time_t(now);
  29. std::stringstream ss;
  30. ss << std::put_time(std::localtime(&time_t), "%d/%b/%Y:%H:%M:%S %z");
  31. return ss.str();
  32. }
  33. std::string get_error_time_format() {
  34. auto now = std::chrono::system_clock::now();
  35. auto time_t = std::chrono::system_clock::to_time_t(now);
  36. std::stringstream ss;
  37. ss << std::put_time(std::localtime(&time_t), "%Y/%m/%d %H:%M:%S");
  38. return ss.str();
  39. }
  40. std::string get_client_ip(const Request &req) {
  41. // Check for X-Forwarded-For header first (common in reverse proxy setups)
  42. auto forwarded_for = req.get_header_value("X-Forwarded-For");
  43. if (!forwarded_for.empty()) {
  44. // Get the first IP if there are multiple
  45. auto comma_pos = forwarded_for.find(',');
  46. if (comma_pos != std::string::npos) {
  47. return forwarded_for.substr(0, comma_pos);
  48. }
  49. return forwarded_for;
  50. }
  51. // Check for X-Real-IP header
  52. auto real_ip = req.get_header_value("X-Real-IP");
  53. if (!real_ip.empty()) { return real_ip; }
  54. // Fallback to remote address (though cpp-httplib doesn't provide this
  55. // directly) For demonstration, we'll use a placeholder
  56. return "127.0.0.1";
  57. }
  58. // NGINX Combined log format:
  59. // $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent
  60. // "$http_referer" "$http_user_agent"
  61. void nginx_access_logger(const Request &req, const Response &res) {
  62. auto remote_addr = get_client_ip(req);
  63. std::string remote_user =
  64. "-"; // cpp-httplib doesn't have built-in auth user tracking
  65. auto time_local = get_time_format();
  66. auto request = std::format("{} {} {}", req.method, req.path, req.version);
  67. auto status = res.status;
  68. auto body_bytes_sent = res.body.size();
  69. auto http_referer = req.get_header_value("Referer");
  70. if (http_referer.empty()) http_referer = "-";
  71. auto http_user_agent = req.get_header_value("User-Agent");
  72. if (http_user_agent.empty()) http_user_agent = "-";
  73. std::cout << std::format("{} - {} [{}] \"{}\" {} {} \"{}\" \"{}\"",
  74. remote_addr, remote_user, time_local, request,
  75. status, body_bytes_sent, http_referer,
  76. http_user_agent)
  77. << std::endl;
  78. }
  79. // NGINX Error log format:
  80. // YYYY/MM/DD HH:MM:SS [level] message, client: client_ip, request: "request",
  81. // host: "host"
  82. void nginx_error_logger(const Error &err, const Request *req) {
  83. auto time_local = get_error_time_format();
  84. std::string level = "error";
  85. if (req) {
  86. auto client_ip = get_client_ip(*req);
  87. auto request =
  88. std::format("{} {} {}", req->method, req->path, req->version);
  89. auto host = req->get_header_value("Host");
  90. if (host.empty()) host = "-";
  91. std::cerr << std::format("{} [{}] {}, client: {}, request: "
  92. "\"{}\", host: \"{}\"",
  93. time_local, level, to_string(err), client_ip,
  94. request, host)
  95. << std::endl;
  96. } else {
  97. // If no request context, just log the error
  98. std::cerr << std::format("{} [{}] {}", time_local, level, to_string(err))
  99. << std::endl;
  100. }
  101. }
  102. void print_usage(const char *program_name) {
  103. std::cout << "Usage: " << program_name << " [OPTIONS]" << std::endl;
  104. std::cout << std::endl;
  105. std::cout << "Options:" << std::endl;
  106. std::cout << " --host <hostname> Server hostname (default: localhost)"
  107. << std::endl;
  108. std::cout << " --port <port> Server port (default: 8080)"
  109. << std::endl;
  110. std::cout << " --mount <mount:path> Mount point and document root"
  111. << std::endl;
  112. std::cout << " Format: mount_point:document_root"
  113. << std::endl;
  114. std::cout << " (default: /:./html)" << std::endl;
  115. std::cout << " --version Show version information"
  116. << std::endl;
  117. std::cout << " --help Show this help message" << std::endl;
  118. std::cout << std::endl;
  119. std::cout << "Examples:" << std::endl;
  120. std::cout << " " << program_name
  121. << " --host localhost --port 8080 --mount /:./html" << std::endl;
  122. std::cout << " " << program_name
  123. << " --host 0.0.0.0 --port 3000 --mount /api:./api" << std::endl;
  124. }
  125. struct ServerConfig {
  126. std::string hostname = "localhost";
  127. int port = 8080;
  128. std::string mount_point = "/";
  129. std::string document_root = "./html";
  130. };
  131. enum class ParseResult { SUCCESS, HELP_REQUESTED, VERSION_REQUESTED, ERROR };
  132. ParseResult parse_command_line(int argc, char *argv[], ServerConfig &config) {
  133. for (int i = 1; i < argc; i++) {
  134. if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
  135. print_usage(argv[0]);
  136. return ParseResult::HELP_REQUESTED;
  137. } else if (strcmp(argv[i], "--host") == 0) {
  138. if (i + 1 >= argc) {
  139. std::cerr << "Error: --host requires a hostname argument" << std::endl;
  140. print_usage(argv[0]);
  141. return ParseResult::ERROR;
  142. }
  143. config.hostname = argv[++i];
  144. } else if (strcmp(argv[i], "--port") == 0) {
  145. if (i + 1 >= argc) {
  146. std::cerr << "Error: --port requires a port number argument"
  147. << std::endl;
  148. print_usage(argv[0]);
  149. return ParseResult::ERROR;
  150. }
  151. config.port = std::atoi(argv[++i]);
  152. if (config.port <= 0 || config.port > 65535) {
  153. std::cerr << "Error: Invalid port number. Must be between 1 and 65535"
  154. << std::endl;
  155. return ParseResult::ERROR;
  156. }
  157. } else if (strcmp(argv[i], "--mount") == 0) {
  158. if (i + 1 >= argc) {
  159. std::cerr
  160. << "Error: --mount requires mount_point:document_root argument"
  161. << std::endl;
  162. print_usage(argv[0]);
  163. return ParseResult::ERROR;
  164. }
  165. std::string mount_arg = argv[++i];
  166. auto colon_pos = mount_arg.find(':');
  167. if (colon_pos == std::string::npos) {
  168. std::cerr << "Error: --mount argument must be in format "
  169. "mount_point:document_root"
  170. << std::endl;
  171. print_usage(argv[0]);
  172. return ParseResult::ERROR;
  173. }
  174. config.mount_point = mount_arg.substr(0, colon_pos);
  175. config.document_root = mount_arg.substr(colon_pos + 1);
  176. if (config.mount_point.empty() || config.document_root.empty()) {
  177. std::cerr
  178. << "Error: Both mount_point and document_root must be non-empty"
  179. << std::endl;
  180. return ParseResult::ERROR;
  181. }
  182. } else if (strcmp(argv[i], "--version") == 0) {
  183. std::cout << CPPHTTPLIB_VERSION << std::endl;
  184. return ParseResult::VERSION_REQUESTED;
  185. } else {
  186. std::cerr << "Error: Unknown option '" << argv[i] << "'" << std::endl;
  187. print_usage(argv[0]);
  188. return ParseResult::ERROR;
  189. }
  190. }
  191. return ParseResult::SUCCESS;
  192. }
  193. bool setup_server(Server &svr, const ServerConfig &config) {
  194. svr.set_logger(nginx_access_logger);
  195. svr.set_error_logger(nginx_error_logger);
  196. auto ret = svr.set_mount_point(config.mount_point, config.document_root);
  197. if (!ret) {
  198. std::cerr
  199. << std::format(
  200. "Error: Cannot mount '{}' to '{}'. Directory may not exist.",
  201. config.mount_point, config.document_root)
  202. << std::endl;
  203. return false;
  204. }
  205. svr.set_file_extension_and_mimetype_mapping("html", "text/html");
  206. svr.set_file_extension_and_mimetype_mapping("htm", "text/html");
  207. svr.set_file_extension_and_mimetype_mapping("css", "text/css");
  208. svr.set_file_extension_and_mimetype_mapping("js", "text/javascript");
  209. svr.set_file_extension_and_mimetype_mapping("json", "application/json");
  210. svr.set_file_extension_and_mimetype_mapping("xml", "application/xml");
  211. svr.set_file_extension_and_mimetype_mapping("png", "image/png");
  212. svr.set_file_extension_and_mimetype_mapping("jpg", "image/jpeg");
  213. svr.set_file_extension_and_mimetype_mapping("jpeg", "image/jpeg");
  214. svr.set_file_extension_and_mimetype_mapping("gif", "image/gif");
  215. svr.set_file_extension_and_mimetype_mapping("svg", "image/svg+xml");
  216. svr.set_file_extension_and_mimetype_mapping("ico", "image/x-icon");
  217. svr.set_file_extension_and_mimetype_mapping("pdf", "application/pdf");
  218. svr.set_file_extension_and_mimetype_mapping("zip", "application/zip");
  219. svr.set_file_extension_and_mimetype_mapping("txt", "text/plain");
  220. svr.set_error_handler([](const Request & /*req*/, Response &res) {
  221. if (res.status == 404) {
  222. res.set_content(
  223. std::format(
  224. "<html><head><title>404 Not Found</title></head>"
  225. "<body><h1>404 Not Found</h1>"
  226. "<p>The requested resource was not found on this server.</p>"
  227. "<hr><p>{}</p></body></html>",
  228. SERVER_NAME),
  229. "text/html");
  230. }
  231. });
  232. svr.set_pre_routing_handler([](const Request & /*req*/, Response &res) {
  233. res.set_header("Server", SERVER_NAME);
  234. return Server::HandlerResponse::Unhandled;
  235. });
  236. signal(SIGINT, signal_handler);
  237. signal(SIGTERM, signal_handler);
  238. return true;
  239. }
  240. int main(int argc, char *argv[]) {
  241. ServerConfig config;
  242. auto result = parse_command_line(argc, argv, config);
  243. switch (result) {
  244. case ParseResult::HELP_REQUESTED:
  245. case ParseResult::VERSION_REQUESTED: return 0;
  246. case ParseResult::ERROR: return 1;
  247. case ParseResult::SUCCESS: break;
  248. }
  249. if (!setup_server(svr, config)) { return 1; }
  250. std::cout << "Serving HTTP on " << config.hostname << ":" << config.port
  251. << std::endl;
  252. std::cout << "Mount point: " << config.mount_point << " -> "
  253. << config.document_root << std::endl;
  254. std::cout << "Press Ctrl+C to shutdown gracefully..." << std::endl;
  255. auto ret = svr.listen(config.hostname, config.port);
  256. std::cout << "Server has been shut down." << std::endl;
  257. return ret ? 0 : 1;
  258. }