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,
|
||||
restd_callback cb, void *userdata)
|
||||
{
|
||||
int i = 0;
|
||||
restd_hook_t *hook;
|
||||
char *fragment;
|
||||
qlist_obj_t obj;
|
||||
|
||||
// Init Hook.
|
||||
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, "/");
|
||||
|
||||
// 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;
|
||||
fragment = obj.data;
|
||||
|
||||
param = strchr(fragment, ':');
|
||||
if (param != NULL)
|
||||
{
|
||||
hook->has_param = true;
|
||||
hook->param_name = strdup(param + 1);
|
||||
hook->has_parameter = true;
|
||||
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);
|
||||
}
|
||||
|
||||
free(fragment);
|
||||
i++;
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
hook->has_param = false;
|
||||
hook->has_parameter = false;
|
||||
|
||||
return hook;
|
||||
}
|
||||
@@ -546,8 +552,8 @@ void restd_hook_free(restd_hook_t *hook)
|
||||
if (hook->path)
|
||||
free(hook->path);
|
||||
|
||||
if (hook->param_name)
|
||||
free(hook->param_name);
|
||||
if (hook->parameter_name)
|
||||
free(hook->parameter_name);
|
||||
|
||||
if (hook->action_name)
|
||||
free(hook->action_name);
|
||||
@@ -579,10 +585,16 @@ void restd_resp_free(restd_resp_t *response)
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
int ret;
|
||||
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 pos = -1;
|
||||
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);
|
||||
if (rett == 0)
|
||||
{
|
||||
hook->cb(response, hook->userdata);
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -127,9 +127,10 @@ struct restd_hook_s
|
||||
char *path;
|
||||
restd_callback cb;
|
||||
void *userdata;
|
||||
bool has_param;
|
||||
qlist_t *path_fragments;
|
||||
char *param_name;
|
||||
bool has_parameter;
|
||||
int parameter_position;
|
||||
char *parameter_name;
|
||||
char *action_name;
|
||||
};
|
||||
|
||||
@@ -140,7 +141,8 @@ struct restd_resp_s
|
||||
{
|
||||
struct evhttp_request *request;
|
||||
bool has_parameter;
|
||||
char *parameter;
|
||||
char *parameter_name;
|
||||
char *parameter_value;
|
||||
char *action;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user