From 7285b89981dfffd9cecdb08ab2d9a5a102bc229a Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 25 Jul 2024 23:32:39 +0300 Subject: [PATCH] ESP8266WebServer: pathArgs() to complement pathArg(N) similar to ::args(), returns number of parsed path arguments shortened #9100 --- .../examples/PathArgServer/PathArgServer.ino | 17 +++++++++++++++++ .../src/ESP8266WebServer-impl.h | 7 +++++++ .../ESP8266WebServer/src/ESP8266WebServer.h | 2 ++ .../src/detail/RequestHandler.h | 10 +++++++--- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WebServer/examples/PathArgServer/PathArgServer.ino b/libraries/ESP8266WebServer/examples/PathArgServer/PathArgServer.ino index 4e2ef12ce0..8c63b0fba1 100644 --- a/libraries/ESP8266WebServer/examples/PathArgServer/PathArgServer.ino +++ b/libraries/ESP8266WebServer/examples/PathArgServer/PathArgServer.ino @@ -16,6 +16,15 @@ const char *password = STAPSK; ESP8266WebServer server(80); +bool checkPathArgs(int number) { + if (server.pathArgs() == number) { + return true; + } + + server.send(500, "text/plain", "request handler received unexpected number of path arguments"); + return false; +} + void setup(void) { Serial.begin(115200); WiFi.mode(WIFI_STA); @@ -40,11 +49,19 @@ void setup(void) { }); server.on(UriBraces("/users/{}"), []() { + if (!checkPathArgs(1)) { + return; + } + String user = server.pathArg(0); server.send(200, "text/plain", "User: '" + user + "'"); }); server.on(UriRegex("^\\/users\\/([0-9]+)\\/devices\\/([0-9]+)$"), []() { + if (!checkPathArgs(2)) { + return; + } + String user = server.pathArg(0); String device = server.pathArg(1); server.send(200, "text/plain", "User: '" + user + "' and Device: '" + device + "'"); diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h index dbcb251135..24502c99d8 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h @@ -656,6 +656,13 @@ void ESP8266WebServerTemplate::_streamFileCore(const size_t fileSize send(200, contentType, emptyString); } +template +size_t ESP8266WebServerTemplate::pathArgs() const { + if (_currentHandler != nullptr) + return _currentHandler->pathArgs(); + return 0; +} + template const String& ESP8266WebServerTemplate::pathArg(unsigned int i) const { if (_currentHandler != nullptr) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index f9eca1c8bc..b5f8118767 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -142,6 +142,8 @@ class ESP8266WebServerTemplate ServerType &getServer() { return _server; } const String& pathArg(unsigned int i) const; // get request path argument by number + int pathArgs() const; // get path arguments count + const String& arg(const String& name) const; // get request argument value by name const String& arg(int i) const; // get request argument value by number const String& argName(int i) const; // get request argument name by number diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandler.h b/libraries/ESP8266WebServer/src/detail/RequestHandler.h index c373c58f1b..af4ff6bef8 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandler.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandler.h @@ -16,7 +16,7 @@ class RequestHandler { /* note: old handler API for backward compatibility */ - + virtual bool canHandle(HTTPMethod method, const String& uri) { (void) method; (void) uri; @@ -43,7 +43,7 @@ class RequestHandler { return false; } virtual bool handle(WebServerType& server, HTTPMethod requestMethod, const String& requestUri) { - (void) server; + (void) server; (void) requestMethod; (void) requestUri; return false; @@ -74,7 +74,11 @@ class RequestHandler { std::vector pathArgs; public: - const String& pathArg(unsigned int i) { + size_t pathArgs() const { + return pathArgs.size(); + } + + const String& pathArg(unsigned int i) const { assert(i < pathArgs.size()); return pathArgs[i]; }