diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-25 17:19:54 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-25 17:19:54 +0000 |
commit | 19c380f237708910a12f14e98960919e9fe6d865 (patch) | |
tree | 0f5ede8bd4b834b0acd083423c2828cadb4b3665 /src | |
parent | 5afc084a9c29b980bf00fddd7372fa15db63de76 (diff) | |
download | gnunet-19c380f237708910a12f14e98960919e9fe6d865.tar.gz gnunet-19c380f237708910a12f14e98960919e9fe6d865.zip |
-more rest handling
Diffstat (limited to 'src')
-rw-r--r-- | src/identity/plugin_rest_identity.c | 38 | ||||
-rw-r--r-- | src/include/gnunet_rest_lib.h | 50 | ||||
-rw-r--r-- | src/include/gnunet_rest_plugin.h | 22 | ||||
-rw-r--r-- | src/rest/rest.c | 36 |
4 files changed, 112 insertions, 34 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index ea611128d..75c65466c 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -285,12 +285,15 @@ get_ego_for_subsys (void *cls, | |||
285 | * @param handle the RequestHandle | 285 | * @param handle the RequestHandle |
286 | */ | 286 | */ |
287 | void | 287 | void |
288 | ego_info_response (struct RequestHandle *handle) | 288 | ego_info_response (struct RestConnectionDataHandle *con, |
289 | const char *url, | ||
290 | void *cls) | ||
289 | { | 291 | { |
290 | const char *egoname; | 292 | const char *egoname; |
291 | char *keystring; | 293 | char *keystring; |
292 | char *result_str; | 294 | char *result_str; |
293 | char *subsys_val; | 295 | char *subsys_val; |
296 | struct RequestHandle *handle = cls; | ||
294 | struct EgoEntry *ego_entry; | 297 | struct EgoEntry *ego_entry; |
295 | struct GNUNET_HashCode key; | 298 | struct GNUNET_HashCode key; |
296 | struct MHD_Response *resp; | 299 | struct MHD_Response *resp; |
@@ -414,9 +417,12 @@ create_finished (void *cls, const char *emsg) | |||
414 | } | 417 | } |
415 | 418 | ||
416 | static void | 419 | static void |
417 | ego_create_cont (struct RequestHandle *handle) | 420 | ego_create_cont (struct RestConnectionDataHandle *con, |
421 | const char *url, | ||
422 | void *cls) | ||
418 | { | 423 | { |
419 | const char* egoname; | 424 | const char* egoname; |
425 | struct RequestHandle *handle = cls; | ||
420 | char term_data[handle->data_size+1]; | 426 | char term_data[handle->data_size+1]; |
421 | struct EgoEntry *ego_entry; | 427 | struct EgoEntry *ego_entry; |
422 | struct MHD_Response *resp; | 428 | struct MHD_Response *resp; |
@@ -500,10 +506,13 @@ ego_create_cont (struct RequestHandle *handle) | |||
500 | } | 506 | } |
501 | 507 | ||
502 | void | 508 | void |
503 | subsys_set_cont (struct RequestHandle *handle) | 509 | subsys_set_cont (struct RestConnectionDataHandle *con, |
510 | const char *url, | ||
511 | void *cls) | ||
504 | { | 512 | { |
505 | const char *egoname; | 513 | const char *egoname; |
506 | const char *subsys; | 514 | const char *subsys; |
515 | struct RequestHandle *handle = cls; | ||
507 | char term_data[handle->data_size+1]; | 516 | char term_data[handle->data_size+1]; |
508 | struct EgoEntry *ego_entry; | 517 | struct EgoEntry *ego_entry; |
509 | struct MHD_Response *resp; | 518 | struct MHD_Response *resp; |
@@ -604,11 +613,14 @@ subsys_set_cont (struct RequestHandle *handle) | |||
604 | } | 613 | } |
605 | 614 | ||
606 | void | 615 | void |
607 | ego_delete_cont (struct RequestHandle *handle) | 616 | ego_delete_cont (struct RestConnectionDataHandle *con_handle, |
617 | const char* url, | ||
618 | void *cls) | ||
608 | { | 619 | { |
609 | const char *egoname; | 620 | const char *egoname; |
610 | struct EgoEntry *ego_entry; | 621 | struct EgoEntry *ego_entry; |
611 | struct MHD_Response *resp; | 622 | struct MHD_Response *resp; |
623 | struct RequestHandle *handle = cls; | ||
612 | int ego_exists = GNUNET_NO; | 624 | int ego_exists = GNUNET_NO; |
613 | 625 | ||
614 | if (strlen (API_NAMESPACE) >= strlen (handle->url)) | 626 | if (strlen (API_NAMESPACE) >= strlen (handle->url)) |
@@ -645,15 +657,15 @@ ego_delete_cont (struct RequestHandle *handle) | |||
645 | void | 657 | void |
646 | init_cont (struct RequestHandle *handle) | 658 | init_cont (struct RequestHandle *handle) |
647 | { | 659 | { |
648 | if (0 == strcasecmp (handle->method, MHD_HTTP_METHOD_GET)) | 660 | static const struct GNUNET_REST_RestConnectionHandler handlers[] = { |
649 | ego_info_response (handle); | 661 | {MHD_HTTP_METHOD_GET, API_NAMESPACE, &ego_info_response}, |
650 | else if (0 == strcasecmp (handle->method, MHD_HTTP_METHOD_POST)) | 662 | {MHD_HTTP_METHOD_POST, API_NAMESPACE, &ego_create_cont}, |
651 | ego_create_cont (handle); | 663 | {MHD_HTTP_METHOD_PUT, API_NAMESPACE, &subsys_set_cont}, |
652 | else if (0 == strcasecmp (handle->method, MHD_HTTP_METHOD_PUT)) | 664 | {MHD_HTTP_METHOD_DELETE, API_NAMESPACE, &ego_delete_cont}, |
653 | subsys_set_cont (handle); | 665 | GNUNET_REST_HANDLER_END |
654 | else if (0 == strcasecmp (handle->method, MHD_HTTP_METHOD_DELETE)) | 666 | }; |
655 | ego_delete_cont (handle); | 667 | |
656 | else | 668 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle)) |
657 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 669 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
658 | } | 670 | } |
659 | 671 | ||
diff --git a/src/include/gnunet_rest_lib.h b/src/include/gnunet_rest_lib.h index 19a0168e5..cc02f01d0 100644 --- a/src/include/gnunet_rest_lib.h +++ b/src/include/gnunet_rest_lib.h | |||
@@ -36,6 +36,52 @@ | |||
36 | 36 | ||
37 | #define GNUNET_REST_JSONAPI_KEY_TYPE "type" | 37 | #define GNUNET_REST_JSONAPI_KEY_TYPE "type" |
38 | 38 | ||
39 | #define GNUNET_REST_HANDLER_END {NULL, NULL, NULL} | ||
40 | |||
41 | struct RestConnectionDataHandle | ||
42 | { | ||
43 | struct GNUNET_CONTAINER_MultiHashMap *url_param_map; | ||
44 | const char *method; | ||
45 | const char *url; | ||
46 | const char *data; | ||
47 | size_t data_size; | ||
48 | |||
49 | }; | ||
50 | |||
51 | struct GNUNET_REST_RestConnectionHandler | ||
52 | { | ||
53 | /** | ||
54 | * Http method to handle | ||
55 | */ | ||
56 | const char *method; | ||
57 | |||
58 | /** | ||
59 | * Namespace to handle | ||
60 | */ | ||
61 | const char *namespace; | ||
62 | |||
63 | /** | ||
64 | * callback handler | ||
65 | */ | ||
66 | void (*proc) (struct RestConnectionDataHandle *handle, | ||
67 | const char *url, | ||
68 | void *cls); | ||
69 | |||
70 | }; | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Iterator called on obtained result for a REST result. | ||
75 | * | ||
76 | * @param cls closure | ||
77 | * @param resp the response | ||
78 | * @param status status code (HTTP) | ||
79 | */ | ||
80 | typedef void (*GNUNET_REST_ResultProcessor) (void *cls, | ||
81 | struct MHD_Response *resp, | ||
82 | int status); | ||
83 | |||
84 | |||
39 | /** | 85 | /** |
40 | * Resource structs for JSON API | 86 | * Resource structs for JSON API |
41 | */ | 87 | */ |
@@ -152,5 +198,9 @@ GNUNET_REST_namespace_match (const char *url, const char *namespace); | |||
152 | GNUNET_REST_create_json_response (const char *data); | 198 | GNUNET_REST_create_json_response (const char *data); |
153 | 199 | ||
154 | 200 | ||
201 | int | ||
202 | GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn, | ||
203 | const struct GNUNET_REST_RestConnectionHandler *handlers, | ||
204 | void *cls); | ||
155 | 205 | ||
156 | #endif | 206 | #endif |
diff --git a/src/include/gnunet_rest_plugin.h b/src/include/gnunet_rest_plugin.h index e085ccf32..e1eabd813 100644 --- a/src/include/gnunet_rest_plugin.h +++ b/src/include/gnunet_rest_plugin.h | |||
@@ -27,6 +27,7 @@ | |||
27 | #define GNUNET_REST_PLUGIN_H | 27 | #define GNUNET_REST_PLUGIN_H |
28 | 28 | ||
29 | #include "gnunet_util_lib.h" | 29 | #include "gnunet_util_lib.h" |
30 | #include "gnunet_rest_lib.h" | ||
30 | #include "microhttpd.h" | 31 | #include "microhttpd.h" |
31 | 32 | ||
32 | #ifdef __cplusplus | 33 | #ifdef __cplusplus |
@@ -39,27 +40,6 @@ extern "C" | |||
39 | 40 | ||
40 | 41 | ||
41 | /** | 42 | /** |
42 | * Iterator called on obtained result for a REST result. | ||
43 | * | ||
44 | * @param cls closure | ||
45 | * @param resp the response | ||
46 | * @param status status code (HTTP) | ||
47 | */ | ||
48 | typedef void (*GNUNET_REST_ResultProcessor) (void *cls, | ||
49 | struct MHD_Response *resp, | ||
50 | int status); | ||
51 | |||
52 | struct RestConnectionDataHandle | ||
53 | { | ||
54 | struct GNUNET_CONTAINER_MultiHashMap *url_param_map; | ||
55 | const char *method; | ||
56 | const char *url; | ||
57 | const char *data; | ||
58 | size_t data_size; | ||
59 | |||
60 | }; | ||
61 | |||
62 | /** | ||
63 | * @brief struct returned by the initialization function of the plugin | 43 | * @brief struct returned by the initialization function of the plugin |
64 | */ | 44 | */ |
65 | struct GNUNET_REST_Plugin | 45 | struct GNUNET_REST_Plugin |
diff --git a/src/rest/rest.c b/src/rest/rest.c index 0b843ec61..ec46e1273 100644 --- a/src/rest/rest.c +++ b/src/rest/rest.c | |||
@@ -68,6 +68,7 @@ struct JsonApiResponse | |||
68 | int res_count; | 68 | int res_count; |
69 | }; | 69 | }; |
70 | 70 | ||
71 | |||
71 | /** | 72 | /** |
72 | * JSON API | 73 | * JSON API |
73 | */ | 74 | */ |
@@ -296,4 +297,39 @@ GNUNET_REST_create_json_response (const char *data) | |||
296 | 297 | ||
297 | } | 298 | } |
298 | 299 | ||
300 | int | ||
301 | GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn, | ||
302 | const struct GNUNET_REST_RestConnectionHandler *handlers, | ||
303 | void *cls) | ||
304 | { | ||
305 | int count; | ||
306 | int i; | ||
307 | char *url; | ||
308 | |||
309 | count = 0; | ||
310 | |||
311 | while (NULL != handlers[count].method) | ||
312 | count++; | ||
313 | |||
314 | GNUNET_asprintf (&url, "%s", conn->url); | ||
315 | if (url[strlen (url)-1] == '/') | ||
316 | url[strlen (url)-1] = '\0'; | ||
317 | |||
318 | for (i = 0; i < count; i++) | ||
319 | { | ||
320 | if (0 != strcasecmp (conn->method, handlers[count].method)) | ||
321 | break; | ||
322 | if (strlen (url) < strlen (handlers[count].namespace)) | ||
323 | break; | ||
324 | if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[count].namespace)) | ||
325 | break; | ||
326 | //Match | ||
327 | handlers[count].proc (conn, (const char*)url, cls); | ||
328 | GNUNET_free (url); | ||
329 | return GNUNET_YES; | ||
330 | } | ||
331 | GNUNET_free (url); | ||
332 | return GNUNET_NO; | ||
333 | } | ||
334 | |||
299 | /* end of rest.c */ | 335 | /* end of rest.c */ |