[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r35443 - in gnunet/src: identity include rest
From: |
gnunet |
Subject: |
[GNUnet-SVN] r35443 - in gnunet/src: identity include rest |
Date: |
Wed, 25 Mar 2015 16:58:38 +0100 |
Author: schanzen
Date: 2015-03-25 16:58:38 +0100 (Wed, 25 Mar 2015)
New Revision: 35443
Added:
gnunet/src/include/gnunet_rest_lib.h
gnunet/src/rest/rest.c
Modified:
gnunet/src/identity/Makefile.am
gnunet/src/identity/plugin_rest_identity.c
gnunet/src/include/gnunet_rest_plugin.h
gnunet/src/rest/Makefile.am
Log:
-start a lib for REST and json:api
Modified: gnunet/src/identity/Makefile.am
===================================================================
--- gnunet/src/identity/Makefile.am 2015-03-25 14:31:03 UTC (rev 35442)
+++ gnunet/src/identity/Makefile.am 2015-03-25 15:58:38 UTC (rev 35443)
@@ -32,6 +32,7 @@
plugin_rest_identity.c
libgnunet_plugin_rest_identity_la_LIBADD = \
$(top_builddir)/src/identity/libgnunetidentity.la \
+ $(top_builddir)/src/rest/libgnunetrest.la \
$(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \
$(LTLIBINTL) -ljansson -lmicrohttpd
libgnunet_plugin_rest_identity_la_LDFLAGS = \
Modified: gnunet/src/identity/plugin_rest_identity.c
===================================================================
--- gnunet/src/identity/plugin_rest_identity.c 2015-03-25 14:31:03 UTC (rev
35442)
+++ gnunet/src/identity/plugin_rest_identity.c 2015-03-25 15:58:38 UTC (rev
35443)
@@ -27,6 +27,7 @@
#include "platform.h"
#include "gnunet_rest_plugin.h"
#include "gnunet_identity_service.h"
+#include "gnunet_rest_lib.h"
#include "microhttpd.h"
#include <jansson.h>
@@ -40,9 +41,9 @@
#define URL_PARAM_SUBSYS "service"
-#define JSON_API_TYPE_EGO "ego"
+#define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego"
-#define JSON_API_TYPE_DATA "data"
+#define GNUNET_REST_JSONAPI_IDENTITY_KEY "key"
/**
* @brief struct returned by the initialization function of the plugin
@@ -168,28 +169,7 @@
};
-/**
- * Create s JSON Response for MHD
- *
- * @param data the JSON to return (can be NULL)
- * @return a MHD_Response handle
- */
-struct MHD_Response*
-create_json_response (const char *data)
-{
- size_t len;
- if (NULL == data)
- len = 0;
- else
- len = strlen (data);
- struct MHD_Response *resp = MHD_create_response_from_buffer (len,
- (void*)data,
-
MHD_RESPMEM_MUST_COPY);
- MHD_add_response_header
(resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
- return resp;
-}
-
/**
* Cleanup lookup handle
* @praram handle Handle to clean up
@@ -236,7 +216,7 @@
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct RequestHandle *handle = cls;
- struct MHD_Response *resp = create_json_response (NULL);
+ struct MHD_Response *resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
cleanup_handle (handle);
}
@@ -277,7 +257,7 @@
ego_json = json_object ();
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 (JSON_API_TYPE_EGO));
+ json_object_set_new (ego_json, "type", json_string
(GNUNET_REST_JSONAPI_IDENTITY_EGO));
json_object_set_new (ego_json, "key", json_string (keystring));
GNUNET_free (keystring);
break;
@@ -288,43 +268,18 @@
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- json_object_set (root_json, JSON_API_TYPE_DATA, ego_json);
+ json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, ego_json);
result_str = json_dumps (root_json, JSON_COMPACT);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
json_decref (ego_json);
json_decref (root_json);
- resp = create_json_response (result_str);
+ resp = GNUNET_REST_create_json_response (result_str);
handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
GNUNET_free (result_str);
cleanup_handle (handle);
}
/**
- * Validate the namespace of the requested url
- * TODO move this to a lib
- *
- * @param url the url
- * @param ns the namespace
- */
-int
-check_namespace (const char *url, const char *ns)
-{
- if (0 != strncmp (EGO_NAMESPACE, url, strlen (EGO_NAMESPACE)))
- {
- return GNUNET_NO;
- }
-
- if ((strlen (EGO_NAMESPACE) < strlen (url)) &&
- (url[strlen (EGO_NAMESPACE)] != '/'))
- {
- return GNUNET_NO;
- }
- return GNUNET_YES;
-
-
-}
-
-/**
* Create a response with requested ego(s)
*
* @param handle the RequestHandle
@@ -339,18 +294,19 @@
struct EgoEntry *ego_entry;
struct GNUNET_HashCode key;
struct MHD_Response *resp;
- json_t *ego_arr;
- json_t *ego_json;
- json_t *root_json;
+ struct JsonApiResponse *json_response;
+ struct JsonApiResource *json_resource;
+ json_t *key_str;
- if (GNUNET_NO == check_namespace (handle->url, EGO_NAMESPACE))
+ if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, EGO_NAMESPACE))
{
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
cleanup_handle (handle);
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 ==
@@ -371,8 +327,7 @@
}
}
}
- ego_arr = json_array ();
- root_json = json_object ();
+ json_response = GNUNET_REST_jsonapi_response_new ();
egoname = &handle->url[strlen (EGO_NAMESPACE)+1];
if (strlen (EGO_NAMESPACE) == strlen (handle->url))
@@ -387,36 +342,24 @@
{
if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
continue;
- ego_json = json_object ();
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, "key", json_string (keystring));
- json_object_set_new (ego_json, "type", json_string (JSON_API_TYPE_EGO));
+ json_resource = GNUNET_REST_jsonapi_resource_new
(GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->identifier);
+ key_str = json_string (keystring);
+ GNUNET_REST_jsonapi_resource_add_attr (json_resource,
+ GNUNET_REST_JSONAPI_IDENTITY_KEY,
+ key_str);
+ json_decref (key_str);
GNUNET_free (keystring);
- if (NULL == egoname)
- {
- json_array_append (ego_arr, ego_json);
- json_decref (ego_json);
- }
- else
- break;
+ GNUNET_REST_jsonapi_response_resource_add (json_response, json_resource);
}
- if (NULL == egoname)
- json_object_set (root_json, "egos", ego_arr);
- else
- json_object_set (root_json, "ego", ego_json);
- result_str = json_dumps (root_json, JSON_COMPACT);
- json_decref (ego_arr);
- if (NULL != egoname)
- json_decref (ego_json);
+ GNUNET_REST_jsonapi_data_serialize (json_response, &result_str);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
- resp = create_json_response (result_str);
+ resp = GNUNET_REST_create_json_response (result_str);
handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
GNUNET_free (result_str);
cleanup_handle (handle);
-
}
static void
@@ -431,7 +374,7 @@
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
cleanup_handle (handle);
}
@@ -448,7 +391,7 @@
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
cleanup_handle (handle);
}
@@ -465,7 +408,7 @@
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
cleanup_handle (handle);
}
@@ -503,7 +446,7 @@
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- data_json = json_object_get (root_json, JSON_API_TYPE_DATA);
+ data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA);
if ((NULL == data_json) || !json_is_object (data_json))
{
GNUNET_SCHEDULER_add_now (&do_error, handle);
@@ -511,11 +454,11 @@
}
type_json = json_object_get (data_json, "type");
if (!json_is_string (type_json) ||
- (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json))))
+ (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value
(type_json))))
{
json_decref (data_json);
json_decref (root_json);
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
cleanup_handle (handle);
return;
@@ -540,7 +483,7 @@
json_decref (egoname_json);
json_decref (data_json);
json_decref (root_json);
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
cleanup_handle (handle);
return;
@@ -591,7 +534,7 @@
}
if (GNUNET_NO == ego_exists)
{
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
cleanup_handle (handle);
return;
@@ -632,7 +575,7 @@
type_json = json_object_get (data_json, "type");
if (!json_is_string (type_json) ||
- (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json))))
+ (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value
(type_json))))
{
json_decref (root_json);
json_decref (data_json);
@@ -687,7 +630,7 @@
}
if (GNUNET_NO == ego_exists)
{
- resp = create_json_response (NULL);
+ resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
cleanup_handle (handle);
return;
Added: gnunet/src/include/gnunet_rest_lib.h
===================================================================
--- gnunet/src/include/gnunet_rest_lib.h (rev 0)
+++ gnunet/src/include/gnunet_rest_lib.h 2015-03-25 15:58:38 UTC (rev
35443)
@@ -0,0 +1,156 @@
+/*
+ This file is part of GNUnet
+ Copyright (C) 2010-2015 Christian Grothoff (and other contributing
authors)
+
+ 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, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * @file include/gnunet_rest_lib.h
+ * @brief API for helper library to parse/create REST
+ * @author Martin Schanzenbach
+ */
+#ifndef GNUNET_REST_LIB_H
+#define GNUNET_REST_LIB_H
+
+#include "gnunet_util_lib.h"
+#include "microhttpd.h"
+#include <jansson.h>
+
+#define GNUNET_REST_JSONAPI_KEY_DATA "data"
+
+#define GNUNET_REST_JSONAPI_KEY_ID "id"
+
+#define GNUNET_REST_JSONAPI_KEY_TYPE "type"
+
+/**
+ * Resource structs for JSON API
+ */
+struct JsonApiResource;
+
+/**
+ * Responses for JSON API
+ */
+struct JsonApiResponse;
+
+/**
+ * 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);
+
+/**
+ * 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 ();
+
+/**
+ * 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_response_delete (struct JsonApiResponse *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_response_resource_add (struct JsonApiResponse *resp,
+ struct JsonApiResource *res);
+
+/**
+ * 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 JsonApiResponse *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 JsonApiResponse *resp,
+ char **result);
+
+/**
+ * Check if namespace is in URL.
+ *
+ * @param url URL to check
+ * @param namespace namespace to check against
+ * @retun GNUNET_YES if namespace matches
+ */
+int
+GNUNET_REST_namespace_match (const char *url, const char *namespace);
+
+/**
+ * Create JSON API MHD response
+ *
+ * @param data JSON result
+ * @retun MHD response
+ */
+ struct MHD_Response*
+GNUNET_REST_create_json_response (const char *data);
+
+
+
+#endif
Modified: gnunet/src/include/gnunet_rest_plugin.h
===================================================================
--- gnunet/src/include/gnunet_rest_plugin.h 2015-03-25 14:31:03 UTC (rev
35442)
+++ gnunet/src/include/gnunet_rest_plugin.h 2015-03-25 15:58:38 UTC (rev
35443)
@@ -37,6 +37,7 @@
#endif
#endif
+
/**
* Iterator called on obtained result for a REST result.
*
Modified: gnunet/src/rest/Makefile.am
===================================================================
--- gnunet/src/rest/Makefile.am 2015-03-25 14:31:03 UTC (rev 35442)
+++ gnunet/src/rest/Makefile.am 2015-03-25 15:58:38 UTC (rev 35443)
@@ -20,6 +20,9 @@
XLIBS = -lgcov
endif
+lib_LTLIBRARIES = \
+ libgnunetrest.la
+
libexec_PROGRAMS = \
gnunet-rest-server
@@ -29,3 +32,12 @@
gnunet_rest_server_LDADD = \
$(top_builddir)/src/util/libgnunetutil.la \
$(GN_LIBINTL) -lmicrohttpd
+
+libgnunetrest_la_SOURCES = \
+ rest.c
+libgnunetrest_la_LIBADD = \
+ $(top_builddir)/src/util/libgnunetutil.la $(XLIB) \
+ $(GN_LIBINTL) -lmicrohttpd -ljansson
+libgnunetrest_la_LDFLAGS = \
+ $(GN_LIB_LDFLAGS) \
+ -version-info 0:0:0
Added: gnunet/src/rest/rest.c
===================================================================
--- gnunet/src/rest/rest.c (rev 0)
+++ gnunet/src/rest/rest.c 2015-03-25 15:58:38 UTC (rev 35443)
@@ -0,0 +1,299 @@
+/*
+ This file is part of GNUnet
+ Copyright (C) 2010-2015 Christian Grothoff (and other contributing
authors)
+
+ 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, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * @file rest/rest.c
+ * @brief helper library to create JSON REST Objects and handle REST
+ * responses/requests.
+ * @author Martin Schanzenbach
+ */
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_rest_lib.h"
+#include "microhttpd.h"
+#include <jansson.h>
+
+
+struct JsonApiResource
+{
+ /**
+ * DLL
+ */
+ struct JsonApiResource *next;
+
+ /**
+ * DLL
+ */
+ struct JsonApiResource *prev;
+
+ /**
+ * Resource content
+ */
+ json_t *res_obj;
+};
+
+
+struct JsonApiResponse
+{
+ /**
+ * 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->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;
+}
+
+
+
+
+/**
+ * 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 JsonApiResponse *result;
+
+ result = GNUNET_new (struct JsonApiResponse);
+ result->res_count = 0;
+ 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_response_delete (struct JsonApiResponse *resp)
+{
+ struct JsonApiResource *res;
+
+ for (res = resp->res_list_head;
+ res != NULL;
+ res = res->next)
+ GNUNET_REST_jsonapi_resource_delete (res);
+ GNUNET_free (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_response_resource_add (struct JsonApiResponse *resp,
+ struct JsonApiResource *res)
+{
+ GNUNET_CONTAINER_DLL_insert (resp->res_list_head,
+ resp->res_list_tail,
+ res);
+
+ resp->res_count++;
+}
+
+/**
+ * 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 JsonApiResponse *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 JsonApiResponse *resp,
+ char **result)
+{
+ struct JsonApiResource *res;
+ json_t *root_json;
+ json_t *res_arr;
+
+ if ( (NULL == resp) ||
+ (0 == resp->res_count) )
+ return GNUNET_SYSERR;
+
+ root_json = json_object ();
+
+ if (1 == resp->res_count)
+ {
+ json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA,
resp->res_list_head->res_obj);
+ }
+ else
+ {
+ 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_COMPACT);
+ return GNUNET_OK;
+}
+
+/**
+ * REST Utilities
+ */
+
+/**
+ * Check if namespace is in URL.
+ *
+ * @param url URL to check
+ * @param namespace namespace to check against
+ * @retun GNUNET_YES if namespace matches
+ */
+int
+GNUNET_REST_namespace_match (const char *url, const char *namespace)
+{
+ if (0 != strncmp (namespace, url, strlen (namespace)))
+ return GNUNET_NO;
+
+ if ((strlen (namespace) < strlen (url)) &&
+ (url[strlen (namespace)] != '/'))
+ return GNUNET_NO;
+
+ return GNUNET_YES;
+}
+
+/**
+ * Create JSON API MHD response
+ *
+ * @param data JSON result
+ * @retun MHD response
+ */
+ struct MHD_Response*
+GNUNET_REST_create_json_response (const char *data)
+{
+ struct MHD_Response *resp;
+ size_t len;
+
+ if (NULL == data)
+ len = 0;
+ else
+ len = strlen (data);
+ resp = MHD_create_response_from_buffer (len,
+ (void*)data,
+ MHD_RESPMEM_MUST_COPY);
+ MHD_add_response_header
(resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
+ return resp;
+
+}
+
+/* end of rest.c */
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r35443 - in gnunet/src: identity include rest,
gnunet <=