From d0166e648d9b6ed637a819bb59d070c40be8f200 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 26 Jul 2020 15:50:35 +0200 Subject: add logic to load templates --- src/backend/taler-merchant-httpd_get-orders-ID.c | 223 ++++++++++++++++++++--- 1 file changed, 196 insertions(+), 27 deletions(-) (limited to 'src/backend/taler-merchant-httpd_get-orders-ID.c') diff --git a/src/backend/taler-merchant-httpd_get-orders-ID.c b/src/backend/taler-merchant-httpd_get-orders-ID.c index 60b12d66..ad1d5964 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; iPay me: {{pay_uri}} {{pay_qr}}", - &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 ( - "Paid. Refund: {{refund_amount}}", - &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