gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r35381 - in gnunet/src: gns identity include rest
Date: Fri, 13 Mar 2015 10:59:43 +0100

Author: schanzen
Date: 2015-03-13 10:59:43 +0100 (Fri, 13 Mar 2015)
New Revision: 35381

Modified:
   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:
-bugfixes, change plugin call

Modified: gnunet/src/gns/plugin_rest_gns.c
===================================================================
--- gnunet/src/gns/plugin_rest_gns.c    2015-03-13 08:11:09 UTC (rev 35380)
+++ gnunet/src/gns/plugin_rest_gns.c    2015-03-13 09:59:43 UTC (rev 35381)
@@ -567,10 +567,7 @@
  * @return GNUNET_OK if request accepted
  */
 void
-rest_gns_process_request(const char *method,
-                         const char *url,
-                         const char *data,
-                         size_t data_size,
+rest_gns_process_request(struct RestConnectionDataHandle *conndata_handle,
                          GNUNET_REST_ResultProcessor proc,
                          void *proc_cls)
 {
@@ -578,7 +575,7 @@
   handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
 
   //parse name and type from url
-  if (GNUNET_OK != parse_url (url, handle))
+  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);
@@ -588,9 +585,9 @@
 
   handle->proc_cls = proc_cls;
   handle->proc = proc;
-  if (0 < data_size)
+  if (0 < conndata_handle->data_size)
   {
-    if (GNUNET_OK != parse_json (data, data_size, handle))
+    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);

Modified: gnunet/src/identity/plugin_rest_identity.c
===================================================================
--- gnunet/src/identity/plugin_rest_identity.c  2015-03-13 08:11:09 UTC (rev 
35380)
+++ gnunet/src/identity/plugin_rest_identity.c  2015-03-13 09:59:43 UTC (rev 
35381)
@@ -69,6 +69,11 @@
    * Ego Pkey
    */
   struct GNUNET_CRYPTO_EcdsaPublicKey pk;
+
+  /**
+   * The Ego
+   */
+  struct GNUNET_IDENTITY_Ego *ego;
 };
 
 struct RequestHandle
@@ -129,14 +134,9 @@
   char *name;
 
   /**
-   * The ego set from REST
-   */
-  char *set_ego;
-  
-  /**
    * The subsystem set from REST
    */
