mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-01 13:10:43 +00:00
Restore to a simple version
This commit is contained in:
1 parent
d83e547fa9
commit
89784ab90a
5 files changed
+26
-247
No files matched your search
+24
-100
@@ -9,9 +9,6 @@ static const char *s_listening_address = "http://localhost:8000";
|
||||
static const char *s_enable_hexdump = "no";
|
||||
static const char *s_ssi_pattern = "#.shtml";
|
||||
|
||||
static struct mg_mgr s_mgr;
|
||||
static struct mg_timer s_timer;
|
||||
|
||||
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_serve_opts opts = {s_root_dir, s_ssi_pattern};
|
||||
@@ -34,114 +31,41 @@ static void usage(const char *prog) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static void cfg(const char *progname, const char *optname, const char *value) {
|
||||
if (value == NULL) {
|
||||
usage(progname);
|
||||
} else if (strcmp(optname, "-D") == 0) {
|
||||
s_debug_level = value;
|
||||
} else if (strcmp(optname, "-H") == 0) {
|
||||
s_enable_hexdump = value;
|
||||
} else if (strcmp(optname, "-S") == 0) {
|
||||
s_ssi_pattern = value;
|
||||
} else if (strcmp(optname, "-d") == 0) {
|
||||
s_root_dir = value;
|
||||
} else if (strcmp(optname, "-l") == 0) {
|
||||
s_listening_address = value;
|
||||
} else {
|
||||
usage(progname);
|
||||
}
|
||||
LOG(LL_INFO, ("Setting option [%s] to [%s]", optname, value));
|
||||
}
|
||||
|
||||
static FILE *opencfg(const char *exe, const char *mode) {
|
||||
char path[1024];
|
||||
const char *p = strrchr(exe, MG_DIRSEP);
|
||||
if (p == NULL) p = strrchr(exe, '/');
|
||||
snprintf(path, sizeof(path), "%.*s%c%s", p == NULL ? 1 : (int) (p - exe),
|
||||
p == NULL ? "." : exe, MG_DIRSEP, "mongoose.conf");
|
||||
return fopen(path, mode);
|
||||
}
|
||||
|
||||
static void parse_config_file(const char *exe) {
|
||||
FILE *fp = opencfg(exe, "rb");
|
||||
if (fp != NULL) {
|
||||
char line[8192], *name;
|
||||
int i, line_no = 0;
|
||||
LOG(LL_INFO, ("Processing config file..."));
|
||||
while (fgets(line, sizeof(line), fp) != NULL) {
|
||||
line_no++;
|
||||
// Trim whitespaces at the end
|
||||
for (i = strlen(line); i > 0 && isspace(line[i - 1]);) line[--i] = '\0';
|
||||
// Ignore empty lines and comments
|
||||
for (i = 0; isspace(line[i]);) i++;
|
||||
if (line[i] == '#' || line[i] == '\0') continue;
|
||||
name = &line[i];
|
||||
while (line[i] != '\0' && !isspace(line[i])) i++;
|
||||
if (line[i] != ' ') {
|
||||
LOG(LL_ERROR, ("Ignoring invalid line %d: %s", line_no, line));
|
||||
} else {
|
||||
line[i++] = '\0'; // nul-terminate name
|
||||
while (isspace(line[i])) i++;
|
||||
cfg(exe, name, strdup(&line[i]));
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
static void ccb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = ev_data;
|
||||
LOG(LL_DEBUG, ("New version: %.*s", (int) hm->body.len, hm->body.ptr));
|
||||
c->is_closing = 1;
|
||||
}
|
||||
(void) fn_data;
|
||||
}
|
||||
|
||||
// Check latest version, alert user if upgrade is available
|
||||
static void timer_fn(void *arg) {
|
||||
struct mg_connection *c =
|
||||
mg_http_connect(&s_mgr, "http://cesanta.com", ccb, NULL);
|
||||
if (c != NULL)
|
||||
mg_printf(c, "%s",
|
||||
"GET /downloads/mws/version.json HTTP/1.0\r\n"
|
||||
"Host: cesanta.com\r\n"
|
||||
"\r\n");
|
||||
(void) arg;
|
||||
}
|
||||
|
||||
static void init_mongoose(int argc, char *argv[]) {
|
||||
int main(int argc, char *argv[]) {
|
||||
struct mg_mgr mgr;
|
||||
struct mg_connection *c;
|
||||
int i;
|
||||
|
||||
// Configure options from a command-line flags and configuration file
|
||||
for (i = 1; i < argc; i += 2) cfg(argv[0], argv[i], argv[i + 1]);
|
||||
parse_config_file(argv[0]);
|
||||
// Parse command-line flags
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-d") == 0) {
|
||||
s_root_dir = argv[++i];
|
||||
} else if (strcmp(argv[i], "-D") == 0) {
|
||||
s_debug_level = argv[++i];
|
||||
} else if (strcmp(argv[i], "-H") == 0) {
|
||||
s_enable_hexdump = argv[++i];
|
||||
} else if (strcmp(argv[i], "-S") == 0) {
|
||||
s_ssi_pattern = argv[++i];
|
||||
} else if (strcmp(argv[i], "-l") == 0) {
|
||||
s_listening_address = argv[++i];
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise stuff
|
||||
mg_log_set(s_debug_level);
|
||||
mg_mgr_init(&s_mgr);
|
||||
if ((c = mg_http_listen(&s_mgr, s_listening_address, cb, &s_mgr)) == NULL) {
|
||||
mg_mgr_init(&mgr);
|
||||
if ((c = mg_http_listen(&mgr, s_listening_address, cb, &mgr)) == NULL) {
|
||||
LOG(LL_ERROR, ("Cannot listen on %s. Use http://ADDR:PORT or :PORT",
|
||||
s_listening_address));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
mg_timer_init(&s_timer, 3600000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn,
|
||||
&s_mgr);
|
||||
if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1;
|
||||
s_root_dir = realpath(s_root_dir, NULL);
|
||||
if (s_root_dir == NULL) s_root_dir = ".";
|
||||
LOG(LL_INFO, ("Starting Mongoose v%s, serving %s", MG_VERSION, s_root_dir));
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "win32/winmain.c"
|
||||
#else
|
||||
int main(int argc, char *argv[]) {
|
||||
init_mongoose(argc, argv);
|
||||
for (;;) mg_mgr_poll(&s_mgr, 1000);
|
||||
mg_timer_free(&s_timer);
|
||||
mg_mgr_free(&s_mgr);
|
||||
// Start infinite event loop
|
||||
LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION));
|
||||
for (;;) mg_mgr_poll(&mgr, 1000);
|
||||
mg_mgr_free(&mgr);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user