1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-07 06:01:35 +03:00

Add Uri with support for regexUri and globUri (#6696)

* Add path args

* Add example

* Update code format

* Add missing include

* Fix codestyle and unsigned int

* fix unsigned int

* Remove tabs

* use vector<>.resize

* rename j to requestUriIndex

* using assert checking the path argument index

* Add missing include "assert.h"

* The order no longer matters.
Path arguments may not contain the value '/'
Updated the example

* make pathArg return a const

* Update PathArgServer.ino

fix trailing space

* const String&

* Add regex support

* Fix to match templating

* Add Uri with support for staticUri, regexUri and globUri

* Update example

* Add deconstructor to remove _uri pointer

* Add newline to end of files

* Suppress gcc warnings (unused params)

* Replace regex with regex.h

* Use the standard STASSID/PSK settings for example

Make the example match the existing examples which allow setting
the SSID/PSK in the local platform.txt file.

* Use 115.2Kbaud for example, match others

Co-authored-by: david gauchard <gauchard@laas.fr>
Co-authored-by: Earle F. Philhower, III <earlephilhower@yahoo.com>
This commit is contained in:
Bob
2020-02-22 20:51:47 +01:00
committed by GitHub
parent a40663b65f
commit 4eca62cb53
9 changed files with 251 additions and 13 deletions

View File

@ -0,0 +1,54 @@
#ifndef URI_BRACES_H
#define URI_BRACES_H
#include "Uri.h"
class UriBraces : public Uri {
public:
explicit UriBraces(const char *uri) : Uri(uri) {};
explicit UriBraces(const String &uri) : Uri(uri) {};
Uri* clone() const override final {
return new UriBraces(_uri);
};
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 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.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.push_back(requestUri.substring(requestUriIndex, uriIndex));
requestUriIndex = (unsigned int) uriIndex;
}
}
return requestUriIndex >= requestUri.length();
}
};
#endif

View File

@ -0,0 +1,22 @@
#ifndef URI_GLOB_H
#define URI_GLOB_H
#include "Uri.h"
#include <fnmatch.h>
class UriGlob : public Uri {
public:
explicit UriGlob(const char *uri) : Uri(uri) {};
explicit UriGlob(const String &uri) : Uri(uri) {};
Uri* clone() const override final {
return new UriGlob(_uri);
};
bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &pathArgs) override final {
return fnmatch(_uri.c_str(), requestUri.c_str(), 0) == 0;
}
};
#endif

View File

@ -0,0 +1,54 @@
#ifndef URI_REGEX_H
#define URI_REGEX_H
#include "Uri.h"
#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) {
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);
};
bool canHandle(const String &requestUri, std::vector<String> &pathArgs) override final {
if (Uri::canHandle(requestUri, pathArgs))
return true;
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;
}
};
#endif