main.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. /* Copyright (c) 2013-2014 the Civetweb developers
  2. * Copyright (c) 2004-2013 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #if defined(_WIN32)
  23. #define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */
  24. #else
  25. #define _XOPEN_SOURCE 600 /* For PATH_MAX on linux */
  26. #endif
  27. #ifndef IGNORE_UNUSED_RESULT
  28. #define IGNORE_UNUSED_RESULT(a) (void)((a) && 1)
  29. #endif
  30. #include <sys/stat.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <signal.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <limits.h>
  37. #include <stddef.h>
  38. #include <stdarg.h>
  39. #include <ctype.h>
  40. #include <assert.h>
  41. #include "civetweb.h"
  42. #ifdef _WIN32
  43. #include <windows.h>
  44. #include <winsvc.h>
  45. #include <shlobj.h>
  46. #define getcwd(a,b) _getcwd(a,b)
  47. extern char *_getcwd(char *buf, size_t size);
  48. static int guard = 0; /* test if any dialog is already open */
  49. #ifndef PATH_MAX
  50. #define PATH_MAX MAX_PATH
  51. #endif
  52. #ifndef S_ISDIR
  53. #define S_ISDIR(x) ((x) & _S_IFDIR)
  54. #endif
  55. #define DIRSEP '\\'
  56. #define snprintf _snprintf
  57. #define vsnprintf _vsnprintf
  58. #define sleep(x) Sleep((x) * 1000)
  59. #define WINCDECL __cdecl
  60. #define abs_path(rel, abs, abs_size) _fullpath((abs), (rel), (abs_size))
  61. #else
  62. #include <sys/wait.h>
  63. #include <unistd.h>
  64. #define DIRSEP '/'
  65. #define WINCDECL
  66. #define abs_path(rel, abs, abs_size) realpath((rel), (abs))
  67. #endif /* _WIN32 */
  68. #define MAX_OPTIONS 50
  69. #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
  70. static int exit_flag = 0; /* Main loop should exit */
  71. static char server_base_name[40]; /* Set by init_server_name() */
  72. static char *server_name; /* Set by init_server_name() */
  73. static char *icon_name; /* Set by init_server_name() */
  74. static char config_file[PATH_MAX] = ""; /* Set by process_command_line_arguments() */
  75. static struct mg_context *ctx; /* Set by start_civetweb() */
  76. #if !defined(CONFIG_FILE)
  77. #define CONFIG_FILE "civetweb.conf"
  78. #endif /* !CONFIG_FILE */
  79. #if !defined(PASSWORDS_FILE_NAME)
  80. #define PASSWORDS_FILE_NAME ".htpasswd"
  81. #endif
  82. /* backup config file */
  83. #if !defined(CONFIG_FILE2) && defined(LINUX)
  84. #define CONFIG_FILE2 "/usr/local/etc/civetweb.conf"
  85. #endif
  86. enum {
  87. OPTION_TITLE,
  88. OPTION_ICON,
  89. NUM_MAIN_OPTIONS
  90. };
  91. static struct mg_option main_config_options[] = {
  92. {"title", CONFIG_TYPE_STRING, NULL},
  93. {"icon", CONFIG_TYPE_STRING, NULL},
  94. {NULL, CONFIG_TYPE_UNKNOWN, NULL}
  95. };
  96. static void WINCDECL signal_handler(int sig_num)
  97. {
  98. exit_flag = sig_num;
  99. }
  100. static void die(const char *fmt, ...)
  101. {
  102. va_list ap;
  103. char msg[200] = "";
  104. va_start(ap, fmt);
  105. (void) vsnprintf(msg, sizeof(msg) -1, fmt, ap);
  106. msg[sizeof(msg)-1] = 0;
  107. va_end(ap);
  108. #if defined(_WIN32)
  109. MessageBox(NULL, msg, "Error", MB_OK);
  110. #else
  111. fprintf(stderr, "%s\n", msg);
  112. #endif
  113. exit(EXIT_FAILURE);
  114. }
  115. #ifdef WIN32
  116. static int MakeConsole();
  117. #endif
  118. static void show_usage_and_exit(void)
  119. {
  120. const struct mg_option *options;
  121. int i;
  122. #ifdef WIN32
  123. MakeConsole();
  124. #endif
  125. fprintf(stderr, "Civetweb v%s, built on %s\n",
  126. mg_version(), __DATE__);
  127. fprintf(stderr, "Usage:\n");
  128. fprintf(stderr, " civetweb -A <htpasswd_file> <realm> <user> <passwd>\n");
  129. fprintf(stderr, " civetweb [config_file]\n");
  130. fprintf(stderr, " civetweb [-option value ...]\n");
  131. fprintf(stderr, "\nOPTIONS:\n");
  132. options = mg_get_valid_options();
  133. for (i = 0; options[i].name != NULL; i++) {
  134. fprintf(stderr, " -%s %s\n", options[i].name, ((options[i].default_value == NULL) ? "<empty>" : options[i].default_value));
  135. }
  136. options = main_config_options;
  137. for (i = 0; options[i].name != NULL; i++) {
  138. fprintf(stderr, " -%s %s\n", options[i].name, ((options[i].default_value == NULL) ? "<empty>" : options[i].default_value));
  139. }
  140. exit(EXIT_FAILURE);
  141. }
  142. #if defined(_WIN32) || defined(USE_COCOA)
  143. static const char *config_file_top_comment =
  144. "# Civetweb web server configuration file.\n"
  145. "# For detailed description of every option, visit\n"
  146. "# https://github.com/bel2125/civetweb/blob/master/docs/UserManual.md\n"
  147. "# Lines starting with '#' and empty lines are ignored.\n"
  148. "# To make a change, remove leading '#', modify option's value,\n"
  149. "# save this file and then restart Civetweb.\n\n";
  150. static const char *get_url_to_first_open_port(const struct mg_context *ctx)
  151. {
  152. static char url[100];
  153. const char *open_ports = mg_get_option(ctx, "listening_ports");
  154. int a, b, c, d, port, n;
  155. if (sscanf(open_ports, "%d.%d.%d.%d:%d%n", &a, &b, &c, &d, &port, &n) == 5) {
  156. snprintf(url, sizeof(url), "%s://%d.%d.%d.%d:%d",
  157. open_ports[n] == 's' ? "https" : "http", a, b, c, d, port);
  158. } else if (sscanf(open_ports, "%d%n", &port, &n) == 1) {
  159. snprintf(url, sizeof(url), "%s://localhost:%d",
  160. open_ports[n] == 's' ? "https" : "http", port);
  161. } else {
  162. snprintf(url, sizeof(url), "%s", "http://localhost:8080");
  163. }
  164. return url;
  165. }
  166. static void create_config_file(const char *path)
  167. {
  168. const struct mg_option *options;
  169. const char *value;
  170. FILE *fp;
  171. int i;
  172. /* Create config file if it is not present yet */
  173. if ((fp = fopen(path, "r")) != NULL) {
  174. fclose(fp);
  175. } else if ((fp = fopen(path, "a+")) != NULL) {
  176. fprintf(fp, "%s", config_file_top_comment);
  177. options = mg_get_valid_options();
  178. for (i = 0; options[i].name != NULL; i++) {
  179. value = mg_get_option(ctx, options[i].name);
  180. fprintf(fp, "# %s %s\n", options[i].name, value ? value : "<value>");
  181. }
  182. fclose(fp);
  183. }
  184. }
  185. #endif
  186. static char *sdup(const char *str)
  187. {
  188. size_t len;
  189. char *p;
  190. len = strlen(str) + 1;
  191. if ((p = (char *) malloc(len)) != NULL) {
  192. memcpy(p, str, len);
  193. }
  194. return p;
  195. }
  196. static const char *get_option(char **options, const char *option_name)
  197. {
  198. int i = 0;
  199. const char *opt_value = NULL;
  200. /* TODO: options should be an array of key-value-pairs, like
  201. struct {const char * key, const char * value} options[]
  202. but it currently is an array with
  203. options[2*i] = key, options[2*i + 1] = value
  204. */
  205. while (options[2*i] != NULL) {
  206. if (strcmp(options[2*i], option_name) == 0) {
  207. opt_value = options[2*i + 1];
  208. break;
  209. }
  210. i++;
  211. }
  212. return opt_value;
  213. }
  214. static int set_option(char **options, const char *name, const char *value)
  215. {
  216. int i, type;
  217. const struct mg_option *default_options = mg_get_valid_options();
  218. for (i = 0; main_config_options[i].name != NULL; i++) {
  219. if (0==strcmp(name, main_config_options[i].name)) {
  220. /* This option is evaluated by main.c, not civetweb.c - just skip it and return OK */
  221. return 1;
  222. }
  223. }
  224. type = CONFIG_TYPE_UNKNOWN;
  225. for (i = 0; default_options[i].name != NULL; i++) {
  226. if (!strcmp(default_options[i].name, name)) {
  227. type = default_options[i].type;
  228. }
  229. }
  230. switch (type) {
  231. case CONFIG_TYPE_UNKNOWN:
  232. /* unknown option */
  233. return 0;
  234. case CONFIG_TYPE_NUMBER:
  235. /* integer number > 0, e.g. number of threads */
  236. if (atol(value)<1) {
  237. /* invalid number */
  238. return 0;
  239. }
  240. break;
  241. case CONFIG_TYPE_STRING:
  242. /* any text */
  243. break;
  244. case CONFIG_TYPE_BOOLEAN:
  245. /* boolean value, yes or no */
  246. if ((0!=strcmp(value,"yes")) && (0!=strcmp(value,"no"))) {
  247. /* invalid boolean */
  248. return 0;
  249. }
  250. break;
  251. case CONFIG_TYPE_FILE:
  252. case CONFIG_TYPE_DIRECTORY:
  253. /* TODO: check this option when it is set, instead of calling verify_existence later */
  254. break;
  255. case CONFIG_TYPE_EXT_PATTERN:
  256. /* list of file extentions */
  257. break;
  258. default:
  259. die("Unknown option type - option %s", name);
  260. break;
  261. }
  262. for (i = 0; i < MAX_OPTIONS; i++) {
  263. if (options[2*i] == NULL) {
  264. options[2*i] = sdup(name);
  265. options[2*i + 1] = sdup(value);
  266. options[2*i + 2] = NULL;
  267. break;
  268. } else if (!strcmp(options[2*i], name)) {
  269. free(options[2*i + 1]);
  270. options[2*i + 1] = sdup(value);
  271. break;
  272. }
  273. }
  274. if (i == MAX_OPTIONS) {
  275. die("Too many options specified");
  276. }
  277. if (options[2*i] == NULL || options[2*i + 1] == NULL) {
  278. die("Out of memory");
  279. }
  280. /* option set correctly */
  281. return 1;
  282. }
  283. static void read_config_file(const char *config_file, char **options)
  284. {
  285. char line[MAX_CONF_FILE_LINE_SIZE], *p;
  286. FILE *fp = NULL;
  287. size_t i, j, cmd_line_opts_start = 1, line_no = 0;
  288. fp = fopen(config_file, "r");
  289. /* If config file was set in command line and open failed, die */
  290. if (cmd_line_opts_start == 2 && fp == NULL) {
  291. die("Cannot open config file %s: %s", config_file, strerror(errno));
  292. }
  293. /* Load config file settings first */
  294. if (fp != NULL) {
  295. fprintf(stderr, "Loading config file %s\n", config_file);
  296. /* Loop over the lines in config file */
  297. while (fgets(line, sizeof(line), fp) != NULL) {
  298. if (!line_no && !memcmp(line,"\xEF\xBB\xBF",3)) {
  299. /* strip UTF-8 BOM */
  300. p = line+3;
  301. } else {
  302. p = line;
  303. }
  304. line_no++;
  305. /* Ignore empty lines and comments */
  306. for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
  307. if (p[i] == '#' || p[i] == '\0') {
  308. continue;
  309. }
  310. /* Skip spaces, \r and \n at the end of the line */
  311. for (j = strlen(line)-1; isspace(* (unsigned char *) &line[j]) || iscntrl(* (unsigned char *) &line[j]); ) line[j--]=0;
  312. /* Find the space character between option name and value */
  313. for (j=i; !isspace(* (unsigned char *) &line[j]) && (line[j]!=0); ) j++;
  314. /* Terminate the string - then the string at (line+i) contains the option name */
  315. line[j] = 0;
  316. j++;
  317. /* Trim additional spaces between option name and value - then (line+j) contains the option value */
  318. while (isspace(line[j])) j++;
  319. /* Set option */
  320. if (!set_option(options, line+i, line+j)) {
  321. printf("%s: line %d is invalid, ignoring it:\n %s",
  322. config_file, (int) line_no, p);
  323. }
  324. }
  325. (void) fclose(fp);
  326. }
  327. }
  328. static void process_command_line_arguments(char *argv[], char **options)
  329. {
  330. char *p;
  331. size_t i, cmd_line_opts_start = 1;
  332. #ifdef CONFIG_FILE2
  333. FILE *fp = NULL;
  334. #endif
  335. /* Should we use a config file ? */
  336. if (argv[1] != NULL && argv[1][0] != '-') {
  337. snprintf(config_file, sizeof(config_file), "%s", argv[1]);
  338. cmd_line_opts_start = 2;
  339. } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
  340. /* No command line flags specified. Look where binary lives */
  341. snprintf(config_file, sizeof(config_file)-1, "%s", CONFIG_FILE);
  342. config_file[sizeof(config_file)-1] = 0;
  343. } else {
  344. snprintf(config_file, sizeof(config_file)-1, "%.*s%c%s",
  345. (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
  346. config_file[sizeof(config_file)-1] = 0;
  347. }
  348. #ifdef CONFIG_FILE2
  349. fp = fopen(config_file, "r");
  350. /* try alternate config file */
  351. if (fp == NULL) {
  352. fp = fopen(CONFIG_FILE2, "r");
  353. if (fp != NULL) {
  354. strcpy(config_file, CONFIG_FILE2);
  355. }
  356. }
  357. if (fp != NULL) {
  358. fclose(fp);
  359. }
  360. #endif
  361. /* read all configurations from a config file */
  362. (void)read_config_file(config_file, options);
  363. /* If we're under MacOS and started by launchd, then the second
  364. argument is process serial number, -psn_.....
  365. In this case, don't process arguments at all. */
  366. if (argv[1] == NULL || memcmp(argv[1], "-psn_", 5) != 0) {
  367. /* Handle command line flags.
  368. They override config file and default settings. */
  369. for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
  370. if (argv[i][0] != '-' || argv[i + 1] == NULL) {
  371. show_usage_and_exit();
  372. }
  373. if (!set_option(options, &argv[i][1], argv[i + 1])) {
  374. printf("command line option is invalid, ignoring it:\n %s %s\n",
  375. argv[i], argv[i + 1]);
  376. }
  377. }
  378. }
  379. }
  380. static void init_server_name(int argc, const char *argv[])
  381. {
  382. int i;
  383. assert(sizeof(main_config_options)/sizeof(main_config_options[0]) == NUM_MAIN_OPTIONS+1);
  384. assert((strlen(mg_version())+12)<sizeof(server_base_name));
  385. snprintf(server_base_name, sizeof(server_base_name), "Civetweb V%s",
  386. mg_version());
  387. server_name = server_base_name;
  388. for (i=0; i<argc-1; i++) {
  389. if ((argv[i][0]=='-') && (0==strcmp(argv[i]+1, main_config_options[OPTION_TITLE].name))) {
  390. server_name = (char*)(argv[i+1]);
  391. }
  392. }
  393. icon_name = NULL;
  394. for (i=0; i<argc-1; i++) {
  395. if ((argv[i][0]=='-') && (0==strcmp(argv[i]+1, main_config_options[OPTION_ICON].name))) {
  396. icon_name = (char*)(argv[i+1]);
  397. }
  398. }
  399. }
  400. static int log_message(const struct mg_connection *conn, const char *message)
  401. {
  402. (void) conn;
  403. printf("%s\n", message);
  404. return 0;
  405. }
  406. static int is_path_absolute(const char *path)
  407. {
  408. #ifdef _WIN32
  409. return path != NULL &&
  410. ((path[0] == '\\' && path[1] == '\\') || /* UNC path, e.g.
  411. \\server\dir */
  412. (isalpha(path[0]) && path[1] == ':' && path[2] == '\\')); /* E.g. X:\dir */
  413. #else
  414. return path != NULL && path[0] == '/';
  415. #endif
  416. }
  417. static void verify_existence(char **options, const char *option_name,
  418. int must_be_dir)
  419. {
  420. struct stat st;
  421. const char *path = get_option(options, option_name);
  422. #ifdef _WIN32
  423. wchar_t wbuf[1024];
  424. char mbbuf[1024];
  425. int len;
  426. if (path) {
  427. memset(wbuf, 0, sizeof(wbuf));
  428. memset(mbbuf, 0, sizeof(mbbuf));
  429. len = MultiByteToWideChar(CP_UTF8, 0, path, -1, wbuf, (int) sizeof(wbuf)/sizeof(wbuf[0])-1);
  430. wcstombs(mbbuf, wbuf, sizeof(mbbuf)-1);
  431. path = mbbuf;
  432. }
  433. #endif
  434. if (path != NULL && (stat(path, &st) != 0 ||
  435. ((S_ISDIR(st.st_mode) ? 1 : 0) != must_be_dir))) {
  436. die("Invalid path for %s: [%s]: (%s). Make sure that path is either "
  437. "absolute, or it is relative to civetweb executable.",
  438. option_name, path, strerror(errno));
  439. }
  440. }
  441. static void set_absolute_path(char *options[], const char *option_name,
  442. const char *path_to_civetweb_exe)
  443. {
  444. char path[PATH_MAX] = "", abs[PATH_MAX] = "";
  445. const char *option_value;
  446. const char *p;
  447. /* Check whether option is already set */
  448. option_value = get_option(options, option_name);
  449. /* If option is already set and it is an absolute path,
  450. leave it as it is -- it's already absolute. */
  451. if (option_value != NULL && !is_path_absolute(option_value)) {
  452. /* Not absolute. Use the directory where civetweb executable lives
  453. be the relative directory for everything.
  454. Extract civetweb executable directory into path. */
  455. if ((p = strrchr(path_to_civetweb_exe, DIRSEP)) == NULL) {
  456. IGNORE_UNUSED_RESULT(getcwd(path, sizeof(path)));
  457. } else {
  458. snprintf(path, sizeof(path)-1, "%.*s", (int) (p - path_to_civetweb_exe),
  459. path_to_civetweb_exe);
  460. path[sizeof(path)-1] = 0;
  461. }
  462. strncat(path, "/", sizeof(path) - strlen(path) - 1);
  463. strncat(path, option_value, sizeof(path) - strlen(path) - 1);
  464. /* Absolutize the path, and set the option */
  465. IGNORE_UNUSED_RESULT(abs_path(path, abs, sizeof(abs)));
  466. set_option(options, option_name, abs);
  467. }
  468. }
  469. #ifdef USE_LUA
  470. #define main luatest_main
  471. #define luaL_openlibs lua_civet_openlibs
  472. struct lua_State;
  473. extern void lua_civet_openlibs(struct lua_State *L);
  474. #include "../src/third_party/lua-5.2.3/src/lua.c"
  475. #undef main
  476. #endif
  477. static void start_civetweb(int argc, char *argv[])
  478. {
  479. struct mg_callbacks callbacks;
  480. char *options[2*MAX_OPTIONS+1];
  481. int i;
  482. /* Edit passwords file, if -A option is specified */
  483. if (argc > 1 && !strcmp(argv[1], "-A")) {
  484. if (argc != 6) {
  485. show_usage_and_exit();
  486. }
  487. exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
  488. EXIT_SUCCESS : EXIT_FAILURE);
  489. }
  490. /* Call Lua with additional Civetweb specific Lua functions, if -L option is specified */
  491. if (argc > 1 && !strcmp(argv[1], "-L")) {
  492. #ifdef WIN32
  493. MakeConsole();
  494. #endif
  495. #ifdef USE_LUA
  496. exit(luatest_main(argc-1, &argv[1]));
  497. #endif
  498. exit(EXIT_FAILURE);
  499. }
  500. /* Show usage if -h or --help options are specified */
  501. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
  502. show_usage_and_exit();
  503. }
  504. options[0] = NULL;
  505. set_option(options, "document_root", ".");
  506. /* Update config based on command line arguments */
  507. process_command_line_arguments(argv, options);
  508. /* Make sure we have absolute paths for files and directories */
  509. set_absolute_path(options, "document_root", argv[0]);
  510. set_absolute_path(options, "put_delete_auth_file", argv[0]);
  511. set_absolute_path(options, "cgi_interpreter", argv[0]);
  512. set_absolute_path(options, "access_log_file", argv[0]);
  513. set_absolute_path(options, "error_log_file", argv[0]);
  514. set_absolute_path(options, "global_auth_file", argv[0]);
  515. #ifdef USE_LUA
  516. set_absolute_path(options, "lua_preload_file", argv[0]);
  517. #endif
  518. set_absolute_path(options, "ssl_certificate", argv[0]);
  519. /* Make extra verification for certain options */
  520. verify_existence(options, "document_root", 1);
  521. verify_existence(options, "cgi_interpreter", 0);
  522. verify_existence(options, "ssl_certificate", 0);
  523. #ifdef USE_LUA
  524. verify_existence(options, "lua_preload_file", 0);
  525. #endif
  526. /* Setup signal handler: quit on Ctrl-C */
  527. signal(SIGTERM, signal_handler);
  528. signal(SIGINT, signal_handler);
  529. /* Start Civetweb */
  530. memset(&callbacks, 0, sizeof(callbacks));
  531. callbacks.log_message = &log_message;
  532. ctx = mg_start(&callbacks, NULL, (const char **) options);
  533. for (i = 0; options[i] != NULL; i++) {
  534. free(options[i]);
  535. }
  536. if (ctx == NULL) {
  537. die("%s", "Failed to start Civetweb.");
  538. }
  539. }
  540. #ifdef _WIN32
  541. enum {
  542. ID_ICON = 100, ID_QUIT, ID_SETTINGS, ID_SEPARATOR, ID_INSTALL_SERVICE,
  543. ID_REMOVE_SERVICE, ID_STATIC, ID_GROUP, ID_PASSWORD,
  544. ID_SAVE, ID_RESET_DEFAULTS, ID_RESET_FILE, ID_RESET_ACTIVE,
  545. ID_STATUS, ID_CONNECT, ID_ADD_USER, ID_ADD_USER_NAME, ID_ADD_USER_REALM,
  546. ID_INPUT_LINE,
  547. /* All dynamically created text boxes for options have IDs starting from
  548. ID_CONTROLS, incremented by one. */
  549. ID_CONTROLS = 200,
  550. /* Text boxes for files have "..." buttons to open file browser. These
  551. buttons have IDs that are ID_FILE_BUTTONS_DELTA higher than associated
  552. text box ID. */
  553. ID_FILE_BUTTONS_DELTA = 1000
  554. };
  555. static HICON hIcon;
  556. static SERVICE_STATUS ss;
  557. static SERVICE_STATUS_HANDLE hStatus;
  558. static const char *service_magic_argument = "--";
  559. static NOTIFYICONDATA TrayIcon;
  560. static void WINAPI ControlHandler(DWORD code)
  561. {
  562. if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
  563. ss.dwWin32ExitCode = 0;
  564. ss.dwCurrentState = SERVICE_STOPPED;
  565. }
  566. SetServiceStatus(hStatus, &ss);
  567. }
  568. static void WINAPI ServiceMain(void)
  569. {
  570. ss.dwServiceType = SERVICE_WIN32;
  571. ss.dwCurrentState = SERVICE_RUNNING;
  572. ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  573. hStatus = RegisterServiceCtrlHandler(server_name, ControlHandler);
  574. SetServiceStatus(hStatus, &ss);
  575. while (ss.dwCurrentState == SERVICE_RUNNING) {
  576. Sleep(1000);
  577. }
  578. mg_stop(ctx);
  579. ss.dwCurrentState = SERVICE_STOPPED;
  580. ss.dwWin32ExitCode = (DWORD) -1;
  581. SetServiceStatus(hStatus, &ss);
  582. }
  583. static void show_error(void)
  584. {
  585. char buf[256];
  586. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  587. NULL, GetLastError(),
  588. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  589. buf, sizeof(buf), NULL);
  590. MessageBox(NULL, buf, "Error", MB_OK);
  591. }
  592. static void *align(void *ptr, DWORD alig)
  593. {
  594. ULONG ul = (ULONG) ptr;
  595. ul += alig;
  596. ul &= ~alig;
  597. return ((void *) ul);
  598. }
  599. static void save_config(HWND hDlg, FILE *fp)
  600. {
  601. char value[2000] = "";
  602. const char *default_value;
  603. const struct mg_option *options;
  604. int i, id;
  605. fprintf(fp, "%s", config_file_top_comment);
  606. options = mg_get_valid_options();
  607. for (i = 0; options[i].name != NULL; i++) {
  608. id = ID_CONTROLS + i;
  609. if (options[i].type == CONFIG_TYPE_BOOLEAN) {
  610. snprintf(value, sizeof(value)-1, "%s",
  611. IsDlgButtonChecked(hDlg, id) ? "yes" : "no");
  612. value[sizeof(value)-1] = 0;
  613. } else {
  614. GetDlgItemText(hDlg, id, value, sizeof(value));
  615. }
  616. default_value = options[i].default_value == NULL ? "" : options[i].default_value;
  617. /* If value is the same as default, skip it */
  618. if (strcmp(value, default_value) != 0) {
  619. fprintf(fp, "%s %s\n", options[i].name, value);
  620. }
  621. }
  622. }
  623. static BOOL CALLBACK SettingsDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP)
  624. {
  625. FILE *fp;
  626. int i, j;
  627. const char *name, *value;
  628. const struct mg_option *default_options = mg_get_valid_options();
  629. char *file_options[MAX_OPTIONS*2+1] = {0};
  630. char *title;
  631. switch (msg) {
  632. case WM_CLOSE:
  633. DestroyWindow(hDlg);
  634. break;
  635. case WM_COMMAND:
  636. switch (LOWORD(wParam)) {
  637. case ID_SAVE:
  638. EnableWindow(GetDlgItem(hDlg, ID_SAVE), FALSE);
  639. if ((fp = fopen(config_file, "w+")) != NULL) {
  640. save_config(hDlg, fp);
  641. fclose(fp);
  642. mg_stop(ctx);
  643. start_civetweb(__argc, __argv);
  644. }
  645. EnableWindow(GetDlgItem(hDlg, ID_SAVE), TRUE);
  646. break;
  647. case ID_RESET_DEFAULTS:
  648. for (i = 0; default_options[i].name != NULL; i++) {
  649. name = default_options[i].name;
  650. value = default_options[i].default_value == NULL ? "" : default_options[i].default_value;
  651. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  652. CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ?
  653. BST_CHECKED : BST_UNCHECKED);
  654. } else {
  655. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value);
  656. }
  657. }
  658. break;
  659. case ID_RESET_FILE:
  660. read_config_file(config_file, file_options);
  661. for (i = 0; default_options[i].name != NULL; i++) {
  662. name = default_options[i].name;
  663. value = default_options[i].default_value;
  664. for (j = 0; file_options[j * 2] != NULL; j++) {
  665. if (!strcmp(name, file_options[j * 2])) {
  666. value = file_options[j * 2 + 1];
  667. }
  668. }
  669. if (value == NULL) {
  670. value = "";
  671. }
  672. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  673. CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ? BST_CHECKED : BST_UNCHECKED);
  674. } else {
  675. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value);
  676. }
  677. }
  678. for (i = 0; i<MAX_OPTIONS; i++) {
  679. free(file_options[2*i]);
  680. free(file_options[2*i+1]);
  681. }
  682. break;
  683. case ID_RESET_ACTIVE:
  684. for (i = 0; default_options[i].name != NULL; i++) {
  685. name = default_options[i].name;
  686. value = mg_get_option(ctx, name);
  687. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  688. CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ?
  689. BST_CHECKED : BST_UNCHECKED);
  690. } else {
  691. SetDlgItemText(hDlg, ID_CONTROLS + i, value == NULL ? "" : value);
  692. }
  693. }
  694. break;
  695. }
  696. for (i = 0; default_options[i].name != NULL; i++) {
  697. name = default_options[i].name;
  698. if (((default_options[i].type == CONFIG_TYPE_FILE) || (default_options[i].type == CONFIG_TYPE_DIRECTORY)) &&
  699. LOWORD(wParam) == ID_CONTROLS + i + ID_FILE_BUTTONS_DELTA) {
  700. OPENFILENAME of;
  701. BROWSEINFO bi;
  702. char path[PATH_MAX] = "";
  703. memset(&of, 0, sizeof(of));
  704. of.lStructSize = sizeof(of);
  705. of.hwndOwner = (HWND) hDlg;
  706. of.lpstrFile = path;
  707. of.nMaxFile = sizeof(path);
  708. of.lpstrInitialDir = mg_get_option(ctx, "document_root");
  709. of.Flags = OFN_CREATEPROMPT | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  710. memset(&bi, 0, sizeof(bi));
  711. bi.hwndOwner = (HWND) hDlg;
  712. bi.lpszTitle = "Choose WWW root directory:";
  713. bi.ulFlags = BIF_RETURNONLYFSDIRS;
  714. if (default_options[i].type == CONFIG_TYPE_DIRECTORY) {
  715. SHGetPathFromIDList(SHBrowseForFolder(&bi), path);
  716. } else {
  717. GetOpenFileName(&of);
  718. }
  719. if (path[0] != '\0') {
  720. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), path);
  721. }
  722. }
  723. }
  724. break;
  725. case WM_INITDIALOG:
  726. SendMessage(hDlg, WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) hIcon);
  727. SendMessage(hDlg, WM_SETICON, (WPARAM) ICON_BIG, (LPARAM) hIcon);
  728. title = malloc(strlen(server_name)+16);
  729. if (title) {
  730. strcpy(title, server_name);
  731. strcat(title, " settings");
  732. SetWindowText(hDlg, title);
  733. free(title);
  734. }
  735. SetFocus(GetDlgItem(hDlg, ID_SAVE));
  736. /* Init dialog with active settings */
  737. SendMessage(hDlg, WM_COMMAND, ID_RESET_ACTIVE, 0);
  738. /* alternative: SendMessage(hDlg, WM_COMMAND, ID_RESET_FILE, 0); */
  739. break;
  740. default:
  741. break;
  742. }
  743. return FALSE;
  744. }
  745. struct tstring_input_buf {
  746. unsigned buflen;
  747. char * buffer;
  748. };
  749. static BOOL CALLBACK InputDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP)
  750. {
  751. static struct tstring_input_buf *inBuf = 0;
  752. WORD ctrlId;
  753. switch (msg) {
  754. case WM_CLOSE:
  755. inBuf = 0;
  756. DestroyWindow(hDlg);
  757. break;
  758. case WM_COMMAND:
  759. ctrlId = LOWORD(wParam);
  760. if (ctrlId == IDOK) {
  761. /* Add user */
  762. GetWindowText(GetDlgItem(hDlg, ID_INPUT_LINE), inBuf->buffer, inBuf->buflen);
  763. if (strlen(inBuf->buffer)>0) {
  764. EndDialog(hDlg, IDOK);
  765. }
  766. } else if (ctrlId == IDCANCEL) {
  767. EndDialog(hDlg, IDCANCEL);
  768. }
  769. break;
  770. case WM_INITDIALOG:
  771. inBuf = (struct tstring_input_buf *) lP;
  772. assert(inBuf != NULL);
  773. assert((inBuf->buffer != NULL) && (inBuf->buflen != 0));
  774. assert(strlen(inBuf->buffer) < inBuf->buflen);
  775. SendMessage(hDlg, WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) hIcon);
  776. SendMessage(hDlg, WM_SETICON, (WPARAM) ICON_BIG, (LPARAM) hIcon);
  777. SendDlgItemMessage(hDlg, ID_INPUT_LINE, EM_LIMITTEXT, inBuf->buflen-1, 0);
  778. SetWindowText(GetDlgItem(hDlg, ID_INPUT_LINE), inBuf->buffer);
  779. SetWindowText(hDlg, "Modify password");
  780. SetFocus(GetDlgItem(hDlg, ID_INPUT_LINE));
  781. break;
  782. default:
  783. break;
  784. }
  785. return FALSE;
  786. }
  787. void suggest_passwd(char *passwd)
  788. {
  789. unsigned u;
  790. char * p;
  791. union {
  792. FILETIME ft;
  793. LARGE_INTEGER li;
  794. } num;
  795. /* valid characters are 32 to 126 */
  796. GetSystemTimeAsFileTime(&num.ft);
  797. num.li.HighPart |= GetCurrentProcessId();
  798. p = passwd;
  799. while (num.li.QuadPart) {
  800. u = (unsigned)(num.li.QuadPart % 95);
  801. num.li.QuadPart -= u;
  802. num.li.QuadPart /= 95;
  803. *p = (char)(u+32);
  804. p++;
  805. }
  806. }
  807. static void add_control(unsigned char **mem, DLGTEMPLATE *dia, WORD type,
  808. DWORD id, DWORD style, WORD x, WORD y,
  809. WORD cx, WORD cy, const char *caption);
  810. static int get_password(const char * user, const char * realm, char * passwd, unsigned passwd_len)
  811. {
  812. #define HEIGHT 15
  813. #define WIDTH 280
  814. #define LABEL_WIDTH 90
  815. HWND hDlg = NULL;
  816. unsigned char mem[4096], *p;
  817. DLGTEMPLATE *dia = (DLGTEMPLATE *) mem;
  818. int ok, y;
  819. struct tstring_input_buf dlgprms = {passwd_len, passwd};
  820. static struct {
  821. DLGTEMPLATE template; /* 18 bytes */
  822. WORD menu, class;
  823. wchar_t caption[1];
  824. WORD fontsiz;
  825. wchar_t fontface[7];
  826. } dialog_header = {{
  827. WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE |
  828. DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW, 0, 200, 200, WIDTH, 0
  829. },
  830. 0, 0, L"", 8, L"Tahoma"
  831. };
  832. assert((user!=NULL) && (realm!=NULL) && (passwd!=NULL));
  833. if (guard < 100) {
  834. guard += 100;
  835. } else {
  836. return 0;
  837. }
  838. /* Create a password suggestion */
  839. memset(passwd, 0, passwd_len);
  840. suggest_passwd(passwd);
  841. /* Create the dialog */
  842. (void) memset(mem, 0, sizeof(mem));
  843. (void) memcpy(mem, &dialog_header, sizeof(dialog_header));
  844. p = mem + sizeof(dialog_header);
  845. y = HEIGHT;
  846. add_control(&p, dia, 0x82, ID_STATIC, WS_VISIBLE | WS_CHILD,
  847. 10, y, LABEL_WIDTH, HEIGHT, "User:");
  848. add_control(&p, dia, 0x81, ID_CONTROLS + 1,
  849. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_DISABLED,
  850. 15+LABEL_WIDTH, y, WIDTH - LABEL_WIDTH - 25, HEIGHT, user);
  851. y += HEIGHT;
  852. add_control(&p, dia, 0x82, ID_STATIC, WS_VISIBLE | WS_CHILD,
  853. 10, y, LABEL_WIDTH, HEIGHT, "Realm:");
  854. add_control(&p, dia, 0x81, ID_CONTROLS + 2,
  855. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_DISABLED,
  856. 15+LABEL_WIDTH, y, WIDTH - LABEL_WIDTH - 25, HEIGHT, realm);
  857. y += HEIGHT;
  858. add_control(&p, dia, 0x82, ID_STATIC, WS_VISIBLE | WS_CHILD,
  859. 10, y, LABEL_WIDTH, HEIGHT, "Password:");
  860. add_control(&p, dia, 0x81, ID_INPUT_LINE,
  861. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_TABSTOP,
  862. 15+LABEL_WIDTH, y, WIDTH - LABEL_WIDTH - 25, HEIGHT, "");
  863. y += (WORD)(HEIGHT * 2);
  864. add_control(&p, dia, 0x80, IDOK,
  865. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  866. 80, y, 55, 12, "Ok");
  867. add_control(&p, dia, 0x80, IDCANCEL,
  868. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  869. 140, y, 55, 12, "Cancel");
  870. assert((int)p - (int)mem < sizeof(mem));
  871. dia->cy = y + (WORD)(HEIGHT * 1.5);
  872. ok = (IDOK == DialogBoxIndirectParam(NULL, dia, NULL, InputDlgProc, (LPARAM) &dlgprms));
  873. guard -= 100;
  874. return ok;
  875. #undef HEIGHT
  876. #undef WIDTH
  877. #undef LABEL_WIDTH
  878. }
  879. static BOOL CALLBACK PasswordDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP)
  880. {
  881. static const char *passfile = 0;
  882. char domain[256], user[256], password[256];
  883. WORD ctrlId;
  884. switch (msg) {
  885. case WM_CLOSE:
  886. passfile = 0;
  887. DestroyWindow(hDlg);
  888. break;
  889. case WM_COMMAND:
  890. ctrlId = LOWORD(wParam);
  891. if (ctrlId == ID_ADD_USER) {
  892. /* Add user */
  893. GetWindowText(GetDlgItem(hDlg, ID_ADD_USER_NAME), user, sizeof(user));
  894. GetWindowText(GetDlgItem(hDlg, ID_ADD_USER_REALM), domain, sizeof(domain));
  895. if (get_password(user, domain, password, sizeof(password))) {
  896. mg_modify_passwords_file(passfile, domain, user, password);
  897. EndDialog(hDlg, IDOK);
  898. }
  899. } else if ((ctrlId>=(ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 3)) &&
  900. (ctrlId<(ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 4))) {
  901. /* Modify password */
  902. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA * 3), user, sizeof(user));
  903. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA * 2), domain, sizeof(domain));
  904. if (get_password(user, domain, password, sizeof(password))) {
  905. mg_modify_passwords_file(passfile, domain, user, password);
  906. EndDialog(hDlg, IDOK);
  907. }
  908. } else if ((ctrlId>=(ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 2)) &&
  909. (ctrlId<(ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 3))) {
  910. /* Remove user */
  911. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA * 2), user, sizeof(user));
  912. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA), domain, sizeof(domain));
  913. mg_modify_passwords_file(passfile, domain, user, NULL);
  914. EndDialog(hDlg, IDOK);
  915. }
  916. break;
  917. case WM_INITDIALOG:
  918. passfile = (const char *)lP;
  919. SendMessage(hDlg, WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) hIcon);
  920. SendMessage(hDlg, WM_SETICON, (WPARAM) ICON_BIG, (LPARAM) hIcon);
  921. SetWindowText(hDlg, passfile);
  922. SetFocus(GetDlgItem(hDlg, ID_ADD_USER_NAME));
  923. break;
  924. default:
  925. break;
  926. }
  927. return FALSE;
  928. }
  929. static void add_control(unsigned char **mem, DLGTEMPLATE *dia, WORD type,
  930. DWORD id, DWORD style, WORD x, WORD y,
  931. WORD cx, WORD cy, const char *caption)
  932. {
  933. DLGITEMTEMPLATE *tp;
  934. LPWORD p;
  935. dia->cdit++;
  936. *mem = align(*mem, 3);
  937. tp = (DLGITEMTEMPLATE *) *mem;
  938. tp->id = (WORD)id;
  939. tp->style = style;
  940. tp->dwExtendedStyle = 0;
  941. tp->x = x;
  942. tp->y = y;
  943. tp->cx = cx;
  944. tp->cy = cy;
  945. p = align(*mem + sizeof(*tp), 1);
  946. *p++ = 0xffff;
  947. *p++ = type;
  948. while (*caption != '\0') {
  949. *p++ = (WCHAR) *caption++;
  950. }
  951. *p++ = 0;
  952. p = align(p, 1);
  953. *p++ = 0;
  954. *mem = (unsigned char *) p;
  955. }
  956. static void show_settings_dialog()
  957. {
  958. #define HEIGHT 15
  959. #define WIDTH 460
  960. #define LABEL_WIDTH 90
  961. unsigned char mem[4096], *p;
  962. const struct mg_option *options;
  963. DWORD style;
  964. DLGTEMPLATE *dia = (DLGTEMPLATE *) mem;
  965. WORD i, cl, x, y, width, nelems = 0;
  966. static struct {
  967. DLGTEMPLATE template; /* 18 bytes */
  968. WORD menu, class;
  969. wchar_t caption[1];
  970. WORD fontsiz;
  971. wchar_t fontface[7];
  972. } dialog_header = {{
  973. WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE |
  974. DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW, 0, 200, 200, WIDTH, 0
  975. },
  976. 0, 0, L"", 8, L"Tahoma"
  977. };
  978. if (guard == 0) {
  979. guard++;
  980. } else {
  981. return;
  982. }
  983. (void) memset(mem, 0, sizeof(mem));
  984. (void) memcpy(mem, &dialog_header, sizeof(dialog_header));
  985. p = mem + sizeof(dialog_header);
  986. options = mg_get_valid_options();
  987. for (i = 0; options[i].name != NULL; i++) {
  988. style = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
  989. x = 10 + (WIDTH / 2) * (nelems % 2);
  990. y = (nelems/2 + 1) * HEIGHT + 5;
  991. width = WIDTH / 2 - 20 - LABEL_WIDTH;
  992. if (options[i].type == CONFIG_TYPE_NUMBER) {
  993. style |= ES_NUMBER;
  994. cl = 0x81;
  995. style |= WS_BORDER | ES_AUTOHSCROLL;
  996. } else if (options[i].type == CONFIG_TYPE_BOOLEAN) {
  997. cl = 0x80;
  998. style |= BS_AUTOCHECKBOX;
  999. } else if ((options[i].type == CONFIG_TYPE_FILE) ||
  1000. (options[i].type == CONFIG_TYPE_DIRECTORY)) {
  1001. style |= WS_BORDER | ES_AUTOHSCROLL;
  1002. width -= 20;
  1003. cl = 0x81;
  1004. add_control(&p, dia, 0x80,
  1005. ID_CONTROLS + i + ID_FILE_BUTTONS_DELTA,
  1006. WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  1007. (WORD) (x + width + LABEL_WIDTH + 5),
  1008. y, 15, 12, "...");
  1009. } else {
  1010. cl = 0x81;
  1011. style |= WS_BORDER | ES_AUTOHSCROLL;
  1012. }
  1013. add_control(&p, dia, 0x82, ID_STATIC, WS_VISIBLE | WS_CHILD,
  1014. x, y, LABEL_WIDTH, HEIGHT, options[i].name);
  1015. add_control(&p, dia, cl, ID_CONTROLS + i, style,
  1016. (WORD) (x + LABEL_WIDTH), y, width, 12, "");
  1017. nelems++;
  1018. assert((int)p - (int)mem < sizeof(mem));
  1019. }
  1020. y = (WORD) (((nelems + 1) / 2 + 1) * HEIGHT + 5);
  1021. add_control(&p, dia, 0x80, ID_GROUP, WS_CHILD | WS_VISIBLE |
  1022. BS_GROUPBOX, 5, 5, WIDTH - 10, y, " Settings ");
  1023. y += 10;
  1024. add_control(&p, dia, 0x80, ID_SAVE,
  1025. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1026. WIDTH - 70, y, 65, 12, "Save Settings");
  1027. add_control(&p, dia, 0x80, ID_RESET_DEFAULTS,
  1028. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1029. WIDTH - 140, y, 65, 12, "Reset to defaults");
  1030. add_control(&p, dia, 0x80, ID_RESET_FILE,
  1031. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1032. WIDTH - 210, y, 65, 12, "Reload from file");
  1033. add_control(&p, dia, 0x80, ID_RESET_ACTIVE,
  1034. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1035. WIDTH - 280, y, 65, 12, "Reload active");
  1036. add_control(&p, dia, 0x82, ID_STATIC,
  1037. WS_CHILD | WS_VISIBLE | WS_DISABLED,
  1038. 5, y, 100, 12, server_base_name);
  1039. assert((int)p - (int)mem < sizeof(mem));
  1040. dia->cy = ((nelems + 1) / 2 + 1) * HEIGHT + 30;
  1041. DialogBoxIndirectParam(NULL, dia, NULL, SettingsDlgProc, (LPARAM) NULL);
  1042. guard--;
  1043. #undef HEIGHT
  1044. #undef WIDTH
  1045. #undef LABEL_WIDTH
  1046. }
  1047. static void change_password_file()
  1048. {
  1049. #define HEIGHT 15
  1050. #define WIDTH 320
  1051. #define LABEL_WIDTH 90
  1052. OPENFILENAME of;
  1053. char path[PATH_MAX] = PASSWORDS_FILE_NAME;
  1054. char strbuf[256], u[256], d[256];
  1055. HWND hDlg = NULL;
  1056. FILE * f;
  1057. int y, nelems;
  1058. unsigned char mem[4096], *p;
  1059. DLGTEMPLATE *dia = (DLGTEMPLATE *) mem;
  1060. const char * domain = mg_get_option(ctx, "authentication_domain");
  1061. static struct {
  1062. DLGTEMPLATE template; /* 18 bytes */
  1063. WORD menu, class;
  1064. wchar_t caption[1];
  1065. WORD fontsiz;
  1066. wchar_t fontface[7];
  1067. } dialog_header = {{
  1068. WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE |
  1069. DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW, 0, 200, 200, WIDTH, 0
  1070. },
  1071. 0, 0, L"", 8, L"Tahoma"
  1072. };
  1073. if (guard == 0) {
  1074. guard++;
  1075. } else {
  1076. return;
  1077. }
  1078. memset(&of, 0, sizeof(of));
  1079. of.lStructSize = sizeof(of);
  1080. of.hwndOwner = (HWND) hDlg;
  1081. of.lpstrFile = path;
  1082. of.nMaxFile = sizeof(path);
  1083. of.lpstrInitialDir = mg_get_option(ctx, "document_root");
  1084. of.Flags = OFN_CREATEPROMPT | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  1085. if (IDOK != GetSaveFileName(&of)) {
  1086. guard--;
  1087. return;
  1088. }
  1089. f = fopen(path, "a+");
  1090. if (f) {
  1091. fclose(f);
  1092. } else {
  1093. MessageBox(NULL, path, "Can not open file", MB_ICONERROR);
  1094. guard--;
  1095. return;
  1096. }
  1097. do {
  1098. (void) memset(mem, 0, sizeof(mem));
  1099. (void) memcpy(mem, &dialog_header, sizeof(dialog_header));
  1100. p = mem + sizeof(dialog_header);
  1101. f = fopen(path, "r+");
  1102. if (!f) {
  1103. MessageBox(NULL, path, "Can not open file", MB_ICONERROR);
  1104. guard--;
  1105. return;
  1106. }
  1107. nelems = 0;
  1108. while (fgets(strbuf, sizeof(strbuf), f)) {
  1109. if (sscanf(strbuf, "%255[^:]:%255[^:]:%*s", u, d) != 2) {
  1110. continue;
  1111. }
  1112. u[255]=0;
  1113. d[255]=0;
  1114. y = (nelems + 1) * HEIGHT + 5;
  1115. add_control(&p, dia, 0x80, ID_CONTROLS + nelems + ID_FILE_BUTTONS_DELTA * 3,
  1116. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1117. 10, y, 65, 12, "Modify password");
  1118. add_control(&p, dia, 0x80, ID_CONTROLS + nelems + ID_FILE_BUTTONS_DELTA * 2,
  1119. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1120. 80, y, 55, 12, "Remove user");
  1121. add_control(&p, dia, 0x81, ID_CONTROLS + nelems + ID_FILE_BUTTONS_DELTA,
  1122. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_DISABLED,
  1123. 245, y, 60, 12, d);
  1124. add_control(&p, dia, 0x81, ID_CONTROLS + nelems,
  1125. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_DISABLED,
  1126. 140, y, 100, 12, u);
  1127. nelems++;
  1128. assert((int)p - (int)mem < sizeof(mem));
  1129. }
  1130. fclose(f);
  1131. y = (WORD) ((nelems + 1) * HEIGHT + 10);
  1132. add_control(&p, dia, 0x80, ID_ADD_USER,
  1133. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1134. 80, y, 55, 12, "Add user");
  1135. add_control(&p, dia, 0x81, ID_ADD_USER_NAME,
  1136. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_TABSTOP,
  1137. 140, y, 100, 12, "");
  1138. add_control(&p, dia, 0x81, ID_ADD_USER_REALM,
  1139. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_TABSTOP,
  1140. 245, y, 60, 12, domain);
  1141. y = (WORD) ((nelems + 2) * HEIGHT + 10);
  1142. add_control(&p, dia, 0x80, ID_GROUP, WS_CHILD | WS_VISIBLE |
  1143. BS_GROUPBOX, 5, 5, WIDTH - 10, y, " Users ");
  1144. y += HEIGHT;
  1145. add_control(&p, dia, 0x82, ID_STATIC,
  1146. WS_CHILD | WS_VISIBLE | WS_DISABLED,
  1147. 5, y, 100, 12, server_base_name);
  1148. assert((int)p - (int)mem < sizeof(mem));
  1149. dia->cy = y + 20;
  1150. } while ((IDOK == DialogBoxIndirectParam(NULL, dia, NULL, PasswordDlgProc, (LPARAM) path)) && (!exit_flag));
  1151. guard--;
  1152. #undef HEIGHT
  1153. #undef WIDTH
  1154. #undef LABEL_WIDTH
  1155. }
  1156. static int manage_service(int action)
  1157. {
  1158. static const char *service_name = "Civetweb"; /* TODO: check using server_name instead of service_name */
  1159. SC_HANDLE hSCM = NULL, hService = NULL;
  1160. SERVICE_DESCRIPTION descr = {server_name};
  1161. char path[PATH_MAX + 20] = "";/* Path to executable plus magic argument */
  1162. int success = 1;
  1163. if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
  1164. GENERIC_WRITE : GENERIC_READ)) == NULL) {
  1165. success = 0;
  1166. show_error();
  1167. } else if (action == ID_INSTALL_SERVICE) {
  1168. path[sizeof(path)-1] = 0;
  1169. GetModuleFileName(NULL, path, sizeof(path)-1);
  1170. strncat(path, " ", sizeof(path)-1);
  1171. strncat(path, service_magic_argument, sizeof(path)-1);
  1172. hService = CreateService(hSCM, service_name, service_name,
  1173. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  1174. SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
  1175. path, NULL, NULL, NULL, NULL, NULL);
  1176. if (hService) {
  1177. ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
  1178. } else {
  1179. show_error();
  1180. }
  1181. } else if (action == ID_REMOVE_SERVICE) {
  1182. if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
  1183. !DeleteService(hService)) {
  1184. show_error();
  1185. }
  1186. } else if ((hService = OpenService(hSCM, service_name,
  1187. SERVICE_QUERY_STATUS)) == NULL) {
  1188. success = 0;
  1189. }
  1190. if (hService)
  1191. CloseServiceHandle(hService);
  1192. if (hSCM)
  1193. CloseServiceHandle(hSCM);
  1194. return success;
  1195. }
  1196. static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
  1197. LPARAM lParam)
  1198. {
  1199. static SERVICE_TABLE_ENTRY service_table[2] = {0};
  1200. int service_installed;
  1201. char buf[200], *service_argv[] = {__argv[0], NULL};
  1202. POINT pt;
  1203. HMENU hMenu;
  1204. static UINT s_uTaskbarRestart; /* for taskbar creation */
  1205. service_table[0].lpServiceName = server_name;
  1206. service_table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
  1207. switch (msg) {
  1208. case WM_CREATE:
  1209. if (__argv[1] != NULL &&
  1210. !strcmp(__argv[1], service_magic_argument)) {
  1211. start_civetweb(1, service_argv);
  1212. StartServiceCtrlDispatcher(service_table);
  1213. exit(EXIT_SUCCESS);
  1214. } else {
  1215. start_civetweb(__argc, __argv);
  1216. s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
  1217. }
  1218. break;
  1219. case WM_COMMAND:
  1220. switch (LOWORD(wParam)) {
  1221. case ID_QUIT:
  1222. mg_stop(ctx);
  1223. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  1224. exit_flag = 1;
  1225. PostQuitMessage(0);
  1226. return 0;
  1227. case ID_SETTINGS:
  1228. show_settings_dialog();
  1229. break;
  1230. case ID_PASSWORD:
  1231. change_password_file();
  1232. break;
  1233. case ID_INSTALL_SERVICE:
  1234. case ID_REMOVE_SERVICE:
  1235. manage_service(LOWORD(wParam));
  1236. break;
  1237. case ID_CONNECT:
  1238. printf("[%s]\n", get_url_to_first_open_port(ctx));
  1239. ShellExecute(NULL, "open", get_url_to_first_open_port(ctx),
  1240. NULL, NULL, SW_SHOW);
  1241. break;
  1242. }
  1243. break;
  1244. case WM_USER:
  1245. switch (lParam) {
  1246. case WM_RBUTTONUP:
  1247. case WM_LBUTTONUP:
  1248. case WM_LBUTTONDBLCLK:
  1249. hMenu = CreatePopupMenu();
  1250. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name);
  1251. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  1252. service_installed = manage_service(0);
  1253. snprintf(buf, sizeof(buf)-1, "NT service: %s installed",
  1254. service_installed ? "" : "not");
  1255. buf[sizeof(buf)-1] = 0;
  1256. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf);
  1257. AppendMenu(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0),
  1258. ID_INSTALL_SERVICE, "Install service");
  1259. AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0),
  1260. ID_REMOVE_SERVICE, "Deinstall service");
  1261. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  1262. AppendMenu(hMenu, MF_STRING, ID_CONNECT, "Start browser");
  1263. AppendMenu(hMenu, MF_STRING, ID_SETTINGS, "Edit settings");
  1264. AppendMenu(hMenu, MF_STRING, ID_PASSWORD, "Modify password file");
  1265. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  1266. AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit");
  1267. GetCursorPos(&pt);
  1268. SetForegroundWindow(hWnd);
  1269. TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
  1270. PostMessage(hWnd, WM_NULL, 0, 0);
  1271. DestroyMenu(hMenu);
  1272. break;
  1273. }
  1274. break;
  1275. case WM_CLOSE:
  1276. mg_stop(ctx);
  1277. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  1278. exit_flag = 1;
  1279. PostQuitMessage(0);
  1280. return 0;/* We've just sent our own quit message, with proper hwnd. */
  1281. default:
  1282. if (msg==s_uTaskbarRestart)
  1283. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  1284. }
  1285. return DefWindowProc(hWnd, msg, wParam, lParam);
  1286. }
  1287. #include <fcntl.h>
  1288. #include <io.h>
  1289. static int MakeConsole() {
  1290. DWORD err;
  1291. int ok = (GetConsoleWindow() != NULL);
  1292. if (!ok) {
  1293. if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
  1294. FreeConsole();
  1295. if (!AllocConsole()) {
  1296. err = GetLastError();
  1297. if (err==ERROR_ACCESS_DENIED) {
  1298. MessageBox(NULL, "Insufficient rights to create a console window", "Error", MB_ICONERROR);
  1299. }
  1300. }
  1301. AttachConsole(GetCurrentProcessId());
  1302. }
  1303. ok = (GetConsoleWindow() != NULL);
  1304. if (ok) {
  1305. freopen("CONIN$", "r", stdin);
  1306. freopen("CONOUT$", "w", stdout);
  1307. freopen("CONOUT$", "w", stderr);
  1308. }
  1309. }
  1310. if (ok) {
  1311. SetConsoleTitle(server_name);
  1312. }
  1313. return ok;
  1314. }
  1315. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show)
  1316. {
  1317. WNDCLASS cls;
  1318. HWND hWnd;
  1319. MSG msg;
  1320. init_server_name(__argc, __argv);
  1321. memset(&cls, 0, sizeof(cls));
  1322. cls.lpfnWndProc = (WNDPROC) WindowProc;
  1323. cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  1324. cls.lpszClassName = server_base_name;
  1325. RegisterClass(&cls);
  1326. hWnd = CreateWindow(cls.lpszClassName, server_name, WS_OVERLAPPEDWINDOW,
  1327. 0, 0, 0, 0, NULL, NULL, NULL, NULL);
  1328. ShowWindow(hWnd, SW_HIDE);
  1329. if (icon_name) {
  1330. hIcon = LoadImage(NULL, icon_name, IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
  1331. } else {
  1332. hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON), IMAGE_ICON, 16, 16, 0);
  1333. }
  1334. TrayIcon.cbSize = sizeof(TrayIcon);
  1335. TrayIcon.uID = ID_ICON;
  1336. TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  1337. TrayIcon.hIcon = hIcon;
  1338. TrayIcon.hWnd = hWnd;
  1339. snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name);
  1340. TrayIcon.uCallbackMessage = WM_USER;
  1341. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  1342. while (GetMessage(&msg, hWnd, 0, 0) > 0) {
  1343. TranslateMessage(&msg);
  1344. DispatchMessage(&msg);
  1345. }
  1346. /* Return the WM_QUIT value. */
  1347. return (int) msg.wParam;
  1348. }
  1349. #if defined(CONSOLE)
  1350. void main(void)
  1351. {
  1352. WinMain(0, 0, 0, 0);
  1353. }
  1354. #endif
  1355. #elif defined(USE_COCOA)
  1356. #import <Cocoa/Cocoa.h>
  1357. @interface Civetweb :
  1358. NSObject<NSApplicationDelegate>
  1359. - (void) openBrowser;
  1360. - (void) shutDown;
  1361. @end
  1362. @implementation Civetweb
  1363. - (void) openBrowser {
  1364. [[NSWorkspace sharedWorkspace]
  1365. openURL:[NSURL URLWithString:
  1366. [NSString stringWithUTF8String:get_url_to_first_open_port(ctx)]]];
  1367. }
  1368. - (void) editConfig {
  1369. create_config_file(config_file);
  1370. [[NSWorkspace sharedWorkspace]
  1371. openFile:[NSString stringWithUTF8String:config_file]
  1372. withApplication:@"TextEdit"];
  1373. }
  1374. - (void)shutDown {
  1375. [NSApp terminate:nil];
  1376. }
  1377. @end
  1378. int main(int argc, char *argv[])
  1379. {
  1380. init_server_name(argc, (const char **)argv);
  1381. start_civetweb(argc, argv);
  1382. [NSAutoreleasePool new];
  1383. [NSApplication sharedApplication];
  1384. /* Add delegate to process menu item actions */
  1385. Civetweb *myDelegate = [[Civetweb alloc] autorelease];
  1386. [NSApp setDelegate: myDelegate];
  1387. /* Run this app as agent */
  1388. ProcessSerialNumber psn = { 0, kCurrentProcess };
  1389. TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
  1390. SetFrontProcess(&psn);
  1391. /* Add status bar menu */
  1392. id menu = [[NSMenu new] autorelease];
  1393. /* Add version menu item */
  1394. [menu addItem:[[[NSMenuItem alloc]
  1395. /*initWithTitle:[NSString stringWithFormat:@"%s", server_name]*/
  1396. initWithTitle:[NSString stringWithUTF8String:server_name]
  1397. action:@selector(noexist) keyEquivalent:@""] autorelease]];
  1398. /* Add configuration menu item */
  1399. [menu addItem:[[[NSMenuItem alloc]
  1400. initWithTitle:@"Edit configuration"
  1401. action:@selector(editConfig) keyEquivalent:@""] autorelease]];
  1402. /* Add connect menu item */
  1403. [menu addItem:[[[NSMenuItem alloc]
  1404. initWithTitle:@"Open web root in a browser"
  1405. action:@selector(openBrowser) keyEquivalent:@""] autorelease]];
  1406. /* Separator */
  1407. [menu addItem:[NSMenuItem separatorItem]];
  1408. /* Add quit menu item */
  1409. [menu addItem:[[[NSMenuItem alloc]
  1410. initWithTitle:@"Quit"
  1411. action:@selector(shutDown) keyEquivalent:@"q"] autorelease]];
  1412. /* Attach menu to the status bar */
  1413. id item = [[[NSStatusBar systemStatusBar]
  1414. statusItemWithLength:NSVariableStatusItemLength] retain];
  1415. [item setHighlightMode:YES];
  1416. [item setImage:[NSImage imageNamed:@"civetweb_22x22.png"]];
  1417. [item setMenu:menu];
  1418. /* Run the app */
  1419. [NSApp activateIgnoringOtherApps:YES];
  1420. [NSApp run];
  1421. mg_stop(ctx);
  1422. return EXIT_SUCCESS;
  1423. }
  1424. #else
  1425. int main(int argc, char *argv[])
  1426. {
  1427. init_server_name(argc, (const char **)argv);
  1428. start_civetweb(argc, argv);
  1429. printf("%s started on port(s) %s with web root [%s]\n",
  1430. server_name, mg_get_option(ctx, "listening_ports"),
  1431. mg_get_option(ctx, "document_root"));
  1432. while (exit_flag == 0) {
  1433. sleep(1);
  1434. }
  1435. printf("Exiting on signal %d, waiting for all threads to finish...",
  1436. exit_flag);
  1437. fflush(stdout);
  1438. mg_stop(ctx);
  1439. printf("%s", " done.\n");
  1440. return EXIT_SUCCESS;
  1441. }
  1442. #endif /* _WIN32 */