gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] branch master updated (5a76696 -> 0a57d9f)


From: gnunet
Subject: [taler-anastasis] branch master updated (5a76696 -> 0a57d9f)
Date: Wed, 20 Nov 2019 08:37:03 +0100

This is an automated email from the git hooks/post-receive script.

dennis-neufeld pushed a change to branch master
in repository anastasis.

    from 5a76696  Initial work on client code
     new f004fc9  Worked on policy post api
     new 0a57d9f  Worked on policy API

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/include/Makefile.am         |   2 +-
 src/include/anastasis.h         |  28 -----
 src/include/anastasis_service.h |  40 +++++-
 src/lib/Makefile.am             |   6 +-
 src/lib/anastasis_api_policy.c  | 264 +++++++++++++++++++++++++++++++++++++++-
 5 files changed, 300 insertions(+), 40 deletions(-)
 delete mode 100644 src/include/anastasis.h

diff --git a/src/include/Makefile.am b/src/include/Makefile.am
index 4be342e..5f8d0a7 100644
--- a/src/include/Makefile.am
+++ b/src/include/Makefile.am
@@ -8,4 +8,4 @@ anastasisinclude_HEADERS = \
   anastasis_database_plugin.h \
   anastasis_service.h \
   anastasis_error_codes.h \
-  anastasis_database_lib.h
+  anastasis_database_lib.h 
\ No newline at end of file
diff --git a/src/include/anastasis.h b/src/include/anastasis.h
deleted file mode 100644
index 8f935f4..0000000
--- a/src/include/anastasis.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
-  This file is part of Anastasis
-  Copyright (C) 2019 Taler Systems SA
-
-  Anastasis is free software; you can redistribute it and/or modify it under 
the
-  terms of the GNU Lesser General Public License as published by the Free 
Software
-  Foundation; either version 3, or (at your option) any later version.
-
-  Anastasis 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
-  Anastasis; see the file COPYING.GPL.  If not, see 
<http://www.gnu.org/licenses/>
-*/
-/**
- * @file include/anastasis.h
- * @brief low level functions for Anastasis
- * @author Christian Grothoff
- * @author Dennis Neufeld
- * @author Dominik Meister
- */
-#ifndef ANASTASIS_H
-#define ANASTASIS_H
-
-#include "anastasis_service.h"
-
-// FIXME
\ No newline at end of file
diff --git a/src/include/anastasis_service.h b/src/include/anastasis_service.h
index 83cabad..88f90bd 100644
--- a/src/include/anastasis_service.h
+++ b/src/include/anastasis_service.h
@@ -126,7 +126,8 @@ ANASTASIS_policy_lookup (struct GNUNET_CURL_Context *ctx,
 struct ANASTASIS_PolicyLookupOperation *
 ANASTASIS_policy_lookup_version (struct GNUNET_CURL_Context *ctx,
                                  const char *backend_url,
-                                 const struct ANASTASIS_AccountPubP 
*anastasis_pub,
+                                 const struct
+                                 ANASTASIS_AccountPubP *anastasis_pub,
                                  ANASTASIS_PolicyLookupCallback cb,
                                  void *cb_cls,
                                  uint32_t *version);
@@ -134,7 +135,7 @@ ANASTASIS_policy_lookup_version (struct GNUNET_CURL_Context 
*ctx,
 /**
  * Cancel a GET /policy request.
  *
- * @param rlo cancel the policy lookup operation
+ * @param plo cancel the policy lookup operation
  */
 void
 ANASTASIS_policy_lookup_cancel (struct
@@ -162,4 +163,37 @@ typedef void
                                   const json_t *obj);
 
 
-#endif  /* _ANASTASIS_MERCHANT_SERVICE_H */
+/**
+ * Store policies
+ *
+ * @param ctx the CURL context used to connect to the backend
+ * @param backend_url backend's base URL, including final "/"
+ * @param anastasis_pub public key of the user's account
+ * @param refund amount to which increase the refund
+ * @param cb callback processing the response from /policy
+ * @param cb_cls closure for cb
+ */
+struct ANASTASIS_PolicyStoreOperation *
+ANASTASIS_policy_store (struct GNUNET_CURL_Context *ctx,
+                        const char *backend_url,
+                        const struct
+                        ANASTASIS_AccountPubP *anastasis_pub,
+                        const void *policy_data,
+                        size_t policy_data_size,
+                        const struct 
+                        ANASTASIS_PaymentSecretP *paymentSecretP,
+                        ANASTASIS_PolicyStoreCallback cb,
+                        void *cb_cls);
+
+
+/**
+ * Cancel a POST /policy request.
+ *
+ * @param pso the policy store operation to cancel
+ */
+void
+ANASTASIS_policy_store_cancel (struct
+                               ANASTASIS_PolicyStoreOperation *pso);
+
+
+#endif  /* _ANASTASIS_SERVICE_H */
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 1774696..3e67e50 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -1,5 +1,5 @@
 # This Makefile.am is in the public domain
-AM_CPPFLAGS = -I$(top_srcdir)/src/include -I$(top_srcdir)/src/backend
+AM_CPPFLAGS = -I$(top_srcdir)/src/include -I$(top_srcdir)/src/backend 
-I$(top_srcdir)/src/lib
 
 if USE_COVERAGE
   AM_CFLAGS = --coverage -O0
@@ -7,7 +7,7 @@ if USE_COVERAGE
 endif
 
 lib_LTLIBRARIES = \
-  libanastasis.la
+  libanastasis.la 
 
 libanastasis_la_LDFLAGS = \
   -version-info 0:0:0 \
@@ -33,3 +33,5 @@ if HAVE_LIBGNURL
 libanastasis_la_LIBADD += -lgnurl
 endif
 endif
+  
+
diff --git a/src/lib/anastasis_api_policy.c b/src/lib/anastasis_api_policy.c
index 7b8359d..c397957 100644
--- a/src/lib/anastasis_api_policy.c
+++ b/src/lib/anastasis_api_policy.c
@@ -31,9 +31,14 @@
 #include <gnunet/gnunet_util_lib.h>
 #include <gnunet/gnunet_curl_lib.h>
 #include <taler/taler_json_lib.h>
+#include <taler/taler_curl_lib.h>
 #include <taler/taler_util.h>
 #include "anastasis_service.h"
 
+#if COMPRESS_BODIES
+#include <zlib.h>
+#endif
+
 
 /**
  * @brief A Contract Operation Handle
@@ -68,6 +73,41 @@ struct ANASTASIS_PolicyLookupOperation
 };
 
 
+struct ANASTASIS_PolicyStoreOperation
+{
+  /**
+   * Complete URL where the backend offers /policy
+   */
+  char *url;
+
+  /**
+   * Minor context that holds body and headers.
+   */
+  struct TEAH_PostContext post_ctx;
+
+  /**
+   * The CURL context to connect to the backend
+   */
+  struct GNUNET_CURL_Context *ctx;
+
+  /**
+   * The callback to pass the backend response to
+   */
+  ANASTASIS_PolicyStoreCallback cb;
+
+  /**
+   * Clasure to pass to the callback
+   */
+  void *cb_cls;
+
+  /**
+   * Handle for the request
+   */
+  struct GNUNET_CURL_Job *job;
+};
+
+
+
 /**
  * Cancel a pending /policy GET request
  *
@@ -272,7 +312,6 @@ ANASTASIS_policy_lookup (struct GNUNET_CURL_Context *ctx,
   plo->cb = cb;
   plo->cb_cls = cb_cls;
 
-  // plo->url = TALER_url_join (backend_url, "policy", "order_id", order_id, 
NULL);
   plo->url = TALER_url_join (backend_url,
                              "policy",
                              anastasis_pub);
@@ -314,11 +353,12 @@ ANASTASIS_policy_lookup (struct GNUNET_CURL_Context *ctx,
  */
 struct ANASTASIS_PolicyLookupOperation *
 ANASTASIS_policy_lookup_version (struct GNUNET_CURL_Context *ctx,
-                         const char *backend_url,
-                         const struct ANASTASIS_AccountPubP *anastasis_pub,
-                         ANASTASIS_PolicyLookupCallback cb,
-                         void *cb_cls,
-                         uint32_t *version)
+                                 const char *backend_url,
+                                 const struct
+                                 ANASTASIS_AccountPubP *anastasis_pub,
+                                 ANASTASIS_PolicyLookupCallback cb,
+                                 void *cb_cls,
+                                 uint32_t *version)
 {
   struct ANASTASIS_PolicyLookupOperation *plo;
   CURL *eh;
@@ -357,3 +397,215 @@ ANASTASIS_policy_lookup_version (struct 
GNUNET_CURL_Context *ctx,
 
   return plo;
 }
+
+
+/**
+ * Callback to process POST /policy response
+ *
+ * @param cls the `struct ANASTASIS_PolicyStoreOperation`
+ * @param response_code HTTP response code, 0 on error
+ * @param json response body, NULL if not JSON
+ */
+static void
+handle_policy_store_finished (void *cls,
+                              long response_code,
+                              const void *response)
+{
+  struct ANASTASIS_PolicyStoreOperation *pso = cls;
+  char *error;
+  char *hint;
+  enum ANASTASIS_ErrorCode code;
+  const json_t *json = response;
+
+  pso->job = NULL;
+  switch (response_code)
+  {
+  case 0:
+    /* Hard error */
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Backend didn't even return from POST /policy\n");
+    return;
+
+  /* Good to hand everything to the callback, as all
+   * the logic is actually there.  */
+  case MHD_HTTP_OK:
+  case MHD_HTTP_BAD_REQUEST:
+  case MHD_HTTP_NOT_FOUND:
+    pso->cb (pso->cb_cls,
+             response_code,
+             ANASTASIS_EC_NONE,
+             json);
+    break;
+  default:
+    /**
+     * The backend gave response, but it's error, log it.
+     * NOTE that json must be a Anastasis-specific error object (FIXME,
+     * need a link to error objects at docs)
+     */
+    if (-1 == json_unpack
+          ((json_t *) json,
+          "{s:s, s:I, s:s}",
+          "error", &error,
+          "code", &code,
+          "hint", &hint))
+
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "/policy failed (HTTP code: %lu), backend did "
+                  "not give a valid error object\n", response_code);
+      break;
+    }
+
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "/policy, error: %s, code: %d, hint: %s\n",
+                error,
+                code,
+                hint);
+  }
+  ANASTASIS_policy_store_cancel (pso);
+}
+
+
+/**
+ * Cancel a POST /policy request.
+ *
+ * @param pso the policy store operation to cancel
+ */
+void
+ANASTASIS_policy_store_cancel (struct
+                               ANASTASIS_PolicyStoreOperation *pso)
+{
+  if (NULL != pso->job)
+  {
+    GNUNET_CURL_job_cancel (pso->job);
+    pso->job = NULL;
+  }
+  TALER_curl_easy_post_finished (&pso->post_ctx);
+  GNUNET_free (pso->url);
+  GNUNET_free (pso);
+}
+
+
+/**
+ * Add the @a body as POST data to the easy handle in @a ctx.
+ *
+ * @param ctx[in,out] a request context (updated)
+ * @param eh easy handle to use
+ * @param body bytestream to add to @e ctx
+ * @return #GNUNET_OK on success #GNUNET_SYSERR on failure
+ */
+int
+ANASTASIS_curl_easy_post (struct TEAH_PostContext *ctx,
+                      CURL *eh,
+                      const void *body,
+                      size_t body_size)
+{
+    size_t slen;
+
+    if (NULL == body)
+    {
+        GNUNET_break (0);
+        return GNUNET_SYSERR;
+    }
+#if COMPRESS_BODIES
+    {
+        Bytef *cbuf;
+        uLongf cbuf_size;
+        int ret;
+
+        cbuf_size = compressBound (body_size);
+        cbuf = GNUNET_malloc (cbuf_size);
+        ret = compress (cbuf,
+                        &cbuf_size,
+                        (const Bytef *) body,
+                        body_size);
+        if (Z_OK != ret)
+        {
+        /* compression failed!? */
+        GNUNET_break (0);
+        GNUNET_free (cbuf);
+        return GNUNET_SYSERR;
+        }
+        slen = (size_t) cbuf_size;
+        ctx->json_enc = (char *) cbuf;
+    }
+    GNUNET_assert
+        (NULL != (ctx->headers = curl_slist_append
+                                (ctx->headers,
+                                "Content-Encoding: deflate")));
+#endif
+
+    GNUNET_assert
+        (NULL != (ctx->headers = curl_slist_append
+                                (ctx->headers,
+                                "Content-Type: anastasis/encrypted-policy")));
+
+    GNUNET_assert (CURLE_OK ==
+                    curl_easy_setopt (eh,
+                                    CURLOPT_POSTFIELDS,
+                                    body));
+    GNUNET_assert (CURLE_OK ==
+                    curl_easy_setopt (eh,
+                                    CURLOPT_POSTFIELDSIZE,
+                                    slen));
+    return GNUNET_OK;
+}
+
+
+/**
+ * Store policies, does a POST /policy/$AccountPub
+ *
+ * @param ctx the CURL context used to connect to the backend
+ * @param backend_url backend's base URL, including final "/"
+ * @param anastasis_pub public key of the user's account
+ * @param refund amount to which increase the refund
+ * @param cb callback processing the response from /policy
+ * @param cb_cls closure for cb
+ */
+struct ANASTASIS_PolicyStoreOperation *
+ANASTASIS_policy_store (struct GNUNET_CURL_Context *ctx,
+                        const char *backend_url,
+                        const struct
+                        ANASTASIS_AccountPubP *anastasis_pub,
+                        const void *policy_data,
+                        size_t policy_data_size,
+                        const struct 
+                        ANASTASIS_PaymentSecretP *paymentSecretP,
+                        ANASTASIS_PolicyStoreCallback cb,
+                        void *cb_cls)
+{
+  struct ANASTASIS_PolicyStoreOperation *pso;
+  CURL *eh;
+
+  pso = GNUNET_new (struct ANASTASIS_PolicyStoreOperation);
+  pso->ctx = ctx;
+  pso->cb = cb;
+  pso->cb_cls = cb_cls;
+  pso->url = ANASTASIS_url_join (backend_url, 
+                                 "policy",
+                                 anastasis_pub);
+  eh = curl_easy_init ();
+  if (CURLE_OK != ANASTASIS_curl_easy_post (&pso->post_ctx,
+                                            eh,
+                                            policy_data,
+                                            policy_data_size))
+  {
+    GNUNET_break (0);
+    GNUNET_free (pso);
+    return NULL;
+  }
+
+  
+
+  GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
+                                               CURLOPT_URL,
+                                               pso->url));
+  pso->job = GNUNET_CURL_job_add2
+               (ctx,
+               eh,
+               pso->post_ctx.headers,
+               &handle_policy_store_finished,
+               pso);
+
+  return pso;
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

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