From 3d78b14deba04f4ac81220f7b1ae6dc5cc1031b6 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 2 Nov 2021 16:01:06 +0000 Subject: [PATCH 1/4] Fix #1396 - JS sorting code for dir listing --- mongoose.c | 13 +++++++------ src/http.c | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/mongoose.c b/mongoose.c index 0143aaff..3dcfe7ab 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1322,20 +1322,21 @@ static void printdirentry(const char *name, void *userdata) { struct printdirentrydata *d = (struct printdirentrydata *) userdata; struct mg_fs *fs = d->opts->fs == NULL ? &mg_fs_posix : d->opts->fs; size_t size = 0; - time_t mtime = 0; + time_t t = 0; char path[MG_PATH_MAX], sz[64], mod[64]; int flags, n = 0; // LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name)); if (snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) < 0) { LOG(LL_ERROR, ("%s truncated", name)); - } else if ((flags = fs->stat(path, &size, &mtime)) == 0) { + } else if ((flags = fs->stat(path, &size, &t)) == 0) { LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno)); } else { const char *slash = flags & MG_FS_DIR ? "/" : ""; - struct tm t; + struct tm tm; if (flags & MG_FS_DIR) { snprintf(sz, sizeof(sz), "%s", "[DIR]"); + size = -1; } else if (size < 1024) { snprintf(sz, sizeof(sz), "%d", (int) size); } else if (size < 0x100000) { @@ -1345,12 +1346,12 @@ static void printdirentry(const char *name, void *userdata) { } else { snprintf(sz, sizeof(sz), "%.1fG", (double) size / 1073741824); } - strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&mtime, &t)); + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&t, &tm)); n = (int) mg_url_encode(name, strlen(name), path, sizeof(path)); mg_printf(d->c, " %s%s" - "%s%s\n", - n, path, slash, name, slash, mod, sz); + "%s%s\n", + n, path, slash, name, slash, (unsigned long) t, mod, size, sz); } } diff --git a/src/http.c b/src/http.c index 6b63e98b..4b5070a4 100644 --- a/src/http.c +++ b/src/http.c @@ -573,20 +573,21 @@ static void printdirentry(const char *name, void *userdata) { struct printdirentrydata *d = (struct printdirentrydata *) userdata; struct mg_fs *fs = d->opts->fs == NULL ? &mg_fs_posix : d->opts->fs; size_t size = 0; - time_t mtime = 0; + time_t t = 0; char path[MG_PATH_MAX], sz[64], mod[64]; int flags, n = 0; // LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name)); if (snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) < 0) { LOG(LL_ERROR, ("%s truncated", name)); - } else if ((flags = fs->stat(path, &size, &mtime)) == 0) { + } else if ((flags = fs->stat(path, &size, &t)) == 0) { LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno)); } else { const char *slash = flags & MG_FS_DIR ? "/" : ""; - struct tm t; + struct tm tm; if (flags & MG_FS_DIR) { snprintf(sz, sizeof(sz), "%s", "[DIR]"); + size = -1; } else if (size < 1024) { snprintf(sz, sizeof(sz), "%d", (int) size); } else if (size < 0x100000) { @@ -596,12 +597,12 @@ static void printdirentry(const char *name, void *userdata) { } else { snprintf(sz, sizeof(sz), "%.1fG", (double) size / 1073741824); } - strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&mtime, &t)); + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&t, &tm)); n = (int) mg_url_encode(name, strlen(name), path, sizeof(path)); mg_printf(d->c, " %s%s" - "%s%s\n", - n, path, slash, name, slash, mod, sz); + "%s%s\n", + n, path, slash, name, slash, (unsigned long) t, mod, size, sz); } } From ba02937ec670a97091a0fe6184f2a9b3caef4a42 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 2 Nov 2021 16:40:25 +0000 Subject: [PATCH 2/4] Squash warnings --- examples/mqtt-over-ws-client/main.c | 2 +- mongoose.c | 4 ++-- src/http.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/mqtt-over-ws-client/main.c b/examples/mqtt-over-ws-client/main.c index ee2ff535..032f285f 100644 --- a/examples/mqtt-over-ws-client/main.c +++ b/examples/mqtt-over-ws-client/main.c @@ -32,7 +32,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { .will_topic = mg_str(s_topic), .will_message = mg_str("goodbye")}; size_t len = c->send.len; - mg_mqtt_login(c, s_url, &opts); + mg_mqtt_login(c, &opts); mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY); } else if (ev == MG_EV_WS_MSG) { struct mg_mqtt_message mm; diff --git a/mongoose.c b/mongoose.c index 3dcfe7ab..b7f0355d 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1336,7 +1336,6 @@ static void printdirentry(const char *name, void *userdata) { struct tm tm; if (flags & MG_FS_DIR) { snprintf(sz, sizeof(sz), "%s", "[DIR]"); - size = -1; } else if (size < 1024) { snprintf(sz, sizeof(sz), "%d", (int) size); } else if (size < 0x100000) { @@ -1351,7 +1350,8 @@ static void printdirentry(const char *name, void *userdata) { mg_printf(d->c, " %s%s" "%s%s\n", - n, path, slash, name, slash, (unsigned long) t, mod, size, sz); + n, path, slash, name, slash, (unsigned long) t, mod, + flags & MG_FS_DIR ? (int64_t) -1 : (int64_t) size, sz); } } diff --git a/src/http.c b/src/http.c index 4b5070a4..f65478d7 100644 --- a/src/http.c +++ b/src/http.c @@ -587,7 +587,6 @@ static void printdirentry(const char *name, void *userdata) { struct tm tm; if (flags & MG_FS_DIR) { snprintf(sz, sizeof(sz), "%s", "[DIR]"); - size = -1; } else if (size < 1024) { snprintf(sz, sizeof(sz), "%d", (int) size); } else if (size < 0x100000) { @@ -602,7 +601,8 @@ static void printdirentry(const char *name, void *userdata) { mg_printf(d->c, " %s%s" "%s%s\n", - n, path, slash, name, slash, (unsigned long) t, mod, size, sz); + n, path, slash, name, slash, (unsigned long) t, mod, + flags & MG_FS_DIR ? (int64_t) -1 : (int64_t) size, sz); } } From 09d28f55707750338d31f8f7856c5018f3fb535a Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 2 Nov 2021 16:53:57 +0000 Subject: [PATCH 3/4] Print a clarifying message at start --- examples/websocket-server/main.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/websocket-server/main.c b/examples/websocket-server/main.c index 392f4a51..f61dee2c 100644 --- a/examples/websocket-server/main.c +++ b/examples/websocket-server/main.c @@ -8,15 +8,17 @@ #include "mongoose.h" -static const char *s_listen_on = "http://localhost:8000"; -static const char *s_web_directory = "."; +static const char *s_listen_on = "ws://localhost:8000"; +static const char *s_web_root = "."; // This RESTful server implements the following endpoints: // /websocket - upgrade to Websocket, and implement websocket echo server // /api/rest - respond with JSON string {"result": 123} -// any other URI serves static files from s_web_directory +// any other URI serves static files from s_web_root static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_HTTP_MSG) { + if (ev == MG_EV_OPEN) { + // c->is_hexdumping = 1; + } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/websocket")) { // Upgrade to websocket. From now on, a connection is a full-duplex @@ -27,7 +29,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123); } else { // Serve static files - struct mg_http_serve_opts opts = {.root_dir = s_web_directory}; + struct mg_http_serve_opts opts = {.root_dir = s_web_root}; mg_http_serve_dir(c, ev_data, &opts); } } else if (ev == MG_EV_WS_MSG) { @@ -40,8 +42,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } int main(void) { - struct mg_mgr mgr; // Event manager - mg_mgr_init(&mgr); // Initialise event manager + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Initialise event manager + printf("Starting WS listener on %s/websocket\n", s_listen_on); mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop mg_mgr_free(&mgr); From c3d237e9c62c1f917b3158ba23cf2ce3c01c9c86 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 2 Nov 2021 22:43:23 +0000 Subject: [PATCH 4/4] More verbose log --- examples/http-server/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/http-server/main.c b/examples/http-server/main.c index b25e0b5f..f5a8ba96 100644 --- a/examples/http-server/main.c +++ b/examples/http-server/main.c @@ -76,7 +76,9 @@ int main(int argc, char *argv[]) { if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1; // Start infinite event loop - LOG(LL_INFO, ("Starting Mongoose v%s, serving [%s]", MG_VERSION, s_root_dir)); + LOG(LL_INFO, ("Mongoose version : v%s", MG_VERSION)); + LOG(LL_INFO, ("Listening on : %s", s_listening_address)); + LOG(LL_INFO, ("Web root : [%s]", s_root_dir)); while (s_signo == 0) mg_mgr_poll(&mgr, 1000); mg_mgr_free(&mgr); LOG(LL_INFO, ("Exiting on signal %d", s_signo));