start to use new GNUNET_JSON_PACK functionality

This commit is contained in:
Christian Grothoff 2021-07-28 09:13:21 +02:00
parent 225543b800
commit 6516f1899f
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
3 changed files with 121 additions and 216 deletions

View File

@ -94,28 +94,27 @@ TEH_RESPONSE_compile_transaction_history (
if (0 !=
json_array_append_new (
history,
json_pack (
"{s:s, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o}",
"type",
"DEPOSIT",
"amount",
TALER_JSON_from_amount (&deposit->amount_with_fee),
"deposit_fee",
TALER_JSON_from_amount (&deposit->deposit_fee),
"timestamp",
GNUNET_JSON_from_time_abs (deposit->timestamp),
"refund_deadline",
GNUNET_JSON_from_time_abs (deposit->refund_deadline),
"merchant_pub",
GNUNET_JSON_from_data_auto (&deposit->merchant_pub),
"h_contract_terms",
GNUNET_JSON_from_data_auto (&deposit->h_contract_terms),
"h_wire",
GNUNET_JSON_from_data_auto (&deposit->h_wire),
"h_denom_pub",
GNUNET_JSON_from_data_auto (&deposit->h_denom_pub),
"coin_sig",
GNUNET_JSON_from_data_auto (&deposit->csig))))
GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("type",
"DEPOSIT"),
TALER_JSON_pack_amount ("amount",
&deposit->amount_with_fee),
TALER_JSON_pack_amount ("deposit_fee",
&deposit->deposit_fee),
GNUNET_JSON_pack_time_abs ("timestamp",
deposit->timestamp),
GNUNET_JSON_pack_time_abs ("refund_deadline",
deposit->refund_deadline),
GNUNET_JSON_pack_data_auto ("merchant_pub",
&deposit->merchant_pub),
GNUNET_JSON_pack_data_auto ("h_contract_terms",
&deposit->h_contract_terms),
GNUNET_JSON_pack_data_auto ("h_wire",
&deposit->h_wire),
GNUNET_JSON_pack_data_auto ("h_denom_pub",
&deposit->h_denom_pub),
GNUNET_JSON_pack_data_auto ("coin_sig",
&deposit->csig))))
{
GNUNET_break (0);
json_decref (history);
@ -155,19 +154,19 @@ TEH_RESPONSE_compile_transaction_history (
if (0 !=
json_array_append_new (
history,
json_pack ("{s:s, s:o, s:o, s:o, s:o, s:o}",
"type",
"MELT",
"amount",
TALER_JSON_from_amount (&melt->amount_with_fee),
"melt_fee",
TALER_JSON_from_amount (&melt->melt_fee),
"rc",
GNUNET_JSON_from_data_auto (&melt->rc),
"h_denom_pub",
GNUNET_JSON_from_data_auto (&melt->h_denom_pub),
"coin_sig",
GNUNET_JSON_from_data_auto (&melt->coin_sig))))
GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("type",
"MELT"),
TALER_JSON_pack_amount ("amount",
&melt->amount_with_fee),
TALER_JSON_pack_amount ("melt_fee",
&melt->melt_fee),
GNUNET_JSON_pack_data_auto ("rc",
&melt->rc),
GNUNET_JSON_pack_data_auto ("h_denom_pub",
&melt->h_denom_pub),
GNUNET_JSON_pack_data_auto ("coin_sig",
&melt->coin_sig))))
{
GNUNET_break (0);
json_decref (history);
@ -216,22 +215,21 @@ TEH_RESPONSE_compile_transaction_history (
if (0 !=
json_array_append_new (
history,
json_pack (
"{s:s, s:o, s:o, s:o, s:o, s:I, s:o}",
"type",
"REFUND",
"amount",
TALER_JSON_from_amount (&value),
"refund_fee",
TALER_JSON_from_amount (&refund->refund_fee),
"h_contract_terms",
GNUNET_JSON_from_data_auto (&refund->h_contract_terms),
"merchant_pub",
GNUNET_JSON_from_data_auto (&refund->merchant_pub),
"rtransaction_id",
(json_int_t) refund->rtransaction_id,
"merchant_sig",
GNUNET_JSON_from_data_auto (&refund->merchant_sig))))
GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("type",
"REFUND"),
TALER_JSON_pack_amount ("amount",
&value),
TALER_JSON_pack_amount ("refund_fee",
&refund->refund_fee),
GNUNET_JSON_pack_data_auto ("h_contract_terms",
&refund->h_contract_terms),
GNUNET_JSON_pack_data_auto ("merchant_pub",
&refund->merchant_pub),
GNUNET_JSON_pack_uint64 ("rtransaction_id",
refund->rtransaction_id),
GNUNET_JSON_pack_data_auto ("merchant_sig",
&refund->merchant_sig))))
{
GNUNET_break (0);
json_decref (history);
@ -269,23 +267,23 @@ TEH_RESPONSE_compile_transaction_history (
the denomination key's RSA signature over coin_pub, but as the
wallet should really already have this information (and cannot
check or do anything with it anyway if it doesn't), it seems
strictly unnecessary. *///
strictly unnecessary. */
if (0 !=
json_array_append_new (
history,
json_pack ("{s:s, s:o, s:o, s:o, s:o, s:o}",
"type",
"OLD-COIN-RECOUP",
"amount",
TALER_JSON_from_amount (&pr->value),
"exchange_sig",
GNUNET_JSON_from_data_auto (&esig),
"exchange_pub",
GNUNET_JSON_from_data_auto (&epub),
"coin_pub",
GNUNET_JSON_from_data_auto (&pr->coin.coin_pub),
"timestamp",
GNUNET_JSON_from_time_abs (pr->timestamp))))
GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("type",
"OLD-COIN-RECOUP"),
TALER_JSON_pack_amount ("amount",
&pr->value),
GNUNET_JSON_pack_data_auto ("exchange_sig",
&esig),
GNUNET_JSON_pack_data_auto ("exchange_pub",
&epub),
GNUNET_JSON_pack_data_auto ("coin_pub",
&pr->coin.coin_pub),
GNUNET_JSON_pack_time_abs ("timestamp",
pr->timestamp))))
{
GNUNET_break (0);
json_decref (history);
@ -321,27 +319,27 @@ TEH_RESPONSE_compile_transaction_history (
if (0 !=
json_array_append_new (
history,
json_pack ("{s:s, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o}",
"type",
"RECOUP",
"amount",
TALER_JSON_from_amount (&recoup->value),
"exchange_sig",
GNUNET_JSON_from_data_auto (&esig),
"exchange_pub",
GNUNET_JSON_from_data_auto (&epub),
"reserve_pub",
GNUNET_JSON_from_data_auto (&recoup->reserve_pub),
"h_denom_pub",
GNUNET_JSON_from_data_auto (&recoup->h_denom_pub),
"coin_sig",
GNUNET_JSON_from_data_auto (&recoup->coin_sig),
"coin_blind",
GNUNET_JSON_from_data_auto (&recoup->coin_blind),
"reserve_pub",
GNUNET_JSON_from_data_auto (&recoup->reserve_pub),
"timestamp",
GNUNET_JSON_from_time_abs (recoup->timestamp))))
GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("type",
"RECOUP"),
TALER_JSON_pack_amount ("amount",
&recoup->value),
GNUNET_JSON_pack_data_auto ("exchange_sig",
&esig),
GNUNET_JSON_pack_data_auto ("exchange_pub",
&epub),
GNUNET_JSON_pack_data_auto ("reserve_pub",
&recoup->reserve_pub),
GNUNET_JSON_pack_data_auto ("h_denom_pub",
&recoup->h_denom_pub),
GNUNET_JSON_pack_data_auto ("coin_sig",
&recoup->coin_sig),
GNUNET_JSON_pack_data_auto ("coin_blind",
&recoup->coin_blind),
GNUNET_JSON_pack_data_auto ("reserve_pub",
&recoup->reserve_pub),
GNUNET_JSON_pack_time_abs ("timestamp",
recoup->timestamp))))
{
GNUNET_break (0);
json_decref (history);

View File

@ -141,6 +141,20 @@ TALER_MHD_reply_json_pack (struct MHD_Connection *connection,
...);
/**
* Function to call to handle the request by building a JSON
* reply from varargs.
*
* @param connection the MHD connection to handle
* @param response_code HTTP response code to use
* @param ... varargs of JSON pack specification
* @return MHD result code
*/
#define TALER_MHD_REPLY_JSON_PACK(connection,response_code,...) \
TALER_MHD_reply_json (connection, GNUNET_JSON_PACK (__VA_ARGS__), \
response_code)
/**
* Send a response indicating an error.
*
@ -194,6 +208,16 @@ TALER_MHD_make_json_pack (const char *fmt,
...);
/**
* Make JSON response object.
*
* @param ... varargs
* @return MHD response object
*/
#define TALER_MHD_MAKE_JSON_PACK(...) \
TALER_MHD_make_json (GNUNET_JSON_PACK (__VA_ARGS__))
/**
* Create a response indicating an internal error.
*

View File

@ -1,6 +1,6 @@
/*
This file is part of TALER
Copyright (C) 2014-2020 Taler Systems SA
Copyright (C) 2014-2021 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
@ -32,12 +32,6 @@
static enum TALER_MHD_GlobalOptions TM_go;
/**
* Set global options for response generation
* within libtalermhd.
*
* @param go global options to use
*/
void
TALER_MHD_setup (enum TALER_MHD_GlobalOptions go)
{
@ -45,13 +39,6 @@ TALER_MHD_setup (enum TALER_MHD_GlobalOptions go)
}
/**
* Add headers we want to return in every response.
* Useful for testing, like if we want to always close
* connections.
*
* @param response response to modify
*/
void
TALER_MHD_add_global_headers (struct MHD_Response *response)
{
@ -69,17 +56,6 @@ TALER_MHD_add_global_headers (struct MHD_Response *response)
}
/**
* Is HTTP body deflate compression supported by the client?
*
* @param connection connection to check
* @return #MHD_YES if 'deflate' compression is allowed
*
* Note that right now we're ignoring q-values, which is technically
* not correct, and also do not support "*" anywhere but in a line by
* itself. This should eventually be fixed, see also
* https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
*/
MHD_RESULT
TALER_MHD_can_compress (struct MHD_Connection *connection)
{
@ -111,13 +87,6 @@ TALER_MHD_can_compress (struct MHD_Connection *connection)
}
/**
* Try to compress a response body. Updates @a buf and @a buf_size.
*
* @param[in,out] buf pointer to body to compress
* @param[in,out] buf_size pointer to initial size of @a buf
* @return #MHD_YES if @a buf was compressed
*/
MHD_RESULT
TALER_MHD_body_compress (void **buf,
size_t *buf_size)
@ -148,12 +117,6 @@ 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)
{
@ -185,14 +148,6 @@ TALER_MHD_make_json (const json_t *json)
}
/**
* Send JSON object as response.
*
* @param connection the MHD connection
* @param json the json object
* @param response_code the http response code
* @return MHD result code
*/
MHD_RESULT
TALER_MHD_reply_json (struct MHD_Connection *connection,
const json_t *json,
@ -264,13 +219,6 @@ TALER_MHD_reply_json (struct MHD_Connection *connection,
}
/**
* Send back a "204 No Content" response with headers
* for the CORS pre-flight request.
*
* @param connection the MHD connection
* @return MHD result code
*/
MHD_RESULT
TALER_MHD_reply_cors_preflight (struct MHD_Connection *connection)
{
@ -307,16 +255,6 @@ TALER_MHD_reply_cors_preflight (struct MHD_Connection *connection)
}
/**
* Function to call to handle the request by building a JSON
* reply from a format string and varargs.
*
* @param connection the MHD connection to handle
* @param response_code HTTP response code to use
* @param fmt format string for pack
* @param ... varargs
* @return MHD result code
*/
MHD_RESULT
TALER_MHD_reply_json_pack (struct MHD_Connection *connection,
unsigned int response_code,
@ -360,13 +298,6 @@ 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,
...)
@ -405,57 +336,34 @@ TALER_MHD_make_json_pack (const char *fmt,
}
/**
* Create a response indicating an internal error.
*
* @param ec error code to return
* @param detail additional optional detail about the error, can be NULL
* @return a MHD response object
*/
struct MHD_Response *
TALER_MHD_make_error (enum TALER_ErrorCode ec,
const char *detail)
{
return TALER_MHD_make_json_pack ("{s:I, s:s, s:s?}",
"code", (json_int_t) ec,
"hint", TALER_ErrorCode_get_hint (ec),
"detail", detail);
return TALER_MHD_make_JSON_PACK (
GNUNET_JSON_pack_uint64 ("code", ec),
GNUNET_JSON_pack_string ("hint", TALER_ErrorCode_get_hint (ec)),
GNUNET_JSON_pack_allow_null (
GNUNET_JSON_pack_string ("detail", detail)));
}
/**
* Send a response indicating an error.
*
* @param connection the MHD connection to use
* @param ec error code uniquely identifying the error
* @param http_status HTTP status code to use
* @param detail additional optional detail about the error, can be NULL
* @return a MHD result code
*/
MHD_RESULT
TALER_MHD_reply_with_error (struct MHD_Connection *connection,
unsigned int http_status,
enum TALER_ErrorCode ec,
const char *detail)
{
return TALER_MHD_reply_json_pack (connection,
http_status,
"{s:I, s:s, s:s?}",
"code", (json_int_t) ec,
"hint", TALER_ErrorCode_get_hint (ec),
"detail", detail);
return TALER_MHD_reply_JSON_PACK (
connection,
http_status,
GNUNET_JSON_pack_uint64 ("code", ec),
GNUNET_JSON_pack_string ("hint", TALER_ErrorCode_get_hint (ec)),
GNUNET_JSON_pack_allow_null (
GNUNET_JSON_pack_string ("detail", detail)));
}
/**
* Send a response indicating an error. The HTTP status code is
* to be derived from the @a ec.
*
* @param connection the MHD connection to use
* @param ec error code uniquely identifying the error
* @param detail additional optional detail about the error
* @return a MHD result code
*/
MHD_RESULT
TALER_MHD_reply_with_ec (struct MHD_Connection *connection,
enum TALER_ErrorCode ec,
@ -478,12 +386,6 @@ TALER_MHD_reply_with_ec (struct MHD_Connection *connection,
}
/**
* Send a response indicating that the request was too big.
*
* @param connection the MHD connection to use
* @return a MHD result code
*/
MHD_RESULT
TALER_MHD_reply_request_too_large (struct MHD_Connection *connection)
{
@ -508,14 +410,6 @@ TALER_MHD_reply_request_too_large (struct MHD_Connection *connection)
}
/**
* Function to call to handle the request by sending
* back a redirect to the AGPL source code.
*
* @param connection the MHD connection to handle
* @param url where to redirect for the sources
* @return MHD result code
*/
MHD_RESULT
TALER_MHD_reply_agpl (struct MHD_Connection *connection,
const char *url)
@ -559,17 +453,6 @@ TALER_MHD_reply_agpl (struct MHD_Connection *connection,
}
/**
* Function to call to handle the request by sending
* back static data.
*
* @param connection the MHD connection to handle
* @param http_status status code to return
* @param mime_type content-type to use
* @param body response payload
* @param body_size number of bytes in @a body
* @return MHD result code
*/
MHD_RESULT
TALER_MHD_reply_static (struct MHD_Connection *connection,
unsigned int http_status,