8000 Add Uri with support for regexUri and globUri by Bmooij · Pull Request #6696 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Add Uri with support for regexUri and globUri #6696

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

Merged
merged 28 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b80ea90
Add path args
bmooij-beeliners Oct 6, 2018
37f3d59
Add example
bmooij-beeliners Oct 6, 2018
41695e6
Update code format
bmooij-beeliners Oct 6, 2018
5dfa3f1
Add missing include
bmooij-beeliners Oct 6, 2018
ddb0f6f
Fix codestyle and unsigned int
bmooij-beeliners Oct 6, 2018
4e49e29
fix unsigned int
bmooij-beeliners Oct 6, 2018
1584859
Remove tabs
bmooij-beeliners Oct 8, 2018
da189ba
use vector<>.resize
bmooij-beeliners Oct 8, 2018
cabf620 8000
rename j to requestUriIndex
bmooij-beeliners Oct 8, 2018
7cd3468
using assert checking the path argument index
bmooij-beeliners Oct 9, 2018
637b9cb
Add missing include "assert.h"
bmooij-beeliners Oct 9, 2018
63163d7
The order no longer matters.
bmooij-beeliners Oct 9, 2018
a626c5f
make pathArg return a const
bmooij-beeliners Oct 9, 2018
977120c
Update PathArgServer.ino
d-a-v Oct 10, 2018
de7b504
const String&
d-a-v Nov 30, 2018
879e076
Add regex support
bmooij-beeliners Dec 24, 2018
afe7854
Fix to match templating
bmooij-beeliners Nov 1, 2019
8f5eda3
Add Uri with support for staticUri, regexUri and globUri
bmooij-beeliners Nov 1, 2019
2ee04d0
Update example
bmooij-beeliners Nov 1, 2019
3edf15a
Add deconstructor to remove _uri pointer
bmooij-beeliners Nov 1, 2019
820597c
Add newline to end of files
bmooij-beeliners Nov 1, 2019
8d95fb6
Suppress gcc warnings (unused params)
bmooij-beeliners Nov 7, 2019
a856527
Merge branch 'master' into feature/multi_uri
Bmooij Nov 28, 2019
87e823c
Merge branch 'master' into feature/multi_uri
d-a-v Nov 28, 2019
baa4c49
Replace regex with regex.h
bmooij-beeliners Dec 27, 2019
f470387
Merge branch 'master' into feature/multi_uri
earlephilhower Feb 22, 2020
bb77646
Use the standard STASSID/PSK settings for example
earlephilhower Feb 22, 2020
be78c3b
Use 115.2Kbaud for example, match others
earlephilhower Feb 22, 2020
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
Replace regex with regex.h
  • Loading branch information
bmooij-beeliners committed Dec 27, 2019
commit baa4c49e5b521f5769bac83a90282fe6af6128a7 8000
2 changes: 0 additions & 2 deletions libraries/ESP8266WebServer/src/Uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class Uri {
return new Uri(_uri);
};

virtual void initPathArgs(__attribute__((unused)) std::vector<String> &pathArgs) {}

virtual bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &pathArgs) {
return _uri == requestUri;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
, _uri(uri.clone())
, _method(method)
{
_uri->initPathArgs(RequestHandler<ServerType>::pathArgs);
}

~FunctionRequestHandler() {
Expand Down
22 changes: 5 additions & 17 deletions libraries/ESP8266WebServer/src/uri/UriBraces.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ class UriBraces : public Uri {
return new UriBraces(_uri);
};

void initPathArgs(std::vector<String> &pathArgs) override final {
int numParams = 0, start = 0;
do {
start = _uri.indexOf("{}", start 8000 );
if (start > 0) {
numParams++;
start += 2;
}
} while (start > 0);
pathArgs.resize(numParams);
}

bool canHandle(const String &requestUri, std::vector<String> &pathArgs) override final {
if (Uri::canHandle(requestUri, pathArgs))
return true;

pathArgs.clear();

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];
Expand All @@ -44,19 +33,18 @@ class UriBraces : public Uri {
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 '/'
pathArgs.push_back(requestUri.substring(requestUriIndex));
return pathArgs.back().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);
pathArgs.push_back(requestUri.substring(requestUriIndex, uriIndex));
requestUriIndex = (unsigned int) uriIndex;
}
pathArgIndex++;
}

return requestUriIndex >= requestUri.length();
Expand Down
48 changes: 29 additions & 19 deletions libraries/ESP8266WebServer/src/uri/UriRegex.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,49 @@
#define URI_REGEX_H

#include "Uri.h"
#include <regex>
#include <regex.h>
#include <assert.h>

#ifndef REGEX_MAX_GROUPS
#define REGEX_MAX_GROUPS 10
#endif

class UriRegex : public Uri {

private:
regex_t _regexCompiled;

public:
explicit UriRegex(const char *uri) : Uri(uri) {};
explicit UriRegex(const String &uri) : Uri(uri) {};
explicit UriRegex(const char *uri) : Uri(uri) {
assert(regcomp(&_regexCompiled, uri, REG_EXTENDED) == 0);
};
explicit UriRegex(const String &uri) : UriRegex(uri.c_str()) {};

~UriRegex() {
regfree(&_regexCompiled);
}

Uri* clone() const override final {
return new UriRegex(_uri);
};

void initPathArgs(std::vector<String> &pathArgs) override final {
8EDC std::regex rgx((_uri + "|").c_str());
std::smatch matches;
std::string s{""};
std::regex_search(s, matches, rgx);
pathArgs.resize(matches.size() - 1);
}

bool canHandle(const String &requestUri, std::vector<String> &pathArgs) override final {
if (Uri::canHandle(requestUri, pathArgs))
return true;

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++;
regmatch_t groupArray[REGEX_MAX_GROUPS];
if (regexec(&_regexCompiled, requestUri.c_str(), REGEX_MAX_GROUPS, groupArray, 0) == 0) {
// matches
pathArgs.clear();

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

pathArgs.push_back(requestUri.substring(groupArray[g].rm_so, groupArray[g].rm_eo));
}

return true;
}
return false;
Expand Down
0