Skip to content

Commit 849ec57

Browse files
ayushsharma82me-no-devpre-commit-ci-lite[bot]
authored
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 <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 211520b commit 849ec57

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Diff for: libraries/WebServer/src/WebServer.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,38 @@ void WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunctio
318318
_addRequestHandler(new FunctionRequestHandler(fn, ufn, uri, method));
319319
}
320320

321+
bool WebServer::removeRoute(const char *uri) {
322+
return removeRoute(String(uri), HTTP_ANY);
323+
}
324+
325+
bool WebServer::removeRoute(const char *uri, HTTPMethod method) {
326+
return removeRoute(String(uri), method);
327+
}
328+
329+
bool WebServer::removeRoute(const String &uri) {
330+
return removeRoute(uri, HTTP_ANY);
331+
}
332+
333+
bool WebServer::removeRoute(const String &uri, HTTPMethod method) {
334+
// Loop through all request handlers and see if there is a match
335+
RequestHandler *handler = _firstHandler;
336+
while (handler) {
337+
if (handler->canHandle(method, uri)) {
338+
return _removeRequestHandler(handler);
339+
}
340+
handler = handler->next();
341+
}
342+
return false;
343+
}
344+
321345
void WebServer::addHandler(RequestHandler *handler) {
322346
_addRequestHandler(handler);
323347
}
324348

349+
bool WebServer::removeHandler(RequestHandler *handler) {
350+
return _removeRequestHandler(handler);
351+
}
352+
325353
void WebServer::_addRequestHandler(RequestHandler *handler) {
326354
if (!_lastHandler) {
327355
_firstHandler = handler;
@@ -332,6 +360,32 @@ void WebServer::_addRequestHandler(RequestHandler *handler) {
332360
}
333361
}
334362

363+
bool WebServer::_removeRequestHandler(RequestHandler *handler) {
364+
RequestHandler *current = _firstHandler;
365+
RequestHandler *previous = nullptr;
366+
367+
while (current != nullptr) {
368+
if (current == handler) {
369+
if (previous == nullptr) {
370+
_firstHandler = current->next();
371+
} else {
372+
previous->next(current->next());
373+
}
374+
375+
if (current == _lastHandler) {
376+
_lastHandler = previous;
377+
}
378+
379+
// Delete 'matching' handler
380+
delete current;
381+
return true;
382+
}
383+
previous = current;
384+
current = current->next();
385+
}
386+
return false;
387+
}
388+
335389
void WebServer::serveStatic(const char *uri, FS &fs, const char *path, const char *cache_header) {
336390
_addRequestHandler(new StaticRequestHandler(fs, path, uri, cache_header));
337391
}

Diff for: libraries/WebServer/src/WebServer.h

+6
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ class WebServer {
147147
void on(const Uri &uri, THandlerFunction fn);
148148
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn);
149149
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads
150+
bool removeRoute(const char *uri);
151+
bool removeRoute(const char *uri, HTTPMethod method);
152+
bool removeRoute(const String &uri);
153+
bool removeRoute(const String &uri, HTTPMethod method);
150154
void addHandler(RequestHandler *handler);
155+
bool removeHandler(RequestHandler *handler);
151156
void serveStatic(const char *uri, fs::FS &fs, const char *path, const char *cache_header = NULL);
152157
void onNotFound(THandlerFunction fn); //called when handler is not assigned
153158
void onFileUpload(THandlerFunction ufn); //handle file uploads
@@ -230,6 +235,7 @@ class WebServer {
230235
return _currentClient.write_P(b, l);
231236
}
232237
void _addRequestHandler(RequestHandler *handler);
238+
bool _removeRequestHandler(RequestHandler *handler);
233239
void _handleRequest();
234240
void _finalizeResponse();
235241
bool _parseRequest(NetworkClient &client);

0 commit comments

Comments
 (0)