gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r35386 - in gnunet/src: gns identity include rest


From: gnunet
Subject: [GNUnet-SVN] r35386 - in gnunet/src: gns identity include rest
Date: Fri, 13 Mar 2015 16:33:47 +0100

Author: schanzen
Date: 2015-03-13 16:33:47 +0100 (Fri, 13 Mar 2015)
New Revision: 35386

Modified:
   gnunet/src/gns/Makefile.am
   gnunet/src/gns/plugin_rest_gns.c
   gnunet/src/identity/plugin_rest_identity.c
   gnunet/src/include/gnunet_rest_plugin.h
   gnunet/src/rest/gnunet-rest-server.c
Log:
-change API

Modified: gnunet/src/gns/Makefile.am
===================================================================
--- gnunet/src/gns/Makefile.am  2015-03-13 14:35:10 UTC (rev 35385)
+++ gnunet/src/gns/Makefile.am  2015-03-13 15:33:47 UTC (rev 35386)
@@ -102,7 +102,7 @@
   $(top_builddir)/src/gns/libgnunetgns.la \
   $(top_builddir)/src/identity/libgnunetidentity.la \
   $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \
-  $(LTLIBINTL) -ljansson
+  $(LTLIBINTL) -ljansson -lmicrohttpd
 libgnunet_plugin_rest_gns_la_LDFLAGS = \
  $(GN_PLUGIN_LDFLAGS)
 

Modified: gnunet/src/gns/plugin_rest_gns.c
===================================================================
--- gnunet/src/gns/plugin_rest_gns.c    2015-03-13 14:35:10 UTC (rev 35385)
+++ gnunet/src/gns/plugin_rest_gns.c    2015-03-13 15:33:47 UTC (rev 35386)
@@ -35,6 +35,14 @@
 
 #define API_NAMESPACE "/gns"
 
+#define GNUNET_REST_JSON_ATTR_ID "id"
+
+#define GNUNET_REST_JSON_ATTR_TYPE "type"
+
+#define GNUNET_GNS_JSON_RECORD_TYPE "rtype"
+
+#define GNUNET_REST_JSON_ATTR_DATA "data"
+
 /**
  * @brief struct returned by the initialization function of the plugin
  */
