summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_templating.c
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-08-21 14:22:15 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-08-21 14:22:15 +0530
commit1684fc64333b97aee8e6128e96ff4920e494e2e0 (patch)
tree11957fa2cb9aa1b09191a92a96a57d8b9902abcc /src/backend/taler-merchant-httpd_templating.c
parent0a5308e71e7f65a9921d74e8d55b389654defca6 (diff)
downloadmerchant-1684fc64333b97aee8e6128e96ff4920e494e2e0.tar.gz
merchant-1684fc64333b97aee8e6128e96ff4920e494e2e0.tar.bz2
merchant-1684fc64333b97aee8e6128e96ff4920e494e2e0.zip
replace key-value templating with JSON templating
Diffstat (limited to 'src/backend/taler-merchant-httpd_templating.c')
-rw-r--r--src/backend/taler-merchant-httpd_templating.c125
1 files changed, 9 insertions, 116 deletions
diff --git a/src/backend/taler-merchant-httpd_templating.c b/src/backend/taler-merchant-httpd_templating.c
index 1b480a67..3d189a75 100644
--- a/src/backend/taler-merchant-httpd_templating.c
+++ b/src/backend/taler-merchant-httpd_templating.c
@@ -24,6 +24,7 @@
#include <taler/taler_mhd_lib.h>
#include "taler-merchant-httpd_templating.h"
#include "../mustach/mustach.h"
+#include "../mustach/mustach-jansson.h"
#include <gnunet/gnunet_mhd_compat.h>
@@ -62,113 +63,6 @@ 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.
- *
- * @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 activate the next item in the
- * section. Does nothing, as we do not support sections.
- *
- * @param cls a `struct KVC[]` array
- * @return 0 (no next item to activate)
- */
-static int
-m_next (void *cls)
-{
- (void) cls;
- 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)
-{
- const 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 = NULL;
- return MUSTACH_OK;
- }
- }
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Template requires value for unexpected name `%s'\n",
- name);
- 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)
-{
- (void) cls;
- (void) status;
-}
-
-
-/**
- * Our 'universal' callbacks for mustach.
- */
-static struct mustach_itf itf = {
- .enter = &m_enter,
- .next = &m_next,
- .leave = &m_leave,
- .get = &m_get,
- .stop = &m_stop
-};
-
-
-/**
* 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.
@@ -215,15 +109,15 @@ lookup_template (struct MHD_Connection *connection,
/**
- * Load a @a template and substitute using @a kvc, returning
+ * Load a @a template and substitute using @a root, returning
* the result to the @a connection with the given
* @a http_status code.
*
* @param connection the connection we act upon
- * @param http_status desired HTTP status code
+ * @param http_status code to use on success
* @param template basename of the template to load
* @param taler_uri value for "Taler:" header to set, or NULL
- * @param kvc key value pairs to fill in
+ * @param root JSON object to pass as the root context
* @return #GNUNET_OK on success (reply queued), #GNUNET_NO if an error was queued,
* #GNUNET_SYSERR on failure (to queue an error)
*/
@@ -232,7 +126,7 @@ TMH_return_from_template (struct MHD_Connection *connection,
unsigned int http_status,
const char *template,
const char *taler_uri,
- const struct KVC *kvc)
+ json_t *root)
{
struct MHD_Response *reply;
char *body;
@@ -258,11 +152,10 @@ TMH_return_from_template (struct MHD_Connection *connection,
return GNUNET_NO;
}
if (0 !=
- (eno = mustach (tmpl,
- &itf,
- (void *) kvc,
- &body,
- &body_size)))
+ (eno = mustach_jansson (tmpl,
+ root,
+ &body,
+ &body_size)))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"mustach failed on template `%s' with error %d\n",