wip match functionality

This commit is contained in:
2018-04-26 00:11:46 +02:00
parent dfbc0aae25
commit 2f178a3e6b
6 changed files with 47 additions and 13 deletions

View File

@@ -94,3 +94,13 @@ WebConnection *WebController::new_connection(struct uhttpd_ops *an_ops, struct c
{ {
return new WebConnection(an_ops, a_client); return new WebConnection(an_ops, a_client);
} }
/*! ----------------------------------------------------------------------------
* @fn find
*
* @brief return true if the controller match with the url.
*/
bool WebController::find(const std::string &an_url)
{
return true;
}

View File

@@ -44,6 +44,8 @@ class WebController
WebController(const std::string &a_path); WebController(const std::string &a_path);
virtual ~WebController(void); virtual ~WebController(void);
virtual bool find(const std::string &an_url);
void set_name(const std::string &a_name); void set_name(const std::string &a_name);
const std::string &get_name(void); const std::string &get_name(void);

View File

@@ -142,6 +142,18 @@ bool RestController::from_json(struct json_object *a_node)
} }
/*! ----------------------------------------------------------------------------
* @fn find
*
* @brief return true if the controller match with the url.
*/
bool RestController::find(const std::string &an_url)
{
return true;
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_method_get * @fn get_method_get
* *

View File

@@ -56,6 +56,9 @@ class RestController : public WebController
bool from_json(struct json_object *a_node); bool from_json(struct json_object *a_node);
bool find(const std::string &an_url);
std::string get_method_get(void); std::string get_method_get(void);
std::string get_method_put(void); std::string get_method_put(void);
uint16_t get_timeout(void); uint16_t get_timeout(void);

View File

@@ -240,13 +240,16 @@ UhttpServer::UhttpServer(void) : m_ctx(NULL),
*/ */
UhttpServer::~UhttpServer(void) UhttpServer::~UhttpServer(void)
{ {
ControllerIterator it;
ConnectionIterator the_cnx_it; ConnectionIterator the_cnx_it;
for (it = m_controller_array.begin(); it != m_controller_array.end(); ++it)
for (std::vector< WebController *>::iterator the_it = m_controllers.begin();
the_it != m_controllers.end(); ++the_it)
{ {
delete it->second; delete *the_it;
} }
m_controllers.clear();
for (the_cnx_it = m_connections.begin(); the_cnx_it != m_connections.end(); ++the_cnx_it) for (the_cnx_it = m_connections.begin(); the_cnx_it != m_connections.end(); ++the_cnx_it)
{ {
delete the_cnx_it->second; delete the_cnx_it->second;
@@ -285,13 +288,15 @@ struct ubus_context *UhttpServer::get_context(void)
*/ */
bool UhttpServer::check_url(const std::string &an_url) bool UhttpServer::check_url(const std::string &an_url)
{ {
std::list<std::string>::iterator the_it; std::vector<WebController *>::iterator the_it;
// printf("UhttpServer::check_url: %s\n", an_url.c_str()); printf("UhttpServer::check_url: %s\n", an_url.c_str());
for (the_it = m_path_list.begin(); the_it != m_path_list.end(); the_it++) for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
{ {
if (an_url.find(*the_it) != std::string::npos) if ( (*the_it)->find(an_url))
{
return true; return true;
}
} }
return false; return false;
@@ -307,7 +312,7 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
std::string the_url, the_parameters; std::string the_url, the_parameters;
std::size_t the_pos; std::size_t the_pos;
WebController *the_controller; WebController *the_controller;
// printf("uhttp_server_handle_request : url: <%s> client: %p\n", an_url.c_str(), a_cl); printf("uhttp_server_handle_request : url: <%s> client: %p\n", an_url.c_str(), a_cl);
// Check if parameters are present on the url. // Check if parameters are present on the url.
the_pos = an_url.find_first_of("?", 0); the_pos = an_url.find_first_of("?", 0);
@@ -323,7 +328,8 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
the_url = an_url; the_url = an_url;
} }
the_controller = m_controller_array[the_url]; #warning todo
// the_controller = m_controller_array[the_url];
if (the_controller == NULL) if (the_controller == NULL)
{ {
std::string the_msg; std::string the_msg;
@@ -657,10 +663,11 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
return -1; return -1;
a_controller->set_name(an_uri); a_controller->set_name(an_uri);
m_controller_array[an_uri] = a_controller; //m_controller_array[an_uri] = a_controller;
// printf("add: an_uri:%s\n", an_uri.c_str()); 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. // 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); the_pos = an_uri.find_first_of("/", 1);
if (the_pos != std::string::npos) if (the_pos != std::string::npos)
@@ -672,7 +679,7 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
m_path_list.push_back(the_path); m_path_list.push_back(the_path);
} }
} }
#endif
return 0; return 0;
} }

View File

@@ -80,7 +80,7 @@ class UhttpServer
private: private:
struct ubus_context *m_ctx; struct ubus_context *m_ctx;
struct uhttpd_ops *m_ops; struct uhttpd_ops *m_ops;
ControllerContainer m_controller_array; std::vector< WebController *> m_controllers;
ConnectionContainer m_connections; ConnectionContainer m_connections;
std::list<std::string> m_path_list; std::list<std::string> m_path_list;
}; };