@@ -190,6 +198,27 @@
 
 
 /**
+ * Create s JSON Response for MHD
+ * TODO move to lib
+ * @param data the JSON to return (can be NULL)
+ * @return a MHD_Response handle
+ */
+struct MHD_Response*
+create_json_response (const char *data)
+{
+  size_t len;
+  if (NULL == data)
+    len = 0;
+  else
+    len = strlen (data);
+  struct MHD_Response *resp = MHD_create_response_from_buffer (len,
+                                                               (void*)data,
+                                                               
MHD_RESPMEM_MUST_COPY);
+  MHD_add_response_header 
(resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
+  return resp;
+}
+
+/**
  * Task run on shutdown.  Cleans up everything.
  *
  * @param cls unused
@@ -200,7 +229,8 @@
           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct LookupHandle *handle = cls;
-  handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+  struct MHD_Response *resp = create_json_response (NULL);
+  handle->proc (handle->proc_cls, resp, GNUNET_SYSERR);
   cleanup_handle (handle);
 }
 
@@ -265,20 +295,22 @@
                        const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct LookupHandle *handle = cls;
+  struct MHD_Response *resp;
   uint32_t i;
   char *result;
   json_t *result_root;
-  json_t *result_name;
+  json_t *result_data;
   json_t *result_array;
   json_t *record_obj;
 
   result_root = json_object();
-  result_name = json_string (handle->name);
   result_array = json_array();
-  json_object_set (result_root, "name", result_name);
-  json_decref (result_name);
+  result_data = json_object();
+  json_object_set_new (result_root, GNUNET_REST_JSON_ATTR_ID, json_string 
(handle->name));
+  json_object_set (result_root,
+                   GNUNET_REST_JSON_ATTR_TYPE,
+                   json_string (GNUNET_GNS_JSON_RECORD_TYPE));
   handle->lookup_request = NULL;
-
   for (i=0; i<rd_count; i++)
   {
     if ( (rd[i].record_type != handle->type) &&
@@ -289,12 +321,15 @@
     json_array_append (result_array, record_obj);
     json_decref (record_obj);
   }
-  json_object_set (result_root, "query_result", result_array);
+  json_object_set (result_root, GNUNET_REST_JSON_ATTR_DATA, result_data);
+  json_decref (result_data);
+  json_object_set (result_data, "query_result", result_array);
   json_decref (result_array);
   result = json_dumps (result_root, JSON_COMPACT);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
   json_decref (result_root);
-  handle->proc (handle->proc_cls, result, strlen (result), GNUNET_OK);
+  resp = create_json_response (result);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
   GNUNET_free (result);
   cleanup_handle (handle);
 }
@@ -329,10 +364,7 @@
   }
   else
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Please specify name to lookup!\n"));
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 }
@@ -405,8 +437,7 @@
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Ego for not found, cannot perform lookup.\n"));
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   else
@@ -443,8 +474,7 @@
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Ego for `gns-master' not found, cannot perform lookup.  Did 
you run gnunet-gns-import.sh?\n"));
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
@@ -501,6 +531,7 @@
 
 /**
  * Parse json from REST request
+ * TODO  1. this leaks 2. Rework JSON API. This is confusing
  *
  * @param data REST data
  * @param data_size data size
@@ -578,8 +609,7 @@
   if (GNUNET_OK != parse_url (conndata_handle->url, handle))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
-    proc (proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 
@@ -590,8 +620,7 @@
     if (GNUNET_OK != parse_json (conndata_handle->data, 
conndata_handle->data_size, handle))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing json...\n");
-      proc (proc_cls, NULL, 0, GNUNET_SYSERR);
-      cleanup_handle (handle);
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
       return;
     }
   }
@@ -608,8 +637,7 @@
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Connecting to GNS failed\n");
-    proc (proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 
@@ -620,8 +648,7 @@
                                                     strlen(handle->pkey_str),
                                                     &(handle->pkey)))
     {
-      proc (proc_cls, NULL, 0, GNUNET_SYSERR);
-      cleanup_handle (handle);
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
       return;
     }
     lookup_with_public_key (handle);

Modified: gnunet/src/identity/plugin_rest_identity.c
===================================================================
--- gnunet/src/identity/plugin_rest_identity.c  2015-03-13 14:35:10 UTC (rev 
35385)
+++ gnunet/src/identity/plugin_rest_identity.c  2015-03-13 15:33:47 UTC (rev 
35386)
@@ -169,6 +169,28 @@
 };
 
 /**
+ * Create s JSON Response for MHD
+ *
+ * @param data the JSON to return (can be NULL)
+ * @return a MHD_Response handle
+ */
+struct MHD_Response*
+create_json_response (const char *data)
+{
+  size_t len;
+  if (NULL == data)
+    len = 0;
+  else
+    len = strlen (data);
+  struct MHD_Response *resp = MHD_create_response_from_buffer (len,
+                                                               (void*)data,
+                                                               
MHD_RESPMEM_MUST_COPY);
+  MHD_add_response_header 
(resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
+  return resp;
+}
+
+
+/**
  * Cleanup lookup handle
  * @praram handle Handle to clean up
  */
@@ -214,10 +236,19 @@
           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct RequestHandle *handle = cls;
-  handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+  struct MHD_Response *resp = create_json_response (NULL);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
   cleanup_handle (handle);
 }
 
+/**
+ * Callback for IDENTITY_get()
+ *
+ * @param cls the RequestHandle
+ * @param ego the Ego found
+ * @param ctx the context
+ * @param name the id of the ego
+ */
 void
 get_ego_for_subsys (void *cls,
                     struct GNUNET_IDENTITY_Ego *ego,
@@ -226,6 +257,7 @@
 {
   struct RequestHandle *handle = cls;
   struct EgoEntry *ego_entry;
+  struct MHD_Response *resp;
   char *result_str;
   char *keystring;
   json_t *ego_json;
@@ -261,11 +293,19 @@
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
   json_decref (ego_json);
   json_decref (root_json);
-  handle->proc (handle->proc_cls, result_str, strlen (result_str), GNUNET_OK);
+  resp = create_json_response (result_str);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
   GNUNET_free (result_str);
   cleanup_handle (handle);
 }
 
+/**
+ * Validate the namespace of the requested url
+ * TODO move this to a lib
+ *
+ * @param url the url
+ * @param ns the namespace
+ */
 int
 check_namespace (const char *url, const char *ns)
 {
@@ -284,6 +324,11 @@
 
 }
 
+/**
+ * Create a response with requested ego(s)
+ *
+ * @param handle the RequestHandle
+ */
 void
 ego_info_response (struct RequestHandle *handle)
 {
@@ -293,13 +338,15 @@
   char *subsys_val;
   struct EgoEntry *ego_entry;
   struct GNUNET_HashCode key;
+  struct MHD_Response *resp;
   json_t *ego_arr;
   json_t *ego_json;
   json_t *root_json;
 
   if (GNUNET_NO == check_namespace (handle->url, EGO_NAMESPACE))
   {
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    resp = create_json_response (NULL);
+    handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
     cleanup_handle (handle);
     GNUNET_break (0);
     return;
@@ -355,7 +402,6 @@
     else
       break;
   }
-  GNUNET_break (0);
   if (NULL == egoname)
     json_object_set (root_json, JSON_API_TYPE_DATA, ego_arr);
   else
@@ -367,7 +413,8 @@
     json_decref (ego_json);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
-  handle->proc (handle->proc_cls, result_str, strlen (result_str), 
MHD_HTTP_OK);
+  resp = create_json_response (result_str);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
   GNUNET_free (result_str);
   cleanup_handle (handle);
 
@@ -377,6 +424,7 @@
 do_finished (void *cls, const char *emsg)
 {
   struct RequestHandle *handle = cls;
+  struct MHD_Response *resp;
 
   handle->op = NULL;
   if (NULL != emsg)
@@ -384,7 +432,8 @@
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  handle->proc (handle->proc_cls, NULL, 0, GNUNET_OK);
+  resp = create_json_response (NULL);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
   cleanup_handle (handle);
 }
 
@@ -392,6 +441,7 @@
 set_finished (void *cls, const char *emsg)
 {
   struct RequestHandle *handle = cls;
+  struct MHD_Response *resp;
 
   handle->op = NULL;
   if (NULL != emsg)
@@ -399,7 +449,8 @@
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  handle->proc (handle->proc_cls, NULL, 0, MHD_HTTP_NO_CONTENT);
+  resp = create_json_response (NULL);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
   cleanup_handle (handle);
 }
 
@@ -407,6 +458,7 @@
 create_finished (void *cls, const char *emsg)
 {
   struct RequestHandle *handle = cls;
+  struct MHD_Response *resp;
 
   handle->op = NULL;
   if (NULL != emsg)
@@ -414,7 +466,8 @@
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  handle->proc (handle->proc_cls, NULL, 0, MHD_HTTP_NO_CONTENT);
+  resp = create_json_response (NULL);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
   cleanup_handle (handle);
 }
 
@@ -423,23 +476,22 @@
 {
   const char* egoname;
   char term_data[handle->data_size+1];
+  struct EgoEntry *ego_entry;
+  struct MHD_Response *resp;
+  json_t *root_json;
+  json_t *data_json;
+  json_t *type_json;
   json_t *egoname_json;
-  json_t *root_json;
   json_error_t error;
-  struct EgoEntry *ego_entry;
 
   if (strlen (API_NAMESPACE) != strlen (handle->url))
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   if (0 >= handle->data_size)
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 
@@ -449,19 +501,36 @@
 
   if ((NULL == root_json) || !json_is_object (root_json))
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  data_json = json_object_get (root_json, JSON_API_TYPE_DATA);
+  if ((NULL == data_json) || !json_is_object (data_json))
+  {
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  type_json = json_object_get (data_json, "type");
+  if (!json_is_string (type_json) ||
+      (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json))))
+  {
+    json_decref (data_json);
+    json_decref (root_json);
+    resp = create_json_response (NULL);
+    handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
     cleanup_handle (handle);
     return;
   }
-  egoname_json = json_object_get (root_json, "ego");
+  json_decref (type_json);
+  egoname_json = json_object_get (data_json, "id");
   if (!json_is_string (egoname_json))
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    json_decref (data_json);
+    json_decref (root_json);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
+
   egoname = json_string_value (egoname_json);
   for (ego_entry = handle->ego_head;
        NULL != ego_entry;
@@ -470,14 +539,17 @@
     if (0 == strcasecmp (egoname, ego_entry->identifier))
     {
       json_decref (egoname_json);
+      json_decref (data_json);
       json_decref (root_json);
-      handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+      resp = create_json_response (NULL);
+      handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
       cleanup_handle (handle);
       return;
     }
   }
   GNUNET_asprintf (&handle->name, "%s", egoname);
   json_decref (egoname_json);
+  json_decref (data_json);
   json_decref (root_json);
   handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
                                        handle->name,
@@ -492,6 +564,7 @@
   const char *subsys;
   char term_data[handle->data_size+1];
   struct EgoEntry *ego_entry;
+  struct MHD_Response *resp;
   int ego_exists = GNUNET_NO;
   json_t *root_json;
   json_t *data_json;
@@ -502,9 +575,7 @@
 
   if (strlen (API_NAMESPACE) > strlen (handle->url))
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 
@@ -521,17 +592,15 @@
   }
   if (GNUNET_NO == ego_exists)
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    resp = create_json_response (NULL);
+    handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
     cleanup_handle (handle);
     return;
   }
 
   if (0 >= handle->data_size)
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 
@@ -541,16 +610,14 @@
 
   if ((NULL == root_json) || !json_is_object (root_json))
   {
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   data_json = json_object_get (root_json, "data");
   if (!json_is_object (data_json))
   {
     json_decref (root_json);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   id_json = json_object_get (data_json, "id");
@@ -559,21 +626,18 @@
   {
     json_decref (root_json);
     json_decref (data_json);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   json_decref (id_json);
-  
+
   type_json = json_object_get (data_json, "type");
   if (!json_is_string (type_json) ||
       (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json))))
   {
     json_decref (root_json);
     json_decref (data_json);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
-    GNUNET_break (0);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   json_decref (type_json);
@@ -583,8 +647,7 @@
   {
     json_decref (root_json);
     json_decref (data_json);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   subsys = json_string_value (subsys_json);
@@ -603,13 +666,12 @@
 {
   const char *egoname;
   struct EgoEntry *ego_entry;
+  struct MHD_Response *resp;
   int ego_exists = GNUNET_NO;
 
   if (strlen (API_NAMESPACE) >= strlen (handle->url))
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
-    cleanup_handle (handle);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
 
@@ -626,8 +688,8 @@
   }
   if (GNUNET_NO == ego_exists)
   {
-    GNUNET_break(0);
-    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    resp = create_json_response (NULL);
+    handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
     cleanup_handle (handle);
     return;
   }

Modified: gnunet/src/include/gnunet_rest_plugin.h
===================================================================
--- gnunet/src/include/gnunet_rest_plugin.h     2015-03-13 14:35:10 UTC (rev 
35385)
+++ gnunet/src/include/gnunet_rest_plugin.h     2015-03-13 15:33:47 UTC (rev 
35386)
@@ -27,6 +27,7 @@
 #define GNUNET_REST_PLUGIN_H
 
 #include "gnunet_util_lib.h"
+#include "microhttpd.h"
 
 #ifdef __cplusplus
 extern "C"
@@ -40,13 +41,11 @@
  * Iterator called on obtained result for a REST result.
  *
  * @param cls closure
- * @param data REST result
- * @param data_len length of result
+ * @param resp the response
  * @param status status code (HTTP)
  */
 typedef void (*GNUNET_REST_ResultProcessor) (void *cls,
-                                             const char *data,
-                                             size_t data_len,
+                                             struct MHD_Response *resp,
                                              int status);
 
 struct RestConnectionDataHandle

Modified: gnunet/src/rest/gnunet-rest-server.c
===================================================================
--- gnunet/src/rest/gnunet-rest-server.c        2015-03-13 14:35:10 UTC (rev 
35385)
+++ gnunet/src/rest/gnunet-rest-server.c        2015-03-13 15:33:47 UTC (rev 
35386)
@@ -168,15 +168,10 @@
  */
 void
 plugin_callback (void *cls,
-                 const char *data,
-                 size_t len,
+                 struct MHD_Response *resp,
                  int status)
 {
   struct MhdConnectionHandle *handle = cls;
-  struct MHD_Response *resp = MHD_create_response_from_buffer (len,
-                                                               (void*)data,
-                                                               
MHD_RESPMEM_MUST_COPY);
-  (void) MHD_add_response_header 
(resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
   handle->status = status;
   handle->response = resp;
   run_mhd_now(); 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]