Skip to content

Commit

Permalink
add server.reset() - fixes #154
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeken committed Aug 12, 2024
1 parent 73312aa commit 884e7e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/PsychicHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/PsychicHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};
Expand Down

0 comments on commit 884e7e4

Please sign in to comment.