Skip to content

Commit 879e076

Browse files
Add regex support
1 parent de7b504 commit 879e076

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

Diff for: libraries/ESP8266WebServer/examples/PathArgServer/PathArgServer.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void setup(void) {
3838
server.send(200, "text/plain", "User: '" + user + "'");
3939
});
4040

41-
server.on("/users/{}/devices/{}", []() {
41+
server.on("^\\/users\\/([0-9]+)\\/devices\\/([0-9]+)$", []() {
4242
String user = server.pathArg(0);
4343
String device = server.pathArg(1);
4444
server.send(200, "text/plain", "User: '" + user + "' and Device: '" + device + "'");

Diff for: libraries/ESP8266WebServer/src/detail/RequestHandler.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class RequestHandler {
2323

2424
protected:
2525
std::vector<String> pathArgs;
26+
bool _isRegex = false;
2627

2728
public:
2829
const String& pathArg(unsigned int i) {

Diff for: libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

+24-37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define REQUESTHANDLERSIMPL_H
33

44
#include <ESP8266WebServer.h>
5+
#include <string>
6+
#include <regex>
57
#include "RequestHandler.h"
68
#include "mimetable.h"
79
#include "WString.h"
@@ -18,15 +20,16 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
1820
, _uri(uri)
1921
, _method(method)
2022
{
21-
int numParams = 0, start = 0;
22-
do {
23-
start = _uri.indexOf("{}", start);
24-
if (start > 0) {
25-
numParams++;
26-
start += 2;
27-
}
28-
} while (start > 0);
29-
pathArgs.resize(numParams);
23+
_isRegex = uri.startsWith("^") && uri.endsWith("$");
24+
if (_isRegex) {
25+
std::regex rgx((_uri + "|").c_str());
26+
std::smatch matches;
27+
std::string s{""};
28+
std::regex_search(s, matches, rgx);
29+
pathArgs.resize(matches.size() - 1);
30+
} else {
31+
pathArgs.resize(0);
32+
}
3033
}
3134

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

39-
size_t uriLength = _uri.length();
40-
unsigned int pathArgIndex = 0;
41-
unsigned int requestUriIndex = 0;
42-
for (unsigned int i = 0; i < uriLength; i++, requestUriIndex++) {
43-
char uriChar = _uri[i];
44-
char requestUriChar = requestUri[requestUriIndex];
45-
46-
if (uriChar == requestUriChar)
47-
continue;
48-
if (uriChar != '{')
49-
return false;
50-
51-
i += 2; // index of char after '}'
52-
if (i >= uriLength) {
53-
// there is no char after '}'
54-
pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex);
55-
return pathArgs[pathArgIndex].indexOf("/") == -1; // path argument may not contain a '/'
56-
}
57-
else
58-
{
59-
char charEnd = _uri[i];
60-
int uriIndex = requestUri.indexOf(charEnd, requestUriIndex);
61-
if (uriIndex < 0)
62-
return false;
63-
pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex, uriIndex);
64-
requestUriIndex = (unsigned int) uriIndex;
42+
if (_isRegex) {
43+
unsigned int pathArgIndex = 0;
44+
std::regex rgx(_uri.c_str());
45+
std::smatch matches;
46+
std::string s(requestUri.c_str());
47+
if (std::regex_search(s, matches, rgx)) {
48+
for (size_t i = 1; i < matches.size(); ++i) { // skip first
49+
pathArgs[pathArgIndex] = String(matches[i].str().c_str());
50+
pathArgIndex++;
51+
}
52+
return true;
6553
}
66-
pathArgIndex++;
6754
}
6855

69-
return requestUriIndex >= requestUri.length();
56+
return false;
7057
}
7158

7259
bool canUpload(String requestUri) override {

0 commit comments

Comments
 (0)