httpd.cpp 9.8 KB

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