diff --git a/src/PsychicHttpServer.cpp b/src/PsychicHttpServer.cpp index f60a250..3eade2c 100644 --- a/src/PsychicHttpServer.cpp +++ b/src/PsychicHttpServer.cpp @@ -46,6 +46,8 @@ PsychicHttpServer::PsychicHttpServer(uint16_t port) : _onOpen(NULL), PsychicHttpServer::~PsychicHttpServer() { + _esp_idf_endpoints.clear(); + for (auto* client : _clients) delete (client); _clients.clear(); @@ -200,6 +202,34 @@ esp_err_t PsychicHttpServer::_stopServer() return httpd_stop(this->server); } +void PsychicHttpServer::reset() +{ + if (_running) + stop(); + + for (auto* client : _clients) + delete (client); + _clients.clear(); + + for (auto* endpoint : _endpoints) + delete (endpoint); + _endpoints.clear(); + + for (auto* handler : _handlers) + delete (handler); + _handlers.clear(); + + for (auto* rewrite : _rewrites) + delete (rewrite); + _rewrites.clear(); + + _esp_idf_endpoints.clear(); + + onNotFound(PsychicHttpServer::defaultNotFoundHandler); + _onOpen = nullptr; + _onClose = nullptr; +} + httpd_uri_match_func_t PsychicHttpServer::getURIMatchFunction() { return _uri_match_fn; diff --git a/src/PsychicHttpServer.h b/src/PsychicHttpServer.h index c30767b..5a09ae0 100644 --- a/src/PsychicHttpServer.h +++ b/src/PsychicHttpServer.h @@ -65,6 +65,7 @@ class PsychicHttpServer esp_err_t end() { return stop(); } esp_err_t start(); esp_err_t stop(); + void reset(); httpd_uri_match_func_t getURIMatchFunction(); void setURIMatchFunction(httpd_uri_match_func_t match_fn); @@ -99,12 +100,12 @@ class PsychicHttpServer static esp_err_t requestHandler(httpd_req_t* req); static esp_err_t notFoundHandler(httpd_req_t* req, httpd_err_code_t err); static esp_err_t defaultNotFoundHandler(PsychicRequest* request); - void onNotFound(PsychicHttpRequestCallback fn); + static esp_err_t openCallback(httpd_handle_t hd, int sockfd); + static void closeCallback(httpd_handle_t hd, int sockfd); + void onNotFound(PsychicHttpRequestCallback fn); void onOpen(PsychicClientCallback handler); void onClose(PsychicClientCallback handler); - static esp_err_t openCallback(httpd_handle_t hd, int sockfd); - static void closeCallback(httpd_handle_t hd, int sockfd); PsychicStaticFileHandler* serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_control = NULL); };