-  char *set_subsystem;
+  char *subsys;
 
   /**
    * The url
@@ -179,10 +179,8 @@
     GNUNET_SCHEDULER_cancel (handle->timeout_task);
   if (NULL != handle->identity_handle)
     GNUNET_IDENTITY_disconnect (handle->identity_handle);
-  if (NULL != handle->set_subsystem)
-    GNUNET_free (handle->set_subsystem);
-  if (NULL != handle->set_ego)
-    GNUNET_free (handle->set_ego);
+  if (NULL != handle->subsys)
+    GNUNET_free (handle->subsys);
   for (ego_entry = handle->ego_head;
        NULL != ego_entry;)
   {
@@ -263,7 +261,7 @@
 }
 
 static void
-delete_finished (void *cls, const char *emsg)
+do_finished (void *cls, const char *emsg)
 {
   struct RequestHandle *handle = cls;
 
@@ -277,24 +275,10 @@
 }
 
 static void
-create_finished (void *cls, const char *emsg)
-{
-  struct RequestHandle *handle = cls;
-
-  handle->op = NULL;
-  if (NULL != emsg)
-  {
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
-  }
-  handle->proc (handle->proc_cls, NULL, 0, GNUNET_OK);
-  cleanup_handle (handle);
-}
-
-static void
 ego_create_cont (struct RequestHandle *handle)
 {
   const char* egoname;
-  char term_data[handle->data_size];
+  char term_data[handle->data_size+1];
   json_t *egoname_json;
   json_t *root_json;
   json_error_t error;
@@ -353,13 +337,85 @@
   json_decref (root_json);
   handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
                                               handle->name,
-                                              &create_finished,
+                                              &do_finished,
                                               handle);
 }
 
 void 
 subsys_set_cont (struct RequestHandle *handle)
 {
+  const char *egoname;
+  const char *subsys;
+  char term_data[handle->data_size+1];
+  struct EgoEntry *ego_entry;
+  int ego_exists = GNUNET_NO;
+  json_t *root_json;
+  json_t *subsys_json;
+  json_error_t error;
+
+  if (strlen (API_NAMESPACE)+1 >= strlen (handle->url))
+  {
+    GNUNET_break(0);
+    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    cleanup_handle (handle);
+    return;
+  }
+
+  egoname = &handle->url[strlen(API_NAMESPACE)+1];
+  for (ego_entry = handle->ego_head;
+       NULL != ego_entry;
+       ego_entry = ego_entry->next)
+  {
+    if (0 == strcasecmp (egoname, ego_entry->identifier))
+    {
+      ego_exists = GNUNET_YES;
+      break;
+    }
+  }
+  if (GNUNET_NO == ego_exists)
+  {
+    GNUNET_break(0);
+    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    cleanup_handle (handle);
+    return;
+  }
+
+  if (0 >= handle->data_size)
+  {
+    GNUNET_break(0);
+    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    cleanup_handle (handle);
+    return;
+  }
+
+  term_data[handle->data_size] = '\0';
+  memcpy (term_data, handle->data, handle->data_size);
+  root_json = json_loads (term_data, 0, &error);
+
+  if ((NULL == root_json) || !json_is_object (root_json))
+  {
+    GNUNET_break(0);
+    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    cleanup_handle (handle);
+    return;
+  }
+  subsys_json = json_object_get (root_json, "subsystem");
+  if (!json_is_string (subsys_json))
+  {
+    GNUNET_break(0);
+    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    cleanup_handle (handle);
+    return;
+  }
+  subsys = json_string_value (subsys_json);
+  GNUNET_asprintf (&handle->subsys, "%s", subsys);
+  json_decref (subsys_json);
+  json_decref (root_json);
+  handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
+                                    handle->subsys,
+                                    ego_entry->ego,
+                                    &do_finished,
+                                    handle);
 }
 
 void 
@@ -397,7 +453,7 @@
   }
   handle->op = GNUNET_IDENTITY_delete (handle->identity_handle,
                                        egoname,
-                                       &delete_finished,
+                                       &do_finished,
                                        handle);
 
 }
@@ -466,41 +522,13 @@
     return;
   }
   if (ID_REST_STATE_INIT == handle->state) {
-        ego_entry = GNUNET_new (struct EgoEntry);
+    ego_entry = GNUNET_new (struct EgoEntry);
     GNUNET_IDENTITY_ego_get_public_key (ego, &(ego_entry->pk));
+    ego_entry->ego = ego;
     GNUNET_asprintf (&ego_entry->identifier, "%s", identifier);
     GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, 
ego_entry);
   }
 
-  if ( (NULL == handle->set_ego) &&
-       (NULL != ego) &&
-       (NULL != identifier) &&
-       (0 == strcmp (identifier,
-                     handle->set_ego)) )
-  {
-    /*handle->set_op = GNUNET_IDENTITY_set (sh,
-                                          handle->set_subsystem,
-                                          ego,
-                                          &set_done,
-                                          handle);
-    GNUNET_free (handle->set_subsystem);
-    handle->set_subsystem = NULL;
-    GNUNET_free (handle->set_ego); //decref?
-    handle->set_ego = NULL;TODO*/
-  }
-  if ( (NULL == ego) &&
-       (NULL != handle->set_ego) )
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Could not set ego to `%s' for subsystem `%s', ego not 
known\n",
-                handle->set_ego,
-                handle->set_subsystem);
-    GNUNET_free (handle->set_subsystem);
-    handle->set_subsystem = NULL;
-    GNUNET_free (handle->set_ego); //decref?
-    handle->set_ego = NULL;
-  }
-  
 }
 
 /**
@@ -515,12 +543,9 @@
  * @return GNUNET_OK if request accepted
  */
 void
