8000 Make regex a define exclusive (#617) · Rzyq-git/ESPAsyncWebServer@a84f169 · GitHub
[go: up one dir, main page]

Skip to content

Commit a84f169

Browse files
Bmooijme-no-dev
authored andcommitted
Make regex a define exclusive (me-no-dev#617)
* Change compiler error to runtime error. Add compiler warining when a ASYNCWEBSERVER_REGEX function is used, but not enabled * Update docs for usage in Arduino IDE * Update docs for platform.local.txt
1 parent f3ebe2d commit a84f169

File tree

7 files changed

+89
-15
lines changed

7 files changed

+89
-15
lines changed

.github/scripts/install-arduino-ide.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,17 @@ if [ ! -d "$ARDUINO_IDE_PATH" ]; then
7575
echo ""
7676
fi
7777

78-
function build_sketch(){ # build_sketch <fqbn> <path-to-ino> [extra-options]
78+
function build_sketch(){ # build_sketch <fqbn> <path-to-ino> <build-flags> [extra-options]
7979
if [ "$#" -lt 2 ]; then
8080
echo "ERROR: Illegal number of parameters"
81-
echo "USAGE: build_sketch <fqbn> <path-to-ino> [extra-options]"
81+
echo "USAGE: build_sketch <fqbn> <path-to-ino> <build-flags> [extra-options]"
8282
return 1
8383
fi
8484

8585
local fqbn="$1"
8686
local sketch="$2"
87-
local xtra_opts="$3"
87+
local build_flags="$3"
88+
local xtra_opts="$4"
8889
local win_opts=""
8990
if [ "$OS_IS_WINDOWS" == "1" ]; then
9091
local ctags_version=`ls "$ARDUINO_IDE_PATH/tools-builder/ctags/"`
@@ -107,6 +108,7 @@ function build_sketch(){ # build_sketch <fqbn> <path-to-ino> [extra-options]
107108
-libraries "$ARDUINO_USR_PATH/libraries" \
108109
-build-cache "$ARDUINO_CACHE_DIR" \
109110
-build-path "$ARDUINO_BUILD_DIR" \
111+
-prefs=compiler.cpp.extra_flags="$build_flags" \
110112
$win_opts $xtra_opts "$sketch"
111113
}
112114

@@ -210,7 +212,13 @@ function build_sketches() # build_sketches <fqbn> <examples-path> <chunk> <total
210212
|| [ "$sketchnum" -gt "$end_index" ]; then
211213
continue
212214
fi
213-
build_sketch "$fqbn" "$sketch" "$xtra_opts"
215+
local sketchBuildFlags=""
216+
if [ -f "$sketchdir/.test.build_flags" ]; then
217+
while read line; do
218+
sketchBuildFlags="$sketchBuildFlags $line"
219+
done < "$sketchdir/.test.build_flags"
220+
fi
221+
build_sketch "$fqbn" "$sketch" "$sketchBuildFlags" "$xtra_opts"
214222
local result=$?
215223
if [ $result -ne 0 ]; then
216224
return $result

.github/scripts/install-platformio.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ echo "PlatformIO has been installed"
1010
echo ""
1111

1212

13-
function build_pio_sketch(){ # build_pio_sketch <board> <path-to-ino>
14-
if [ "$#" -lt 2 ]; then
13+
function build_pio_sketch(){ # build_pio_sketch <board> <path-to-ino> <build-flags>
14+
if [ "$#" -lt 3 ]; then
1515
echo "ERROR: Illegal number of parameters"
16-
echo "USAGE: build_pio_sketch <board> <path-to-ino>"
16+
echo "USAGE: build_pio_sketch <board> <path-to-ino> <build-flags>"
1717
return 1
1818
fi
1919

2020
local board="$1"
2121
local sketch="$2"
22+
local buildFlags="$3"
2223
local sketch_dir=$(dirname "$sketch")
2324
echo ""
2425
echo "Compiling '"$(basename "$sketch")"' ..."
25-
python -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv"
26+
python -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv" --project-option="build_flags=$buildFlags"
2627
}
2728

2829
function count_sketches() # count_sketches <examples-path>
@@ -118,12 +119,18 @@ function build_pio_sketches() # build_pio_sketches <board> <examples-path> <chun
118119
|| [ -f "$sketchdir/.test.skip" ]; then
119120
continue
120121
fi
122+
local sketchBuildFlags=""
123+
if [ -f "$sketchdir/.test.build_flags" ]; then
124+
while read line; do
125+
sketchBuildFlags="$sketchBuildFlags $line"
126+
done < "$sketchdir/.test.build_flags"
127+
fi
121128
sketchnum=$(($sketchnum + 1))
122129
if [ "$sketchnum" -le "$start_index" ] \
123130
|| [ "$sketchnum" -gt "$end_index" ]; then
124131
continue
125132
fi
126-
build_pio_sketch "$board" "$sketch"
133+
build_pio_sketch "$board" "$sketch" "$sketchBuildFlags"
127134
local result=$?
128135
if [ $result -ne 0 ]; then
129136
return $result

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ To use this library you might need to have the latest git versions of [ESP32](ht
8989
- [Setup global and class functions as request handlers](#setup-global-and-class-functions-as-request-handlers)
9090
- [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections)
9191
- [Adding Default Headers](#adding-default-headers)
92+
- [Path variable](#path-variable)
9293

9394
## Installation
9495

@@ -1484,3 +1485,37 @@ webServer.onNotFound([](AsyncWebServerRequest *request) {
14841485
}
14851486
});
14861487
```
1488+
1489+
### Path variable
1490+
1491+
With path variable you can create a custom regex rule for a specific parameter in a route.
1492+
For example we want a `sensorId` parameter in a route rule to match only a integer.
1493+
1494+
```cpp
1495+
server.on("^\\/sensor\\/([0-9]+)$", HTTP_GET, [] (AsyncWebServerRequest *request) {
1496+
String sensorId = request->pathArg(0);
1497+
});
1498+
```
1499+
*NOTE*: All regex patterns starts with `^` and ends with `$`
1500+
1501+
To enable the `Path variable` support, you have to define the buildflag `-DASYNCWEBSERVER_REGEX`.
1502+
1503+
1504+
For Arduino IDE create/update `platform.local.txt`:
1505+
1506+
`Windows`: C:\Users\(username)\AppData\Local\Arduino15\packages\\`{espxxxx}`\hardware\\`espxxxx`\\`{version}`\platform.local.txt
1507+
1508+
`Linux`: ~/.arduino15/packages/`{espxxxx}`/hardware/`{espxxxx}`/`{version}`/platform.local.txt
1509+
1510+
Add/Update the following line:
1511+
```
1512+
compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX
1513+
```
1514+
1515+
For platformio modify `platformio.ini`:
1516+
```ini
1517+
[env:myboard]
1518+
build_flags =
1519+
-DASYNCWEBSERVER_REGEX
1520+
```
1521+
*NOTE*: By enabling `ASYNCWEBSERVER_REGEX`, `<regex>` will be included. This will add an 100k to your binary.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-DASYNCWEBSERVER_REGEX=1

examples/regex_patterns/regex_patterns.ino

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
// * handle missing pages / 404s
66
//
77

8+
// Add buildflag ASYNCWEBSERVER_REGEX to enable the regex support
9+
10+
// For platformio: platformio.ini:
11+
// build_flags =
12+
// -DASYNCWEBSERVER_REGEX
13+
14+
// For arduino IDE: create/update platform.local.txt
15+
// Windows: C:\Users\(username)\AppData\Local\Arduino15\packages\espxxxx\hardware\espxxxx\{version}\platform.local.txt
16+
// Linux: ~/.arduino15/packages/espxxxx/hardware/espxxxx/{version}/platform.local.txt
17+
//
18+
// compiler.cpp.extra_flags=-DASYNCWEBSERVER_REGEX=1
19+
820
#include <Arduino.h>
921
#ifdef ESP32
1022
#include <WiFi.h>

src/ESPAsyncWebServer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
#error Platform not supported
3939
#endif
4040

41+
#ifdef ASYNCWEBSERVER_REGEX
42+
#define ASYNCWEBSERVER_REGEX_ATTRIBUTE
43+
#else
44+
#define ASYNCWEBSERVER_REGEX_ATTRIBUTE __attribute__((warning("ASYNCWEBSERVER_REGEX not defined")))
45+
#endif
46+
4147
#define DEBUGF(...) //Serial.printf(__VA_ARGS__)
4248

4349
class AsyncWebServer;
@@ -271,7 +277,7 @@ class AsyncWebServerRequest {
271277
bool hasArg(const char* name) const; // check if argument exists
272278
bool hasArg(const __FlashStringHelper * data) const; // check if F(argument) exists
273279

274-
const String& pathArg(size_t i) const;
280+
const String& ASYNCWEBSERVER_REGEX_ATTRIBUTE pathArg(size_t i) const;
275281

276282
const String& header(const char* name) const;// get request header value by name
277283
const String& header(const __FlashStringHelper * data) const;// get request header value by F(name)

src/WebHandlerImpl.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#define ASYNCWEBSERVERHANDLERIMPL_H_
2323

2424
#include <string>
25+
#ifdef ASYNCWEBSERVER_REGEX
2526
#include <regex>
27+
#endif
2628

2729
#include "stddef.h"
2830
#include <time.h>
@@ -71,10 +73,10 @@ class AsyncCallbackWebHandler: public AsyncWebHandler {
7173
ArBodyHandlerFunction _onBody;
7274
bool _isRegex;
7375
public:
74-
AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false){}
76+
AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
7577
void setUri(const String& uri){
7678
_uri = uri;
77-
_isRegex = uri.startsWith("^") && uri.endsWith("$");
79+
_isRegex = uri.startsWith("^") && uri.endsWith("$");
7880
}
7981
void setMethod(WebRequestMethodComposite method){ _method = method; }
8082
void onRequest(ArRequestHandlerFunction fn){ _onRequest = fn; }
@@ -89,18 +91,21 @@ class AsyncCallbackWebHandler: public AsyncWebHandler {
8991
if(!(_method & request->method()))
9092
return false;
9193

94+
#ifdef ASYNCWEBSERVER_REGEX
9295
if (_isRegex) {
93-
std::regex rgx(_uri.c_str());
96+
std::regex pattern(_uri.c_str());
9497
std::smatch matches;
9598
std::string s(request->url().c_str());
96-
if(std::regex_search(s, matches, rgx)) {
99+
if(std::regex_search(s, matches, pattern)) {
97100
for (size_t i = 1; i < matches.size(); ++i) { // start from 1
98101
request->_addPathParam(matches[i].str().c_str());
99102
}
100103
} else {
101104
return false;
102105
}
103-
} else if (_uri.length() && _uri.endsWith("*")) {
106+
} else
107+
#endif
108+
if (_uri.length() && _uri.endsWith("*")) {
104109
String uriTemplate = String(_uri);
105110
uriTemplate = uriTemplate.substring(0, uriTemplate.length() - 1);
106111
if (!request->url().startsWith(uriTemplate))

0 commit comments

Comments
 (0)
0