From 86824795a07f9c3e2e109d47b2d120e91f2f7183 Mon Sep 17 00:00:00 2001 From: cpq Date: Sat, 2 Jan 2021 12:30:09 +0000 Subject: [PATCH] Pass opts structure to mg_http_serve_dir() --- mongoose.c | 6 +++--- mongoose.h | 8 +++++++- src/http.c | 6 +++--- src/http.h | 8 +++++++- test/unit_test.c | 9 ++++++--- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/mongoose.c b/mongoose.c index 4fe39d29..57b5ed2b 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1009,11 +1009,11 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm, #endif void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, - const char *dir) { + const struct mg_http_serve_opts *opts) { char path[PATH_MAX + 2], root[sizeof(path) - 2], real[sizeof(path) - 2]; path[0] = root[0] = real[0] = '\0'; - if (realpath(dir, root) == NULL) - LOG(LL_DEBUG, ("realpath(%s): %d", dir, errno)); + if (realpath(opts->root_dir, root) == NULL) + LOG(LL_DEBUG, ("realpath(%s): %d", opts->root_dir, errno)); if (!mg_is_dir(root)) { mg_http_reply(c, 400, "", "Bad web root [%s]\n", root); } else { diff --git a/mongoose.h b/mongoose.h index ecceb6b5..ca7f6834 100644 --- a/mongoose.h +++ b/mongoose.h @@ -665,6 +665,12 @@ struct mg_http_message { struct mg_str message; // Request line + headers + body }; +// Parameter for mg_http_serve_dir() +struct mg_http_serve_opts { + const char *root_dir; + const char *ssi_pattern; +}; + int mg_http_parse(const char *s, size_t len, struct mg_http_message *); int mg_http_get_request_len(const unsigned char *buf, size_t buf_len); void mg_http_printf_chunk(struct mg_connection *cnn, const char *fmt, ...); @@ -674,7 +680,7 @@ struct mg_connection *mg_http_listen(struct mg_mgr *, const char *url, struct mg_connection *mg_http_connect(struct mg_mgr *, const char *url, mg_event_handler_t fn, void *fn_data); void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm, - const char *path); + const struct mg_http_serve_opts *); void mg_http_serve_file(struct mg_connection *, struct mg_http_message *, const char *, const char *mime); void mg_http_reply(struct mg_connection *, int status_code, const char *headers, diff --git a/src/http.c b/src/http.c index bc764593..40a7539b 100644 --- a/src/http.c +++ b/src/http.c @@ -595,11 +595,11 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm, #endif void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, - const char *dir) { + const struct mg_http_serve_opts *opts) { char path[PATH_MAX + 2], root[sizeof(path) - 2], real[sizeof(path) - 2]; path[0] = root[0] = real[0] = '\0'; - if (realpath(dir, root) == NULL) - LOG(LL_DEBUG, ("realpath(%s): %d", dir, errno)); + if (realpath(opts->root_dir, root) == NULL) + LOG(LL_DEBUG, ("realpath(%s): %d", opts->root_dir, errno)); if (!mg_is_dir(root)) { mg_http_reply(c, 400, "", "Bad web root [%s]\n", root); } else { diff --git a/src/http.h b/src/http.h index d43d6a49..4272e5d3 100644 --- a/src/http.h +++ b/src/http.h @@ -19,6 +19,12 @@ struct mg_http_message { struct mg_str message; // Request line + headers + body }; +// Parameter for mg_http_serve_dir() +struct mg_http_serve_opts { + const char *root_dir; + const char *ssi_pattern; +}; + int mg_http_parse(const char *s, size_t len, struct mg_http_message *); int mg_http_get_request_len(const unsigned char *buf, size_t buf_len); void mg_http_printf_chunk(struct mg_connection *cnn, const char *fmt, ...); @@ -28,7 +34,7 @@ struct mg_connection *mg_http_listen(struct mg_mgr *, const char *url, struct mg_connection *mg_http_connect(struct mg_mgr *, const char *url, mg_event_handler_t fn, void *fn_data); void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm, - const char *path); + const struct mg_http_serve_opts *); void mg_http_serve_file(struct mg_connection *, struct mg_http_message *, const char *, const char *mime); void mg_http_reply(struct mg_connection *, int status_code, const char *headers, diff --git a/test/unit_test.c b/test/unit_test.c index 5f802449..7fa27640 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -337,7 +337,8 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (mg_http_match_uri(hm, "/bar")) { mg_http_reply(c, 404, "", "not found"); } else if (mg_http_match_uri(hm, "/badroot")) { - mg_http_serve_dir(c, hm, "/BAAADDD!"); + struct mg_http_serve_opts opts = {"/BAAADDD!", NULL}; + mg_http_serve_dir(c, hm, &opts); } else if (mg_http_match_uri(hm, "/creds")) { char user[100], pass[100]; mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass)); @@ -345,9 +346,11 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (mg_http_match_uri(hm, "/upload")) { mg_http_upload(c, hm, "."); } else if (mg_http_match_uri(hm, "/test/")) { - mg_http_serve_dir(c, hm, "."); + struct mg_http_serve_opts opts = {".", NULL}; + mg_http_serve_dir(c, hm, &opts); } else { - mg_http_serve_dir(c, hm, "./test/data"); + struct mg_http_serve_opts opts = {"./test/data", NULL}; + mg_http_serve_dir(c, hm, &opts); } } else if (ev == MG_EV_WS_MSG) { struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;