1
0

httpd.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "hv.h"
  2. #include "hssl.h"
  3. #include "hmain.h"
  4. #include "iniparser.h"
  5. #include "HttpServer.h"
  6. #include "hasync.h" // import hv::async
  7. #include "router.h"
  8. hv::HttpServer g_http_server;
  9. hv::HttpService g_http_service;
  10. static void print_version();
  11. static void print_help();
  12. static int parse_confile(const char* confile);
  13. // short options
  14. static const char options[] = "hvc:ts:dp:";
  15. // long options
  16. static const option_t long_options[] = {
  17. {'h', "help", NO_ARGUMENT},
  18. {'v', "version", NO_ARGUMENT},
  19. {'c', "confile", REQUIRED_ARGUMENT},
  20. {'t', "test", NO_ARGUMENT},
  21. {'s', "signal", REQUIRED_ARGUMENT},
  22. {'d', "daemon", NO_ARGUMENT},
  23. {'p', "port", REQUIRED_ARGUMENT}
  24. };
  25. static const char detail_options[] = R"(
  26. -h|--help Print this information
  27. -v|--version Print version
  28. -c|--confile <confile> Set configure file, default etc/{program}.conf
  29. -t|--test Test configure file and exit
  30. -s|--signal <signal> Send <signal> to process,
  31. <signal>=[start,stop,restart,status,reload]
  32. -d|--daemon Daemonize
  33. -p|--port <port> Set listen port
  34. )";
  35. void print_version() {
  36. printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
  37. }
  38. void print_help() {
  39. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  40. printf("Options:\n%s\n", detail_options);
  41. }
  42. int parse_confile(const char* confile) {
  43. IniParser ini;
  44. int ret = ini.LoadFromFile(confile);
  45. if (ret != 0) {
  46. printf("Load confile [%s] failed: %d\n", confile, ret);
  47. exit(-40);
  48. }
  49. // logfile
  50. std::string str = ini.GetValue("logfile");
  51. if (!str.empty()) {
  52. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  53. }
  54. hlog_set_file(g_main_ctx.logfile);
  55. // loglevel
  56. str = ini.GetValue("loglevel");
  57. if (!str.empty()) {
  58. hlog_set_level_by_str(str.c_str());
  59. }
  60. // log_filesize
  61. str = ini.GetValue("log_filesize");
  62. if (!str.empty()) {
  63. hlog_set_max_filesize_by_str(str.c_str());
  64. }
  65. // log_remain_days
  66. str = ini.GetValue("log_remain_days");
  67. if (!str.empty()) {
  68. hlog_set_remain_days(atoi(str.c_str()));
  69. }
  70. // log_fsync
  71. str = ini.GetValue("log_fsync");
  72. if (!str.empty()) {
  73. logger_enable_fsync(hlog, hv_getboolean(str.c_str()));
  74. }
  75. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  76. hlog_fsync();
  77. // worker_processes
  78. int worker_processes = 0;
  79. #ifdef DEBUG
  80. // Disable multi-processes mode for debugging
  81. worker_processes = 0;
  82. #else
  83. str = ini.GetValue("worker_processes");
  84. if (str.size() != 0) {
  85. if (strcmp(str.c_str(), "auto") == 0) {
  86. worker_processes = get_ncpu();
  87. hlogd("worker_processes=ncpu=%d", worker_processes);
  88. }
  89. else {
  90. worker_processes = atoi(str.c_str());
  91. }
  92. }
  93. #endif
  94. g_http_server.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  95. // worker_threads
  96. int worker_threads = 0;
  97. str = ini.GetValue("worker_threads");
  98. if (str.size() != 0) {
  99. if (strcmp(str.c_str(), "auto") == 0) {
  100. worker_threads = get_ncpu();
  101. hlogd("worker_threads=ncpu=%d", worker_threads);
  102. }
  103. else {
  104. worker_threads = atoi(str.c_str());
  105. }
  106. }
  107. g_http_server.worker_threads = LIMIT(0, worker_threads, 64);
  108. // worker_connections
  109. str = ini.GetValue("worker_connections");
  110. if (str.size() != 0) {
  111. g_http_server.worker_connections = atoi(str.c_str());
  112. }
  113. // http_port
  114. int port = 0;
  115. const char* szPort = get_arg("p");
  116. if (szPort) {
  117. port = atoi(szPort);
  118. }
  119. if (port == 0) {
  120. port = ini.Get<int>("port");
  121. }
  122. if (port == 0) {
  123. port = ini.Get<int>("http_port");
  124. }
  125. g_http_server.port = port;
  126. // https_port
  127. if (HV_WITH_SSL) {
  128. g_http_server.https_port = ini.Get<int>("https_port");
  129. }
  130. if (g_http_server.port == 0 && g_http_server.https_port == 0) {
  131. printf("Please config listen port!\n");
  132. exit(-10);
  133. }
  134. // base_url
  135. str = ini.GetValue("base_url");
  136. if (str.size() != 0) {
  137. g_http_service.base_url = str;
  138. }
  139. // document_root
  140. str = ini.GetValue("document_root");
  141. if (str.size() != 0) {
  142. g_http_service.document_root = str;
  143. }
  144. // home_page
  145. str = ini.GetValue("home_page");
  146. if (str.size() != 0) {
  147. g_http_service.home_page = str;
  148. }
  149. // error_page
  150. str = ini.GetValue("error_page");
  151. if (str.size() != 0) {
  152. g_http_service.error_page = str;
  153. }
  154. // index_of
  155. str = ini.GetValue("index_of");
  156. if (str.size() != 0) {
  157. g_http_service.index_of = str;
  158. }
  159. // keepalive_timeout
  160. str = ini.GetValue("keepalive_timeout");
  161. if (str.size() != 0) {
  162. g_http_service.keepalive_timeout = atoi(str.c_str());
  163. }
  164. // limit_rate
  165. str = ini.GetValue("limit_rate");
  166. if (str.size() != 0) {
  167. g_http_service.limit_rate = atoi(str.c_str());
  168. }
  169. // access_log
  170. str = ini.GetValue("access_log");
  171. if (str.size() != 0) {
  172. g_http_service.enable_access_log = hv_getboolean(str.c_str());
  173. }
  174. // cors
  175. if (ini.Get<bool>("cors")) {
  176. g_http_service.AllowCORS();
  177. }
  178. // ssl
  179. if (g_http_server.https_port > 0) {
  180. std::string crt_file = ini.GetValue("ssl_certificate");
  181. std::string key_file = ini.GetValue("ssl_privatekey");
  182. std::string ca_file = ini.GetValue("ssl_ca_certificate");
  183. hlogi("SSL backend is %s", hssl_backend());
  184. hssl_ctx_opt_t param;
  185. memset(&param, 0, sizeof(param));
  186. param.crt_file = crt_file.c_str();
  187. param.key_file = key_file.c_str();
  188. param.ca_file = ca_file.c_str();
  189. param.endpoint = HSSL_SERVER;
  190. if (g_http_server.newSslCtx(&param) != 0) {
  191. #ifdef OS_WIN
  192. if (strcmp(hssl_backend(), "schannel") == 0) {
  193. hlogw("schannel needs pkcs12 formatted certificate file.");
  194. g_http_server.https_port = 0;
  195. }
  196. #else
  197. hloge("SSL certificate verify failed!");
  198. exit(0);
  199. #endif
  200. }
  201. else {
  202. hlogi("SSL certificate verify ok!");
  203. }
  204. }
  205. // proxy
  206. auto proxy_keys = ini.GetKeys("proxy");
  207. for (const auto& proxy_key : proxy_keys) {
  208. str = ini.GetValue(proxy_key, "proxy");
  209. if (str.empty()) continue;
  210. if (proxy_key[0] == '/') {
  211. // reverse proxy
  212. const std::string& path = proxy_key;
  213. std::string proxy_url = hv::ltrim(str, "> ");
  214. hlogi("reverse_proxy %s => %s", path.c_str(), proxy_url.c_str());
  215. g_http_service.Proxy(path.c_str(), proxy_url.c_str());
  216. }
  217. else if (strcmp(proxy_key.c_str(), "proxy_connect_timeout") == 0) {
  218. g_http_service.proxy_connect_timeout = atoi(str.c_str());
  219. }
  220. else if (strcmp(proxy_key.c_str(), "proxy_read_timeout") == 0) {
  221. g_http_service.proxy_read_timeout = atoi(str.c_str());
  222. }
  223. else if (strcmp(proxy_key.c_str(), "proxy_write_timeout") == 0) {
  224. g_http_service.proxy_write_timeout = atoi(str.c_str());
  225. }
  226. else if (strcmp(proxy_key.c_str(), "forward_proxy") == 0) {
  227. hlogi("forward_proxy = %s", str.c_str());
  228. if (hv_getboolean(str.c_str())) {
  229. g_http_service.EnableForwardProxy();
  230. }
  231. }
  232. else if (strcmp(proxy_key.c_str(), "trust_proxies") == 0) {
  233. auto trust_proxies = hv::split(str, ';');
  234. for (auto trust_proxy : trust_proxies) {
  235. trust_proxy = hv::trim(trust_proxy);
  236. if (trust_proxy.empty()) continue;
  237. hlogi("trust_proxy %s", trust_proxy.c_str());
  238. g_http_service.AddTrustProxy(trust_proxy.c_str());
  239. }
  240. }
  241. else if (strcmp(proxy_key.c_str(), "no_proxies") == 0) {
  242. auto no_proxies = hv::split(str, ';');
  243. for (auto no_proxy : no_proxies) {
  244. no_proxy = hv::trim(no_proxy);
  245. if (no_proxy.empty()) continue;
  246. hlogi("no_proxy %s", no_proxy.c_str());
  247. g_http_service.AddNoProxy(no_proxy.c_str());
  248. }
  249. }
  250. }
  251. hlogi("parse_confile('%s') OK", confile);
  252. return 0;
  253. }
  254. static void on_reload(void* userdata) {
  255. hlogi("reload confile [%s]", g_main_ctx.confile);
  256. parse_confile(g_main_ctx.confile);
  257. }
  258. int main(int argc, char** argv) {
  259. // g_main_ctx
  260. main_ctx_init(argc, argv);
  261. //int ret = parse_opt(argc, argv, options);
  262. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  263. if (ret != 0) {
  264. print_help();
  265. exit(ret);
  266. }
  267. // help
  268. if (get_arg("h")) {
  269. print_help();
  270. exit(0);
  271. }
  272. // version
  273. if (get_arg("v")) {
  274. print_version();
  275. exit(0);
  276. }
  277. // parse_confile
  278. const char* confile = get_arg("c");
  279. if (confile) {
  280. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  281. }
  282. parse_confile(g_main_ctx.confile);
  283. // test
  284. if (get_arg("t")) {
  285. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  286. exit(0);
  287. }
  288. // signal
  289. signal_init(on_reload);
  290. const char* signal = get_arg("s");
  291. if (signal) {
  292. signal_handle(signal);
  293. }
  294. #ifdef OS_UNIX
  295. // daemon
  296. if (get_arg("d")) {
  297. // nochdir, noclose
  298. int ret = daemon(1, 1);
  299. if (ret != 0) {
  300. printf("daemon error: %d\n", ret);
  301. exit(-10);
  302. }
  303. }
  304. #endif
  305. // pidfile
  306. create_pidfile();
  307. // http_server
  308. Router::Register(g_http_service);
  309. g_http_server.registerHttpService(&g_http_service);
  310. #if 0
  311. std::atomic_flag init_flag = ATOMIC_FLAG_INIT;
  312. g_http_server.onWorkerStart = [&init_flag](){
  313. if (!init_flag.test_and_set()) {
  314. hv::async::startup();
  315. }
  316. };
  317. g_http_server.onWorkerStop = [&init_flag](){
  318. if (init_flag.test_and_set()) {
  319. hv::async::cleanup();
  320. }
  321. };
  322. #endif
  323. g_http_server.run();
  324. return ret;
  325. }