-rest_identity_process_request(const char *method,
-                               const char *url,
-                               const char *data,
-                               size_t data_size,
-                               GNUNET_REST_ResultProcessor proc,
-                               void *proc_cls)
+rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle,
+                              GNUNET_REST_ResultProcessor proc,
+                              void *proc_cls)
 {
   struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
 
@@ -533,15 +558,19 @@
   handle->state = ID_REST_STATE_INIT;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Connecting...\n");
-  handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle); 
-  handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
-                                                       &do_error, handle);
-  handle->data = data;
-  handle->data_size = data_size;
-  handle->url = url;
+  handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
+                                                     &list_ego,
+                                                     handle); 
+  handle->timeout_task =
+    GNUNET_SCHEDULER_add_delayed (handle->timeout,
+                                  &do_error,
+                                  handle);
+  handle->data = conndata_handle->data;
+  handle->data_size = conndata_handle->data_size;
+  handle->url = conndata_handle->url;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Connected\n");
-  handle->method = method;
+  handle->method = conndata_handle->method;
 }
 
 /**

Modified: gnunet/src/include/gnunet_rest_plugin.h
===================================================================
--- gnunet/src/include/gnunet_rest_plugin.h     2015-03-13 08:11:09 UTC (rev 
35380)
+++ gnunet/src/include/gnunet_rest_plugin.h     2015-03-13 09:59:43 UTC (rev 
35381)
@@ -1,22 +1,22 @@
 /*
-     This file is part of GNUnet.
-     Copyright (C) 2012-2015 Christian Grothoff (and other contributing 
authors)
+   This file is part of GNUnet.
+   Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+   GNUnet is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation; either version 3, or (at your
+   option) any later version.
 
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+   GNUnet is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
 
-     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.
-*/
+   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.
+   */
 /**
  * @author Martin Schanzenbach
  * @file include/gnunet_rest_plugin.h
@@ -45,10 +45,20 @@
  * @param status status code (HTTP)
  */
 typedef void (*GNUNET_REST_ResultProcessor) (void *cls,
-                          const char *data,
-                          size_t data_len,
-                          int status);
+                                             const char *data,
+                                             size_t data_len,
+                                             int status);
 
+struct RestConnectionDataHandle
+{
+  struct GNUNET_CONTAINER_MultiHashMap *url_param_map;
+  const char *method;
+  const char *url;
+  const char *data;
+  size_t data_size;
+
+};
+
 /**
  * @brief struct returned by the initialization function of the plugin
  */
@@ -61,7 +71,7 @@
    *
    */
   void *cls;
-  
+
   /**
    * Plugin name. Used as the namespace for the API.
    * e.g. http://hostname:port/<name>
@@ -78,12 +88,9 @@
    * @param proc the callback for result
    * @param proc_cls closure for callback
    */
-  void (*process_request) (const char *method,
-                          const char *url,
-                          const char *data,
-                          size_t data_size,
-                          GNUNET_REST_ResultProcessor proc,
-                          void *proc_cls);
+  void (*process_request) (struct RestConnectionDataHandle *handle,
+                           GNUNET_REST_ResultProcessor proc,
+                           void *proc_cls);
 
 };
 

Modified: gnunet/src/rest/gnunet-rest-server.c
===================================================================
--- gnunet/src/rest/gnunet-rest-server.c        2015-03-13 08:11:09 UTC (rev 
35380)
+++ gnunet/src/rest/gnunet-rest-server.c        2015-03-13 09:59:43 UTC (rev 
35381)
@@ -56,8 +56,7 @@
 #define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply 
(GNUNET_TIME_UNIT_MINUTES, 5)
 
 #define GN_REST_STATE_INIT 0
