summaryrefslogtreecommitdiff
path: root/src/mhd/mhd_responses.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-11-24 17:15:01 +0100
committerChristian Grothoff <christian@grothoff.org>2019-11-24 17:15:01 +0100
commit287a8dec9b3d6d2ee77a55fec4afff45f2cb826d (patch)
treedd19753c7e7440c9aa0ee2986dfa0d725f464ac4 /src/mhd/mhd_responses.c
parent481223e2eb9d1311b245fc35ad487cffbbb4de8f (diff)
downloadexchange-287a8dec9b3d6d2ee77a55fec4afff45f2cb826d.tar.gz
exchange-287a8dec9b3d6d2ee77a55fec4afff45f2cb826d.tar.bz2
exchange-287a8dec9b3d6d2ee77a55fec4afff45f2cb826d.zip
add another convenience function to libtalermhd
Diffstat (limited to 'src/mhd/mhd_responses.c')
-rw-r--r--src/mhd/mhd_responses.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/mhd/mhd_responses.c b/src/mhd/mhd_responses.c
index e1609d996..3642e7d54 100644
--- a/src/mhd/mhd_responses.c
+++ b/src/mhd/mhd_responses.c
@@ -152,6 +152,43 @@ TALER_MHD_body_compress (void **buf,
/**
+ * Make JSON response object.
+ *
+ * @param json the json object
+ * @return MHD response object
+ */
+struct MHD_Response *
+TALER_MHD_make_json (const json_t *json)
+{
+ struct MHD_Response *resp;
+ char *json_str;
+
+ json_str = json_dumps (json,
+ JSON_INDENT (2));
+ if (NULL == json_str)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ resp = MHD_create_response_from_buffer (strlen (json_str),
+ json_str,
+ MHD_RESPMEM_MUST_FREE);
+ if (NULL == resp)
+ {
+ free (json_str);
+ GNUNET_break (0);
+ return NULL;
+ }
+ TALER_MHD_add_global_headers (resp);
+ GNUNET_break (MHD_YES ==
+ MHD_add_response_header (resp,
+ MHD_HTTP_HEADER_CONTENT_TYPE,
+ "application/json"));
+ return resp;
+}
+
+
+/**
* Send JSON object as response.
*
* @param connection the MHD connection
@@ -268,6 +305,60 @@ TALER_MHD_reply_json_pack (struct MHD_Connection *connection,
/**
+ * Make JSON response object.
+ *
+ * @param fmt format string for pack
+ * @param ... varargs
+ * @return MHD response object
+ */
+struct MHD_Response *
+TALER_MHD_make_json_pack (const char *fmt,
+ ...)
+{
+ json_t *json;
+ va_list argp;
+ struct MHD_Response *ret;
+ json_error_t jerror;
+
+ va_start (argp, fmt);
+ json = json_vpack_ex (&jerror,
+ 0,
+ fmt,
+ argp);
+ va_end (argp);
+ if (NULL == json)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to pack JSON with format `%s': %s\n",
+ fmt,
+ jerror.text);
+ GNUNET_break (0);
+ return MHD_NO;
+ }
+ ret = TALER_MHD_make_json (json);
+ json_decref (json);
+ return ret;
+}
+
+
+/**
+ * Create a response indicating an internal error.
+ *
+ * @param ec error code to return
+ * @param hint hint about the internal error's nature
+ * @return a MHD response object
+ */
+struct MHD_Response *
+TALER_MHD_make_error (enum TALER_ErrorCode ec,
+ const char *hint)
+{
+ return TALER_MHD_make_json_pack ("{s:I, s:s}",
+ "code", (json_int_t) ec,
+ "hint", hint);
+}
+
+
+/**
* Send a response indicating an error.
*
* @param connection the MHD connection to use