diff options
Diffstat (limited to 'src/templating/templating_api.c')
-rw-r--r-- | src/templating/templating_api.c | 95 |
1 files changed, 81 insertions, 14 deletions
diff --git a/src/templating/templating_api.c b/src/templating/templating_api.c index 999ba9dae..88a17c682 100644 --- a/src/templating/templating_api.c +++ b/src/templating/templating_api.c @@ -100,7 +100,7 @@ lookup_template (struct MHD_Connection *connection, if (NULL == best) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No templates found in `%s'\n", + "No templates found for `%s'\n", name); return NULL; } @@ -171,13 +171,40 @@ make_static_url (struct MHD_Connection *con, } +int +TALER_TEMPLATING_fill (const char *tmpl, + const json_t *root, + void **result, + size_t *result_size) +{ + int eno; + + if (0 != + (eno = mustach_jansson_mem (tmpl, + 0, /* length of tmpl */ + (json_t *) root, + Mustach_With_AllExtensions, + (char **) result, + result_size))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "mustach failed on template with error %d\n", + eno); + *result = NULL; + *result_size = 0; + return eno; + } + return eno; +} + + enum GNUNET_GenericReturnValue TALER_TEMPLATING_build (struct MHD_Connection *connection, unsigned int *http_status, const char *template, const char *instance_id, const char *taler_uri, - json_t *root, + const json_t *root, struct MHD_Response **reply) { char *body; @@ -191,9 +218,6 @@ TALER_TEMPLATING_build (struct MHD_Connection *connection, template); if (NULL == tmpl) { - /* FIXME: should this not be an - internal failure? The language - mismatch is not critical here! */ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to load template `%s'\n", template); @@ -209,16 +233,18 @@ TALER_TEMPLATING_build (struct MHD_Connection *connection, instance_id); GNUNET_break (0 == - json_object_set_new (root, + json_object_set_new ((json_t *) root, "static_url", json_string (static_url))); GNUNET_free (static_url); } if (0 != - (eno = mustach_jansson (tmpl, - root, - &body, - &body_size))) + (eno = mustach_jansson_mem (tmpl, + 0, + (json_t *) root, + Mustach_With_NoExtensions, + &body, + &body_size))) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "mustach failed on template `%s' with error %d\n", @@ -284,7 +310,7 @@ TALER_TEMPLATING_reply (struct MHD_Connection *connection, const char *template, const char *instance_id, const char *taler_uri, - json_t *root) + const json_t *root) { enum GNUNET_GenericReturnValue res; struct MHD_Response *reply; @@ -314,7 +340,7 @@ TALER_TEMPLATING_reply (struct MHD_Connection *connection, /** * Function called with a template's filename. * - * @param cls closure + * @param cls closure, NULL * @param filename complete filename (absolute path) * @return #GNUNET_OK to continue to iterate, * #GNUNET_NO to stop iteration with no error, @@ -331,9 +357,9 @@ load_template (void *cls, char *map; const char *name; + (void) cls; if ('.' == filename[0]) return GNUNET_OK; - name = strrchr (filename, '/'); if (NULL == name) @@ -368,7 +394,7 @@ load_template (void *cls, &sb)) { GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, - "open", + "fstat", filename); GNUNET_break (0 == close (fd)); return GNUNET_OK; @@ -405,6 +431,47 @@ load_template (void *cls, } +MHD_RESULT +TALER_TEMPLATING_reply_error ( + struct MHD_Connection *connection, + const char *template_basename, + unsigned int http_status, + enum TALER_ErrorCode ec, + const char *detail) +{ + json_t *data; + enum GNUNET_GenericReturnValue ret; + + data = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_uint64 ("ec", + ec), + GNUNET_JSON_pack_string ("hint", + TALER_ErrorCode_get_hint (ec)), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("detail", + detail)) + ); + ret = TALER_TEMPLATING_reply (connection, + http_status, + template_basename, + NULL, + NULL, + data); + json_decref (data); + switch (ret) + { + case GNUNET_OK: + return MHD_YES; + case GNUNET_NO: + return MHD_YES; + case GNUNET_SYSERR: + return MHD_NO; + } + GNUNET_assert (0); + return MHD_NO; +} + + enum GNUNET_GenericReturnValue TALER_TEMPLATING_init (const char *subsystem) { |