[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r35462 - in gnunet/src: identity include rest
From: |
gnunet |
Subject: |
[GNUnet-SVN] r35462 - in gnunet/src: identity include rest |
Date: |
Thu, 26 Mar 2015 15:57:06 +0100 |
Author: schanzen
Date: 2015-03-26 15:57:05 +0100 (Thu, 26 Mar 2015)
New Revision: 35462
Modified:
gnunet/src/identity/plugin_rest_identity.c
gnunet/src/include/gnunet_rest_lib.h
gnunet/src/rest/rest.c
Log:
-more REST
Modified: gnunet/src/identity/plugin_rest_identity.c
===================================================================
--- gnunet/src/identity/plugin_rest_identity.c 2015-03-26 12:12:09 UTC (rev
35461)
+++ gnunet/src/identity/plugin_rest_identity.c 2015-03-26 14:57:05 UTC (rev
35462)
@@ -45,6 +45,8 @@
#define GNUNET_REST_JSONAPI_IDENTITY_KEY "key"
+#define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem"
+
/**
* @brief struct returned by the initialization function of the plugin
*/
@@ -236,16 +238,16 @@
const char *name)
{
struct RequestHandle *handle = cls;
+ struct JsonApiObject *json_object;
+ struct JsonApiResource *json_resource;
struct EgoEntry *ego_entry;
struct MHD_Response *resp;
+ json_t *key_json;
char *result_str;
char *keystring;
- json_t *ego_json;
- json_t *root_json;
- root_json = json_object ();
-
- //Return all egos
+ json_object = GNUNET_REST_jsonapi_object_new ();
+
for (ego_entry = handle->ego_head;
NULL != ego_entry;
ego_entry = ego_entry->next)
@@ -254,26 +256,25 @@
continue;
if (NULL == name)
continue;
- ego_json = json_object ();
+ json_resource = GNUNET_REST_jsonapi_resource_new
(GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->identifier);
keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk);
- json_object_set_new (ego_json, "id", json_string (ego_entry->identifier));
- json_object_set_new (ego_json, "type", json_string
(GNUNET_REST_JSONAPI_IDENTITY_EGO));
- json_object_set_new (ego_json, "key", json_string (keystring));
+ key_json = json_string (keystring);
+ GNUNET_REST_jsonapi_resource_add_attr (json_resource,
GNUNET_REST_JSONAPI_IDENTITY_KEY, key_json);
+ json_decref (key_json);
GNUNET_free (keystring);
+ GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
break;
}
- if (NULL == ego_json)
+ if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
{
- json_decref (root_json);
+ GNUNET_REST_jsonapi_object_delete (json_object);
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, ego_json);
- result_str = json_dumps (root_json, JSON_COMPACT);
+ GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
- json_decref (ego_json);
- json_decref (root_json);
resp = GNUNET_REST_create_json_response (result_str);
+ GNUNET_REST_jsonapi_object_delete (json_object);
handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
GNUNET_free (result_str);
cleanup_handle (handle);
@@ -297,7 +298,7 @@
struct EgoEntry *ego_entry;
struct GNUNET_HashCode key;
struct MHD_Response *resp;
- struct JsonApiResponse *json_response;
+ struct JsonApiObject *json_object;
struct JsonApiResource *json_resource;
json_t *key_str;
@@ -309,7 +310,6 @@
GNUNET_break (0);
return;
}
- json_response = GNUNET_REST_jsonapi_response_new ();
if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) {
GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key);
if ( GNUNET_YES ==
@@ -330,7 +330,7 @@
}
}
}
- json_response = GNUNET_REST_jsonapi_response_new ();
+ json_object = GNUNET_REST_jsonapi_object_new ();
egoname = &handle->url[strlen (EGO_NAMESPACE)+1];
if (strlen (EGO_NAMESPACE) == strlen (handle->url))
@@ -353,13 +353,13 @@
key_str);
json_decref (key_str);
GNUNET_free (keystring);
- GNUNET_REST_jsonapi_response_resource_add (json_response, json_resource);
+ GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
}
- GNUNET_REST_jsonapi_data_serialize (json_response, &result_str);
-
+ GNUNET_REST_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);
handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
GNUNET_free (result_str);
cleanup_handle (handle);
@@ -421,16 +421,14 @@
const char *url,
void *cls)
{
- const char* egoname;
struct RequestHandle *handle = cls;
- char term_data[handle->data_size+1];
struct EgoEntry *ego_entry;
struct MHD_Response *resp;
- json_t *root_json;
- json_t *data_json;
- json_t *type_json;
+ struct JsonApiObject *json_obj;
+ struct JsonApiResource *json_res;
json_t *egoname_json;
- json_error_t error;
+ char term_data[handle->data_size+1];
+ const char* egoname;
if (strlen (API_NAMESPACE) != strlen (handle->url))
{
@@ -442,43 +440,39 @@
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
-
term_data[handle->data_size] = '\0';
memcpy (term_data, handle->data, handle->data_size);
- root_json = json_loads (term_data, 0, &error);
+ json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
- if ((NULL == root_json) || !json_is_object (root_json))
+ if (NULL == json_obj)
{
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA);
- if ((NULL == data_json) || !json_is_object (data_json))
+ if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
{
+ GNUNET_REST_jsonapi_object_delete (json_obj);
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- type_json = json_object_get (data_json, "type");
- if (!json_is_string (type_json) ||
- (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value
(type_json))))
+
+ 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_decref (data_json);
- json_decref (root_json);
+ GNUNET_REST_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;
}
- json_decref (type_json);
- egoname_json = json_object_get (data_json, "id");
+
+ egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res,
GNUNET_REST_JSONAPI_KEY_ID);
if (!json_is_string (egoname_json))
{
- json_decref (data_json);
- json_decref (root_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
-
egoname = json_string_value (egoname_json);
for (ego_entry = handle->ego_head;
NULL != ego_entry;
@@ -487,8 +481,7 @@
if (0 == strcasecmp (egoname, ego_entry->identifier))
{
json_decref (egoname_json);
- json_decref (data_json);
- json_decref (root_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
cleanup_handle (handle);
@@ -497,8 +490,7 @@
}
GNUNET_asprintf (&handle->name, "%s", egoname);
json_decref (egoname_json);
- json_decref (data_json);
- json_decref (root_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
handle->name,
&create_finished,
@@ -510,6 +502,8 @@
const char *url,
void *cls)
{
+ struct JsonApiObject *json_obj;
+ struct JsonApiResource *json_res;
const char *egoname;
const char *subsys;
struct RequestHandle *handle = cls;
@@ -517,13 +511,8 @@
struct EgoEntry *ego_entry;
struct MHD_Response *resp;
int ego_exists = GNUNET_NO;
- json_t *root_json;
- json_t *data_json;
- json_t *type_json;
- json_t *id_json;
json_t *subsys_json;
- json_error_t error;
-
+
if (strlen (API_NAMESPACE) > strlen (handle->url))
{
GNUNET_SCHEDULER_add_now (&do_error, handle);
@@ -557,54 +546,47 @@
term_data[handle->data_size] = '\0';
memcpy (term_data, handle->data, handle->data_size);
- root_json = json_loads (term_data, 0, &error);
+ json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
- if ((NULL == root_json) || !json_is_object (root_json))
+ if (NULL == json_obj)
{
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- data_json = json_object_get (root_json, "data");
- if (!json_is_object (data_json))
+ if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
{
- json_decref (root_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- id_json = json_object_get (data_json, "id");
- if (!json_is_string (id_json) ||
- (0 != strcmp (egoname, json_string_value (id_json))))
+
+ json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
+ if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_id (json_res, egoname))
{
- json_decref (root_json);
- json_decref (data_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- json_decref (id_json);
-
- type_json = json_object_get (data_json, "type");
- if (!json_is_string (type_json) ||
- (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value
(type_json))))
+
+ if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_id (json_res,
GNUNET_REST_JSONAPI_IDENTITY_EGO))
{
- json_decref (root_json);
- json_decref (data_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- json_decref (type_json);
- subsys_json = json_object_get (data_json, "subsystem");
+ subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res,
GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM);
if (!json_is_string (subsys_json))
{
- json_decref (root_json);
- json_decref (data_json);
+ json_decref (subsys_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
subsys = json_string_value (subsys_json);
GNUNET_asprintf (&handle->subsys, "%s", subsys);
json_decref (subsys_json);
- json_decref (root_json);
+ GNUNET_REST_jsonapi_object_delete (json_obj);
handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
handle->subsys,
ego_entry->ego,
Modified: gnunet/src/include/gnunet_rest_lib.h
===================================================================
--- gnunet/src/include/gnunet_rest_lib.h 2015-03-26 12:12:09 UTC (rev
35461)
+++ gnunet/src/include/gnunet_rest_lib.h 2015-03-26 14:57:05 UTC (rev
35462)
@@ -90,7 +90,7 @@
/**
* Responses for JSON API
*/
-struct JsonApiResponse;
+struct JsonApiObject;
/**
* Create a JSON API resource
@@ -124,18 +124,64 @@
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 JsonApiResponse*
-GNUNET_REST_jsonapi_response_new ();
+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
@@ -143,7 +189,7 @@
* @return a new JSON API resource or NULL on error.
*/
void
-GNUNET_REST_jsonapi_response_delete (struct JsonApiResponse *resp);
+GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp);
/**
* Add a JSON API resource to primary data
@@ -153,10 +199,29 @@
* @return the new number of resources
*/
void
-GNUNET_REST_jsonapi_response_resource_add (struct JsonApiResponse *resp,
+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
@@ -164,7 +229,7 @@
* @return the new number of resources
*/
void
-GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiResponse *resp,
+GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp,
struct JsonApiResource *res);
/**
@@ -175,7 +240,7 @@
* @return GNUNET_SYSERR on error else GNUNET_OK
*/
int
-GNUNET_REST_jsonapi_data_serialize (const struct JsonApiResponse *resp,
+GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp,
char **result);
/**
Modified: gnunet/src/rest/rest.c
===================================================================
--- gnunet/src/rest/rest.c 2015-03-26 12:12:09 UTC (rev 35461)
+++ gnunet/src/rest/rest.c 2015-03-26 14:57:05 UTC (rev 35462)
@@ -50,7 +50,7 @@
};
-struct JsonApiResponse
+struct JsonApiObject
{
/**
* DLL Resource
@@ -135,27 +135,164 @@
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))))
+ {
+ json_decref (value);
+ return GNUNET_NO;
+ }
+ json_decref (value);
+ 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, id, GNUNET_REST_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_REST_jsonapi_resource_check_type (const struct JsonApiResource
*resource,
+ const char* type)
+{
+ return check_resource_attr_str (resource, type,
GNUNET_REST_JSONAPI_KEY_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 JsonApiResponse*
-GNUNET_REST_jsonapi_response_new ()
+struct JsonApiObject*
+GNUNET_REST_jsonapi_object_new ()
{
- struct JsonApiResponse *result;
+ struct JsonApiObject *result;
- result = GNUNET_new (struct JsonApiResponse);
+ 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;
+ json_t *id_json;
+
+ id_json = json_object_get (res_json, GNUNET_REST_JSONAPI_KEY_ID);
+ type_json = json_object_get (res_json, GNUNET_REST_JSONAPI_KEY_TYPE);
+
+ if (!json_is_string (id_json) || !json_is_string (type_json))
+ return;
+
+ res = GNUNET_new (struct JsonApiResource);
+ 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 == res_count)
+ {
+ GNUNET_free (result);
+ result = NULL;
+ }
+ return result;
+}
+
+
+/**
* Delete a JSON API primary data
*
* @param type the JSON API resource type
@@ -163,7 +300,7 @@
* @return a new JSON API resource or NULL on error.
*/
void
-GNUNET_REST_jsonapi_response_delete (struct JsonApiResponse *resp)
+GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp)
{
struct JsonApiResource *res;
@@ -175,14 +312,14 @@
}
/**
- * Add a JSON API resource to primary data
+ * 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_response_resource_add (struct JsonApiResponse *resp,
+GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp,
struct JsonApiResource *res)
{
GNUNET_CONTAINER_DLL_insert (resp->res_list_head,
@@ -192,7 +329,45 @@
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
@@ -200,7 +375,7 @@
* @return the new number of resources
*/
void
-GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiResponse *resp,
+GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp,
struct JsonApiResource *res)
{
GNUNET_CONTAINER_DLL_remove (resp->res_list_head,
@@ -217,7 +392,7 @@
* @return GNUNET_SYSERR on error else GNUNET_OK
*/
int
-GNUNET_REST_jsonapi_data_serialize (const struct JsonApiResponse *resp,
+GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp,
char **result)
{
struct JsonApiResource *res;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r35462 - in gnunet/src: identity include rest,
gnunet <=