Skip to content

Feature/path arguments #5214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add regex support
  • Loading branch information
bmooij-beeliners committed Nov 1, 2019
commit 879e076dd774f9afb791c350a50f284ebef83bfa
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void setup(void) {
server.send(200, "text/plain", "User: '" + user + "'");
});

server.on("/users/{}/devices/{}", []() {
server.on("^\\/users\\/([0-9]+)\\/devices\\/([0-9]+)$", []() {
String user = server.pathArg(0);
String device = server.pathArg(1);
server.send(200, "text/plain", "User: '" + user + "' and Device: '" + device + "'");
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WebServer/src/detail/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class RequestHandler {

protected:
std::vector<String> pathArgs;
bool _isRegex = false;

public:
const String& pathArg(unsigned int i) {
Expand Down
61 changes: 24 additions & 37 deletions libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define REQUESTHANDLERSIMPL_H

#include <ESP8266WebServer.h>
#include <string>
#include <regex>
#include "RequestHandler.h"
#include "mimetable.h"
#include "WString.h"
Expand All @@ -18,15 +20,16 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
, _uri(uri)
, _method(method)
{
int numParams = 0, start = 0;
do {
start = _uri.indexOf("{}", start);
if (start > 0) {
numParams++;
start += 2;
}
} while (start > 0);
pathArgs.resize(numParams);
_isRegex = uri.startsWith("^") && uri.endsWith("$");
if (_isRegex) {
std::regex rgx((_uri + "|").c_str());
std::smatch matches;
std::string s{""};
std::regex_search(s, matches, rgx);
pathArgs.resize(matches.size() - 1);
} else {
pathArgs.resize(0);
}
}

bool canHandle(HTTPMethod requestMethod, String requestUri) override {
Expand All @@ -36,37 +39,21 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
if (_uri == requestUri)
return true;

size_t uriLength = _uri.length();
unsigned int pathArgIndex = 0;
unsigned int requestUriIndex = 0;
for (unsigned int i = 0; i < uriLength; i++, requestUriIndex++) {
char uriChar = _uri[i];
char requestUriChar = requestUri[requestUriIndex];

if (uriChar == requestUriChar)
continue;
if (uriChar != '{')
return false;

i += 2; // index of char after '}'
if (i >= uriLength) {
// there is no char after '}'
pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex);
return pathArgs[pathArgIndex].indexOf("/") == -1; // path argument may not contain a '/'
}
else
{
char charEnd = _uri[i];
int uriIndex = requestUri.indexOf(charEnd, requestUriIndex);
if (uriIndex < 0)
return false;
pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex, uriIndex);
requestUriIndex = (unsigned int) uriIndex;
if (_isRegex) {
unsigned int pathArgIndex = 0;
std::regex rgx(_uri.c_str());
std::smatch matches;
std::string s(requestUri.c_str());
if (std::regex_search(s, matches, rgx)) {
for (size_t i = 1; i < matches.size(); ++i) { // skip first
pathArgs[pathArgIndex] = String(matches[i].str().c_str());
pathArgIndex++;
}
return true;
}
pathArgIndex++;
}

return requestUriIndex >= requestUri.length();
return false;
}

bool canUpload(String requestUri) override {
Expand Down