From 4f7b141648a68137269cfca9be13fe7d20051c29 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 26 Jul 2020 15:24:23 +0200 Subject: use mustach templating --- src/backend/Makefile.am | 3 +- src/backend/taler-merchant-httpd_get-orders-ID.c | 183 ++++++++++++++++++++--- 2 files changed, 166 insertions(+), 20 deletions(-) (limited to 'src') 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 #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? @@ -230,6 +231,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. */ @@ -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, - "%s", - 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 ( + "Pay me: {{pay_uri}} {{pay_qr}}", + &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 ( + "Paid. Refund: {{refund_amount}}", + &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, - "Paid. Refund: %s", - 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); -- cgit v1.2.3