-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / identity / plugin_rest_identity.c
index 52685c52e2d3f832fcf14b4b2c9b1596c7cb2de4..fd6562f1dbb648ae9a8978783e2f3334fd710284 100644 (file)
@@ -17,6 +17,7 @@
    */
 /**
  * @author Martin Schanzenbach
+ * @author Philippe Buschmann
  * @file identity/plugin_rest_identity.c
  * @brief GNUnet Namestore REST plugin
  *
@@ -26,8 +27,6 @@
 #include "gnunet_rest_plugin.h"
 #include "gnunet_identity_service.h"
 #include "gnunet_rest_lib.h"
-#include "gnunet_jsonapi_lib.h"
-#include "gnunet_jsonapi_util.h"
 #include "microhttpd.h"
 #include <jansson.h>
 #include "gnunet_signatures.h"
 /**
  * Resource type
  */
-#define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego"
+#define GNUNET_REST_JSON_IDENTITY_EGO "ego"
 
 /**
  * Name attribute
  */
-#define GNUNET_REST_JSONAPI_IDENTITY_NAME "name"
+#define GNUNET_REST_JSON_IDENTITY_NAME "name"
 
 /**
  * Attribute to rename "name" TODO we changed id to the pubkey
  * so this can be unified with "name"
  */
-#define GNUNET_REST_JSONAPI_IDENTITY_NEWNAME "newname"
+#define GNUNET_REST_JSON_IDENTITY_NEWNAME "newname"
 
 /**
  * URL parameter to change the subsytem for ego
  */
-#define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem"
+#define GNUNET_REST_JSON_IDENTITY_SUBSYSTEM "subsystem"
 
 
 /**
@@ -74,6 +73,7 @@
  */
 #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
 #define GNUNET_REST_ERROR_NO_DATA "No data"
