2 This file is part of GNUnet
3 Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief helper library to create JSON REST Objects and handle REST
25 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_rest_lib.h"
30 #include "microhttpd.h"
34 struct JsonApiResource
39 struct JsonApiResource *next;
44 struct JsonApiResource *prev;
53 struct JsonApiResponse
58 struct JsonApiResource *res_list_head;
63 struct JsonApiResource *res_list_tail;
76 * Create a JSON API resource
78 * @param type the JSON API resource type
79 * @param id the JSON API resource id
80 * @return a new JSON API resource or NULL on error.
82 struct JsonApiResource*
83 GNUNET_REST_jsonapi_resource_new (const char *type, const char *id)
85 struct JsonApiResource *res;
87 if ( (NULL == type) || (0 == strlen (type)) )
89 if ( (NULL == id) || (0 == strlen (id)) )
92 res = GNUNET_new (struct JsonApiResource);
94 res->res_obj = json_object ();
96 json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_ID, json_string (id));
97 json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_TYPE, json_string (type));
103 * Delete a JSON API resource
105 * @param res the JSON resource
106 * @param result Pointer where the resource should be stored
109 GNUNET_REST_jsonapi_resource_delete (struct JsonApiResource *resource)
111 json_decref (resource->res_obj);
112 GNUNET_free (resource);
116 * Add a JSON API attribute
118 * @param res the JSON resource
119 * @param key the key for the attribute
120 * @param json the json_t attribute to add
121 * @return #GNUNET_OK if added successfully
122 * #GNUNET_SYSERR if not
125 GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource,
129 if ( (NULL == resource) ||
132 return GNUNET_SYSERR;
133 json_object_set (resource->res_obj, key, json);
141 * Create a JSON API primary data
143 * @param type the JSON API resource type
144 * @param id the JSON API resource id
145 * @return a new JSON API resource or NULL on error.
147 struct JsonApiResponse*
148 GNUNET_REST_jsonapi_response_new ()
150 struct JsonApiResponse *result;
152 result = GNUNET_new (struct JsonApiResponse);
153 result->res_count = 0;
158 * Delete a JSON API primary data
160 * @param type the JSON API resource type
161 * @param id the JSON API resource id
162 * @return a new JSON API resource or NULL on error.
165 GNUNET_REST_jsonapi_response_delete (struct JsonApiResponse *resp)
167 struct JsonApiResource *res;
169 for (res = resp->res_list_head;
172 GNUNET_REST_jsonapi_resource_delete (res);
177 * Add a JSON API resource to primary data
179 * @param data The JSON API data to add to
180 * @param res the JSON API resource to add
181 * @return the new number of resources
184 GNUNET_REST_jsonapi_response_resource_add (struct JsonApiResponse *resp,
185 struct JsonApiResource *res)
187 GNUNET_CONTAINER_DLL_insert (resp->res_list_head,
195 * Add a JSON API resource to primary data
197 * @param data The JSON API data to add to
198 * @param res the JSON API resource to add
199 * @return the new number of resources
202 GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiResponse *resp,
203 struct JsonApiResource *res)
205 GNUNET_CONTAINER_DLL_remove (resp->res_list_head,
212 * String serialze jsonapi primary data
214 * @param data the JSON API primary data
215 * @param result where to store the result
216 * @return GNUNET_SYSERR on error else GNUNET_OK
219 GNUNET_REST_jsonapi_data_serialize (const struct JsonApiResponse *resp,
222 struct JsonApiResource *res;
226 if ( (NULL == resp) ||
227 (0 == resp->res_count) )
228 return GNUNET_SYSERR;
230 root_json = json_object ();
232 if (1 == resp->res_count)
234 json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, resp->res_list_head->res_obj);
238 res_arr = json_array ();
239 for (res = resp->res_list_head;
243 json_array_append (res_arr, res->res_obj);
245 json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr);
247 *result = json_dumps (root_json, JSON_COMPACT);
256 * Check if namespace is in URL.
258 * @param url URL to check
259 * @param namespace namespace to check against
260 * @retun GNUNET_YES if namespace matches
263 GNUNET_REST_namespace_match (const char *url, const char *namespace)
265 if (0 != strncmp (namespace, url, strlen (namespace)))
268 if ((strlen (namespace) < strlen (url)) &&
269 (url[strlen (namespace)] != '/'))
276 * Create JSON API MHD response
278 * @param data JSON result
279 * @retun MHD response
282 GNUNET_REST_create_json_response (const char *data)
284 struct MHD_Response *resp;
291 resp = MHD_create_response_from_buffer (len,
293 MHD_RESPMEM_MUST_COPY);
294 MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");