Skip to content
Merged
Changes from all commits
Commits
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
26 changes: 18 additions & 8 deletions libraries/ESP8266WebServer/src/uri/UriRegex.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#define URI_REGEX_H

#include "Uri.h"

#include <cassert>
#include <regex.h>
#include <assert.h>

#ifndef REGEX_MAX_GROUPS
#define REGEX_MAX_GROUPS 10
Expand All @@ -12,29 +13,38 @@
class UriRegex : public Uri {

private:
regex_t _regexCompiled;
regex_t _regexCompiled{};
int _regexErr{REG_EMPTY};

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() = delete;

explicit UriRegex(const char *uri) :
Uri(uri),
_regexErr(regcomp(&_regexCompiled, uri, REG_EXTENDED))
{
assert(_regexErr == 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;

if (_regexErr != 0)
return false;

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;
Expand Down