diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-05-02 16:32:36 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-05-02 16:32:36 +0000 |
commit | bee208bcd6803829aa26c55a4c8e176a5f2c815f (patch) | |
tree | 6974bc9e94a1ec938b5376d2631233d2e75baf7d /src | |
parent | fc9b25be2dbe5392501dbc0b6659c1aa50809bc2 (diff) | |
download | gnunet-bee208bcd6803829aa26c55a4c8e176a5f2c815f.tar.gz gnunet-bee208bcd6803829aa26c55a4c8e176a5f2c815f.zip |
- Refactor jsonapi into separate module
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/gns/Makefile.am | 1 | ||||
-rw-r--r-- | src/gns/plugin_rest_gns.c | 17 | ||||
-rw-r--r-- | src/identity-provider/Makefile.am | 1 | ||||
-rw-r--r-- | src/identity-provider/identity_token.c | 2 | ||||
-rw-r--r-- | src/identity-provider/plugin_rest_identity_provider.c | 33 | ||||
-rw-r--r-- | src/identity/Makefile.am | 1 | ||||
-rw-r--r-- | src/identity/plugin_rest_identity.c | 93 | ||||
-rw-r--r-- | src/include/gnunet_jsonapi_lib.h | 202 | ||||
-rw-r--r-- | src/include/gnunet_rest_lib.h | 168 | ||||
-rw-r--r-- | src/jsonapi/Makefile.am | 34 | ||||
-rw-r--r-- | src/jsonapi/jsonapi.c | 478 | ||||
-rw-r--r-- | src/namestore/Makefile.am | 1 | ||||
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 65 | ||||
-rw-r--r-- | src/rest/rest.c | 391 |
15 files changed, 830 insertions, 662 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 5244c795d..c2e6fd64e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -16,6 +16,7 @@ if HAVE_EXPERIMENTAL | |||
16 | rps \ | 16 | rps \ |
17 | $(CONSENSUS) \ | 17 | $(CONSENSUS) \ |
18 | $(SECRETSHARING) | 18 | $(SECRETSHARING) |
19 | |||
19 | endif | 20 | endif |
20 | 21 | ||
21 | if HAVE_REST | 22 | if HAVE_REST |
@@ -51,6 +52,9 @@ endif | |||
51 | 52 | ||
52 | if HAVE_REST | 53 | if HAVE_REST |
53 | REST_DIR = rest | 54 | REST_DIR = rest |
55 | if HAVE_JSON | ||
56 | JSONAPI_DIR = jsonapi | ||
57 | endif | ||
54 | endif | 58 | endif |
55 | 59 | ||
56 | if TALER_ONLY | 60 | if TALER_ONLY |
@@ -63,6 +67,7 @@ SUBDIRS = \ | |||
63 | include $(INTLEMU_SUBDIRS) \ | 67 | include $(INTLEMU_SUBDIRS) \ |
64 | util \ | 68 | util \ |
65 | $(JSON_DIR) \ | 69 | $(JSON_DIR) \ |
70 | $(JSONAPI_DIR) \ | ||
66 | $(REST_DIR) \ | 71 | $(REST_DIR) \ |
67 | hello \ | 72 | hello \ |
68 | tun \ | 73 | tun \ |
diff --git a/src/gns/Makefile.am b/src/gns/Makefile.am index b058a8033..5ac598735 100644 --- a/src/gns/Makefile.am +++ b/src/gns/Makefile.am | |||
@@ -234,6 +234,7 @@ libgnunet_plugin_rest_gns_la_LIBADD = \ | |||
234 | libgnunetgns.la \ | 234 | libgnunetgns.la \ |
235 | $(top_builddir)/src/rest/libgnunetrest.la \ | 235 | $(top_builddir)/src/rest/libgnunetrest.la \ |
236 | $(top_builddir)/src/identity/libgnunetidentity.la \ | 236 | $(top_builddir)/src/identity/libgnunetidentity.la \ |
237 | $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ | ||
237 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | 238 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ |
238 | $(LTLIBINTL) -ljansson -lmicrohttpd | 239 | $(LTLIBINTL) -ljansson -lmicrohttpd |
239 | libgnunet_plugin_rest_gns_la_LDFLAGS = \ | 240 | libgnunet_plugin_rest_gns_la_LDFLAGS = \ |
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c index 1315e6cda..91a091f2e 100644 --- a/src/gns/plugin_rest_gns.c +++ b/src/gns/plugin_rest_gns.c | |||
@@ -32,6 +32,7 @@ | |||
32 | #include <gnunet_namestore_service.h> | 32 | #include <gnunet_namestore_service.h> |
33 | #include <gnunet_gns_service.h> | 33 | #include <gnunet_gns_service.h> |
34 | #include <gnunet_rest_lib.h> | 34 | #include <gnunet_rest_lib.h> |
35 | #include <gnunet_jsonapi_lib.h> | ||
35 | #include <jansson.h> | 36 | #include <jansson.h> |
36 | 37 | ||
37 | #define GNUNET_REST_API_NS_GNS "/gns" | 38 | #define GNUNET_REST_API_NS_GNS "/gns" |
@@ -284,16 +285,16 @@ process_lookup_result (void *cls, uint32_t rd_count, | |||
284 | { | 285 | { |
285 | struct LookupHandle *handle = cls; | 286 | struct LookupHandle *handle = cls; |
286 | struct MHD_Response *resp; | 287 | struct MHD_Response *resp; |
287 | struct JsonApiObject *json_object; | 288 | struct GNUNET_JSONAPI_Object *json_object; |
288 | struct JsonApiResource *json_resource; | 289 | struct GNUNET_JSONAPI_Resource *json_resource; |
289 | uint32_t i; | 290 | uint32_t i; |
290 | char *result; | 291 | char *result; |
291 | json_t *result_array; | 292 | json_t *result_array; |
292 | json_t *record_obj; | 293 | json_t *record_obj; |
293 | 294 | ||
294 | result_array = json_array(); | 295 | result_array = json_array(); |
295 | json_object = GNUNET_REST_jsonapi_object_new (); | 296 | json_object = GNUNET_JSONAPI_object_new (); |
296 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name); | 297 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name); |
297 | handle->lookup_request = NULL; | 298 | handle->lookup_request = NULL; |
298 | for (i=0; i<rd_count; i++) | 299 | for (i=0; i<rd_count; i++) |
299 | { | 300 | { |
@@ -304,14 +305,14 @@ process_lookup_result (void *cls, uint32_t rd_count, | |||
304 | json_array_append (result_array, record_obj); | 305 | json_array_append (result_array, record_obj); |
305 | json_decref (record_obj); | 306 | json_decref (record_obj); |
306 | } | 307 | } |
307 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 308 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
308 | GNUNET_REST_JSONAPI_GNS_RECORD, | 309 | GNUNET_REST_JSONAPI_GNS_RECORD, |
309 | result_array); | 310 | result_array); |
310 | GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); | 311 | GNUNET_JSONAPI_object_resource_add (json_object, json_resource); |
311 | GNUNET_REST_jsonapi_data_serialize (json_object, &result); | 312 | GNUNET_JSONAPI_data_serialize (json_object, &result); |
312 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); | 313 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); |
313 | json_decref (result_array); | 314 | json_decref (result_array); |
314 | GNUNET_REST_jsonapi_object_delete (json_object); | 315 | GNUNET_JSONAPI_object_delete (json_object); |
315 | resp = GNUNET_REST_create_json_response (result); | 316 | resp = GNUNET_REST_create_json_response (result); |
316 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 317 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
317 | GNUNET_free (result); | 318 | GNUNET_free (result); |
diff --git a/src/identity-provider/Makefile.am b/src/identity-provider/Makefile.am index 4ba2f4700..ca224b860 100644 --- a/src/identity-provider/Makefile.am +++ b/src/identity-provider/Makefile.am | |||
@@ -58,6 +58,7 @@ libgnunet_plugin_rest_identity_provider_la_LIBADD = \ | |||
58 | $(top_builddir)/src/identity/libgnunetidentity.la \ | 58 | $(top_builddir)/src/identity/libgnunetidentity.la \ |
59 | libgnunetidentityprovider.la \ | 59 | libgnunetidentityprovider.la \ |
60 | $(top_builddir)/src/rest/libgnunetrest.la \ | 60 | $(top_builddir)/src/rest/libgnunetrest.la \ |
61 | $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ | ||
61 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ | 62 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ |
62 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | 63 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ |
63 | $(LTLIBINTL) -ljansson -lmicrohttpd | 64 | $(LTLIBINTL) -ljansson -lmicrohttpd |
diff --git a/src/identity-provider/identity_token.c b/src/identity-provider/identity_token.c index 958597630..3bed6962e 100644 --- a/src/identity-provider/identity_token.c +++ b/src/identity-provider/identity_token.c | |||
@@ -629,7 +629,7 @@ ticket_payload_serialize (struct TokenTicketPayload *payload, | |||
629 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); | 629 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); |
630 | 630 | ||
631 | GNUNET_asprintf (result, | 631 | GNUNET_asprintf (result, |
632 | "{\"nonce\": \""SCNu64"\",\"identity\": \"%s\",\"label\": \"%s\"}", | 632 | "{\"nonce\": \"%"SCNu64"\",\"identity\": \"%s\",\"label\": \"%s\"}", |
633 | payload->nonce, identity_key_str, payload->label); | 633 | payload->nonce, identity_key_str, payload->label); |
634 | GNUNET_free (identity_key_str); | 634 | GNUNET_free (identity_key_str); |
635 | 635 | ||
diff --git a/src/identity-provider/plugin_rest_identity_provider.c b/src/identity-provider/plugin_rest_identity_provider.c index 376110a6d..1f03713ea 100644 --- a/src/identity-provider/plugin_rest_identity_provider.c +++ b/src/identity-provider/plugin_rest_identity_provider.c | |||
@@ -31,6 +31,7 @@ | |||
31 | #include "gnunet_gnsrecord_lib.h" | 31 | #include "gnunet_gnsrecord_lib.h" |
32 | #include "gnunet_namestore_service.h" | 32 | #include "gnunet_namestore_service.h" |
33 | #include "gnunet_rest_lib.h" | 33 | #include "gnunet_rest_lib.h" |
34 | #include "gnunet_jsonapi_lib.h" | ||
34 | #include "microhttpd.h" | 35 | #include "microhttpd.h" |
35 | #include <jansson.h> | 36 | #include <jansson.h> |
36 | #include <inttypes.h> | 37 | #include <inttypes.h> |
@@ -273,7 +274,7 @@ struct RequestHandle | |||
273 | /** | 274 | /** |
274 | * Response object | 275 | * Response object |
275 | */ | 276 | */ |
276 | struct JsonApiObject *resp_object; | 277 | struct GNUNET_JSONAPI_Object *resp_object; |
277 | 278 | ||
278 | }; | 279 | }; |
279 | 280 | ||
@@ -290,7 +291,7 @@ cleanup_handle (struct RequestHandle *handle) | |||
290 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 291 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
291 | "Cleaning up\n"); | 292 | "Cleaning up\n"); |
292 | if (NULL != handle->resp_object) | 293 | if (NULL != handle->resp_object) |
293 | GNUNET_REST_jsonapi_object_delete (handle->resp_object); | 294 | GNUNET_JSONAPI_object_delete (handle->resp_object); |
294 | if (NULL != handle->timeout_task) | 295 | if (NULL != handle->timeout_task) |
295 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | 296 | GNUNET_SCHEDULER_cancel (handle->timeout_task); |
296 | if (NULL != handle->identity_handle) | 297 | if (NULL != handle->identity_handle) |
@@ -381,7 +382,7 @@ token_creat_cont (void *cls, | |||
381 | const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket, | 382 | const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket, |
382 | const struct GNUNET_IDENTITY_PROVIDER_Token *token) | 383 | const struct GNUNET_IDENTITY_PROVIDER_Token *token) |
383 | { | 384 | { |
384 | struct JsonApiResource *json_resource; | 385 | struct GNUNET_JSONAPI_Resource *json_resource; |
385 | struct RequestHandle *handle = cls; | 386 | struct RequestHandle *handle = cls; |
386 | struct MHD_Response *resp; | 387 | struct MHD_Response *resp; |
387 | json_t *ticket_json; | 388 | json_t *ticket_json; |
@@ -397,26 +398,26 @@ token_creat_cont (void *cls, | |||
397 | return; | 398 | return; |
398 | } | 399 | } |
399 | 400 | ||
400 | handle->resp_object = GNUNET_REST_jsonapi_object_new (); | 401 | handle->resp_object = GNUNET_JSONAPI_object_new (); |
401 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET, | 402 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET, |
402 | label); | 403 | label); |
403 | ticket_str = GNUNET_IDENTITY_PROVIDER_ticket_to_string (ticket); | 404 | ticket_str = GNUNET_IDENTITY_PROVIDER_ticket_to_string (ticket); |
404 | token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token); | 405 | token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token); |
405 | ticket_json = json_string (ticket_str); | 406 | ticket_json = json_string (ticket_str); |
406 | token_json = json_string (token_str); | 407 | token_json = json_string (token_str); |
407 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 408 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
408 | GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET, | 409 | GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET, |
409 | ticket_json); | 410 | ticket_json); |
410 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 411 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
411 | GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN, | 412 | GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN, |
412 | token_json); | 413 | token_json); |
413 | GNUNET_free (ticket_str); | 414 | GNUNET_free (ticket_str); |
414 | GNUNET_free (token_str); | 415 | GNUNET_free (token_str); |
415 | json_decref (ticket_json); | 416 | json_decref (ticket_json); |
416 | json_decref (token_json); | 417 | json_decref (token_json); |
417 | GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource); | 418 | GNUNET_JSONAPI_object_resource_add (handle->resp_object, json_resource); |
418 | 419 | ||
419 | GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result_str); | 420 | GNUNET_JSONAPI_data_serialize (handle->resp_object, &result_str); |
420 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 421 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
421 | resp = GNUNET_REST_create_json_response (result_str); | 422 | resp = GNUNET_REST_create_json_response (result_str); |
422 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 423 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
@@ -632,7 +633,7 @@ return_token_list (void *cls) | |||
632 | struct RequestHandle *handle = cls; | 633 | struct RequestHandle *handle = cls; |
633 | struct MHD_Response *resp; | 634 | struct MHD_Response *resp; |
634 | 635 | ||
635 | GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result_str); | 636 | GNUNET_JSONAPI_data_serialize (handle->resp_object, &result_str); |
636 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 637 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
637 | resp = GNUNET_REST_create_json_response (result_str); | 638 | resp = GNUNET_REST_create_json_response (result_str); |
638 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 639 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
@@ -657,7 +658,7 @@ token_collect (void *cls, | |||
657 | char* data; | 658 | char* data; |
658 | struct RequestHandle *handle = cls; | 659 | struct RequestHandle *handle = cls; |
659 | struct EgoEntry *ego_tmp; | 660 | struct EgoEntry *ego_tmp; |
660 | struct JsonApiResource *json_resource; | 661 | struct GNUNET_JSONAPI_Resource *json_resource; |
661 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key; | 662 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key; |
662 | json_t *issuer; | 663 | json_t *issuer; |
663 | json_t *token; | 664 | json_t *token; |
@@ -698,20 +699,20 @@ token_collect (void *cls, | |||
698 | rd[i].data, | 699 | rd[i].data, |
699 | rd[i].data_size); | 700 | rd[i].data_size); |
700 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token: %s\n", data); | 701 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token: %s\n", data); |
701 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN, | 702 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN, |
702 | label); | 703 | label); |
703 | issuer = json_string (handle->ego_head->identifier); | 704 | issuer = json_string (handle->ego_head->identifier); |
704 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 705 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
705 | GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST, | 706 | GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST, |
706 | issuer); | 707 | issuer); |
707 | json_decref (issuer); | 708 | json_decref (issuer); |
708 | token = json_string (data); | 709 | token = json_string (data); |
709 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 710 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
710 | GNUNET_REST_JSONAPI_IDENTITY_TOKEN, | 711 | GNUNET_REST_JSONAPI_IDENTITY_TOKEN, |
711 | token); | 712 | token); |
712 | json_decref (token); | 713 | json_decref (token); |
713 | 714 | ||
714 | GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource); | 715 | GNUNET_JSONAPI_object_resource_add (handle->resp_object, json_resource); |
715 | GNUNET_free (data); | 716 | GNUNET_free (data); |
716 | } | 717 | } |
717 | } | 718 | } |
@@ -770,7 +771,7 @@ list_token_cont (struct RestConnectionDataHandle *con_handle, | |||
770 | GNUNET_free (ego_tmp); | 771 | GNUNET_free (ego_tmp); |
771 | } | 772 | } |
772 | } | 773 | } |
773 | handle->resp_object = GNUNET_REST_jsonapi_object_new (); | 774 | handle->resp_object = GNUNET_JSONAPI_object_new (); |
774 | if (NULL == handle->ego_head) | 775 | if (NULL == handle->ego_head) |
775 | { | 776 | { |
776 | //Done | 777 | //Done |
diff --git a/src/identity/Makefile.am b/src/identity/Makefile.am index 0a0f08fc3..f902f74fd 100644 --- a/src/identity/Makefile.am +++ b/src/identity/Makefile.am | |||
@@ -67,6 +67,7 @@ libgnunet_plugin_rest_identity_la_SOURCES = \ | |||
67 | libgnunet_plugin_rest_identity_la_LIBADD = \ | 67 | libgnunet_plugin_rest_identity_la_LIBADD = \ |
68 | libgnunetidentity.la \ | 68 | libgnunetidentity.la \ |
69 | $(top_builddir)/src/rest/libgnunetrest.la \ | 69 | $(top_builddir)/src/rest/libgnunetrest.la \ |
70 | $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ | ||
70 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | 71 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ |
71 | $(LTLIBINTL) -ljansson -lmicrohttpd | 72 | $(LTLIBINTL) -ljansson -lmicrohttpd |
72 | libgnunet_plugin_rest_identity_la_LDFLAGS = \ | 73 | libgnunet_plugin_rest_identity_la_LDFLAGS = \ |
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 5bed8af55..b827bfb2b 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include "gnunet_rest_plugin.h" | 28 | #include "gnunet_rest_plugin.h" |
29 | #include "gnunet_identity_service.h" | 29 | #include "gnunet_identity_service.h" |
30 | #include "gnunet_rest_lib.h" | 30 | #include "gnunet_rest_lib.h" |
31 | #include "gnunet_jsonapi_lib.h" | ||
31 | #include "microhttpd.h" | 32 | #include "microhttpd.h" |
32 | #include <jansson.h> | 33 | #include <jansson.h> |
33 | #include "gnunet_signatures.h" | 34 | #include "gnunet_signatures.h" |
@@ -296,14 +297,14 @@ get_ego_for_subsys (void *cls, | |||
296 | const char *name) | 297 | const char *name) |
297 | { | 298 | { |
298 | struct RequestHandle *handle = cls; | 299 | struct RequestHandle *handle = cls; |
299 | struct JsonApiObject *json_object; | 300 | struct GNUNET_JSONAPI_Object *json_object; |
300 | struct JsonApiResource *json_resource; | 301 | struct GNUNET_JSONAPI_Resource *json_resource; |
301 | struct EgoEntry *ego_entry; | 302 | struct EgoEntry *ego_entry; |
302 | struct MHD_Response *resp; | 303 | struct MHD_Response *resp; |
303 | json_t *name_json; | 304 | json_t *name_json; |
304 | char *result_str; | 305 | char *result_str; |
305 | 306 | ||
306 | json_object = GNUNET_REST_jsonapi_object_new (); | 307 | json_object = GNUNET_JSONAPI_object_new (); |
307 | 308 | ||
308 | for (ego_entry = handle->ego_head; | 309 | for (ego_entry = handle->ego_head; |
309 | NULL != ego_entry; | 310 | NULL != ego_entry; |
@@ -313,27 +314,27 @@ get_ego_for_subsys (void *cls, | |||
313 | continue; | 314 | continue; |
314 | if (NULL == name) | 315 | if (NULL == name) |
315 | continue; | 316 | continue; |
316 | json_resource = GNUNET_REST_jsonapi_resource_new | 317 | json_resource = GNUNET_JSONAPI_resource_new |
317 | (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring); | 318 | (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring); |
318 | name_json = json_string (ego_entry->identifier); | 319 | name_json = json_string (ego_entry->identifier); |
319 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 320 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
320 | GNUNET_REST_JSONAPI_IDENTITY_NAME, | 321 | GNUNET_REST_JSONAPI_IDENTITY_NAME, |
321 | name_json); | 322 | name_json); |
322 | json_decref (name_json); | 323 | json_decref (name_json); |
323 | GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); | 324 | GNUNET_JSONAPI_object_resource_add (json_object, json_resource); |
324 | break; | 325 | break; |
325 | } | 326 | } |
326 | if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) | 327 | if (0 == GNUNET_JSONAPI_object_resource_count (json_object)) |
327 | { | 328 | { |
328 | GNUNET_REST_jsonapi_object_delete (json_object); | 329 | GNUNET_JSONAPI_object_delete (json_object); |
329 | handle->emsg = GNUNET_strdup("No identity matches results!"); | 330 | handle->emsg = GNUNET_strdup("No identity matches results!"); |
330 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 331 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
331 | return; | 332 | return; |
332 | } | 333 | } |
333 | GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); | 334 | GNUNET_JSONAPI_data_serialize (json_object, &result_str); |
334 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 335 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
335 | resp = GNUNET_REST_create_json_response (result_str); | 336 | resp = GNUNET_REST_create_json_response (result_str); |
336 | GNUNET_REST_jsonapi_object_delete (json_object); | 337 | GNUNET_JSONAPI_object_delete (json_object); |
337 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 338 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
338 | GNUNET_free (result_str); | 339 | GNUNET_free (result_str); |
339 | cleanup_handle (handle); | 340 | cleanup_handle (handle); |
@@ -359,8 +360,8 @@ ego_info_response (struct RestConnectionDataHandle *con, | |||
359 | struct EgoEntry *ego_entry; | 360 | struct EgoEntry *ego_entry; |
360 | struct GNUNET_HashCode key; | 361 | struct GNUNET_HashCode key; |
361 | struct MHD_Response *resp; | 362 | struct MHD_Response *resp; |
362 | struct JsonApiObject *json_object; | 363 | struct GNUNET_JSONAPI_Object *json_object; |
363 | struct JsonApiResource *json_resource; | 364 | struct GNUNET_JSONAPI_Resource *json_resource; |
364 | json_t *name_str; | 365 | json_t *name_str; |
365 | 366 | ||
366 | if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY)) | 367 | if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY)) |
@@ -409,7 +410,7 @@ ego_info_response (struct RestConnectionDataHandle *con, | |||
409 | } | 410 | } |
410 | } | 411 | } |
411 | 412 | ||
412 | json_object = GNUNET_REST_jsonapi_object_new (); | 413 | json_object = GNUNET_JSONAPI_object_new (); |
413 | 414 | ||
414 | //Return all egos | 415 | //Return all egos |
415 | for (ego_entry = handle->ego_head; | 416 | for (ego_entry = handle->ego_head; |
@@ -418,27 +419,27 @@ ego_info_response (struct RestConnectionDataHandle *con, | |||
418 | { | 419 | { |
419 | if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) | 420 | if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) |
420 | continue; | 421 | continue; |
421 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, | 422 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, |
422 | ego_entry->keystring); | 423 | ego_entry->keystring); |
423 | name_str = json_string (ego_entry->identifier); | 424 | name_str = json_string (ego_entry->identifier); |
424 | GNUNET_REST_jsonapi_resource_add_attr ( | 425 | GNUNET_JSONAPI_resource_add_attr ( |
425 | json_resource, | 426 | json_resource, |
426 | GNUNET_REST_JSONAPI_IDENTITY_NAME, | 427 | GNUNET_REST_JSONAPI_IDENTITY_NAME, |
427 | name_str); | 428 | name_str); |
428 | json_decref (name_str); | 429 | json_decref (name_str); |
429 | GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); | 430 | GNUNET_JSONAPI_object_resource_add (json_object, json_resource); |
430 | } | 431 | } |
431 | if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) | 432 | if (0 == GNUNET_JSONAPI_object_resource_count (json_object)) |
432 | { | 433 | { |
433 | GNUNET_REST_jsonapi_object_delete (json_object); | 434 | GNUNET_JSONAPI_object_delete (json_object); |
434 | handle->emsg = GNUNET_strdup ("No identities found!"); | 435 | handle->emsg = GNUNET_strdup ("No identities found!"); |
435 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 436 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
436 | return; | 437 | return; |
437 | } | 438 | } |
438 | GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); | 439 | GNUNET_JSONAPI_data_serialize (json_object, &result_str); |
439 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 440 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
440 | resp = GNUNET_REST_create_json_response (result_str); | 441 | resp = GNUNET_REST_create_json_response (result_str); |
441 | GNUNET_REST_jsonapi_object_delete (json_object); | 442 | GNUNET_JSONAPI_object_delete (json_object); |
442 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 443 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
443 | GNUNET_free (result_str); | 444 | GNUNET_free (result_str); |
444 | cleanup_handle (handle); | 445 | cleanup_handle (handle); |
@@ -483,8 +484,8 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
483 | struct RequestHandle *handle = cls; | 484 | struct RequestHandle *handle = cls; |
484 | struct EgoEntry *ego_entry; | 485 | struct EgoEntry *ego_entry; |
485 | struct MHD_Response *resp; | 486 | struct MHD_Response *resp; |
486 | struct JsonApiObject *json_obj; | 487 | struct GNUNET_JSONAPI_Object *json_obj; |
487 | struct JsonApiResource *json_res; | 488 | struct GNUNET_JSONAPI_Resource *json_res; |
488 | json_t *egoname_json; | 489 | json_t *egoname_json; |
489 | const char* egoname; | 490 | const char* egoname; |
490 | char term_data[handle->data_size+1]; | 491 | char term_data[handle->data_size+1]; |
@@ -503,32 +504,32 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
503 | } | 504 | } |
504 | term_data[handle->data_size] = '\0'; | 505 | term_data[handle->data_size] = '\0'; |
505 | memcpy (term_data, handle->data, handle->data_size); | 506 | memcpy (term_data, handle->data, handle->data_size); |
506 | json_obj = GNUNET_REST_jsonapi_object_parse (term_data); | 507 | json_obj = GNUNET_JSONAPI_object_parse (term_data); |
507 | if (NULL == json_obj) | 508 | if (NULL == json_obj) |
508 | { | 509 | { |
509 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 510 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
510 | return; | 511 | return; |
511 | } | 512 | } |
512 | if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) | 513 | if (1 != GNUNET_JSONAPI_object_resource_count (json_obj)) |
513 | { | 514 | { |
514 | GNUNET_REST_jsonapi_object_delete (json_obj); | 515 | GNUNET_JSONAPI_object_delete (json_obj); |
515 | handle->emsg = GNUNET_strdup ("Provided resource count invalid"); | 516 | handle->emsg = GNUNET_strdup ("Provided resource count invalid"); |
516 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 517 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
517 | return; | 518 | return; |
518 | } | 519 | } |
519 | json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); | 520 | json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0); |
520 | if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) | 521 | if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) |
521 | { | 522 | { |
522 | GNUNET_REST_jsonapi_object_delete (json_obj); | 523 | GNUNET_JSONAPI_object_delete (json_obj); |
523 | resp = GNUNET_REST_create_json_response (NULL); | 524 | resp = GNUNET_REST_create_json_response (NULL); |
524 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 525 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
525 | cleanup_handle (handle); | 526 | cleanup_handle (handle); |
526 | return; | 527 | return; |
527 | } | 528 | } |
528 | egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME); | 529 | egoname_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME); |
529 | if (!json_is_string (egoname_json)) | 530 | if (!json_is_string (egoname_json)) |
530 | { | 531 | { |
531 | GNUNET_REST_jsonapi_object_delete (json_obj); | 532 | GNUNET_JSONAPI_object_delete (json_obj); |
532 | handle->emsg = GNUNET_strdup ("No name provided"); | 533 | handle->emsg = GNUNET_strdup ("No name provided"); |
533 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 534 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
534 | return; | 535 | return; |
@@ -540,7 +541,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
540 | { | 541 | { |
541 | if (0 == strcasecmp (egoname, ego_entry->identifier)) | 542 | if (0 == strcasecmp (egoname, ego_entry->identifier)) |
542 | { | 543 | { |
543 | GNUNET_REST_jsonapi_object_delete (json_obj); | 544 | GNUNET_JSONAPI_object_delete (json_obj); |
544 | resp = GNUNET_REST_create_json_response (NULL); | 545 | resp = GNUNET_REST_create_json_response (NULL); |
545 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 546 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
546 | cleanup_handle (handle); | 547 | cleanup_handle (handle); |
@@ -548,7 +549,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
548 | } | 549 | } |
549 | } | 550 | } |
550 | GNUNET_asprintf (&handle->name, "%s", egoname); | 551 | GNUNET_asprintf (&handle->name, "%s", egoname); |
551 | GNUNET_REST_jsonapi_object_delete (json_obj); | 552 | GNUNET_JSONAPI_object_delete (json_obj); |
552 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, | 553 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, |
553 | handle->name, | 554 | handle->name, |
554 | &do_finished, | 555 | &do_finished, |
@@ -568,8 +569,8 @@ ego_edit_cont (struct RestConnectionDataHandle *con, | |||
568 | const char *url, | 569 | const char *url, |
569 | void *cls) | 570 | void *cls) |
570 | { | 571 | { |
571 | struct JsonApiObject *json_obj; | 572 | struct GNUNET_JSONAPI_Object *json_obj; |
572 | struct JsonApiResource *json_res; | 573 | struct GNUNET_JSONAPI_Resource *json_res; |
573 | struct RequestHandle *handle = cls; | 574 | struct RequestHandle *handle = cls; |
574 | struct EgoEntry *ego_entry; | 575 | struct EgoEntry *ego_entry; |
575 | struct MHD_Response *resp; | 576 | struct MHD_Response *resp; |
@@ -617,7 +618,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con, | |||
617 | 618 | ||
618 | term_data[handle->data_size] = '\0'; | 619 | term_data[handle->data_size] = '\0'; |
619 | memcpy (term_data, handle->data, handle->data_size); | 620 | memcpy (term_data, handle->data, handle->data_size); |
620 | json_obj = GNUNET_REST_jsonapi_object_parse (term_data); | 621 | json_obj = GNUNET_JSONAPI_object_parse (term_data); |
621 | 622 | ||
622 | if (NULL == json_obj) | 623 | if (NULL == json_obj) |
623 | { | 624 | { |
@@ -626,25 +627,25 @@ ego_edit_cont (struct RestConnectionDataHandle *con, | |||
626 | return; | 627 | return; |
627 | } | 628 | } |
628 | 629 | ||
629 | if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) | 630 | if (1 != GNUNET_JSONAPI_object_resource_count (json_obj)) |
630 | { | 631 | { |
631 | GNUNET_REST_jsonapi_object_delete (json_obj); | 632 | GNUNET_JSONAPI_object_delete (json_obj); |
632 | handle->emsg = GNUNET_strdup ("Resource amount invalid"); | 633 | handle->emsg = GNUNET_strdup ("Resource amount invalid"); |
633 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 634 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
634 | return; | 635 | return; |
635 | } | 636 | } |
636 | json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); | 637 | json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0); |
637 | 638 | ||
638 | if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) | 639 | if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) |
639 | { | 640 | { |
640 | GNUNET_REST_jsonapi_object_delete (json_obj); | 641 | GNUNET_JSONAPI_object_delete (json_obj); |
641 | handle->emsg = GNUNET_strdup ("Resource type invalid"); | 642 | handle->emsg = GNUNET_strdup ("Resource type invalid"); |
642 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 643 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
643 | return; | 644 | return; |
644 | } | 645 | } |
645 | 646 | ||
646 | //This is a rename | 647 | //This is a rename |
647 | name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, | 648 | name_json = GNUNET_JSONAPI_resource_read_attr (json_res, |
648 | GNUNET_REST_JSONAPI_IDENTITY_NEWNAME); | 649 | GNUNET_REST_JSONAPI_IDENTITY_NEWNAME); |
649 | if ((NULL != name_json) && json_is_string (name_json)) | 650 | if ((NULL != name_json) && json_is_string (name_json)) |
650 | { | 651 | { |
@@ -657,7 +658,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con, | |||
657 | 0 != strcasecmp (keystring, ego_entry->keystring)) | 658 | 0 != strcasecmp (keystring, ego_entry->keystring)) |
658 | { | 659 | { |
659 | //Ego with same name not allowed | 660 | //Ego with same name not allowed |
660 | GNUNET_REST_jsonapi_object_delete (json_obj); | 661 | GNUNET_JSONAPI_object_delete (json_obj); |
661 | resp = GNUNET_REST_create_json_response (NULL); | 662 | resp = GNUNET_REST_create_json_response (NULL); |
662 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 663 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
663 | cleanup_handle (handle); | 664 | cleanup_handle (handle); |
@@ -669,17 +670,17 @@ ego_edit_cont (struct RestConnectionDataHandle *con, | |||
669 | newname, | 670 | newname, |
670 | &do_finished, | 671 | &do_finished, |
671 | handle); | 672 | handle); |
672 | GNUNET_REST_jsonapi_object_delete (json_obj); | 673 | GNUNET_JSONAPI_object_delete (json_obj); |
673 | return; | 674 | return; |
674 | } | 675 | } |
675 | 676 | ||
676 | //Set subsystem | 677 | //Set subsystem |
677 | subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM); | 678 | subsys_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM); |
678 | if ( (NULL != subsys_json) && json_is_string (subsys_json)) | 679 | if ( (NULL != subsys_json) && json_is_string (subsys_json)) |
679 | { | 680 | { |
680 | subsys = json_string_value (subsys_json); | 681 | subsys = json_string_value (subsys_json); |
681 | GNUNET_asprintf (&handle->subsys, "%s", subsys); | 682 | GNUNET_asprintf (&handle->subsys, "%s", subsys); |
682 | GNUNET_REST_jsonapi_object_delete (json_obj); | 683 | GNUNET_JSONAPI_object_delete (json_obj); |
683 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, | 684 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, |
684 | handle->subsys, | 685 | handle->subsys, |
685 | ego_entry->ego, | 686 | ego_entry->ego, |
@@ -687,7 +688,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con, | |||
687 | handle); | 688 | handle); |
688 | return; | 689 | return; |
689 | } | 690 | } |
690 | GNUNET_REST_jsonapi_object_delete (json_obj); | 691 | GNUNET_JSONAPI_object_delete (json_obj); |
691 | handle->emsg = GNUNET_strdup ("Subsystem not provided"); | 692 | handle->emsg = GNUNET_strdup ("Subsystem not provided"); |
692 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 693 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
693 | } | 694 | } |
diff --git a/src/include/gnunet_jsonapi_lib.h b/src/include/gnunet_jsonapi_lib.h new file mode 100644 index 000000000..18dfbdf95 --- /dev/null +++ b/src/include/gnunet_jsonapi_lib.h | |||
@@ -0,0 +1,202 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file gnunet_jsonapi_lib.h | ||
18 | * @brief functions to parse jsonapi objects | ||
19 | * @author Martin Schanzenbach | ||
20 | */ | ||
21 | #ifndef GNUNET_JSONAPI_LIB_H | ||
22 | #define GNUNET_JSONAPI_LIB_H | ||
23 | |||
24 | #include "gnunet_util_lib.h" | ||
25 | #include "gnunet_json_lib.h" | ||
26 | |||
27 | |||
28 | /* ****************** JSONAPI parsing ******************* */ | ||
29 | |||
30 | struct GNUNET_JSONAPI_Resource; | ||
31 | |||
32 | struct GNUNET_JSONAPI_Object; | ||
33 | |||
34 | /** | ||
35 | * Specification for parsing a jsonapi object. | ||
36 | * | ||
37 | * @param jsonapi_obj where to store the jsonapi object | ||
38 | */ | ||
39 | struct GNUNET_JSONAPI_Specification | ||
40 | GNUNET_JSONAPI_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_obj); | ||
41 | |||
42 | /** | ||
43 | * Create a JSON API resource | ||
44 | * | ||
45 | * @param type the JSON API resource type | ||
46 | * @param id the JSON API resource id | ||
47 | * @return a new JSON API resource or NULL on error. | ||
48 | */ | ||
49 | struct GNUNET_JSONAPI_Resource* | ||
50 | GNUNET_JSONAPI_resource_new (const char *type, const char *id); | ||
51 | |||
52 | /** | ||
53 | * Delete a JSON API resource | ||
54 | * | ||
55 | * @param res the JSON resource | ||
56 | * @param result Pointer where the resource should be stored | ||
57 | */ | ||
58 | void | ||
59 | GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource); | ||
60 | |||
61 | /** | ||
62 | * Add a JSON API attribute | ||
63 | * | ||
64 | * @param res the JSON resource | ||
65 | * @param key the key for the attribute | ||
66 | * @param json the json_t attribute to add | ||
67 | * @return #GNUNET_OK if added successfully | ||
68 | * #GNUNET_SYSERR if not | ||
69 | */ | ||
70 | int | ||
71 | GNUNET_JSONAPI_resource_add_attr (const struct GNUNET_JSONAPI_Resource *resource, | ||
72 | const char* key, | ||
73 | json_t *json); | ||
74 | /** | ||
75 | * Read a JSON API attribute | ||
76 | * | ||
77 | * @param res the JSON resource | ||
78 | * @param key the key for the attribute | ||
79 | * @return the json attr | ||
80 | */ | ||
81 | json_t* | ||
82 | GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource, | ||
83 | const char* key); | ||
84 | |||
85 | |||
86 | /** | ||
87 | * Check a JSON API resource id | ||
88 | * | ||
89 | * @param res the JSON resource | ||
90 | * @param id the expected id | ||
91 | * @return GNUNET_YES if id matches | ||
92 | */ | ||
93 | int | ||
94 | GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource, | ||
95 | const char* id); | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Check a JSON API resource type | ||
100 | * | ||
101 | * @param res the JSON resource | ||
102 | * @param type the expected type | ||
103 | * @return GNUNET_YES if id matches | ||
104 | */ | ||
105 | int | ||
106 | GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource, | ||
107 | const char* type); | ||
108 | |||
109 | |||
110 | /** | ||
111 | * Create a JSON API primary data | ||
112 | * | ||
113 | * @param type the JSON API resource type | ||
114 | * @param id the JSON API resource id | ||
115 | * @return a new JSON API resource or NULL on error. | ||
116 | */ | ||
117 | struct GNUNET_JSONAPI_Object* | ||
118 | GNUNET_JSONAPI_object_new (); | ||
119 | |||
120 | |||
121 | /** | ||
122 | * Create a JSON API primary data from a string | ||
123 | * | ||
124 | * @param data the string of the JSON API data | ||
125 | * @return a new JSON API resource or NULL on error. | ||
126 | */ | ||
127 | struct GNUNET_JSONAPI_Object* | ||
128 | GNUNET_JSONAPI_object_parse (const char* data); | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Delete a JSON API primary data | ||
133 | * | ||
134 | * @param type the JSON API resource type | ||
135 | * @param id the JSON API resource id | ||
136 | * @return a new JSON API resource or NULL on error. | ||
137 | */ | ||
138 | void | ||
139 | GNUNET_JSONAPI_object_delete (struct GNUNET_JSONAPI_Object *resp); | ||
140 | |||
141 | /** | ||
142 | * Add a JSON API resource to primary data | ||
143 | * | ||
144 | * @param data The JSON API data to add to | ||
145 | * @param res the JSON API resource to add | ||
146 | * @return the new number of resources | ||
147 | */ | ||
148 | void | ||
149 | GNUNET_JSONAPI_object_resource_add (struct GNUNET_JSONAPI_Object *resp, | ||
150 | struct GNUNET_JSONAPI_Resource *res); | ||
151 | /** | ||
152 | * Get a JSON API object resource count | ||
153 | * | ||
154 | * @param resp the JSON API object | ||
155 | * @return the number of resources | ||
156 | */ | ||
157 | int | ||
158 | GNUNET_JSONAPI_object_resource_count (struct GNUNET_JSONAPI_Object *resp); | ||
159 | |||
160 | /** | ||
161 | * Get a JSON API object resource num | ||
162 | * | ||
163 | * @param resp the JSON API object | ||
164 | * @param num the number of the resource | ||
165 | * @return the resource | ||
166 | */ | ||
167 | struct GNUNET_JSONAPI_Resource* | ||
168 | GNUNET_JSONAPI_object_get_resource (struct GNUNET_JSONAPI_Object *resp, int num); | ||
169 | |||
170 | |||
171 | /** | ||
172 | * Add a JSON API resource to primary data | ||
173 | * | ||
174 | * @param resp The JSON API data to add to | ||
175 | * @param res the JSON API resource to add | ||
176 | * @return the new number of resources | ||
177 | */ | ||
178 | void | ||
179 | GNUNET_JSONAPI_data_resource_remove (struct GNUNET_JSONAPI_Object *resp, | ||
180 | struct GNUNET_JSONAPI_Resource *res); | ||
181 | |||
182 | /** | ||
183 | * String serialze jsonapi primary data | ||
184 | * | ||
185 | * @param data the JSON API primary data | ||
186 | * @param result where to store the result | ||
187 | * @return GNUNET_SYSERR on error else GNUNET_OK | ||
188 | */ | ||
189 | int | ||
190 | GNUNET_JSONAPI_data_serialize (const struct GNUNET_JSONAPI_Object *resp, | ||
191 | char **result); | ||
192 | |||
193 | /** | ||
194 | * Check a JSON API resource id | ||
195 | * | ||
196 | * @param res the JSON resource | ||
197 | * @return the resource id | ||
198 | */ | ||
199 | json_t* | ||
200 | GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource); | ||
201 | /* end of gnunet_jsonapi_lib.h */ | ||
202 | #endif | ||
diff --git a/src/include/gnunet_rest_lib.h b/src/include/gnunet_rest_lib.h index a5afae2c9..ea254cc9b 100644 --- a/src/include/gnunet_rest_lib.h +++ b/src/include/gnunet_rest_lib.h | |||
@@ -35,12 +35,6 @@ | |||
35 | #include "microhttpd.h" | 35 | #include "microhttpd.h" |
36 | #include <jansson.h> | 36 | #include <jansson.h> |
37 | 37 | ||
38 | #define GNUNET_REST_JSONAPI_KEY_DATA "data" | ||
39 | |||
40 | #define GNUNET_REST_JSONAPI_KEY_ID "id" | ||
41 | |||
42 | #define GNUNET_REST_JSONAPI_KEY_TYPE "type" | ||
43 | |||
44 | #define GNUNET_REST_HANDLER_END {NULL, NULL, NULL} | 38 | #define GNUNET_REST_HANDLER_END {NULL, NULL, NULL} |
45 | 39 | ||
46 | struct RestConnectionDataHandle | 40 | struct RestConnectionDataHandle |
@@ -86,168 +80,6 @@ typedef void (*GNUNET_REST_ResultProcessor) (void *cls, | |||
86 | struct MHD_Response *resp, | 80 | struct MHD_Response *resp, |
87 | int status); | 81 | int status); |
88 | 82 | ||
89 | |||
90 | /** | ||
91 | * Resource structs for JSON API | ||
92 | */ | ||
93 | struct JsonApiResource; | ||
94 | |||
95 | /** | ||
96 | * Responses for JSON API | ||
97 | */ | ||
98 | struct JsonApiObject; | ||
99 | |||
100 | /** | ||
101 | * Create a JSON API resource | ||
102 | * | ||
103 | * @param type the JSON API resource type | ||
104 | * @param id the JSON API resource id | ||
105 | * @return a new JSON API resource or NULL on error. | ||
106 | */ | ||
107 | struct JsonApiResource* | ||
108 | GNUNET_REST_jsonapi_resource_new (const char *type, const char *id); | ||
109 | |||
110 | /** | ||
111 | * Delete a JSON API resource | ||
112 | * | ||
113 | * @param res the JSON resource | ||
114 | * @param result Pointer where the resource should be stored | ||
115 | */ | ||
116 | void | ||
117 | GNUNET_REST_jsonapi_resource_delete (struct JsonApiResource *resource); | ||
118 | |||
119 | /** | ||
120 | * Add a JSON API attribute | ||
121 | * | ||
122 | * @param res the JSON resource | ||
123 | * @param key the key for the attribute | ||
124 | * @param json the json_t attribute to add | ||
125 | * @return #GNUNET_OK if added successfully | ||
126 | * #GNUNET_SYSERR if not | ||
127 | */ | ||
128 | int | ||
129 | GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource, | ||
130 | const char* key, | ||
131 | json_t *json); | ||
132 | /** | ||
133 | * Read a JSON API attribute | ||
134 | * | ||
135 | * @param res the JSON resource | ||
136 | * @param key the key for the attribute | ||
137 | * @return the json attr | ||
138 | */ | ||
139 | json_t* | ||
140 | GNUNET_REST_jsonapi_resource_read_attr (const struct JsonApiResource *resource, | ||
141 | const char* key); | ||
142 | |||
143 | |||
144 | /** | ||
145 | * Check a JSON API resource id | ||
146 | * | ||
147 | * @param res the JSON resource | ||
148 | * @param id the expected id | ||
149 | * @return GNUNET_YES if id matches | ||
150 | */ | ||
151 | int | ||
152 | GNUNET_REST_jsonapi_resource_check_id (const struct JsonApiResource *resource, | ||
153 | const char* id); | ||
154 | |||
155 | |||
156 | /** | ||
157 | * Check a JSON API resource type | ||
158 | * | ||
159 | * @param res the JSON resource | ||
160 | * @param type the expected type | ||
161 | * @return GNUNET_YES if id matches | ||
162 | */ | ||
163 | int | ||
164 | GNUNET_REST_jsonapi_resource_check_type (const struct JsonApiResource *resource, | ||
165 | const char* type); | ||
166 | |||
167 | |||
168 | /** | ||
169 | * Create a JSON API primary data | ||
170 | * | ||
171 | * @param type the JSON API resource type | ||
172 | * @param id the JSON API resource id | ||
173 | * @return a new JSON API resource or NULL on error. | ||
174 | */ | ||
175 | struct JsonApiObject* | ||
176 | GNUNET_REST_jsonapi_object_new (); | ||
177 | |||
178 | |||
179 | /** | ||
180 | * Create a JSON API primary data from a string | ||
181 | * | ||
182 | * @param data the string of the JSON API data | ||
183 | * @return a new JSON API resource or NULL on error. | ||
184 | */ | ||
185 | struct JsonApiObject* | ||
186 | GNUNET_REST_jsonapi_object_parse (const char* data); | ||
187 | |||
188 | |||
189 | /** | ||
190 | * Delete a JSON API primary data | ||
191 | * | ||
192 | * @param type the JSON API resource type | ||
193 | * @param id the JSON API resource id | ||
194 | * @return a new JSON API resource or NULL on error. | ||
195 | */ | ||
196 | void | ||
197 | GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp); | ||
198 | |||
199 | /** | ||
200 | * Add a JSON API resource to primary data | ||
201 | * | ||
202 | * @param data The JSON API data to add to | ||
203 | * @param res the JSON API resource to add | ||
204 | * @return the new number of resources | ||
205 | */ | ||
206 | void | ||
207 | GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp, | ||
208 | struct JsonApiResource *res); | ||
209 | /** | ||
210 | * Get a JSON API object resource count | ||
211 | * | ||
212 | * @param resp the JSON API object | ||
213 | * @return the number of resources | ||
214 | */ | ||
215 | int | ||
216 | GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp); | ||
217 | |||
218 | /** | ||
219 | * Get a JSON API object resource num | ||
220 | * | ||
221 | * @param resp the JSON API object | ||
222 | * @param num the number of the resource | ||
223 | * @return the resource | ||
224 | */ | ||
225 | struct JsonApiResource* | ||
226 | GNUNET_REST_jsonapi_object_get_resource (struct JsonApiObject *resp, int num); | ||
227 | |||
228 | |||
229 | /** | ||
230 | * Add a JSON API resource to primary data | ||
231 | * | ||
232 | * @param resp The JSON API data to add to | ||
233 | * @param res the JSON API resource to add | ||
234 | * @return the new number of resources | ||
235 | */ | ||
236 | void | ||
237 | GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp, | ||
238 | struct JsonApiResource *res); | ||
239 | |||
240 | /** | ||
241 | * String serialze jsonapi primary data | ||
242 | * | ||
243 | * @param data the JSON API primary data | ||
244 | * @param result where to store the result | ||
245 | * @return GNUNET_SYSERR on error else GNUNET_OK | ||
246 | */ | ||
247 | int | ||
248 | GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp, | ||
249 | char **result); | ||
250 | |||
251 | /** | 83 | /** |
252 | * Check if namespace is in URL. | 84 | * Check if namespace is in URL. |
253 | * | 85 | * |
diff --git a/src/jsonapi/Makefile.am b/src/jsonapi/Makefile.am new file mode 100644 index 000000000..1d3fcc760 --- /dev/null +++ b/src/jsonapi/Makefile.am | |||
@@ -0,0 +1,34 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | if USE_COVERAGE | ||
5 | AM_CFLAGS = --coverage -O0 | ||
6 | XLIB = -lgcov | ||
7 | endif | ||
8 | |||
9 | lib_LTLIBRARIES = \ | ||
10 | libgnunetjsonapi.la | ||
11 | |||
12 | libgnunetjsonapi_la_LDFLAGS = \ | ||
13 | -version-info 0:0:0 \ | ||
14 | -no-undefined | ||
15 | libgnunetjsonapi_la_SOURCES = \ | ||
16 | jsonapi.c | ||
17 | libgnunetjsonapi_la_LIBADD = \ | ||
18 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
19 | $(top_builddir)/src/json/libgnunetjson.la \ | ||
20 | -ljansson \ | ||
21 | $(XLIB) | ||
22 | |||
23 | #check_PROGRAMS = \ | ||
24 | # test_json | ||
25 | |||
26 | #TESTS = \ | ||
27 | # $(check_PROGRAMS) | ||
28 | |||
29 | #test_json_SOURCES = \ | ||
30 | # test_json.c | ||
31 | #test_json_LDADD = \ | ||
32 | # libgnunetjson.la \ | ||
33 | # $(top_builddir)/src/util/libgnunetutil.la \ | ||
34 | # -ljansson | ||
diff --git a/src/jsonapi/jsonapi.c b/src/jsonapi/jsonapi.c new file mode 100644 index 000000000..7bc4c05e5 --- /dev/null +++ b/src/jsonapi/jsonapi.c | |||
@@ -0,0 +1,478 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file json/jsonapi.c | ||
18 | * @brief functions to generate specifciations for JSONAPI parsing | ||
19 | * @author Martin Schanzenbach | ||
20 | */ | ||
21 | #include "platform.h" | ||
22 | #include "gnunet_json_lib.h" | ||
23 | |||
24 | #define GNUNET_JSONAPI_JSONAPI_KEY_DATA "data" | ||
25 | |||
26 | #define GNUNET_JSONAPI_JSONAPI_KEY_ID "id" | ||
27 | |||
28 | #define GNUNET_JSONAPI_JSONAPI_KEY_TYPE "type" | ||
29 | |||
30 | struct GNUNET_JSONAPI_Resource | ||
31 | { | ||
32 | /** | ||
33 | * DLL | ||
34 | */ | ||
35 | struct GNUNET_JSONAPI_Resource *next; | ||
36 | |||
37 | /** | ||
38 | * DLL | ||
39 | */ | ||
40 | struct GNUNET_JSONAPI_Resource *prev; | ||
41 | |||
42 | /** | ||
43 | * Resource content | ||
44 | */ | ||
45 | json_t *res_obj; | ||
46 | }; | ||
47 | |||
48 | |||
49 | struct GNUNET_JSONAPI_Object | ||
50 | { | ||
51 | /** | ||
52 | * DLL Resource | ||
53 | */ | ||
54 | struct GNUNET_JSONAPI_Resource *res_list_head; | ||
55 | |||
56 | /** | ||
57 | * DLL Resource | ||
58 | */ | ||
59 | struct GNUNET_JSONAPI_Resource *res_list_tail; | ||
60 | |||
61 | /** | ||
62 | * num resources | ||
63 | */ | ||
64 | int res_count; | ||
65 | }; | ||
66 | |||
67 | |||
68 | /** | ||
69 | * JSON API | ||
70 | */ | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Create a JSON API resource | ||
75 | * | ||
76 | * @param type the JSON API resource type | ||
77 | * @param id the JSON API resource id | ||
78 | * @return a new JSON API resource or NULL on error. | ||
79 | */ | ||
80 | struct GNUNET_JSONAPI_Resource* | ||
81 | GNUNET_JSONAPI_resource_new (const char *type, const char *id) | ||
82 | { | ||
83 | struct GNUNET_JSONAPI_Resource *res; | ||
84 | |||
85 | if ( (NULL == type) || (0 == strlen (type)) ) | ||
86 | return NULL; | ||
87 | if ( (NULL == id) || (0 == strlen (id)) ) | ||
88 | return NULL; | ||
89 | |||
90 | res = GNUNET_new (struct GNUNET_JSONAPI_Resource); | ||
91 | res->prev = NULL; | ||
92 | res->next = NULL; | ||
93 | |||
94 | res->res_obj = json_object (); | ||
95 | |||
96 | json_object_set_new (res->res_obj, GNUNET_JSONAPI_JSONAPI_KEY_ID, json_string (id)); | ||
97 | json_object_set_new (res->res_obj, GNUNET_JSONAPI_JSONAPI_KEY_TYPE, json_string (type)); | ||
98 | |||
99 | return res; | ||
100 | } | ||
101 | |||
102 | |||
103 | |||
104 | /** | ||
105 | * Add a JSON API attribute | ||
106 | * | ||
107 | * @param res the JSON resource | ||
108 | * @param key the key for the attribute | ||
109 | * @param json the json_t attribute to add | ||
110 | * @return #GNUNET_OK if added successfully | ||
111 | * #GNUNET_SYSERR if not | ||
112 | */ | ||
113 | int | ||
114 | GNUNET_JSONAPI_resource_add_attr (const struct GNUNET_JSONAPI_Resource *resource, | ||
115 | const char* key, | ||
116 | json_t *json) | ||
117 | { | ||
118 | if ( (NULL == resource) || | ||
119 | (NULL == key) || | ||
120 | (NULL == json) ) | ||
121 | return GNUNET_SYSERR; | ||
122 | json_object_set (resource->res_obj, key, json); | ||
123 | return GNUNET_OK; | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * Read a JSON API attribute | ||
128 | * | ||
129 | * @param res the JSON resource | ||
130 | * @param key the key for the attribute | ||
131 | * @return the json_t object | ||
132 | */ | ||
133 | json_t* | ||
134 | GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource, | ||
135 | const char* key) | ||
136 | { | ||
137 | if ( (NULL == resource) || | ||
138 | (NULL == key)) | ||
139 | return NULL; | ||
140 | return json_object_get (resource->res_obj, key); | ||
141 | } | ||
142 | |||
143 | int | ||
144 | check_resource_attr_str (const struct GNUNET_JSONAPI_Resource *resource, | ||
145 | const char* key, | ||
146 | const char* attr) | ||
147 | { | ||
148 | json_t *value; | ||
149 | if ( (NULL == resource) || | ||
150 | (NULL == key) || | ||
151 | (NULL == attr)) | ||
152 | return GNUNET_NO; | ||
153 | value = json_object_get (resource->res_obj, key); | ||
154 | if (NULL == value) | ||
155 | return GNUNET_NO; | ||
156 | if (!json_is_string (value) || | ||
157 | (0 != strcmp (attr, json_string_value(value)))) | ||
158 | { | ||
159 | return GNUNET_NO; | ||
160 | } | ||
161 | return GNUNET_YES; | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * Check a JSON API resource id | ||
166 | * | ||
167 | * @param res the JSON resource | ||
168 | * @param id the expected id | ||
169 | * @return GNUNET_YES if id matches | ||
170 | */ | ||
171 | int | ||
172 | GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource, | ||
173 | const char* id) | ||
174 | { | ||
175 | return check_resource_attr_str (resource, GNUNET_JSONAPI_JSONAPI_KEY_ID, id); | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Check a JSON API resource id | ||
180 | * | ||
181 | * @param res the JSON resource | ||
182 | * @return the resource id | ||
183 | */ | ||
184 | json_t* | ||
185 | GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource) | ||
186 | { | ||
187 | return GNUNET_JSONAPI_resource_read_attr (resource, GNUNET_JSONAPI_JSONAPI_KEY_ID); | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * Check a JSON API resource type | ||
192 | * | ||
193 | * @param res the JSON resource | ||
194 | * @param type the expected type | ||
195 | * @return GNUNET_YES if id matches | ||
196 | */ | ||
197 | int | ||
198 | GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource, | ||
199 | const char* type) | ||
200 | { | ||
201 | return check_resource_attr_str (resource, GNUNET_JSONAPI_JSONAPI_KEY_TYPE, type); | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Get a JSON API object resource count | ||
206 | * | ||
207 | * @param resp the JSON API object | ||
208 | * @return the number of resources | ||
209 | */ | ||
210 | int | ||
211 | GNUNET_JSONAPI_object_resource_count (struct GNUNET_JSONAPI_Object *resp) | ||
212 | { | ||
213 | return resp->res_count; | ||
214 | } | ||
215 | |||
216 | /** | ||
217 | * Get a JSON API object resource by index | ||
218 | * | ||
219 | * @param resp the JSON API object | ||
220 | * @param num the number of the resource | ||
221 | * @return the resource | ||
222 | */ | ||
223 | struct GNUNET_JSONAPI_Resource* | ||
224 | GNUNET_JSONAPI_object_get_resource (struct GNUNET_JSONAPI_Object *resp, | ||
225 | int num) | ||
226 | { | ||
227 | struct GNUNET_JSONAPI_Resource *res; | ||
228 | int i; | ||
229 | |||
230 | if ((0 == resp->res_count) || | ||
231 | (num >= resp->res_count)) | ||
232 | return NULL; | ||
233 | res = resp->res_list_head; | ||
234 | for (i = 0; i < num; i++) | ||
235 | { | ||
236 | res = res->next; | ||
237 | } | ||
238 | return res; | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | * Delete a JSON API resource | ||
243 | * | ||
244 | * @param res the JSON resource | ||
245 | * @param result Pointer where the resource should be stored | ||
246 | */ | ||
247 | void | ||
248 | GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource) | ||
249 | { | ||
250 | json_decref (resource->res_obj); | ||
251 | GNUNET_free (resource); | ||
252 | resource = NULL; | ||
253 | } | ||
254 | |||
255 | /** | ||
256 | * Delete a JSON API primary data | ||
257 | * | ||
258 | * @param type the JSON API resource type | ||
259 | * @param id the JSON API resource id | ||
260 | * @return a new JSON API resource or NULL on error. | ||
261 | */ | ||
262 | void | ||
263 | GNUNET_JSONAPI_object_delete (struct GNUNET_JSONAPI_Object *resp) | ||
264 | { | ||
265 | struct GNUNET_JSONAPI_Resource *res; | ||
266 | struct GNUNET_JSONAPI_Resource *res_next; | ||
267 | |||
268 | for (res = resp->res_list_head; | ||
269 | res != NULL;) | ||
270 | { | ||
271 | res_next = res->next; | ||
272 | GNUNET_CONTAINER_DLL_remove (resp->res_list_head, | ||
273 | resp->res_list_tail, | ||
274 | res); | ||
275 | GNUNET_JSONAPI_resource_delete (res); | ||
276 | res = res_next; | ||
277 | } | ||
278 | GNUNET_free (resp); | ||
279 | resp = NULL; | ||
280 | } | ||
281 | |||
282 | /** | ||
283 | * Create a JSON API primary data | ||
284 | * | ||
285 | * @return a new JSON API resource or NULL on error. | ||
286 | */ | ||
287 | struct GNUNET_JSONAPI_Object* | ||
288 | GNUNET_JSONAPI_jsonapi_object_new () | ||
289 | { | ||
290 | struct GNUNET_JSONAPI_Object *result; | ||
291 | |||
292 | result = GNUNET_new (struct GNUNET_JSONAPI_Object); | ||
293 | result->res_count = 0; | ||
294 | return result; | ||
295 | } | ||
296 | |||
297 | /** | ||
298 | * Add a JSON API object to primary data | ||
299 | * | ||
300 | * @param data The JSON API data to add to | ||
301 | * @param res the JSON API resource to add | ||
302 | * @return the new number of resources | ||
303 | */ | ||
304 | void | ||
305 | GNUNET_JSONAPI_object_resource_add (struct GNUNET_JSONAPI_Object *resp, | ||
306 | struct GNUNET_JSONAPI_Resource *res) | ||
307 | { | ||
308 | GNUNET_CONTAINER_DLL_insert (resp->res_list_head, | ||
309 | resp->res_list_tail, | ||
310 | res); | ||
311 | |||
312 | resp->res_count++; | ||
313 | } | ||
314 | |||
315 | static void | ||
316 | add_json_resource (struct GNUNET_JSONAPI_Object *obj, | ||
317 | const json_t *res_json) | ||
318 | { | ||
319 | struct GNUNET_JSONAPI_Resource *res; | ||
320 | const char *type_json; | ||
321 | |||
322 | struct GNUNET_JSON_Specification dspec[] = { | ||
323 | GNUNET_JSON_spec_string (GNUNET_JSONAPI_JSONAPI_KEY_TYPE, &type_json), | ||
324 | GNUNET_JSON_spec_end() | ||
325 | }; | ||
326 | |||
327 | GNUNET_assert (GNUNET_OK == | ||
328 | GNUNET_JSON_parse (res_json, dspec, | ||
329 | NULL, NULL)); | ||
330 | GNUNET_JSON_parse_free (dspec); | ||
331 | res = GNUNET_new (struct GNUNET_JSONAPI_Resource); | ||
332 | res->next = NULL; | ||
333 | res->prev = NULL; | ||
334 | res->res_obj = json_deep_copy (res_json); | ||
335 | GNUNET_JSONAPI_object_resource_add (obj, res); | ||
336 | } | ||
337 | |||
338 | /** | ||
339 | * Parse given JSON object to RSA public key. | ||
340 | * | ||
341 | * @param cls closure, NULL | ||
342 | * @param root the json object representing data | ||
343 | * @param[out] spec where to write the data | ||
344 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
345 | */ | ||
346 | static int | ||
347 | parse_jsonapiobject (void *cls, | ||
348 | json_t *root, | ||
349 | struct GNUNET_JSON_Specification *spec) | ||
350 | { | ||
351 | struct GNUNET_JSONAPI_Object *result; | ||
352 | json_t *data_json; | ||
353 | int res_count = 0; | ||
354 | int i; | ||
355 | |||
356 | struct GNUNET_JSON_Specification jsonapispec[] = { | ||
357 | GNUNET_JSON_spec_json (GNUNET_JSONAPI_JSONAPI_KEY_DATA, &data_json), | ||
358 | GNUNET_JSON_spec_end() | ||
359 | }; | ||
360 | if (GNUNET_OK != | ||
361 | GNUNET_JSON_parse (root, jsonapispec, | ||
362 | NULL, NULL) || (NULL == data_json)) | ||
363 | { | ||
364 | return GNUNET_SYSERR; | ||
365 | } | ||
366 | |||
367 | result = GNUNET_new (struct GNUNET_JSONAPI_Object); | ||
368 | result->res_count = 0; | ||
369 | if (json_is_object (data_json)) | ||
370 | add_json_resource (result, data_json); | ||
371 | else if (json_is_array (data_json)) | ||
372 | { | ||
373 | res_count = json_array_size (data_json); | ||
374 | for (i = 0; i < res_count; i++) | ||
375 | add_json_resource (result, json_array_get (data_json, i)); | ||
376 | } | ||
377 | if (0 == result->res_count) | ||
378 | { | ||
379 | GNUNET_free (result); | ||
380 | GNUNET_JSON_parse_free (jsonapispec); | ||
381 | return GNUNET_SYSERR; | ||
382 | } | ||
383 | *(struct GNUNET_JSONAPI_Object **) spec->ptr = result; | ||
384 | GNUNET_JSON_parse_free (jsonapispec); | ||
385 | return GNUNET_OK; | ||
386 | } | ||
387 | |||
388 | |||
389 | /** | ||
390 | * Cleanup data left from parsing RSA public key. | ||
391 | * | ||
392 | * @param cls closure, NULL | ||
393 | * @param[out] spec where to free the data | ||
394 | */ | ||
395 | static void | ||
396 | clean_jsonapiobject (void *cls, | ||
397 | struct GNUNET_JSON_Specification *spec) | ||
398 | { | ||
399 | struct GNUNET_JSONAPI_Object **jsonapi_obj; | ||
400 | jsonapi_obj = (struct GNUNET_JSONAPI_Object **) spec->ptr; | ||
401 | if (NULL != *jsonapi_obj) | ||
402 | { | ||
403 | GNUNET_JSONAPI_object_delete (*jsonapi_obj); | ||
404 | *jsonapi_obj = NULL; | ||
405 | } | ||
406 | } | ||
407 | |||
408 | /** | ||
409 | * Add a JSON API resource to primary data | ||
410 | * | ||
411 | * @param data The JSON API data to add to | ||
412 | * @param res the JSON API resource to add | ||
413 | * @return the new number of resources | ||
414 | */ | ||
415 | void | ||
416 | GNUNET_JSONAPI_data_resource_remove (struct GNUNET_JSONAPI_Object *resp, | ||
417 | struct GNUNET_JSONAPI_Resource *res) | ||
418 | { | ||
419 | GNUNET_CONTAINER_DLL_remove (resp->res_list_head, | ||
420 | resp->res_list_tail, | ||
421 | res); | ||
422 | resp->res_count--; | ||
423 | } | ||
424 | |||
425 | /** | ||
426 | * String serialze jsonapi primary data | ||
427 | * | ||
428 | * @param data the JSON API primary data | ||
429 | * @param result where to store the result | ||
430 | * @return GNUNET_SYSERR on error else GNUNET_OK | ||
431 | */ | ||
432 | int | ||
433 | GNUNET_JSONAPI_data_serialize (const struct GNUNET_JSONAPI_Object *resp, | ||
434 | char **result) | ||
435 | { | ||
436 | struct GNUNET_JSONAPI_Resource *res; | ||
437 | json_t *root_json; | ||
438 | json_t *res_arr; | ||
439 | |||
440 | if ((NULL == resp)) | ||
441 | return GNUNET_SYSERR; | ||
442 | |||
443 | root_json = json_object (); | ||
444 | res_arr = json_array (); | ||
445 | for (res = resp->res_list_head; | ||
446 | res != NULL; | ||
447 | res = res->next) | ||
448 | { | ||
449 | json_array_append (res_arr, res->res_obj); | ||
450 | } | ||
451 | json_object_set (root_json, GNUNET_JSONAPI_JSONAPI_KEY_DATA, res_arr); | ||
452 | *result = json_dumps (root_json, JSON_INDENT(2)); | ||
453 | json_decref (root_json); | ||
454 | json_decref (res_arr); | ||
455 | return GNUNET_OK; | ||
456 | } | ||
457 | |||
458 | /** | ||
459 | * JSON object. | ||
460 | * | ||
461 | * @param name name of the JSON field | ||
462 | * @param[out] jsonp where to store the JSON found under @a name | ||
463 | */ | ||
464 | struct GNUNET_JSON_Specification | ||
465 | GNUNET_JSON_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_object) | ||
466 | { | ||
467 | struct GNUNET_JSON_Specification ret = { | ||
468 | .parser = &parse_jsonapiobject, | ||
469 | .cleaner = &clean_jsonapiobject, | ||
470 | .cls = NULL, | ||
471 | .field = NULL, | ||
472 | .ptr = jsonapi_object, | ||
473 | .ptr_size = 0, | ||
474 | .size_ptr = NULL | ||
475 | }; | ||
476 | *jsonapi_object = NULL; | ||
477 | return ret; | ||
478 | } | ||
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am index 887dea4ed..cf47e8507 100644 --- a/src/namestore/Makefile.am +++ b/src/namestore/Makefile.am | |||
@@ -192,6 +192,7 @@ libgnunet_plugin_rest_namestore_la_LIBADD = \ | |||
192 | libgnunetnamestore.la \ | 192 | libgnunetnamestore.la \ |
193 | $(top_builddir)/src/rest/libgnunetrest.la \ | 193 | $(top_builddir)/src/rest/libgnunetrest.la \ |
194 | $(top_builddir)/src/identity/libgnunetidentity.la \ | 194 | $(top_builddir)/src/identity/libgnunetidentity.la \ |
195 | $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ | ||
195 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | 196 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ |
196 | $(LTLIBINTL) -ljansson -lmicrohttpd | 197 | $(LTLIBINTL) -ljansson -lmicrohttpd |
197 | libgnunet_plugin_rest_namestore_la_LDFLAGS = \ | 198 | libgnunet_plugin_rest_namestore_la_LDFLAGS = \ |
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index 4612528c6..d33bf483b 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #include "gnunet_namestore_service.h" | 29 | #include "gnunet_namestore_service.h" |
30 | #include "gnunet_identity_service.h" | 30 | #include "gnunet_identity_service.h" |
31 | #include "gnunet_rest_lib.h" | 31 | #include "gnunet_rest_lib.h" |
32 | #include "gnunet_jsonapi_lib.h" | ||
32 | #include "microhttpd.h" | 33 | #include "microhttpd.h" |
33 | #include <jansson.h> | 34 | #include <jansson.h> |
34 | 35 | ||
@@ -105,7 +106,7 @@ struct RequestHandle | |||
105 | /** | 106 | /** |
106 | * JSON response object | 107 | * JSON response object |
107 | */ | 108 | */ |
108 | struct JsonApiObject *resp_object; | 109 | struct GNUNET_JSONAPI_Object *resp_object; |
109 | 110 | ||
110 | /** | 111 | /** |
111 | * Rest connection | 112 | * Rest connection |
@@ -255,7 +256,7 @@ cleanup_handle (struct RequestHandle *handle) | |||
255 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 256 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
256 | "Cleaning up\n"); | 257 | "Cleaning up\n"); |
257 | if (NULL != handle->resp_object) | 258 | if (NULL != handle->resp_object) |
258 | GNUNET_REST_jsonapi_object_delete (handle->resp_object); | 259 | GNUNET_JSONAPI_object_delete (handle->resp_object); |
259 | if (NULL != handle->name) | 260 | if (NULL != handle->name) |
260 | GNUNET_free (handle->name); | 261 | GNUNET_free (handle->name); |
261 | if (NULL != handle->timeout_task) | 262 | if (NULL != handle->timeout_task) |
@@ -403,7 +404,7 @@ namestore_list_response (void *cls, | |||
403 | const struct GNUNET_GNSRECORD_Data *rd) | 404 | const struct GNUNET_GNSRECORD_Data *rd) |
404 | { | 405 | { |
405 | struct RequestHandle *handle = cls; | 406 | struct RequestHandle *handle = cls; |
406 | struct JsonApiResource *json_resource; | 407 | struct GNUNET_JSONAPI_Resource *json_resource; |
407 | struct MHD_Response *resp; | 408 | struct MHD_Response *resp; |
408 | json_t *result_array; | 409 | json_t *result_array; |
409 | json_t *record_obj; | 410 | json_t *record_obj; |
@@ -411,13 +412,13 @@ namestore_list_response (void *cls, | |||
411 | char *result; | 412 | char *result; |
412 | 413 | ||
413 | if (NULL == handle->resp_object) | 414 | if (NULL == handle->resp_object) |
414 | handle->resp_object = GNUNET_REST_jsonapi_object_new (); | 415 | handle->resp_object = GNUNET_JSONAPI_object_new (); |
415 | 416 | ||
416 | if (NULL == rname) | 417 | if (NULL == rname) |
417 | { | 418 | { |
418 | handle->list_it = NULL; | 419 | handle->list_it = NULL; |
419 | //Handle response | 420 | //Handle response |
420 | if (GNUNET_SYSERR == GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result)) | 421 | if (GNUNET_SYSERR == GNUNET_JSONAPI_data_serialize (handle->resp_object, &result)) |
421 | { | 422 | { |
422 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 423 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
423 | return; | 424 | return; |
@@ -455,12 +456,12 @@ namestore_list_response (void *cls, | |||
455 | 456 | ||
456 | if (0 < json_array_size(result_array)) | 457 | if (0 < json_array_size(result_array)) |
457 | { | 458 | { |
458 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO, | 459 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO, |
459 | rname); | 460 | rname); |
460 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 461 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
461 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD, | 462 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD, |
462 | result_array); | 463 | result_array); |
463 | GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource); | 464 | GNUNET_JSONAPI_object_resource_add (handle->resp_object, json_resource); |
464 | } | 465 | } |
465 | 466 | ||
466 | json_decref (result_array); | 467 | json_decref (result_array); |
@@ -723,8 +724,8 @@ namestore_create_cont (struct RestConnectionDataHandle *con, | |||
723 | { | 724 | { |
724 | struct RequestHandle *handle = cls; | 725 | struct RequestHandle *handle = cls; |
725 | struct MHD_Response *resp; | 726 | struct MHD_Response *resp; |
726 | struct JsonApiObject *json_obj; | 727 | struct GNUNET_JSONAPI_Object *json_obj; |
727 | struct JsonApiResource *json_res; | 728 | struct GNUNET_JSONAPI_Resource *json_res; |
728 | json_t *name_json; | 729 | json_t *name_json; |
729 | json_t *records_json; | 730 | json_t *records_json; |
730 | char term_data[handle->data_size+1]; | 731 | char term_data[handle->data_size+1]; |
@@ -743,7 +744,7 @@ namestore_create_cont (struct RestConnectionDataHandle *con, | |||
743 | } | 744 | } |
744 | term_data[handle->data_size] = '\0'; | 745 | term_data[handle->data_size] = '\0'; |
745 | memcpy (term_data, handle->data, handle->data_size); | 746 | memcpy (term_data, handle->data, handle->data_size); |
746 | json_obj = GNUNET_REST_jsonapi_object_parse (term_data); | 747 | json_obj = GNUNET_JSONAPI_object_parse (term_data); |
747 | if (NULL == json_obj) | 748 | if (NULL == json_obj) |
748 | { | 749 | { |
749 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 750 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -752,54 +753,54 @@ namestore_create_cont (struct RestConnectionDataHandle *con, | |||
752 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 753 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
753 | return; | 754 | return; |
754 | } | 755 | } |
755 | if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) | 756 | if (1 != GNUNET_JSONAPI_object_resource_count (json_obj)) |
756 | { | 757 | { |
757 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 758 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
758 | "Cannot create more than 1 resource! (Got %d)\n", | 759 | "Cannot create more than 1 resource! (Got %d)\n", |
759 | GNUNET_REST_jsonapi_object_resource_count (json_obj)); | 760 | GNUNET_JSONAPI_object_resource_count (json_obj)); |
760 | GNUNET_REST_jsonapi_object_delete (json_obj); | 761 | GNUNET_JSONAPI_object_delete (json_obj); |
761 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 762 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
762 | return; | 763 | return; |
763 | } | 764 | } |
764 | json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); | 765 | json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0); |
765 | if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, | 766 | if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, |
766 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD)) | 767 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD)) |
767 | { | 768 | { |
768 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 769 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
769 | "Unsupported JSON data type\n"); | 770 | "Unsupported JSON data type\n"); |
770 | GNUNET_REST_jsonapi_object_delete (json_obj); | 771 | GNUNET_JSONAPI_object_delete (json_obj); |
771 | resp = GNUNET_REST_create_json_response (NULL); | 772 | resp = GNUNET_REST_create_json_response (NULL); |
772 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 773 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
773 | cleanup_handle (handle); | 774 | cleanup_handle (handle); |
774 | return; | 775 | return; |
775 | } | 776 | } |
776 | name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_KEY_ID); | 777 | name_json = GNUNET_JSONAPI_resource_get_id (json_res); |
777 | if (!json_is_string (name_json)) | 778 | if (!json_is_string (name_json)) |
778 | { | 779 | { |
779 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 780 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
780 | "Name property is no string\n"); | 781 | "Name property is no string\n"); |
781 | GNUNET_REST_jsonapi_object_delete (json_obj); | 782 | GNUNET_JSONAPI_object_delete (json_obj); |
782 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 783 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
783 | return; | 784 | return; |
784 | } | 785 | } |
785 | GNUNET_asprintf (&handle->name, "%s", json_string_value (name_json)); | 786 | GNUNET_asprintf (&handle->name, "%s", json_string_value (name_json)); |
786 | records_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, | 787 | records_json = GNUNET_JSONAPI_resource_read_attr (json_res, |
787 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD); | 788 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD); |
788 | if (NULL == records_json) | 789 | if (NULL == records_json) |
789 | { | 790 | { |
790 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 791 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
791 | "No records given\n"); | 792 | "No records given\n"); |
792 | GNUNET_REST_jsonapi_object_delete (json_obj); | 793 | GNUNET_JSONAPI_object_delete (json_obj); |
793 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 794 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
794 | return; | 795 | return; |
795 | } | 796 | } |
796 | if (GNUNET_SYSERR == json_to_gnsrecord (records_json, &handle->rd, &handle->rd_count)) | 797 | if (GNUNET_SYSERR == json_to_gnsrecord (records_json, &handle->rd, &handle->rd_count)) |
797 | { | 798 | { |
798 | GNUNET_REST_jsonapi_object_delete (json_obj); | 799 | GNUNET_JSONAPI_object_delete (json_obj); |
799 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 800 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
800 | return; | 801 | return; |
801 | } | 802 | } |
802 | GNUNET_REST_jsonapi_object_delete (json_obj); | 803 | GNUNET_JSONAPI_object_delete (json_obj); |
803 | 804 | ||
804 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | 805 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, |
805 | &handle->zone_pkey, | 806 | &handle->zone_pkey, |
@@ -816,34 +817,34 @@ namestore_zkey_response (void *cls, | |||
816 | { | 817 | { |
817 | struct RequestHandle *handle = cls; | 818 | struct RequestHandle *handle = cls; |
818 | struct MHD_Response *resp; | 819 | struct MHD_Response *resp; |
819 | struct JsonApiObject *json_obj; | 820 | struct GNUNET_JSONAPI_Object *json_obj; |
820 | struct JsonApiResource *json_res; | 821 | struct GNUNET_JSONAPI_Resource *json_res; |
821 | json_t *name_json; | 822 | json_t *name_json; |
822 | char* result; | 823 | char* result; |
823 | 824 | ||
824 | handle->reverse_qe = NULL; | 825 | handle->reverse_qe = NULL; |
825 | json_obj = GNUNET_REST_jsonapi_object_new (); | 826 | json_obj = GNUNET_JSONAPI_object_new (); |
826 | if (NULL != label) | 827 | if (NULL != label) |
827 | { | 828 | { |
828 | name_json = json_string (label); | 829 | name_json = json_string (label); |
829 | json_res = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO, | 830 | json_res = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO, |
830 | handle->zkey_str); | 831 | handle->zkey_str); |
831 | GNUNET_REST_jsonapi_resource_add_attr (json_res, | 832 | GNUNET_JSONAPI_resource_add_attr (json_res, |
832 | GNUNET_REST_JSONAPI_NAMESTORE_NAME, | 833 | GNUNET_REST_JSONAPI_NAMESTORE_NAME, |
833 | name_json); | 834 | name_json); |
834 | GNUNET_REST_jsonapi_object_resource_add (json_obj, json_res); | 835 | GNUNET_JSONAPI_object_resource_add (json_obj, json_res); |
835 | json_decref (name_json); | 836 | json_decref (name_json); |
836 | } | 837 | } |
837 | //Handle response | 838 | //Handle response |
838 | if (GNUNET_SYSERR == GNUNET_REST_jsonapi_data_serialize (json_obj, &result)) | 839 | if (GNUNET_SYSERR == GNUNET_JSONAPI_data_serialize (json_obj, &result)) |
839 | { | 840 | { |
840 | GNUNET_REST_jsonapi_object_delete (json_obj); | 841 | GNUNET_JSONAPI_object_delete (json_obj); |
841 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 842 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
842 | return; | 843 | return; |
843 | } | 844 | } |
844 | resp = GNUNET_REST_create_json_response (result); | 845 | resp = GNUNET_REST_create_json_response (result); |
845 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 846 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
846 | GNUNET_REST_jsonapi_object_delete (json_obj); | 847 | GNUNET_JSONAPI_object_delete (json_obj); |
847 | GNUNET_free (result); | 848 | GNUNET_free (result); |
848 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | 849 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); |
849 | return; | 850 | return; |
diff --git a/src/rest/rest.c b/src/rest/rest.c index aa0ab7ccd..a17955314 100644 --- a/src/rest/rest.c +++ b/src/rest/rest.c | |||
@@ -30,397 +30,6 @@ | |||
30 | #include "microhttpd.h" | 30 | #include "microhttpd.h" |
31 | #include <jansson.h> | 31 | #include <jansson.h> |
32 | 32 | ||
33 | |||
34 | struct JsonApiResource | ||
35 | { | ||
36 | /** | ||
37 | * DLL | ||
38 | */ | ||
39 | struct JsonApiResource *next; | ||
40 | |||
41 | /** | ||
42 | * DLL | ||
43 | */ | ||
44 | struct JsonApiResource *prev; | ||
45 | |||
46 | /** | ||
47 | * Resource content | ||
48 | */ | ||
49 | json_t *res_obj; | ||
50 | }; | ||
51 | |||
52 | |||
53 | struct JsonApiObject | ||
54 | { | ||
55 | /** | ||
56 | * DLL Resource | ||
57 | */ | ||
58 | struct JsonApiResource *res_list_head; | ||
59 | |||
60 | /** | ||
61 | * DLL Resource | ||
62 | */ | ||
63 | struct JsonApiResource *res_list_tail; | ||
64 | |||
65 | /** | ||
66 | * num resources | ||
67 | */ | ||
68 | int res_count; | ||
69 | }; | ||
70 | |||
71 | |||
72 | /** | ||
73 | * JSON API | ||
74 | */ | ||
75 | |||
76 | /** | ||
77 | * Create a JSON API resource | ||
78 | * | ||
79 | * @param type the JSON API resource type | ||
80 | * @param id the JSON API resource id | ||
81 | * @return a new JSON API resource or NULL on error. | ||
82 | */ | ||
83 | struct JsonApiResource* | ||
84 | GNUNET_REST_jsonapi_resource_new (const char *type, const char *id) | ||
85 | { | ||
86 | struct JsonApiResource *res; | ||
87 | |||
88 | if ( (NULL == type) || (0 == strlen (type)) ) | ||
89 | return NULL; | ||
90 | if ( (NULL == id) || (0 == strlen (id)) ) | ||
91 | return NULL; | ||
92 | |||
93 | res = GNUNET_new (struct JsonApiResource); | ||
94 | res->prev = NULL; | ||
95 | res->next = NULL; | ||
96 | |||
97 | res->res_obj = json_object (); | ||
98 | |||
99 | json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_ID, json_string (id)); | ||
100 | json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_TYPE, json_string (type)); | ||
101 | |||
102 | return res; | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Delete a JSON API resource | ||
107 | * | ||
108 | * @param res the JSON resource | ||
109 | * @param result Pointer where the resource should be stored | ||
110 | */ | ||
111 | void | ||
112 | GNUNET_REST_jsonapi_resource_delete (struct JsonApiResource *resource) | ||
113 | { | ||
114 | json_decref (resource->res_obj); | ||
115 | GNUNET_free (resource); | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * Add a JSON API attribute | ||
120 | * | ||
121 | * @param res the JSON resource | ||
122 | * @param key the key for the attribute | ||
123 | * @param json the json_t attribute to add | ||
124 | * @return #GNUNET_OK if added successfully | ||
125 | * #GNUNET_SYSERR if not | ||
126 | */ | ||
127 | int | ||
128 | GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource, | ||
129 | const char* key, | ||
130 | json_t *json) | ||
131 | { | ||
132 | if ( (NULL == resource) || | ||
133 | (NULL == key) || | ||
134 | (NULL == json) ) | ||
135 | return GNUNET_SYSERR; | ||
136 | json_object_set (resource->res_obj, key, json); | ||
137 | return GNUNET_OK; | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * Read a JSON API attribute | ||
142 | * | ||
143 | * @param res the JSON resource | ||
144 | * @param key the key for the attribute | ||
145 | * @return the json_t object | ||
146 | */ | ||
147 | json_t* | ||
148 | GNUNET_REST_jsonapi_resource_read_attr (const struct JsonApiResource *resource, | ||
149 | const char* key) | ||
150 | { | ||
151 | if ( (NULL == resource) || | ||
152 | (NULL == key)) | ||
153 | return NULL; | ||
154 | return json_object_get (resource->res_obj, key); | ||
155 | } | ||
156 | |||
157 | int | ||
158 | check_resource_attr_str (const struct JsonApiResource *resource, | ||
159 | const char* key, | ||
160 | const char* attr) | ||
161 | { | ||
162 | json_t *value; | ||
163 | if ( (NULL == resource) || | ||
164 | (NULL == key) || | ||
165 | (NULL == attr)) | ||
166 | return GNUNET_NO; | ||
167 | value = json_object_get (resource->res_obj, key); | ||
168 | if (NULL == value) | ||
169 | return GNUNET_NO; | ||
170 | if (!json_is_string (value) || | ||
171 | (0 != strcmp (attr, json_string_value(value)))) | ||
172 | { | ||
173 | return GNUNET_NO; | ||
174 | } | ||
175 | return GNUNET_YES; | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Check a JSON API resource id | ||
180 | * | ||
181 | * @param res the JSON resource | ||
182 | * @param id the expected id | ||
183 | * @return GNUNET_YES if id matches | ||
184 | */ | ||
185 | int | ||
186 | GNUNET_REST_jsonapi_resource_check_id (const struct JsonApiResource *resource, | ||
187 | const char* id) | ||
188 | { | ||
189 | return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_ID, id); | ||
190 | } | ||
191 | |||
192 | |||
193 | /** | ||
194 | * Check a JSON API resource type | ||
195 | * | ||
196 | * @param res the JSON resource | ||
197 | * @param type the expected type | ||
198 | * @return GNUNET_YES if id matches | ||
199 | */ | ||
200 | int | ||
201 | GNUNET_REST_jsonapi_resource_check_type (const struct JsonApiResource *resource, | ||
202 | const char* type) | ||
203 | { | ||
204 | return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_TYPE, type); | ||
205 | } | ||
206 | |||
207 | |||
208 | /** | ||
209 | * Create a JSON API primary data | ||
210 | * | ||
211 | * @return a new JSON API resource or NULL on error. | ||
212 | */ | ||
213 | struct JsonApiObject* | ||
214 | GNUNET_REST_jsonapi_object_new () | ||
215 | { | ||
216 | struct JsonApiObject *result; | ||
217 | |||
218 | result = GNUNET_new (struct JsonApiObject); | ||
219 | result->res_count = 0; | ||
220 | return result; | ||
221 | } | ||
222 | |||
223 | |||
224 | static void | ||
225 | add_json_resource (struct JsonApiObject *obj, | ||
226 | const json_t *res_json) | ||
227 | { | ||
228 | struct JsonApiResource *res; | ||
229 | json_t *type_json; | ||
230 | |||
231 | type_json = json_object_get (res_json, GNUNET_REST_JSONAPI_KEY_TYPE); | ||
232 | if (!json_is_string (type_json)) | ||
233 | return; | ||
234 | res = GNUNET_new (struct JsonApiResource); | ||
235 | res->next = NULL; | ||
236 | res->prev = NULL; | ||
237 | res->res_obj = json_deep_copy (res_json); | ||
238 | GNUNET_REST_jsonapi_object_resource_add (obj, res); | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | * Create a JSON API primary data from a string | ||
243 | * | ||
244 | * @param data the string of the JSON API data | ||
245 | * @return a new JSON API resource or NULL on error. | ||
246 | */ | ||
247 | struct JsonApiObject* | ||
248 | GNUNET_REST_jsonapi_object_parse (const char* data) | ||
249 | { | ||
250 | struct JsonApiObject *result; | ||
251 | json_t *root_json; | ||
252 | json_t *data_json; | ||
253 | json_error_t error; | ||
254 | int res_count = 0; | ||
255 | int i; | ||
256 | if (NULL == data) | ||
257 | return NULL; | ||
258 | root_json = json_loads (data, 0, &error); | ||
259 | |||
260 | if ( (NULL == root_json) || !json_is_object (root_json)) | ||
261 | { | ||
262 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "json error: %s", error.text); | ||
263 | return NULL; | ||
264 | } | ||
265 | data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA); | ||
266 | if (NULL == data_json) | ||
267 | { | ||
268 | json_decref (root_json); | ||
269 | return NULL; | ||
270 | } | ||
271 | |||
272 | result = GNUNET_new (struct JsonApiObject); | ||
273 | result->res_count = 0; | ||
274 | if (json_is_object (data_json)) | ||
275 | add_json_resource (result, data_json); | ||
276 | else if (json_is_array (data_json)) | ||
277 | { | ||
278 | res_count = json_array_size (data_json); | ||
279 | for (i = 0; i < res_count; i++) | ||
280 | add_json_resource (result, json_array_get (data_json, i)); | ||
281 | } | ||
282 | json_decref (root_json); | ||
283 | if (0 == result->res_count) | ||
284 | { | ||
285 | GNUNET_free (result); | ||
286 | result = NULL; | ||
287 | } | ||
288 | return result; | ||
289 | } | ||
290 | |||
291 | |||
292 | /** | ||
293 | * Delete a JSON API primary data | ||
294 | * | ||
295 | * @param type the JSON API resource type | ||
296 | * @param id the JSON API resource id | ||
297 | * @return a new JSON API resource or NULL on error. | ||
298 | */ | ||
299 | void | ||
300 | GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp) | ||
301 | { | ||
302 | struct JsonApiResource *res; | ||
303 | struct JsonApiResource *res_next; | ||
304 | |||
305 | for (res = resp->res_list_head; | ||
306 | res != NULL;) | ||
307 | { | ||
308 | res_next = res->next; | ||
309 | GNUNET_CONTAINER_DLL_remove (resp->res_list_head, | ||
310 | resp->res_list_tail, | ||
311 | res); | ||
312 | GNUNET_REST_jsonapi_resource_delete (res); | ||
313 | res = res_next; | ||
314 | } | ||
315 | GNUNET_free (resp); | ||
316 | } | ||
317 | |||
318 | /** | ||
319 | * Add a JSON API object to primary data | ||
320 | * | ||
321 | * @param data The JSON API data to add to | ||
322 | * @param res the JSON API resource to add | ||
323 | * @return the new number of resources | ||
324 | */ | ||
325 | void | ||
326 | GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp, | ||
327 | struct JsonApiResource *res) | ||
328 | { | ||
329 | GNUNET_CONTAINER_DLL_insert (resp->res_list_head, | ||
330 | resp->res_list_tail, | ||
331 | res); | ||
332 | |||
333 | resp->res_count++; | ||
334 | } | ||
335 | |||
336 | |||
337 | /** | ||
338 | * Get a JSON API object resource count | ||
339 | * | ||
340 | * @param resp the JSON API object | ||
341 | * @return the number of resources | ||
342 | */ | ||
343 | int | ||
344 | GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp) | ||
345 | { | ||
346 | return resp->res_count; | ||
347 | } | ||
348 | |||
349 | /** | ||
350 | * Get a JSON API object resource num | ||
351 | * | ||
352 | * @param resp the JSON API object | ||
353 | * @param num the number of the resource | ||
354 | * @return the resource | ||
355 | */ | ||
356 | struct JsonApiResource* | ||
357 | GNUNET_REST_jsonapi_object_get_resource (struct JsonApiObject *resp, int num) | ||
358 | { | ||
359 | struct JsonApiResource *res; | ||
360 | int i; | ||
361 | |||
362 | if ((0 == resp->res_count) || | ||
363 | (num >= resp->res_count)) | ||
364 | return NULL; | ||
365 | res = resp->res_list_head; | ||
366 | for (i = 0; i < num; i++) | ||
367 | { | ||
368 | res = res->next; | ||
369 | } | ||
370 | return res; | ||
371 | } | ||
372 | |||
373 | |||
374 | /** | ||
375 | * Add a JSON API resource to primary data | ||
376 | * | ||
377 | * @param data The JSON API data to add to | ||
378 | * @param res the JSON API resource to add | ||
379 | * @return the new number of resources | ||
380 | */ | ||
381 | void | ||
382 | GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp, | ||
383 | struct JsonApiResource *res) | ||
384 | { | ||
385 | GNUNET_CONTAINER_DLL_remove (resp->res_list_head, | ||
386 | resp->res_list_tail, | ||
387 | res); | ||
388 | resp->res_count--; | ||
389 | } | ||
390 | |||
391 | /** | ||
392 | * String serialze jsonapi primary data | ||
393 | * | ||
394 | * @param data the JSON API primary data | ||
395 | * @param result where to store the result | ||
396 | * @return GNUNET_SYSERR on error else GNUNET_OK | ||
397 | */ | ||
398 | int | ||
399 | GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp, | ||
400 | char **result) | ||
401 | { | ||
402 | struct JsonApiResource *res; | ||
403 | json_t *root_json; | ||
404 | json_t *res_arr; | ||
405 | |||
406 | if ((NULL == resp)) | ||
407 | return GNUNET_SYSERR; | ||
408 | |||
409 | root_json = json_object (); | ||
410 | res_arr = json_array (); | ||
411 | for (res = resp->res_list_head; | ||
412 | res != NULL; | ||
413 | res = res->next) | ||
414 | { | ||
415 | json_array_append (res_arr, res->res_obj); | ||
416 | } | ||
417 | json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr); | ||
418 | *result = json_dumps (root_json, JSON_INDENT(2)); | ||
419 | json_decref (root_json); | ||
420 | json_decref (res_arr); | ||
421 | return GNUNET_OK; | ||
422 | } | ||
423 | |||
424 | /** | 33 | /** |
425 | * REST Utilities | 34 | * REST Utilities |
426 | */ | 35 | */ |