summaryrefslogtreecommitdiff
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
parent0a5308e71e7f65a9921d74e8d55b389654defca6 (diff)
downloadmerchant-1684fc64333b97aee8e6128e96ff4920e494e2e0.tar.gz
merchant-1684fc64333b97aee8e6128e96ff4920e494e2e0.tar.bz2
merchant-1684fc64333b97aee8e6128e96ff4920e494e2e0.zip
replace key-value templating with JSON templating
-rw-r--r--src/backend/taler-merchant-httpd_get-orders-ID.c89
-rw-r--r--src/backend/taler-merchant-httpd_get-tips-ID.c23
-rw-r--r--src/backend/taler-merchant-httpd_templating.c125
-rw-r--r--src/backend/taler-merchant-httpd_templating.h22
4 files changed, 64 insertions, 195 deletions
diff --git a/src/backend/taler-merchant-httpd_get-orders-ID.c b/src/backend/taler-merchant-httpd_get-orders-ID.c
index a02c45d5..aabdde7f 100644
--- a/src/backend/taler-merchant-httpd_get-orders-ID.c
+++ b/src/backend/taler-merchant-httpd_get-orders-ID.c
@@ -532,30 +532,30 @@ send_pay_request (struct GetOrderData *god,
return MHD_NO;
}
{
- struct KVC kvc[] = {
- { "taler_pay_uri",
- taler_pay_uri },
- { "order_status_url",
- order_status_url },
- { "taler_pay_qrcode_svg",
- qr },
- { "order_summary",
- get_order_summary (god) },
- { NULL, NULL }
- };
enum GNUNET_GenericReturnValue res;
-
+ json_t *context;
+ context = json_pack ("{s:s, s:s, s:s, s:s:}",
+ "taler_pay_uri",
+ taler_pay_uri,
+ "order_status_url",
+ order_status_url,
+ "taler_pay_qrcode_svg",
+ qr,
+ "order_summary",
+ get_order_summary (god));
+ GNUNET_assert (NULL != context);
res = TMH_return_from_template (god->sc.con,
MHD_HTTP_PAYMENT_REQUIRED,
"request_payment",
taler_pay_uri,
- kvc);
+ context);
if (GNUNET_SYSERR == res)
{
GNUNET_break (0);
ret = MHD_NO;
}
ret = MHD_YES;
+ json_decref (context);
}
GNUNET_free (qr);
}
@@ -1110,51 +1110,44 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh,
"during QR code generation");
}
{
- struct KVC kvc[] = {
- { "order_summary",
- get_order_summary (god) },
- { "refund_amount",
- TALER_amount2s (&god->refund_amount) },
- { "taler_refund_uri",
- uri },
- { "taler_refund_qrcode_svg",
- qr },
- { NULL, NULL }
- };
-
+ json_t *context;
+ context = json_pack ("{s:s, s:s, s:s, s:s}"
+ "order_summary",
+ get_order_summary (god),
+ "refund_amount",
+ TALER_amount2s (&god->refund_amount),
+ "taler_refund_uri",
+ uri,
+ "taler_refund_qrcode_svg",
+ qr);
+ GNUNET_assert (NULL != context);
res = TMH_return_from_template (god->sc.con,
MHD_HTTP_OK,
"offer_refund",
uri,
- kvc);
+ context);
+ json_decref (context);
}
GNUNET_free (uri);
GNUNET_free (qr);
}
else
{
- char *j;
-
- j = json_dumps (god->contract_terms,
- JSON_COMPACT | JSON_SORT_KEYS); /* use most efficient encoding */
- {
- struct KVC kvc[] = {
- { "order_summary",
- get_order_summary (god) },
- { "refund_amount",
- TALER_amount2s (&god->refund_amount) },
- { "contract",
- j },
- { NULL, NULL }
- };
-
- res = TMH_return_from_template (god->sc.con,
- MHD_HTTP_OK,
- "show_order_details",
- NULL,
- kvc);
- }
- free (j);
+ json_t *context;
+ context = json_pack ("{s:O, s:s, s:s}",
+ "contract_terms",
+ god->contract_terms,
+ "order_summary",
+ get_order_summary (god),
+ "refund_amount",
+ TALER_amount2s (&god->refund_amount));
+ GNUNET_assert (NULL != context);
+ res = TMH_return_from_template (god->sc.con,
+ MHD_HTTP_OK,
+ "show_order_details",
+ NULL,
+ context);
+ json_decref (context);
}
if (GNUNET_SYSERR == res)
{
diff --git a/src/backend/taler-merchant-httpd_get-tips-ID.c b/src/backend/taler-merchant-httpd_get-tips-ID.c
index 237c1a4a..0d853d70 100644
--- a/src/backend/taler-merchant-httpd_get-tips-ID.c
+++ b/src/backend/taler-merchant-httpd_get-tips-ID.c
@@ -269,17 +269,15 @@ TMH_get_tips_ID (const struct TMH_RequestHandler *rh,
}
else
{
- struct KVC kvc[] = {
- { "remaining_tip",
- TALER_amount2s (&remaining) },
- { "tip_status_url",
- tip_status_url },
- { "taler_tip_uri",
- uri },
- { "taler_tip_qrcode_svg",
- qr },
- { NULL, NULL }
- };
+ json_t *context;
+ context = json_pack ("{s:s, s:s, s:s, s:s}",
+ "remaining_tip",
+ TALER_amount2s (&remaining),
+ "taler_tip_uri",
+ uri,
+ "taler_tip_qrcode_svg",
+ qr);
+ GNUNET_assert (NULL != context);
ret = TMH_return_from_template (connection,
( (0 == remaining.value) &&
@@ -291,7 +289,8 @@ TMH_get_tips_ID (const struct TMH_RequestHandler *rh,
? "depleted_tip"
: "offer_tip",
uri,
- kvc);
+ context);
+ json_decref (context);
}
GNUNET_free (tip_status_url);
GNUNET_free (uri);
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",
diff --git a/src/backend/taler-merchant-httpd_templating.h b/src/backend/taler-merchant-httpd_templating.h
index e3c9d53e..6f22e791 100644
--- a/src/backend/taler-merchant-httpd_templating.h
+++ b/src/backend/taler-merchant-httpd_templating.h
@@ -23,25 +23,9 @@
#include <microhttpd.h>
-/**
- * Entry in a key-value array we use as the mustach closure.
- */
-struct KVC
-{
- /**
- * A name, used as the key. NULL for the last entry.
- */
- const char *name;
-
- /**
- * 0-terminated string value to return for @e name.
- */
- const char *value;
-};
-
/**
- * 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.
*
@@ -49,7 +33,7 @@ struct KVC
* @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)
*/
@@ -58,7 +42,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);
/**
* Preload templates.