Skip to content
This repository has been archived by the owner on Jan 21, 2025. It is now read-only.

Commit

Permalink
AsyncWebServer::_handlers repalce with STL container
Browse files Browse the repository at this point in the history
repalce LinkedList with std::list<std::unique_ptr<AsyncWebHandler> > _handlers;
  • Loading branch information
vortigont authored and mathieucarbou committed Jun 27, 2024
1 parent d5c2af8 commit 9bd3cd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 12 additions & 7 deletions src/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -203,7 +208,7 @@ void AsyncWebServer::onRequestBody(ArBodyHandlerFunction fn){

void AsyncWebServer::reset(){
_rewrites.clear();
_handlers.free();
_handlers.clear();

if (_catchAllHandler != NULL){
_catchAllHandler->onRequest(NULL);
Expand Down

0 comments on commit 9bd3cd9

Please sign in to comment.