|
| 1 | +// Copyright (c) 2024 Cesanta Software Limited |
| 2 | +// All rights reserved |
| 3 | + |
| 4 | +#include <signal.h> |
| 5 | +#include "mongoose.h" |
| 6 | + |
| 7 | +static int s_debug_level = MG_LL_INFO; |
| 8 | +static int s_max_size = 10000; |
| 9 | +static const char *s_root_dir = "web_root"; |
| 10 | +static const char *s_upld_dir = "upload"; |
| 11 | +static const char *s_listening_address = "http://0.0.0.0:8090"; |
| 12 | +static const char *s_user = "user"; |
| 13 | +static const char *s_pass = "pass"; |
| 14 | + |
| 15 | +// Handle interrupts, like Ctrl-C |
| 16 | +static int s_signo; |
| 17 | +static void signal_handler(int signo) { |
| 18 | + s_signo = signo; |
| 19 | +} |
| 20 | + |
| 21 | +static bool authuser(struct mg_http_message *hm) { |
| 22 | + char user[256], pass[256]; |
| 23 | + mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass)); |
| 24 | + if (strcmp(user, s_user) == 0 && strcmp(pass, s_pass) == 0) return true; |
| 25 | + return false; |
| 26 | +} |
| 27 | + |
| 28 | +// Streaming upload example. Demonstrates how to use MG_EV_READ events |
| 29 | +// to get large payload in smaller chunks. To test, use curl utility: |
| 30 | +static void cb(struct mg_connection *c, int ev, void *ev_data) { |
| 31 | + if (ev == MG_EV_READ) { |
| 32 | + // Parse the incoming data ourselves. If we can parse the request, |
| 33 | + // store two size_t variables in c->data: expected len and recv len. |
| 34 | + size_t *data = (size_t *) c->data; |
| 35 | + struct mg_fd *fd = (struct mg_fd *) c->fn_data; // store file descriptor |
| 36 | + if (data[0]) { // Already parsed, receiving body |
| 37 | + data[1] += c->recv.len; |
| 38 | + MG_DEBUG(("Got chunk len %lu, %lu total", c->recv.len, data[1])); |
| 39 | + fd->fs->wr(fd->fd, c->recv.buf, c->recv.len); |
| 40 | + c->recv.len = 0; // And cleanup the receive buffer. Streaming! |
| 41 | + if (data[1] >= data[0]) { |
| 42 | + mg_fs_close(fd); |
| 43 | + mg_http_reply(c, 200, "", "ok\n"); |
| 44 | + } |
| 45 | + } else { |
| 46 | + struct mg_http_message hm; |
| 47 | + int n = mg_http_parse((char *) c->recv.buf, c->recv.len, &hm); |
| 48 | + if (n < 0) mg_error(c, "Bad response"); |
| 49 | + if (n > 0) { |
| 50 | + if (mg_http_match_uri(&hm, "/upload/#")) { |
| 51 | + if (!authuser(&hm)) { |
| 52 | + mg_http_reply(c, 403, "", "Denied\n"); |
| 53 | + } else if (hm.body.len > (size_t) s_max_size) { |
| 54 | + mg_http_reply(c, 400, "", "Too long\n"); |
| 55 | + } else { |
| 56 | + char fpath[MG_PATH_MAX]; |
| 57 | + char *fname = strndup(hm.uri.ptr + 8, hm.uri.len - 8); // 8: /upload/ |
| 58 | + if (fname[0] == '\0') { |
| 59 | + mg_http_reply(c, 400, "", "Name required\n"); |
| 60 | + } else if (!mg_path_is_sane(fname)) { |
| 61 | + mg_http_reply(c, 400, "", "Invalid path\n"); |
| 62 | + } else { |
| 63 | + snprintf(fpath, MG_PATH_MAX, "%s%c%s", s_upld_dir, MG_DIRSEP, fname); |
| 64 | + MG_DEBUG(("Got request, chunk len %lu", c->recv.len - n)); |
| 65 | + if ((fd = mg_fs_open(&mg_fs_posix, fpath, MG_FS_WRITE)) == NULL) { |
| 66 | + mg_http_reply(c, 400, "", "open failed: %d", errno); |
| 67 | + } else { |
| 68 | + c->fn_data = fd; |
| 69 | + c->recv.len -= n; // remove headers |
| 70 | + data[0] = hm.body.len; |
| 71 | + data[1] = c->recv.len; |
| 72 | + if (c->recv.len) fd->fs->wr(fd->fd, c->recv.buf + n, c->recv.len); |
| 73 | + if (data[1] >= data[0]) { |
| 74 | + mg_fs_close(fd); |
| 75 | + mg_http_reply(c, 200, "", "ok\n"); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + free(fname); |
| 80 | + } |
| 81 | + } else { |
| 82 | + struct mg_http_serve_opts opts = {0}; |
| 83 | + opts.root_dir = s_root_dir; |
| 84 | + mg_http_serve_dir(c, &hm, &opts); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + (void) ev_data; |
| 90 | +} |
| 91 | + |
| 92 | +static void usage(const char *prog) { |
| 93 | + fprintf(stderr, |
| 94 | + "File Transfer server based on Mongoose v.%s\n" |
| 95 | + "Usage: %s OPTIONS\n" |
| 96 | + " -u NAME - user name, default: '%s'\n" |
| 97 | + " -p PWD - password, default: '%s'\n" |
| 98 | + " -d DIR - directory to serve, default: '%s'\n" |
| 99 | + " -D DIR - directory to store uploads, default: '%s'\n" |
| 100 | + " -s SIZE - maximum allowed file size, default: '%d'\n" |
| 101 | + " -l ADDR - listening address, default: '%s'\n" |
| 102 | + " -v LEVEL - debug level, from 0 to 4, default: %d\n", |
| 103 | + MG_VERSION, prog, s_user, s_pass, s_root_dir, s_upld_dir, s_max_size, |
| 104 | + s_listening_address, s_debug_level); |
| 105 | + exit(EXIT_FAILURE); |
| 106 | +} |
| 107 | + |
| 108 | +int main(int argc, char *argv[]) { |
| 109 | + char spath[MG_PATH_MAX] = "."; |
| 110 | + char upath[MG_PATH_MAX] = "."; |
| 111 | + struct mg_mgr mgr; |
| 112 | + int i; |
| 113 | + |
| 114 | + // Parse command-line flags |
| 115 | + for (i = 1; i < argc; i++) { |
| 116 | + if (strcmp(argv[i], "-d") == 0) { |
| 117 | + s_root_dir = argv[++i]; |
| 118 | + } else if (strcmp(argv[i], "-D") == 0) { |
| 119 | + s_upld_dir = argv[++i]; |
| 120 | + } else if (strcmp(argv[i], "-u") == 0) { |
| 121 | + s_user = argv[++i]; |
| 122 | + } else if (strcmp(argv[i], "-p") == 0) { |
| 123 | + s_pass = argv[++i]; |
| 124 | + } else if (strcmp(argv[i], "-l") == 0) { |
| 125 | + s_listening_address = argv[++i]; |
| 126 | + } else if (strcmp(argv[i], "-v") == 0) { |
| 127 | + s_debug_level = atoi(argv[++i]); |
| 128 | + } else if (strcmp(argv[i], "-s") == 0) { |
| 129 | + s_max_size = atoi(argv[++i]); |
| 130 | + } else { |
| 131 | + usage(argv[0]); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Root directory must not contain double dots. Make it absolute |
| 136 | + // Do the conversion only if the root dir spec does not contain overrides |
| 137 | + if (strchr(s_root_dir, ',') == NULL) { |
| 138 | + realpath(s_root_dir, spath); |
| 139 | + s_root_dir = spath; |
| 140 | + } |
| 141 | + if (strchr(s_upld_dir, ',') == NULL) { |
| 142 | + realpath(s_upld_dir, upath); |
| 143 | + s_upld_dir = upath; |
| 144 | + } |
| 145 | + |
| 146 | + // Initialise stuff |
| 147 | + signal(SIGINT, signal_handler); |
| 148 | + signal(SIGTERM, signal_handler); |
| 149 | + mg_log_set(s_debug_level); |
| 150 | + mg_mgr_init(&mgr); |
| 151 | + if (mg_http_listen(&mgr, s_listening_address, cb, NULL) == NULL) { |
| 152 | + MG_ERROR(("Cannot listen on %s.", s_listening_address)); |
| 153 | + exit(EXIT_FAILURE); |
| 154 | + } |
| 155 | + |
| 156 | + // Start infinite event loop |
| 157 | + MG_INFO(("Mongoose version : v%s", MG_VERSION)); |
| 158 | + MG_INFO(("Listening on : %s", s_listening_address)); |
| 159 | + MG_INFO(("Web root : [%s]", s_root_dir)); |
| 160 | + MG_INFO(("Uploading to : [%s]", s_upld_dir)); |
| 161 | + while (s_signo == 0) mg_mgr_poll(&mgr, 1000); |
| 162 | + mg_mgr_free(&mgr); |
| 163 | + MG_INFO(("Exiting on signal %d", s_signo)); |
| 164 | + return 0; |
| 165 | +} |
0 commit comments