4 #undef UNICODE // Use ANSI WinAPI functions 5 #undef _UNICODE // Use multibyte encoding on Windows 6 #define _MBCS // Use multibyte encoding on Windows 7 #define _WIN32_WINNT 0x500 // Enable MIIM_BITMAP 8 #define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005 9 #define _XOPEN_SOURCE 600 // For PATH_MAX on linux 10 #undef WIN32_LEAN_AND_MEAN // Let windows.h always include winsock2.h 33 #define PATH_MAX MAX_PATH 37 #define S_ISDIR(x) ((x) & _S_IFDIR) 41 #define snprintf _snprintf 42 #define vsnprintf _vsnprintf 43 #define sleep(x) Sleep((x) * 1000) 44 #define abs_path(rel, abs, abs_size) _fullpath((abs), (rel), (abs_size)) 47 #define stat(x, y) _stat((x), (y)) 59 #define abs_path(rel, abs, abs_size) realpath((rel), (abs)) 62 #define MAX_OPTIONS 100 63 #define MAX_CONF_FILE_LINE_SIZE (8 * 1024) 66 #define MVER MONGOOSE_VERSION 79 #if !defined(CONFIG_FILE) 80 #define CONFIG_FILE "mongoose.conf" 91 if (sig_num == SIGCHLD) {
92 do {}
while (waitpid(-1, &sig_num, WNOHANG) > 0);
98 static void vnotify(
const char *fmt, va_list ap,
int must_exit) {
106 static void notify(
const char *fmt, ...) {
113 static void die(
const char *fmt, ...) {
124 fprintf(stderr,
"Mongoose version %s (c) Sergey Lyubka, built on %s\n",
127 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM) 128 fprintf(stderr,
" mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
130 fprintf(stderr,
" mongoose [config_file]\n");
131 fprintf(stderr,
" mongoose [-option value ...]\n");
132 fprintf(stderr,
"\nOPTIONS:\n");
135 for (i = 0; names[
i] != NULL; i += 2) {
137 names[i], names[i + 1] == NULL ?
"<empty>" : names[i + 1]);
142 #define EV_HANDLER NULL 146 if ((p = (
char *)
malloc(strlen(str) + 1)) != NULL) {
156 if (options[i] == NULL) {
157 options[
i] =
sdup(name);
158 options[i + 1] =
sdup(value);
159 options[i + 2] = NULL;
161 }
else if (!strcmp(options[i], name)) {
162 free(options[i + 1]);
163 options[i + 1] =
sdup(value);
168 if (i == MAX_OPTIONS - 3) {
169 die(
"%s",
"Too many options specified");
177 size_t i, cmd_line_opts_start = 1, line_no = 0;
180 if (argv[1] != NULL && argv[1][0] !=
'-') {
181 snprintf(cpath,
sizeof(cpath),
"%s", argv[1]);
182 cmd_line_opts_start = 2;
183 }
else if ((p = strrchr(argv[0],
DIRSEP)) == NULL) {
187 snprintf(cpath,
sizeof(cpath),
"%.*s%c%s",
195 if (cmd_line_opts_start == 2 && fp == NULL) {
204 while (fgets(line,
sizeof(line), fp) != NULL) {
208 for (i = 0; isspace(* (
unsigned char *) &line[i]); ) i++;
209 if (line[i] ==
'#' || line[i] ==
'\0') {
213 if (sscanf(line,
"%s %[^\r\n#]", opt, val) != 2) {
214 printf(
"%s: line %d is invalid, ignoring it:\n %s",
227 if (argv[1] == NULL || memcmp(argv[1],
"-psn_", 5) != 0) {
230 for (i = cmd_line_opts_start; argv[
i] != NULL; i += 2) {
231 if (argv[i][0] !=
'-' || argv[i + 1] == NULL) {
234 set_option(options, &argv[i][1], argv[i + 1]);
240 const char *descr =
"";
247 return path != NULL &&
248 ((path[0] ==
'\\' && path[1] ==
'\\') ||
249 (isalpha(path[0]) && path[1] ==
':' && path[2] ==
'\\'));
251 return path != NULL && path[0] ==
'/';
255 static char *
get_option(
char **options,
const char *option_name) {
258 for (i = 0; options[
i] != NULL; i++)
259 if (!strcmp(options[i], option_name))
260 return options[i + 1];
275 return path == NULL || (
stat(path, &st) == 0 &&
276 ((S_ISDIR(st.st_mode) ? 1 : 0) == is_dir));
282 notify(
"Invalid path for %s: [%s]: (%s). Make sure that path is either " 283 "absolute, or it is relative to mongoose executable.",
284 name, path, strerror(errno));
289 char path[PATH_MAX],
abs[PATH_MAX], *option_value;
293 option_value =
get_option(options, option_name);
302 getcwd(path,
sizeof(path));
308 strncat(path,
"/",
sizeof(path) - 1);
309 strncat(path, option_value,
sizeof(path) - 1);
317 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM) 319 const char *user,
const char *pass) {
321 char line[512], u[512],
d[512], ha1[33], tmp[PATH_MAX];
328 if (pass != NULL && pass[0] ==
'\0') {
332 (void)
snprintf(tmp,
sizeof(tmp),
"%s.tmp", fname);
335 if ((fp = fopen(fname,
"a+")) != NULL) {
340 if ((fp = fopen(fname,
"r")) == NULL) {
342 }
else if ((fp2 = fopen(tmp,
"w+")) == NULL) {
348 while (fgets(line,
sizeof(line), fp) != NULL) {
349 if (sscanf(line,
"%[^:]:%[^:]:%*s", u, d) != 2) {
353 if (!strcmp(u, user) && !strcmp(d, domain)) {
356 mg_md5(ha1, user,
":", domain,
":", pass, NULL);
357 fprintf(fp2,
"%s:%s:%s\n", user, domain, ha1);
365 if (!found && pass != NULL) {
366 mg_md5(ha1, user,
":", domain,
":", pass, NULL);
367 fprintf(fp2,
"%s:%s:%s\n", user, domain, ha1);
385 die(
"%s",
"Failed to start Mongoose.");
388 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM) 390 if (argc > 1 && !strcmp(argv[1],
"-A")) {
395 EXIT_SUCCESS : EXIT_FAILURE);
400 if (argc == 2 && (!strcmp(argv[1],
"-h") || !strcmp(argv[1],
"--help"))) {
429 notify(
"Setting document_root to [%s]",
438 for (i = 0; options[
i] != NULL; i += 2) {
439 const char *msg =
mg_set_option(server, options[i], options[i + 1]);
441 notify(
"Failed to set option [%s] to [%s]: %s",
442 options[i], options[i + 1], msg);
443 if (!strcmp(options[i],
"listening_port")) {
449 free(options[i + 1]);
459 const char *env =
getenv(
"MONGOOSE_LISTENING_SOCKET");
460 if (env != NULL && atoi(env) > 0 ) {
461 mg_set_listening_socket(server, atoi(env));
474 int main(
int argc,
char *argv[]) {
477 printf(
"%s serving [%s] on port %s\n",
static void die(const char *fmt,...)
static void __cdecl signal_handler(int sig_num)
int stat(const char *path, struct stat *buffer)
static const char * s_default_listening_port
static void set_absolute_path(char *options[], const char *option_name)
static char server_name[50]
char * mg_md5(char buf[33],...)
struct mg_server * mg_create_server(void *server_data, mg_handler_t handler)
const char * mg_get_option(const struct mg_server *server, const char *name)
static void show_usage_and_exit(void)
const char ** mg_get_valid_option_names(void)
static int path_exists(const char *path, int is_dir)
static void process_command_line_arguments(char *argv[], char **options)
static char s_config_file[PATH_MAX]
static int is_path_absolute(const char *path)
time_t mg_poll_server(struct mg_server *server, int milliseconds)
#define MAX_CONF_FILE_LINE_SIZE
static void verify_existence(char **options, const char *name, int is_dir)
static void start_mongoose(int argc, char *argv[])
static void set_options(char *argv[])
static struct mg_server * server
static void init_server_name(void)
static void * serving_thread_func(void *param)
INLINE Rall1d< T, V, S > abs(const Rall1d< T, V, S > &x)
#define abs_path(rel, abs, abs_size)
static void vnotify(const char *fmt, va_list ap, int must_exit)
const char * mg_set_option(struct mg_server *server, const char *name, const char *value)
UNITTEST_START int * value
int main(int argc, char *argv[])
static char * sdup(const char *str)
int modify_passwords_file(const char *fname, const char *domain, const char *user, const char *pass)
static void set_option(char **options, const char *name, const char *value)
void mg_destroy_server(struct mg_server **server)
static void notify(const char *fmt,...)
static const char * s_default_document_root
static char * get_option(char **options, const char *option_name)