main.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /* Copyright (c) 2004-2013 Sergey Lyubka
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #if defined(_WIN32)
  22. #define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */
  23. #else
  24. #define _XOPEN_SOURCE 600 /* For PATH_MAX on linux */
  25. #endif
  26. #ifndef IGNORE_UNUSED_RESULT
  27. #define IGNORE_UNUSED_RESULT(a) (void)((a) && 1)
  28. #endif
  29. #include <sys/stat.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <signal.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. #include <limits.h>
  36. #include <stddef.h>
  37. #include <stdarg.h>
  38. #include <ctype.h>
  39. #include "civetweb.h"
  40. #ifdef _WIN32
  41. #include <windows.h>
  42. #include <winsvc.h>
  43. #include <shlobj.h>
  44. #define getcwd(a,b) _getcwd(a,b)
  45. extern char *_getcwd(char *buf, size_t size);
  46. #ifndef PATH_MAX
  47. #define PATH_MAX MAX_PATH
  48. #endif
  49. #ifndef S_ISDIR
  50. #define S_ISDIR(x) ((x) & _S_IFDIR)
  51. #endif
  52. #define DIRSEP '\\'
  53. #define snprintf _snprintf
  54. #define vsnprintf _vsnprintf
  55. #define sleep(x) Sleep((x) * 1000)
  56. #define WINCDECL __cdecl
  57. #define abs_path(rel, abs, abs_size) _fullpath((abs), (rel), (abs_size))
  58. #else
  59. #include <sys/wait.h>
  60. #include <unistd.h>
  61. #define DIRSEP '/'
  62. #define WINCDECL
  63. #define abs_path(rel, abs, abs_size) realpath((rel), (abs))
  64. #endif /* _WIN32 */
  65. #define MAX_OPTIONS 100
  66. #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
  67. static int exit_flag;
  68. static char server_name[40]; /* Set by init_server_name() */
  69. static char config_file[PATH_MAX] = ""; /* Set by
  70. process_command_line_arguments() */
  71. static struct mg_context *ctx; /* Set by start_civetweb() */
  72. #if !defined(CONFIG_FILE)
  73. #define CONFIG_FILE "civetweb.conf"
  74. #endif /* !CONFIG_FILE */
  75. /* backup config file */
  76. #if !defined(CONFIG_FILE2) && defined(LINUX)
  77. #define CONFIG_FILE2 "/usr/local/etc/civetweb.conf"
  78. #endif
  79. static void WINCDECL signal_handler(int sig_num)
  80. {
  81. exit_flag = sig_num;
  82. }
  83. static void die(const char *fmt, ...)
  84. {
  85. va_list ap;
  86. char msg[200] = "";
  87. va_start(ap, fmt);
  88. (void) vsnprintf(msg, sizeof(msg) -1, fmt, ap);
  89. msg[sizeof(msg)-1] = 0;
  90. va_end(ap);
  91. #if defined(_WIN32)
  92. MessageBox(NULL, msg, "Error", MB_OK);
  93. #else
  94. fprintf(stderr, "%s\n", msg);
  95. #endif
  96. exit(EXIT_FAILURE);
  97. }
  98. static void show_usage_and_exit(void)
  99. {
  100. const struct mg_option *options;
  101. int i;
  102. fprintf(stderr, "Civetweb v%s, built on %s\n",
  103. mg_version(), __DATE__);
  104. fprintf(stderr, "Usage:\n");
  105. fprintf(stderr, " civetweb -A <htpasswd_file> <realm> <user> <passwd>\n");
  106. fprintf(stderr, " civetweb [config_file]\n");
  107. fprintf(stderr, " civetweb [-option value ...]\n");
  108. fprintf(stderr, "\nOPTIONS:\n");
  109. options = mg_get_valid_options();
  110. for (i = 0; options[i].name != NULL; i++) {
  111. fprintf(stderr, " -%s %s\n",
  112. options[i].name[i], options[i].default_value == NULL ? "<empty>" : options[i].default_value);
  113. }
  114. exit(EXIT_FAILURE);
  115. }
  116. #if defined(_WIN32) || defined(USE_COCOA)
  117. static const char *config_file_top_comment =
  118. "# Civetweb web server configuration file.\n"
  119. "# For detailed description of every option, visit\n"
  120. "# https://github.com/sunsetbrew/civetweb/blob/master/docs/UserManual.md\n"
  121. "# Lines starting with '#' and empty lines are ignored.\n"
  122. "# To make a change, remove leading '#', modify option's value,\n"
  123. "# save this file and then restart Civetweb.\n\n";
  124. static const char *get_url_to_first_open_port(const struct mg_context *ctx)
  125. {
  126. static char url[100];
  127. const char *open_ports = mg_get_option(ctx, "listening_ports");
  128. int a, b, c, d, port, n;
  129. if (sscanf(open_ports, "%d.%d.%d.%d:%d%n", &a, &b, &c, &d, &port, &n) == 5) {
  130. snprintf(url, sizeof(url), "%s://%d.%d.%d.%d:%d",
  131. open_ports[n] == 's' ? "https" : "http", a, b, c, d, port);
  132. } else if (sscanf(open_ports, "%d%n", &port, &n) == 1) {
  133. snprintf(url, sizeof(url), "%s://localhost:%d",
  134. open_ports[n] == 's' ? "https" : "http", port);
  135. } else {
  136. snprintf(url, sizeof(url), "%s", "http://localhost:8080");
  137. }
  138. return url;
  139. }
  140. static void create_config_file(const char *path)
  141. {
  142. const struct mg_option *options;
  143. const char *value;
  144. FILE *fp;
  145. int i;
  146. /* Create config file if it is not present yet */
  147. if ((fp = fopen(path, "r")) != NULL) {
  148. fclose(fp);
  149. } else if ((fp = fopen(path, "a+")) != NULL) {
  150. fprintf(fp, "%s", config_file_top_comment);
  151. options = mg_get_valid_options();
  152. for (i = 0; options[i].name != NULL; i++) {
  153. value = mg_get_option(ctx, options[i].name);
  154. fprintf(fp, "# %s %s\n", options[i].name, value ? value : "<value>");
  155. }
  156. fclose(fp);
  157. }
  158. }
  159. #endif
  160. static char *sdup(const char *str)
  161. {
  162. size_t len;
  163. char *p;
  164. len = strlen(str) + 1;
  165. if ((p = (char *) malloc(len)) != NULL) {
  166. memcpy(p, str, len);
  167. }
  168. return p;
  169. }
  170. static int set_option(char **options, const char *name, const char *value)
  171. {
  172. int i;
  173. for (i = 0; i < MAX_OPTIONS - 3; i++) {
  174. if (options[i] == NULL) {
  175. options[i] = sdup(name);
  176. options[i + 1] = sdup(value);
  177. options[i + 2] = NULL;
  178. break;
  179. } else if (!strcmp(options[i], name)) {
  180. free(options[i + 1]);
  181. options[i + 1] = sdup(value);
  182. break;
  183. }
  184. }
  185. if (i == MAX_OPTIONS - 3) {
  186. die("%s", "Too many options specified");
  187. }
  188. /* TODO: check if this option is defined and the correct data type, return 1 (OK) or 0 (false) */
  189. return 1;
  190. }
  191. static void read_config_file(const char *config_file, char **options)
  192. {
  193. char line[MAX_CONF_FILE_LINE_SIZE], *p;
  194. FILE *fp = NULL;
  195. size_t i, j, cmd_line_opts_start = 1, line_no = 0;
  196. fp = fopen(config_file, "r");
  197. /* If config file was set in command line and open failed, die */
  198. if (cmd_line_opts_start == 2 && fp == NULL) {
  199. die("Cannot open config file %s: %s", config_file, strerror(errno));
  200. }
  201. /* Load config file settings first */
  202. if (fp != NULL) {
  203. fprintf(stderr, "Loading config file %s\n", config_file);
  204. /* Loop over the lines in config file */
  205. while (fgets(line, sizeof(line), fp) != NULL) {
  206. if (!line_no && !memcmp(line,"\xEF\xBB\xBF",3)) {
  207. /* strip UTF-8 BOM */
  208. p = line+3;
  209. } else {
  210. p = line;
  211. }
  212. line_no++;
  213. /* Ignore empty lines and comments */
  214. for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
  215. if (p[i] == '#' || p[i] == '\0') {
  216. continue;
  217. }
  218. /* Skip spaces, \r and \n at the end of the line */
  219. for (j = strlen(line)-1; isspace(* (unsigned char *) &line[j]) || iscntrl(* (unsigned char *) &line[j]); ) line[j--]=0;
  220. /* Find the space character between option name and value */
  221. for (j=i; !isspace(* (unsigned char *) &line[j]) && (line[j]!=0); ) j++;
  222. /* Terminate the string - then the string at (line+i) contains the option name */
  223. line[j] = 0;
  224. j++;
  225. /* Trim additional spaces between option name and value - then (line+j) contains the option value */
  226. while (isspace(line[j])) j++;
  227. /* Set option */
  228. if (!set_option(options, line+i, line+j)) {
  229. printf("%s: line %d is invalid, ignoring it:\n %s",
  230. config_file, (int) line_no, p);
  231. }
  232. }
  233. (void) fclose(fp);
  234. }
  235. }
  236. static void process_command_line_arguments(char *argv[], char **options)
  237. {
  238. char *p;
  239. FILE *fp = NULL;
  240. size_t i, cmd_line_opts_start = 1, line_no = 0;
  241. /* Should we use a config file ? */
  242. if (argv[1] != NULL && argv[1][0] != '-') {
  243. snprintf(config_file, sizeof(config_file), "%s", argv[1]);
  244. cmd_line_opts_start = 2;
  245. } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
  246. /* No command line flags specified. Look where binary lives */
  247. snprintf(config_file, sizeof(config_file)-1, "%s", CONFIG_FILE);
  248. config_file[sizeof(config_file)-1] = 0;
  249. } else {
  250. snprintf(config_file, sizeof(config_file)-1, "%.*s%c%s",
  251. (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
  252. config_file[sizeof(config_file)-1] = 0;
  253. }
  254. #ifdef CONFIG_FILE2
  255. fp = fopen(config_file, "r");
  256. /* try alternate config file */
  257. if (fp == NULL) {
  258. fp = fopen(CONFIG_FILE2, "r");
  259. if (fp != NULL) {
  260. strcpy(config_file, CONFIG_FILE2);
  261. }
  262. }
  263. if (fp != NULL) {
  264. fclose(fp);
  265. }
  266. #endif
  267. /* read all configurations from a config file */
  268. (void)read_config_file(config_file, options);
  269. /* If we're under MacOS and started by launchd, then the second
  270. argument is process serial number, -psn_.....
  271. In this case, don't process arguments at all. */
  272. if (argv[1] == NULL || memcmp(argv[1], "-psn_", 5) != 0) {
  273. /* Handle command line flags.
  274. They override config file and default settings. */
  275. for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
  276. if (argv[i][0] != '-' || argv[i + 1] == NULL) {
  277. show_usage_and_exit();
  278. }
  279. set_option(options, &argv[i][1], argv[i + 1]);
  280. }
  281. }
  282. }
  283. static void init_server_name(void)
  284. {
  285. snprintf(server_name, sizeof(server_name), "Civetweb v%s",
  286. mg_version());
  287. }
  288. static int log_message(const struct mg_connection *conn, const char *message)
  289. {
  290. (void) conn;
  291. printf("%s\n", message);
  292. return 0;
  293. }
  294. static int is_path_absolute(const char *path)
  295. {
  296. #ifdef _WIN32
  297. return path != NULL &&
  298. ((path[0] == '\\' && path[1] == '\\') || /* UNC path, e.g.
  299. \\server\dir */
  300. (isalpha(path[0]) && path[1] == ':' && path[2] == '\\')); /* E.g. X:\dir */
  301. #else
  302. return path != NULL && path[0] == '/';
  303. #endif
  304. }
  305. static char *get_option(char **options, const char *option_name)
  306. {
  307. int i;
  308. for (i = 0; options[i] != NULL; i++)
  309. if (!strcmp(options[i], option_name))
  310. return options[i + 1];
  311. return NULL;
  312. }
  313. static void verify_existence(char **options, const char *option_name,
  314. int must_be_dir)
  315. {
  316. struct stat st;
  317. const char *path = get_option(options, option_name);
  318. #ifdef _WIN32
  319. wchar_t wbuf[1024];
  320. char mbbuf[1024];
  321. int len;
  322. if (path) {
  323. memset(wbuf, 0, sizeof(wbuf));
  324. memset(mbbuf, 0, sizeof(mbbuf));
  325. len = MultiByteToWideChar(CP_UTF8, 0, path, -1, wbuf, (int) sizeof(wbuf)/sizeof(wbuf[0])-1);
  326. wcstombs(mbbuf, wbuf, sizeof(mbbuf)-1);
  327. path = mbbuf;
  328. }
  329. #endif
  330. if (path != NULL && (stat(path, &st) != 0 ||
  331. ((S_ISDIR(st.st_mode) ? 1 : 0) != must_be_dir))) {
  332. die("Invalid path for %s: [%s]: (%s). Make sure that path is either "
  333. "absolute, or it is relative to civetweb executable.",
  334. option_name, path, strerror(errno));
  335. }
  336. }
  337. static void set_absolute_path(char *options[], const char *option_name,
  338. const char *path_to_civetweb_exe)
  339. {
  340. char path[PATH_MAX] = "", abs[PATH_MAX] = "", *option_value;
  341. const char *p;
  342. /* Check whether option is already set */
  343. option_value = get_option(options, option_name);
  344. /* If option is already set and it is an absolute path,
  345. leave it as it is -- it's already absolute. */
  346. if (option_value != NULL && !is_path_absolute(option_value)) {
  347. /* Not absolute. Use the directory where civetweb executable lives
  348. be the relative directory for everything.
  349. Extract civetweb executable directory into path. */
  350. if ((p = strrchr(path_to_civetweb_exe, DIRSEP)) == NULL) {
  351. IGNORE_UNUSED_RESULT(getcwd(path, sizeof(path)));
  352. } else {
  353. snprintf(path, sizeof(path)-1, "%.*s", (int) (p - path_to_civetweb_exe),
  354. path_to_civetweb_exe);
  355. path[sizeof(path)-1] = 0;
  356. }
  357. strncat(path, "/", sizeof(path) - 1);
  358. strncat(path, option_value, sizeof(path) - 1);
  359. /* Absolutize the path, and set the option */
  360. IGNORE_UNUSED_RESULT(abs_path(path, abs, sizeof(abs)));
  361. set_option(options, option_name, abs);
  362. }
  363. }
  364. static void start_civetweb(int argc, char *argv[])
  365. {
  366. struct mg_callbacks callbacks;
  367. char *options[MAX_OPTIONS];
  368. int i;
  369. /* Edit passwords file if -A option is specified */
  370. if (argc > 1 && !strcmp(argv[1], "-A")) {
  371. if (argc != 6) {
  372. show_usage_and_exit();
  373. }
  374. exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
  375. EXIT_SUCCESS : EXIT_FAILURE);
  376. }
  377. /* Show usage if -h or --help options are specified */
  378. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
  379. show_usage_and_exit();
  380. }
  381. options[0] = NULL;
  382. set_option(options, "document_root", ".");
  383. /* Update config based on command line arguments */
  384. process_command_line_arguments(argv, options);
  385. /* Make sure we have absolute paths for files and directories */
  386. set_absolute_path(options, "document_root", argv[0]);
  387. set_absolute_path(options, "put_delete_auth_file", argv[0]);
  388. set_absolute_path(options, "cgi_interpreter", argv[0]);
  389. set_absolute_path(options, "access_log_file", argv[0]);
  390. set_absolute_path(options, "error_log_file", argv[0]);
  391. set_absolute_path(options, "global_auth_file", argv[0]);
  392. #ifdef USE_LUA
  393. set_absolute_path(options, "lua_preload_file", argv[0]);
  394. #endif
  395. set_absolute_path(options, "ssl_certificate", argv[0]);
  396. /* Make extra verification for certain options */
  397. verify_existence(options, "document_root", 1);
  398. verify_existence(options, "cgi_interpreter", 0);
  399. verify_existence(options, "ssl_certificate", 0);
  400. #ifdef USE_LUA
  401. verify_existence(options, "lua_preload_file", 0);
  402. #endif
  403. /* Setup signal handler: quit on Ctrl-C */
  404. signal(SIGTERM, signal_handler);
  405. signal(SIGINT, signal_handler);
  406. /* Start Civetweb */
  407. memset(&callbacks, 0, sizeof(callbacks));
  408. callbacks.log_message = &log_message;
  409. ctx = mg_start(&callbacks, NULL, (const char **) options);
  410. for (i = 0; options[i] != NULL; i++) {
  411. free(options[i]);
  412. }
  413. if (ctx == NULL) {
  414. die("%s", "Failed to start Civetweb.");
  415. }
  416. }
  417. #ifdef _WIN32
  418. enum {
  419. ID_ICON = 100, ID_QUIT, ID_SETTINGS, ID_SEPARATOR, ID_INSTALL_SERVICE,
  420. ID_REMOVE_SERVICE, ID_STATIC, ID_GROUP,
  421. ID_SAVE, ID_RESET_DEFAULTS, ID_RESET_FILE, ID_RESET_ACTIVE,
  422. ID_STATUS, ID_CONNECT,
  423. /* All dynamically created text boxes for options have IDs starting from
  424. ID_CONTROLS, incremented by one. */
  425. ID_CONTROLS = 200,
  426. /* Text boxes for files have "..." buttons to open file browser. These
  427. buttons have IDs that are ID_FILE_BUTTONS_DELTA higher than associated
  428. text box ID. */
  429. ID_FILE_BUTTONS_DELTA = 1000
  430. };
  431. static HICON hIcon;
  432. static SERVICE_STATUS ss;
  433. static SERVICE_STATUS_HANDLE hStatus;
  434. static const char *service_magic_argument = "--";
  435. static NOTIFYICONDATA TrayIcon;
  436. static void WINAPI ControlHandler(DWORD code)
  437. {
  438. if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
  439. ss.dwWin32ExitCode = 0;
  440. ss.dwCurrentState = SERVICE_STOPPED;
  441. }
  442. SetServiceStatus(hStatus, &ss);
  443. }
  444. static void WINAPI ServiceMain(void)
  445. {
  446. ss.dwServiceType = SERVICE_WIN32;
  447. ss.dwCurrentState = SERVICE_RUNNING;
  448. ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  449. hStatus = RegisterServiceCtrlHandler(server_name, ControlHandler);
  450. SetServiceStatus(hStatus, &ss);
  451. while (ss.dwCurrentState == SERVICE_RUNNING) {
  452. Sleep(1000);
  453. }
  454. mg_stop(ctx);
  455. ss.dwCurrentState = SERVICE_STOPPED;
  456. ss.dwWin32ExitCode = (DWORD) -1;
  457. SetServiceStatus(hStatus, &ss);
  458. }
  459. static void show_error(void)
  460. {
  461. char buf[256];
  462. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  463. NULL, GetLastError(),
  464. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  465. buf, sizeof(buf), NULL);
  466. MessageBox(NULL, buf, "Error", MB_OK);
  467. }
  468. static void *align(void *ptr, DWORD alig)
  469. {
  470. ULONG ul = (ULONG) ptr;
  471. ul += alig;
  472. ul &= ~alig;
  473. return ((void *) ul);
  474. }
  475. /*
  476. static int is_boolean_option(const char *option_name)
  477. {
  478. return !strcmp(option_name, "enable_directory_listing") ||
  479. !strcmp(option_name, "enable_keep_alive");
  480. }
  481. static int is_filename_option(const char *option_name)
  482. {
  483. return !strcmp(option_name, "cgi_interpreter") ||
  484. !strcmp(option_name, "global_auth_file") ||
  485. !strcmp(option_name, "put_delete_auth_file") ||
  486. !strcmp(option_name, "access_log_file") ||
  487. !strcmp(option_name, "error_log_file") ||
  488. #ifdef USE_LUA
  489. !strcmp(option_name, "lua_preload_file") ||
  490. #endif
  491. !strcmp(option_name, "ssl_certificate");
  492. }
  493. static int is_directory_option(const char *option_name)
  494. {
  495. return !strcmp(option_name, "document_root") ||
  496. #if defined(USE_WEBSOCKET)
  497. !strcmp(option_name, "websocket_root") ||
  498. #endif
  499. 0;
  500. }
  501. static int is_numeric_options(const char *option_name)
  502. {
  503. return !strcmp(option_name, "num_threads") ||
  504. !strcmp(option_name, "request_timeout_ms");
  505. }
  506. */
  507. static void save_config(HWND hDlg, FILE *fp)
  508. {
  509. char value[2000] = "";
  510. const char *default_value;
  511. const struct mg_option *options;
  512. int i, id;
  513. fprintf(fp, "%s", config_file_top_comment);
  514. options = mg_get_valid_options();
  515. for (i = 0; options[i].name != NULL; i++) {
  516. id = ID_CONTROLS + i;
  517. if (options[i].type == CONFIG_TYPE_BOOLEAN) {
  518. snprintf(value, sizeof(value)-1, "%s",
  519. IsDlgButtonChecked(hDlg, id) ? "yes" : "no");
  520. value[sizeof(value)-1] = 0;
  521. } else {
  522. GetDlgItemText(hDlg, id, value, sizeof(value));
  523. }
  524. default_value = options[i].default_value == NULL ? "" : options[i].default_value;
  525. /* If value is the same as default, skip it */
  526. if (strcmp(value, default_value) != 0) {
  527. fprintf(fp, "%s %s\n", options[i].name, value);
  528. }
  529. }
  530. }
  531. static BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP)
  532. {
  533. FILE *fp;
  534. int i, j;
  535. const char *name, *value;
  536. const struct mg_option *default_options = mg_get_valid_options();
  537. char *file_options[MAX_OPTIONS] = {0};
  538. switch (msg) {
  539. case WM_CLOSE:
  540. DestroyWindow(hDlg);
  541. break;
  542. case WM_COMMAND:
  543. switch (LOWORD(wParam)) {
  544. case ID_SAVE:
  545. EnableWindow(GetDlgItem(hDlg, ID_SAVE), FALSE);
  546. if ((fp = fopen(config_file, "w+")) != NULL) {
  547. save_config(hDlg, fp);
  548. fclose(fp);
  549. mg_stop(ctx);
  550. start_civetweb(__argc, __argv);
  551. }
  552. EnableWindow(GetDlgItem(hDlg, ID_SAVE), TRUE);
  553. break;
  554. case ID_RESET_DEFAULTS:
  555. for (i = 0; default_options[i].name != NULL; i++) {
  556. name = default_options[i].name;
  557. value = default_options[i].default_value == NULL ? "" : default_options[i].default_value;
  558. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  559. CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ?
  560. BST_CHECKED : BST_UNCHECKED);
  561. } else {
  562. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value);
  563. }
  564. }
  565. break;
  566. case ID_RESET_FILE:
  567. read_config_file(config_file, file_options);
  568. for (i = 0; default_options[i].name != NULL; i++) {
  569. name = default_options[i].name;
  570. value = default_options[i].default_value;
  571. for (j = 0; file_options[j * 2] != NULL; j++) {
  572. if (!strcmp(name, file_options[j * 2])) {
  573. value = file_options[j * 2 + 1];
  574. }
  575. }
  576. if (value == NULL) {
  577. value = "";
  578. }
  579. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  580. CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ? BST_CHECKED : BST_UNCHECKED);
  581. } else {
  582. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value);
  583. }
  584. }
  585. for (i = 0; i<MAX_OPTIONS; i++) {
  586. free(file_options[i]);
  587. }
  588. break;
  589. case ID_RESET_ACTIVE:
  590. for (i = 0; default_options[i].name != NULL; i++) {
  591. name = default_options[i].name;
  592. value = mg_get_option(ctx, name);
  593. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  594. CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ?
  595. BST_CHECKED : BST_UNCHECKED);
  596. } else {
  597. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value);
  598. }
  599. }
  600. break;
  601. }
  602. for (i = 0; default_options[i].name != NULL; i++) {
  603. name = default_options[i].name;
  604. if ((is_filename_option(name) || is_directory_option(name)) &&
  605. LOWORD(wParam) == ID_CONTROLS + i + ID_FILE_BUTTONS_DELTA) {
  606. OPENFILENAME of;
  607. BROWSEINFO bi;
  608. char path[PATH_MAX] = "";
  609. memset(&of, 0, sizeof(of));
  610. of.lStructSize = sizeof(of);
  611. of.hwndOwner = (HWND) hDlg;
  612. of.lpstrFile = path;
  613. of.nMaxFile = sizeof(path);
  614. of.lpstrInitialDir = mg_get_option(ctx, "document_root");
  615. of.Flags = OFN_CREATEPROMPT | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  616. memset(&bi, 0, sizeof(bi));
  617. bi.hwndOwner = (HWND) hDlg;
  618. bi.lpszTitle = "Choose WWW root directory:";
  619. bi.ulFlags = BIF_RETURNONLYFSDIRS;
  620. if (is_directory_option(name)) {
  621. SHGetPathFromIDList(SHBrowseForFolder(&bi), path);
  622. } else {
  623. GetOpenFileName(&of);
  624. }
  625. if (path[0] != '\0') {
  626. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), path);
  627. }
  628. }
  629. }
  630. break;
  631. case WM_INITDIALOG:
  632. SendMessage(hDlg, WM_SETICON,(WPARAM) ICON_SMALL, (LPARAM) hIcon);
  633. SendMessage(hDlg, WM_SETICON,(WPARAM) ICON_BIG, (LPARAM) hIcon);
  634. SetWindowText(hDlg, "Civetweb settings");
  635. SetFocus(GetDlgItem(hDlg, ID_SAVE));
  636. for (i = 0; default_options[i].name != NULL; i++) {
  637. name = default_options[i].name;
  638. value = mg_get_option(ctx, name);
  639. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  640. CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ?
  641. BST_CHECKED : BST_UNCHECKED);
  642. } else {
  643. SetDlgItemText(hDlg, ID_CONTROLS + i, value == NULL ? "" : value);
  644. }
  645. }
  646. break;
  647. default:
  648. break;
  649. }
  650. return FALSE;
  651. }
  652. static void add_control(unsigned char **mem, DLGTEMPLATE *dia, WORD type,
  653. DWORD id, DWORD style, WORD x, WORD y,
  654. WORD cx, WORD cy, const char *caption)
  655. {
  656. DLGITEMTEMPLATE *tp;
  657. LPWORD p;
  658. dia->cdit++;
  659. *mem = align(*mem, 3);
  660. tp = (DLGITEMTEMPLATE *) *mem;
  661. tp->id = (WORD)id;
  662. tp->style = style;
  663. tp->dwExtendedStyle = 0;
  664. tp->x = x;
  665. tp->y = y;
  666. tp->cx = cx;
  667. tp->cy = cy;
  668. p = align(*mem + sizeof(*tp), 1);
  669. *p++ = 0xffff;
  670. *p++ = type;
  671. while (*caption != '\0') {
  672. *p++ = (WCHAR) *caption++;
  673. }
  674. *p++ = 0;
  675. p = align(p, 1);
  676. *p++ = 0;
  677. *mem = (unsigned char *) p;
  678. }
  679. static void show_settings_dialog()
  680. {
  681. #define HEIGHT 15
  682. #define WIDTH 460
  683. #define LABEL_WIDTH 90
  684. unsigned char mem[4096], *p;
  685. const struct mg_option *options;
  686. DWORD style;
  687. DLGTEMPLATE *dia = (DLGTEMPLATE *) mem;
  688. WORD i, cl, x, y, width, nelems = 0;
  689. static int guard = 0; /* test if dialog is already open */
  690. static struct {
  691. DLGTEMPLATE template; /* 18 bytes */
  692. WORD menu, class;
  693. wchar_t caption[1];
  694. WORD fontsiz;
  695. wchar_t fontface[7];
  696. } dialog_header = {{
  697. WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE |
  698. DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW, 0, 200, 200, WIDTH, 0
  699. },
  700. 0, 0, L"", 8, L"Tahoma"
  701. };
  702. if (guard == 0) {
  703. guard++;
  704. } else {
  705. return;
  706. }
  707. (void) memset(mem, 0, sizeof(mem));
  708. (void) memcpy(mem, &dialog_header, sizeof(dialog_header));
  709. p = mem + sizeof(dialog_header);
  710. options = mg_get_valid_options();
  711. for (i = 0; options[i].name != NULL; i++) {
  712. style = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
  713. x = 10 + (WIDTH / 2) * (nelems % 2);
  714. y = (nelems/2 + 1) * HEIGHT + 5;
  715. width = WIDTH / 2 - 20 - LABEL_WIDTH;
  716. if (is_numeric_options(options[i].name)) {
  717. style |= ES_NUMBER;
  718. cl = 0x81;
  719. style |= WS_BORDER | ES_AUTOHSCROLL;
  720. } else if (options[i].type == CONFIG_TYPE_BOOLEAN) {
  721. cl = 0x80;
  722. style |= BS_AUTOCHECKBOX;
  723. } else if (is_filename_option(options[i].name) ||
  724. is_directory_option(options[i].name)) {
  725. style |= WS_BORDER | ES_AUTOHSCROLL;
  726. width -= 20;
  727. cl = 0x81;
  728. add_control(&p, dia, 0x80,
  729. ID_CONTROLS + i + ID_FILE_BUTTONS_DELTA,
  730. WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  731. (WORD) (x + width + LABEL_WIDTH + 5),
  732. y, 15, 12, "...");
  733. } else {
  734. cl = 0x81;
  735. style |= WS_BORDER | ES_AUTOHSCROLL;
  736. }
  737. add_control(&p, dia, 0x82, ID_STATIC, WS_VISIBLE | WS_CHILD,
  738. x, y, LABEL_WIDTH, HEIGHT, options[i].name);
  739. add_control(&p, dia, cl, ID_CONTROLS + i, style,
  740. (WORD) (x + LABEL_WIDTH), y, width, 12, "");
  741. nelems++;
  742. }
  743. y = (WORD) (((nelems + 1) / 2 + 1) * HEIGHT + 5);
  744. add_control(&p, dia, 0x80, ID_GROUP, WS_CHILD | WS_VISIBLE |
  745. BS_GROUPBOX, 5, 5, WIDTH - 10, y, " Settings ");
  746. y += 10;
  747. add_control(&p, dia, 0x80, ID_SAVE,
  748. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  749. WIDTH - 70, y, 65, 12, "Save Settings");
  750. add_control(&p, dia, 0x80, ID_RESET_DEFAULTS,
  751. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  752. WIDTH - 140, y, 65, 12, "Reset to defaults");
  753. add_control(&p, dia, 0x80, ID_RESET_FILE,
  754. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  755. WIDTH - 210, y, 65, 12, "Reload from file");
  756. add_control(&p, dia, 0x80, ID_RESET_ACTIVE,
  757. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  758. WIDTH - 280, y, 65, 12, "Reload active");
  759. add_control(&p, dia, 0x82, ID_STATIC,
  760. WS_CHILD | WS_VISIBLE | WS_DISABLED,
  761. 5, y, 100, 12, server_name);
  762. dia->cy = ((nelems + 1) / 2 + 1) * HEIGHT + 30;
  763. DialogBoxIndirectParam(NULL, dia, NULL, DlgProc, (LPARAM) NULL);
  764. guard--;
  765. }
  766. static int manage_service(int action)
  767. {
  768. static const char *service_name = "Civetweb";
  769. SC_HANDLE hSCM = NULL, hService = NULL;
  770. SERVICE_DESCRIPTION descr = {server_name};
  771. char path[PATH_MAX + 20] = "";/* Path to executable plus magic argument */
  772. int success = 1;
  773. if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
  774. GENERIC_WRITE : GENERIC_READ)) == NULL) {
  775. success = 0;
  776. show_error();
  777. } else if (action == ID_INSTALL_SERVICE) {
  778. path[sizeof(path)-1] = 0;
  779. GetModuleFileName(NULL, path, sizeof(path)-1);
  780. strncat(path, " ", sizeof(path)-1);
  781. strncat(path, service_magic_argument, sizeof(path)-1);
  782. hService = CreateService(hSCM, service_name, service_name,
  783. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  784. SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
  785. path, NULL, NULL, NULL, NULL, NULL);
  786. if (hService) {
  787. ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
  788. } else {
  789. show_error();
  790. }
  791. } else if (action == ID_REMOVE_SERVICE) {
  792. if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
  793. !DeleteService(hService)) {
  794. show_error();
  795. }
  796. } else if ((hService = OpenService(hSCM, service_name,
  797. SERVICE_QUERY_STATUS)) == NULL) {
  798. success = 0;
  799. }
  800. if (hService)
  801. CloseServiceHandle(hService);
  802. if (hSCM)
  803. CloseServiceHandle(hSCM);
  804. return success;
  805. }
  806. static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
  807. LPARAM lParam)
  808. {
  809. static SERVICE_TABLE_ENTRY service_table[] = {
  810. {server_name, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
  811. {NULL, NULL}
  812. };
  813. int service_installed;
  814. char buf[200], *service_argv[] = {__argv[0], NULL};
  815. POINT pt;
  816. HMENU hMenu;
  817. static UINT s_uTaskbarRestart; /* for taskbar creation */
  818. switch (msg) {
  819. case WM_CREATE:
  820. if (__argv[1] != NULL &&
  821. !strcmp(__argv[1], service_magic_argument)) {
  822. start_civetweb(1, service_argv);
  823. StartServiceCtrlDispatcher(service_table);
  824. exit(EXIT_SUCCESS);
  825. } else {
  826. start_civetweb(__argc, __argv);
  827. s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
  828. }
  829. break;
  830. case WM_COMMAND:
  831. switch (LOWORD(wParam)) {
  832. case ID_QUIT:
  833. mg_stop(ctx);
  834. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  835. PostQuitMessage(0);
  836. return 0;
  837. case ID_SETTINGS:
  838. show_settings_dialog();
  839. break;
  840. case ID_INSTALL_SERVICE:
  841. case ID_REMOVE_SERVICE:
  842. manage_service(LOWORD(wParam));
  843. break;
  844. case ID_CONNECT:
  845. printf("[%s]\n", get_url_to_first_open_port(ctx));
  846. ShellExecute(NULL, "open", get_url_to_first_open_port(ctx),
  847. NULL, NULL, SW_SHOW);
  848. break;
  849. }
  850. break;
  851. case WM_USER:
  852. switch (lParam) {
  853. case WM_RBUTTONUP:
  854. case WM_LBUTTONUP:
  855. case WM_LBUTTONDBLCLK:
  856. hMenu = CreatePopupMenu();
  857. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name);
  858. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  859. service_installed = manage_service(0);
  860. snprintf(buf, sizeof(buf)-1, "NT service: %s installed",
  861. service_installed ? "" : "not");
  862. buf[sizeof(buf)-1] = 0;
  863. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf);
  864. AppendMenu(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0),
  865. ID_INSTALL_SERVICE, "Install service");
  866. AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0),
  867. ID_REMOVE_SERVICE, "Deinstall service");
  868. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  869. AppendMenu(hMenu, MF_STRING, ID_CONNECT, "Start browser");
  870. AppendMenu(hMenu, MF_STRING, ID_SETTINGS, "Edit Settings");
  871. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  872. AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit");
  873. GetCursorPos(&pt);
  874. SetForegroundWindow(hWnd);
  875. TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
  876. PostMessage(hWnd, WM_NULL, 0, 0);
  877. DestroyMenu(hMenu);
  878. break;
  879. }
  880. break;
  881. case WM_CLOSE:
  882. mg_stop(ctx);
  883. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  884. PostQuitMessage(0);
  885. return 0;/* We've just sent our own quit message, with proper hwnd. */
  886. default:
  887. if (msg==s_uTaskbarRestart)
  888. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  889. }
  890. return DefWindowProc(hWnd, msg, wParam, lParam);
  891. }
  892. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show)
  893. {
  894. WNDCLASS cls;
  895. HWND hWnd;
  896. MSG msg;
  897. init_server_name();
  898. memset(&cls, 0, sizeof(cls));
  899. cls.lpfnWndProc = (WNDPROC) WindowProc;
  900. cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  901. cls.lpszClassName = server_name;
  902. RegisterClass(&cls);
  903. hWnd = CreateWindow(cls.lpszClassName, server_name, WS_OVERLAPPEDWINDOW,
  904. 0, 0, 0, 0, NULL, NULL, NULL, NULL);
  905. ShowWindow(hWnd, SW_HIDE);
  906. TrayIcon.cbSize = sizeof(TrayIcon);
  907. TrayIcon.uID = ID_ICON;
  908. TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  909. TrayIcon.hIcon = hIcon = LoadImage(GetModuleHandle(NULL),
  910. MAKEINTRESOURCE(ID_ICON),
  911. IMAGE_ICON, 16, 16, 0);
  912. TrayIcon.hWnd = hWnd;
  913. snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name);
  914. TrayIcon.uCallbackMessage = WM_USER;
  915. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  916. while (GetMessage(&msg, hWnd, 0, 0) > 0) {
  917. TranslateMessage(&msg);
  918. DispatchMessage(&msg);
  919. }
  920. /* Return the WM_QUIT value. */
  921. return (int) msg.wParam;
  922. }
  923. #elif defined(USE_COCOA)
  924. #import <Cocoa/Cocoa.h>
  925. @interface Civetweb :
  926. NSObject<NSApplicationDelegate>
  927. - (void) openBrowser;
  928. - (void) shutDown;
  929. @end
  930. @implementation Civetweb
  931. - (void) openBrowser {
  932. [[NSWorkspace sharedWorkspace]
  933. openURL:[NSURL URLWithString:
  934. [NSString stringWithUTF8String:get_url_to_first_open_port(ctx)]]];
  935. }
  936. - (void) editConfig {
  937. create_config_file(config_file);
  938. [[NSWorkspace sharedWorkspace]
  939. openFile:[NSString stringWithUTF8String:config_file]
  940. withApplication:@"TextEdit"];
  941. }
  942. - (void)shutDown {
  943. [NSApp terminate:nil];
  944. }
  945. @end
  946. int main(int argc, char *argv[])
  947. {
  948. init_server_name();
  949. start_civetweb(argc, argv);
  950. [NSAutoreleasePool new];
  951. [NSApplication sharedApplication];
  952. /* Add delegate to process menu item actions */
  953. Civetweb *myDelegate = [[Civetweb alloc] autorelease];
  954. [NSApp setDelegate: myDelegate];
  955. /* Run this app as agent */
  956. ProcessSerialNumber psn = { 0, kCurrentProcess };
  957. TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
  958. SetFrontProcess(&psn);
  959. /* Add status bar menu */
  960. id menu = [[NSMenu new] autorelease];
  961. /* Add version menu item */
  962. [menu addItem:[[[NSMenuItem alloc]
  963. /*initWithTitle:[NSString stringWithFormat:@"%s", server_name]*/
  964. initWithTitle:[NSString stringWithUTF8String:server_name]
  965. action:@selector(noexist) keyEquivalent:@""] autorelease]];
  966. /* Add configuration menu item */
  967. [menu addItem:[[[NSMenuItem alloc]
  968. initWithTitle:@"Edit configuration"
  969. action:@selector(editConfig) keyEquivalent:@""] autorelease]];
  970. /* Add connect menu item */
  971. [menu addItem:[[[NSMenuItem alloc]
  972. initWithTitle:@"Open web root in a browser"
  973. action:@selector(openBrowser) keyEquivalent:@""] autorelease]];
  974. /* Separator */
  975. [menu addItem:[NSMenuItem separatorItem]];
  976. /* Add quit menu item */
  977. [menu addItem:[[[NSMenuItem alloc]
  978. initWithTitle:@"Quit"
  979. action:@selector(shutDown) keyEquivalent:@"q"] autorelease]];
  980. /* Attach menu to the status bar */
  981. id item = [[[NSStatusBar systemStatusBar]
  982. statusItemWithLength:NSVariableStatusItemLength] retain];
  983. [item setHighlightMode:YES];
  984. [item setImage:[NSImage imageNamed:@"civetweb_22x22.png"]];
  985. [item setMenu:menu];
  986. /* Run the app */
  987. [NSApp activateIgnoringOtherApps:YES];
  988. [NSApp run];
  989. mg_stop(ctx);
  990. return EXIT_SUCCESS;
  991. }
  992. #else
  993. int main(int argc, char *argv[])
  994. {
  995. init_server_name();
  996. start_civetweb(argc, argv);
  997. printf("%s started on port(s) %s with web root [%s]\n",
  998. server_name, mg_get_option(ctx, "listening_ports"),
  999. mg_get_option(ctx, "document_root"));
  1000. while (exit_flag == 0) {
  1001. sleep(1);
  1002. }
  1003. printf("Exiting on signal %d, waiting for all threads to finish...",
  1004. exit_flag);
  1005. fflush(stdout);
  1006. mg_stop(ctx);
  1007. printf("%s", " done.\n");
  1008. return EXIT_SUCCESS;
  1009. }
  1010. #endif /* _WIN32 */