+#define GNUNET_REST_ERROR_DATA_INVALID "Data invalid"
 
 /**
  * GNUid token lifetime
@@ -276,16 +276,16 @@ do_error (void *cls)
   struct MHD_Response *resp;
   char *json_error;
 
-  GNUNET_asprintf (&json_error,
-                   "{Error while processing request: %s}",
-                   &handle->emsg);
+  if (NULL == handle->emsg)
+    handle->emsg = GNUNET_strdup("Unknown Error");
+
+  GNUNET_asprintf (&json_error, "{\"error\": \"%s\"}", handle->emsg);
+  handle->response_code = MHD_HTTP_OK;
 
   resp = GNUNET_REST_create_response (json_error);
-  handle->proc (handle->proc_cls,
-               resp,
-               handle->response_code);
+  handle->proc (handle->proc_cls, resp, handle->response_code);
   cleanup_handle (handle);
-  GNUNET_free (json_error);
+  GNUNET_free(json_error);
 }
 
 
@@ -304,14 +304,15 @@ get_ego_for_subsys (void *cls,
                     const char *name)
 {
   struct RequestHandle *handle = cls;
-  struct GNUNET_JSONAPI_Document *json_document;
-  struct GNUNET_JSONAPI_Resource *json_resource;
   struct EgoEntry *ego_entry;
   struct MHD_Response *resp;
+  json_t *json_root;
+  json_t *json_ego;
   json_t *name_json;
   char *result_str;
+  size_t index;
 
-  json_document = GNUNET_JSONAPI_document_new ();
+  json_root = json_array();
 
   for (ego_entry = handle->ego_head;
        NULL != ego_entry;
@@ -321,32 +322,38 @@ get_ego_for_subsys (void *cls,
       continue;
     if (NULL == name)
       continue;
-    json_resource = GNUNET_JSONAPI_resource_new
-      (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring);
+
+    json_ego = json_object();
     name_json = json_string (ego_entry->identifier);
-    GNUNET_JSONAPI_resource_add_attr (json_resource,
-                                           GNUNET_REST_JSONAPI_IDENTITY_NAME,
-                                           name_json);
-    json_decref (name_json);
-    GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
+    json_object_set_new(json_ego, GNUNET_REST_JSON_IDENTITY_EGO, name_json);
+    json_array_append(json_root, json_ego);
+
     break;
   }
-  if (0 == GNUNET_JSONAPI_document_resource_count (json_document))
+
+  if (0 == json_array_size(json_root))
   {
-    GNUNET_JSONAPI_document_delete (json_document);
+    json_decref(json_root);
     handle->emsg = GNUNET_strdup("No identity matches results!");
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  GNUNET_JSONAPI_document_serialize (json_document, &result_str);
+
+  result_str = json_dumps(json_root, 0);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
   resp = GNUNET_REST_create_response (result_str);
-  GNUNET_JSONAPI_document_delete (json_document);
+
+  json_array_foreach(json_root, index, json_ego )
+  {
+    json_decref(json_ego);
+  }
+  json_decref (json_root);
   handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
   GNUNET_free (result_str);
   cleanup_handle (handle);
 }
 
+
 /**
  * Create a response with requested ego(s)
  *
@@ -367,9 +374,10 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con,
   struct EgoEntry *ego_entry;
   struct GNUNET_HashCode key;
   struct MHD_Response *resp;
-  struct GNUNET_JSONAPI_Document *json_document;
-  struct GNUNET_JSONAPI_Resource *json_resource;
+  json_t *json_root;
+  json_t *json_ego;
   json_t *name_str;
+  size_t index;
 
   if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY))
   {
@@ -395,8 +403,8 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con,
   }
 
   if ( NULL == egoname ) {
-    GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM,
-                        strlen (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM),
+    GNUNET_CRYPTO_hash (GNUNET_REST_JSON_IDENTITY_SUBSYSTEM,
+                        strlen (GNUNET_REST_JSON_IDENTITY_SUBSYSTEM),
                         &key);
     if ( GNUNET_YES ==
          GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
@@ -417,7 +425,7 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con,
     }
   }
 
-  json_document = GNUNET_JSONAPI_document_new ();
+  json_root = json_array();
 
   //Return all egos
   for (ego_entry = handle->ego_head;
@@ -426,27 +434,35 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con,
   {
     if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
       continue;
-    json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO,
-                                                      ego_entry->keystring);
+
+    json_ego = json_object();
+
+    json_object_set_new( json_ego, "id", json_string (ego_entry->keystring));
+    json_object_set_new( json_ego, "type", json_string (GNUNET_REST_JSON_IDENTITY_EGO));
     name_str = json_string (ego_entry->identifier);
-    GNUNET_JSONAPI_resource_add_attr (
-                                           json_resource,
-                                           GNUNET_REST_JSONAPI_IDENTITY_NAME,
-                                           name_str);
-    json_decref (name_str);
-    GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
+    json_object_set_new( json_ego, "name", name_str);
+
+    json_array_append( json_root, json_ego );
   }
-  if (0 == GNUNET_JSONAPI_document_resource_count (json_document))
+
+  if ((size_t)0 == json_array_size(json_root))
   {
-    GNUNET_JSONAPI_document_delete (json_document);
+    json_decref (json_root);
     handle->emsg = GNUNET_strdup ("No identities found!");
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  GNUNET_JSONAPI_document_serialize (json_document, &result_str);
+
+  result_str = json_dumps(json_root, 0);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
   resp = GNUNET_REST_create_response (result_str);
-  GNUNET_JSONAPI_document_delete (json_document);
+
+  //delete json_objects in json_array with macro
+  json_array_foreach(json_root, index, json_ego )
+  {
+    json_decref(json_ego);
+  }
+  json_decref (json_root);
   handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
   GNUNET_free (result_str);
   cleanup_handle (handle);
@@ -472,7 +488,7 @@ do_finished (void *cls, const char *emsg)
     return;
   }
   resp = GNUNET_REST_create_response (NULL);
-  handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
+  handle->proc (handle->proc_cls, resp, handle->response_code);
   cleanup_handle (handle);
 }
 
@@ -491,23 +507,19 @@ ego_create_cont (struct GNUNET_REST_RequestHandle *con,
   struct RequestHandle *handle = cls;
   struct EgoEntry *ego_entry;
   struct MHD_Response *resp;
-  struct GNUNET_JSONAPI_Document *json_obj;
-  struct GNUNET_JSONAPI_Resource *json_res;
   json_t *egoname_json;
   json_t *data_js;
   json_error_t err;
   const char* egoname;
   char term_data[handle->data_size+1];
-  struct GNUNET_JSON_Specification docspec[] = {
-    GNUNET_JSON_spec_jsonapi_document (&json_obj),
-    GNUNET_JSON_spec_end()
-  };
+
   if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url))
   {
     handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
+
   if (0 >= handle->data_size)
   {
     handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
@@ -519,50 +531,54 @@ ego_create_cont (struct GNUNET_REST_RequestHandle *con,
   data_js = json_loads (term_data,
                         JSON_DECODE_ANY,
                         &err);
-  GNUNET_assert (NULL != data_js);
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_JSON_parse (data_js, docspec,
-                                    NULL, NULL));
 
-  json_decref (data_js);
 
-  if (NULL == json_obj)
+  if (NULL == data_js)
   {
+    handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_DATA_INVALID);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  if (1 != GNUNET_JSONAPI_document_resource_count (json_obj))
+  //instead of parse
+  if (!json_is_object(data_js))
   {
-    GNUNET_JSONAPI_document_delete (json_obj);
-    handle->emsg = GNUNET_strdup ("Provided resource count invalid");
+    json_decref(data_js);
+    handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_DATA_INVALID);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0);
-  if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
+
+  if (1 != json_object_size (data_js))
   {
-    GNUNET_JSONAPI_document_delete (json_obj);
-    resp = GNUNET_REST_create_response (NULL);
-    handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
-    cleanup_handle (handle);
+    json_decref (data_js);
+    handle->emsg = GNUNET_strdup("Provided resource count invalid");
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  egoname_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME);
+
+  egoname_json = json_object_get (data_js, GNUNET_REST_JSON_IDENTITY_NAME);
   if (!json_is_string (egoname_json))
   {
-    GNUNET_JSONAPI_document_delete (json_obj);
+    json_decref (data_js);
     handle->emsg = GNUNET_strdup ("No name provided");
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   egoname = json_string_value (egoname_json);
+  if(0 >= strlen(egoname))
+  {
+    json_decref (data_js);
+    handle->emsg = GNUNET_strdup ("No name provided");
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
   for (ego_entry = handle->ego_head;
        NULL != ego_entry;
        ego_entry = ego_entry->next)
   {
     if (0 == strcasecmp (egoname, ego_entry->identifier))
     {
-      GNUNET_JSONAPI_document_delete (json_obj);
+      json_decref (data_js);
       resp = GNUNET_REST_create_response (NULL);
       handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
       cleanup_handle (handle);
@@ -570,7 +586,8 @@ ego_create_cont (struct GNUNET_REST_RequestHandle *con,
     }
   }
   GNUNET_asprintf (&handle->name, "%s", egoname);
-  GNUNET_JSONAPI_document_delete (json_obj);
+  json_decref (data_js);
+  handle->response_code = MHD_HTTP_CREATED;
   handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
                                        handle->name,
                                        &do_finished,
@@ -590,8 +607,6 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con,
                const char *url,
                void *cls)
 {
-  struct GNUNET_JSONAPI_Document *json_obj;
-  struct GNUNET_JSONAPI_Resource *json_res;
   struct RequestHandle *handle = cls;
   struct EgoEntry *ego_entry;
   struct EgoEntry *ego_entry_tmp;
@@ -605,10 +620,6 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con,
   const char *newname;
   char term_data[handle->data_size+1];
   int ego_exists = GNUNET_NO;
-  struct GNUNET_JSON_Specification docspec[] = {
-    GNUNET_JSON_spec_jsonapi_document (&json_obj),
-    GNUNET_JSON_spec_end()
-  };
 
   if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url))
   {
@@ -649,43 +660,40 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con,
   data_js = json_loads (term_data,
                         JSON_DECODE_ANY,
                         &err);
-  GNUNET_assert (NULL != data_js);
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_JSON_parse (data_js, docspec,
-                                    NULL, NULL));
-
-  json_decref (data_js);
-
-  if (NULL == json_obj)
+  if (NULL == data_js)
   {
-    handle->emsg = GNUNET_strdup ("Data invalid");
+    handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-
-  if (1 != GNUNET_JSONAPI_document_resource_count (json_obj))
+  if (!json_is_object(data_js))
   {
-    GNUNET_JSONAPI_document_delete (json_obj);
-    handle->emsg = GNUNET_strdup ("Resource amount invalid");
+    json_decref (data_js);
+    handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_DATA_INVALID);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0);
 
-  if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
+  if (1 != json_object_size(data_js))
   {
-    GNUNET_JSONAPI_document_delete (json_obj);
-    handle->emsg = GNUNET_strdup ("Resource type invalid");
+    json_decref (data_js);
+    handle->emsg = GNUNET_strdup ("Resource amount invalid");
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 
   //This is a rename
-  name_json = GNUNET_JSONAPI_resource_read_attr (json_res,
-                                                 GNUNET_REST_JSONAPI_IDENTITY_NEWNAME);
+  name_json = json_object_get (data_js, GNUNET_REST_JSON_IDENTITY_NEWNAME);
   if ((NULL != name_json) && json_is_string (name_json))
   {
     newname = json_string_value (name_json);
+    if(0 >= strlen(newname))
+    {
+      json_decref (data_js);
+      handle->emsg = GNUNET_strdup ("No name provided");
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
+      return;
+    }
     for (ego_entry_tmp = handle->ego_head;
          NULL != ego_entry_tmp;
          ego_entry_tmp = ego_entry_tmp->next)
@@ -694,29 +702,38 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con,
           0 != strcasecmp (keystring, ego_entry_tmp->keystring))
       {
         //Ego with same name not allowed
-        GNUNET_JSONAPI_document_delete (json_obj);
+       json_decref (data_js);
         resp = GNUNET_REST_create_response (NULL);
         handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
         cleanup_handle (handle);
         return;
       }
     }
+    handle->response_code = MHD_HTTP_NO_CONTENT;
     handle->op = GNUNET_IDENTITY_rename (handle->identity_handle,
                                          ego_entry->identifier,
                                          newname,
                                          &do_finished,
                                          handle);
-    GNUNET_JSONAPI_document_delete (json_obj);
+    json_decref (data_js);
     return;
   }
 
   //Set subsystem
-  subsys_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM);
+  subsys_json = json_object_get (data_js, GNUNET_REST_JSON_IDENTITY_SUBSYSTEM);
   if ( (NULL != subsys_json) && json_is_string (subsys_json))
   {
     subsys = json_string_value (subsys_json);
+    if(0 >= strlen(subsys))
+    {
+      json_decref (data_js);
+      handle->emsg = GNUNET_strdup ("No name provided");
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
+      return;
+    }
     GNUNET_asprintf (&handle->subsys, "%s", subsys);
-    GNUNET_JSONAPI_document_delete (json_obj);
+    json_decref (data_js);
+    handle->response_code = MHD_HTTP_NO_CONTENT;
     handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
                                       handle->subsys,
                                       ego_entry->ego,
@@ -724,11 +741,18 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con,
                                       handle);
     return;
   }
-  GNUNET_JSONAPI_document_delete (json_obj);
+  json_decref (data_js);
   handle->emsg = GNUNET_strdup ("Subsystem not provided");
   GNUNET_SCHEDULER_add_now (&do_error, handle);
 }
 
+/**
+ * Handle ego delete request
+ *
+ * @param con_handle the connection handle
+ * @param url the url
+ * @param cls the RequestHandle
+ */
 void
 ego_delete_cont (struct GNUNET_REST_RequestHandle *con_handle,
                  const char* url,
@@ -764,6 +788,7 @@ ego_delete_cont (struct GNUNET_REST_RequestHandle *con_handle,
     cleanup_handle (handle);
     return;
   }
+  handle->response_code = MHD_HTTP_NO_CONTENT;
   handle->op = GNUNET_IDENTITY_delete (handle->identity_handle,
                                        ego_entry->identifier,
                                        &do_finished,
@@ -815,7 +840,7 @@ init_cont (struct RequestHandle *handle)
     GNUNET_REST_HANDLER_END
   };
 
-  if (GNUNET_NO == GNUNET_JSONAPI_handle_request (handle->conndata_handle,
+  if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle,
                                                   handlers,
                                                   &err,
                                                   handle))
@@ -907,7 +932,7 @@ rest_identity_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
 
 
   handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
-
+  handle->response_code = MHD_HTTP_OK;
   handle->proc_cls = proc_cls;
   handle->proc = proc;
   handle->state = ID_REST_STATE_INIT;