Skip to content

Commit

Permalink
ESP8266WebServer - UriRegex runtime logic within assert() (#9185)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr authored Aug 1, 2024
1 parent bb3360d commit ccea728
Showing 1 changed file with 18 additions and 8 deletions.
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

0 comments on commit ccea728

Please sign in to comment.