From: Martin Schanzenbach Date: Mon, 2 May 2016 16:32:36 +0000 (+0000) Subject: - Refactor jsonapi into separate module X-Git-Tag: initial-import-from-subversion-38251~929 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=bee208bcd6803829aa26c55a4c8e176a5f2c815f;p=oweals%2Fgnunet.git - Refactor jsonapi into separate module --- 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 rps \ $(CONSENSUS) \ $(SECRETSHARING) + endif if HAVE_REST @@ -51,6 +52,9 @@ endif if HAVE_REST REST_DIR = rest +if HAVE_JSON + JSONAPI_DIR = jsonapi +endif endif if TALER_ONLY @@ -63,6 +67,7 @@ SUBDIRS = \ include $(INTLEMU_SUBDIRS) \ util \ $(JSON_DIR) \ + $(JSONAPI_DIR) \ $(REST_DIR) \ hello \ 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 = \ libgnunetgns.la \ $(top_builddir)/src/rest/libgnunetrest.la \ $(top_builddir)/src/identity/libgnunetidentity.la \ + $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ $(LTLIBINTL) -ljansson -lmicrohttpd 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 @@ #include #include #include +#include #include #define GNUNET_REST_API_NS_GNS "/gns" @@ -284,16 +285,16 @@ process_lookup_result (void *cls, uint32_t rd_count, { struct LookupHandle *handle = cls; struct MHD_Response *resp; - struct JsonApiObject *json_object; - struct JsonApiResource *json_resource; + struct GNUNET_JSONAPI_Object *json_object; + struct GNUNET_JSONAPI_Resource *json_resource; uint32_t i; char *result; json_t *result_array; json_t *record_obj; result_array = json_array(); - json_object = GNUNET_REST_jsonapi_object_new (); - json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name); + json_object = GNUNET_JSONAPI_object_new (); + json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name); handle->lookup_request = NULL; for (i=0; iproc (handle->proc_cls, resp, MHD_HTTP_OK); 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 = \ $(top_builddir)/src/identity/libgnunetidentity.la \ libgnunetidentityprovider.la \ $(top_builddir)/src/rest/libgnunetrest.la \ + $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ $(top_builddir)/src/namestore/libgnunetnamestore.la \ $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ $(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, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); GNUNET_asprintf (result, - "{\"nonce\": \""SCNu64"\",\"identity\": \"%s\",\"label\": \"%s\"}", + "{\"nonce\": \"%"SCNu64"\",\"identity\": \"%s\",\"label\": \"%s\"}", payload->nonce, identity_key_str, payload->label); GNUNET_free (identity_key_str); 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 @@ #include "gnunet_gnsrecord_lib.h" #include "gnunet_namestore_service.h" #include "gnunet_rest_lib.h" +#include "gnunet_jsonapi_lib.h" #include "microhttpd.h" #include #include @@ -273,7 +274,7 @@ struct RequestHandle /** * Response object */ - struct JsonApiObject *resp_object; + struct GNUNET_JSONAPI_Object *resp_object; }; @@ -290,7 +291,7 @@ cleanup_handle (struct RequestHandle *handle) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); if (NULL != handle->resp_object) - GNUNET_REST_jsonapi_object_delete (handle->resp_object); + GNUNET_JSONAPI_object_delete (handle->resp_object); if (NULL != handle->timeout_task) GNUNET_SCHEDULER_cancel (handle->timeout_task); if (NULL != handle->identity_handle) @@ -381,7 +382,7 @@ token_creat_cont (void *cls, const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket, const struct GNUNET_IDENTITY_PROVIDER_Token *token) { - struct JsonApiResource *json_resource; + struct GNUNET_JSONAPI_Resource *json_resource; struct RequestHandle *handle = cls; struct MHD_Response *resp; json_t *ticket_json; @@ -397,26 +398,26 @@ token_creat_cont (void *cls, return; } - handle->resp_object = GNUNET_REST_jsonapi_object_new (); - json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET, + handle->resp_object = GNUNET_JSONAPI_object_new (); + json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET, label); ticket_str = GNUNET_IDENTITY_PROVIDER_ticket_to_string (ticket); token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token); ticket_json = json_string (ticket_str); token_json = json_string (token_str); - GNUNET_REST_jsonapi_resource_add_attr (json_resource, + GNUNET_JSONAPI_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET, ticket_json); - GNUNET_REST_jsonapi_resource_add_attr (json_resource, + GNUNET_JSONAPI_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN, token_json); GNUNET_free (ticket_str); GNUNET_free (token_str); json_decref (ticket_json); json_decref (token_json); - GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource); + GNUNET_JSONAPI_object_resource_add (handle->resp_object, json_resource); - GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result_str); + GNUNET_JSONAPI_data_serialize (handle->resp_object, &result_str); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); resp = GNUNET_REST_create_json_response (result_str); handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); @@ -632,7 +633,7 @@ return_token_list (void *cls) struct RequestHandle *handle = cls; struct MHD_Response *resp; - GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result_str); + GNUNET_JSONAPI_data_serialize (handle->resp_object, &result_str); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); resp = GNUNET_REST_create_json_response (result_str); handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); @@ -657,7 +658,7 @@ token_collect (void *cls, char* data; struct RequestHandle *handle = cls; struct EgoEntry *ego_tmp; - struct JsonApiResource *json_resource; + struct GNUNET_JSONAPI_Resource *json_resource; const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key; json_t *issuer; json_t *token; @@ -698,20 +699,20 @@ token_collect (void *cls, rd[i].data, rd[i].data_size); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token: %s\n", data); - json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN, + json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN, label); issuer = json_string (handle->ego_head->identifier); - GNUNET_REST_jsonapi_resource_add_attr (json_resource, + GNUNET_JSONAPI_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST, issuer); json_decref (issuer); token = json_string (data); - GNUNET_REST_jsonapi_resource_add_attr (json_resource, + GNUNET_JSONAPI_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_IDENTITY_TOKEN, token); json_decref (token); - GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource); + GNUNET_JSONAPI_object_resource_add (handle->resp_object, json_resource); GNUNET_free (data); } } @@ -770,7 +771,7 @@ list_token_cont (struct RestConnectionDataHandle *con_handle, GNUNET_free (ego_tmp); } } - handle->resp_object = GNUNET_REST_jsonapi_object_new (); + handle->resp_object = GNUNET_JSONAPI_object_new (); if (NULL == handle->ego_head) { //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 = \ libgnunet_plugin_rest_identity_la_LIBADD = \ libgnunetidentity.la \ $(top_builddir)/src/rest/libgnunetrest.la \ + $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ $(LTLIBINTL) -ljansson -lmicrohttpd 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 @@ #include "gnunet_rest_plugin.h" #include "gnunet_identity_service.h" #include "gnunet_rest_lib.h" +#include "gnunet_jsonapi_lib.h" #include "microhttpd.h" #include #include "gnunet_signatures.h" @@ -296,14 +297,14 @@ get_ego_for_subsys (void *cls, const char *name) { struct RequestHandle *handle = cls; - struct JsonApiObject *json_object; - struct JsonApiResource *json_resource; + struct GNUNET_JSONAPI_Object *json_object; + struct GNUNET_JSONAPI_Resource *json_resource; struct EgoEntry *ego_entry; struct MHD_Response *resp; json_t *name_json; char *result_str; - json_object = GNUNET_REST_jsonapi_object_new (); + json_object = GNUNET_JSONAPI_object_new (); for (ego_entry = handle->ego_head; NULL != ego_entry; @@ -313,27 +314,27 @@ get_ego_for_subsys (void *cls, continue; if (NULL == name) continue; - json_resource = GNUNET_REST_jsonapi_resource_new + json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring); name_json = json_string (ego_entry->identifier); - GNUNET_REST_jsonapi_resource_add_attr (json_resource, + GNUNET_JSONAPI_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_IDENTITY_NAME, name_json); json_decref (name_json); - GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); + GNUNET_JSONAPI_object_resource_add (json_object, json_resource); break; } - if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) + if (0 == GNUNET_JSONAPI_object_resource_count (json_object)) { - GNUNET_REST_jsonapi_object_delete (json_object); + GNUNET_JSONAPI_object_delete (json_object); handle->emsg = GNUNET_strdup("No identity matches results!"); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); + GNUNET_JSONAPI_data_serialize (json_object, &result_str); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); resp = GNUNET_REST_create_json_response (result_str); - GNUNET_REST_jsonapi_object_delete (json_object); + GNUNET_JSONAPI_object_delete (json_object); handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); GNUNET_free (result_str); cleanup_handle (handle); @@ -359,8 +360,8 @@ ego_info_response (struct RestConnectionDataHandle *con, struct EgoEntry *ego_entry; struct GNUNET_HashCode key; struct MHD_Response *resp; - struct JsonApiObject *json_object; - struct JsonApiResource *json_resource; + struct GNUNET_JSONAPI_Object *json_object; + struct GNUNET_JSONAPI_Resource *json_resource; json_t *name_str; if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY)) @@ -409,7 +410,7 @@ ego_info_response (struct RestConnectionDataHandle *con, } } - json_object = GNUNET_REST_jsonapi_object_new (); + json_object = GNUNET_JSONAPI_object_new (); //Return all egos for (ego_entry = handle->ego_head; @@ -418,27 +419,27 @@ ego_info_response (struct RestConnectionDataHandle *con, { if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) continue; - json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, + json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring); name_str = json_string (ego_entry->identifier); - GNUNET_REST_jsonapi_resource_add_attr ( + GNUNET_JSONAPI_resource_add_attr ( json_resource, GNUNET_REST_JSONAPI_IDENTITY_NAME, name_str); json_decref (name_str); - GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); + GNUNET_JSONAPI_object_resource_add (json_object, json_resource); } - if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) + if (0 == GNUNET_JSONAPI_object_resource_count (json_object)) { - GNUNET_REST_jsonapi_object_delete (json_object); + GNUNET_JSONAPI_object_delete (json_object); handle->emsg = GNUNET_strdup ("No identities found!"); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); + GNUNET_JSONAPI_data_serialize (json_object, &result_str); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); resp = GNUNET_REST_create_json_response (result_str); - GNUNET_REST_jsonapi_object_delete (json_object); + GNUNET_JSONAPI_object_delete (json_object); handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); GNUNET_free (result_str); cleanup_handle (handle); @@ -483,8 +484,8 @@ ego_create_cont (struct RestConnectionDataHandle *con, struct RequestHandle *handle = cls; struct EgoEntry *ego_entry; struct MHD_Response *resp; - struct JsonApiObject *json_obj; - struct JsonApiResource *json_res; + struct GNUNET_JSONAPI_Object *json_obj; + struct GNUNET_JSONAPI_Resource *json_res; json_t *egoname_json; const char* egoname; char term_data[handle->data_size+1]; @@ -503,32 +504,32 @@ ego_create_cont (struct RestConnectionDataHandle *con, } term_data[handle->data_size] = '\0'; memcpy (term_data, handle->data, handle->data_size); - json_obj = GNUNET_REST_jsonapi_object_parse (term_data); + json_obj = GNUNET_JSONAPI_object_parse (term_data); if (NULL == json_obj) { GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) + if (1 != GNUNET_JSONAPI_object_resource_count (json_obj)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->emsg = GNUNET_strdup ("Provided resource count invalid"); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); - if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) + json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0); + if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); resp = GNUNET_REST_create_json_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); cleanup_handle (handle); return; } - egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME); + egoname_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME); if (!json_is_string (egoname_json)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->emsg = GNUNET_strdup ("No name provided"); GNUNET_SCHEDULER_add_now (&do_error, handle); return; @@ -540,7 +541,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, { if (0 == strcasecmp (egoname, ego_entry->identifier)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); resp = GNUNET_REST_create_json_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); cleanup_handle (handle); @@ -548,7 +549,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, } } GNUNET_asprintf (&handle->name, "%s", egoname); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->op = GNUNET_IDENTITY_create (handle->identity_handle, handle->name, &do_finished, @@ -568,8 +569,8 @@ ego_edit_cont (struct RestConnectionDataHandle *con, const char *url, void *cls) { - struct JsonApiObject *json_obj; - struct JsonApiResource *json_res; + struct GNUNET_JSONAPI_Object *json_obj; + struct GNUNET_JSONAPI_Resource *json_res; struct RequestHandle *handle = cls; struct EgoEntry *ego_entry; struct MHD_Response *resp; @@ -617,7 +618,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con, term_data[handle->data_size] = '\0'; memcpy (term_data, handle->data, handle->data_size); - json_obj = GNUNET_REST_jsonapi_object_parse (term_data); + json_obj = GNUNET_JSONAPI_object_parse (term_data); if (NULL == json_obj) { @@ -626,25 +627,25 @@ ego_edit_cont (struct RestConnectionDataHandle *con, return; } - if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) + if (1 != GNUNET_JSONAPI_object_resource_count (json_obj)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->emsg = GNUNET_strdup ("Resource amount invalid"); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); + json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0); - if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) + if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->emsg = GNUNET_strdup ("Resource type invalid"); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } //This is a rename - name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, + name_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NEWNAME); if ((NULL != name_json) && json_is_string (name_json)) { @@ -657,7 +658,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con, 0 != strcasecmp (keystring, ego_entry->keystring)) { //Ego with same name not allowed - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); resp = GNUNET_REST_create_json_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); cleanup_handle (handle); @@ -669,17 +670,17 @@ ego_edit_cont (struct RestConnectionDataHandle *con, newname, &do_finished, handle); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); return; } //Set subsystem - subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM); + subsys_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM); if ( (NULL != subsys_json) && json_is_string (subsys_json)) { subsys = json_string_value (subsys_json); GNUNET_asprintf (&handle->subsys, "%s", subsys); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->op = GNUNET_IDENTITY_set (handle->identity_handle, handle->subsys, ego_entry->ego, @@ -687,7 +688,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con, handle); return; } - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->emsg = GNUNET_strdup ("Subsystem not provided"); GNUNET_SCHEDULER_add_now (&do_error, handle); } 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 @@ +/* + This file is part of GNUnet + Copyright (C) 2014, 2015, 2016 GNUnet e.V. + + GNUnet is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNUnet; see the file COPYING. If not, If not, see +*/ +/** + * @file gnunet_jsonapi_lib.h + * @brief functions to parse jsonapi objects + * @author Martin Schanzenbach + */ +#ifndef GNUNET_JSONAPI_LIB_H +#define GNUNET_JSONAPI_LIB_H + +#include "gnunet_util_lib.h" +#include "gnunet_json_lib.h" + + +/* ****************** JSONAPI parsing ******************* */ + +struct GNUNET_JSONAPI_Resource; + +struct GNUNET_JSONAPI_Object; + +/** + * Specification for parsing a jsonapi object. + * + * @param jsonapi_obj where to store the jsonapi object + */ +struct GNUNET_JSONAPI_Specification +GNUNET_JSONAPI_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_obj); + +/** + * Create a JSON API resource + * + * @param type the JSON API resource type + * @param id the JSON API resource id + * @return a new JSON API resource or NULL on error. + */ +struct GNUNET_JSONAPI_Resource* +GNUNET_JSONAPI_resource_new (const char *type, const char *id); + +/** + * Delete a JSON API resource + * + * @param res the JSON resource + * @param result Pointer where the resource should be stored + */ +void +GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource); + +/** + * Add a JSON API attribute + * + * @param res the JSON resource + * @param key the key for the attribute + * @param json the json_t attribute to add + * @return #GNUNET_OK if added successfully + * #GNUNET_SYSERR if not + */ +int +GNUNET_JSONAPI_resource_add_attr (const struct GNUNET_JSONAPI_Resource *resource, + const char* key, + json_t *json); +/** + * Read a JSON API attribute + * + * @param res the JSON resource + * @param key the key for the attribute + * @return the json attr + */ +json_t* +GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource, + const char* key); + + +/** + * Check a JSON API resource id + * + * @param res the JSON resource + * @param id the expected id + * @return GNUNET_YES if id matches + */ +int +GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource, + const char* id); + + +/** + * Check a JSON API resource type + * + * @param res the JSON resource + * @param type the expected type + * @return GNUNET_YES if id matches + */ +int +GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource, + const char* type); + + +/** + * Create a JSON API primary data + * + * @param type the JSON API resource type + * @param id the JSON API resource id + * @return a new JSON API resource or NULL on error. + */ +struct GNUNET_JSONAPI_Object* +GNUNET_JSONAPI_object_new (); + + +/** + * Create a JSON API primary data from a string + * + * @param data the string of the JSON API data + * @return a new JSON API resource or NULL on error. + */ +struct GNUNET_JSONAPI_Object* +GNUNET_JSONAPI_object_parse (const char* data); + + +/** + * Delete a JSON API primary data + * + * @param type the JSON API resource type + * @param id the JSON API resource id + * @return a new JSON API resource or NULL on error. + */ +void +GNUNET_JSONAPI_object_delete (struct GNUNET_JSONAPI_Object *resp); + +/** + * Add a JSON API resource to primary data + * + * @param data The JSON API data to add to + * @param res the JSON API resource to add + * @return the new number of resources + */ +void +GNUNET_JSONAPI_object_resource_add (struct GNUNET_JSONAPI_Object *resp, + struct GNUNET_JSONAPI_Resource *res); +/** + * Get a JSON API object resource count + * + * @param resp the JSON API object + * @return the number of resources + */ +int +GNUNET_JSONAPI_object_resource_count (struct GNUNET_JSONAPI_Object *resp); + +/** + * Get a JSON API object resource num + * + * @param resp the JSON API object + * @param num the number of the resource + * @return the resource + */ +struct GNUNET_JSONAPI_Resource* +GNUNET_JSONAPI_object_get_resource (struct GNUNET_JSONAPI_Object *resp, int num); + + +/** + * Add a JSON API resource to primary data + * + * @param resp The JSON API data to add to + * @param res the JSON API resource to add + * @return the new number of resources + */ +void +GNUNET_JSONAPI_data_resource_remove (struct GNUNET_JSONAPI_Object *resp, + struct GNUNET_JSONAPI_Resource *res); + +/** + * String serialze jsonapi primary data + * + * @param data the JSON API primary data + * @param result where to store the result + * @return GNUNET_SYSERR on error else GNUNET_OK + */ +int +GNUNET_JSONAPI_data_serialize (const struct GNUNET_JSONAPI_Object *resp, + char **result); + +/** + * Check a JSON API resource id + * + * @param res the JSON resource + * @return the resource id + */ +json_t* +GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource); +/* end of gnunet_jsonapi_lib.h */ +#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 @@ #include "microhttpd.h" #include -#define GNUNET_REST_JSONAPI_KEY_DATA "data" - -#define GNUNET_REST_JSONAPI_KEY_ID "id" - -#define GNUNET_REST_JSONAPI_KEY_TYPE "type" - #define GNUNET_REST_HANDLER_END {NULL, NULL, NULL} struct RestConnectionDataHandle @@ -86,168 +80,6 @@ typedef void (*GNUNET_REST_ResultProcessor) (void *cls, struct MHD_Response *resp, int status); - -/** - * Resource structs for JSON API - */ -struct JsonApiResource; - -/** - * Responses for JSON API - */ -struct JsonApiObject; - -/** - * Create a JSON API resource - * - * @param type the JSON API resource type - * @param id the JSON API resource id - * @return a new JSON API resource or NULL on error. - */ -struct JsonApiResource* -GNUNET_REST_jsonapi_resource_new (const char *type, const char *id); - -/** - * Delete a JSON API resource - * - * @param res the JSON resource - * @param result Pointer where the resource should be stored - */ -void -GNUNET_REST_jsonapi_resource_delete (struct JsonApiResource *resource); - -/** - * Add a JSON API attribute - * - * @param res the JSON resource - * @param key the key for the attribute - * @param json the json_t attribute to add - * @return #GNUNET_OK if added successfully - * #GNUNET_SYSERR if not - */ -int -GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource, - const char* key, - json_t *json); -/** - * Read a JSON API attribute - * - * @param res the JSON resource - * @param key the key for the attribute - * @return the json attr - */ -json_t* -GNUNET_REST_jsonapi_resource_read_attr (const struct JsonApiResource *resource, - const char* key); - - -/** - * Check a JSON API resource id - * - * @param res the JSON resource - * @param id the expected id - * @return GNUNET_YES if id matches - */ -int -GNUNET_REST_jsonapi_resource_check_id (const struct JsonApiResource *resource, - const char* id); - - -/** - * Check a JSON API resource type - * - * @param res the JSON resource - * @param type the expected type - * @return GNUNET_YES if id matches - */ -int -GNUNET_REST_jsonapi_resource_check_type (const struct JsonApiResource *resource, - const char* type); - - -/** - * Create a JSON API primary data - * - * @param type the JSON API resource type - * @param id the JSON API resource id - * @return a new JSON API resource or NULL on error. - */ -struct JsonApiObject* -GNUNET_REST_jsonapi_object_new (); - - -/** - * Create a JSON API primary data from a string - * - * @param data the string of the JSON API data - * @return a new JSON API resource or NULL on error. - */ -struct JsonApiObject* -GNUNET_REST_jsonapi_object_parse (const char* data); - - -/** - * Delete a JSON API primary data - * - * @param type the JSON API resource type - * @param id the JSON API resource id - * @return a new JSON API resource or NULL on error. - */ -void -GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp); - -/** - * Add a JSON API resource to primary data - * - * @param data The JSON API data to add to - * @param res the JSON API resource to add - * @return the new number of resources - */ -void -GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp, - struct JsonApiResource *res); -/** - * Get a JSON API object resource count - * - * @param resp the JSON API object - * @return the number of resources - */ -int -GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp); - -/** - * Get a JSON API object resource num - * - * @param resp the JSON API object - * @param num the number of the resource - * @return the resource - */ -struct JsonApiResource* -GNUNET_REST_jsonapi_object_get_resource (struct JsonApiObject *resp, int num); - - -/** - * Add a JSON API resource to primary data - * - * @param resp The JSON API data to add to - * @param res the JSON API resource to add - * @return the new number of resources - */ -void -GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp, - struct JsonApiResource *res); - -/** - * String serialze jsonapi primary data - * - * @param data the JSON API primary data - * @param result where to store the result - * @return GNUNET_SYSERR on error else GNUNET_OK - */ -int -GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp, - char **result); - /** * Check if namespace is in URL. * 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 @@ +# This Makefile.am is in the public domain +AM_CPPFLAGS = -I$(top_srcdir)/src/include + +if USE_COVERAGE + AM_CFLAGS = --coverage -O0 + XLIB = -lgcov +endif + +lib_LTLIBRARIES = \ + libgnunetjsonapi.la + +libgnunetjsonapi_la_LDFLAGS = \ + -version-info 0:0:0 \ + -no-undefined +libgnunetjsonapi_la_SOURCES = \ + jsonapi.c +libgnunetjsonapi_la_LIBADD = \ + $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/json/libgnunetjson.la \ + -ljansson \ + $(XLIB) + +#check_PROGRAMS = \ +# test_json + +#TESTS = \ +# $(check_PROGRAMS) + +#test_json_SOURCES = \ +# test_json.c +#test_json_LDADD = \ +# libgnunetjson.la \ +# $(top_builddir)/src/util/libgnunetutil.la \ +# -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 @@ +/* + This file is part of GNUnet + Copyright (C) 2014, 2015, 2016 GNUnet e.V. + + GNUnet is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNUnet; see the file COPYING. If not, If not, see +*/ +/** + * @file json/jsonapi.c + * @brief functions to generate specifciations for JSONAPI parsing + * @author Martin Schanzenbach + */ +#include "platform.h" +#include "gnunet_json_lib.h" + +#define GNUNET_JSONAPI_JSONAPI_KEY_DATA "data" + +#define GNUNET_JSONAPI_JSONAPI_KEY_ID "id" + +#define GNUNET_JSONAPI_JSONAPI_KEY_TYPE "type" + +struct GNUNET_JSONAPI_Resource +{ + /** + * DLL + */ + struct GNUNET_JSONAPI_Resource *next; + + /** + * DLL + */ + struct GNUNET_JSONAPI_Resource *prev; + + /** + * Resource content + */ + json_t *res_obj; +}; + + +struct GNUNET_JSONAPI_Object +{ + /** + * DLL Resource + */ + struct GNUNET_JSONAPI_Resource *res_list_head; + + /** + * DLL Resource + */ + struct GNUNET_JSONAPI_Resource *res_list_tail; + + /** + * num resources + */ + int res_count; +}; + + +/** + * JSON API + */ + + +/** + * Create a JSON API resource + * + * @param type the JSON API resource type + * @param id the JSON API resource id + * @return a new JSON API resource or NULL on error. + */ +struct GNUNET_JSONAPI_Resource* +GNUNET_JSONAPI_resource_new (const char *type, const char *id) +{ + struct GNUNET_JSONAPI_Resource *res; + + if ( (NULL == type) || (0 == strlen (type)) ) + return NULL; + if ( (NULL == id) || (0 == strlen (id)) ) + return NULL; + + res = GNUNET_new (struct GNUNET_JSONAPI_Resource); + res->prev = NULL; + res->next = NULL; + + res->res_obj = json_object (); + + json_object_set_new (res->res_obj, GNUNET_JSONAPI_JSONAPI_KEY_ID, json_string (id)); + json_object_set_new (res->res_obj, GNUNET_JSONAPI_JSONAPI_KEY_TYPE, json_string (type)); + + return res; +} + + + +/** + * Add a JSON API attribute + * + * @param res the JSON resource + * @param key the key for the attribute + * @param json the json_t attribute to add + * @return #GNUNET_OK if added successfully + * #GNUNET_SYSERR if not + */ +int +GNUNET_JSONAPI_resource_add_attr (const struct GNUNET_JSONAPI_Resource *resource, + const char* key, + json_t *json) +{ + if ( (NULL == resource) || + (NULL == key) || + (NULL == json) ) + return GNUNET_SYSERR; + json_object_set (resource->res_obj, key, json); + return GNUNET_OK; +} + +/** + * Read a JSON API attribute + * + * @param res the JSON resource + * @param key the key for the attribute + * @return the json_t object + */ +json_t* +GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource, + const char* key) +{ + if ( (NULL == resource) || + (NULL == key)) + return NULL; + return json_object_get (resource->res_obj, key); +} + +int +check_resource_attr_str (const struct GNUNET_JSONAPI_Resource *resource, + const char* key, + const char* attr) +{ + json_t *value; + if ( (NULL == resource) || + (NULL == key) || + (NULL == attr)) + return GNUNET_NO; + value = json_object_get (resource->res_obj, key); + if (NULL == value) + return GNUNET_NO; + if (!json_is_string (value) || + (0 != strcmp (attr, json_string_value(value)))) + { + return GNUNET_NO; + } + return GNUNET_YES; +} + +/** + * Check a JSON API resource id + * + * @param res the JSON resource + * @param id the expected id + * @return GNUNET_YES if id matches + */ +int +GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource, + const char* id) +{ + return check_resource_attr_str (resource, GNUNET_JSONAPI_JSONAPI_KEY_ID, id); +} + +/** + * Check a JSON API resource id + * + * @param res the JSON resource + * @return the resource id + */ +json_t* +GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource) +{ + return GNUNET_JSONAPI_resource_read_attr (resource, GNUNET_JSONAPI_JSONAPI_KEY_ID); +} + +/** + * Check a JSON API resource type + * + * @param res the JSON resource + * @param type the expected type + * @return GNUNET_YES if id matches + */ +int +GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource, + const char* type) +{ + return check_resource_attr_str (resource, GNUNET_JSONAPI_JSONAPI_KEY_TYPE, type); +} + +/** + * Get a JSON API object resource count + * + * @param resp the JSON API object + * @return the number of resources + */ +int +GNUNET_JSONAPI_object_resource_count (struct GNUNET_JSONAPI_Object *resp) +{ + return resp->res_count; +} + +/** + * Get a JSON API object resource by index + * + * @param resp the JSON API object + * @param num the number of the resource + * @return the resource + */ +struct GNUNET_JSONAPI_Resource* +GNUNET_JSONAPI_object_get_resource (struct GNUNET_JSONAPI_Object *resp, + int num) +{ + struct GNUNET_JSONAPI_Resource *res; + int i; + + if ((0 == resp->res_count) || + (num >= resp->res_count)) + return NULL; + res = resp->res_list_head; + for (i = 0; i < num; i++) + { + res = res->next; + } + return res; +} + +/** + * Delete a JSON API resource + * + * @param res the JSON resource + * @param result Pointer where the resource should be stored + */ +void +GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource) +{ + json_decref (resource->res_obj); + GNUNET_free (resource); + resource = NULL; +} + +/** + * Delete a JSON API primary data + * + * @param type the JSON API resource type + * @param id the JSON API resource id + * @return a new JSON API resource or NULL on error. + */ +void +GNUNET_JSONAPI_object_delete (struct GNUNET_JSONAPI_Object *resp) +{ + struct GNUNET_JSONAPI_Resource *res; + struct GNUNET_JSONAPI_Resource *res_next; + + for (res = resp->res_list_head; + res != NULL;) + { + res_next = res->next; + GNUNET_CONTAINER_DLL_remove (resp->res_list_head, + resp->res_list_tail, + res); + GNUNET_JSONAPI_resource_delete (res); + res = res_next; + } + GNUNET_free (resp); + resp = NULL; +} + +/** + * Create a JSON API primary data + * + * @return a new JSON API resource or NULL on error. + */ +struct GNUNET_JSONAPI_Object* +GNUNET_JSONAPI_jsonapi_object_new () +{ + struct GNUNET_JSONAPI_Object *result; + + result = GNUNET_new (struct GNUNET_JSONAPI_Object); + result->res_count = 0; + return result; +} + +/** + * Add a JSON API object to primary data + * + * @param data The JSON API data to add to + * @param res the JSON API resource to add + * @return the new number of resources + */ +void +GNUNET_JSONAPI_object_resource_add (struct GNUNET_JSONAPI_Object *resp, + struct GNUNET_JSONAPI_Resource *res) +{ + GNUNET_CONTAINER_DLL_insert (resp->res_list_head, + resp->res_list_tail, + res); + + resp->res_count++; +} + +static void +add_json_resource (struct GNUNET_JSONAPI_Object *obj, + const json_t *res_json) +{ + struct GNUNET_JSONAPI_Resource *res; + const char *type_json; + + struct GNUNET_JSON_Specification dspec[] = { + GNUNET_JSON_spec_string (GNUNET_JSONAPI_JSONAPI_KEY_TYPE, &type_json), + GNUNET_JSON_spec_end() + }; + + GNUNET_assert (GNUNET_OK == + GNUNET_JSON_parse (res_json, dspec, + NULL, NULL)); + GNUNET_JSON_parse_free (dspec); + res = GNUNET_new (struct GNUNET_JSONAPI_Resource); + res->next = NULL; + res->prev = NULL; + res->res_obj = json_deep_copy (res_json); + GNUNET_JSONAPI_object_resource_add (obj, res); +} + +/** + * Parse given JSON object to RSA public key. + * + * @param cls closure, NULL + * @param root the json object representing data + * @param[out] spec where to write the data + * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error + */ +static int +parse_jsonapiobject (void *cls, + json_t *root, + struct GNUNET_JSON_Specification *spec) +{ + struct GNUNET_JSONAPI_Object *result; + json_t *data_json; + int res_count = 0; + int i; + + struct GNUNET_JSON_Specification jsonapispec[] = { + GNUNET_JSON_spec_json (GNUNET_JSONAPI_JSONAPI_KEY_DATA, &data_json), + GNUNET_JSON_spec_end() + }; + if (GNUNET_OK != + GNUNET_JSON_parse (root, jsonapispec, + NULL, NULL) || (NULL == data_json)) + { + return GNUNET_SYSERR; + } + + result = GNUNET_new (struct GNUNET_JSONAPI_Object); + result->res_count = 0; + if (json_is_object (data_json)) + add_json_resource (result, data_json); + else if (json_is_array (data_json)) + { + res_count = json_array_size (data_json); + for (i = 0; i < res_count; i++) + add_json_resource (result, json_array_get (data_json, i)); + } + if (0 == result->res_count) + { + GNUNET_free (result); + GNUNET_JSON_parse_free (jsonapispec); + return GNUNET_SYSERR; + } + *(struct GNUNET_JSONAPI_Object **) spec->ptr = result; + GNUNET_JSON_parse_free (jsonapispec); + return GNUNET_OK; +} + + +/** + * Cleanup data left from parsing RSA public key. + * + * @param cls closure, NULL + * @param[out] spec where to free the data + */ +static void +clean_jsonapiobject (void *cls, + struct GNUNET_JSON_Specification *spec) +{ + struct GNUNET_JSONAPI_Object **jsonapi_obj; + jsonapi_obj = (struct GNUNET_JSONAPI_Object **) spec->ptr; + if (NULL != *jsonapi_obj) + { + GNUNET_JSONAPI_object_delete (*jsonapi_obj); + *jsonapi_obj = NULL; + } +} + +/** + * Add a JSON API resource to primary data + * + * @param data The JSON API data to add to + * @param res the JSON API resource to add + * @return the new number of resources + */ +void +GNUNET_JSONAPI_data_resource_remove (struct GNUNET_JSONAPI_Object *resp, + struct GNUNET_JSONAPI_Resource *res) +{ + GNUNET_CONTAINER_DLL_remove (resp->res_list_head, + resp->res_list_tail, + res); + resp->res_count--; +} + +/** + * String serialze jsonapi primary data + * + * @param data the JSON API primary data + * @param result where to store the result + * @return GNUNET_SYSERR on error else GNUNET_OK + */ +int +GNUNET_JSONAPI_data_serialize (const struct GNUNET_JSONAPI_Object *resp, + char **result) +{ + struct GNUNET_JSONAPI_Resource *res; + json_t *root_json; + json_t *res_arr; + + if ((NULL == resp)) + return GNUNET_SYSERR; + + root_json = json_object (); + res_arr = json_array (); + for (res = resp->res_list_head; + res != NULL; + res = res->next) + { + json_array_append (res_arr, res->res_obj); + } + json_object_set (root_json, GNUNET_JSONAPI_JSONAPI_KEY_DATA, res_arr); + *result = json_dumps (root_json, JSON_INDENT(2)); + json_decref (root_json); + json_decref (res_arr); + return GNUNET_OK; +} + +/** + * JSON object. + * + * @param name name of the JSON field + * @param[out] jsonp where to store the JSON found under @a name + */ +struct GNUNET_JSON_Specification +GNUNET_JSON_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_object) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_jsonapiobject, + .cleaner = &clean_jsonapiobject, + .cls = NULL, + .field = NULL, + .ptr = jsonapi_object, + .ptr_size = 0, + .size_ptr = NULL + }; + *jsonapi_object = NULL; + return ret; +} 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 = \ libgnunetnamestore.la \ $(top_builddir)/src/rest/libgnunetrest.la \ $(top_builddir)/src/identity/libgnunetidentity.la \ + $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ $(LTLIBINTL) -ljansson -lmicrohttpd 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 @@ #include "gnunet_namestore_service.h" #include "gnunet_identity_service.h" #include "gnunet_rest_lib.h" +#include "gnunet_jsonapi_lib.h" #include "microhttpd.h" #include @@ -105,7 +106,7 @@ struct RequestHandle /** * JSON response object */ - struct JsonApiObject *resp_object; + struct GNUNET_JSONAPI_Object *resp_object; /** * Rest connection @@ -255,7 +256,7 @@ cleanup_handle (struct RequestHandle *handle) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); if (NULL != handle->resp_object) - GNUNET_REST_jsonapi_object_delete (handle->resp_object); + GNUNET_JSONAPI_object_delete (handle->resp_object); if (NULL != handle->name) GNUNET_free (handle->name); if (NULL != handle->timeout_task) @@ -403,7 +404,7 @@ namestore_list_response (void *cls, const struct GNUNET_GNSRECORD_Data *rd) { struct RequestHandle *handle = cls; - struct JsonApiResource *json_resource; + struct GNUNET_JSONAPI_Resource *json_resource; struct MHD_Response *resp; json_t *result_array; json_t *record_obj; @@ -411,13 +412,13 @@ namestore_list_response (void *cls, char *result; if (NULL == handle->resp_object) - handle->resp_object = GNUNET_REST_jsonapi_object_new (); + handle->resp_object = GNUNET_JSONAPI_object_new (); if (NULL == rname) { handle->list_it = NULL; //Handle response - if (GNUNET_SYSERR == GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result)) + if (GNUNET_SYSERR == GNUNET_JSONAPI_data_serialize (handle->resp_object, &result)) { GNUNET_SCHEDULER_add_now (&do_error, handle); return; @@ -455,12 +456,12 @@ namestore_list_response (void *cls, if (0 < json_array_size(result_array)) { - json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO, + json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO, rname); - GNUNET_REST_jsonapi_resource_add_attr (json_resource, + GNUNET_JSONAPI_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_NAMESTORE_RECORD, result_array); - GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource); + GNUNET_JSONAPI_object_resource_add (handle->resp_object, json_resource); } json_decref (result_array); @@ -723,8 +724,8 @@ namestore_create_cont (struct RestConnectionDataHandle *con, { struct RequestHandle *handle = cls; struct MHD_Response *resp; - struct JsonApiObject *json_obj; - struct JsonApiResource *json_res; + struct GNUNET_JSONAPI_Object *json_obj; + struct GNUNET_JSONAPI_Resource *json_res; json_t *name_json; json_t *records_json; char term_data[handle->data_size+1]; @@ -743,7 +744,7 @@ namestore_create_cont (struct RestConnectionDataHandle *con, } term_data[handle->data_size] = '\0'; memcpy (term_data, handle->data, handle->data_size); - json_obj = GNUNET_REST_jsonapi_object_parse (term_data); + json_obj = GNUNET_JSONAPI_object_parse (term_data); if (NULL == json_obj) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -752,54 +753,54 @@ namestore_create_cont (struct RestConnectionDataHandle *con, GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) + if (1 != GNUNET_JSONAPI_object_resource_count (json_obj)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot create more than 1 resource! (Got %d)\n", - GNUNET_REST_jsonapi_object_resource_count (json_obj)); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_resource_count (json_obj)); + GNUNET_JSONAPI_object_delete (json_obj); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); - if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, + json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0); + if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_NAMESTORE_RECORD)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported JSON data type\n"); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); resp = GNUNET_REST_create_json_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); cleanup_handle (handle); return; } - name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_KEY_ID); + name_json = GNUNET_JSONAPI_resource_get_id (json_res); if (!json_is_string (name_json)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Name property is no string\n"); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } GNUNET_asprintf (&handle->name, "%s", json_string_value (name_json)); - records_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, + records_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_NAMESTORE_RECORD); if (NULL == records_json) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No records given\n"); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } if (GNUNET_SYSERR == json_to_gnsrecord (records_json, &handle->rd, &handle->rd_count)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, &handle->zone_pkey, @@ -816,34 +817,34 @@ namestore_zkey_response (void *cls, { struct RequestHandle *handle = cls; struct MHD_Response *resp; - struct JsonApiObject *json_obj; - struct JsonApiResource *json_res; + struct GNUNET_JSONAPI_Object *json_obj; + struct GNUNET_JSONAPI_Resource *json_res; json_t *name_json; char* result; handle->reverse_qe = NULL; - json_obj = GNUNET_REST_jsonapi_object_new (); + json_obj = GNUNET_JSONAPI_object_new (); if (NULL != label) { name_json = json_string (label); - json_res = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO, + json_res = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO, handle->zkey_str); - GNUNET_REST_jsonapi_resource_add_attr (json_res, + GNUNET_JSONAPI_resource_add_attr (json_res, GNUNET_REST_JSONAPI_NAMESTORE_NAME, name_json); - GNUNET_REST_jsonapi_object_resource_add (json_obj, json_res); + GNUNET_JSONAPI_object_resource_add (json_obj, json_res); json_decref (name_json); } //Handle response - if (GNUNET_SYSERR == GNUNET_REST_jsonapi_data_serialize (json_obj, &result)) + if (GNUNET_SYSERR == GNUNET_JSONAPI_data_serialize (json_obj, &result)) { - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); GNUNET_SCHEDULER_add_now (&do_error, handle); return; } resp = GNUNET_REST_create_json_response (result); handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); - GNUNET_REST_jsonapi_object_delete (json_obj); + GNUNET_JSONAPI_object_delete (json_obj); GNUNET_free (result); GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); 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 @@ #include "microhttpd.h" #include - -struct JsonApiResource -{ - /** - * DLL - */ - struct JsonApiResource *next; - - /** - * DLL - */ - struct JsonApiResource *prev; - - /** - * Resource content - */ - json_t *res_obj; -}; - - -struct JsonApiObject -{ - /** - * DLL Resource - */ - struct JsonApiResource *res_list_head; - - /** - * DLL Resource - */ - struct JsonApiResource *res_list_tail; - - /** - * num resources - */ - int res_count; -}; - - -/** - * JSON API - */ - -/** - * Create a JSON API resource - * - * @param type the JSON API resource type - * @param id the JSON API resource id - * @return a new JSON API resource or NULL on error. - */ -struct JsonApiResource* -GNUNET_REST_jsonapi_resource_new (const char *type, const char *id) -{ - struct JsonApiResource *res; - - if ( (NULL == type) || (0 == strlen (type)) ) - return NULL; - if ( (NULL == id) || (0 == strlen (id)) ) - return NULL; - - res = GNUNET_new (struct JsonApiResource); - res->prev = NULL; - res->next = NULL; - - res->res_obj = json_object (); - - json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_ID, json_string (id)); - json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_TYPE, json_string (type)); - - return res; -} - -/** - * Delete a JSON API resource - * - * @param res the JSON resource - * @param result Pointer where the resource should be stored - */ -void -GNUNET_REST_jsonapi_resource_delete (struct JsonApiResource *resource) -{ - json_decref (resource->res_obj); - GNUNET_free (resource); -} - -/** - * Add a JSON API attribute - * - * @param res the JSON resource - * @param key the key for the attribute - * @param json the json_t attribute to add - * @return #GNUNET_OK if added successfully - * #GNUNET_SYSERR if not - */ -int -GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource, - const char* key, - json_t *json) -{ - if ( (NULL == resource) || - (NULL == key) || - (NULL == json) ) - return GNUNET_SYSERR; - json_object_set (resource->res_obj, key, json); - return GNUNET_OK; -} - -/** - * Read a JSON API attribute - * - * @param res the JSON resource - * @param key the key for the attribute - * @return the json_t object - */ -json_t* -GNUNET_REST_jsonapi_resource_read_attr (const struct JsonApiResource *resource, - const char* key) -{ - if ( (NULL == resource) || - (NULL == key)) - return NULL; - return json_object_get (resource->res_obj, key); -} - -int -check_resource_attr_str (const struct JsonApiResource *resource, - const char* key, - const char* attr) -{ - json_t *value; - if ( (NULL == resource) || - (NULL == key) || - (NULL == attr)) - return GNUNET_NO; - value = json_object_get (resource->res_obj, key); - if (NULL == value) - return GNUNET_NO; - if (!json_is_string (value) || - (0 != strcmp (attr, json_string_value(value)))) - { - return GNUNET_NO; - } - return GNUNET_YES; -} - -/** - * Check a JSON API resource id - * - * @param res the JSON resource - * @param id the expected id - * @return GNUNET_YES if id matches - */ -int -GNUNET_REST_jsonapi_resource_check_id (const struct JsonApiResource *resource, - const char* id) -{ - return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_ID, id); -} - - -/** - * Check a JSON API resource type - * - * @param res the JSON resource - * @param type the expected type - * @return GNUNET_YES if id matches - */ -int -GNUNET_REST_jsonapi_resource_check_type (const struct JsonApiResource *resource, - const char* type) -{ - return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_TYPE, type); -} - - -/** - * Create a JSON API primary data - * - * @return a new JSON API resource or NULL on error. - */ -struct JsonApiObject* -GNUNET_REST_jsonapi_object_new () -{ - struct JsonApiObject *result; - - result = GNUNET_new (struct JsonApiObject); - result->res_count = 0; - return result; -} - - -static void -add_json_resource (struct JsonApiObject *obj, - const json_t *res_json) -{ - struct JsonApiResource *res; - json_t *type_json; - - type_json = json_object_get (res_json, GNUNET_REST_JSONAPI_KEY_TYPE); - if (!json_is_string (type_json)) - return; - res = GNUNET_new (struct JsonApiResource); - res->next = NULL; - res->prev = NULL; - res->res_obj = json_deep_copy (res_json); - GNUNET_REST_jsonapi_object_resource_add (obj, res); -} - -/** - * Create a JSON API primary data from a string - * - * @param data the string of the JSON API data - * @return a new JSON API resource or NULL on error. - */ -struct JsonApiObject* -GNUNET_REST_jsonapi_object_parse (const char* data) -{ - struct JsonApiObject *result; - json_t *root_json; - json_t *data_json; - json_error_t error; - int res_count = 0; - int i; - if (NULL == data) - return NULL; - root_json = json_loads (data, 0, &error); - - if ( (NULL == root_json) || !json_is_object (root_json)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "json error: %s", error.text); - return NULL; - } - data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA); - if (NULL == data_json) - { - json_decref (root_json); - return NULL; - } - - result = GNUNET_new (struct JsonApiObject); - result->res_count = 0; - if (json_is_object (data_json)) - add_json_resource (result, data_json); - else if (json_is_array (data_json)) - { - res_count = json_array_size (data_json); - for (i = 0; i < res_count; i++) - add_json_resource (result, json_array_get (data_json, i)); - } - json_decref (root_json); - if (0 == result->res_count) - { - GNUNET_free (result); - result = NULL; - } - return result; -} - - -/** - * Delete a JSON API primary data - * - * @param type the JSON API resource type - * @param id the JSON API resource id - * @return a new JSON API resource or NULL on error. - */ -void -GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp) -{ - struct JsonApiResource *res; - struct JsonApiResource *res_next; - - for (res = resp->res_list_head; - res != NULL;) - { - res_next = res->next; - GNUNET_CONTAINER_DLL_remove (resp->res_list_head, - resp->res_list_tail, - res); - GNUNET_REST_jsonapi_resource_delete (res); - res = res_next; - } - GNUNET_free (resp); -} - -/** - * Add a JSON API object to primary data - * - * @param data The JSON API data to add to - * @param res the JSON API resource to add - * @return the new number of resources - */ -void -GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp, - struct JsonApiResource *res) -{ - GNUNET_CONTAINER_DLL_insert (resp->res_list_head, - resp->res_list_tail, - res); - - resp->res_count++; -} - - -/** - * Get a JSON API object resource count - * - * @param resp the JSON API object - * @return the number of resources - */ -int -GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp) -{ - return resp->res_count; -} - -/** - * Get a JSON API object resource num - * - * @param resp the JSON API object - * @param num the number of the resource - * @return the resource - */ -struct JsonApiResource* -GNUNET_REST_jsonapi_object_get_resource (struct JsonApiObject *resp, int num) -{ - struct JsonApiResource *res; - int i; - - if ((0 == resp->res_count) || - (num >= resp->res_count)) - return NULL; - res = resp->res_list_head; - for (i = 0; i < num; i++) - { - res = res->next; - } - return res; -} - - -/** - * Add a JSON API resource to primary data - * - * @param data The JSON API data to add to - * @param res the JSON API resource to add - * @return the new number of resources - */ -void -GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp, - struct JsonApiResource *res) -{ - GNUNET_CONTAINER_DLL_remove (resp->res_list_head, - resp->res_list_tail, - res); - resp->res_count--; -} - -/** - * String serialze jsonapi primary data - * - * @param data the JSON API primary data - * @param result where to store the result - * @return GNUNET_SYSERR on error else GNUNET_OK - */ -int -GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp, - char **result) -{ - struct JsonApiResource *res; - json_t *root_json; - json_t *res_arr; - - if ((NULL == resp)) - return GNUNET_SYSERR; - - root_json = json_object (); - res_arr = json_array (); - for (res = resp->res_list_head; - res != NULL; - res = res->next) - { - json_array_append (res_arr, res->res_obj); - } - json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr); - *result = json_dumps (root_json, JSON_INDENT(2)); - json_decref (root_json); - json_decref (res_arr); - return GNUNET_OK; -} - /** * REST Utilities */