2
2
#define REQUESTHANDLERSIMPL_H
3
3
4
4
#include < ESP8266WebServer.h>
5
+ #include < string>
6
+ #include < regex>
5
7
#include " RequestHandler.h"
6
8
#include " mimetable.h"
7
9
#include " WString.h"
@@ -18,15 +20,16 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
18
20
, _uri(uri)
19
21
, _method(method)
20
22
{
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
+ }
30
33
}
31
34
32
35
bool canHandle (HTTPMethod requestMethod, String requestUri) override {
@@ -36,37 +39,21 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
36
39
if (_uri == requestUri)
37
40
return true ;
38
41
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 ;
65
53
}
66
- pathArgIndex++;
67
54
}
68
55
69
- return requestUriIndex >= requestUri. length () ;
56
+ return false ;
70
57
}
71
58
72
59
bool canUpload (String requestUri) override {
0 commit comments