From 77a8e7f648d78bb146c4d1c22f757d96f769f0c2 Mon Sep 17 00:00:00 2001 From: Nicola <61830443+nicola02nb@users.noreply.github.com> Date: Tue, 30 Sep 2025 17:14:40 +0200 Subject: [PATCH 1/6] Enhance AsyncCallbackJsonWebHandler with regex support Added regex support for URI handling in AsyncCallbackJsonWebHandler. --- src/AsyncJson.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/AsyncJson.cpp b/src/AsyncJson.cpp index 03e0015bb..ecaf49bf5 100644 --- a/src/AsyncJson.cpp +++ b/src/AsyncJson.cpp @@ -93,7 +93,7 @@ AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(const String &uri, ArJs : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {} #else AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(const String &uri, ArJsonRequestHandlerFunction onRequest) - : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} + : _uri(uri), _isRegex(uri.startsWith("^") && uri.endsWith("$")), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} #endif bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) const { @@ -101,6 +101,21 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) cons return false; } +#ifdef ASYNCWEBSERVER_REGEX + if (_isRegex) { + std::regex pattern(_uri.c_str()); + std::smatch matches; + std::string s(request->url().c_str()); + if (std::regex_search(s, matches, pattern)) { + for (size_t i = 1; i < matches.size(); ++i) { // start from 1 + request->_addPathParam(matches[i].str().c_str()); + } + } else { + return false; + } + } else +#endif + if (_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri + "/"))) { return false; } From 98fdab799f3d2a6d2e7d909a37febf0c2ee21570 Mon Sep 17 00:00:00 2001 From: Nicola <61830443+nicola02nb@users.noreply.github.com> Date: Tue, 30 Sep 2025 17:19:41 +0200 Subject: [PATCH 2/6] Add _isRegex member to AsyncCallbackJsonWebHandler --- src/AsyncJson.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index 2194069d1..4ddcc244d 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -75,6 +75,7 @@ class PrettyAsyncJsonResponse : public AsyncJsonResponse { typedef std::function ArJsonRequestHandlerFunction; class AsyncCallbackJsonWebHandler : public AsyncWebHandler { +private: protected: String _uri; WebRequestMethodComposite _method; @@ -83,6 +84,7 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler { size_t maxJsonBufferSize; #endif size_t _maxContentLength; + bool _isRegex; public: #if ARDUINOJSON_VERSION_MAJOR == 6 From 629ff60b964a4899ce4c00cc94b031a2b6c5203b Mon Sep 17 00:00:00 2001 From: Nicola <61830443+nicola02nb@users.noreply.github.com> Date: Wed, 1 Oct 2025 09:34:35 +0200 Subject: [PATCH 3/6] Added AsyncCallbackJsonWebHandler as friend of AsyncWebServerRequest --- src/ESPAsyncWebServer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 9c150dbaa..d4ce7ca7a 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -203,6 +203,7 @@ class AsyncWebServerRequest { using FS = fs::FS; friend class AsyncWebServer; friend class AsyncCallbackWebHandler; + friend class AsyncCallbackJsonWebHandler; friend class AsyncFileResponse; private: From 75da329aa78f516d335facf7d3a2acd615ee3387 Mon Sep 17 00:00:00 2001 From: Nicola <61830443+nicola02nb@users.noreply.github.com> Date: Wed, 1 Oct 2025 09:43:26 +0200 Subject: [PATCH 4/6] Fixed warning --- src/AsyncJson.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index 4ddcc244d..71c61d64c 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -78,13 +78,13 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler { private: protected: String _uri; + bool _isRegex; WebRequestMethodComposite _method; ArJsonRequestHandlerFunction _onRequest; #if ARDUINOJSON_VERSION_MAJOR == 6 size_t maxJsonBufferSize; #endif size_t _maxContentLength; - bool _isRegex; public: #if ARDUINOJSON_VERSION_MAJOR == 6 From 9925ccd3b2a04e136c2a223b82522579d958bfa1 Mon Sep 17 00:00:00 2001 From: Nicola <61830443+nicola02nb@users.noreply.github.com> Date: Wed, 1 Oct 2025 09:45:21 +0200 Subject: [PATCH 5/6] Added json webserver with regex example --- examples/Json/Json.ino | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/examples/Json/Json.ino b/examples/Json/Json.ino index a29fec1b2..c9938c9d2 100644 --- a/examples/Json/Json.ino +++ b/examples/Json/Json.ino @@ -92,6 +92,21 @@ void setup() { }); server.addHandler(handler); + +#ifdef ASYNCWEBSERVER_REGEX + AsyncCallbackJsonWebHandler *pageRegexHandler = new AsyncCallbackJsonWebHandler("^\\/page\\/(\\d+)$", + [](AsyncWebServerRequest *request, JsonVariant &json) { + String number = request->pathArg(0); + AsyncJsonResponse *response = new AsyncJsonResponse(); + JsonObject root = response->getRoot().to(); + root["pageStr"] = number; + root["pageInt"] = number.toInt(); + response->setLength(); + request->send(response); + }); + server.addHandler(pageRegexHandler); +#endif + #endif server.begin(); From c3c2e39778598db0dad7e5c2341d4ad4171583a7 Mon Sep 17 00:00:00 2001 From: Nicola <61830443+nicola02nb@users.noreply.github.com> Date: Wed, 1 Oct 2025 09:46:07 +0200 Subject: [PATCH 6/6] Added ASYNCWEBSERVER_REGEX build flag in platformio.ini --- platformio.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/platformio.ini b/platformio.ini index 338e97e74..157a71b4b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -58,6 +58,7 @@ build_flags = ; -D TAG=\"core\" ; -D LOG_LOCAL_LEVEL=ESP_LOG_VERBOSE ; -D ASYNCWEBSERVER_LOG_DEBUG + ; -D ASYNCWEBSERVER_REGEX upload_protocol = esptool monitor_speed = 115200 monitor_filters = esp32_exception_decoder, log2file