Skip to content
Closed
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions examples/Json/Json.ino
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ void setup() {
response->setLength();
request->send(response);
});


#ifdef ASYNCWEBSERVER_REGEX
server.on("^\\/page\\/(\\d+)$", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &json) {
String number = request->pathArg(0);
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject root = response->getRoot().to<JsonObject>();
root["pageStr"] = number;
root["pageInt"] = number.toInt();
response->setLength();
request->send(response);
});
#endif
#endif

server.begin();
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ build_flags =
; -D TAG=\"core\"
; -D LOG_LOCAL_LEVEL=ESP_LOG_VERBOSE
; -D ASYNCWEBSERVER_LOG_DEBUG
; -D ASYNCWEBSERVER_REGEX
upload_protocol = esptool
monitor_speed = 115200
monitor_filters = esp32_exception_decoder, log2file
Expand Down
17 changes: 16 additions & 1 deletion src/AsyncJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,29 @@ AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(const String &uri, ArJs
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
#else
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(const String &uri, ArJsonRequestHandlerFunction onRequest)
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
: _uri(uri), _isRegex(uri.startsWith("^") && uri.endsWith("$")), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
#endif

bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) const {
if (!_onRequest || !request->isHTTP() || !(_method & request->method())) {
return false;
}

#ifdef ASYNCWEBSERVER_REGEX
if (_isRegex) {
std::regex pattern(_uri.c_str());
std::smatch matches;
std::string s(request->url().c_str());
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
#endif

if (_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri + "/"))) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/AsyncJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ class AsyncMessagePackResponse : public AsyncJsonResponse {
// Body handler supporting both content types: JSON and MessagePack

class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
private:
protected:
String _uri;
bool _isRegex;
WebRequestMethodComposite _method;
ArJsonRequestHandlerFunction _onRequest;
#if ARDUINOJSON_VERSION_MAJOR == 6
Expand Down
1 change: 1 addition & 0 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class AsyncWebServerRequest {
using FS = fs::FS;
friend class AsyncWebServer;
friend class AsyncCallbackWebHandler;
friend class AsyncCallbackJsonWebHandler;
friend class AsyncFileResponse;
friend class AsyncStaticWebHandler;

Expand Down