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

This commit is contained in:
2020-02-19 23:04:50 +01:00
parent 29fb5f456d
commit d0646de38a
3 changed files with 37 additions and 24 deletions

View File

@@ -466,7 +466,10 @@ void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_
{ {
char *param; char *param;
fragment = obj.data; fragment = obj.data;
if (hook->has_parameter == true)
{
hook->action_name = strdup(fragment);
}
param = strchr(fragment, ':'); param = strchr(fragment, ':');
if (param != NULL) if (param != NULL)
{ {
@@ -474,10 +477,6 @@ void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_
hook->parameter_position = i; hook->parameter_position = i;
hook->parameter_name = strdup(param + 1); hook->parameter_name = strdup(param + 1);
} }
if (hook->has_parameter == true)
{
hook->action_name = strdup(fragment);
}
free(fragment); free(fragment);
i++; i++;
@@ -590,11 +589,6 @@ void restd_resp_free(restd_resp_t *response)
free(response->parameter_name); free(response->parameter_name);
} }
if (response->parameter_value)
{
free(response->parameter_value);
}
if (response->action) if (response->action)
{ {
free(response->action); free(response->action);
@@ -952,14 +946,14 @@ bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request
else else
{ {
int list_size; int list_size;
printf ("Avec param...\n"); bool result = true;
// printf("Avec param... %s\n", request_path);
qlist_t *uri_fragments = qstrtokenizer(request_path, "/"); qlist_t *uri_fragments = qstrtokenizer(request_path, "/");
list_size = qlist_size(hook->path_fragments); list_size = qlist_size(hook->path_fragments);
if (list_size == qlist_size(uri_fragments)) if (list_size == qlist_size(uri_fragments))
{ {
qlist_obj_t obj_hook; qlist_obj_t obj_hook;
qlist_obj_t obj_uri; qlist_obj_t obj_uri;
printf ("Les Listes ont le meme tailles...\n");
memset((void *)&obj_hook, 0, sizeof(obj_hook)); memset((void *)&obj_hook, 0, sizeof(obj_hook));
memset((void *)&obj_uri, 0, sizeof(obj_uri)); memset((void *)&obj_uri, 0, sizeof(obj_uri));
@@ -969,17 +963,37 @@ bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request
qlist_getnext(hook->path_fragments, &obj_hook, true); qlist_getnext(hook->path_fragments, &obj_hook, true);
qlist_getnext(uri_fragments, &obj_uri, true); qlist_getnext(uri_fragments, &obj_uri, true);
printf ("%d] %s == %s\n", i, (char*)obj_hook.data, (char*)obj_uri.data); // printf("%d] %s == %s\n", i, (char *)obj_hook.data, (char *)obj_uri.data);
if (hook->parameter_position != i)
{
if (strcmp(obj_hook.data, obj_uri.data) != 0)
{
result = false;
}
}
else
{
response->parameter_value = atoi((char *)obj_uri.data);
}
free(obj_hook.data); free(obj_hook.data);
free(obj_uri.data); free(obj_uri.data);
if (result == false)
return false;
} }
response->has_parameter = true;
response->parameter_name = strdup(hook->parameter_name);
if (hook->action_name != NULL)
response->action = strdup(hook->action_name);
hook->cb(response, hook->userdata);
qlist_free(uri_fragments);
return true;
} }
else else
{ {
printf("rate: path fragment: %ld != uri_fragment: %ld\n", qlist_size(hook->path_fragments), qlist_size(uri_fragments)); printf("rate: path fragment: %ld != uri_fragment: %ld\n", qlist_size(hook->path_fragments), qlist_size(uri_fragments));
} }
qlist_free(uri_fragments); qlist_free(uri_fragments);
} }
#if 0 #if 0

View File

@@ -142,7 +142,7 @@ struct restd_resp_s
struct evhttp_request *request; struct evhttp_request *request;
bool has_parameter; bool has_parameter;
char *parameter_name; char *parameter_name;
char *parameter_value; uint32_t parameter_value;
char *action; char *action;
}; };

View File

@@ -31,10 +31,9 @@
#define ksuccess_post_body "{\"status\":\"post\"}" #define ksuccess_post_body "{\"status\":\"post\"}"
#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\",\"action\":\"todo\" }" #define ksuccess_put_param2 "{ \"id\":\"1977\",\"action\":\"todo\" }"
#define kserver_port "7777" #define kserver_port "7777"
#define kapi_test_get "/api/v1/test_get" #define kapi_test_get "/api/v1/test_get"
@@ -98,11 +97,11 @@ int my_success_rest_put_param_handler(restd_resp_t *response, void *arg)
struct json_object *root_node; struct json_object *root_node;
root_node = json_object_new_object(); root_node = json_object_new_object();
if (response->has_parameter) if (response->has_parameter == true)
{ {
if ((response->parameter_value != NULL) && (response->parameter_name != NULL)) if (response->parameter_name != NULL)
{ {
json_object_object_add(root_node, response->parameter_name, json_object_new_string(response->parameter_value)); json_object_object_add(root_node, response->parameter_name, json_object_new_int(response->parameter_value));
} }
if (response->action != NULL) if (response->action != NULL)
{ {