diff --git a/lib/include/restd_http_handler.h b/lib/include/restd_http_handler.h index 6855ac7..e158f09 100644 --- a/lib/include/restd_http_handler.h +++ b/lib/include/restd_http_handler.h @@ -91,6 +91,8 @@ enum restd_http_request_status_e /*----------------------------------------------------------------------------*\ | PUBLIC FUNCTIONS | \*----------------------------------------------------------------------------*/ +extern restd_http_t *http_new(struct evbuffer *out); +extern void http_free_cb(restd_conn_t *conn, void *userdata); extern int restd_http_handler(short event, restd_conn_t *conn, void *userdata); extern enum restd_http_request_status_e restd_http_get_status(restd_conn_t *conn); diff --git a/lib/src/restd_http_handler.c b/lib/src/restd_http_handler.c index 33a11f6..7492fdc 100644 --- a/lib/src/restd_http_handler.c +++ b/lib/src/restd_http_handler.c @@ -41,9 +41,7 @@ #include "macro.h" #ifndef _DOXYGEN_SKIP -static restd_http_t *http_new(struct evbuffer *out); static void http_free(restd_http_t *http); -static void http_free_cb(restd_conn_t *conn, void *userdata); static size_t http_add_inbuf(struct evbuffer *buffer, restd_http_t *http, size_t maxsize); @@ -535,7 +533,7 @@ const char *restd_http_get_reason(int code) *****************************************************************************/ #ifndef _DOXYGEN_SKIP -static restd_http_t *http_new(struct evbuffer *out) +restd_http_t *http_new(struct evbuffer *out) { // Create a new connection container. restd_http_t *http = NEW_OBJECT(restd_http_t); @@ -599,7 +597,7 @@ static void http_free(restd_http_t *http) /*--------------------------------------------------------------------------*/ -static void http_free_cb(restd_conn_t *conn, void *userdata) +void http_free_cb(restd_conn_t *conn, void *userdata) { http_free((restd_http_t *)userdata); } diff --git a/lib/src/restd_rest_handler.c b/lib/src/restd_rest_handler.c index 9675ee3..5e347ba 100644 --- a/lib/src/restd_rest_handler.c +++ b/lib/src/restd_rest_handler.c @@ -28,6 +28,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include #include #include #include @@ -172,12 +173,45 @@ static bool contain(const char *src, const char *dest, int len) /*! ---------------------------------------------------------------------------- * @fn restd_rest_handler * - * @brief Main function to manage http request. + * @brief Main function to manage rest request. */ int restd_rest_handler(short event, restd_conn_t *conn, void *userdata) +{ + if (event & RESTD_EVENT_INIT) + { + DEBUG("==> HTTP INIT"); + restd_http_t *http = http_new(conn->out); + if (http == NULL) + return RESTD_CLOSE; + restd_conn_set_extra(conn, http, http_free_cb); + return RESTD_OK; + } + else if (event & RESTD_EVENT_READ) + { + DEBUG("==> HTTP READ"); + return RESTD_OK; + } + else if (event & RESTD_EVENT_WRITE) + { + DEBUG("==> HTTP WRITE"); + return RESTD_OK; + } + else if (event & RESTD_EVENT_CLOSE) + { + DEBUG("==> HTTP CLOSE=%x (TIMEOUT=%d, SHUTDOWN=%d)", + event, event & RESTD_EVENT_TIMEOUT, event & RESTD_EVENT_SHUTDOWN); + return RESTD_OK; + } + + BUG_EXIT(); + return RESTD_CLOSE; +} + + +int restd_rest_handler2(short event, restd_conn_t *conn, void *userdata) { int reason = RESTD_ERROR_PATH_NOT_FOUND; - DEBUG("********restd_rest_handler: event 0x%x", event); + DEBUG("ZOB********restd_rest_handler: event 0x%x", event); restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn); char *root_path; qlist_t *hooks = conn->server->hooks; @@ -189,9 +223,9 @@ int restd_rest_handler(short event, restd_conn_t *conn, void *userdata) restd_hook_t *hook = (restd_hook_t *)obj.data; if (hook->cb) { - // DEBUG("call_hooks: method: %s - %s \n", hook->method, conn->method); - // DEBUG("call_hooks: path: %s - %s\n", hook->path, http->request.path); - // DEBUG("HOOK FOUND !!!!\n"); + DEBUG("ZOB call_hooks: method: %s - %s \n", hook->method, conn->method); + DEBUG("call_hooks: path: %s - %s\n", hook->path, http->request.path); + DEBUG("HOOK FOUND !!!!\n"); if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0)) { // DEBUG("Hook found but method failed.\n"); diff --git a/src/rest/rest_devices_handlers.c b/src/rest/rest_devices_handlers.c index c027427..6c3cf00 100644 --- a/src/rest/rest_devices_handlers.c +++ b/src/rest/rest_devices_handlers.c @@ -27,53 +27,56 @@ #include -#include "devices/devices_manager.h" +#include +#include "devices/devices_manager.h" +#include "domo.h" #include "rest_devices_handlers.h" +// Code Description +// 204 Success. No content. +// 400 Bad request + /*--------------------------------------------------------------------------*/ int outlet_create_handler(short event, restd_conn_t *conn, void *userdata) { - if (event & RESTD_EVENT_INIT) + printf("outlet_create_handler\n\n"); + if (event & RESTD_EVENT_CLOSE) { - printf("==> RESTD_EVENT_INIT"); + off_t data_size; - // Attach to this connection. - //ad_conn_set_userdata(conn, cdata, my_userdata_free_cb); + data_size = restd_http_get_content_length(conn); + printf("1/data size: %ld\n", data_size); + if (data_size > 0) + { + char *data; + struct json_object *root_node = NULL; + + data = restd_http_get_content(conn, 0, &data_size); + printf("2/data size: %ld\n", data_size); + root_node = json_tokener_parse(data); + free(data); + + if (root_node != NULL) + { + int ret; + devices_manager_t *dm; + + ret = devices_manager_create(dm, kOutletEntry, root_node); + json_object_put(root_node); + if (ret == 0) + { + restd_http_response(conn, 204, "application/json", "{}", 2); + return RESTD_CLOSE; + } + } + } + + restd_http_response(conn, 400, "application/json", "{\"status\":\"error\"}", 18); + return RESTD_OK; } - else if (event & RESTD_EVENT_READ) - { - printf("==> RESTD_EVENT_READ"); - } - - else if (event & RESTD_EVENT_CLOSE) - { - long int taille; - char *data; - data = restd_http_get_content(conn, 0, &taille); - printf("******* : <%s>\n", data); - free(data); - } - -#if 0 - devices_manager_t *dm = (devices_manager_t *)userdata; - struct evbuffer *buffer; - - size_t size = 0; - void *data = restd_http_get_content(conn, 0, &size); - - printf(">>>>>>>>>>>>>>>>>>>> %ld\n", size); - - restd_http_response(conn, 200, "application/json", data, size); - - //restd_http_response(conn, 200, "application/json", "{\"status\":\"error\"}", 18); - - return RESTD_CLOSE; // Close connection. - -#endif - // Return AD_OK will let the hook loop to continue. return RESTD_OK; } @@ -81,7 +84,9 @@ int outlet_create_handler(short event, restd_conn_t *conn, void *userdata) int outlet_list_handler(short event, restd_conn_t *conn, void *userdata) { - return 0; + printf("outlet_list_handler\n\n"); + restd_http_response(conn, 200, "application/json", "{}", 2); + return RESTD_CLOSE; } /*--------------------------------------------------------------------------*/ diff --git a/src/rest/rest_server.c b/src/rest/rest_server.c index 3bbd86c..ca83349 100644 --- a/src/rest/rest_server.c +++ b/src/rest/rest_server.c @@ -59,10 +59,10 @@ int setup_rest_server(restd_server_t *rest_server, const char *port, const char printf (">>>>>>>>>>\n"); - restd_server_register_request_handler(rest_server, restd_rest_handler); + //restd_server_register_request_handler(rest_server, restd_rest_handler); restd_server_register_error_handler(rest_server, my_error_handler); - restd_server_register_hook(rest_server, restd_http_handler, NULL); + restd_server_register_hook(rest_server, restd_rest_handler, NULL); // Capabilities. restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/capabilities", NULL, dm);