Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions libraries/WebServer/src/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down
6 changes: 6 additions & 0 deletions libraries/WebServer/src/WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down