-fix (C) notices
[oweals/gnunet.git] / src / rest / rest.c
index f3251db1a23d4047280ab6bfcdd844117a2a5ccb..aa0ab7ccd516af1a1f7090d4d753ac504dc75620 100644 (file)
@@ -1,6 +1,6 @@
 /*
       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
@@ -14,8 +14,8 @@
 
       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.
  */
 
 /**
@@ -170,10 +170,8 @@ check_resource_attr_str (const struct JsonApiResource *resource,
   if (!json_is_string (value) ||
       (0 != strcmp (attr, json_string_value(value))))
   {
-    json_decref (value);
     return GNUNET_NO;
   }
-  json_decref (value);
   return GNUNET_YES;
 }
 
@@ -188,7 +186,7 @@ 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);  
+  return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_ID, id);  
 }
 
 
@@ -203,7 +201,7 @@ 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);  
+  return check_resource_attr_str (resource, GNUNET_REST_JSONAPI_KEY_TYPE, type);  
 }
 
 
@@ -229,14 +227,10 @@ add_json_resource (struct JsonApiObject *obj,
 {
   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;
@@ -259,7 +253,6 @@ GNUNET_REST_jsonapi_object_parse (const char* data)
   json_error_t error;
   int res_count = 0;
   int i;
-  
   if (NULL == data)
     return NULL;
   root_json = json_loads (data, 0, &error);
@@ -287,7 +280,7 @@ GNUNET_REST_jsonapi_object_parse (const char* data)
       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;
@@ -312,10 +305,10 @@ GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp)
   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;
   }
@@ -350,11 +343,11 @@ GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp,
 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
@@ -366,8 +359,8 @@ 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))
+  if ((0 == resp->res_count) ||
+      (num >= resp->res_count))
     return NULL;
   res = resp->res_list_head;
   for (i = 0; i < num; i++)
@@ -410,28 +403,21 @@ GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp,
   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;
 }
 
@@ -439,24 +425,17 @@ GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp,
  * 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));
 }
 
 /**
@@ -505,11 +484,11 @@ GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn,
   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);