From ef61d6ea48c8809ad5dc5a229b6fc2c41f439880 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Wed, 17 Apr 2024 16:13:10 -0300 Subject: [PATCH] remove http_match_uri() --- .../arduino/teensy41-http/teensy41-http.ino | 2 +- examples/device-dashboard/net.c | 28 +++++++++---------- examples/file-transfer/server.c | 2 +- examples/file-upload-html-form/main.c | 2 +- examples/file-upload-multiple-posts/main.c | 2 +- examples/http-restful-server/main.c | 4 +-- examples/huge-response/main.c | 2 +- examples/json-rpc-over-websocket/main.c | 2 +- examples/live-log/main.c | 4 +-- examples/mip-pcap/main.c | 4 +-- examples/modbus-dashboard/net.c | 10 +++---- examples/multi-threaded-12m/main.c | 2 +- examples/multi-threaded/main.c | 2 +- .../main.c | 2 +- .../main.c | 2 +- .../nucleo-g031-make-baremetal-builtin/main.c | 4 +-- .../main.c | 2 +- examples/timers/main.c | 2 +- examples/uart-bridge/net.c | 6 ++-- examples/video-stream/main.c | 2 +- examples/websocket-server/main.c | 4 +-- examples/webui-login/main.c | 6 ++-- examples/webui-plain/main.c | 4 +-- examples/webui-push-rest/main.c | 2 +- examples/webui-push-ws/main.c | 2 +- examples/webui-rest/main.c | 4 +-- examples/wifi-router-dashboard/net.c | 18 ++++++------ examples/zephyr/http-server/src/main.c | 2 +- examples/zephyr/websocket-server/src/main.c | 4 +-- mongoose.c | 8 ++---- mongoose.h | 1 - src/http.c | 8 ++---- src/http.h | 1 - test/unit_test.c | 26 ++++++++--------- 34 files changed, 83 insertions(+), 93 deletions(-) diff --git a/examples/arduino/teensy41-http/teensy41-http.ino b/examples/arduino/teensy41-http/teensy41-http.ino index fff9f801..142858e8 100644 --- a/examples/arduino/teensy41-http/teensy41-http.ino +++ b/examples/arduino/teensy41-http/teensy41-http.ino @@ -32,7 +32,7 @@ static void simple_http_listener(struct mg_connection *c, int ev, void *ev_data) struct mg_http_message *hm = (struct mg_http_message *) ev_data; // If the requested URI is "/api/hi", send a simple JSON response back - if (mg_http_match_uri(hm, "/api/hi")) { + if (mg_match(hm->uri, mg_str("/api/hi"), NULL)) { // Use mg_http_reply() API function to generate JSON response. It adds a // Content-Length header automatically. In the response, we show // the requested URI and HTTP body: diff --git a/examples/device-dashboard/net.c b/examples/device-dashboard/net.c index 6dd3a32e..4ad5c2ff 100644 --- a/examples/device-dashboard/net.c +++ b/examples/device-dashboard/net.c @@ -273,33 +273,33 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct user *u = authenticate(hm); - if (mg_http_match_uri(hm, "/api/#") && u == NULL) { + if (mg_match(hm->uri, mg_str("/api/#"), NULL) && u == NULL) { mg_http_reply(c, 403, "", "Not Authorised\n"); - } else if (mg_http_match_uri(hm, "/api/login")) { + } else if (mg_match(hm->uri, mg_str("/api/login"), NULL)) { handle_login(c, u); - } else if (mg_http_match_uri(hm, "/api/logout")) { + } else if (mg_match(hm->uri, mg_str("/api/logout"), NULL)) { handle_logout(c); - } else if (mg_http_match_uri(hm, "/api/debug")) { + } else if (mg_match(hm->uri, mg_str("/api/debug"), NULL)) { handle_debug(c, hm); - } else if (mg_http_match_uri(hm, "/api/stats/get")) { + } else if (mg_match(hm->uri, mg_str("/api/stats/get"), NULL)) { handle_stats_get(c); - } else if (mg_http_match_uri(hm, "/api/events/get")) { + } else if (mg_match(hm->uri, mg_str("/api/events/get"), NULL)) { handle_events_get(c, hm); - } else if (mg_http_match_uri(hm, "/api/settings/get")) { + } else if (mg_match(hm->uri, mg_str("/api/settings/get"), NULL)) { handle_settings_get(c); - } else if (mg_http_match_uri(hm, "/api/settings/set")) { + } else if (mg_match(hm->uri, mg_str("/api/settings/set"), NULL)) { handle_settings_set(c, hm->body); - } else if (mg_http_match_uri(hm, "/api/firmware/upload")) { + } else if (mg_match(hm->uri, mg_str("/api/firmware/upload"), NULL)) { handle_firmware_upload(c, hm); - } else if (mg_http_match_uri(hm, "/api/firmware/commit")) { + } else if (mg_match(hm->uri, mg_str("/api/firmware/commit"), NULL)) { handle_firmware_commit(c); - } else if (mg_http_match_uri(hm, "/api/firmware/rollback")) { + } else if (mg_match(hm->uri, mg_str("/api/firmware/rollback"), NULL)) { handle_firmware_rollback(c); - } else if (mg_http_match_uri(hm, "/api/firmware/status")) { + } else if (mg_match(hm->uri, mg_str("/api/firmware/status"), NULL)) { handle_firmware_status(c); - } else if (mg_http_match_uri(hm, "/api/device/reset")) { + } else if (mg_match(hm->uri, mg_str("/api/device/reset"), NULL)) { handle_device_reset(c); - } else if (mg_http_match_uri(hm, "/api/device/eraselast")) { + } else if (mg_match(hm->uri, mg_str("/api/device/eraselast"), NULL)) { handle_device_eraselast(c); } else { struct mg_http_serve_opts opts; diff --git a/examples/file-transfer/server.c b/examples/file-transfer/server.c index 13786b58..031b585b 100644 --- a/examples/file-transfer/server.c +++ b/examples/file-transfer/server.c @@ -40,7 +40,7 @@ static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) { // HTTP headers but not necessarily full HTTP body if (ev == MG_EV_HTTP_HDRS) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/upload/#")) { + if (mg_match(hm->uri, mg_str("/upload/#"), NULL)) { c->pfn = NULL; // Silence HTTP protocol handler, we'll take over if (!authuser(hm)) { mg_http_reply(c, 403, "", "Denied\n"); diff --git a/examples/file-upload-html-form/main.c b/examples/file-upload-html-form/main.c index d986ec25..e83789d8 100644 --- a/examples/file-upload-html-form/main.c +++ b/examples/file-upload-html-form/main.c @@ -21,7 +21,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; MG_INFO(("New request to: [%.*s], body size: %lu", (int) hm->uri.len, hm->uri.buf, (unsigned long) hm->body.len)); - if (mg_http_match_uri(hm, "/upload")) { + if (mg_match(hm->uri, mg_str("/upload"), NULL)) { struct mg_http_part part; size_t ofs = 0; while ((ofs = mg_http_next_multipart(hm->body, ofs, &part)) > 0) { diff --git a/examples/file-upload-multiple-posts/main.c b/examples/file-upload-multiple-posts/main.c index 0e34474a..756f9135 100644 --- a/examples/file-upload-multiple-posts/main.c +++ b/examples/file-upload-multiple-posts/main.c @@ -9,7 +9,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/upload")) { + if (mg_match(hm->uri, mg_str("/upload"), NULL)) { mg_http_upload(c, hm, &mg_fs_posix, "/tmp", 99999); } else { struct mg_http_serve_opts opts = {.root_dir = "web_root"}; diff --git a/examples/http-restful-server/main.c b/examples/http-restful-server/main.c index ef5d9bbb..36470bd1 100644 --- a/examples/http-restful-server/main.c +++ b/examples/http-restful-server/main.c @@ -68,7 +68,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { } if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/stats")) { + if (mg_match(hm->uri, mg_str("/api/stats"), NULL)) { // Print some statistics about currently established connections mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); mg_http_printf_chunk(c, "ID PROTO TYPE LOCAL REMOTE\n"); @@ -81,7 +81,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { mg_print_ip, &t->loc, mg_print_ip, &t->rem); } mg_http_printf_chunk(c, ""); // Don't forget the last empty chunk - } else if (mg_http_match_uri(hm, "/api/f2/*")) { + } else if (mg_match(hm->uri, mg_str("/api/f2/*"), NULL)) { mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len, hm->uri.buf); } else { diff --git a/examples/huge-response/main.c b/examples/huge-response/main.c index 69e437ee..cfb73c4c 100644 --- a/examples/huge-response/main.c +++ b/examples/huge-response/main.c @@ -44,7 +44,7 @@ static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) { static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = ev_data; - if (mg_http_match_uri(hm, "/api/data")) { + if (mg_match(hm->uri, mg_str("/api/data"), NULL)) { const char *headers = "content-type: text/json\r\n"; long start = getparam(hm, "$.start"); long version = getparam(hm, "$.version"); diff --git a/examples/json-rpc-over-websocket/main.c b/examples/json-rpc-over-websocket/main.c index 2a4ce3aa..a00de624 100644 --- a/examples/json-rpc-over-websocket/main.c +++ b/examples/json-rpc-over-websocket/main.c @@ -33,7 +33,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { c->data[0] = 'W'; // Mark this connection as an established WS client } 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")) { + if (mg_match(hm->uri, mg_str("/websocket"), NULL)) { // Upgrade to websocket. From now on, a connection is a full-duplex // Websocket connection, which will receive MG_EV_WS_MSG events. mg_ws_upgrade(c, hm, NULL); diff --git a/examples/live-log/main.c b/examples/live-log/main.c index 204e0c85..cee22b38 100644 --- a/examples/live-log/main.c +++ b/examples/live-log/main.c @@ -10,10 +10,10 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/log/static")) { + if (mg_match(hm->uri, mg_str("/api/log/static"), NULL)) { struct mg_http_serve_opts opts = {.root_dir = NULL}; mg_http_serve_file(c, hm, "log.txt", &opts); - } else if (mg_http_match_uri(hm, "/api/log/live")) { + } else if (mg_match(hm->uri, mg_str("/api/log/live"), NULL)) { c->data[0] = 'L'; // Mark that connection as live log listener mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); } else { diff --git a/examples/mip-pcap/main.c b/examples/mip-pcap/main.c index 11e01267..eb4c3cb4 100644 --- a/examples/mip-pcap/main.c +++ b/examples/mip-pcap/main.c @@ -58,11 +58,11 @@ static void fn2(struct mg_connection *c, int ev, void *ev_data) { static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/debug")) { + if (mg_match(hm->uri, mg_str("/api/debug"), NULL)) { int level = mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG); mg_log_set(level); mg_http_reply(c, 200, "", "Debug level set to %d\n", level); - } else if (mg_http_match_uri(hm, "/api/url")) { + } else if (mg_match(hm->uri, mg_str("/api/url"), NULL)) { char *url = mg_json_get_str(hm->body, "$.url"); if (url == NULL) { mg_http_reply(c, 200, NULL, "no url, rl %d\r\n", (int) c->recv.len); diff --git a/examples/modbus-dashboard/net.c b/examples/modbus-dashboard/net.c index 25341d39..a226b1f5 100644 --- a/examples/modbus-dashboard/net.c +++ b/examples/modbus-dashboard/net.c @@ -253,15 +253,15 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/settings/get")) { + if (mg_match(hm->uri, mg_str("/api/settings/get"), NULL)) { handle_settings_get(c); - } else if (mg_http_match_uri(hm, "/api/settings/set")) { + } else if (mg_match(hm->uri, mg_str("/api/settings/set"), NULL)) { handle_settings_set(c, hm->body); - } else if (mg_http_match_uri(hm, "/api/settings/set")) { + } else if (mg_match(hm->uri, mg_str("/api/settings/set"), NULL)) { handle_settings_set(c, hm->body); - } else if (mg_http_match_uri(hm, "/api/modbus/exec")) { + } else if (mg_match(hm->uri, mg_str("/api/modbus/exec"), NULL)) { handle_modbus_exec(c, hm->body); - } else if (mg_http_match_uri(hm, "/api/device/reset")) { + } else if (mg_match(hm->uri, mg_str("/api/device/reset"), NULL)) { mg_timer_add(c->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL); mg_http_reply(c, 200, s_json_header, "true\n"); } else { diff --git a/examples/multi-threaded-12m/main.c b/examples/multi-threaded-12m/main.c index 8d91694c..648a84fd 100644 --- a/examples/multi-threaded-12m/main.c +++ b/examples/multi-threaded-12m/main.c @@ -52,7 +52,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { start_thread(thread_function, data); // Start thread and pass data } 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")) { + if (mg_match(hm->uri, mg_str("/websocket"), NULL)) { mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to Websocket c->data[0] = 'W'; // Set some unique mark on a connection } else { diff --git a/examples/multi-threaded/main.c b/examples/multi-threaded/main.c index 3f03ab8f..86ccc910 100644 --- a/examples/multi-threaded/main.c +++ b/examples/multi-threaded/main.c @@ -42,7 +42,7 @@ static void *thread_function(void *param) { static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/fast")) { + if (mg_match(hm->uri, mg_str("/fast"), NULL)) { // Single-threaded code path, for performance comparison // The /fast URI responds immediately mg_http_reply(c, 200, "Host: foo.com\r\n", "hi\n"); diff --git a/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c b/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c index dce94871..4c7f689c 100644 --- a/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c +++ b/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c @@ -66,7 +66,7 @@ static bool usb_up(struct mg_tcpip_if *ifp) { static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/debug")) { + if (mg_match(hm->uri, mg_str("/api/debug"), NULL)) { int level = mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG); mg_log_set(level); mg_http_reply(c, 200, "", "Debug level set to %d\n", level); diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c index dce94871..4c7f689c 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c @@ -66,7 +66,7 @@ static bool usb_up(struct mg_tcpip_if *ifp) { static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/debug")) { + if (mg_match(hm->uri, mg_str("/api/debug"), NULL)) { int level = mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG); mg_log_set(level); mg_http_reply(c, 200, "", "Debug level set to %d\n", level); diff --git a/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c b/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c index 7a4be44c..ec4ba68b 100644 --- a/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c @@ -46,13 +46,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/hello")) { // Request to /api/hello + if (mg_match(hm->uri, mg_str("/api/hello"), NULL)) { // Request to /api/hello mg_http_reply(c, 200, "", "{%m:%u,%m:%u,%m:%u,%m:%u,%m:%u}\n", MG_ESC("eth"), ifp->state, MG_ESC("frames_received"), ifp->nrecv, MG_ESC("frames_sent"), ifp->nsent, MG_ESC("frames_dropped"), ifp->ndrop, MG_ESC("interface_errors"), ifp->nerr); - } else if (mg_http_match_uri(hm, "/")) { // Index page + } else if (mg_match(hm->uri, mg_str("/"), NULL)) { // Index page mg_http_reply( c, 200, "", "%s", "" diff --git a/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c b/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c index 750f4831..d8974cd5 100644 --- a/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c +++ b/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c @@ -64,7 +64,7 @@ static bool usb_up(struct mg_tcpip_if *ifp) { static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/debug")) { + if (mg_match(hm->uri, mg_str("/api/debug"), NULL)) { int level = mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG); mg_log_set(level); mg_http_reply(c, 200, "", "Debug level set to %d\n", level); diff --git a/examples/timers/main.c b/examples/timers/main.c index 70ece63f..7808bc73 100644 --- a/examples/timers/main.c +++ b/examples/timers/main.c @@ -14,7 +14,7 @@ static const char *s_web_root = "web_root"; static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/websocket")) { + if (mg_match(hm->uri, mg_str("/websocket"), NULL)) { mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to Websocket c->data[0] = 'W'; // Set some unique mark on a connection } else { diff --git a/examples/uart-bridge/net.c b/examples/uart-bridge/net.c index 25b7700f..1a4c6cea 100644 --- a/examples/uart-bridge/net.c +++ b/examples/uart-bridge/net.c @@ -198,13 +198,13 @@ void uart_bridge_fn(struct mg_connection *c, int ev, void *ev_data) { // mg_log_set(MG_LL_DEBUG); // Set log level } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/hi")) { + if (mg_match(hm->uri, mg_str("/api/hi"), NULL)) { mg_http_reply(c, 200, "", "hi\n"); // Testing endpoint - } else if (mg_http_match_uri(hm, "/api/config/set")) { + } else if (mg_match(hm->uri, mg_str("/api/config/set"), NULL)) { config_apply(hm->body); config_write(hm->body); mg_http_reply(c, 200, "", "true\n"); - } else if (mg_http_match_uri(hm, "/api/config/get")) { + } else if (mg_match(hm->uri, mg_str("/api/config/get"), NULL)) { mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:{%m:%m,%m:%s},%m:{%m:%m,%m:%s},%m:{%m:%m,%m:%s}," "%m:%d,%m:%d,%m:%d}\n", diff --git a/examples/video-stream/main.c b/examples/video-stream/main.c index 8ff5299b..3c8b34f0 100644 --- a/examples/video-stream/main.c +++ b/examples/video-stream/main.c @@ -9,7 +9,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/video1")) { + if (mg_match(hm->uri, mg_str("/api/video1"), NULL)) { c->data[0] = 'S'; // Mark that connection as live streamer mg_printf( c, "%s", diff --git a/examples/websocket-server/main.c b/examples/websocket-server/main.c index 0d7b786a..ba6637d5 100644 --- a/examples/websocket-server/main.c +++ b/examples/websocket-server/main.c @@ -17,11 +17,11 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { // 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")) { + if (mg_match(hm->uri, mg_str("/websocket"), NULL)) { // Upgrade to websocket. From now on, a connection is a full-duplex // Websocket connection, which will receive MG_EV_WS_MSG events. mg_ws_upgrade(c, hm, NULL); - } else if (mg_http_match_uri(hm, "/rest")) { + } else if (mg_match(hm->uri, mg_str("/rest"), NULL)) { // Serve REST response mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123); } else { diff --git a/examples/webui-login/main.c b/examples/webui-login/main.c index 0704fb70..4a5fde58 100644 --- a/examples/webui-login/main.c +++ b/examples/webui-login/main.c @@ -47,14 +47,14 @@ void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct user *u = getuser(hm); - if (u == NULL && mg_http_match_uri(hm, "/api/#")) { + if (u == NULL && mg_match(hm->uri, mg_str("/api/#"), NULL)) { // All URIs starting with /api/ must be authenticated mg_http_reply(c, 403, "", "Denied\n"); - } else if (mg_http_match_uri(hm, "/api/data")) { + } else if (mg_match(hm->uri, mg_str("/api/data"), NULL)) { mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%m,%m:%m}\n", MG_ESC("text"), MG_ESC("Hello!"), MG_ESC("data"), MG_ESC("somedata")); - } else if (mg_http_match_uri(hm, "/api/login")) { + } else if (mg_match(hm->uri, mg_str("/api/login"), NULL)) { mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%m,%m:%m}\n", MG_ESC("user"), MG_ESC(u->name), MG_ESC("token"), MG_ESC(u->token)); diff --git a/examples/webui-plain/main.c b/examples/webui-plain/main.c index c126798e..5a6b079f 100644 --- a/examples/webui-plain/main.c +++ b/examples/webui-plain/main.c @@ -36,12 +36,12 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { s_config.sub = strdup(MQTT_SUBSCRIBE_TOPIC); } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/config/get")) { + if (mg_match(hm->uri, mg_str("/api/config/get"), NULL)) { mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%m,%m:%m,%m:%m}\n", MG_ESC("url"), MG_ESC(s_config.url), MG_ESC("pub"), MG_ESC(s_config.pub), MG_ESC("sub"), MG_ESC(s_config.sub)); - } else if (mg_http_match_uri(hm, "/api/config/set")) { + } else if (mg_match(hm->uri, mg_str("/api/config/set"), NULL)) { struct mg_str json = hm->body; update_config(json, "$.url", &s_config.url); update_config(json, "$.pub", &s_config.pub); diff --git a/examples/webui-push-rest/main.c b/examples/webui-push-rest/main.c index 0ef3b731..aefad861 100644 --- a/examples/webui-push-rest/main.c +++ b/examples/webui-push-rest/main.c @@ -40,7 +40,7 @@ static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) { static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = ev_data; - if (mg_http_match_uri(hm, "/api/data")) { + if (mg_match(hm->uri, mg_str("/api/data"), NULL)) { long version = getparam(hm, "$.version"); if (version > 0 && version == s_version) { // Version match: no changes diff --git a/examples/webui-push-ws/main.c b/examples/webui-push-ws/main.c index f66fdcdb..7f60e45c 100644 --- a/examples/webui-push-ws/main.c +++ b/examples/webui-push-ws/main.c @@ -13,7 +13,7 @@ static const char *s_web_root = "web_root"; static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/watch")) { + if (mg_match(hm->uri, mg_str("/api/watch"), NULL)) { mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to Websocket c->data[0] = 'W'; // Set some unique mark on the connection } else { diff --git a/examples/webui-rest/main.c b/examples/webui-rest/main.c index 43177703..9b5c83e7 100644 --- a/examples/webui-rest/main.c +++ b/examples/webui-rest/main.c @@ -16,10 +16,10 @@ static const char *s_root_dir = "web_root"; static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/f1")) { + if (mg_match(hm->uri, mg_str("/api/f1"), NULL)) { mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%d}\n", MG_ESC("result"), 123); - } else if (mg_http_match_uri(hm, "/api/sum")) { + } else if (mg_match(hm->uri, mg_str("/api/sum"), NULL)) { // Attempt to fetch a JSON array from the body, hm->body struct mg_str json = hm->body; double num1, num2; diff --git a/examples/wifi-router-dashboard/net.c b/examples/wifi-router-dashboard/net.c index 6ab4461b..131400ba 100644 --- a/examples/wifi-router-dashboard/net.c +++ b/examples/wifi-router-dashboard/net.c @@ -282,23 +282,23 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct user *u = authenticate(hm); - if (mg_http_match_uri(hm, "/api/#") && u == NULL) { + if (mg_match(hm->uri, mg_str("/api/#"), NULL) && u == NULL) { mg_http_reply(c, 403, "", "Not Authorised\n"); - } else if (mg_http_match_uri(hm, "/api/login")) { + } else if (mg_match(hm->uri, mg_str("/api/login"), NULL)) { handle_login(c, u); - } else if (mg_http_match_uri(hm, "/api/logout")) { + } else if (mg_match(hm->uri, mg_str("/api/logout"), NULL)) { handle_logout(c); - } else if (mg_http_match_uri(hm, "/api/debug")) { + } else if (mg_match(hm->uri, mg_str("/api/debug"), NULL)) { handle_debug(c, hm); - } else if (mg_http_match_uri(hm, "/api/stats/get")) { + } else if (mg_match(hm->uri, mg_str("/api/stats/get"), NULL)) { handle_stats_get(c); - } else if (mg_http_match_uri(hm, "/api/events/get")) { + } else if (mg_match(hm->uri, mg_str("/api/events/get"), NULL)) { handle_events_get(c); - } else if (mg_http_match_uri(hm, "/api/devices/get")) { + } else if (mg_match(hm->uri, mg_str("/api/devices/get"), NULL)) { handle_devices_get(c); - } else if (mg_http_match_uri(hm, "/api/dhcp/get")) { + } else if (mg_match(hm->uri, mg_str("/api/dhcp/get"), NULL)) { handle_dhcp_get(c); - } else if (mg_http_match_uri(hm, "/api/dhcp/set")) { + } else if (mg_match(hm->uri, mg_str("/api/dhcp/set"), NULL)) { handle_dhcp_set(c, hm->body); } else { struct mg_http_serve_opts opts; diff --git a/examples/zephyr/http-server/src/main.c b/examples/zephyr/http-server/src/main.c index 7b3a26fa..8b19e91a 100644 --- a/examples/zephyr/http-server/src/main.c +++ b/examples/zephyr/http-server/src/main.c @@ -19,7 +19,7 @@ static void wcb(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = ev_data; MG_INFO(("%.*s %.*s %ld", (int) hm->method.len, hm->method.buf, (int) hm->uri.len, hm->uri.buf, (long) hm->body.len)); - if (mg_http_match_uri(hm, "/api/#")) { // REST API requests + if (mg_match(hm->uri, mg_str("/api/#"), NULL)) { // REST API requests // Print some statistics about currently established connections mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); mg_http_printf_chunk(c, "ID PROTO TYPE LOCAL REMOTE\n"); diff --git a/examples/zephyr/websocket-server/src/main.c b/examples/zephyr/websocket-server/src/main.c index d12ed0c9..a1c7642d 100644 --- a/examples/zephyr/websocket-server/src/main.c +++ b/examples/zephyr/websocket-server/src/main.c @@ -21,11 +21,11 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { mg_tls_init(c, &opts); } 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")) { + if (mg_match(hm->uri, mg_str("/websocket"), NULL)) { // Upgrade to websocket. From now on, a connection is a full-duplex // Websocket connection, which will receive MG_EV_WS_MSG events. mg_ws_upgrade(c, hm, NULL); - } else if (mg_http_match_uri(hm, "/rest")) { + } else if (mg_match(hm->uri, mg_str("/rest"), NULL)) { // Serve REST response mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123); } diff --git a/mongoose.c b/mongoose.c index 1e137524..0e6ac4ab 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3130,10 +3130,6 @@ struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v) { return mg_str_n(NULL, 0); } -bool mg_http_match_uri(const struct mg_http_message *hm, const char *glob) { - return mg_match(hm->uri, mg_str(glob), NULL); -} - long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, struct mg_fs *fs, const char *dir, size_t max_size) { char buf[20] = "0", file[MG_PATH_MAX], path[MG_PATH_MAX]; @@ -3295,11 +3291,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) { static void mg_hfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/quit")) { + if (mg_match(hm->uri, mg_str("/quit"), NULL)) { mg_http_reply(c, 200, "", "ok\n"); c->is_draining = 1; c->data[0] = 'X'; - } else if (mg_http_match_uri(hm, "/debug")) { + } else if (mg_match(hm->uri, mg_str("/debug"), NULL)) { int level = (int) mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG); mg_log_set(level); mg_http_reply(c, 200, "", "Debug level set to %d\n", level); diff --git a/mongoose.h b/mongoose.h index 971a7c0b..1cbfb414 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2153,7 +2153,6 @@ int mg_http_get_var(const struct mg_str *, const char *name, char *, size_t); int mg_url_decode(const char *s, size_t n, char *to, size_t to_len, int form); size_t mg_url_encode(const char *s, size_t n, char *buf, size_t len); void mg_http_creds(struct mg_http_message *, char *, size_t, char *, size_t); -bool mg_http_match_uri(const struct mg_http_message *, const char *glob); long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, struct mg_fs *fs, const char *dir, size_t max_size); void mg_http_bauth(struct mg_connection *, const char *user, const char *pass); diff --git a/src/http.c b/src/http.c index 5b8dd5cc..ffc7ed7a 100644 --- a/src/http.c +++ b/src/http.c @@ -914,10 +914,6 @@ struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v) { return mg_str_n(NULL, 0); } -bool mg_http_match_uri(const struct mg_http_message *hm, const char *glob) { - return mg_match(hm->uri, mg_str(glob), NULL); -} - long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, struct mg_fs *fs, const char *dir, size_t max_size) { char buf[20] = "0", file[MG_PATH_MAX], path[MG_PATH_MAX]; @@ -1079,11 +1075,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) { static void mg_hfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/quit")) { + if (mg_match(hm->uri, mg_str("/quit"), NULL)) { mg_http_reply(c, 200, "", "ok\n"); c->is_draining = 1; c->data[0] = 'X'; - } else if (mg_http_match_uri(hm, "/debug")) { + } else if (mg_match(hm->uri, mg_str("/debug"), NULL)) { int level = (int) mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG); mg_log_set(level); mg_http_reply(c, 200, "", "Debug level set to %d\n", level); diff --git a/src/http.h b/src/http.h index 17a28b8b..02c067d2 100644 --- a/src/http.h +++ b/src/http.h @@ -57,7 +57,6 @@ int mg_http_get_var(const struct mg_str *, const char *name, char *, size_t); int mg_url_decode(const char *s, size_t n, char *to, size_t to_len, int form); size_t mg_url_encode(const char *s, size_t n, char *buf, size_t len); void mg_http_creds(struct mg_http_message *, char *, size_t, char *, size_t); -bool mg_http_match_uri(const struct mg_http_message *, const char *glob); long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, struct mg_fs *fs, const char *dir, size_t max_size); void mg_http_bauth(struct mg_connection *, const char *user, const char *pass); diff --git a/test/unit_test.c b/test/unit_test.c index 3500e8a4..982c5bfe 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -630,35 +630,35 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; MG_INFO(("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.buf, (int) hm->uri.len, hm->uri.buf, (int) hm->message.len)); - if (mg_http_match_uri(hm, "/foo/*")) { + if (mg_match(hm->uri, mg_str("/foo/*"), NULL)) { mg_http_reply(c, 200, "", "uri: %.*s", hm->uri.len - 5, hm->uri.buf + 5); - } else if (mg_http_match_uri(hm, "/ws")) { + } else if (mg_match(hm->uri, mg_str("/ws"), NULL)) { mg_ws_upgrade(c, hm, NULL); - } else if (mg_http_match_uri(hm, "/body")) { + } else if (mg_match(hm->uri, mg_str("/body"), NULL)) { mg_http_reply(c, 200, "", "%.*s", (int) hm->body.len, hm->body.buf); - } else if (mg_http_match_uri(hm, "/bar")) { + } else if (mg_match(hm->uri, mg_str("/bar"), NULL)) { mg_http_reply(c, 404, "", "not found"); - } else if (mg_http_match_uri(hm, "/no_reason")) { + } else if (mg_match(hm->uri, mg_str("/no_reason"), NULL)) { mg_printf(c, "%s", "HTTP/1.0 200\r\nContent-Length: 2\r\n\r\nok"); - } else if (mg_http_match_uri(hm, "/badroot")) { + } else if (mg_match(hm->uri, mg_str("/badroot"), NULL)) { struct mg_http_serve_opts sopts; memset(&sopts, 0, sizeof(sopts)); sopts.root_dir = "/BAAADDD!"; mg_http_serve_dir(c, hm, &sopts); - } else if (mg_http_match_uri(hm, "/creds")) { + } else if (mg_match(hm->uri, mg_str("/creds"), NULL)) { char user[100], pass[100]; mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass)); mg_http_reply(c, 200, "", "[%s]:[%s]", user, pass); - } else if (mg_http_match_uri(hm, "/upload")) { + } else if (mg_match(hm->uri, mg_str("/upload"), NULL)) { mg_http_upload(c, hm, &mg_fs_posix, ".", 99999); c->is_hexdumping = 1; - } else if (mg_http_match_uri(hm, "/test/")) { + } else if (mg_match(hm->uri, mg_str("/test/"), NULL)) { struct mg_http_serve_opts sopts; memset(&sopts, 0, sizeof(sopts)); sopts.root_dir = "."; sopts.extra_headers = "A: B\r\nC: D\r\n"; mg_http_serve_dir(c, hm, &sopts); - } else if (mg_http_match_uri(hm, "/servefile")) { + } else if (mg_match(hm->uri, mg_str("/servefile"), NULL)) { struct mg_http_serve_opts sopts; memset(&sopts, 0, sizeof(sopts)); sopts.mime_types = "foo=a/b,txt=c/d"; @@ -1116,13 +1116,13 @@ static void h4(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; MG_INFO(("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.buf, (int) hm->uri.len, hm->uri.buf, (int) hm->message.len)); - if (mg_http_match_uri(hm, "/a/#")) { + if (mg_match(hm->uri, mg_str("/a/#"), NULL)) { 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/#")) { + } else if (mg_match(hm->uri, mg_str("/b/#"), NULL)) { struct mg_http_serve_opts opts; memset(&opts, 0, sizeof(opts)); opts.root_dir = "/b=./test/data"; @@ -2343,7 +2343,7 @@ static void test_crc32(void) { static void us(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (ev == MG_EV_HTTP_MSG && mg_http_match_uri(hm, "/upload")) { + if (ev == MG_EV_HTTP_MSG && mg_match(hm->uri, mg_str("/upload"), NULL)) { MG_DEBUG(("Got all %lu bytes!", (unsigned long) hm->body.len)); MG_DEBUG(("Query string: [%.*s]", (int) hm->query.len, hm->query.buf)); // MG_DEBUG(("Body:\n%.*s", (int) hm->body.len, hm->body.buf));