@@ -318,10 +318,38 @@ void WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunctio
318
318
_addRequestHandler (new FunctionRequestHandler (fn, ufn, uri, method));
319
319
}
320
320
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
+
321
345
void WebServer::addHandler (RequestHandler *handler) {
322
346
_addRequestHandler (handler);
323
347
}
324
348
349
+ bool WebServer::removeHandler (RequestHandler *handler) {
350
+ return _removeRequestHandler (handler);
351
+ }
352
+
325
353
void WebServer::_addRequestHandler (RequestHandler *handler) {
326
354
if (!_lastHandler) {
327
355
_firstHandler = handler;
@@ -332,6 +360,32 @@ void WebServer::_addRequestHandler(RequestHandler *handler) {
332
360
}
333
361
}
334
362
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
+
335
389
void WebServer::serveStatic (const char *uri, FS &fs, const char *path, const char *cache_header) {
336
390
_addRequestHandler (new StaticRequestHandler (fs, path, uri, cache_header));
337
391
}
0 commit comments