-#define GN_REST_STATE_UPLOAD 1
-#define GN_REST_STATE_RECV 2
+#define GN_REST_STATE_PROCESSING 1
 
 /**
  * The task ID
@@ -125,6 +124,8 @@
 
   struct GNUNET_REST_Plugin *plugin;
 
+  struct RestConnectionDataHandle *data_handle;
+
   int status;
 
   int state;
@@ -181,6 +182,64 @@
   run_mhd_now(); 
 }
 
+int
+cleanup_url_map (void *cls,
+                 const struct GNUNET_HashCode *key,
+                 void *value)
+{
+  GNUNET_free_non_null (value);
+  return GNUNET_YES;
+}
+
+void
+cleanup_handle (struct MhdConnectionHandle *handle)
+{
+  if (NULL != handle->response)
+    MHD_destroy_response (handle->response);
+  if (NULL != handle->data_handle)
+  {
+    if (NULL != handle->data_handle->url_param_map)
+    {
+      GNUNET_CONTAINER_multihashmap_iterate 
(handle->data_handle->url_param_map,
+                                             &cleanup_url_map,
+                                             NULL);
+      GNUNET_CONTAINER_multihashmap_destroy 
(handle->data_handle->url_param_map);
+    }
+    GNUNET_free (handle->data_handle);
+  }
+  GNUNET_free (handle);
+
+}
+
+int
+url_iterator (void *cls,
+              enum MHD_ValueKind kind,
+              const char *key,
+              const char *value)
+{
+  struct RestConnectionDataHandle *handle = cls;
+  struct GNUNET_HashCode hkey;
+  char *val;
+  if (NULL == handle->url_param_map)
+  {
+    handle->url_param_map = GNUNET_CONTAINER_multihashmap_create (16,
+                                                                  GNUNET_NO);
+  }
+  GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
+  GNUNET_asprintf (&val, "%s", value);
+  if (GNUNET_OK !=
+      GNUNET_CONTAINER_multihashmap_put (handle->url_param_map,
+                                         &hkey,
+                                         val,
+                                         
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not load add url param `%s'=%s\n",
+                key, value);
+  }
+  return MHD_YES;
+}
+
 /* ********************************* MHD response generation 
******************* */
 
 /**
@@ -219,6 +278,7 @@
   char *plugin_name;
   struct GNUNET_HashCode key;
   struct MhdConnectionHandle *con_handle;
+  struct RestConnectionDataHandle *rest_conndata_handle;
 
   con_handle = *con_cls;
 
@@ -258,29 +318,22 @@
   }
   if (GN_REST_STATE_INIT == con_handle->state)
   {
-    if (0 != *upload_data_size)
-    {
-      con_handle->state = GN_REST_STATE_UPLOAD;
+    rest_conndata_handle = GNUNET_new (struct RestConnectionDataHandle);
+    rest_conndata_handle->method = meth;
+    rest_conndata_handle->url = url;
+    rest_conndata_handle->data = upload_data;
+    rest_conndata_handle->data_size = *upload_data_size;
+    con_handle->data_handle = rest_conndata_handle;
+    MHD_get_connection_values (con,
+                               MHD_GET_ARGUMENT_KIND,
+                               &url_iterator,
+                               rest_conndata_handle);
+    con_handle->state = GN_REST_STATE_PROCESSING;
+    con_handle->plugin->process_request (rest_conndata_handle,
+                                         &plugin_callback,
+                                         con_handle);
+    *upload_data_size = 0;
 
-      con_handle->plugin->process_request (meth,
-                                           url,
-                                           upload_data,
-                                           *upload_data_size,
-                                           &plugin_callback,
-                                           con_handle);
-      *upload_data_size = 0;
-
-    }
-    else 
-    {
-      con_handle->state = GN_REST_STATE_RECV;
-      con_handle->plugin->process_request (meth,
-                                           url,
-                                           NULL,
-                                           0,
-                                           &plugin_callback,
-                                           con_handle);
-    }
   }
   if (NULL != con_handle->response)
   {
@@ -296,6 +349,7 @@
                                  MHD_HTTP_BAD_REQUEST,
                                  con_handle->response);
     }
+    cleanup_handle (con_handle);
   }
   return MHD_YES;
 }




reply via email to

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