diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index e427d1f7..1bd92d5d 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -33,7 +33,8 @@ "${workspaceRoot}/_builds/x86_64_domo-debug/cross/opt/ext-toolchain/x86_64-buildroot-linux-gnu/include/c++/5.4.0/x86_64-buildroot-linux-gnu", "${workspaceRoot}/_builds/x86_64_domo-debug/cross/opt/ext-toolchain/lib/gcc/x86_64-buildroot-linux-gnu/5.4.0/include", "${workspaceFolder}/src/lib/libubus-cpp/include", - "${workspaceFolder}/src/prog/domod/include" + "${workspaceFolder}/src/prog/domod/include", + "${workspaceFolder}/src/plugins/uhttpd/uhttpd-rest-api" ], "defines": [], "intelliSenseMode": "clang-x64", diff --git a/src/plugins/uhttpd/uhttpd-rest-api/etag-rest/etag-rest-connection.cpp b/src/plugins/uhttpd/uhttpd-rest-api/etag-rest/etag-rest-connection.cpp index 5e85b995..559c890a 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/etag-rest/etag-rest-connection.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/etag-rest/etag-rest-connection.cpp @@ -91,7 +91,7 @@ void EtagRestConnection::invoke(struct ubus_context *a_ctx) m_ctx = a_ctx; - the_method = m_controller->get_method(m_client->request.method); + //TODO the_method = m_controller->get_method(m_client->request.method); parse_parameter(); diff --git a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-connection.cpp b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-connection.cpp index 729aafa9..79b3f6d9 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-connection.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-connection.cpp @@ -93,14 +93,11 @@ void RestConnection::invoke(struct ubus_context *a_ctx) { uint32_t the_id = 0; UBusCall the_cmd; - std::string the_method; m_ctx = a_ctx; - the_method = m_controller->get_method(m_client->request.method); - // 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_ubus().get_path().c_str(), &the_id)) { json_object *the_parameter_doc; @@ -110,7 +107,8 @@ void RestConnection::invoke(struct ubus_context *a_ctx) the_parameter_doc = parse_form_encoded_data(); // printf("RestConnection::invoke launch async call (%d)....\n", the_id); - the_cmd.exec_async(a_ctx, the_id, the_method, json_object_to_json_string(the_parameter_doc), this); + the_cmd.exec_async(a_ctx, the_id, m_controller->get_ubus().get_method().c_str(), + json_object_to_json_string(the_parameter_doc), this); json_object_put(the_parameter_doc); } diff --git a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp index 8f671af5..00e47c1f 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp @@ -30,8 +30,7 @@ #include - -#define kControllerModelKey "model" +#define kControllerModelKey "model" #define kControllerEndpointKey "endpoint" #define kControllerUbusKey "ubus" @@ -52,7 +51,8 @@ RestController::RestController(void) * * @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) : WebController(a_path) +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), + mf_raw_response(false) #if 0 m_method_get(a_method_get), m_method_put(a_method_put), @@ -81,7 +81,6 @@ WebConnection *RestController::new_connection(struct uhttpd_ops *an_ops, struct return new RestConnection(an_ops, a_client, this, a_parameters); } - /*! ---------------------------------------------------------------------------- * @fn from_json * @@ -128,21 +127,18 @@ bool RestController::from_json(struct json_object *a_node) // path if (json_object_object_get_ex(the_ubus_node, kControllerPathKey, &the_value_node)) { - m_ubus.m_path = json_object_get_string(the_value_node); + m_ubus.set_path(json_object_get_string(the_value_node)); } //method if (json_object_object_get_ex(the_ubus_node, kControllerMethodKey, &the_value_node)) { - m_ubus.m_method = json_object_get_string(the_value_node); + m_ubus.set_method(json_object_get_string(the_value_node)); } - } return true; } - - /*! ---------------------------------------------------------------------------- * @fn find * @@ -150,10 +146,44 @@ bool RestController::from_json(struct json_object *a_node) */ bool RestController::find(const std::string &an_url) { - return true; + std::vector::iterator the_it; + + printf("find : %s\n", an_url.c_str()); + + for (the_it = m_endpoint.begin(); the_it != m_endpoint.end(); ++the_it) + { + 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 false; +} + +/*! ---------------------------------------------------------------------------- + * @fn get_ubus + * + * @brief return the ubus reference for this controller. + */ +const Ubus &RestController::get_ubus(void) const +{ + return m_ubus; } +/*! ---------------------------------------------------------------------------- + * @fn is_raw_response + * + * @brief return the true if the controller didn't need the api response template. + */ +bool RestController::is_raw_response(void) +{ + return mf_raw_response; +} + +#if 0 /*! ---------------------------------------------------------------------------- * @fn get_method_get * @@ -184,15 +214,6 @@ uint16_t RestController::get_timeout(void) return 0; //m_timeout; } -/*! ---------------------------------------------------------------------------- - * @fn is_raw_response - * - * @brief return the true if the controller didn't need the awox api response template. - */ -bool RestController::is_raw_response(void) -{ - return false; //mf_raw_response; -} /*! ---------------------------------------------------------------------------- * @fn get_method @@ -218,3 +239,4 @@ std::string RestController::get_method(uint8_t a_method) #endif return ""; } +#endif \ No newline at end of file diff --git a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h index 365ff15f..eeb0e567 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h +++ b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h @@ -58,18 +58,22 @@ class RestController : public WebController bool find(const std::string &an_url); + const Ubus &get_ubus(void) const; + bool is_raw_response(void); + +/* std::string get_method_get(void); std::string get_method_put(void); uint16_t get_timeout(void); - bool is_raw_response(void); std::string get_method(uint8_t a_method); - +*/ protected: uint8_t m_method; std::vector m_endpoint; Ubus m_ubus; + bool mf_raw_response; /* std::string m_name; @@ -77,7 +81,6 @@ class RestController : public WebController std::string m_method_get; std::string m_method_put; uint16_t m_timeout; - bool mf_raw_response; */ }; diff --git a/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.cpp b/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.cpp index 1f342c9b..0e98720d 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.cpp @@ -29,6 +29,46 @@ * * @brief constructor of the ubus object. */ - Ubus::Ubus(void) - { - } +Ubus::Ubus(void) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn get_path + * + * @brief return the path. + */ +const std::string &Ubus::get_path(void) const +{ + return m_path; +} + +/*! ---------------------------------------------------------------------------- + * @fn set_path + * + * @brief set the path. + */ +void Ubus::set_path(const char *a_path) +{ + m_path = a_path; +} + +/*! ---------------------------------------------------------------------------- + * @fn get_method + * + * @brief return the method + */ +const std::string &Ubus::get_method(void) const +{ + return m_method; +} + +/*! ---------------------------------------------------------------------------- + * @fn set_method + * + * @brief set the method + */ +void Ubus::set_method(const char *a_method) +{ + m_method = a_method; +} diff --git a/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.h b/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.h index 7f87b1bc..268a1d49 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.h +++ b/src/plugins/uhttpd/uhttpd-rest-api/rest/ubus.h @@ -34,6 +34,13 @@ class Ubus public: Ubus(void); + const std::string &get_path(void) const; + void set_path(const char *a_path); + + const std::string &get_method(void) const; + void set_method(const char *a_method); + + private: std::string m_path; std::string m_method; }; diff --git a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp index f3cd70c1..16181640 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp @@ -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::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 * diff --git a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h index 66631d22..9cccdf2b 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h +++ b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h @@ -75,6 +75,7 @@ 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); void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg); private: