Fix indent trouble

This commit is contained in:
jbnadal
2018-04-24 18:22:59 +02:00
parent b1b3f8742c
commit f2a9e3c9c7
29 changed files with 594 additions and 676 deletions

View File

@@ -74,7 +74,6 @@ void HttpHeader::send(uhttpd_ops *an_ops, client *a_client, uint16_t a_code, boo
if (!m_etag.empty()) if (!m_etag.empty())
{ {
ustream_printf(a_client->us, "ETag: %s\r\n", m_etag.c_str()); ustream_printf(a_client->us, "ETag: %s\r\n", m_etag.c_str());
} }

View File

@@ -34,8 +34,8 @@ struct uhttpd_ops;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class HttpHeader { class HttpHeader
{
public: public:
HttpHeader(void); HttpHeader(void);
HttpHeader(uint16_t a_timeout); HttpHeader(uint16_t a_timeout);

View File

@@ -34,7 +34,6 @@ extern "C" {
#include "http-parameter.h" #include "http-parameter.h"
#define kIfNoneMatch "if-none-match" #define kIfNoneMatch "if-none-match"
#define kPrefer "prefer" #define kPrefer "prefer"
#define kOrigin "origin" #define kOrigin "origin"
@@ -44,12 +43,10 @@ extern "C" {
* *
* @brief constructor of the http parameter. * @brief constructor of the http parameter.
*/ */
HttpParameter::HttpParameter (void) : HttpParameter::HttpParameter(void) : m_request_timeout(-1)
m_request_timeout(-1)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn parse_from_uri * @fn parse_from_uri
* *
@@ -66,12 +63,12 @@ std::string HttpParameter::parse_from_uri (const std::string &a_parameters)
the_root_node = json_object_new_object(); the_root_node = json_object_new_object();
the_list = split_params(a_parameters, '&'); the_list = split_params(a_parameters, '&');
for (the_it = the_list.begin(); the_it != the_list.end(); ++the_it) { for (the_it = the_list.begin(); the_it != the_list.end(); ++the_it)
{
// printf("--> %s\n", (*the_it).c_str()); // printf("--> %s\n", (*the_it).c_str());
the_pos = (*the_it).find_first_of("=", 0); the_pos = (*the_it).find_first_of("=", 0);
if (the_pos != std::string::npos) { if (the_pos != std::string::npos)
{
the_key = (*the_it).substr(0, the_pos); the_key = (*the_it).substr(0, the_pos);
the_value = (*the_it).substr(the_pos + 1); the_value = (*the_it).substr(the_pos + 1);
the_value_decoded = UriTransform::decode(the_value); the_value_decoded = UriTransform::decode(the_value);
@@ -100,7 +97,6 @@ std::string HttpParameter::parse_from_uri (const std::string &a_parameters)
return the_result; return the_result;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn parse * @fn parse
* *
@@ -116,20 +112,20 @@ bool HttpParameter::parse (client *a_client)
// printf("HttpParameter::parse: <%s>\n", the_string); // printf("HttpParameter::parse: <%s>\n", the_string);
free(the_string); free(the_string);
if (the_root_node == NULL) { if (the_root_node == NULL)
{
return false; return false;
} }
// Get if-none-match // Get if-none-match
if (json_object_object_get_ex(the_root_node, kIfNoneMatch, &the_value_node)) { if (json_object_object_get_ex(the_root_node, kIfNoneMatch, &the_value_node))
{
m_etag = json_object_get_string(the_value_node); m_etag = json_object_get_string(the_value_node);
} }
// Get Prefer - wait RFC 7240 // Prefer: wait=120 // Get Prefer - wait RFC 7240 // Prefer: wait=120
if (json_object_object_get_ex(the_root_node, kPrefer, &the_value_node)) { if (json_object_object_get_ex(the_root_node, kPrefer, &the_value_node))
{
std::vector<std::string> the_list; std::vector<std::string> the_list;
the_prefer_string = json_object_get_string(the_value_node); the_prefer_string = json_object_get_string(the_value_node);
the_list = split_params(the_prefer_string, '='); the_list = split_params(the_prefer_string, '=');
@@ -137,16 +133,16 @@ bool HttpParameter::parse (client *a_client)
//printf("the_prefer_string: %s\n", the_prefer_string.c_str()); //printf("the_prefer_string: %s\n", the_prefer_string.c_str());
//printf("the_token: %s\n", the_token.c_str()); //printf("the_token: %s\n", the_token.c_str());
if (is_number(the_token)) { if (is_number(the_token))
{
m_request_timeout = std::atoi(the_token.c_str()); m_request_timeout = std::atoi(the_token.c_str());
} }
// printf("timeout = %d\n", m_request_timeout); // printf("timeout = %d\n", m_request_timeout);
} }
// Get Origin // Get Origin
if (json_object_object_get_ex(the_root_node, kOrigin, &the_value_node)) { if (json_object_object_get_ex(the_root_node, kOrigin, &the_value_node))
{
m_origin = json_object_get_string(the_value_node); m_origin = json_object_get_string(the_value_node);
} }
@@ -157,7 +153,6 @@ bool HttpParameter::parse (client *a_client)
return true; return true;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_origin * @fn get_origin
* *
@@ -168,7 +163,6 @@ std::string HttpParameter::get_origin (void)
return m_origin; return m_origin;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_etag * @fn get_etag
* *
@@ -179,7 +173,6 @@ std::string HttpParameter::get_etag (void)
return m_etag; return m_etag;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_request_timeout * @fn get_request_timeout
* *
@@ -191,7 +184,6 @@ int16_t HttpParameter::get_request_timeout (void)
return m_request_timeout; return m_request_timeout;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn split_params * @fn split_params
* *
@@ -204,8 +196,8 @@ std::vector<std::string> HttpParameter::split_params (const std::string &a_strin
std::string::size_type the_prev_pos = 0, the_pos = 0; std::string::size_type the_prev_pos = 0, the_pos = 0;
while ((the_pos = a_string.find(a_seperator, the_pos)) != std::string::npos) { while ((the_pos = a_string.find(a_seperator, the_pos)) != std::string::npos)
{
std::string substring(a_string.substr(the_prev_pos, the_pos - the_prev_pos)); std::string substring(a_string.substr(the_prev_pos, the_pos - the_prev_pos));
the_output.push_back(substring); the_output.push_back(substring);
the_prev_pos = ++the_pos; the_prev_pos = ++the_pos;
@@ -216,7 +208,6 @@ std::vector<std::string> HttpParameter::split_params (const std::string &a_strin
return the_output; return the_output;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn is_number * @fn is_number
* *

View File

@@ -36,8 +36,8 @@ struct client;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class HttpParameter { class HttpParameter
{
public: public:
HttpParameter(void); HttpParameter(void);

View File

@@ -24,7 +24,6 @@
#include "http-reason.h" #include "http-reason.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn HttpReason * @fn HttpReason
* *
@@ -34,7 +33,6 @@ HttpReason::HttpReason (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get * @fn get
* *
@@ -42,7 +40,8 @@ HttpReason::HttpReason (void)
*/ */
std::string HttpReason::get(uint16_t a_code) std::string HttpReason::get(uint16_t a_code)
{ {
switch (a_code) { switch (a_code)
{
// --- 100 // --- 100
case 100: case 100:
return "Continue"; return "Continue";

View File

@@ -30,8 +30,8 @@
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class HttpReason { class HttpReason
{
public: public:
HttpReason(void); HttpReason(void);

View File

@@ -30,7 +30,6 @@ extern "C" {
#include "ubus-reason.h" #include "ubus-reason.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn UbusReason * @fn UbusReason
* *
@@ -40,7 +39,6 @@ UbusReason::UbusReason (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get * @fn get
* *
@@ -51,8 +49,8 @@ uint16_t UbusReason::get (int16_t an_exec_result)
int the_result_code; int the_result_code;
// printf("%s result code: %d\n", __PRETTY_FUNCTION__, an_exec_result); // printf("%s result code: %d\n", __PRETTY_FUNCTION__, an_exec_result);
switch (an_exec_result) { switch (an_exec_result)
{
case 0: case 0:
the_result_code = 200; // HTTP_OK the_result_code = 200; // HTTP_OK
break; break;

View File

@@ -30,8 +30,8 @@
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class UbusReason { class UbusReason
{
public: public:
UbusReason(void); UbusReason(void);

View File

@@ -24,8 +24,6 @@
#include "uri-transform.h" #include "uri-transform.h"
// Only alphanum is safe. // Only alphanum is safe.
const char SAFE[256] = const char SAFE[256] =
{ {
@@ -48,8 +46,7 @@ const char SAFE[256] =
/* C */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* D */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* E */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* F */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 /* F */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
};
const int8_t HEX2DEC[256] = const int8_t HEX2DEC[256] =
{ {
@@ -72,8 +69,7 @@ const int8_t HEX2DEC[256] =
/* C */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* C */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* D */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* D */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* E */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* E */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* F */ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1 /* F */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
};
std::string iso_8859_1_to_utf8(std::string str) std::string iso_8859_1_to_utf8(std::string str)
{ {
@@ -81,10 +77,12 @@ std::string iso_8859_1_to_utf8 (std::string str)
for (std::string::iterator it = str.begin(); it != str.end(); ++it) for (std::string::iterator it = str.begin(); it != str.end(); ++it)
{ {
uint8_t ch = *it; uint8_t ch = *it;
if (ch < 0x80) { if (ch < 0x80)
{
strOut.push_back(ch); strOut.push_back(ch);
} }
else { else
{
strOut.push_back(0xc0 | ch >> 6); strOut.push_back(0xc0 | ch >> 6);
strOut.push_back(0x80 | (ch & 0x3f)); strOut.push_back(0x80 | (ch & 0x3f));
} }
@@ -92,7 +90,6 @@ std::string iso_8859_1_to_utf8 (std::string str)
return strOut; return strOut;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn UriTransform * @fn UriTransform
* *
@@ -116,12 +113,12 @@ std::string UriTransform::encode (const std::string &a_src)
unsigned char *p_end = p_start; unsigned char *p_end = p_start;
const unsigned char *const SRC_END = p_src + SRC_LEN; const unsigned char *const SRC_END = p_src + SRC_LEN;
for (; p_src < SRC_END; ++p_src) { for (; p_src < SRC_END; ++p_src)
{
if (SAFE[*p_src]) if (SAFE[*p_src])
*p_end++ = *p_src; *p_end++ = *p_src;
else { else
{
// escape this char // escape this char
*p_end++ = '%'; *p_end++ = '%';
*p_end++ = DEC2HEX[*p_src >> 4]; *p_end++ = DEC2HEX[*p_src >> 4];
@@ -134,7 +131,6 @@ std::string UriTransform::encode (const std::string &a_src)
return s_result; return s_result;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn decode * @fn decode
* *
@@ -154,14 +150,13 @@ std::string UriTransform::decode (const std::string &a_src)
char *const p_start = new char[SRC_LEN]; char *const p_start = new char[SRC_LEN];
char *p_end = p_start; char *p_end = p_start;
while (p_src < SRC_LAST_DEC) { while (p_src < SRC_LAST_DEC)
{
if (*p_src == '%') { if (*p_src == '%')
{
char dec1, dec2; char dec1, dec2;
if (-1 != (dec1 = HEX2DEC[*(p_src + 1)]) if (-1 != (dec1 = HEX2DEC[*(p_src + 1)]) && -1 != (dec2 = HEX2DEC[*(p_src + 2)]))
&& -1 != (dec2 = HEX2DEC[*(p_src + 2)])) { {
*p_end++ = (dec1 << 4) + dec2; *p_end++ = (dec1 << 4) + dec2;
p_src += 3; p_src += 3;
continue; continue;

View File

@@ -30,8 +30,8 @@
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class UriTransform { class UriTransform
{
public: public:
UriTransform(void); UriTransform(void);

View File

@@ -27,20 +27,17 @@
#include "core/web-connection.h" #include "core/web-connection.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn WebConnection * @fn WebConnection
* *
* @brief constructor of the web connection object. * @brief constructor of the web connection object.
*/ */
WebConnection::WebConnection (struct uhttpd_ops *an_ops, struct client *a_client) : WebConnection::WebConnection(struct uhttpd_ops *an_ops, struct client *a_client) : m_ops(an_ops),
m_ops(an_ops),
m_client(a_client), m_client(a_client),
m_should_be_removed(true) m_should_be_removed(true)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~WebConnection * @fn ~WebConnection
* *
@@ -51,7 +48,6 @@ WebConnection::~WebConnection (void)
// printf("WebConnection:: desctructor....\n"); // printf("WebConnection:: desctructor....\n");
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn add_data * @fn add_data
* *
@@ -64,7 +60,6 @@ int WebConnection::add_data (const char *a_data, int a_len)
return a_len; return a_len;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_client * @fn get_client
* *
@@ -75,7 +70,6 @@ struct client * WebConnection::get_client (void)
return m_client; return m_client;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn invoke * @fn invoke
* *
@@ -85,7 +79,6 @@ void WebConnection::invoke (struct ubus_context *a_ctx)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn should_be_removed * @fn should_be_removed
* *

View File

@@ -39,11 +39,10 @@ struct ubus_context;
#define kEvent_version_entry "version" #define kEvent_version_entry "version"
#define kEvent_content_entry "content" #define kEvent_content_entry "content"
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class WebConnection { class WebConnection
{
public: public:
WebConnection(struct uhttpd_ops *an_ops, struct client *a_client); WebConnection(struct uhttpd_ops *an_ops, struct client *a_client);
virtual ~WebConnection(void); virtual ~WebConnection(void);

View File

@@ -31,12 +31,10 @@
* *
* @brief constructor of the web controller object. * @brief constructor of the web controller object.
*/ */
WebController::WebController (const std::string &a_path) : WebController::WebController(const std::string &a_path) : m_path(a_path)
m_path(a_path)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~WebController * @fn ~WebController
* *
@@ -46,7 +44,6 @@ WebController::~WebController (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set_name * @fn set_name
* *
@@ -57,7 +54,6 @@ void WebController::set_name (const std::string &a_name)
m_name = a_name; m_name = a_name;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_name * @fn get_name
* *
@@ -68,7 +64,6 @@ const std::string &WebController::get_name (void)
return m_name; return m_name;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_path * @fn get_path
* *
@@ -79,7 +74,6 @@ std::string WebController::get_path (void)
return m_path; return m_path;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn new_connection * @fn new_connection
* *

View File

@@ -37,8 +37,8 @@ struct client;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class WebController { class WebController
{
public: public:
WebController(const std::string &a_path); WebController(const std::string &a_path);
virtual ~WebController(void); virtual ~WebController(void);
@@ -55,5 +55,4 @@ private:
std::string m_path; std::string m_path;
}; };
#endif /* _WEB_CONTROLLER_H */ #endif /* _WEB_CONTROLLER_H */

View File

@@ -40,7 +40,6 @@
#include <uhttpd/uhttpd.h> #include <uhttpd/uhttpd.h>
#include <uhttpd/plugin.h> #include <uhttpd/plugin.h>
#define kdefaultTimeout 30000 #define kdefaultTimeout 30000
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
@@ -48,8 +47,7 @@
* *
* @brief constructor of the etag rest connection object. * @brief constructor of the etag rest connection object.
*/ */
EtagRestConnection::EtagRestConnection (struct uhttpd_ops *an_ops, struct client *a_client, EtagRestController *a_controller, const std::string &a_parameters) : EtagRestConnection::EtagRestConnection(struct uhttpd_ops *an_ops, struct client *a_client, EtagRestController *a_controller, const std::string &a_parameters) : WebConnection(an_ops, a_client),
WebConnection(an_ops, a_client),
m_controller(a_controller), m_controller(a_controller),
m_http_header(kdefaultTimeout), m_http_header(kdefaultTimeout),
m_ctx(NULL), m_ctx(NULL),
@@ -59,7 +57,6 @@ EtagRestConnection::EtagRestConnection (struct uhttpd_ops *an_ops, struct client
// printf("%s\n", __PRETTY_FUNCTION__); // printf("%s\n", __PRETTY_FUNCTION__);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~EtagRestConnection * @fn ~EtagRestConnection
* *
@@ -71,8 +68,8 @@ EtagRestConnection::~EtagRestConnection (void)
// Stop Timers. // Stop Timers.
stop(); stop();
// Stop receive async events. // Stop receive async events.
if (m_ctx != NULL) { if (m_ctx != NULL)
{
unregister_event(m_ctx); unregister_event(m_ctx);
abort(m_ctx); abort(m_ctx);
} }
@@ -81,7 +78,6 @@ EtagRestConnection::~EtagRestConnection (void)
uh_client_unref(m_client); uh_client_unref(m_client);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn invoke * @fn invoke
* *
@@ -100,19 +96,19 @@ void EtagRestConnection::invoke (struct ubus_context *a_ctx)
parse_parameter(); parse_parameter();
// printf("%s (object: %s)....\n", __PRETTY_FUNCTION__, m_controller->get_path().c_str()); // printf("%s (object: %s)....\n", __PRETTY_FUNCTION__, m_controller->get_path().c_str());
if (!ubus_lookup_id(a_ctx, m_controller->get_path().c_str(), &the_id)) { if (!ubus_lookup_id(a_ctx, m_controller->get_path().c_str(), &the_id))
{
// printf ("%s launch async call (%s)(%s)....\n", __PRETTY_FUNCTION__, the_method.c_str(), m_parameters.c_str()); // printf ("%s launch async call (%s)(%s)....\n", __PRETTY_FUNCTION__, the_method.c_str(), m_parameters.c_str());
the_cmd.exec_async(a_ctx, the_id, the_method, m_parameters, this); the_cmd.exec_async(a_ctx, the_id, the_method, m_parameters, this);
} }
if (m_client->request.method == UH_HTTP_MSG_GET) { if (m_client->request.method == UH_HTTP_MSG_GET)
{
// printf("start timer (%d)....\n", m_wait_response_timeout); // printf("start timer (%d)....\n", m_wait_response_timeout);
start(m_wait_response_timeout, false); start(m_wait_response_timeout, false);
} }
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn complete * @fn complete
* *
@@ -124,7 +120,6 @@ void EtagRestConnection::complete (void)
manage_response(get_data()); manage_response(get_data());
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn handle_event * @fn handle_event
* *
@@ -136,7 +131,6 @@ void EtagRestConnection::handle_event (const char *a_type, const char *a_json_ms
manage_response(a_json_msg); manage_response(a_json_msg);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn parse_parameter * @fn parse_parameter
* *
@@ -150,8 +144,8 @@ int EtagRestConnection::parse_parameter (void)
int32_t the_wait_response_timeout = -1; int32_t the_wait_response_timeout = -1;
struct json_object *the_response_node; struct json_object *the_response_node;
if (the_http_params.parse(m_client)) { if (the_http_params.parse(m_client))
{
the_etag = the_http_params.get_etag(); the_etag = the_http_params.get_etag();
the_wait_response_timeout = the_http_params.get_request_timeout(); the_wait_response_timeout = the_http_params.get_request_timeout();
the_origin = the_http_params.get_origin(); the_origin = the_http_params.get_origin();
@@ -159,16 +153,16 @@ int EtagRestConnection::parse_parameter (void)
m_http_header.add_origin(the_origin); m_http_header.add_origin(the_origin);
} }
// printf("EtagRestConnection::parse_parameter timeout: %d\n", the_wait_response_timeout); // printf("EtagRestConnection::parse_parameter timeout: %d\n", the_wait_response_timeout);
if (the_wait_response_timeout != -1) { if (the_wait_response_timeout != -1)
{
// Convert it as milli seconds. // Convert it as milli seconds.
m_wait_response_timeout = the_wait_response_timeout * 1000; m_wait_response_timeout = the_wait_response_timeout * 1000;
} }
// printf("%s - etag: %s, keep alive: %d\n", __PRETTY_FUNCTION__, the_etag.c_str(), the_wait_response_timeout); // printf("%s - etag: %s, keep alive: %d\n", __PRETTY_FUNCTION__, the_etag.c_str(), the_wait_response_timeout);
if (m_client->request.method == UH_HTTP_MSG_GET) { if (m_client->request.method == UH_HTTP_MSG_GET)
{
the_response_node = json_object_new_object(); the_response_node = json_object_new_object();
if (!the_etag.empty()) if (!the_etag.empty())
@@ -177,15 +171,16 @@ int EtagRestConnection::parse_parameter (void)
m_parameters = json_object_to_json_string(the_response_node); m_parameters = json_object_to_json_string(the_response_node);
json_object_put(the_response_node); json_object_put(the_response_node);
} }
else { else
{
struct json_object *the_content_node, *the_root_node; struct json_object *the_content_node, *the_root_node;
if (!m_connection_data.empty()) { if (!m_connection_data.empty())
{
the_root_node = json_object_new_object(); the_root_node = json_object_new_object();
the_content_node = json_tokener_parse(m_connection_data.c_str()); the_content_node = json_tokener_parse(m_connection_data.c_str());
if (the_content_node != NULL) { if (the_content_node != NULL)
{
json_object_object_add(the_root_node, kEvent_content_entry, the_content_node); json_object_object_add(the_root_node, kEvent_content_entry, the_content_node);
} }
@@ -197,7 +192,6 @@ int EtagRestConnection::parse_parameter (void)
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn manage_response * @fn manage_response
* *
@@ -213,8 +207,8 @@ void EtagRestConnection::manage_response (std::string a_response_data)
// printf("\tdata: (%s)\n", a_response_data.c_str()); // printf("\tdata: (%s)\n", a_response_data.c_str());
the_root_node = json_tokener_parse(a_response_data.c_str()); the_root_node = json_tokener_parse(a_response_data.c_str());
if (the_root_node == NULL) { if (the_root_node == NULL)
{
// response couldn't be parsed. // response couldn't be parsed.
m_http_header.send(m_ops, m_client, 500); m_http_header.send(m_ops, m_client, 500);
m_ops->chunk_printf(m_client, "0\r\n\r\n"); m_ops->chunk_printf(m_client, "0\r\n\r\n");
@@ -223,24 +217,25 @@ void EtagRestConnection::manage_response (std::string a_response_data)
} }
// get status // get status
if (json_object_object_get_ex(the_root_node, kEvent_status_entry, &the_status_node)) { if (json_object_object_get_ex(the_root_node, kEvent_status_entry, &the_status_node))
{
the_result_code = json_object_get_int(the_status_node); the_result_code = json_object_get_int(the_status_node);
} }
// get etag. // get etag.
if (json_object_object_get_ex(the_root_node, kEvent_version_entry, &the_etag_node)) { if (json_object_object_get_ex(the_root_node, kEvent_version_entry, &the_etag_node))
{
enum json_type the_json_type; enum json_type the_json_type;
the_json_type = json_object_get_type(the_etag_node); the_json_type = json_object_get_type(the_etag_node);
if (the_json_type == json_type_int) { if (the_json_type == json_type_int)
{
int the_tmp_value; int the_tmp_value;
the_tmp_value = json_object_get_int(the_etag_node); the_tmp_value = json_object_get_int(the_etag_node);
the_etag = std::to_string(the_tmp_value); the_etag = std::to_string(the_tmp_value);
} }
else if (the_json_type == json_type_string) { else if (the_json_type == json_type_string)
{
the_etag = json_object_get_string(the_etag_node); the_etag = json_object_get_string(the_etag_node);
} }
@@ -249,13 +244,13 @@ void EtagRestConnection::manage_response (std::string a_response_data)
// get contents // get contents
the_content_node = NULL; the_content_node = NULL;
if (!json_object_object_get_ex(the_root_node, kEvent_content_entry, &the_content_node)) { if (!json_object_object_get_ex(the_root_node, kEvent_content_entry, &the_content_node))
{
the_content_node = the_root_node; the_content_node = the_root_node;
} }
if (the_result_code == 204) { if (the_result_code == 204)
{
json_object_put(the_root_node); json_object_put(the_root_node);
register_event(m_ctx, m_controller->get_event_name()); register_event(m_ctx, m_controller->get_event_name());
return; return;
@@ -273,7 +268,6 @@ void EtagRestConnection::manage_response (std::string a_response_data)
m_ops->request_done(m_client); m_ops->request_done(m_client);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn expire * @fn expire
* *

View File

@@ -36,13 +36,13 @@
#include "core/web-connection.h" #include "core/web-connection.h"
class EtagRestController; class EtagRestController;
struct ubus_context; struct ubus_context;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class EtagRestConnection : public WebConnection, public UBusExecReceiver, public UBusEventReceiver, public ULoopTimer { class EtagRestConnection : public WebConnection, public UBusExecReceiver, public UBusEventReceiver, public ULoopTimer
{
public: public:
EtagRestConnection(struct uhttpd_ops *an_ops, struct client *a_client, EtagRestController *a_controller, const std::string &a_parameters); EtagRestConnection(struct uhttpd_ops *an_ops, struct client *a_client, EtagRestController *a_controller, const std::string &a_parameters);

View File

@@ -22,26 +22,22 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include "etag-rest/etag-rest-connection.h" #include "etag-rest/etag-rest-connection.h"
#include "etag-rest/etag-rest-controller.h" #include "etag-rest/etag-rest-controller.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn EtagRestController * @fn EtagRestController
* *
* @brief constructor of the etag rest controller object. * @brief constructor of the etag rest controller object.
*/ */
EtagRestController::EtagRestController(const std::string &a_path, const std::string &an_event_name, const std::string &an_etag_key, EtagRestController::EtagRestController(const std::string &a_path, const std::string &an_event_name, const std::string &an_etag_key,
const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : RestController(a_path, a_method_get, a_method_put, a_timeout, a_raw_response),
RestController(a_path, a_method_get, a_method_put, a_timeout, a_raw_response),
m_event_name(an_event_name), m_event_name(an_event_name),
m_etag_key(an_etag_key) m_etag_key(an_etag_key)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~EtagRestController * @fn ~EtagRestController
* *
@@ -51,7 +47,6 @@ EtagRestController::~EtagRestController (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn new_connection * @fn new_connection
* *
@@ -60,11 +55,9 @@ EtagRestController::~EtagRestController (void)
WebConnection *EtagRestController::new_connection(struct uhttpd_ops *an_ops, struct client *a_client, const std::string &a_parameters) WebConnection *EtagRestController::new_connection(struct uhttpd_ops *an_ops, struct client *a_client, const std::string &a_parameters)
{ {
// printf("%s.\n", __PRETTY_FUNCTION__); // printf("%s.\n", __PRETTY_FUNCTION__);
return new EtagRestConnection(an_ops, a_client, this, a_parameters); return new EtagRestConnection(an_ops, a_client, this, a_parameters);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_event_name * @fn get_event_name
* *
@@ -75,7 +68,6 @@ std::string EtagRestController::get_event_name (void)
return m_event_name; return m_event_name;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_etag_key * @fn get_etag_key
* *

View File

@@ -40,8 +40,8 @@
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class EtagRestController : public RestController { class EtagRestController : public RestController
{
public: public:
EtagRestController(const std::string &a_path, const std::string &an_event_name, const std::string &an_etag_key, EtagRestController(const std::string &a_path, const std::string &an_event_name, const std::string &an_etag_key,
const std::string &a_method_get = "", const std::string &a_method_put = "", const std::string &a_method_get = "", const std::string &a_method_put = "",
@@ -58,5 +58,4 @@ private:
std::string m_etag_key; std::string m_etag_key;
}; };
#endif /* _ETAG_REST_CONTROLLER_H */ #endif /* _ETAG_REST_CONTROLLER_H */

View File

@@ -36,21 +36,18 @@ extern "C" {
#define kdefaultTimeout 20000 #define kdefaultTimeout 20000
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn NotificationConnection * @fn NotificationConnection
* *
* @brief constructor of the notification connection object. * @brief constructor of the notification connection object.
*/ */
NotificationConnection::NotificationConnection (struct uhttpd_ops *an_ops, struct client *a_client, NotificationController *a_controller) : NotificationConnection::NotificationConnection(struct uhttpd_ops *an_ops, struct client *a_client, NotificationController *a_controller) : WebConnection(an_ops, a_client),
WebConnection(an_ops, a_client),
m_controller(a_controller), m_controller(a_controller),
m_ctx(NULL) m_ctx(NULL)
{ {
// printf("constructor NotificationConnection : %p\n", this); // printf("constructor NotificationConnection : %p\n", this);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~NotificationConnection * @fn ~NotificationConnection
* *
@@ -60,12 +57,12 @@ NotificationConnection::~NotificationConnection (void)
{ {
//printf("destructor NotificationConnection : %p\n", this); //printf("destructor NotificationConnection : %p\n", this);
stop(); stop();
if (m_ctx != NULL) { if (m_ctx != NULL)
{
unregister_event(m_ctx); unregister_event(m_ctx);
} }
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn invoke * @fn invoke
* *
@@ -82,7 +79,6 @@ void NotificationConnection::invoke (struct ubus_context *a_ctx)
start(kdefaultTimeout, true); start(kdefaultTimeout, true);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn handle_event * @fn handle_event
* *
@@ -96,7 +92,6 @@ void NotificationConnection::handle_event (const char *a_type, const char *a_jso
start(kdefaultTimeout, true); start(kdefaultTimeout, true);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn expire * @fn expire
* *

View File

@@ -37,8 +37,8 @@ struct ubus_context;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class NotificationConnection : public WebConnection, public UBusEventReceiver, public ULoopTimer { class NotificationConnection : public WebConnection, public UBusEventReceiver, public ULoopTimer
{
public: public:
NotificationConnection(struct uhttpd_ops *an_ops, struct client *a_client, NotificationController *a_controller); NotificationConnection(struct uhttpd_ops *an_ops, struct client *a_client, NotificationController *a_controller);
virtual ~NotificationConnection(void); virtual ~NotificationConnection(void);

View File

@@ -31,12 +31,10 @@
* *
* @brief constructor of the notification controller object. * @brief constructor of the notification controller object.
*/ */
NotificationController::NotificationController (const std::string &a_path) : NotificationController::NotificationController(const std::string &a_path) : WebController(a_path)
WebController(a_path)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~NotificationController * @fn ~NotificationController
* *
@@ -46,7 +44,6 @@ NotificationController::~NotificationController (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn new_connection * @fn new_connection
* *
@@ -55,6 +52,5 @@ NotificationController::~NotificationController (void)
WebConnection *NotificationController::new_connection(struct uhttpd_ops *an_ops, struct client *a_client, const std::string &a_parameters) WebConnection *NotificationController::new_connection(struct uhttpd_ops *an_ops, struct client *a_client, const std::string &a_parameters)
{ {
// printf("%s.\n", __PRETTY_FUNCTION__); // printf("%s.\n", __PRETTY_FUNCTION__);
return new NotificationConnection(an_ops, a_client, this); return new NotificationConnection(an_ops, a_client, this);
} }

View File

@@ -32,7 +32,8 @@
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class NotificationController : public WebController { class NotificationController : public WebController
{
public: public:
NotificationController(const std::string &a_path); NotificationController(const std::string &a_path);
~NotificationController(void); ~NotificationController(void);

View File

@@ -34,7 +34,6 @@ extern int uhttp_server_setup (struct ubus_context *a_ctx, const struct uhttpd_o
extern bool uhttp_server_check_url(const char *an_url); extern bool uhttp_server_check_url(const char *an_url);
extern void uhttp_server_handle_request(struct client *a_cl, char *an_url, struct path_info *a_pi); extern void uhttp_server_handle_request(struct client *a_cl, char *an_url, struct path_info *a_pi);
bool uhttp_server_check_path(struct path_info *pi, const char *url) bool uhttp_server_check_path(struct path_info *pi, const char *url)
{ {
// printf ("uhttp_server_check_path: url: %s\n", url); // printf ("uhttp_server_check_path: url: %s\n", url);
@@ -49,7 +48,6 @@ static struct dispatch_handler g_ubus_dispatch = {
.handle_request = uhttp_server_handle_request, .handle_request = uhttp_server_handle_request,
}; };
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uh_awox_api_plugin_init * @fn uh_awox_api_plugin_init
* *
@@ -60,8 +58,8 @@ static int uh_awox_api_plugin_init (const struct uhttpd_ops *an_ops, struct conf
struct ubus_context *the_ctx; struct ubus_context *the_ctx;
the_ctx = ubus_connect(NULL); the_ctx = ubus_connect(NULL);
if (!the_ctx) { if (!the_ctx)
{
fprintf(stderr, "Unable to connect to ubus socket\n"); fprintf(stderr, "Unable to connect to ubus socket\n");
return -1; return -1;
} }
@@ -75,7 +73,6 @@ static int uh_awox_api_plugin_init (const struct uhttpd_ops *an_ops, struct conf
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uh_awox_api_plugin_post_init * @fn uh_awox_api_plugin_post_init
* *

View File

@@ -43,25 +43,22 @@ extern "C" {
#include "rest/rest-connection.h" #include "rest/rest-connection.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn RestConnection * @fn RestConnection
* *
* @brief constructor of the rest connection object. * @brief constructor of the rest connection object.
*/ */
RestConnection::RestConnection (struct uhttpd_ops *an_ops, struct client *a_client, RestController *a_controller, const std::string &a_parameters) : RestConnection::RestConnection(struct uhttpd_ops *an_ops, struct client *a_client, RestController *a_controller, const std::string &a_parameters) : WebConnection(an_ops, a_client),
WebConnection(an_ops, a_client),
m_controller(a_controller), m_ctx(0) m_controller(a_controller), m_ctx(0)
{ {
if (!a_parameters.empty()) { if (!a_parameters.empty())
{
HttpParameter the_http_parm; HttpParameter the_http_parm;
m_connection_data = the_http_parm.parse_from_uri(a_parameters); m_connection_data = the_http_parm.parse_from_uri(a_parameters);
} }
// printf("RestConnection:: constructor....\n"); // printf("RestConnection:: constructor....\n");
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~RestConnection * @fn ~RestConnection
* *
@@ -72,7 +69,6 @@ RestConnection::~RestConnection (void)
abort(m_ctx); abort(m_ctx);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn complete * @fn complete
* *
@@ -88,7 +84,6 @@ void RestConnection::complete (void)
send_response(the_result_code, get_data().c_str()); send_response(the_result_code, get_data().c_str());
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn invoke * @fn invoke
* *
@@ -105,7 +100,8 @@ void RestConnection::invoke (struct ubus_context *a_ctx)
the_method = m_controller->get_method(m_client->request.method); the_method = m_controller->get_method(m_client->request.method);
// printf("RestConnection::invoke(object: %s)....\n", m_controller->get_path().c_str()); // printf("RestConnection::invoke(object: %s)....\n", m_controller->get_path().c_str());
if (!ubus_lookup_id(a_ctx, m_controller->get_path().c_str(), &the_id)) { if (!ubus_lookup_id(a_ctx, m_controller->get_path().c_str(), &the_id))
{
json_object *the_parameter_doc; json_object *the_parameter_doc;
the_parameter_doc = json_tokener_parse(m_connection_data.c_str()); the_parameter_doc = json_tokener_parse(m_connection_data.c_str());
@@ -139,7 +135,8 @@ json_object* RestConnection::parse_form_encoded_data ()
the_parsed_document = json_object_new_object(); the_parsed_document = json_object_new_object();
for (the_encoded_name = strtok(the_parameters, "="); the_encoded_name != NULL; the_encoded_name = strtok(NULL, "=")) { for (the_encoded_name = strtok(the_parameters, "="); the_encoded_name != NULL; the_encoded_name = strtok(NULL, "="))
{
std::string the_name, the_value; std::string the_name, the_value;
the_encoded_value = strtok(NULL, "&"); the_encoded_value = strtok(NULL, "&");
@@ -169,8 +166,8 @@ void RestConnection::send_response(int a_result_code, std::string a_content_docu
struct json_object *the_root_node; struct json_object *the_root_node;
HttpHeader the_http_header; HttpHeader the_http_header;
if (m_controller->is_raw_response()) { if (m_controller->is_raw_response())
{
the_http_header.send(m_ops, m_client, a_result_code); the_http_header.send(m_ops, m_client, a_result_code);
m_ops->chunk_printf(m_client, "%s", get_data().c_str()); m_ops->chunk_printf(m_client, "%s", get_data().c_str());
m_ops->request_done(m_client); m_ops->request_done(m_client);
@@ -182,10 +179,12 @@ void RestConnection::send_response(int a_result_code, std::string a_content_docu
else else
the_status = kFailed; the_status = kFailed;
if (a_content_document.empty()) { if (a_content_document.empty())
{
the_root_node = json_object_new_object(); the_root_node = json_object_new_object();
} }
else { else
{
the_root_node = json_tokener_parse(get_data().c_str()); the_root_node = json_tokener_parse(get_data().c_str());
} }

View File

@@ -39,8 +39,8 @@ struct json_object;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class RestConnection : public WebConnection, public UBusExecReceiver { class RestConnection : public WebConnection, public UBusExecReceiver
{
public: public:
RestConnection(struct uhttpd_ops *an_ops, struct client *a_client, RestController *a_controller, const std::string &a_parameters); RestConnection(struct uhttpd_ops *an_ops, struct client *a_client, RestController *a_controller, const std::string &a_parameters);
~RestConnection(void); ~RestConnection(void);

View File

@@ -33,8 +33,7 @@
* *
* @brief constructor of the rest controller object. * @brief constructor of the rest controller object.
*/ */
RestController::RestController (const std::string &a_path, const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : RestController::RestController(const std::string &a_path, const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : WebController(a_path),
WebController(a_path),
m_method_get(a_method_get), m_method_get(a_method_get),
m_method_put(a_method_put), m_method_put(a_method_put),
m_timeout(a_timeout), m_timeout(a_timeout),
@@ -42,7 +41,6 @@ RestController::RestController (const std::string &a_path, const std::string &a_
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~RestController * @fn ~RestController
* *
@@ -52,7 +50,6 @@ RestController::~RestController (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn new_connection * @fn new_connection
* *
@@ -63,7 +60,6 @@ WebConnection *RestController::new_connection (struct uhttpd_ops *an_ops, struct
return new RestConnection(an_ops, a_client, this, a_parameters); return new RestConnection(an_ops, a_client, this, a_parameters);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_method_get * @fn get_method_get
* *
@@ -74,7 +70,6 @@ std::string RestController::get_method_get (void)
return m_method_get; return m_method_get;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_method_put * @fn get_method_put
* *
@@ -85,7 +80,6 @@ std::string RestController::get_method_put (void)
return m_method_put; return m_method_put;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_timeout * @fn get_timeout
* *
@@ -96,7 +90,6 @@ uint16_t RestController::get_timeout (void)
return m_timeout; return m_timeout;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn is_raw_response * @fn is_raw_response
* *
@@ -107,7 +100,6 @@ bool RestController::is_raw_response (void)
return mf_raw_response; return mf_raw_response;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_method * @fn get_method
* *
@@ -115,8 +107,8 @@ bool RestController::is_raw_response (void)
*/ */
std::string RestController::get_method(uint8_t a_method) std::string RestController::get_method(uint8_t a_method)
{ {
switch (a_method) { switch (a_method)
{
case UH_HTTP_MSG_GET: case UH_HTTP_MSG_GET:
return m_method_get; return m_method_get;
break; break;

View File

@@ -38,11 +38,10 @@
/*----------------------------- Dependencies --------------------------------*/ /*----------------------------- Dependencies --------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class RestController : public WebController { class RestController : public WebController
{
public: public:
RestController(const std::string &a_path, const std::string &a_method_get = "", const std::string &a_method_put = "", int a_timeout = kDefaultTimeout, bool a_raw_response = false); RestController(const std::string &a_path, const std::string &a_method_get = "", const std::string &a_method_put = "", int a_timeout = kDefaultTimeout, bool a_raw_response = false);
virtual ~RestController(void); virtual ~RestController(void);
@@ -65,5 +64,4 @@ protected:
bool mf_raw_response; bool mf_raw_response;
}; };
#endif /* _REST_CONTROLLER_H */ #endif /* _REST_CONTROLLER_H */

View File

@@ -20,7 +20,6 @@
* @Date: 16/06/2017 * @Date: 16/06/2017
*/ */
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <string> #include <string>
@@ -58,8 +57,6 @@ extern "C" {
#include <uhttpd/uhttpd.h> #include <uhttpd/uhttpd.h>
#include <uhttpd/plugin.h> #include <uhttpd/plugin.h>
/*------------------------------- GLOBALS ----------------------------------*/ /*------------------------------- GLOBALS ----------------------------------*/
#define kConfigDirectory "/usr/local/configs/restd/" #define kConfigDirectory "/usr/local/configs/restd/"
@@ -80,14 +77,12 @@ extern "C" {
#define kdefaultGetMethod "get" #define kdefaultGetMethod "get"
#define kdefaultSetMethod "set" #define kdefaultSetMethod "set"
#define kJsonControlerNotFound "{{\n" \ #define kJsonControlerNotFound "{{\n" \
" \"id\": \"{}\",\n" \ " \"id\": \"{}\",\n" \
" \"status\": \"failed\",\n" \ " \"status\": \"failed\",\n" \
" \"error\": \"Controller Not Found\",\n" \ " \"error\": \"Controller Not Found\",\n" \
" \"response_code\": 404\n}}" " \"response_code\": 404\n}}"
#define DEBUG_REQUEST 1 #define DEBUG_REQUEST 1
#define BUFFER_SIZE 100 #define BUFFER_SIZE 100
@@ -131,7 +126,6 @@ double time_diff(struct timeval x , struct timeval y)
extern "C" struct ubus_context *get_uhttp_server_ctx(void) extern "C" struct ubus_context *get_uhttp_server_ctx(void)
{ {
return UhttpServer::get_instance().get_context(); return UhttpServer::get_instance().get_context();
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
@@ -144,7 +138,6 @@ extern "C" int uhttp_server_setup (struct ubus_context *a_ctx, const struct uhtt
return UhttpServer::get_instance().setup(a_ctx, an_ops); return UhttpServer::get_instance().setup(a_ctx, an_ops);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uhttp_server_check_url * @fn uhttp_server_check_url
* *
@@ -155,7 +148,6 @@ extern "C" bool uhttp_server_check_url (const char *an_url)
return UhttpServer::get_instance().check_url(an_url); return UhttpServer::get_instance().check_url(an_url);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uhttp_server_handle_request * @fn uhttp_server_handle_request
* *
@@ -172,7 +164,6 @@ extern "C" void uhttp_server_handle_request (struct client *a_cl, char *an_url,
UhttpServer::get_instance().handle_request(a_cl, an_url, a_pi); UhttpServer::get_instance().handle_request(a_cl, an_url, a_pi);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uhttp_server_data_send * @fn uhttp_server_data_send
* *
@@ -186,7 +177,6 @@ static int uhttp_server_data_send (struct client *a_cl, const char *a_data, int
return UhttpServer::get_instance().data_send(a_cl, a_data, a_len); return UhttpServer::get_instance().data_send(a_cl, a_data, a_len);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uhttp_server_data_done * @fn uhttp_server_data_done
* *
@@ -197,7 +187,6 @@ static void uhttp_server_data_done (struct client *a_cl)
UhttpServer::get_instance().invoke_request(a_cl); UhttpServer::get_instance().invoke_request(a_cl);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uhttp_server_request_free * @fn uhttp_server_request_free
* *
@@ -215,7 +204,6 @@ static void uhttp_server_request_free (struct client *a_cl)
UhttpServer::get_instance().request_free(a_cl); UhttpServer::get_instance().request_free(a_cl);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn uhttp_server_close_fds * @fn uhttp_server_close_fds
* *
@@ -226,7 +214,6 @@ static void uhttp_server_close_fds (struct client *cl)
// printf("uhttp_server_close_fds(%p)... client id: %d\n", cl, cl->id); // printf("uhttp_server_close_fds(%p)... client id: %d\n", cl, cl->id);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_instance * @fn get_instance
* *
@@ -238,19 +225,16 @@ UhttpServer &UhttpServer::get_instance (void)
return m_ptr; return m_ptr;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn UhttpServer * @fn UhttpServer
* *
* @brief Constructor of the Uhttp Server * @brief Constructor of the Uhttp Server
*/ */
UhttpServer::UhttpServer (void): UhttpServer::UhttpServer(void) : m_ctx(NULL),
m_ctx(NULL),
m_ops(NULL) m_ops(NULL)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~UhttpServer * @fn ~UhttpServer
* *
@@ -260,20 +244,19 @@ UhttpServer::~UhttpServer (void)
{ {
ControllerIterator it; ControllerIterator it;
ConnectionIterator the_cnx_it; ConnectionIterator the_cnx_it;
for (it=m_controller_array.begin(); it!=m_controller_array.end(); ++it) { for (it = m_controller_array.begin(); it != m_controller_array.end(); ++it)
{
delete it->second; delete it->second;
} }
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;
} }
ubus_free(m_ctx); ubus_free(m_ctx);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn setup * @fn setup
* *
@@ -287,8 +270,6 @@ int UhttpServer::setup (struct ubus_context *a_ctx, const struct uhttpd_ops *an_
return load_config_dir(); return load_config_dir();
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_context * @fn get_context
* *
@@ -299,7 +280,6 @@ struct ubus_context *UhttpServer::get_context (void)
return m_ctx; return m_ctx;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn check_url * @fn check_url
* *
@@ -310,8 +290,8 @@ bool UhttpServer::check_url (const std::string &an_url)
std::list<std::string>::iterator the_it; std::list<std::string>::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_path_list.begin(); the_it != m_path_list.end(); the_it++)
{
if (an_url.find(*the_it) != std::string::npos) if (an_url.find(*the_it) != std::string::npos)
return true; return true;
} }
@@ -319,7 +299,6 @@ bool UhttpServer::check_url (const std::string &an_url)
return false; return false;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn handle_request * @fn handle_request
* *
@@ -334,19 +313,21 @@ void UhttpServer::handle_request (struct client *a_cl, const std::string &an_url
// 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);
if (the_pos != std::string::npos) { if (the_pos != std::string::npos)
{
// parameters are present. // parameters are present.
the_url = an_url.substr(0, the_pos); the_url = an_url.substr(0, the_pos);
the_parameters = an_url.substr(the_pos + 1); the_parameters = an_url.substr(the_pos + 1);
// printf("url: <%s> | parameters: <%s>\n", the_url.c_str(), the_parameters.c_str()); // printf("url: <%s> | parameters: <%s>\n", the_url.c_str(), the_parameters.c_str());
} }
else { else
{
the_url = an_url; the_url = an_url;
} }
the_controller = m_controller_array[the_url]; the_controller = m_controller_array[the_url];
if (the_controller == NULL) { if (the_controller == NULL)
{
std::string the_msg; std::string the_msg;
the_msg = fmt::format(kJsonControlerNotFound, the_url); the_msg = fmt::format(kJsonControlerNotFound, the_url);
send_error(a_cl, 404, "Not Found", the_msg); send_error(a_cl, 404, "Not Found", the_msg);
@@ -384,7 +365,6 @@ void UhttpServer::handle_request (struct client *a_cl, const std::string &an_url
// printf("uhttp_server_handle_request -done.\n"); // printf("uhttp_server_handle_request -done.\n");
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn invoke_request * @fn invoke_request
* *
@@ -399,7 +379,6 @@ void UhttpServer::invoke_request (struct client *a_client)
uh_client_unref(a_client); uh_client_unref(a_client);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn request_free * @fn request_free
* *
@@ -412,9 +391,10 @@ void UhttpServer::request_free (struct client *a_client)
uh_client_ref(a_client); uh_client_ref(a_client);
the_it = m_connections.find(a_client->id); the_it = m_connections.find(a_client->id);
if (the_it != m_connections.end()) { if (the_it != m_connections.end())
{
if ((the_it->second)->should_be_removed()) { if ((the_it->second)->should_be_removed())
{
delete the_it->second; delete the_it->second;
m_connections.erase(the_it); m_connections.erase(the_it);
} }
@@ -424,7 +404,6 @@ void UhttpServer::request_free (struct client *a_client)
// printf("UhttpServer::request_free-done\n"); // printf("UhttpServer::request_free-done\n");
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn data_send * @fn data_send
* *
@@ -435,7 +414,6 @@ int UhttpServer::data_send (struct client *a_client, const char *a_data, int a_l
return m_connections[a_client->id]->add_data(a_data, a_len); return m_connections[a_client->id]->add_data(a_data, a_len);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn load_config_dir * @fn load_config_dir
* *
@@ -449,17 +427,17 @@ int UhttpServer::load_config_dir (void)
struct json_object *the_root_node; struct json_object *the_root_node;
the_rep = opendir(kConfigDirectory); the_rep = opendir(kConfigDirectory);
while ((the_dir_ent = readdir(the_rep)) != NULL) { while ((the_dir_ent = readdir(the_rep)) != NULL)
{
if (strcmp(the_dir_ent->d_name, ".") != 0 && strcmp(the_dir_ent->d_name, "..") != 0) { if (strcmp(the_dir_ent->d_name, ".") != 0 && strcmp(the_dir_ent->d_name, "..") != 0)
{
// printf ("load: %s\n", the_dir_ent->d_name); // printf ("load: %s\n", the_dir_ent->d_name);
the_path = kConfigDirectory; the_path = kConfigDirectory;
the_path += the_dir_ent->d_name; the_path += the_dir_ent->d_name;
the_root_node = load(the_path); the_root_node = load(the_path);
if (the_root_node == NULL) { if (the_root_node == NULL)
{
fprintf(stderr, "Failed to load controller configuration file (%s).\n", the_path.c_str()); fprintf(stderr, "Failed to load controller configuration file (%s).\n", the_path.c_str());
return -1; return -1;
} }
@@ -474,7 +452,6 @@ int UhttpServer::load_config_dir (void)
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn add_controller_from_json * @fn add_controller_from_json
* *
@@ -489,15 +466,16 @@ int UhttpServer::add_controller_from_json (struct json_object *a_root_node)
int the_len; int the_len;
int i; 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, kControllerKey, &the_ctr_array_node))
{
the_len = json_object_array_length(the_ctr_array_node); the_len = json_object_array_length(the_ctr_array_node);
for (i = 0; i < the_len; i++) { for (i = 0; i < the_len; i++)
{
the_ctrl_node = json_object_array_get_idx(the_ctr_array_node, i); the_ctrl_node = json_object_array_get_idx(the_ctr_array_node, i);
if (get_controller_fields(the_ctrl_node, the_path, the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, the_raw_response)) { if (get_controller_fields(the_ctrl_node, the_path, the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, the_raw_response))
{
#if 0 #if 0
printf("Path: <%s>\n", the_path.c_str()); printf("Path: <%s>\n", the_path.c_str());
printf("\t- model name: <%s>\n", the_model_name.c_str()); printf("\t- model name: <%s>\n", the_model_name.c_str());
@@ -509,16 +487,16 @@ int UhttpServer::add_controller_from_json (struct json_object *a_root_node)
} }
} }
} }
if (json_object_object_get_ex(a_root_node, kEtagControllerKey, &the_etag_ctrl_array_node)) { if (json_object_object_get_ex(a_root_node, kEtagControllerKey, &the_etag_ctrl_array_node))
{
the_len = json_object_array_length(the_etag_ctrl_array_node); the_len = json_object_array_length(the_etag_ctrl_array_node);
for (i = 0; i < the_len; i++) { for (i = 0; i < the_len; i++)
{
the_ctrl_node = json_object_array_get_idx(the_etag_ctrl_array_node, i); the_ctrl_node = json_object_array_get_idx(the_etag_ctrl_array_node, i);
if (get_controller_fields(the_ctrl_node, the_path, the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, the_raw_response)) { if (get_controller_fields(the_ctrl_node, the_path, the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, the_raw_response))
{
EtagRestController *the_etag_rest_controller; EtagRestController *the_etag_rest_controller;
the_etag_rest_controller = new EtagRestController(the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, kDefaultTimeout, the_raw_response); the_etag_rest_controller = new EtagRestController(the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, kDefaultTimeout, the_raw_response);
@@ -526,16 +504,16 @@ int UhttpServer::add_controller_from_json (struct json_object *a_root_node)
} }
} }
} }
if (json_object_object_get_ex(a_root_node, kNotificationKey, &the_note_array_node)) { if (json_object_object_get_ex(a_root_node, kNotificationKey, &the_note_array_node))
{
the_len = json_object_array_length(the_note_array_node); the_len = json_object_array_length(the_note_array_node);
for (i = 0; i < the_len; i++) { for (i = 0; i < the_len; i++)
{
the_ctrl_node = json_object_array_get_idx(the_note_array_node, i); the_ctrl_node = json_object_array_get_idx(the_note_array_node, i);
if (get_controller_fields(the_ctrl_node, the_path, the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, the_raw_response)) { if (get_controller_fields(the_ctrl_node, the_path, the_model_name, the_event_name, the_etag_key, the_get_method, the_set_method, the_raw_response))
{
NotificationController *the_notification_controller; NotificationController *the_notification_controller;
the_notification_controller = new NotificationController(the_event_name); the_notification_controller = new NotificationController(the_event_name);
add_controller(the_path, the_notification_controller); add_controller(the_path, the_notification_controller);
@@ -546,7 +524,6 @@ int UhttpServer::add_controller_from_json (struct json_object *a_root_node)
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_controller_fields * @fn get_controller_fields
* *
@@ -561,60 +538,73 @@ bool UhttpServer::get_controller_fields (struct json_object *a_node, std::string
struct json_object *the_set_method_node, *the_raw_response_node; struct json_object *the_set_method_node, *the_raw_response_node;
// path // path
if (json_object_object_get_ex(a_node, kControllerPathKey, &the_path_node)) { if (json_object_object_get_ex(a_node, kControllerPathKey, &the_path_node))
{
a_path = json_object_get_string(the_path_node); a_path = json_object_get_string(the_path_node);
the_result = true; the_result = true;
} }
else { else
{
a_path.clear(); a_path.clear();
} }
// model_name // model_name
if (json_object_object_get_ex(a_node, kControllerModelNameKey, &the_ubus_model_node)) { if (json_object_object_get_ex(a_node, kControllerModelNameKey, &the_ubus_model_node))
{
a_model_name = json_object_get_string(the_ubus_model_node); a_model_name = json_object_get_string(the_ubus_model_node);
} }
else { else
{
a_model_name.clear(); a_model_name.clear();
} }
// event name // event name
if (json_object_object_get_ex(a_node, kControllerEventNameKey, &the_ubus_event_node)) { if (json_object_object_get_ex(a_node, kControllerEventNameKey, &the_ubus_event_node))
{
an_event_name = json_object_get_string(the_ubus_event_node); an_event_name = json_object_get_string(the_ubus_event_node);
} }
else { else
{
an_event_name.clear(); an_event_name.clear();
} }
// etag key // etag key
if (json_object_object_get_ex(a_node, kControllerEtagKey, &the_etag_key_node)) { if (json_object_object_get_ex(a_node, kControllerEtagKey, &the_etag_key_node))
{
an_etag_key = json_object_get_string(the_etag_key_node); an_etag_key = json_object_get_string(the_etag_key_node);
} }
else { else
{
an_etag_key.clear(); an_etag_key.clear();
} }
// get_method // get_method
if (json_object_object_get_ex(a_node, kControllerGetMethodKey, &the_get_method_node)) { if (json_object_object_get_ex(a_node, kControllerGetMethodKey, &the_get_method_node))
{
a_get_method = json_object_get_string(the_get_method_node); a_get_method = json_object_get_string(the_get_method_node);
} }
else { else
{
a_get_method = kdefaultGetMethod; a_get_method = kdefaultGetMethod;
} }
// set_method // set_method
if (json_object_object_get_ex(a_node, kControllerSetMethodKey, &the_set_method_node)) { if (json_object_object_get_ex(a_node, kControllerSetMethodKey, &the_set_method_node))
{
a_set_method = json_object_get_string(the_set_method_node); a_set_method = json_object_get_string(the_set_method_node);
} }
else { else
{
a_set_method = kdefaultSetMethod; a_set_method = kdefaultSetMethod;
} }
// raw_response // raw_response
if (json_object_object_get_ex(a_node, kControllerRawResponseKey, &the_raw_response_node)) { if (json_object_object_get_ex(a_node, kControllerRawResponseKey, &the_raw_response_node))
{
a_raw_response = json_object_get_boolean(the_raw_response_node); a_raw_response = json_object_get_boolean(the_raw_response_node);
} }
else { else
{
a_raw_response = false; a_raw_response = false;
} }
return the_result; return the_result;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn load * @fn load
* *
@@ -629,21 +619,21 @@ struct json_object *UhttpServer::load (const std::string &a_file)
char *the_addr; char *the_addr;
the_fd = open(a_file.c_str(), O_RDONLY); the_fd = open(a_file.c_str(), O_RDONLY);
if (the_fd == -1) { if (the_fd == -1)
{
perror("open"); perror("open");
return NULL; return NULL;
} }
if (fstat(the_fd, &the_stat) == -1) { if (fstat(the_fd, &the_stat) == -1)
{
perror("fstat"); perror("fstat");
close(the_fd); close(the_fd);
return NULL; return NULL;
} }
the_addr = (char *)mmap(NULL, the_stat.st_size, PROT_READ, MAP_PRIVATE, the_fd, 0); the_addr = (char *)mmap(NULL, the_stat.st_size, PROT_READ, MAP_PRIVATE, the_fd, 0);
if (the_addr == MAP_FAILED) { if (the_addr == MAP_FAILED)
{
perror("mmap"); perror("mmap");
close(the_fd); close(the_fd);
return NULL; return NULL;
@@ -656,7 +646,6 @@ struct json_object *UhttpServer::load (const std::string &a_file)
return the_root_node; return the_root_node;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn add_rest_controller * @fn add_rest_controller
* *
@@ -676,10 +665,11 @@ int UhttpServer::add_controller (const std::string &an_uri, WebController *a_con
// 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)
{
the_path = an_uri.substr(0, the_pos + 1); the_path = an_uri.substr(0, the_pos + 1);
if (std::find(m_path_list.begin(), m_path_list.end(), the_path) == m_path_list.end()) { if (std::find(m_path_list.begin(), m_path_list.end(), the_path) == m_path_list.end())
{
// uniq version. we add it. // uniq version. we add it.
m_path_list.push_back(the_path); m_path_list.push_back(the_path);
} }
@@ -688,7 +678,6 @@ int UhttpServer::add_controller (const std::string &an_uri, WebController *a_con
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn send_error * @fn send_error
* *

View File

@@ -31,7 +31,6 @@
/*--------------------------------- Define ----------------------------------*/ /*--------------------------------- Define ----------------------------------*/
/*----------------------------- Dependencies --------------------------------*/ /*----------------------------- Dependencies --------------------------------*/
struct ubus_context; struct ubus_context;
@@ -43,8 +42,8 @@ class WebConnection;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class UhttpServer { class UhttpServer
{
public: public:
typedef std::unordered_map<std::string, WebController *> ControllerContainer; typedef std::unordered_map<std::string, WebController *> ControllerContainer;
typedef ControllerContainer::iterator ControllerIterator; typedef ControllerContainer::iterator ControllerIterator;