From 4dd1891594487687a01845ee0ef528f4f1163eb1 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 31 May 2022 23:44:03 +0100 Subject: [PATCH] Add mg_http_serve_opts.page404 --- docs/README.md | 13 ++++++++++- mongoose.c | 17 +++++++++------ mongoose.h | 1 + src/http.c | 17 +++++++++------ src/http.h | 1 + test/data/404.html | 1 + test/unit_test.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 test/data/404.html diff --git a/docs/README.md b/docs/README.md index 2e726009..071e7e55 100644 --- a/docs/README.md +++ b/docs/README.md @@ -959,14 +959,25 @@ void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } ``` -### mg\_http\_serve\_dir() +### struct mg\_http\_serve\_opts ```c struct mg_http_serve_opts { const char *root_dir; // Web root directory, must be non-NULL const char *ssi_pattern; // SSI file name pattern, e.g. #.shtml const char *extra_headers; // Extra HTTP headers to add in responses + const char *mime_types; // Extra mime types, ext1=type1,ext2=type2,.. + const char *page404; // Path to the 404 page, or NULL by default + struct mg_fs *fs; // Filesystem implementation. Use NULL for POSIX }; +``` + +A structure passed to `mg_http_serve_dir()` and `mg_http_serve_file()`, which +drives the behavior of those two functions. + +### mg\_http\_serve\_dir() + +```c void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, const struct mg_http_serve_opts *opts); ``` diff --git a/mongoose.c b/mongoose.c index 61192b9b..ecfa1ddb 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1431,14 +1431,14 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, const struct mg_http_serve_opts *opts) { char etag[64]; struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs; - struct mg_fd *fd = mg_fs_open(fs, path, MG_FS_READ); + struct mg_fd *fd = path == NULL ? NULL : mg_fs_open(fs, path, MG_FS_READ); size_t size = 0; time_t mtime = 0; struct mg_str *inm = NULL; if (fd == NULL || fs->st(path, &size, &mtime) == 0) { - MG_DEBUG(("404 [%s] %p", path, (void *) fd)); - mg_http_reply(c, 404, "", "%s", "Not found\n"); + // MG_DEBUG(("404 [%s] %p", path, (void *) fd)); + mg_http_reply(c, 404, opts->extra_headers, "Not found\n"); mg_fs_close(fd); // NOTE: mg_http_etag() call should go first! } else if (mg_http_etag(etag, sizeof(etag), size, mtime) != NULL && @@ -1634,7 +1634,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, 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 + // Do nothing - let's caller decide } else if ((flags & MG_FS_DIR) && hm->uri.len > 0 && hm->uri.ptr[hm->uri.len - 1] != '/') { mg_printf(c, @@ -1643,7 +1643,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, "Content-Length: 0\r\n" "\r\n", (int) hm->uri.len, hm->uri.ptr); - flags = 0; + flags = -1; } 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) || @@ -1676,8 +1676,11 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, char path[MG_PATH_MAX] = ""; const char *sp = opts->ssi_pattern; int flags = uri_to_path(c, hm, opts, path, sizeof(path)); - if (flags == 0) return; - if (flags & MG_FS_DIR) { + if (flags < 0) { + // Do nothing: the response has already been sent by uri_to_path() + } else if (flags == 0) { + mg_http_serve_file(c, hm, opts->page404, opts); + } else if (flags & MG_FS_DIR) { listdir(c, hm, opts, path); } else if (sp != NULL && mg_globmatch(sp, strlen(sp), path, strlen(path))) { mg_http_serve_ssi(c, opts->root_dir, path); diff --git a/mongoose.h b/mongoose.h index c77a66ec..bb376421 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1057,6 +1057,7 @@ struct mg_http_serve_opts { const char *ssi_pattern; // SSI file name pattern, e.g. #.shtml const char *extra_headers; // Extra HTTP headers to add in responses const char *mime_types; // Extra mime types, ext1=type1,ext2=type2,.. + const char *page404; // Path to the 404 page, or NULL by default struct mg_fs *fs; // Filesystem implementation. Use NULL for POSIX }; diff --git a/src/http.c b/src/http.c index d91f8148..657b4a8a 100644 --- a/src/http.c +++ b/src/http.c @@ -437,14 +437,14 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, const struct mg_http_serve_opts *opts) { char etag[64]; struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs; - struct mg_fd *fd = mg_fs_open(fs, path, MG_FS_READ); + struct mg_fd *fd = path == NULL ? NULL : mg_fs_open(fs, path, MG_FS_READ); size_t size = 0; time_t mtime = 0; struct mg_str *inm = NULL; if (fd == NULL || fs->st(path, &size, &mtime) == 0) { - MG_DEBUG(("404 [%s] %p", path, (void *) fd)); - mg_http_reply(c, 404, "", "%s", "Not found\n"); + // MG_DEBUG(("404 [%s] %p", path, (void *) fd)); + mg_http_reply(c, 404, opts->extra_headers, "Not found\n"); mg_fs_close(fd); // NOTE: mg_http_etag() call should go first! } else if (mg_http_etag(etag, sizeof(etag), size, mtime) != NULL && @@ -640,7 +640,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, 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 + // Do nothing - let's caller decide } else if ((flags & MG_FS_DIR) && hm->uri.len > 0 && hm->uri.ptr[hm->uri.len - 1] != '/') { mg_printf(c, @@ -649,7 +649,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, "Content-Length: 0\r\n" "\r\n", (int) hm->uri.len, hm->uri.ptr); - flags = 0; + flags = -1; } 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) || @@ -682,8 +682,11 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, char path[MG_PATH_MAX] = ""; const char *sp = opts->ssi_pattern; int flags = uri_to_path(c, hm, opts, path, sizeof(path)); - if (flags == 0) return; - if (flags & MG_FS_DIR) { + if (flags < 0) { + // Do nothing: the response has already been sent by uri_to_path() + } else if (flags == 0) { + mg_http_serve_file(c, hm, opts->page404, opts); + } else if (flags & MG_FS_DIR) { listdir(c, hm, opts, path); } else if (sp != NULL && mg_globmatch(sp, strlen(sp), path, strlen(path))) { mg_http_serve_ssi(c, opts->root_dir, path); diff --git a/src/http.h b/src/http.h index e94cf85b..25e9a6cb 100644 --- a/src/http.h +++ b/src/http.h @@ -26,6 +26,7 @@ struct mg_http_serve_opts { const char *ssi_pattern; // SSI file name pattern, e.g. #.shtml const char *extra_headers; // Extra HTTP headers to add in responses const char *mime_types; // Extra mime types, ext1=type1,ext2=type2,.. + const char *page404; // Path to the 404 page, or NULL by default struct mg_fs *fs; // Filesystem implementation. Use NULL for POSIX }; diff --git a/test/data/404.html b/test/data/404.html new file mode 100644 index 00000000..5819a185 --- /dev/null +++ b/test/data/404.html @@ -0,0 +1 @@ +boo diff --git a/test/unit_test.c b/test/unit_test.c index 7d3099ed..a79aa144 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -825,6 +825,59 @@ static void test_http_server(void) { ASSERT(mgr.conns == NULL); } +static void h4(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_HTTP_MSG) { + struct mg_http_message *hm = (struct mg_http_message *) ev_data; + MG_INFO(("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.ptr, + (int) hm->uri.len, hm->uri.ptr, (int) hm->message.len)); + if (mg_http_match_uri(hm, "/a/#")) { + struct mg_http_serve_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.root_dir = "/a=./test/data"; + opts.page404 = "./test/data/404.html"; // existing 404 page + mg_http_serve_dir(c, hm, &opts); + } else if (mg_http_match_uri(hm, "/b/#")) { + struct mg_http_serve_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.root_dir = "/b=./test/data"; + opts.page404 = "./test/data/nooooo.html"; // non-existing 404 page + mg_http_serve_dir(c, hm, &opts); + } else { // null 404 page + struct mg_http_serve_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.root_dir = "./test/data"; + mg_http_serve_dir(c, hm, &opts); + } + } + (void) fn_data; +} + +static void test_http_404(void) { + struct mg_mgr mgr; + const char *url = "http://127.0.0.1:22343"; + char buf[FETCH_BUF_SIZE]; + + mg_mgr_init(&mgr); + mg_http_listen(&mgr, url, h4, NULL); + + ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\n\n") == 200); + ASSERT(cmpbody(buf, "hello\n") == 0); + ASSERT(fetch(&mgr, buf, url, "GET /a/a.txt HTTP/1.0\n\n") == 200); + ASSERT(cmpbody(buf, "hello\n") == 0); + ASSERT(fetch(&mgr, buf, url, "GET /b/a.txt HTTP/1.0\n\n") == 200); + ASSERT(cmpbody(buf, "hello\n") == 0); + + ASSERT(fetch(&mgr, buf, url, "GET /xx.txt HTTP/1.0\n\n") == 404); + ASSERT(cmpbody(buf, "Not found\n") == 0); + ASSERT(fetch(&mgr, buf, url, "GET /a/xx.txt HTTP/1.0\n\n") == 200); + ASSERT(cmpbody(buf, "boo\n") == 0); + ASSERT(fetch(&mgr, buf, url, "GET /b/xx.txt HTTP/1.0\n\n") == 404); + ASSERT(cmpbody(buf, "Not found\n") == 0); + + mg_mgr_free(&mgr); + ASSERT(mgr.conns == NULL); +} + static void test_tls(void) { #if MG_ENABLE_MBEDTLS || MG_ENABLE_OPENSSL struct mg_tls_opts opts = {.ca = "./test/data/ss_ca.pem", @@ -2024,6 +2077,7 @@ int main(void) { test_ws_fragmentation(); test_http_client(); test_http_server(); + test_http_404(); test_http_no_content_length(); test_http_pipeline(); test_http_range();