2020-12-05 19:26:32 +08:00
|
|
|
// Copyright (c) 2020 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
|
2021-03-22 19:30:48 +08:00
|
|
|
#include <signal.h>
|
2020-12-05 19:26:32 +08:00
|
|
|
#include "mongoose.h"
|
|
|
|
|
2022-08-01 18:19:32 +08:00
|
|
|
static int s_debug_level = MG_LL_INFO;
|
2022-02-11 01:11:03 +08:00
|
|
|
static const char *s_root_dir = ".";
|
2022-01-23 01:03:27 +08:00
|
|
|
static const char *s_listening_address = "http://0.0.0.0:8000";
|
2020-12-05 19:26:32 +08:00
|
|
|
static const char *s_enable_hexdump = "no";
|
2021-12-21 00:35:16 +08:00
|
|
|
static const char *s_ssi_pattern = "#.html";
|
2024-02-20 19:30:06 +08:00
|
|
|
static const char *s_upload_dir = NULL; // File uploads disabled by default
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2021-03-22 19:30:48 +08:00
|
|
|
// Handle interrupts, like Ctrl-C
|
|
|
|
static int s_signo;
|
|
|
|
static void signal_handler(int signo) {
|
|
|
|
s_signo = signo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event handler for the listening connection.
|
|
|
|
// Simply serve static files from `s_root_dir`
|
2024-01-09 04:34:34 +08:00
|
|
|
static void cb(struct mg_connection *c, int ev, void *ev_data) {
|
2020-12-05 19:26:32 +08:00
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
2024-02-20 19:30:06 +08:00
|
|
|
struct mg_http_message *hm = ev_data;
|
|
|
|
|
|
|
|
if (mg_match(hm->uri, mg_str("/upload"), NULL)) {
|
|
|
|
// Serve file upload
|
|
|
|
if (s_upload_dir == NULL) {
|
|
|
|
mg_http_reply(c, 403, "", "Denied: file upload directory not set\n");
|
|
|
|
} else {
|
|
|
|
struct mg_http_part part;
|
|
|
|
size_t pos = 0, total_bytes = 0, num_files = 0;
|
|
|
|
while ((pos = mg_http_next_multipart(hm->body, pos, &part)) > 0) {
|
|
|
|
char path[MG_PATH_MAX];
|
|
|
|
MG_INFO(("Chunk name: [%.*s] filename: [%.*s] length: %lu bytes",
|
2024-03-15 15:42:24 +08:00
|
|
|
part.name.len, part.name.buf, part.filename.len,
|
|
|
|
part.filename.buf, part.body.len));
|
2024-02-20 19:30:06 +08:00
|
|
|
mg_snprintf(path, sizeof(path), "%s/%.*s", s_upload_dir,
|
2024-03-15 15:42:24 +08:00
|
|
|
part.filename.len, part.filename.buf);
|
2024-04-22 22:27:22 +08:00
|
|
|
if (mg_path_is_sane(mg_str(path))) {
|
2024-03-15 15:42:24 +08:00
|
|
|
mg_file_write(&mg_fs_posix, path, part.body.buf, part.body.len);
|
2024-02-20 19:30:06 +08:00
|
|
|
total_bytes += part.body.len;
|
|
|
|
num_files++;
|
|
|
|
} else {
|
|
|
|
MG_ERROR(("Rejecting dangerous path %s", path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mg_http_reply(c, 200, "", "Uploaded %lu files, %lu bytes\n", num_files,
|
|
|
|
total_bytes);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Serve web root directory
|
|
|
|
struct mg_http_serve_opts opts = {0};
|
|
|
|
opts.root_dir = s_root_dir;
|
|
|
|
opts.ssi_pattern = s_ssi_pattern;
|
|
|
|
mg_http_serve_dir(c, hm, &opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log request
|
2024-03-15 15:42:24 +08:00
|
|
|
MG_INFO(("%.*s %.*s %lu -> %.*s %lu", hm->method.len, hm->method.buf,
|
|
|
|
hm->uri.len, hm->uri.buf, hm->body.len, 3, c->send.buf + 9,
|
2024-02-20 19:30:06 +08:00
|
|
|
c->send.len));
|
2020-12-05 19:26:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char *prog) {
|
|
|
|
fprintf(stderr,
|
2021-07-14 11:00:27 +08:00
|
|
|
"Mongoose v.%s\n"
|
|
|
|
"Usage: %s OPTIONS\n"
|
2020-12-05 19:26:32 +08:00
|
|
|
" -H yes|no - enable traffic hexdump, default: '%s'\n"
|
2021-12-21 00:35:16 +08:00
|
|
|
" -S PAT - SSI filename pattern, default: '%s'\n"
|
2020-12-05 19:26:32 +08:00
|
|
|
" -d DIR - directory to serve, default: '%s'\n"
|
2021-01-25 18:35:14 +08:00
|
|
|
" -l ADDR - listening address, default: '%s'\n"
|
2024-02-20 19:30:06 +08:00
|
|
|
" -u DIR - file upload directory, default: unset\n"
|
2022-08-01 18:19:32 +08:00
|
|
|
" -v LEVEL - debug level, from 0 to 4, default: %d\n",
|
2021-12-21 00:35:16 +08:00
|
|
|
MG_VERSION, prog, s_enable_hexdump, s_ssi_pattern, s_root_dir,
|
|
|
|
s_listening_address, s_debug_level);
|
2020-12-05 19:26:32 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2021-01-19 22:21:50 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2021-12-23 02:05:03 +08:00
|
|
|
char path[MG_PATH_MAX] = ".";
|
2021-01-19 22:21:50 +08:00
|
|
|
struct mg_mgr mgr;
|
2020-12-05 19:26:32 +08:00
|
|
|
struct mg_connection *c;
|
|
|
|
int i;
|
|
|
|
|
2021-01-19 22:21:50 +08:00
|
|
|
// Parse command-line flags
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-d") == 0) {
|
2022-02-01 19:10:01 +08:00
|
|
|
s_root_dir = argv[++i];
|
2021-01-19 22:21:50 +08:00
|
|
|
} else if (strcmp(argv[i], "-H") == 0) {
|
|
|
|
s_enable_hexdump = argv[++i];
|
2021-12-21 00:35:16 +08:00
|
|
|
} else if (strcmp(argv[i], "-S") == 0) {
|
|
|
|
s_ssi_pattern = argv[++i];
|
2021-01-19 22:21:50 +08:00
|
|
|
} else if (strcmp(argv[i], "-l") == 0) {
|
|
|
|
s_listening_address = argv[++i];
|
2024-02-20 19:30:06 +08:00
|
|
|
} else if (strcmp(argv[i], "-u") == 0) {
|
|
|
|
s_upload_dir = argv[++i];
|
2021-01-25 18:35:14 +08:00
|
|
|
} else if (strcmp(argv[i], "-v") == 0) {
|
2022-08-01 18:19:32 +08:00
|
|
|
s_debug_level = atoi(argv[++i]);
|
2021-01-19 22:21:50 +08:00
|
|
|
} else {
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2021-12-23 02:05:03 +08:00
|
|
|
// Root directory must not contain double dots. Make it absolute
|
2022-02-01 19:10:01 +08:00
|
|
|
// Do the conversion only if the root dir spec does not contain overrides
|
|
|
|
if (strchr(s_root_dir, ',') == NULL) {
|
|
|
|
realpath(s_root_dir, path);
|
|
|
|
s_root_dir = path;
|
|
|
|
}
|
2021-12-23 02:05:03 +08:00
|
|
|
|
2020-12-05 19:26:32 +08:00
|
|
|
// Initialise stuff
|
2021-03-22 19:30:48 +08:00
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
2020-12-05 19:26:32 +08:00
|
|
|
mg_log_set(s_debug_level);
|
2021-01-19 22:21:50 +08:00
|
|
|
mg_mgr_init(&mgr);
|
|
|
|
if ((c = mg_http_listen(&mgr, s_listening_address, cb, &mgr)) == NULL) {
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_ERROR(("Cannot listen on %s. Use http://ADDR:PORT or :PORT",
|
|
|
|
s_listening_address));
|
2020-12-05 19:26:32 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1;
|
|
|
|
|
2021-01-19 22:21:50 +08:00
|
|
|
// Start infinite event loop
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_INFO(("Mongoose version : v%s", MG_VERSION));
|
|
|
|
MG_INFO(("Listening on : %s", s_listening_address));
|
|
|
|
MG_INFO(("Web root : [%s]", s_root_dir));
|
2024-02-20 19:30:06 +08:00
|
|
|
MG_INFO(("Upload dir : [%s]", s_upload_dir ? s_upload_dir : "unset"));
|
2021-03-22 19:30:48 +08:00
|
|
|
while (s_signo == 0) mg_mgr_poll(&mgr, 1000);
|
2021-01-19 22:21:50 +08:00
|
|
|
mg_mgr_free(&mgr);
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_INFO(("Exiting on signal %d", s_signo));
|
2020-12-05 19:26:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|