Unverified Commit 849ec57c authored by Ayush Sharma's avatar Ayush Sharma Committed by GitHub

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: default avatarMe No Dev <me-no-dev@users.noreply.github.com>
Co-authored-by: default avatarpre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
parent 211520b4
......@@ -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));
}
......
......@@ -147,7 +147,12 @@ public:
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 @@ protected:
return _currentClient.write_P(b, l);
}
void _addRequestHandler(RequestHandler *handler);
bool _removeRequestHandler(RequestHandler *handler);
void _handleRequest();
void _finalizeResponse();
bool _parseRequest(NetworkClient &client);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment