wip manage controller

This commit is contained in:
2018-04-29 23:14:44 +02:00
parent 2f178a3e6b
commit e65a08ed1f
9 changed files with 143 additions and 39 deletions

View File

@@ -328,16 +328,17 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
the_url = an_url;
}
#warning todo
// the_controller = m_controller_array[the_url];
the_controller = get_controller(the_url);
if (the_controller == NULL)
{
printf ("not found :( \n");
std::string the_msg;
the_msg = fmt::format(kJsonControlerNotFound, the_url);
send_error(a_cl, 404, "Not Found", the_msg);
return;
}
#warning TODO check if controller is available for this method.
// We found the controller.
// printf("method: %d\n", a_cl->request.method);
struct dispatch *d = &a_cl->dispatch;
@@ -362,11 +363,11 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
break;
default:
// printf("Invalid Request\n");
printf("Invalid Request\n");
send_error(a_cl, 400, "Bad Request", "Invalid Request");
}
// printf("uhttp_server_handle_request -done.\n");
printf("uhttp_server_handle_request -done.\n");
}
/*! ----------------------------------------------------------------------------
@@ -486,7 +487,7 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node)
if (the_rest_controller->from_json(the_ctrl_node))
{
//new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response)
add_controller(the_path, the_rest_controller);
add_controller("", the_rest_controller);
}
}
}
@@ -662,12 +663,21 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
if (a_controller == NULL)
return -1;
a_controller->set_name(an_uri);
if (an_uri.empty())
{
m_controllers.push_back(a_controller);
}
else {
a_controller->set_name(an_uri);
// TODO
}
//m_controller_array[an_uri] = a_controller;
#if 0
printf("add: an_uri:%s\n", an_uri.c_str());
#if 0
// Keep a list of the API root. to check if a controller is managed by this plugin or not.
the_pos = an_uri.find_first_of("/", 1);
if (the_pos != std::string::npos)
@@ -683,6 +693,28 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn send_error
*
* @brief Send an error message to the client.
*/
WebController *UhttpServer::get_controller(const std::string &an_url)
{
std::vector<WebController *>::iterator the_it;
WebController *the_controller = NULL;
for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
{
if ((*the_it)->find(an_url))
{
printf ("return the controller.\n");
return (*the_it);
}
}
return the_controller;
}
/*! ----------------------------------------------------------------------------
* @fn send_error
*