From 08dc8f93feefa2f45bcca675a9a66c538e054cc5 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 29 May 2024 15:35:41 +0300 Subject: [PATCH] net: http_server: Add wildcard support to resource strings Allow user to specify resource string using wildcard characters so that multiple URL paths can be served with just one handler. Fixes #73367 Signed-off-by: Jukka Rissanen --- .../networking/api/http_server.rst | 18 ++++++++++++++++++ subsys/net/lib/http/Kconfig | 8 ++++++++ subsys/net/lib/http/http_server_core.c | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/doc/connectivity/networking/api/http_server.rst b/doc/connectivity/networking/api/http_server.rst index 362d19ac374..11a0ca20807 100644 --- a/doc/connectivity/networking/api/http_server.rst +++ b/doc/connectivity/networking/api/http_server.rst @@ -130,6 +130,24 @@ Alternatively, an HTTPS service can be defined with with Once HTTP(s) service is defined, resources can be registered for it with :c:macro:`HTTP_RESOURCE_DEFINE` macro. +Application can enable resource wildcard support by enabling +:kconfig:option:`CONFIG_HTTP_SERVER_RESOURCE_WILDCARD` option. When this +option is set, then it is possible to match several incoming HTTP requests +with just one resource handler. The `fnmatch() +`__ +POSIX API function is used to match the pattern in the URL paths. + +Example: + +.. code-block:: c + + HTTP_RESOURCE_DEFINE(my_resource, my_service, "/foo*", &resource_detail); + +This would match all URLs that start with a string ``foo``. See +`POSIX.2 chapter 2.13 +`__ +for pattern matching syntax description. + Static resources ================ diff --git a/subsys/net/lib/http/Kconfig b/subsys/net/lib/http/Kconfig index 9926b9b1d38..e9639b0eeec 100644 --- a/subsys/net/lib/http/Kconfig +++ b/subsys/net/lib/http/Kconfig @@ -115,6 +115,14 @@ config HTTP_SERVER_WEBSOCKET handler that is called after upgrading to handle the Websocket network traffic. +config HTTP_SERVER_RESOURCE_WILDCARD + bool "Allow wildcard matching of resources" + select FNMATCH + help + Allow user to specify wildcards when setting up resource strings. + This means that instead of specifying multiple resources with exact + string matches, one resource handler could handle multiple URLs. + endif # Hidden option to avoid having multiple individual options that are ORed together diff --git a/subsys/net/lib/http/http_server_core.c b/subsys/net/lib/http/http_server_core.c index e0402259faf..cc1dc18f845 100644 --- a/subsys/net/lib/http/http_server_core.c +++ b/subsys/net/lib/http/http_server_core.c @@ -19,6 +19,7 @@ #include #include #include +#include LOG_MODULE_REGISTER(net_http_server, CONFIG_NET_HTTP_SERVER_LOG_LEVEL); @@ -661,6 +662,16 @@ struct http_resource_detail *get_resource_detail(const char *path, continue; } + if (IS_ENABLED(CONFIG_HTTP_SERVER_RESOURCE_WILDCARD)) { + int ret; + + ret = fnmatch(resource->resource, path, FNM_PATHNAME); + if (ret == 0) { + *path_len = strlen(resource->resource); + return resource->detail; + } + } + if (compare_strings(path, resource->resource) == 0) { NET_DBG("Got match for %s", resource->resource);