mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 15:01:03 +08:00
Fix ESP32 example
This commit is contained in:
parent
0ed64707f3
commit
b20eded30e
@ -5,18 +5,10 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "mongoose.h"
|
||||
|
||||
#define WIFI_SSID "WIFI_NETWORK" // SET THIS!
|
||||
#define WIFI_PASS "WIFI_PASSWORD" // SET THIS!
|
||||
#define WIFI_SSID "VMDF554B9" // SET THIS!
|
||||
#define WIFI_PASS "Mp7wjmamPafa" // SET THIS!
|
||||
#define FS_ROOT "/spiffs"
|
||||
|
||||
// SPIFFS is flat, so tell Mongoose that the FS root is a directory
|
||||
// This cludge is not required for filesystems with directory support
|
||||
static int my_stat(const char *path, size_t *size, time_t *mtime) {
|
||||
int flags = mg_fs_posix.st(path, size, mtime);
|
||||
if (strcmp(path, FS_ROOT) == 0) flags |= MG_FS_DIR;
|
||||
return flags;
|
||||
}
|
||||
|
||||
// Event handler for an server (accepted) connection. Implemented endpoints:
|
||||
// /api/stats - return JSON object with ESP32 stats (free RAM)
|
||||
// any other - serve files from the filesystem
|
||||
@ -26,9 +18,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (mg_http_match_uri(hm, "/api/stats")) {
|
||||
mg_http_reply(c, 200, "", "{\"ram\": %lu}\n", xPortGetFreeHeapSize());
|
||||
} else {
|
||||
struct mg_fs fs = mg_fs_posix;
|
||||
fs.st = my_stat;
|
||||
struct mg_http_serve_opts opts = {.root_dir = FS_ROOT, .fs = &fs};
|
||||
struct mg_http_serve_opts opts = {.root_dir = FS_ROOT};
|
||||
mg_http_serve_dir(c, hm, &opts);
|
||||
}
|
||||
}
|
||||
@ -49,7 +39,7 @@ void app_main(void) {
|
||||
|
||||
// Connected to WiFi, now start HTTP server
|
||||
struct mg_mgr mgr;
|
||||
mg_log_set("3");
|
||||
mg_log_set("4");
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, "http://0.0.0.0:80", cb, &mgr); // Listening server
|
||||
MG_INFO(("Starting Mongoose web server v%s", MG_VERSION));
|
||||
|
66
mongoose.c
66
mongoose.c
@ -528,7 +528,7 @@ static time_t ff_time_to_epoch(uint16_t fdate, uint16_t ftime) {
|
||||
|
||||
static int ff_stat(const char *path, size_t *size, time_t *mtime) {
|
||||
FILINFO fi;
|
||||
if (path[0] == '\0' || strcmp(path, MG_FATFS_ROOT) == 0) {
|
||||
if (path[0] == '\0') {
|
||||
if (size) *size = 0;
|
||||
if (mtime) *mtime = 0;
|
||||
return MG_FS_DIR;
|
||||
@ -1659,44 +1659,39 @@ static void remove_double_dots(char *s) {
|
||||
static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
|
||||
struct mg_fs *fs, struct mg_str url, struct mg_str dir,
|
||||
char *path, size_t path_size) {
|
||||
int flags = 0, tmp;
|
||||
int flags, tmp;
|
||||
// Append URI to the root_dir, and sanitize it
|
||||
size_t n = mg_snprintf(path, path_size, "%.*s", (int) dir.len, dir.ptr);
|
||||
if (n > path_size) n = path_size;
|
||||
path[path_size - 1] = '\0';
|
||||
if ((fs->st(path, NULL, NULL) & MG_FS_DIR) == 0) {
|
||||
mg_http_reply(c, 400, "", "Invalid web root [%.*s]\n", (int) dir.len,
|
||||
dir.ptr);
|
||||
} else {
|
||||
if (n + 2 < path_size) path[n++] = '/', path[n] = '\0';
|
||||
mg_url_decode(hm->uri.ptr + url.len, hm->uri.len - url.len, path + n,
|
||||
path_size - n, 0);
|
||||
path[path_size - 1] = '\0'; // Double-check
|
||||
remove_double_dots(path);
|
||||
n = strlen(path);
|
||||
MG_VERBOSE(("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
|
||||
flags = fs->st(path, NULL, NULL); // Does it exist?
|
||||
if (flags == 0) {
|
||||
mg_http_reply(c, 404, "", "Not found\n"); // Does not exist, doh
|
||||
} else if ((flags & MG_FS_DIR) && hm->uri.len > 0 &&
|
||||
hm->uri.ptr[hm->uri.len - 1] != '/') {
|
||||
mg_printf(c,
|
||||
"HTTP/1.1 301 Moved\r\n"
|
||||
"Location: %.*s/\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
(int) hm->uri.len, hm->uri.ptr);
|
||||
flags = 0;
|
||||
} else if (flags & MG_FS_DIR) {
|
||||
if (((mg_snprintf(path + n, path_size - n, "/" MG_HTTP_INDEX) > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0) ||
|
||||
(mg_snprintf(path + n, path_size - n, "/index.shtml") > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0))) {
|
||||
flags = tmp;
|
||||
} else {
|
||||
path[n] = '\0'; // Remove appended index file name
|
||||
}
|
||||
if (n + 2 < path_size) path[n++] = '/', path[n] = '\0';
|
||||
mg_url_decode(hm->uri.ptr + url.len, hm->uri.len - url.len, path + n,
|
||||
path_size - n, 0);
|
||||
path[path_size - 1] = '\0'; // Double-check
|
||||
remove_double_dots(path);
|
||||
n = strlen(path);
|
||||
MG_VERBOSE(("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
|
||||
flags = mg_vcmp(&hm->uri, "/") == 0 ? MG_FS_DIR : fs->st(path, NULL, NULL);
|
||||
if (flags == 0) {
|
||||
mg_http_reply(c, 404, "", "Not found\n"); // Does not exist, doh
|
||||
} else if ((flags & MG_FS_DIR) && hm->uri.len > 0 &&
|
||||
hm->uri.ptr[hm->uri.len - 1] != '/') {
|
||||
mg_printf(c,
|
||||
"HTTP/1.1 301 Moved\r\n"
|
||||
"Location: %.*s/\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
(int) hm->uri.len, hm->uri.ptr);
|
||||
flags = 0;
|
||||
} else if (flags & MG_FS_DIR) {
|
||||
if (((mg_snprintf(path + n, path_size - n, "/" MG_HTTP_INDEX) > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0) ||
|
||||
(mg_snprintf(path + n, path_size - n, "/index.shtml") > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0))) {
|
||||
flags = tmp;
|
||||
} else {
|
||||
path[n] = '\0'; // Remove appended index file name
|
||||
}
|
||||
}
|
||||
return flags;
|
||||
@ -1722,7 +1717,6 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
|
||||
const char *sp = opts->ssi_pattern;
|
||||
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
|
||||
if (flags == 0) return;
|
||||
MG_VERBOSE(("%.*s %s %d", (int) hm->uri.len, hm->uri.ptr, path, flags));
|
||||
if (flags & MG_FS_DIR) {
|
||||
listdir(c, hm, opts, path);
|
||||
} else if (sp != NULL && mg_globmatch(sp, strlen(sp), path, strlen(path))) {
|
||||
|
@ -495,14 +495,6 @@ int sscanf(const char *, const char *, ...);
|
||||
#define MG_ENABLE_FATFS 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_FATFS_ROOT
|
||||
#define MG_FATFS_ROOT "/"
|
||||
#endif
|
||||
|
||||
#ifndef MG_FATFS_BSIZE
|
||||
#define MG_FATFS_BSIZE 64
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_SOCKET
|
||||
#define MG_ENABLE_SOCKET 1
|
||||
#endif
|
||||
|
@ -4,14 +4,6 @@
|
||||
#define MG_ENABLE_FATFS 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_FATFS_ROOT
|
||||
#define MG_FATFS_ROOT "/"
|
||||
#endif
|
||||
|
||||
#ifndef MG_FATFS_BSIZE
|
||||
#define MG_FATFS_BSIZE 64
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_SOCKET
|
||||
#define MG_ENABLE_SOCKET 1
|
||||
#endif
|
||||
|
@ -41,7 +41,7 @@ static time_t ff_time_to_epoch(uint16_t fdate, uint16_t ftime) {
|
||||
|
||||
static int ff_stat(const char *path, size_t *size, time_t *mtime) {
|
||||
FILINFO fi;
|
||||
if (path[0] == '\0' || strcmp(path, MG_FATFS_ROOT) == 0) {
|
||||
if (path[0] == '\0') {
|
||||
if (size) *size = 0;
|
||||
if (mtime) *mtime = 0;
|
||||
return MG_FS_DIR;
|
||||
|
64
src/http.c
64
src/http.c
@ -669,44 +669,39 @@ static void remove_double_dots(char *s) {
|
||||
static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
|
||||
struct mg_fs *fs, struct mg_str url, struct mg_str dir,
|
||||
char *path, size_t path_size) {
|
||||
int flags = 0, tmp;
|
||||
int flags, tmp;
|
||||
// Append URI to the root_dir, and sanitize it
|
||||
size_t n = mg_snprintf(path, path_size, "%.*s", (int) dir.len, dir.ptr);
|
||||
if (n > path_size) n = path_size;
|
||||
path[path_size - 1] = '\0';
|
||||
if ((fs->st(path, NULL, NULL) & MG_FS_DIR) == 0) {
|
||||
mg_http_reply(c, 400, "", "Invalid web root [%.*s]\n", (int) dir.len,
|
||||
dir.ptr);
|
||||
} else {
|
||||
if (n + 2 < path_size) path[n++] = '/', path[n] = '\0';
|
||||
mg_url_decode(hm->uri.ptr + url.len, hm->uri.len - url.len, path + n,
|
||||
path_size - n, 0);
|
||||
path[path_size - 1] = '\0'; // Double-check
|
||||
remove_double_dots(path);
|
||||
n = strlen(path);
|
||||
MG_VERBOSE(("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
|
||||
flags = fs->st(path, NULL, NULL); // Does it exist?
|
||||
if (flags == 0) {
|
||||
mg_http_reply(c, 404, "", "Not found\n"); // Does not exist, doh
|
||||
} else if ((flags & MG_FS_DIR) && hm->uri.len > 0 &&
|
||||
hm->uri.ptr[hm->uri.len - 1] != '/') {
|
||||
mg_printf(c,
|
||||
"HTTP/1.1 301 Moved\r\n"
|
||||
"Location: %.*s/\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
(int) hm->uri.len, hm->uri.ptr);
|
||||
flags = 0;
|
||||
} else if (flags & MG_FS_DIR) {
|
||||
if (((mg_snprintf(path + n, path_size - n, "/" MG_HTTP_INDEX) > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0) ||
|
||||
(mg_snprintf(path + n, path_size - n, "/index.shtml") > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0))) {
|
||||
flags = tmp;
|
||||
} else {
|
||||
path[n] = '\0'; // Remove appended index file name
|
||||
}
|
||||
if (n + 2 < path_size) path[n++] = '/', path[n] = '\0';
|
||||
mg_url_decode(hm->uri.ptr + url.len, hm->uri.len - url.len, path + n,
|
||||
path_size - n, 0);
|
||||
path[path_size - 1] = '\0'; // Double-check
|
||||
remove_double_dots(path);
|
||||
n = strlen(path);
|
||||
MG_VERBOSE(("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
|
||||
flags = mg_vcmp(&hm->uri, "/") == 0 ? MG_FS_DIR : fs->st(path, NULL, NULL);
|
||||
if (flags == 0) {
|
||||
mg_http_reply(c, 404, "", "Not found\n"); // Does not exist, doh
|
||||
} else if ((flags & MG_FS_DIR) && hm->uri.len > 0 &&
|
||||
hm->uri.ptr[hm->uri.len - 1] != '/') {
|
||||
mg_printf(c,
|
||||
"HTTP/1.1 301 Moved\r\n"
|
||||
"Location: %.*s/\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
(int) hm->uri.len, hm->uri.ptr);
|
||||
flags = 0;
|
||||
} else if (flags & MG_FS_DIR) {
|
||||
if (((mg_snprintf(path + n, path_size - n, "/" MG_HTTP_INDEX) > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0) ||
|
||||
(mg_snprintf(path + n, path_size - n, "/index.shtml") > 0 &&
|
||||
(tmp = fs->st(path, NULL, NULL)) != 0))) {
|
||||
flags = tmp;
|
||||
} else {
|
||||
path[n] = '\0'; // Remove appended index file name
|
||||
}
|
||||
}
|
||||
return flags;
|
||||
@ -732,7 +727,6 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
|
||||
const char *sp = opts->ssi_pattern;
|
||||
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
|
||||
if (flags == 0) return;
|
||||
MG_VERBOSE(("%.*s %s %d", (int) hm->uri.len, hm->uri.ptr, path, flags));
|
||||
if (flags & MG_FS_DIR) {
|
||||
listdir(c, hm, opts, path);
|
||||
} else if (sp != NULL && mg_globmatch(sp, strlen(sp), path, strlen(path))) {
|
||||
|
@ -365,14 +365,12 @@ static void test_mqtt(void) {
|
||||
// Connect with empty client ID
|
||||
c = mg_mqtt_connect(&mgr, url, NULL, mqtt_cb, buf);
|
||||
for (i = 0; i < 200 && buf[0] == 0; i++) mg_mgr_poll(&mgr, 10);
|
||||
if (buf[0] != 'X')
|
||||
MG_INFO(("[%s]", buf));
|
||||
if (buf[0] != 'X') MG_INFO(("[%s]", buf));
|
||||
ASSERT(buf[0] == 'X');
|
||||
mg_mqtt_sub(c, topic, 1);
|
||||
mg_mqtt_pub(c, topic, data, 1, false);
|
||||
for (i = 0; i < 300 && buf[1] == 0; i++) mg_mgr_poll(&mgr, 10);
|
||||
if (strcmp(buf, "Xx/f12/hi") != 0)
|
||||
MG_INFO(("[%s]", buf));
|
||||
if (strcmp(buf, "Xx/f12/hi") != 0) MG_INFO(("[%s]", buf));
|
||||
ASSERT(strcmp(buf, "Xx/f12/hi") == 0);
|
||||
|
||||
// Set params
|
||||
@ -387,14 +385,12 @@ static void test_mqtt(void) {
|
||||
opts.will_message = mg_str("mg_will_messsage");
|
||||
c = mg_mqtt_connect(&mgr, url, &opts, mqtt_cb, buf);
|
||||
for (i = 0; i < 300 && buf[0] == 0; i++) mg_mgr_poll(&mgr, 10);
|
||||
if (buf[0] != 'X')
|
||||
MG_INFO(("[%s]", buf));
|
||||
if (buf[0] != 'X') MG_INFO(("[%s]", buf));
|
||||
ASSERT(buf[0] == 'X');
|
||||
mg_mqtt_sub(c, topic, 1);
|
||||
mg_mqtt_pub(c, topic, data, 1, false);
|
||||
for (i = 0; i < 500 && buf[1] == 0; i++) mg_mgr_poll(&mgr, 10);
|
||||
if (strcmp(buf, "Xx/f12/hi") != 0)
|
||||
MG_INFO(("[%s]", buf));
|
||||
if (strcmp(buf, "Xx/f12/hi") != 0) MG_INFO(("[%s]", buf));
|
||||
ASSERT(strcmp(buf, "Xx/f12/hi") == 0);
|
||||
|
||||
mg_mgr_free(&mgr);
|
||||
@ -662,8 +658,8 @@ static void test_http_server(void) {
|
||||
mg_str("text/html; charset=utf-8")) == 0);
|
||||
}
|
||||
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /badroot HTTP/1.0\r\n\n") == 400);
|
||||
ASSERT(cmpbody(buf, "Invalid web root [/BAAADDD!]\n") == 0);
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /badroot HTTP/1.0\r\n\n") == 404);
|
||||
// ASSERT(cmpbody(buf, "Invalid web root [/BAAADDD!]\n") == 0);
|
||||
|
||||
{
|
||||
char *data = mg_file_read(&mg_fs_posix, "./test/data/ca.pem", NULL);
|
||||
|
Loading…
x
Reference in New Issue
Block a user