From 849ec57ca6b815e55c8b86767d3cb33d5db9d4ce Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 13 Jun 2024 11:21:32 +0530 Subject: [PATCH] Added support for removing routes in WebServer library (#9832) * feat: added removeRoutes and removeHandler methods * feat: added removeRoute and removeHandler methods * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Me No Dev Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- libraries/WebServer/src/WebServer.cpp | 54 +++++++++++++++++++++++++++ libraries/WebServer/src/WebServer.h | 6 +++ 2 files changed, 60 insertions(+) diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index 84b4de33518..83c22b7e493 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -318,10 +318,38 @@ void WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunctio _addRequestHandler(new FunctionRequestHandler(fn, ufn, uri, method)); } +bool WebServer::removeRoute(const char *uri) { + return removeRoute(String(uri), HTTP_ANY); +} + +bool WebServer::removeRoute(const char *uri, HTTPMethod method) { + return removeRoute(String(uri), method); +} + +bool WebServer::removeRoute(const String &uri) { + return removeRoute(uri, HTTP_ANY); +} + +bool WebServer::removeRoute(const String &uri, HTTPMethod method) { + // Loop through all request handlers and see if there is a match + RequestHandler *handler = _firstHandler; + while (handler) { + if (handler->canHandle(method, uri)) { + return _removeRequestHandler(handler); + } + handler = handler->next(); + } + return false; +} + void WebServer::addHandler(RequestHandler *handler) { _addRequestHandler(handler); } +bool WebServer::removeHandler(RequestHandler *handler) { + return _removeRequestHandler(handler); +} + void WebServer::_addRequestHandler(RequestHandler *handler) { if (!_lastHandler) { _firstHandler = handler; @@ -332,6 +360,32 @@ void WebServer::_addRequestHandler(RequestHandler *handler) { } } +bool WebServer::_removeRequestHandler(RequestHandler *handler) { + RequestHandler *current = _firstHandler; + RequestHandler *previous = nullptr; + + while (current != nullptr) { + if (current == handler) { + if (previous == nullptr) { + _firstHandler = current->next(); + } else { + previous->next(current->next()); + } + + if (current == _lastHandler) { + _lastHandler = previous; + } + + // Delete 'matching' handler + delete current; + return true; + } + previous = current; + current = current->next(); + } + return false; +} + void WebServer::serveStatic(const char *uri, FS &fs, const char *path, const char *cache_header) { _addRequestHandler(new StaticRequestHandler(fs, path, uri, cache_header)); } diff --git a/libraries/WebServer/src/WebServer.h b/libraries/WebServer/src/WebServer.h index 1382885e1d9..f69c08f22b6 100644 --- a/libraries/WebServer/src/WebServer.h +++ b/libraries/WebServer/src/WebServer.h @@ -147,7 +147,12 @@ class WebServer { void on(const Uri &uri, THandlerFunction fn); void on(const Uri &uri, HTTPMethod method, THandlerFunction fn); void on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads + bool removeRoute(const char *uri); + bool removeRoute(const char *uri, HTTPMethod method); + bool removeRoute(const String &uri); + bool removeRoute(const String &uri, HTTPMethod method); void addHandler(RequestHandler *handler); + bool removeHandler(RequestHandler *handler); void serveStatic(const char *uri, fs::FS &fs, const char *path, const char *cache_header = NULL); void onNotFound(THandlerFunction fn); //called when handler is not assigned void onFileUpload(THandlerFunction ufn); //handle file uploads @@ -230,6 +235,7 @@ class WebServer { return _currentClient.write_P(b, l); } void _addRequestHandler(RequestHandler *handler); + bool _removeRequestHandler(RequestHandler *handler); void _handleRequest(); void _finalizeResponse(); bool _parseRequest(NetworkClient &client);