wip plugin
This commit is contained in:
@@ -100,7 +100,7 @@ WebConnection *WebController::new_connection(struct uhttpd_ops *an_ops, struct c
|
||||
*
|
||||
* @brief return true if the controller match with the url.
|
||||
*/
|
||||
bool WebController::find(const std::string &an_url)
|
||||
bool WebController::find(uint8_t a_method, const std::string &an_url)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class WebController
|
||||
WebController(const std::string &a_path);
|
||||
virtual ~WebController(void);
|
||||
|
||||
virtual bool find(const std::string &an_url);
|
||||
virtual bool find(uint8_t a_method, const std::string &an_url);
|
||||
|
||||
void set_name(const std::string &a_name);
|
||||
const std::string &get_name(void);
|
||||
|
||||
@@ -49,11 +49,11 @@ static struct dispatch_handler g_ubus_dispatch = {
|
||||
};
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn uh_awox_api_plugin_init
|
||||
* @fn uh_rest_api_plugin_init
|
||||
*
|
||||
* @brief
|
||||
*/
|
||||
static int uh_awox_api_plugin_init(const struct uhttpd_ops *an_ops, struct config *a_conf)
|
||||
static int uh_rest_api_plugin_init(const struct uhttpd_ops *an_ops, struct config *a_conf)
|
||||
{
|
||||
struct ubus_context *the_ctx;
|
||||
|
||||
@@ -72,11 +72,11 @@ static int uh_awox_api_plugin_init(const struct uhttpd_ops *an_ops, struct confi
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn uh_awox_api_plugin_post_init
|
||||
* @fn uh_rest_api_plugin_post_init
|
||||
*
|
||||
* @brief
|
||||
*/
|
||||
static void uh_awox_api_plugin_post_init(void)
|
||||
static void uh_rest_api_plugin_post_init(void)
|
||||
{
|
||||
ubus_add_uloop(get_uhttp_server_ctx());
|
||||
}
|
||||
@@ -85,6 +85,6 @@ static void uh_awox_api_plugin_post_init(void)
|
||||
|
||||
struct uhttpd_plugin uhttpd_plugin = {
|
||||
|
||||
.init = uh_awox_api_plugin_init,
|
||||
.post_init = uh_awox_api_plugin_post_init,
|
||||
.init = uh_rest_api_plugin_init,
|
||||
.post_init = uh_rest_api_plugin_post_init,
|
||||
};
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <json-c/json.h>
|
||||
|
||||
#include "rest/rest-connection.h"
|
||||
@@ -108,17 +110,7 @@ bool RestController::from_json(struct json_object *a_node)
|
||||
// endpoint
|
||||
if (json_object_object_get_ex(a_node, kControllerEndpointKey, &the_value_node))
|
||||
{
|
||||
int the_len, i;
|
||||
struct json_object *the_ep_node;
|
||||
the_len = json_object_array_length(the_value_node);
|
||||
|
||||
for (i = 0; i < the_len; i++)
|
||||
{
|
||||
std::string the_endpoint;
|
||||
the_ep_node = json_object_array_get_idx(the_value_node, i);
|
||||
the_endpoint = json_object_get_string(the_ep_node);
|
||||
m_endpoint.push_back(the_endpoint);
|
||||
}
|
||||
m_endpoint = json_object_get_string(the_value_node);
|
||||
}
|
||||
|
||||
// ubus
|
||||
@@ -144,21 +136,28 @@ bool RestController::from_json(struct json_object *a_node)
|
||||
*
|
||||
* @brief return true if the controller match with the url.
|
||||
*/
|
||||
bool RestController::find(const std::string &an_url)
|
||||
bool RestController::find(uint8_t a_method, const std::string &an_url)
|
||||
{
|
||||
std::vector<std::string>::iterator the_it;
|
||||
std::size_t the_pos;
|
||||
|
||||
printf("find : %s\n", an_url.c_str());
|
||||
printf("find : url: %s, endpoint:%s \n", an_url.c_str(), m_endpoint.c_str());
|
||||
|
||||
for (the_it = m_endpoint.begin(); the_it != m_endpoint.end(); ++the_it)
|
||||
if ((m_endpoint == an_url) && (m_method == a_method))
|
||||
{
|
||||
int the_error;
|
||||
the_error = (*the_it).compare(an_url);
|
||||
printf("compare <%s> et <%s> %d\n", (*the_it).c_str(), an_url.c_str(), the_error);
|
||||
if (the_error == 0)
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
the_pos = m_endpoint.find_first_of(":");
|
||||
if (the_pos == std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("ya un param.\n");
|
||||
std::string the_endpoint = m_endpoint.substr(the_pos);
|
||||
printf("endpoint: %s\n", the_endpoint.c_str());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -172,6 +171,16 @@ const Ubus &RestController::get_ubus(void) const
|
||||
return m_ubus;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn get_endpoint
|
||||
*
|
||||
* @brief return the endpoint for this controller.
|
||||
*/
|
||||
|
||||
const std::string &RestController::get_endpoint(void) const
|
||||
{
|
||||
return m_endpoint;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn is_raw_response
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <core/web-controller.h>
|
||||
#include <rest/ubus.h>
|
||||
@@ -56,9 +55,10 @@ class RestController : public WebController
|
||||
|
||||
bool from_json(struct json_object *a_node);
|
||||
|
||||
bool find(const std::string &an_url);
|
||||
bool find(uint8_t a_method, const std::string &an_url);
|
||||
|
||||
const Ubus &get_ubus(void) const;
|
||||
const std::string &get_endpoint(void) const;
|
||||
|
||||
bool is_raw_response(void);
|
||||
|
||||
@@ -71,7 +71,7 @@ class RestController : public WebController
|
||||
*/
|
||||
protected:
|
||||
uint8_t m_method;
|
||||
std::vector<std::string> m_endpoint;
|
||||
std::string m_endpoint;
|
||||
Ubus m_ubus;
|
||||
bool mf_raw_response;
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ extern "C" {
|
||||
|
||||
/*------------------------------- GLOBALS ----------------------------------*/
|
||||
|
||||
#define kControllerKey "controller"
|
||||
#define kResourcesKey "resources"
|
||||
#define kEtagControllerKey "etag_controller"
|
||||
|
||||
#define kNotificationKey "notification"
|
||||
@@ -242,12 +242,6 @@ UhttpServer::~UhttpServer(void)
|
||||
{
|
||||
ConnectionIterator the_cnx_it;
|
||||
|
||||
for (std::vector< WebController *>::iterator the_it = m_controllers.begin();
|
||||
the_it != m_controllers.end(); ++the_it)
|
||||
{
|
||||
delete *the_it;
|
||||
}
|
||||
|
||||
m_controllers.clear();
|
||||
|
||||
for (the_cnx_it = m_connections.begin(); the_cnx_it != m_connections.end(); ++the_cnx_it)
|
||||
@@ -288,12 +282,12 @@ struct ubus_context *UhttpServer::get_context(void)
|
||||
*/
|
||||
bool UhttpServer::check_url(const std::string &an_url)
|
||||
{
|
||||
std::vector<WebController *>::iterator the_it;
|
||||
ControllerIterator the_it;
|
||||
printf("UhttpServer::check_url: %s\n", an_url.c_str());
|
||||
|
||||
for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
|
||||
{
|
||||
if ( (*the_it)->find(an_url))
|
||||
if ( the_it->second.get()->find(0, an_url))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -328,7 +322,7 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
|
||||
the_url = an_url;
|
||||
}
|
||||
|
||||
the_controller = get_controller(the_url);
|
||||
the_controller = get_controller(a_cl->request.method, the_url);
|
||||
if (the_controller == NULL)
|
||||
{
|
||||
printf ("not found :( \n");
|
||||
@@ -474,7 +468,7 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node)
|
||||
int the_len;
|
||||
int i;
|
||||
|
||||
if (json_object_object_get_ex(a_root_node, kControllerKey, &the_ctr_array_node))
|
||||
if (json_object_object_get_ex(a_root_node, kResourcesKey, &the_ctr_array_node))
|
||||
{
|
||||
RestController *the_rest_controller;
|
||||
the_len = json_object_array_length(the_ctr_array_node);
|
||||
@@ -487,7 +481,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_rest_controller);
|
||||
add_controller(the_rest_controller->get_endpoint(), the_rest_controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -660,18 +654,12 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
|
||||
{
|
||||
std::string the_path;
|
||||
std::size_t the_pos;
|
||||
|
||||
if (a_controller == NULL)
|
||||
return -1;
|
||||
|
||||
if (an_uri.empty())
|
||||
{
|
||||
m_controllers.push_back(a_controller);
|
||||
}
|
||||
else {
|
||||
|
||||
a_controller->set_name(an_uri);
|
||||
// TODO
|
||||
}
|
||||
a_controller->set_name(an_uri);
|
||||
m_controllers[an_uri] = std::unique_ptr<WebController>(a_controller);
|
||||
|
||||
//m_controller_array[an_uri] = a_controller;
|
||||
|
||||
@@ -698,17 +686,17 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
|
||||
*
|
||||
* @brief Send an error message to the client.
|
||||
*/
|
||||
WebController *UhttpServer::get_controller(const std::string &an_url)
|
||||
WebController *UhttpServer::get_controller(uint8_t a_method, const std::string &an_url)
|
||||
{
|
||||
std::vector<WebController *>::iterator the_it;
|
||||
ControllerIterator 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))
|
||||
if (the_it->second.get()->find(a_method, an_url))
|
||||
{
|
||||
printf ("return the controller.\n");
|
||||
return (*the_it);
|
||||
return the_it->second.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
@@ -45,7 +46,7 @@ class WebConnection;
|
||||
class UhttpServer
|
||||
{
|
||||
public:
|
||||
typedef std::unordered_map<std::string, WebController *> ControllerContainer;
|
||||
typedef std::unordered_map<std::string, std::unique_ptr<WebController>> ControllerContainer;
|
||||
typedef ControllerContainer::iterator ControllerIterator;
|
||||
typedef std::unordered_map<int, WebConnection *> ConnectionContainer;
|
||||
typedef ConnectionContainer::iterator ConnectionIterator;
|
||||
@@ -75,13 +76,13 @@ class UhttpServer
|
||||
std::string &a_get_method, std::string &a_set_method, bool &a_raw_response);
|
||||
struct json_object *load(const std::string &a_file);
|
||||
int add_controller(const std::string &an_uri, WebController *a_controller);
|
||||
WebController *get_controller(const std::string &an_url);
|
||||
WebController *get_controller(uint8_t a_method, const std::string &an_url);
|
||||
void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg);
|
||||
|
||||
private:
|
||||
struct ubus_context *m_ctx;
|
||||
struct uhttpd_ops *m_ops;
|
||||
std::vector< WebController *> m_controllers;
|
||||
ControllerContainer m_controllers;
|
||||
ConnectionContainer m_connections;
|
||||
std::list<std::string> m_path_list;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user