Skip to content

Commit d9ced97

Browse files
author
Andreas Horn
committed
implements pathArgs method in analogy to the args method
This allows to use UriRegex with optional capture groups and therefore varying number of path arguments.
1 parent 4eeee1c commit d9ced97

File tree

8 files changed

+27
-16
lines changed

8 files changed

+27
-16
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,13 @@ const String& ESP8266WebServerTemplate<ServerType>::pathArg(unsigned int i) cons
588588
return emptyString;
589589
}
590590

591+
template <typename ServerType>
592+
const int ESP8266WebServerTemplate<ServerType>::pathArgs() const {
593+
if (_currentHandler != nullptr)
594+
return _currentHandler->pathArgs();
595+
return 0;
596+
}
597+
591598
template <typename ServerType>
592599
const String& ESP8266WebServerTemplate<ServerType>::arg(const String& name) const {
593600
for (int i = 0; i < _currentArgCount + _currentArgsHavePlain; ++i) {

libraries/ESP8266WebServer/src/ESP8266WebServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class ESP8266WebServerTemplate
136136
ServerType &getServer() { return _server; }
137137

138138
const String& pathArg(unsigned int i) const; // get request path argument by number
139+
const int pathArgs() const; // get path arguments count
139140
const String& arg(const String& name) const; // get request argument value by name
140141
const String& arg(int i) const; // get request argument value by number
141142
const String& argName(int i) const; // get request argument name by number

libraries/ESP8266WebServer/src/Uri.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Uri {
1919
return new Uri(_uri);
2020
};
2121

22-
virtual bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &pathArgs) {
22+
virtual bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &currentPathArgs) {
2323
return _uri == requestUri;
2424
}
2525
};

libraries/ESP8266WebServer/src/detail/RequestHandler.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ class RequestHandler {
2424
RequestHandler<ServerType>* _next = nullptr;
2525

2626
protected:
27-
std::vector<String> pathArgs;
27+
std::vector<String> currentPathArgs;
2828

2929
public:
3030
const String& pathArg(unsigned int i) {
31-
assert(i < pathArgs.size());
32-
return pathArgs[i];
31+
assert(i < currentPathArgs.size());
32+
return currentPathArgs[i];
33+
}
34+
const int pathArgs() {
35+
return currentPathArgs.size();
3336
}
3437
};
3538

libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
4949
if (_method != HTTP_ANY && _method != requestMethod)
5050
return false;
5151

52-
return _uri->canHandle(requestUri, RequestHandler<ServerType>::pathArgs);
52+
return _uri->canHandle(requestUri, RequestHandler<ServerType>::currentPathArgs);
5353
}
5454

5555
bool canUpload(const String& requestUri) override {

libraries/ESP8266WebServer/src/uri/UriBraces.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class UriBraces : public Uri {
1313
return new UriBraces(_uri);
1414
};
1515

16-
bool canHandle(const String &requestUri, std::vector<String> &pathArgs) override final {
17-
if (Uri::canHandle(requestUri, pathArgs))
16+
bool canHandle(const String &requestUri, std::vector<String> &currentPathArgs) override final {
17+
if (Uri::canHandle(requestUri, currentPathArgs))
1818
return true;
1919

20-
pathArgs.clear();
20+
currentPathArgs.clear();
2121

2222
size_t uriLength = _uri.length();
2323
unsigned int requestUriIndex = 0;
@@ -33,16 +33,16 @@ class UriBraces : public Uri {
3333
i += 2; // index of char after '}'
3434
if (i >= uriLength) {
3535
// there is no char after '}'
36-
pathArgs.push_back(requestUri.substring(requestUriIndex));
37-
return pathArgs.back().indexOf("/") == -1; // path argument may not contain a '/'
36+
currentPathArgs.push_back(requestUri.substring(requestUriIndex));
37+
return currentPathArgs.back().indexOf("/") == -1; // path argument may not contain a '/'
3838
}
3939
else
4040
{
4141
char charEnd = _uri[i];
4242
int uriIndex = requestUri.indexOf(charEnd, requestUriIndex);
4343
if (uriIndex < 0)
4444
return false;
45-
pathArgs.push_back(requestUri.substring(requestUriIndex, uriIndex));
45+
currentPathArgs.push_back(requestUri.substring(requestUriIndex, uriIndex));
4646
requestUriIndex = (unsigned int) uriIndex;
4747
}
4848
}

libraries/ESP8266WebServer/src/uri/UriGlob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UriGlob : public Uri {
1414
return new UriGlob(_uri);
1515
};
1616

17-
bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &pathArgs) override final {
17+
bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &currentPathArgs) override final {
1818
return fnmatch(_uri.c_str(), requestUri.c_str(), 0) == 0;
1919
}
2020
};

libraries/ESP8266WebServer/src/uri/UriRegex.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ class UriRegex : public Uri {
2828
return new UriRegex(_uri);
2929
};
3030

31-
bool canHandle(const String &requestUri, std::vector<String> &pathArgs) override final {
32-
if (Uri::canHandle(requestUri, pathArgs))
31+
bool canHandle(const String &requestUri, std::vector<String> &currentPathArgs) override final {
32+
if (Uri::canHandle(requestUri, currentPathArgs))
3333
return true;
3434

3535
regmatch_t groupArray[REGEX_MAX_GROUPS];
3636
if (regexec(&_regexCompiled, requestUri.c_str(), REGEX_MAX_GROUPS, groupArray, 0) == 0) {
3737
// matches
38-
pathArgs.clear();
38+
currentPathArgs.clear();
3939

4040
unsigned int g = 1;
4141
for (; g < REGEX_MAX_GROUPS; g++) {
4242
if (groupArray[g].rm_so == (long int)-1)
4343
break; // No more groups
4444

45-
pathArgs.push_back(requestUri.substring(groupArray[g].rm_so, groupArray[g].rm_eo));
45+
currentPathArgs.push_back(requestUri.substring(groupArray[g].rm_so, groupArray[g].rm_eo));
4646
}
4747

4848
return true;

0 commit comments

Comments
 (0)