Skip to content

Commit

Permalink
ESP8266WebServer: pathArgs() to complement pathArg(N)
Browse files Browse the repository at this point in the history
similar to ::args(), returns number of parsed path arguments
shortened #9100
  • Loading branch information
mcspr committed Jul 25, 2024
1 parent 7e0d20e commit 7285b89
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 + "'");
Expand Down
7 changes: 7 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,13 @@ void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize
send(200, contentType, emptyString);
}

template <typename ServerType>
size_t ESP8266WebServerTemplate<ServerType>::pathArgs() const {
if (_currentHandler != nullptr)
return _currentHandler->pathArgs();
return 0;
}

template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::pathArg(unsigned int i) const {
if (_currentHandler != nullptr)
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions libraries/ESP8266WebServer/src/detail/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -74,7 +74,11 @@ class RequestHandler {
std::vector<String> 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];
}
Expand Down

0 comments on commit 7285b89

Please sign in to comment.