* Asyncwebserver * Squashed commit of the following: commit 575ef02f593918ec6654c87407a4d11fc17071b8 Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 17:56:11 2024 +0200 merge master commit 35e5adf4ecd80f9829e8801181f35dd2c1d94759 Merge: a2cc7be221adca01Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 17:41:04 2024 +0200 Merge branch 'master' of github.com:technyon/nuki_hub into DM9051 commit a2cc7be2954cbd8767ab8186296c0b14134d1d0b Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:51:50 2024 +0200 update nuki ble commit 20c809f3dca28b29b219d1ff3a183f1981316de5 Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:44:46 2024 +0200 backup commit dd41c218efb5270f5efeb734e64dff695920db16 Merge: 153000b5e84b944aAuthor: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:40:03 2024 +0200 Merge branch 'master' of github.com:technyon/nuki_hub into DM9051 commit 153000b5b1af7df1fbeb5263df94eb26f689cc0a Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:23:07 2024 +0200 fix linker error commit a93bbfbfc4301e46ff3696a763dd13c6c89efefb Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 11 11:27:07 2024 +0200 backup commit f611c75ce8c35f829bcad6cf7e86188f4b3ec331 Merge: f1964917063fbab6Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 11 11:24:47 2024 +0200 merge master commit f1964917b4dade3920f1ecdb699c58630199e6da Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 15:17:45 2024 +0200 update platformio.ini commit f448e5e8a7e93be38e09e2ab0b622199a3721af6 Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 11:28:09 2024 +0200 add SPIClass instance for DM9051 commit 1f190e9aa08033535a2eb442a92e6e20409bbda1 Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 11:22:26 2024 +0200 add definitions and constructor for DM9051 commit 726b3602ae91594ee1210ad5b6714f75cc5e42a7 Merge: 50a2eb134af90cbcAuthor: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 10:19:34 2024 +0200 merge master commit 50a2eb136d75d90921f1c6974f18bc107bddc123 Author: technyon <j.o.schuemann@gmx.de> Date: Fri Aug 9 11:52:09 2024 +0200 add comment commit 9437e485cae169efdf8e5a7bf188a1c7e792d1e5 Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 4 08:29:21 2024 +0200 move LAN8720 definitions to seperate file * Remove Core 2 Ethernet library * Custom Ethernet * GPIO and Preferences * H2
228 lines
6.5 KiB
C++
228 lines
6.5 KiB
C++
/*
|
|
Asynchronous WebServer library for Espressif MCUs
|
|
|
|
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
|
|
This file is part of the esp8266 core for Arduino environment.
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
#include "ESPAsyncWebServer.h"
|
|
#include "WebHandlerImpl.h"
|
|
|
|
using namespace asyncsrv;
|
|
|
|
bool ON_STA_FILTER(AsyncWebServerRequest* request) {
|
|
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
|
return WiFi.localIP() == request->client()->localIP();
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
bool ON_AP_FILTER(AsyncWebServerRequest* request) {
|
|
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
|
return WiFi.localIP() != request->client()->localIP();
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
#ifndef HAVE_FS_FILE_OPEN_MODE
|
|
const char* fs::FileOpenMode::read = "r";
|
|
const char* fs::FileOpenMode::write = "w";
|
|
const char* fs::FileOpenMode::append = "a";
|
|
#endif
|
|
|
|
AsyncWebServer::AsyncWebServer(uint16_t port)
|
|
: _server(port) {
|
|
_catchAllHandler = new AsyncCallbackWebHandler();
|
|
if (_catchAllHandler == NULL)
|
|
return;
|
|
_server.onClient([](void* s, AsyncClient* c) {
|
|
if (c == NULL)
|
|
return;
|
|
c->setRxTimeout(3);
|
|
AsyncWebServerRequest* r = new AsyncWebServerRequest((AsyncWebServer*)s, c);
|
|
if (r == NULL) {
|
|
c->close(true);
|
|
c->free();
|
|
delete c;
|
|
}
|
|
},
|
|
this);
|
|
}
|
|
|
|
AsyncWebServer::~AsyncWebServer() {
|
|
reset();
|
|
end();
|
|
if (_catchAllHandler)
|
|
delete _catchAllHandler;
|
|
}
|
|
|
|
AsyncWebRewrite& AsyncWebServer::addRewrite(std::shared_ptr<AsyncWebRewrite> rewrite) {
|
|
_rewrites.emplace_back(rewrite);
|
|
return *_rewrites.back().get();
|
|
}
|
|
|
|
AsyncWebRewrite& AsyncWebServer::addRewrite(AsyncWebRewrite* rewrite) {
|
|
_rewrites.emplace_back(rewrite);
|
|
return *_rewrites.back().get();
|
|
}
|
|
|
|
bool AsyncWebServer::removeRewrite(AsyncWebRewrite* rewrite) {
|
|
return removeRewrite(rewrite->from().c_str(), rewrite->toUrl().c_str());
|
|
}
|
|
|
|
bool AsyncWebServer::removeRewrite(const char* from, const char* to) {
|
|
for (auto r = _rewrites.begin(); r != _rewrites.end(); ++r) {
|
|
if (r->get()->from() == from && r->get()->toUrl() == to) {
|
|
_rewrites.erase(r);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
AsyncWebRewrite& AsyncWebServer::rewrite(const char* from, const char* to) {
|
|
_rewrites.emplace_back(std::make_shared<AsyncWebRewrite>(from, to));
|
|
return *_rewrites.back().get();
|
|
}
|
|
|
|
AsyncWebHandler& AsyncWebServer::addHandler(AsyncWebHandler* handler) {
|
|
_handlers.emplace_back(handler);
|
|
return *(_handlers.back().get());
|
|
}
|
|
|
|
bool AsyncWebServer::removeHandler(AsyncWebHandler* handler) {
|
|
for (auto i = _handlers.begin(); i != _handlers.end(); ++i) {
|
|
if (i->get() == handler) {
|
|
_handlers.erase(i);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void AsyncWebServer::begin() {
|
|
_server.setNoDelay(true);
|
|
_server.begin();
|
|
}
|
|
|
|
void AsyncWebServer::end() {
|
|
_server.end();
|
|
}
|
|
|
|
#if ASYNC_TCP_SSL_ENABLED
|
|
void AsyncWebServer::onSslFileRequest(AcSSlFileHandler cb, void* arg) {
|
|
_server.onSslFileRequest(cb, arg);
|
|
}
|
|
|
|
void AsyncWebServer::beginSecure(const char* cert, const char* key, const char* password) {
|
|
_server.beginSecure(cert, key, password);
|
|
}
|
|
#endif
|
|
|
|
void AsyncWebServer::_handleDisconnect(AsyncWebServerRequest* request) {
|
|
delete request;
|
|
}
|
|
|
|
void AsyncWebServer::_rewriteRequest(AsyncWebServerRequest* request) {
|
|
for (const auto& r : _rewrites) {
|
|
if (r->match(request)) {
|
|
request->_url = r->toUrl();
|
|
request->_addGetParams(r->params());
|
|
}
|
|
}
|
|
}
|
|
|
|
void AsyncWebServer::_attachHandler(AsyncWebServerRequest* request) {
|
|
for (auto& h : _handlers) {
|
|
if (h->filter(request) && h->canHandle(request)) {
|
|
request->setHandler(h.get());
|
|
return;
|
|
}
|
|
}
|
|
|
|
request->addInterestingHeader(T_ANY);
|
|
request->setHandler(_catchAllHandler);
|
|
}
|
|
|
|
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload, ArBodyHandlerFunction onBody) {
|
|
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
|
|
handler->setUri(uri);
|
|
handler->setMethod(method);
|
|
handler->onRequest(onRequest);
|
|
handler->onUpload(onUpload);
|
|
handler->onBody(onBody);
|
|
addHandler(handler);
|
|
return *handler;
|
|
}
|
|
|
|
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload) {
|
|
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
|
|
handler->setUri(uri);
|
|
handler->setMethod(method);
|
|
handler->onRequest(onRequest);
|
|
handler->onUpload(onUpload);
|
|
addHandler(handler);
|
|
return *handler;
|
|
}
|
|
|
|
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest) {
|
|
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
|
|
handler->setUri(uri);
|
|
handler->setMethod(method);
|
|
handler->onRequest(onRequest);
|
|
addHandler(handler);
|
|
return *handler;
|
|
}
|
|
|
|
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, ArRequestHandlerFunction onRequest) {
|
|
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
|
|
handler->setUri(uri);
|
|
handler->onRequest(onRequest);
|
|
addHandler(handler);
|
|
return *handler;
|
|
}
|
|
|
|
AsyncStaticWebHandler& AsyncWebServer::serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_control) {
|
|
AsyncStaticWebHandler* handler = new AsyncStaticWebHandler(uri, fs, path, cache_control);
|
|
addHandler(handler);
|
|
return *handler;
|
|
}
|
|
|
|
void AsyncWebServer::onNotFound(ArRequestHandlerFunction fn) {
|
|
_catchAllHandler->onRequest(fn);
|
|
}
|
|
|
|
void AsyncWebServer::onFileUpload(ArUploadHandlerFunction fn) {
|
|
_catchAllHandler->onUpload(fn);
|
|
}
|
|
|
|
void AsyncWebServer::onRequestBody(ArBodyHandlerFunction fn) {
|
|
_catchAllHandler->onBody(fn);
|
|
}
|
|
|
|
void AsyncWebServer::reset() {
|
|
_rewrites.clear();
|
|
_handlers.clear();
|
|
|
|
if (_catchAllHandler != NULL) {
|
|
_catchAllHandler->onRequest(NULL);
|
|
_catchAllHandler->onUpload(NULL);
|
|
_catchAllHandler->onBody(NULL);
|
|
}
|
|
}
|