/*
This file is part of GNUnet
- Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2010-2015 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
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.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
if (!json_is_string (value) ||
(0 != strcmp (attr, json_string_value(value))))
{
- json_decref (value);
return GNUNET_NO;
}
- json_decref (value);
return GNUNET_YES;
}
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);
+ return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_ID, id);
}
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);
+ return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_TYPE, type);
}
{
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))
+ if (!json_is_string (type_json))
return;
-
res = GNUNET_new (struct JsonApiResource);
res->next = NULL;
res->prev = NULL;
json_error_t error;
int res_count = 0;
int i;
-
if (NULL == data)
return NULL;
root_json = json_loads (data, 0, &error);
add_json_resource (result, json_array_get (data_json, i));
}
json_decref (root_json);
- if (0 == res_count)
+ if (0 == result->res_count)
{
GNUNET_free (result);
result = NULL;
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);
- res_next = res->next;
GNUNET_REST_jsonapi_resource_delete (res);
res = res_next;
}
int
GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp)
{
- return resp->res_count++;
+ return resp->res_count;
}
/**
- * Get a JSON API object resource #num
+ * Get a JSON API object resource num
*
* @param resp the JSON API object
* @param num the number of the resource
struct JsonApiResource *res;
int i;
- if ((0 < resp->res_count) &&
- (num < resp->res_count))
+ if ((0 == resp->res_count) ||
+ (num >= resp->res_count))
return NULL;
res = resp->res_list_head;
for (i = 0; i < num; i++)
json_t *root_json;
json_t *res_arr;
- if ( (NULL == resp) ||
- (0 == resp->res_count) )
+ if ((NULL == resp))
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)
{
- 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);
+ 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
*/
-/**
- * Check if namespace is in URL.
- *
- * @param url URL to check
- * @param namespace namespace to check against
- * @retun GNUNET_YES if namespace matches
- */
+ /**
+ * 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;
+ return 0 == strncmp (namespace, url, strlen (namespace));
}
/**
for (i = 0; i < count; i++)
{
if (0 != strcasecmp (conn->method, handlers[i].method))
- break;
+ continue;
if (strlen (url) < strlen (handlers[i].namespace))
- break;
+ continue;
if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
- break;
+ continue;
//Match
handlers[i].proc (conn, (const char*)url, cls);
GNUNET_free (url);