diff --git a/.github/scripts/install-arduino-ide.sh b/.github/scripts/install-arduino-ide.sh index 7e268b1..ce60cb8 100755 --- a/.github/scripts/install-arduino-ide.sh +++ b/.github/scripts/install-arduino-ide.sh @@ -75,16 +75,17 @@ if [ ! -d "$ARDUINO_IDE_PATH" ]; then echo "" fi -function build_sketch(){ # build_sketch [extra-options] +function build_sketch(){ # build_sketch [extra-options] if [ "$#" -lt 2 ]; then echo "ERROR: Illegal number of parameters" - echo "USAGE: build_sketch [extra-options]" + echo "USAGE: build_sketch [extra-options]" return 1 fi local fqbn="$1" local sketch="$2" - local xtra_opts="$3" + local build_flags="$3" + local xtra_opts="$4" local win_opts="" if [ "$OS_IS_WINDOWS" == "1" ]; then local ctags_version=`ls "$ARDUINO_IDE_PATH/tools-builder/ctags/"` @@ -107,6 +108,7 @@ function build_sketch(){ # build_sketch [extra-options] -libraries "$ARDUINO_USR_PATH/libraries" \ -build-cache "$ARDUINO_CACHE_DIR" \ -build-path "$ARDUINO_BUILD_DIR" \ + -prefs=compiler.cpp.extra_flags="$build_flags" \ $win_opts $xtra_opts "$sketch" } @@ -210,7 +212,13 @@ function build_sketches() # build_sketches - if [ "$#" -lt 2 ]; then +function build_pio_sketch(){ # build_pio_sketch + if [ "$#" -lt 3 ]; then echo "ERROR: Illegal number of parameters" - echo "USAGE: build_pio_sketch " + echo "USAGE: build_pio_sketch " return 1 fi local board="$1" local sketch="$2" + local buildFlags="$3" local sketch_dir=$(dirname "$sketch") echo "" echo "Compiling '"$(basename "$sketch")"' ..." - python -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv" + python -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv" --project-option="build_flags=$buildFlags" } function count_sketches() # count_sketches @@ -118,12 +119,18 @@ function build_pio_sketches() # build_pio_sketches pathArg(0); + }); +``` +*NOTE*: All regex patterns starts with `^` and ends with `$` + +To enable the `Path variable` support, you have to define the buildflag `-DASYNCWEBSERVER_REGEX`. + + +For Arduino IDE create/update `platform.local.txt`: + +`Windows`: C:\Users\(username)\AppData\Local\Arduino15\packages\\`{espxxxx}`\hardware\\`espxxxx`\\`{version}`\platform.local.txt + +`Linux`: ~/.arduino15/packages/`{espxxxx}`/hardware/`{espxxxx}`/`{version}`/platform.local.txt + +Add/Update the following line: +``` + compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX +``` + +For platformio modify `platformio.ini`: +```ini +[env:myboard] +build_flags = + -DASYNCWEBSERVER_REGEX +``` +*NOTE*: By enabling `ASYNCWEBSERVER_REGEX`, `` will be included. This will add an 100k to your binary. diff --git a/examples/regex_patterns/.test.build_flags b/examples/regex_patterns/.test.build_flags new file mode 100644 index 0000000..9ea3bb7 --- /dev/null +++ b/examples/regex_patterns/.test.build_flags @@ -0,0 +1 @@ +-DASYNCWEBSERVER_REGEX=1 diff --git a/examples/regex_patterns/regex_patterns.ino b/examples/regex_patterns/regex_patterns.ino index 4ec9c85..fb01306 100644 --- a/examples/regex_patterns/regex_patterns.ino +++ b/examples/regex_patterns/regex_patterns.ino @@ -5,6 +5,18 @@ // * handle missing pages / 404s // +// Add buildflag ASYNCWEBSERVER_REGEX to enable the regex support + +// For platformio: platformio.ini: +// build_flags = +// -DASYNCWEBSERVER_REGEX + +// For arduino IDE: create/update platform.local.txt +// Windows: C:\Users\(username)\AppData\Local\Arduino15\packages\espxxxx\hardware\espxxxx\{version}\platform.local.txt +// Linux: ~/.arduino15/packages/espxxxx/hardware/espxxxx/{version}/platform.local.txt +// +// compiler.cpp.extra_flags=-DASYNCWEBSERVER_REGEX=1 + #include #ifdef ESP32 #include diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 4f0574a..7cd21aa 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -38,6 +38,12 @@ #error Platform not supported #endif +#ifdef ASYNCWEBSERVER_REGEX +#define ASYNCWEBSERVER_REGEX_ATTRIBUTE +#else +#define ASYNCWEBSERVER_REGEX_ATTRIBUTE __attribute__((warning("ASYNCWEBSERVER_REGEX not defined"))) +#endif + #define DEBUGF(...) //Serial.printf(__VA_ARGS__) class AsyncWebServer; @@ -271,7 +277,7 @@ class AsyncWebServerRequest { bool hasArg(const char* name) const; // check if argument exists bool hasArg(const __FlashStringHelper * data) const; // check if F(argument) exists - const String& pathArg(size_t i) const; + const String& ASYNCWEBSERVER_REGEX_ATTRIBUTE pathArg(size_t i) const; const String& header(const char* name) const;// get request header value by name const String& header(const __FlashStringHelper * data) const;// get request header value by F(name) diff --git a/src/WebHandlerImpl.h b/src/WebHandlerImpl.h index 5ae7b49..d121fa7 100644 --- a/src/WebHandlerImpl.h +++ b/src/WebHandlerImpl.h @@ -22,7 +22,9 @@ #define ASYNCWEBSERVERHANDLERIMPL_H_ #include +#ifdef ASYNCWEBSERVER_REGEX #include +#endif #include "stddef.h" #include @@ -71,10 +73,10 @@ class AsyncCallbackWebHandler: public AsyncWebHandler { ArBodyHandlerFunction _onBody; bool _isRegex; public: - AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false){} + AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {} void setUri(const String& uri){ _uri = uri; - _isRegex = uri.startsWith("^") && uri.endsWith("$"); + _isRegex = uri.startsWith("^") && uri.endsWith("$"); } void setMethod(WebRequestMethodComposite method){ _method = method; } void onRequest(ArRequestHandlerFunction fn){ _onRequest = fn; } @@ -89,18 +91,21 @@ class AsyncCallbackWebHandler: public AsyncWebHandler { if(!(_method & request->method())) return false; +#ifdef ASYNCWEBSERVER_REGEX if (_isRegex) { - std::regex rgx(_uri.c_str()); + std::regex pattern(_uri.c_str()); std::smatch matches; std::string s(request->url().c_str()); - if(std::regex_search(s, matches, rgx)) { + if(std::regex_search(s, matches, pattern)) { for (size_t i = 1; i < matches.size(); ++i) { // start from 1 request->_addPathParam(matches[i].str().c_str()); } } else { return false; } - } else if (_uri.length() && _uri.endsWith("*")) { + } else +#endif + if (_uri.length() && _uri.endsWith("*")) { String uriTemplate = String(_uri); uriTemplate = uriTemplate.substring(0, uriTemplate.length() - 1); if (!request->url().startsWith(uriTemplate))