wip
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-01-17 17:53:43 +01:00
parent 17cdd989e8
commit 9acd116ab2
5 changed files with 87 additions and 48 deletions

View File

@@ -91,6 +91,8 @@ enum restd_http_request_status_e
/*----------------------------------------------------------------------------*\ /*----------------------------------------------------------------------------*\
| PUBLIC FUNCTIONS | | 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 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); extern enum restd_http_request_status_e restd_http_get_status(restd_conn_t *conn);

View File

@@ -41,9 +41,7 @@
#include "macro.h" #include "macro.h"
#ifndef _DOXYGEN_SKIP #ifndef _DOXYGEN_SKIP
static restd_http_t *http_new(struct evbuffer *out);
static void http_free(restd_http_t *http); 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, static size_t http_add_inbuf(struct evbuffer *buffer, restd_http_t *http,
size_t maxsize); size_t maxsize);
@@ -535,7 +533,7 @@ const char *restd_http_get_reason(int code)
*****************************************************************************/ *****************************************************************************/
#ifndef _DOXYGEN_SKIP #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. // Create a new connection container.
restd_http_t *http = NEW_OBJECT(restd_http_t); 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); http_free((restd_http_t *)userdata);
} }

View File

@@ -28,6 +28,7 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <assert.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
@@ -172,12 +173,45 @@ static bool contain(const char *src, const char *dest, int len)
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn restd_rest_handler * @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) 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; 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); restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
char *root_path; char *root_path;
qlist_t *hooks = conn->server->hooks; 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; restd_hook_t *hook = (restd_hook_t *)obj.data;
if (hook->cb) if (hook->cb)
{ {
// DEBUG("call_hooks: method: %s - %s \n", hook->method, conn->method); 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("call_hooks: path: %s - %s\n", hook->path, http->request.path);
// DEBUG("HOOK FOUND !!!!\n"); DEBUG("HOOK FOUND !!!!\n");
if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0)) if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0))
{ {
// DEBUG("Hook found but method failed.\n"); // DEBUG("Hook found but method failed.\n");

View File

@@ -27,53 +27,56 @@
#include <event2/buffer.h> #include <event2/buffer.h>
#include "devices/devices_manager.h" #include <json.h>
#include "devices/devices_manager.h"
#include "domo.h"
#include "rest_devices_handlers.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) 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. data_size = restd_http_get_content_length(conn);
//ad_conn_set_userdata(conn, cdata, my_userdata_free_cb); 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; 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) 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;
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/

View File

@@ -59,10 +59,10 @@ int setup_rest_server(restd_server_t *rest_server, const char *port, const char
printf (">>>>>>>>>>\n"); 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_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. // Capabilities.
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/capabilities", NULL, dm); restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/capabilities", NULL, dm);