gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] 01/02: Worked on policy post api


From: gnunet
Subject: [taler-anastasis] 01/02: Worked on policy post api
Date: Wed, 20 Nov 2019 08:37:04 +0100

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

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

commit f004fc9f096efe15ae318b39881194da899645e8
Author: Dennis Neufeld <address@hidden>
AuthorDate: Mon Nov 18 11:51:15 2019 +0000

    Worked on policy post api
---
 src/include/anastasis_service.h |  40 +++++-
 src/lib/anastasis_api_policy.c  | 264 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 295 insertions(+), 9 deletions(-)

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/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]