This commit is contained in:
@@ -446,8 +446,10 @@ void restd_server_set_option(restd_server_t *server, const char *key, const char
|
|||||||
void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_type method, const char *path,
|
void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_type method, const char *path,
|
||||||
restd_callback cb, void *userdata)
|
restd_callback cb, void *userdata)
|
||||||
{
|
{
|
||||||
|
int i = 0;
|
||||||
restd_hook_t *hook;
|
restd_hook_t *hook;
|
||||||
char *fragment;
|
char *fragment;
|
||||||
|
qlist_obj_t obj;
|
||||||
|
|
||||||
// Init Hook.
|
// Init Hook.
|
||||||
hook = restd_hook_new();
|
hook = restd_hook_new();
|
||||||
@@ -459,22 +461,26 @@ void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_
|
|||||||
hook->path_fragments = qstrtokenizer(path, "/");
|
hook->path_fragments = qstrtokenizer(path, "/");
|
||||||
|
|
||||||
// Split URI and detect parameter and action.
|
// Split URI and detect parameter and action.
|
||||||
while ((fragment = qlist_popfirst(hook->path_fragments, NULL)) != NULL)
|
memset((void *)&obj, 0, sizeof(obj));
|
||||||
|
while (qlist_getnext(hook->path_fragments, &obj, true))
|
||||||
{
|
{
|
||||||
char *param;
|
char *param;
|
||||||
|
fragment = obj.data;
|
||||||
|
|
||||||
param = strchr(fragment, ':');
|
param = strchr(fragment, ':');
|
||||||
if (param != NULL)
|
if (param != NULL)
|
||||||
{
|
{
|
||||||
hook->has_param = true;
|
hook->has_parameter = true;
|
||||||
hook->param_name = strdup(param + 1);
|
hook->parameter_position = i;
|
||||||
|
hook->parameter_name = strdup(param + 1);
|
||||||
}
|
}
|
||||||
if (hook->has_param == true)
|
if (hook->has_parameter == true)
|
||||||
{
|
{
|
||||||
hook->action_name = strdup(fragment);
|
hook->action_name = strdup(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(fragment);
|
free(fragment);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
server->hooks->addlast(server->hooks, (void *)hook, sizeof(restd_hook_t));
|
server->hooks->addlast(server->hooks, (void *)hook, sizeof(restd_hook_t));
|
||||||
@@ -530,7 +536,7 @@ restd_hook_t *restd_hook_new(void)
|
|||||||
|
|
||||||
bzero((void *)hook, sizeof(restd_hook_t));
|
bzero((void *)hook, sizeof(restd_hook_t));
|
||||||
|
|
||||||
hook->has_param = false;
|
hook->has_parameter = false;
|
||||||
|
|
||||||
return hook;
|
return hook;
|
||||||
}
|
}
|
||||||
@@ -546,8 +552,8 @@ void restd_hook_free(restd_hook_t *hook)
|
|||||||
if (hook->path)
|
if (hook->path)
|
||||||
free(hook->path);
|
free(hook->path);
|
||||||
|
|
||||||
if (hook->param_name)
|
if (hook->parameter_name)
|
||||||
free(hook->param_name);
|
free(hook->parameter_name);
|
||||||
|
|
||||||
if (hook->action_name)
|
if (hook->action_name)
|
||||||
free(hook->action_name);
|
free(hook->action_name);
|
||||||
@@ -579,10 +585,16 @@ void restd_resp_free(restd_resp_t *response)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response->parameter)
|
if (response->parameter_name)
|
||||||
{
|
{
|
||||||
free(response->parameter);
|
free(response->parameter_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (response->parameter_value)
|
||||||
|
{
|
||||||
|
free(response->parameter_value);
|
||||||
|
}
|
||||||
|
|
||||||
if (response->action)
|
if (response->action)
|
||||||
{
|
{
|
||||||
free(response->action);
|
free(response->action);
|
||||||
@@ -926,8 +938,68 @@ void restd_http_response_from_file(struct evhttp_request *req, int code, int fd,
|
|||||||
|
|
||||||
bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request_path)
|
bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request_path)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
if ((hook->path != NULL) && (request_path != NULL))
|
if ((hook->path != NULL) && (request_path != NULL))
|
||||||
{
|
{
|
||||||
|
if (hook->has_parameter == false)
|
||||||
|
{
|
||||||
|
if (strcmp(hook->path, request_path) == 0)
|
||||||
|
{
|
||||||
|
hook->cb(response, hook->userdata);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int list_size;
|
||||||
|
printf ("Avec param...\n");
|
||||||
|
qlist_t *uri_fragments = qstrtokenizer(request_path, "/");
|
||||||
|
list_size = qlist_size(hook->path_fragments);
|
||||||
|
if (list_size == qlist_size(uri_fragments))
|
||||||
|
{
|
||||||
|
qlist_obj_t obj_hook;
|
||||||
|
qlist_obj_t obj_uri;
|
||||||
|
printf ("Les Listes ont le meme tailles...\n");
|
||||||
|
|
||||||
|
memset((void *)&obj_hook, 0, sizeof(obj_hook));
|
||||||
|
memset((void *)&obj_uri, 0, sizeof(obj_uri));
|
||||||
|
|
||||||
|
for (int i= 0; i < list_size; i++)
|
||||||
|
{
|
||||||
|
qlist_getnext(hook->path_fragments, &obj_hook, true);
|
||||||
|
qlist_getnext(uri_fragments, &obj_uri, true);
|
||||||
|
|
||||||
|
printf ("%d] %s == %s\n", i, (char*)obj_hook.data, (char*)obj_uri.data);
|
||||||
|
free(obj_hook.data);
|
||||||
|
free(obj_uri.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("rate: path fragment: %ld != uri_fragment: %ld\n", qlist_size(hook->path_fragments), qlist_size(uri_fragments));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
qlist_free(uri_fragments);
|
||||||
|
}
|
||||||
|
#if 0
|
||||||
|
|
||||||
|
qlist_obj_t obj;
|
||||||
|
|
||||||
|
// Init Hook.
|
||||||
|
hook = restd_hook_new();
|
||||||
|
|
||||||
|
hook->method = method;
|
||||||
|
hook->path = (path) ? strdup(path) : NULL;
|
||||||
|
hook->cb = cb;
|
||||||
|
hook->userdata = userdata;
|
||||||
|
hook->path_fragments = qstrtokenizer(path, "/");
|
||||||
|
|
||||||
|
// Split URI and detect parameter and action.
|
||||||
|
memset((void *)&obj, 0, sizeof(obj));
|
||||||
|
while (qlist_getnext(hook->path_fragments, &obj, true))
|
||||||
|
{
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int pos = -1;
|
int pos = -1;
|
||||||
while (hook->path[i])
|
while (hook->path[i])
|
||||||
@@ -949,10 +1021,10 @@ bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request
|
|||||||
int rett = strcmp(hook->path, request_path);
|
int rett = strcmp(hook->path, request_path);
|
||||||
if (rett == 0)
|
if (rett == 0)
|
||||||
{
|
{
|
||||||
hook->cb(response, hook->userdata);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -127,9 +127,10 @@ struct restd_hook_s
|
|||||||
char *path;
|
char *path;
|
||||||
restd_callback cb;
|
restd_callback cb;
|
||||||
void *userdata;
|
void *userdata;
|
||||||
bool has_param;
|
|
||||||
qlist_t *path_fragments;
|
qlist_t *path_fragments;
|
||||||
char *param_name;
|
bool has_parameter;
|
||||||
|
int parameter_position;
|
||||||
|
char *parameter_name;
|
||||||
char *action_name;
|
char *action_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -140,7 +141,8 @@ struct restd_resp_s
|
|||||||
{
|
{
|
||||||
struct evhttp_request *request;
|
struct evhttp_request *request;
|
||||||
bool has_parameter;
|
bool has_parameter;
|
||||||
char *parameter;
|
char *parameter_name;
|
||||||
|
char *parameter_value;
|
||||||
char *action;
|
char *action;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
#define kerror_body "{\"status\":\"error\"}"
|
#define kerror_body "{\"status\":\"error\"}"
|
||||||
|
|
||||||
#define ksuccess_put_param1 "{\"id\":\"1\"}"
|
#define ksuccess_put_param1 "{\"id\":\"1\"}"
|
||||||
#define ksuccess_put_param2 "{\"id\":\"1977\"}"
|
#define ksuccess_put_param2 "{\"id\":\"1977\",\"action\":\"todo\"}"
|
||||||
|
|
||||||
|
|
||||||
#define kserver_port "7777"
|
#define kserver_port "7777"
|
||||||
@@ -40,8 +40,8 @@
|
|||||||
|
|
||||||
#define kapi_test_put_id1 "/api/v1/test_put/:id"
|
#define kapi_test_put_id1 "/api/v1/test_put/:id"
|
||||||
#define kapi_test_put_id1_body "/api/v1/test_put/1"
|
#define kapi_test_put_id1_body "/api/v1/test_put/1"
|
||||||
#define kapi_test_put_id2 "/api/v1/test_put/:id/action"
|
#define kapi_test_put_id2 "/api/v1/test_put/:id/todo"
|
||||||
#define kapi_test_put_id2_body "/api/v1/test_put/1977/action"
|
#define kapi_test_put_id2_body "/api/v1/test_put/1977/todo"
|
||||||
|
|
||||||
#define kpost_method "POST"
|
#define kpost_method "POST"
|
||||||
#define kget_method "GET"
|
#define kget_method "GET"
|
||||||
@@ -93,6 +93,31 @@ int my_success_rest_put_handler(restd_resp_t *response, void *arg)
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
int my_success_rest_put_param_handler(restd_resp_t *response, void *arg)
|
||||||
|
{
|
||||||
|
struct json_object *root_node;
|
||||||
|
root_node = json_object_new_object();
|
||||||
|
|
||||||
|
if (response->has_parameter)
|
||||||
|
{
|
||||||
|
if ((response->parameter_value != NULL) && (response->parameter_name != NULL))
|
||||||
|
{
|
||||||
|
json_object_object_add(root_node, response->parameter_name, json_object_new_string(response->parameter_value));
|
||||||
|
}
|
||||||
|
if (response->action != NULL)
|
||||||
|
{
|
||||||
|
json_object_object_add(root_node, "action", json_object_new_string(response->action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
restd_http_response(response, 200, "application/json", json_object_to_json_string(root_node));
|
||||||
|
json_object_put(root_node);
|
||||||
|
|
||||||
|
return RESTD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
int my_success_rest_post_handler(restd_resp_t *response, void *arg)
|
int my_success_rest_post_handler(restd_resp_t *response, void *arg)
|
||||||
{
|
{
|
||||||
char *body;
|
char *body;
|
||||||
@@ -469,7 +494,7 @@ TEST("Rest - create start access to all rest hook free\t")
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
#if 0
|
|
||||||
TEST("Rest - create start access to rest hook with params free\t")
|
TEST("Rest - create start access to rest hook with params free\t")
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -486,8 +511,8 @@ TEST("Rest - create start access to rest hook with params free\t")
|
|||||||
//restd_server_register_call_hooks_handler(rest_server, restd_rest_handler);
|
//restd_server_register_call_hooks_handler(rest_server, restd_rest_handler);
|
||||||
|
|
||||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, kapi_test_get, my_success_rest_get_handler, NULL);
|
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, kapi_test_get, my_success_rest_get_handler, NULL);
|
||||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_PUT, kapi_test_put_id1, my_success_rest_put_handler, NULL);
|
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_PUT, kapi_test_put_id1, my_success_rest_put_param_handler, NULL);
|
||||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_PUT, kapi_test_put_id2, my_success_rest_put_handler, NULL);
|
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_PUT, kapi_test_put_id2, my_success_rest_put_param_handler, NULL);
|
||||||
|
|
||||||
ret = restd_server_start(rest_server);
|
ret = restd_server_start(rest_server);
|
||||||
ASSERT_EQUAL_INT(ret, 0);
|
ASSERT_EQUAL_INT(ret, 0);
|
||||||
@@ -500,4 +525,3 @@ TEST("Rest - create start access to rest hook with params free\t")
|
|||||||
|
|
||||||
restd_server_free(rest_server);
|
restd_server_free(rest_server);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user