summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-07-26 15:24:23 +0200
committerChristian Grothoff <christian@grothoff.org>2020-07-26 15:24:23 +0200
commit4f7b141648a68137269cfca9be13fe7d20051c29 (patch)
tree35fabef99620571827fc961557e19bb95faf45dc /src
parent5d5539925f978df37ac069fd8030a85e60a21b91 (diff)
downloadmerchant-4f7b141648a68137269cfca9be13fe7d20051c29.tar.gz
merchant-4f7b141648a68137269cfca9be13fe7d20051c29.tar.bz2
merchant-4f7b141648a68137269cfca9be13fe7d20051c29.zip
use mustach templating
Diffstat (limited to 'src')
-rw-r--r--src/backend/Makefile.am3
-rw-r--r--src/backend/taler-merchant-httpd_get-orders-ID.c183
2 files changed, 166 insertions, 20 deletions
diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am
index a95026fb..ac2eaafd 100644
--- a/src/backend/Makefile.am
+++ b/src/backend/Makefile.am
@@ -104,7 +104,8 @@ taler_merchant_httpd_LDADD = \
-lgnunetcurl \
-lgnunetjson \
-lgnunetutil \
- @QR_LIBS@
+ $(top_builddir)/src/mustach/libmustach.a \
+ @QR_LIBS@ \
$(XLIB)
taler_merchant_httpd_CFLAGS = \
@QR_CFLAGS@
diff --git a/src/backend/taler-merchant-httpd_get-orders-ID.c b/src/backend/taler-merchant-httpd_get-orders-ID.c
index 5966f02b..60b12d66 100644
--- a/src/backend/taler-merchant-httpd_get-orders-ID.c
+++ b/src/backend/taler-merchant-httpd_get-orders-ID.c
@@ -27,6 +27,7 @@
#include <taler/taler_exchange_service.h>
#include "taler-merchant-httpd_exchanges.h"
#include "taler-merchant-httpd_get-orders-ID.h"
+#include "../mustach/mustach.h"
/**
* How often do we retry DB transactions on serialization failures?
@@ -231,6 +232,103 @@ struct GetOrderData
/**
+ * Entry in a key-value array we use as the mustach closure.
+ */
+struct KVC
+{
+ /**
+ * A name, used as the key. NULL for the last entry.
+ */
+ const char *name;
+
+ /**
+ * 0-terminated string value to return for @e name.
+ */
+ char *value;
+};
+
+
+/**
+ * Function called by Mustach to enter the section @a name.
+ * As we do not support sections, we always return 0.
+ *
+ * @param cls a `struct KVC[]` array
+ * @param name section to enter
+ * @return 0 (do not enter)
+ */
+static int
+m_enter (void *cls, const char *name)
+{
+ (void) cls;
+ (void) name;
+ return 0;
+}
+
+
+/**
+ * Function called by Mustach to leave the current section.
+ * As we do not support sections, we should never be called.
+ *
+ * @param cls a `struct KVC[]` array
+ * @return 0 (not documented by mustach)
+ */
+static int
+m_leave (void *cls)
+{
+ GNUNET_assert (0);
+ return 0;
+}
+
+
+/**
+ * Return the value of @a name in @a sbuf.
+ *
+ * @param cls a `struct KVC[]` array
+ * @param name the value to lookup
+ * @param[out] sbuf where to return the data
+ * @return mustach-defined status code
+ */
+static int
+m_get (void *cls,
+ const char *name,
+ struct mustach_sbuf *sbuf)
+{
+ struct KVC *kvc = cls;
+
+ for (unsigned int i = 0; NULL != kvc[i].name; i++)
+ {
+ if (0 == strcmp (name,
+ kvc[i].name))
+ {
+ sbuf->value = kvc[i].value;
+ sbuf->releasecb = NULL;
+ sbuf->closure = &kvc[i];
+ return MUSTACH_OK;
+ }
+ }
+ return MUSTACH_ERROR_ITEM_NOT_FOUND;
+}
+
+
+/**
+ * Mustach callback at the end. Cleans up the @a cls.
+ *
+ * @param cls a `struct KVC[]` array
+ * @param status status of mustach (ignored)
+ */
+static void
+m_stop (void *cls,
+ int status)
+{
+ struct KVC *kvc = cls;
+
+ (void) status;
+ for (unsigned int i = 0; NULL != kvc[i].name; i++)
+ GNUNET_free (kvc[i].value);
+}
+
+
+/**
* Head of DLL of (suspended) requests.
*/
static struct GetOrderData *god_head;
@@ -458,6 +556,7 @@ send_pay_request (struct GetOrderData *god,
struct MHD_Response *reply;
char *qr;
char *body;
+ size_t body_size;
qr = create_qrcode (taler_pay_uri);
if (NULL == qr)
@@ -465,14 +564,38 @@ send_pay_request (struct GetOrderData *god,
GNUNET_break (0);
return MHD_NO;
}
- GNUNET_asprintf (&body,
- "<html><body>%s</body></html>",
- qr);
- GNUNET_free (qr);
- reply = MHD_create_response_from_buffer (strlen (body),
+ {
+ struct KVC kvc[] = {
+ { "pay_uri",
+ GNUNET_strdup (taler_pay_uri) },
+ { "pay_qr",
+ qr },
+ { NULL, NULL }
+ };
+ struct mustach_itf itf = {
+ .enter = m_enter,
+ .leave = m_leave,
+ .get = m_get,
+ .stop = m_stop
+ };
+
+ if (0 !=
+ mustach (
+ "<html><body>Pay me: {{pay_uri}} {{pay_qr}}</body></html>",
+ &itf,
+ &kvc,
+ &body,
+ &body_size))
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+ "mustach");
+ return MHD_NO; // FIXME: add nicer error reply...
+ }
+ }
+
+ reply = MHD_create_response_from_buffer (body_size,
body,
- MHD_RESPMEM_MUST_COPY);
- GNUNET_free (body);
+ MHD_RESPMEM_MUST_FREE);
if (NULL == reply)
{
GNUNET_break (0);
@@ -1219,25 +1342,47 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh,
int ret;
struct MHD_Response *reply;
char *body;
-
-#if 0
+ size_t body_size;
char *qr;
- qr = create_qrcode (taler_refund_uri);
+ qr = create_qrcode ("taler://refund/FIXME");
if (NULL == qr)
{
GNUNET_break (0);
- return MHD_NO;
+ return MHD_NO; // FIXME: add nicer error reply...
+ }
+
+ {
+ struct KVC kvc[] = {
+ { "refund_amount",
+ GNUNET_strdup (TALER_amount2s (&god->refund_amount)) },
+ { "refund_uri",
+ qr },
+ { NULL, NULL }
+ };
+ struct mustach_itf itf = {
+ .enter = m_enter,
+ .leave = m_leave,
+ .get = m_get,
+ .stop = m_stop
+ };
+
+ if (0 !=
+ mustach (
+ "<html><body>Paid. Refund: {{refund_amount}}</body></html>",
+ &itf,
+ &kvc,
+ &body,
+ &body_size))
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+ "mustach");
+ return MHD_NO; // FIXME: add nicer error reply...
+ }
}
-#endif
- GNUNET_asprintf (&body,
- "<html><body>Paid. Refund: %s</body></html>",
- TALER_amount2s (&god->refund_amount));
- // GNUNET_free (qr);
- reply = MHD_create_response_from_buffer (strlen (body),
+ reply = MHD_create_response_from_buffer (body_size,
body,
- MHD_RESPMEM_MUST_COPY);
- GNUNET_free (body);
+ MHD_RESPMEM_MUST_FREE);
if (NULL == reply)
{
GNUNET_break (0);