diff --git a/examples/Json/Json.ino b/examples/Json/Json.ino index 69ab251e..45b5f91c 100644 --- a/examples/Json/Json.ino +++ b/examples/Json/Json.ino @@ -100,6 +100,19 @@ void setup() { response->setLength(); request->send(response); }); + + +#ifdef ASYNCWEBSERVER_REGEX + server.on("^\\/page\\/(\\d+)$", HTTP_POST, [](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); + }); +#endif #endif server.begin(); diff --git a/platformio.ini b/platformio.ini index e961548d..3eb0b758 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 diff --git a/src/AsyncJson.cpp b/src/AsyncJson.cpp index c6643f76..16157e3a 100644 --- a/src/AsyncJson.cpp +++ b/src/AsyncJson.cpp @@ -116,7 +116,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 { @@ -124,6 +124,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; } diff --git a/src/AsyncJson.h b/src/AsyncJson.h index 42ec7b5e..87dc71a2 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -87,8 +87,10 @@ class AsyncMessagePackResponse : public AsyncJsonResponse { // Body handler supporting both content types: JSON and MessagePack class AsyncCallbackJsonWebHandler : public AsyncWebHandler { +private: protected: String _uri; + bool _isRegex; WebRequestMethodComposite _method; ArJsonRequestHandlerFunction _onRequest; #if ARDUINOJSON_VERSION_MAJOR == 6 diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index d0b20c19..2f73e0c3 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -220,6 +220,7 @@ class AsyncWebServerRequest { using FS = fs::FS; friend class AsyncWebServer; friend class AsyncCallbackWebHandler; + friend class AsyncCallbackJsonWebHandler; friend class AsyncFileResponse; friend class AsyncStaticWebHandler;