AsyncWebServer::_handlers repalce with STL container

repalce LinkedList with std::list<std::unique_ptr<AsyncWebHandler> > _handlers;
This commit is contained in:
Emil Muratov
2024-06-26 22:46:24 +09:00
committed by Mathieu Carbou
parent d5c2af8dc2
commit 9bd3cd9472
2 changed files with 15 additions and 10 deletions

View File

@@ -358,11 +358,11 @@ class AsyncWebRewrite {
class AsyncWebHandler { class AsyncWebHandler {
protected: protected:
ArRequestFilterFunction _filter; ArRequestFilterFunction _filter{nullptr};
String _username; String _username;
String _password; String _password;
public: public:
AsyncWebHandler():_username(""), _password(""){} AsyncWebHandler(){}
AsyncWebHandler& setFilter(ArRequestFilterFunction fn) { _filter = fn; return *this; } 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 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; }; AsyncWebHandler& setAuthentication(const String& username, const String& password){ _username = username;_password = password; return *this; };
@@ -430,7 +430,7 @@ class AsyncWebServer {
protected: protected:
AsyncServer _server; AsyncServer _server;
std::list<std::shared_ptr<AsyncWebRewrite> > _rewrites; std::list<std::shared_ptr<AsyncWebRewrite> > _rewrites;
LinkedList<AsyncWebHandler*> _handlers; std::list<std::unique_ptr<AsyncWebHandler> > _handlers;
AsyncCallbackWebHandler* _catchAllHandler; AsyncCallbackWebHandler* _catchAllHandler;
public: public:

View File

@@ -37,7 +37,6 @@ const char *fs::FileOpenMode::append = "a";
AsyncWebServer::AsyncWebServer(uint16_t port) AsyncWebServer::AsyncWebServer(uint16_t port)
: _server(port) : _server(port)
, _handlers(LinkedList<AsyncWebHandler*>([](AsyncWebHandler* h){ delete h; }))
{ {
_catchAllHandler = new AsyncCallbackWebHandler(); _catchAllHandler = new AsyncCallbackWebHandler();
if(_catchAllHandler == NULL) if(_catchAllHandler == NULL)
@@ -92,12 +91,18 @@ AsyncWebRewrite& AsyncWebServer::rewrite(const char* from, const char* to){
} }
AsyncWebHandler& AsyncWebServer::addHandler(AsyncWebHandler* handler){ AsyncWebHandler& AsyncWebServer::addHandler(AsyncWebHandler* handler){
_handlers.add(handler); _handlers.emplace_back(handler);
return *handler; return *(_handlers.back().get());
} }
bool AsyncWebServer::removeHandler(AsyncWebHandler *handler){ 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(){ void AsyncWebServer::begin(){
@@ -133,9 +138,9 @@ void AsyncWebServer::_rewriteRequest(AsyncWebServerRequest *request){
} }
void AsyncWebServer::_attachHandler(AsyncWebServerRequest *request){ void AsyncWebServer::_attachHandler(AsyncWebServerRequest *request){
for(const auto& h: _handlers){ for(auto& h: _handlers){
if (h->filter(request) && h->canHandle(request)){ if (h->filter(request) && h->canHandle(request)){
request->setHandler(h); request->setHandler(h.get());
return; return;
} }
} }
@@ -203,7 +208,7 @@ void AsyncWebServer::onRequestBody(ArBodyHandlerFunction fn){
void AsyncWebServer::reset(){ void AsyncWebServer::reset(){
_rewrites.clear(); _rewrites.clear();
_handlers.free(); _handlers.clear();
if (_catchAllHandler != NULL){ if (_catchAllHandler != NULL){
_catchAllHandler->onRequest(NULL); _catchAllHandler->onRequest(NULL);