gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r35468 - gnunet/src/gns


From: gnunet
Subject: [GNUnet-SVN] r35468 - gnunet/src/gns
Date: Thu, 26 Mar 2015 19:22:20 +0100

Author: schanzen
Date: 2015-03-26 19:22:20 +0100 (Thu, 26 Mar 2015)
New Revision: 35468

Modified:
   gnunet/src/gns/Makefile.am
   gnunet/src/gns/plugin_rest_gns.c
Log:
-gns rest

Modified: gnunet/src/gns/Makefile.am
===================================================================
--- gnunet/src/gns/Makefile.am  2015-03-26 17:11:08 UTC (rev 35467)
+++ gnunet/src/gns/Makefile.am  2015-03-26 18:22:20 UTC (rev 35468)
@@ -101,8 +101,9 @@
 libgnunet_plugin_rest_gns_la_LIBADD = \
   $(top_builddir)/src/gns/libgnunetgns.la \
   $(top_builddir)/src/identity/libgnunetidentity.la \
+       $(top_builddir)/src/rest/libgnunetrest.la \
   $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \
-  $(LTLIBINTL) -ljansson -lmicrohttpd
+  $(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-26 17:11:08 UTC (rev 35467)
+++ gnunet/src/gns/plugin_rest_gns.c    2015-03-26 18:22:20 UTC (rev 35468)
@@ -31,18 +31,22 @@
 #include <gnunet_gnsrecord_lib.h>
 #include <gnunet_namestore_service.h>
 #include <gnunet_gns_service.h>
+#include <gnunet_rest_lib.h>
 #include <jansson.h>
 
 #define API_NAMESPACE "/gns"
 
-#define GNUNET_REST_JSON_ATTR_ID "id"
 
-#define GNUNET_REST_JSON_ATTR_TYPE "type"
+#define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "type"
 
-#define GNUNET_GNS_JSON_RECORD_TYPE "rtype"
+#define GNUNET_REST_JSONAPI_GNS_RECORD "records"
 
-#define GNUNET_REST_JSON_ATTR_DATA "data"
+#define GNUNET_REST_JSONAPI_GNS_EGO "ego"
 
+#define GNUNET_REST_JSONAPI_GNS_PKEY "pkey"
+
+#define GNUNET_REST_JSONAPI_GNS_OPTIONS "options"
+
 /**
  * @brief struct returned by the initialization function of the plugin
  */
@@ -118,12 +122,12 @@
 
   /**
    * The Pkey to use
-   * In stirng representation from JSON
+   * In string representation from JSON
    */
   const char *pkey_str;
 
   /**
-   * The record type to look up
+   * The record type
    */
   int type;
 
@@ -200,28 +204,6 @@
 
 
 /**
- * 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
@@ -232,7 +214,7 @@
           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct LookupHandle *handle = cls;
-  struct MHD_Response *resp = create_json_response (NULL);
+  struct MHD_Response *resp = GNUNET_REST_create_json_response (NULL);
   handle->proc (handle->proc_cls, resp, GNUNET_SYSERR);
   cleanup_handle (handle);
 }
@@ -300,39 +282,35 @@
 {
   struct LookupHandle *handle = cls;
   struct MHD_Response *resp;
+  struct JsonApiObject *json_object;
+  struct JsonApiResource *json_resource;
   uint32_t i;
   char *result;
-  json_t *result_root;
-  json_t *result_data;
   json_t *result_array;
   json_t *record_obj;
 
-  result_root = json_object();
   result_array = json_array();
-  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));
+  json_object = GNUNET_REST_jsonapi_object_new ();
+  json_resource = GNUNET_REST_jsonapi_resource_new 
(GNUNET_REST_JSONAPI_GNS_RECORD, handle->name);
   handle->lookup_request = NULL;
   for (i=0; i<rd_count; i++)
   {
     if ( (rd[i].record_type != handle->type) &&
          (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
       continue;
-
     record_obj = gnsrecord_to_json (&(rd[i]));
     json_array_append (result_array, record_obj);
     json_decref (record_obj);
   }
-  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);
+  GNUNET_REST_jsonapi_resource_add_attr (json_resource,
+                                         GNUNET_REST_JSONAPI_GNS_RECORD,
+                                         result_array);
+  GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
+  GNUNET_REST_jsonapi_data_serialize (json_object, &result);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
   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);
-  resp = create_json_response (result);
+  GNUNET_REST_jsonapi_object_delete (json_object);
+  resp = GNUNET_REST_create_json_response (result);
   handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
   GNUNET_free (result);
   cleanup_handle (handle);
@@ -505,7 +483,6 @@
 parse_url (const char *url, struct LookupHandle *handle)
 {
   char *name;
-  char *type;
   char tmp_url[strlen(url)+1];
   char *tok;
 
@@ -521,76 +498,10 @@
                    name);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Got name: %s\n", handle->name);
-  type = strtok (NULL, "/");
-  if (NULL == type)
-  {
-    handle->type = GNUNET_GNSRECORD_TYPE_ANY;
-    return GNUNET_OK;
-  }
-  handle->type = GNUNET_GNSRECORD_typename_to_number (type);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got type: %s\n", type);
   return GNUNET_OK;
 }
 
 /**
- * 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
- * @param handle Handle to populate
- * @return GNUNET_SYSERR on error
- */
-int
-parse_json (const char *data, size_t data_size, struct LookupHandle *handle)
-{
-  json_error_t error;
-  json_t *pkey_json;
-  json_t *ego_json;
-  json_t *options_json;
-
-  char term_data[data_size+1];
-  term_data[data_size] = '\0';
-
-  memcpy (term_data, data, data_size);
-
-  handle->json_root = json_loads (term_data, 0, &error);
-
-  if (NULL == handle->json_root)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, error.text);
-    return GNUNET_SYSERR;
-  }
-
-  if(!json_is_object(handle->json_root))
-  {
-    return GNUNET_SYSERR;
-  }
-
-  ego_json = json_object_get (handle->json_root, "ego");
-
-  if(json_is_string(ego_json))
-  {
-    handle->ego_str = json_string_value (ego_json);
-  }
-
-  pkey_json = json_object_get (handle->json_root, "pkey");
-  if(json_is_string(pkey_json))
-  {
-    handle->pkey_str = json_string_value (pkey_json);
-  }
-
-  options_json = json_object_get (handle->json_root, "options");
-  if(json_is_integer (options_json))
-  {
-    handle->options = json_integer_value (options_json);
-  }
-  return GNUNET_OK;
-}
-
-
-/**
  * Function processing the REST call
  *
  * @param method HTTP method
@@ -607,8 +518,9 @@
                          void *proc_cls)
 {
   struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
+  struct GNUNET_HashCode key;
+
   handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
-
   //parse name and type from url
   if (GNUNET_OK != parse_url (conndata_handle->url, handle))
   {
@@ -616,18 +528,8 @@
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-
   handle->proc_cls = proc_cls;
   handle->proc = proc;
-  if (0 < conndata_handle->data_size)
-  {
-    if (GNUNET_OK != parse_json (conndata_handle->data, 
conndata_handle->data_size, handle))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing json...\n");
-      GNUNET_SCHEDULER_add_now (&do_error, handle);
-      return;
-    }
-  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Connecting...\n");
   handle->gns = GNUNET_GNS_connect (cfg);
@@ -636,7 +538,6 @@
                                                        &do_error, handle);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Connected\n");
-
   if (NULL == handle->gns)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -644,9 +545,40 @@
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
+  GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS,
+                      strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS),
+                      &key);
+  handle->options = GNUNET_GNS_LO_DEFAULT;
+  if ( GNUNET_YES ==
+       GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
+                                               &key) )
+  {
+    handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) 
GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
+                                                         //&key);
+  }
+  GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE,
+                      strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE),
+                      &key);
+  if ( GNUNET_YES ==
+       GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
+                                               &key) )
+  {
+    handle->type = GNUNET_GNSRECORD_typename_to_number 
+      (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
+      &key));
+  }
+  else
+    handle->type = GNUNET_GNSRECORD_TYPE_ANY;
 
-  if (NULL != handle->pkey_str)
+  GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY,
+                      strlen (GNUNET_REST_JSONAPI_GNS_PKEY),
+                      &key);
+  if ( GNUNET_YES ==
+       GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
+                                               &key) )
   {
+    handle->pkey_str = GNUNET_CONTAINER_multihashmap_get 
(conndata_handle->url_param_map,
+                                                          &key);
     if (GNUNET_OK !=
         GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
                                                     strlen(handle->pkey_str),
@@ -656,10 +588,17 @@
       return;
     }
     lookup_with_public_key (handle);
-
+    return;
   }
-  if (NULL != handle->ego_str)
+  GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
+                      strlen (GNUNET_REST_JSONAPI_GNS_EGO),
+                      &key);
+  if ( GNUNET_YES ==
+       GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
+                                               &key) )
   {
+    handle->ego_str = GNUNET_CONTAINER_multihashmap_get 
(conndata_handle->url_param_map,
+                                                         &key);
     handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
                                              handle->ego_str,
                                              &identity_zone_cb,
@@ -671,7 +610,6 @@
        (0 == strcmp (".zkey",
                      &handle->name[strlen (handle->name) - 4])) )
   {
-    /* no zone required, use 'anonymous' zone */
     GNUNET_CRYPTO_ecdsa_key_get_public
       (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
        &(handle->pkey));




reply via email to

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