Layer: L7 Application ยท Build flags: none (core features only)
What this example teaches
on_regex() matches the whole request path against a small regular-expression subset, for routing rules that :param segments cannot express (numeric-only ids, specific file extensions). The matcher is allocation-free and bounded by RE_MAX_STEPS so it stays deterministic - no catastrophic backtracking.
The supported subset. ., the quantifiers * + ?, character classes [...] / [^...] with a-z ranges, the shorthands \d \w \s, and \ escapes. It is non-capturing (use :name path params if you need to capture):
@ HTTP_GET
Safe, idempotent read.
/sensor/42 matches but /sensor/abc does not (404); /img/cat.png matches but /img/cat.gif does not. The whole path must match (it is anchored), and the match cost is capped by RE_MAX_STEPS, which is why this is safe to expose to untrusted input.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L7-Application/RegexRoutes/RegexRoutes.ino
curl http://<ip>/sensor/42 # 200 (digits)
curl http://<ip>/sensor/abc # 404
curl http://<ip>/img/cat.png # 200
curl http://<ip>/img/cat.gif # 404
Annotated source
The complete sketch (RegexRoutes.ino), reproduced verbatim with added explanatory comments:
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
void handle_sensor(uint8_t slot_id,
HttpReq *req)
{
char body[96];
snprintf(body,
sizeof(body),
"{\"path\":\"%s\",\"matched\":\"numeric sensor id\"}", req->
path);
server.
send(slot_id, 200,
"application/json", body);
}
void handle_png(uint8_t slot_id,
HttpReq *req)
{
char body[96];
snprintf(body,
sizeof(body),
"{\"path\":\"%s\",\"matched\":\"png image\"}", req->
path);
server.
send(slot_id, 200,
"application/json", body);
}
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
int32_t result = server.
begin(80);
if (result < 0)
{
Serial.printf("begin() failed (error %d)\n", result);
return;
}
Serial.println("Server started on port 80");
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
void on_regex(const char *pattern, HttpMethod method, Handler callback)
Register a route whose path is a regular expression.
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
Fully-parsed HTTP/1.1 request.
char path[MAX_PATH_LEN]
URL path, null-terminated; no query string.