summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_templating.c
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2021-05-17 14:39:43 +0200
committerFlorian Dold <florian@dold.me>2021-05-17 14:39:52 +0200
commite0e86588565baf0111db24b431e8a4c908017da5 (patch)
tree85d7e6073e1aebc359db49fe4d135a50400e8179 /src/backend/taler-merchant-httpd_templating.c
parentd671ac4a7a9fcac65d8a0baa4f846e934e77f0d9 (diff)
downloadmerchant-e0e86588565baf0111db24b431e8a4c908017da5.tar.gz
merchant-e0e86588565baf0111db24b431e8a4c908017da5.tar.bz2
merchant-e0e86588565baf0111db24b431e8a4c908017da5.zip
pass static_url to templates
Diffstat (limited to 'src/backend/taler-merchant-httpd_templating.c')
-rw-r--r--src/backend/taler-merchant-httpd_templating.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/backend/taler-merchant-httpd_templating.c b/src/backend/taler-merchant-httpd_templating.c
index 393104dc..9d7766b9 100644
--- 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,