gnunet-svn
[Top][All Lists]
Advanced

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

[taler-merchant] branch master updated: add logic to load templates


From: gnunet
Subject: [taler-merchant] branch master updated: add logic to load templates
Date: Sun, 26 Jul 2020 15:50:37 +0200

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

grothoff pushed a commit to branch master
in repository merchant.

The following commit(s) were added to refs/heads/master by this push:
     new d0166e6  add logic to load templates
d0166e6 is described below

commit d0166e648d9b6ed637a819bb59d070c40be8f200
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sun Jul 26 15:50:35 2020 +0200

    add logic to load templates
---
 src/backend/taler-merchant-httpd_get-orders-ID.c | 223 ++++++++++++++++++++---
 1 file changed, 196 insertions(+), 27 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_get-orders-ID.c 
b/src/backend/taler-merchant-httpd_get-orders-ID.c
index 60b12d6..ad1d596 100644
--- a/src/backend/taler-merchant-httpd_get-orders-ID.c
+++ b/src/backend/taler-merchant-httpd_get-orders-ID.c
@@ -248,6 +248,45 @@ struct KVC
 };
 
 
+/**
+ * Entry in a key-value array we use to cache templates.
+ */
+struct TVE
+{
+  /**
+   * A name, used as the key. NULL for the last entry.
+   */
+  const char *name;
+
+  /**
+   * 0-terminated (!) file data to return for @e name.
+   */
+  char *value;
+
+};
+
+
+/**
+ * Head of DLL of (suspended) requests.
+ */
+static struct GetOrderData *god_head;
+
+/**
+ * Tail of DLL of (suspended) requests.
+ */
+static struct GetOrderData *god_tail;
+
+/**
+ * Templated loaded into RAM.
+ */
+static struct TVE *loaded;
+
+/**
+ * Length of the #loaded array.
+ */
+static unsigned int loaded_length;
+
+
 /**
  * Function called by Mustach to enter the section @a name.
  * As we do not support sections, we always return 0.
@@ -329,14 +368,104 @@ m_stop (void *cls,
 
 
 /**
- * Head of DLL of (suspended) requests.
+ * Our 'universal' callbacks for mustach.
  */
-static struct GetOrderData *god_head;
+static struct mustach_itf itf = {
+  .enter = m_enter,
+  .leave = m_leave,
+  .get = m_get,
+  .stop = m_stop
+};
+
 
 /**
- * Tail of DLL of (suspended) requests.
+ * Load Mustach template into memory.  Note that we intentionally cache
+ * failures, that is if we ever failed to load a template, we will never try
+ * again.
+ *
+ * @param name name of the template file to load
+ *        (MUST be a 'static' string in memory!)
+ * @return NULL on error, otherwise the template
  */
-static struct GetOrderData *god_tail;
+static const char *
+load_template (const char *name)
+{
+  char *fn;
+  char *map;
+  int fd;
+  struct stat sb;
+
+  for (unsigned int i = 0; i<loaded_length; i++)
+  {
+    if (0 == strcmp (loaded[i].name,
+                     name))
+      return loaded[i].value;
+  }
+  GNUNET_array_grow (loaded,
+                     loaded_length,
+                     loaded_length + 1);
+  loaded[loaded_length - 1].name = name;
+  {
+    char *path;
+
+    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
+    if (NULL == path)
+    {
+      GNUNET_break (0);
+      return NULL;
+    }
+    GNUNET_asprintf (&fn,
+                     "%s/%s.must",
+                     path,
+                     name);
+    GNUNET_free (path);
+  }
+  fd = open (fn, O_RDONLY);
+  if (-1 == fd)
+  {
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                              "open",
+                              fn);
+    GNUNET_free (fn);
+    return NULL;
+  }
+  if (0 !=
+      fstat (fd,
+             &sb))
+  {
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                              "open",
+                              fn);
+    GNUNET_free (fn);
+    GNUNET_break (0 == close (fd));
+    return NULL;
+  }
+  map = GNUNET_malloc_large (sb.st_size + 1);
+  if (NULL == map)
+  {
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                         "malloc");
+    GNUNET_free (fn);
+    GNUNET_break (0 == close (fd));
+    return NULL;
+  }
+  if (sb.st_size !=
+      read (fd,
+            map,
+            sb.st_size))
+  {
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                              "read",
+                              fn);
+    GNUNET_free (fn);
+    GNUNET_break (0 == close (fd));
+    return NULL;
+  }
+  GNUNET_break (0 == close (fd));
+  GNUNET_free (fn);
+  loaded[loaded_length - 1].value = map;
+  return loaded[loaded_length - 1].value;
+}
 
 
 /**
@@ -572,20 +701,21 @@ send_pay_request (struct GetOrderData *god,
           qr },
         { NULL, NULL }
       };
-      struct mustach_itf itf = {
-        .enter = m_enter,
-        .leave = m_leave,
-        .get = m_get,
-        .stop = m_stop
-      };
+      const char *tmpl;
 
+      tmpl = load_template ("request_payment");
+      if (NULL == tmpl)
+      {
+        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                             "mustach");
+        return MHD_NO; // FIXME: add nicer error reply...
+      }
       if (0 !=
-          mustach (
-            "<html><body>Pay me: {{pay_uri}} {{pay_qr}}</body></html>",
-            &itf,
-            &kvc,
-            &body,
-            &body_size))
+          mustach (tmpl,
+                   &itf,
+                   &kvc,
+                   &body,
+                   &body_size))
       {
         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                              "mustach");
@@ -1352,6 +1482,7 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh,
         return MHD_NO; // FIXME: add nicer error reply...
       }
 
+      if (god->refunded)
       {
         struct KVC kvc[] = {
           { "refund_amount",
@@ -1360,20 +1491,47 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh,
             qr },
           { NULL, NULL }
         };
-        struct mustach_itf itf = {
-          .enter = m_enter,
-          .leave = m_leave,
-          .get = m_get,
-          .stop = m_stop
+        const char *tmpl;
+
+        tmpl = load_template ("offer_refund");
+        if (NULL == tmpl)
+        {
+          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                               "mustach");
+          return MHD_NO; // FIXME: add nicer error reply...
+        }
+        if (0 !=
+            mustach (tmpl,
+                     &itf,
+                     &kvc,
+                     &body,
+                     &body_size))
+        {
+          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                               "mustach");
+          return MHD_NO; // FIXME: add nicer error reply...
+        }
+      }
+      else
+      {
+        struct KVC kvc[] = {
+          { NULL, NULL }
         };
+        const char *tmpl;
 
+        tmpl = load_template ("show_order_details");
+        if (NULL == tmpl)
+        {
+          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                               "mustach");
+          return MHD_NO; // FIXME: add nicer error reply...
+        }
         if (0 !=
-            mustach (
-              "<html><body>Paid. Refund: {{refund_amount}}</body></html>",
-              &itf,
-              &kvc,
-              &body,
-              &body_size))
+            mustach (tmpl,
+                     &itf,
+                     &kvc,
+                     &body,
+                     &body_size))
         {
           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                                "mustach");
@@ -1416,4 +1574,15 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh,
 }
 
 
+/**
+ * Nicely shut down.
+ */
+void __attribute__ ((destructor))
+get_orders_fini ()
+{
+  for (unsigned int i = 0; i<loaded_length; i++)
+    GNUNET_free (loaded[i].value);
+}
+
+
 /* end of taler-merchant-httpd_get-orders-ID.c */

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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