mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-08-02 12:14:39 +02:00
AsyncWebServer::_handlers repalce with STL container
repalce LinkedList with std::list<std::unique_ptr<AsyncWebHandler> > _handlers;
This commit is contained in:
committed by
Mathieu Carbou
parent
d5c2af8dc2
commit
9bd3cd9472
@@ -358,11 +358,11 @@ class AsyncWebRewrite {
|
||||
|
||||
class AsyncWebHandler {
|
||||
protected:
|
||||
ArRequestFilterFunction _filter;
|
||||
ArRequestFilterFunction _filter{nullptr};
|
||||
String _username;
|
||||
String _password;
|
||||
public:
|
||||
AsyncWebHandler():_username(""), _password(""){}
|
||||
AsyncWebHandler(){}
|
||||
AsyncWebHandler& setFilter(ArRequestFilterFunction fn) { _filter = fn; return *this; }
|
||||
AsyncWebHandler& setAuthentication(const char *username, const char *password){ _username = String(username);_password = String(password); return *this; };
|
||||
AsyncWebHandler& setAuthentication(const String& username, const String& password){ _username = username;_password = password; return *this; };
|
||||
@@ -430,7 +430,7 @@ class AsyncWebServer {
|
||||
protected:
|
||||
AsyncServer _server;
|
||||
std::list<std::shared_ptr<AsyncWebRewrite> > _rewrites;
|
||||
LinkedList<AsyncWebHandler*> _handlers;
|
||||
std::list<std::unique_ptr<AsyncWebHandler> > _handlers;
|
||||
AsyncCallbackWebHandler* _catchAllHandler;
|
||||
|
||||
public:
|
||||
|
@@ -37,7 +37,6 @@ const char *fs::FileOpenMode::append = "a";
|
||||
|
||||
AsyncWebServer::AsyncWebServer(uint16_t port)
|
||||
: _server(port)
|
||||
, _handlers(LinkedList<AsyncWebHandler*>([](AsyncWebHandler* h){ delete h; }))
|
||||
{
|
||||
_catchAllHandler = new AsyncCallbackWebHandler();
|
||||
if(_catchAllHandler == NULL)
|
||||
@@ -92,12 +91,18 @@ AsyncWebRewrite& AsyncWebServer::rewrite(const char* from, const char* to){
|
||||
}
|
||||
|
||||
AsyncWebHandler& AsyncWebServer::addHandler(AsyncWebHandler* handler){
|
||||
_handlers.add(handler);
|
||||
return *handler;
|
||||
_handlers.emplace_back(handler);
|
||||
return *(_handlers.back().get());
|
||||
}
|
||||
|
||||
bool AsyncWebServer::removeHandler(AsyncWebHandler *handler){
|
||||
return _handlers.remove(handler);
|
||||
for (auto i = _handlers.begin(); i != _handlers.end(); ++i){
|
||||
if (i->get() == handler ){
|
||||
_handlers.erase(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AsyncWebServer::begin(){
|
||||
@@ -133,9 +138,9 @@ void AsyncWebServer::_rewriteRequest(AsyncWebServerRequest *request){
|
||||
}
|
||||
|
||||
void AsyncWebServer::_attachHandler(AsyncWebServerRequest *request){
|
||||
for(const auto& h: _handlers){
|
||||
for(auto& h: _handlers){
|
||||
if (h->filter(request) && h->canHandle(request)){
|
||||
request->setHandler(h);
|
||||
request->setHandler(h.get());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -203,7 +208,7 @@ void AsyncWebServer::onRequestBody(ArBodyHandlerFunction fn){
|
||||
|
||||
void AsyncWebServer::reset(){
|
||||
_rewrites.clear();
|
||||
_handlers.free();
|
||||
_handlers.clear();
|
||||
|
||||
if (_catchAllHandler != NULL){
|
||||
_catchAllHandler->onRequest(NULL);
|
||||
|
Reference in New Issue
Block a user