- Fixed bug in request handler
- needed to add support for     non-param URL requests
This commit is contained in:
ZanzyTHEbar 2022-08-29 13:23:55 +01:00
parent 2d1906c5cc
commit c220bf6606
2 changed files with 47 additions and 28 deletions

View File

@ -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);

View File

@ -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");
}
}