2 This file is part of GNUnet
3 Copyright (C) 2010-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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;
58 struct JsonApiResource *res_list_head;
63 struct JsonApiResource *res_list_tail;
77 * Create a JSON API resource
79 * @param type the JSON API resource type
80 * @param id the JSON API resource id
81 * @return a new JSON API resource or NULL on error.
83 struct JsonApiResource*
84 GNUNET_REST_jsonapi_resource_new (const char *type, const char *id)
86 struct JsonApiResource *res;
88 if ( (NULL == type) || (0 == strlen (type)) )
90 if ( (NULL == id) || (0 == strlen (id)) )
93 res = GNUNET_new (struct JsonApiResource);
97 res->res_obj = json_object ();
99 json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_ID, json_string (id));
100 json_object_set_new (res->res_obj, GNUNET_REST_JSONAPI_KEY_TYPE, json_string (type));
106 * Delete a JSON API resource
108 * @param res the JSON resource
109 * @param result Pointer where the resource should be stored
112 GNUNET_REST_jsonapi_resource_delete (struct JsonApiResource *resource)
114 json_decref (resource->res_obj);
115 GNUNET_free (resource);
119 * Add a JSON API attribute
121 * @param res the JSON resource
122 * @param key the key for the attribute
123 * @param json the json_t attribute to add
124 * @return #GNUNET_OK if added successfully
125 * #GNUNET_SYSERR if not
128 GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource,
132 if ( (NULL == resource) ||
135 return GNUNET_SYSERR;
136 json_object_set (resource->res_obj, key, json);
141 * Read a JSON API attribute
143 * @param res the JSON resource
144 * @param key the key for the attribute
145 * @return the json_t object
148 GNUNET_REST_jsonapi_resource_read_attr (const struct JsonApiResource *resource,
151 if ( (NULL == resource) ||
154 return json_object_get (resource->res_obj, key);
158 check_resource_attr_str (const struct JsonApiResource *resource,
163 if ( (NULL == resource) ||
167 value = json_object_get (resource->res_obj, key);
170 if (!json_is_string (value) ||
171 (0 != strcmp (attr, json_string_value(value))))
179 * Check a JSON API resource id
181 * @param res the JSON resource
182 * @param id the expected id
183 * @return GNUNET_YES if id matches
186 GNUNET_REST_jsonapi_resource_check_id (const struct JsonApiResource *resource,
189 return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_ID, id);
194 * Check a JSON API resource type
196 * @param res the JSON resource
197 * @param type the expected type
198 * @return GNUNET_YES if id matches
201 GNUNET_REST_jsonapi_resource_check_type (const struct JsonApiResource *resource,
204 return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_TYPE, type);
209 * Create a JSON API primary data
211 * @return a new JSON API resource or NULL on error.
213 struct JsonApiObject*
214 GNUNET_REST_jsonapi_object_new ()
216 struct JsonApiObject *result;
218 result = GNUNET_new (struct JsonApiObject);
219 result->res_count = 0;
225 add_json_resource (struct JsonApiObject *obj,
226 const json_t *res_json)
228 struct JsonApiResource *res;
231 type_json = json_object_get (res_json, GNUNET_REST_JSONAPI_KEY_TYPE);
232 if (!json_is_string (type_json))
234 res = GNUNET_new (struct JsonApiResource);
237 res->res_obj = json_deep_copy (res_json);
238 GNUNET_REST_jsonapi_object_resource_add (obj, res);
242 * Create a JSON API primary data from a string
244 * @param data the string of the JSON API data
245 * @return a new JSON API resource or NULL on error.
247 struct JsonApiObject*
248 GNUNET_REST_jsonapi_object_parse (const char* data)
250 struct JsonApiObject *result;
258 root_json = json_loads (data, 0, &error);
260 if ( (NULL == root_json) || !json_is_object (root_json))
262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "json error: %s", error.text);
265 data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA);
266 if (NULL == data_json)
268 json_decref (root_json);
272 result = GNUNET_new (struct JsonApiObject);
273 result->res_count = 0;
274 if (json_is_object (data_json))
275 add_json_resource (result, data_json);
276 else if (json_is_array (data_json))
278 res_count = json_array_size (data_json);
279 for (i = 0; i < res_count; i++)
280 add_json_resource (result, json_array_get (data_json, i));
282 json_decref (root_json);
283 if (0 == result->res_count)
285 GNUNET_free (result);
293 * Delete a JSON API primary data
295 * @param type the JSON API resource type
296 * @param id the JSON API resource id
297 * @return a new JSON API resource or NULL on error.
300 GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp)
302 struct JsonApiResource *res;
303 struct JsonApiResource *res_next;
305 for (res = resp->res_list_head;
308 res_next = res->next;
309 GNUNET_CONTAINER_DLL_remove (resp->res_list_head,
312 GNUNET_REST_jsonapi_resource_delete (res);
319 * Add a JSON API object to primary data
321 * @param data The JSON API data to add to
322 * @param res the JSON API resource to add
323 * @return the new number of resources
326 GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp,
327 struct JsonApiResource *res)
329 GNUNET_CONTAINER_DLL_insert (resp->res_list_head,
338 * Get a JSON API object resource count
340 * @param resp the JSON API object
341 * @return the number of resources
344 GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp)
346 return resp->res_count;
350 * Get a JSON API object resource num
352 * @param resp the JSON API object
353 * @param num the number of the resource
354 * @return the resource
356 struct JsonApiResource*
357 GNUNET_REST_jsonapi_object_get_resource (struct JsonApiObject *resp, int num)
359 struct JsonApiResource *res;
362 if ((0 == resp->res_count) ||
363 (num >= resp->res_count))
365 res = resp->res_list_head;
366 for (i = 0; i < num; i++)
375 * Add a JSON API resource to primary data
377 * @param data The JSON API data to add to
378 * @param res the JSON API resource to add
379 * @return the new number of resources
382 GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp,
383 struct JsonApiResource *res)
385 GNUNET_CONTAINER_DLL_remove (resp->res_list_head,
392 * String serialze jsonapi primary data
394 * @param data the JSON API primary data
395 * @param result where to store the result
396 * @return GNUNET_SYSERR on error else GNUNET_OK
399 GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp,
402 struct JsonApiResource *res;
407 return GNUNET_SYSERR;
409 root_json = json_object ();
410 res_arr = json_array ();
411 for (res = resp->res_list_head;
415 json_array_append (res_arr, res->res_obj);
417 json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr);
418 *result = json_dumps (root_json, JSON_INDENT(2));
419 json_decref (root_json);
420 json_decref (res_arr);
429 * Check if namespace is in URL.
431 * @param url URL to check
432 * @param namespace namespace to check against
433 * @retun GNUNET_YES if namespace matches
436 GNUNET_REST_namespace_match (const char *url, const char *namespace)
438 return 0 == strncmp (namespace, url, strlen (namespace));
442 * Create JSON API MHD response
444 * @param data JSON result
445 * @retun MHD response
448 GNUNET_REST_create_json_response (const char *data)
450 struct MHD_Response *resp;
460 resp = MHD_create_response_from_buffer (len,
462 MHD_RESPMEM_MUST_COPY);
463 MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
469 GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn,
470 const struct GNUNET_REST_RestConnectionHandler *handlers,
478 while (NULL != handlers[count].method)
481 GNUNET_asprintf (&url, "%s", conn->url);
482 if (url[strlen (url)-1] == '/')
483 url[strlen (url)-1] = '\0';
484 for (i = 0; i < count; i++)
486 if (0 != strcasecmp (conn->method, handlers[i].method))
488 if (strlen (url) < strlen (handlers[i].namespace))
490 if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
493 handlers[i].proc (conn, (const char*)url, cls);