merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

commit e0e86588565baf0111db24b431e8a4c908017da5
parent d671ac4a7a9fcac65d8a0baa4f846e934e77f0d9
Author: Florian Dold <florian@dold.me>
Date:   Mon, 17 May 2021 14:39:43 +0200

pass static_url to templates

Diffstat:
Mcontrib/depleted_tip.en.must | 2+-
Mcontrib/offer_refund.en.must | 2+-
Mcontrib/offer_tip.en.must | 2+-
Mcontrib/request_payment.en.must | 2+-
Mcontrib/show_order_details.en.must | 2+-
Msrc/backend/taler-merchant-httpd_get-orders-ID.c | 3+++
Msrc/backend/taler-merchant-httpd_get-tips-ID.c | 1+
Msrc/backend/taler-merchant-httpd_templating.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/backend/taler-merchant-httpd_templating.h | 2++
9 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/contrib/depleted_tip.en.must b/contrib/depleted_tip.en.must @@ -24,7 +24,7 @@ </noscript> <title>Status of your tip</title> <link rel="stylesheet" - href="/static/pure-min.css" + href="{{static_url}}pure-min.css" integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" crossorigin="anonymous"> <style> diff --git a/contrib/offer_refund.en.must b/contrib/offer_refund.en.must @@ -24,7 +24,7 @@ </noscript> <title>Refund available for {{order_summary}}</title> <link rel="stylesheet" - href="/static/pure-min.css" + href="{{static_url}}pure-min.css" integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" crossorigin="anonymous"> <style> diff --git a/contrib/offer_tip.en.must b/contrib/offer_tip.en.must @@ -24,7 +24,7 @@ </noscript> <title>Tip available</title> <link rel="stylesheet" - href="/static/pure-min.css" + href="{{static_url}}pure-min.css" integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" crossorigin="anonymous"> <style> diff --git a/contrib/request_payment.en.must b/contrib/request_payment.en.must @@ -24,7 +24,7 @@ </noscript> <title>Payment required for {{order_summary}}</title> <link rel="stylesheet" - href="/static/pure-min.css" + href="{{static_url}}pure-min.css" integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" crossorigin="anonymous"> <style> diff --git a/contrib/show_order_details.en.must b/contrib/show_order_details.en.must @@ -24,7 +24,7 @@ </noscript> <title>Status of your order for {{order_summary}}</title> <link rel="stylesheet" - href="/static/pure-min.css" + href="{{static_url}}pure-min.css" integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" crossorigin="anonymous"> <style> diff --git a/src/backend/taler-merchant-httpd_get-orders-ID.c b/src/backend/taler-merchant-httpd_get-orders-ID.c @@ -562,6 +562,7 @@ send_pay_request (struct GetOrderData *god, res = TMH_return_from_template (god->sc.con, MHD_HTTP_PAYMENT_REQUIRED, "request_payment", + god->hc->instance->settings.id, taler_pay_uri, context); if (GNUNET_SYSERR == res) @@ -1162,6 +1163,7 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh, res = TMH_return_from_template (god->sc.con, MHD_HTTP_OK, "offer_refund", + hc->instance->settings.id, uri, context); json_decref (context); @@ -1184,6 +1186,7 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh, res = TMH_return_from_template (god->sc.con, MHD_HTTP_OK, "show_order_details", + hc->instance->settings.id, NULL, context); json_decref (context); diff --git a/src/backend/taler-merchant-httpd_get-tips-ID.c b/src/backend/taler-merchant-httpd_get-tips-ID.c @@ -262,6 +262,7 @@ TMH_get_tips_ID (const struct TMH_RequestHandler *rh, (0 == remaining.fraction) ) ? "depleted_tip" : "offer_tip", + hc->instance->settings.id, uri, context); json_decref (context); diff --git a/src/backend/taler-merchant-httpd_templating.c b/src/backend/taler-merchant-httpd_templating.c @@ -107,6 +107,70 @@ lookup_template (struct MHD_Connection *connection, return best->value; } +/** + * Get the base URL for static resources. + * + * @param con the MHD connection + * @param instance_id the instance ID + * @returns the static files base URL, guaranteed + * to have a trailing slash. + */ +static char * +make_static_url (struct MHD_Connection *con, + const char *instance_id) +{ + const char *host; + const char *forwarded_host; + const char *uri_path; + struct GNUNET_Buffer buf = { 0 }; + + host = MHD_lookup_connection_value (con, + MHD_HEADER_KIND, + "Host"); + forwarded_host = MHD_lookup_connection_value (con, + MHD_HEADER_KIND, + "X-Forwarded-Host"); + + uri_path = MHD_lookup_connection_value (con, + MHD_HEADER_KIND, + "X-Forwarded-Prefix"); + if (NULL != forwarded_host) + host = forwarded_host; + + if (NULL == host) + { + GNUNET_break (0); + return NULL; + } + + GNUNET_assert (NULL != instance_id); + + if (GNUNET_NO == TALER_mhd_is_https (con)) + GNUNET_buffer_write_str (&buf, + "http://"); + else + GNUNET_buffer_write_str (&buf, + "https://"); + GNUNET_buffer_write_str (&buf, + host); + if (NULL != uri_path) + GNUNET_buffer_write_path (&buf, + uri_path); + if (0 != strcmp ("default", + instance_id)) + { + GNUNET_buffer_write_path (&buf, + "instances"); + GNUNET_buffer_write_path (&buf, + instance_id); + } + GNUNET_buffer_write_path (&buf, + "static/"); + return GNUNET_buffer_reap_str (&buf); +} + + + /** * Load a @a template and substitute using @a root, returning @@ -116,6 +180,7 @@ lookup_template (struct MHD_Connection *connection, * @param connection the connection we act upon * @param http_status code to use on success * @param template basename of the template to load + * @param instance_id instance ID, used to compute static files URL * @param taler_uri value for "Taler:" header to set, or NULL * @param root JSON object to pass as the root context * @return #GNUNET_OK on success (reply queued), #GNUNET_NO if an error was queued, @@ -125,6 +190,7 @@ enum GNUNET_GenericReturnValue TMH_return_from_template (struct MHD_Connection *connection, unsigned int http_status, const char *template, + const char *instance_id, const char *taler_uri, json_t *root) { @@ -151,6 +217,15 @@ TMH_return_from_template (struct MHD_Connection *connection, return GNUNET_SYSERR; return GNUNET_NO; } + /* Add default values to the context */ + { + char *static_url = make_static_url (connection, + instance_id); + json_object_set (root, + "static_url", + json_string (static_url)); + GNUNET_free (static_url); + } if (0 != (eno = mustach_jansson (tmpl, root, diff --git a/src/backend/taler-merchant-httpd_templating.h b/src/backend/taler-merchant-httpd_templating.h @@ -32,6 +32,7 @@ * @param connection the connection we act upon * @param http_status code to use on success * @param template basename of the template to load + * @param instance_id instance ID, used to compute static files URL * @param taler_uri value for "Taler:" header to set, or NULL * @param root JSON object to pass as the root context * @return #GNUNET_OK on success (reply queued), #GNUNET_NO if an error was queued, @@ -41,6 +42,7 @@ enum GNUNET_GenericReturnValue TMH_return_from_template (struct MHD_Connection *connection, unsigned int http_status, const char *template, + const char *instance_id, const char *taler_uri, json_t *root);