Merge branch 'master' into dec

This commit is contained in:
Sergey Lyubka 2021-11-02 22:43:59 +00:00
commit a6efd0b335
5 changed files with 28 additions and 21 deletions

View File

@ -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));

View File

@ -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;

View File

@ -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);

View File

@ -1322,18 +1322,18 @@ 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]");
} else if (size < 1024) {
@ -1345,12 +1345,13 @@ 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,
" <tr><td><a href=\"%.*s%s\">%s%s</a></td>"
"<td>%s</td><td>%s</td></tr>\n",
n, path, slash, name, slash, mod, sz);
"<td name=%lu>%s</td><td name=" MG_INT64_FMT ">%s</td></tr>\n",
n, path, slash, name, slash, (unsigned long) t, mod,
flags & MG_FS_DIR ? (int64_t) -1 : (int64_t) size, sz);
}
}

View File

@ -573,18 +573,18 @@ 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]");
} else if (size < 1024) {
@ -596,12 +596,13 @@ 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,
" <tr><td><a href=\"%.*s%s\">%s%s</a></td>"
"<td>%s</td><td>%s</td></tr>\n",
n, path, slash, name, slash, mod, sz);
"<td name=%lu>%s</td><td name=" MG_INT64_FMT ">%s</td></tr>\n",
n, path, slash, name, slash, (unsigned long) t, mod,
flags & MG_FS_DIR ? (int64_t) -1 : (int64_t) size, sz);
}
}