diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-07-26 15:50:35 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-07-26 15:50:35 +0200 |
commit | d0166e648d9b6ed637a819bb59d070c40be8f200 (patch) | |
tree | c3649b690c177c0ca1587849aae2b133aac3299c /src/backend/taler-merchant-httpd_get-orders-ID.c | |
parent | a6bc15b77838981d705e647adb40af0a51621286 (diff) | |
download | merchant-d0166e648d9b6ed637a819bb59d070c40be8f200.tar.gz merchant-d0166e648d9b6ed637a819bb59d070c40be8f200.tar.bz2 merchant-d0166e648d9b6ed637a819bb59d070c40be8f200.zip |
add logic to load templates
Diffstat (limited to 'src/backend/taler-merchant-httpd_get-orders-ID.c')
-rw-r--r-- | src/backend/taler-merchant-httpd_get-orders-ID.c | 223 |
1 files 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 60b12d66..ad1d5964 100644 --- a/src/backend/taler-merchant-httpd_get-orders-ID.c +++ b/src/backend/taler-merchant-httpd_get-orders-ID.c @@ -249,6 +249,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 */ |