From c220bf66066cc27da63c04cce65a716e4d5627ca Mon Sep 17 00:00:00 2001 From: ZanzyTHEbar Date: Mon, 29 Aug 2022 13:23:55 +0100 Subject: [PATCH] update - Fixed bug in request handler - needed to add support for non-param URL requests --- ESP/lib/src/data/config/project_config.cpp | 6 +- ESP/lib/src/network/api/webserverHandler.cpp | 69 +++++++++++++------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/ESP/lib/src/data/config/project_config.cpp b/ESP/lib/src/data/config/project_config.cpp index 4430aad..3966213 100644 --- a/ESP/lib/src/data/config/project_config.cpp +++ b/ESP/lib/src/data/config/project_config.cpp @@ -65,10 +65,8 @@ void ProjectConfig::load() ESP.restart(); return; } - else - { - log_d("Project config found - Config length: %d", configLen); - } + + log_d("Project config found - Config length: %d", configLen); char buff[configLen]; getBytes("config", buff, configLen); diff --git a/ESP/lib/src/network/api/webserverHandler.cpp b/ESP/lib/src/network/api/webserverHandler.cpp index c9223d6..39f067d 100644 --- a/ESP/lib/src/network/api/webserverHandler.cpp +++ b/ESP/lib/src/network/api/webserverHandler.cpp @@ -41,7 +41,6 @@ void APIServer::setupServer() routes.emplace("deleteRoute", &APIServer::deleteRoute); // Camera Routes - //! reserve enough memory for all routes - must be called after adding routes and before adding routes to route_map indexes.reserve(routes.size()); // this is done to avoid reallocation of memory and copying of data @@ -80,46 +79,68 @@ void APIServer::handleRequest(AsyncWebServerRequest *request) { try { + size_t params = request->params(); // Get the route log_i("Request URL: %s", request->url().c_str()); - int params = request->params(); - auto it_map = route_map.find(request->pathArg(0).c_str()); - log_i("Request First Arg: %s", request->pathArg(0).c_str()); - auto it_method = it_map->second.find(request->pathArg(1).c_str()); - log_i("Request Second Arg: %s", request->pathArg(1).c_str()); + log_i("Request: %s", request->pathArg(0).c_str()); + log_i("Request: %s", request->pathArg(1).c_str()); - for (int i = 0; i < params; i++) + auto it_map = route_map.find(request->pathArg(0).c_str()); + auto it_method = it_map->second.find(request->pathArg(1).c_str()); + + log_d("Params: %d", params); + if (params > 0) { - AsyncWebParameter *param = request->getParam(i); + log_d("We have params!"); + for (size_t i = 0; i < params; i++) { + log_d("We are executing the for loop"); + AsyncWebParameter *param = request->getParam(i); + if (it_map != route_map.end()) { - if (it_map != route_map.end()) + if (it_method != it_map->second.end()) { - if (it_method != it_map->second.end()) - { - (*this.*(it_method->second))(request); - } - else - { - request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Command\"}"); - request->redirect("/"); - return; - } + (*this.*(it_method->second))(request); } else { - request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Map Index\"}"); - request->redirect("/"); + request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Command\"}"); return; } } + else + { + request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Map Index\"}"); + return; + } log_i("%s[%s]: %s\n", _networkMethodsMap[request->method()].c_str(), param->name().c_str(), param->value().c_str()); } } - request->send(200, MIMETYPE_JSON, "{\"msg\":\"Command executed\"}"); + else + { + log_d("No params, so we skipped the for loop"); + if (it_map != route_map.end()) + { + if (it_method != it_map->second.end()) + { + log_d("We are trying to execute the function"); + (*this.*(it_method->second))(request); + } + else + { + request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Command\"}"); + return; + } + } + else + { + request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Map Index\"}"); + return; + } + } } - catch (...) // catch all exceptions + catch (...) { - request->send(400, MIMETYPE_JSON, "{\"msg\":\"An Error has occurred\"}"); + log_e("Error handling request"); } }