From 589efd719a711751a1828ddea0e5d5e269ec0caa Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 1 Aug 2021 16:07:26 +0200 Subject: -misc fixes, more json_pack converting --- src/backend/taler-merchant-httpd.c | 8 +- src/backend/taler-merchant-httpd_auditors.c | 16 +-- src/backend/taler-merchant-httpd_config.c | 13 +- src/backend/taler-merchant-httpd_helper.c | 8 +- src/backend/taler-merchant-httpd_helper.h | 12 ++ .../taler-merchant-httpd_post-orders-ID-pay.c | 144 +++++++-------------- ...ler-merchant-httpd_private-patch-instances-ID.c | 8 +- src/lib/merchant_api_patch_instance.c | 4 - src/testing/testing_api_cmd_get_instance.c | 5 +- src/testing/testing_api_cmd_patch_instance.c | 3 + 10 files changed, 98 insertions(+), 123 deletions(-) (limited to 'src') diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index 577672b9..3d63ed46 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -2068,9 +2068,11 @@ add_instance_cb (void *cls, wm = GNUNET_new (struct TMH_WireMethod); wm->h_wire = acc->h_wire; - wm->j_wire = json_pack ("{s:s, s:o}", - "payto_uri", acc->payto_uri, - "salt", GNUNET_JSON_from_data_auto (&acc->salt)); + wm->j_wire = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("payto_uri", + acc->payto_uri), + GNUNET_JSON_pack_data_auto ("salt", + &acc->salt)); wm->wire_method = TALER_payto_get_method (acc->payto_uri); wm->active = acc->active; GNUNET_CONTAINER_DLL_insert (mi->wm_head, diff --git a/src/backend/taler-merchant-httpd_auditors.c b/src/backend/taler-merchant-httpd_auditors.c index e58115f6..2a20e260 100644 --- a/src/backend/taler-merchant-httpd_auditors.c +++ b/src/backend/taler-merchant-httpd_auditors.c @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2014, 2015, 2016, 2018 Taler Systems SA + (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 @@ -232,13 +232,13 @@ TMH_AUDITORS_init (const struct GNUNET_CONFIGURATION_Handle *cfg) GNUNET_assert (0 == json_array_append_new ( j_auditors, - json_pack ("{s:s, s:o, s:s}", - "name", auditors[cnt].name, - "auditor_pub", - GNUNET_JSON_from_data_auto ( - &auditors[cnt].public_key), - "url", - auditors[cnt].url))); + GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("name", + auditors[cnt].name), + GNUNET_JSON_pack_data_auto ("auditor_pub", + &auditors[cnt].public_key), + GNUNET_JSON_pack_string ("url", + auditors[cnt].url)))); return nauditors; } diff --git a/src/backend/taler-merchant-httpd_config.c b/src/backend/taler-merchant-httpd_config.c index a4d6d0f8..7685619d 100644 --- a/src/backend/taler-merchant-httpd_config.c +++ b/src/backend/taler-merchant-httpd_config.c @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2019, 2020 Taler Systems SA + (C) 2019, 2020, 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 @@ -55,10 +55,13 @@ MH_handler_config (struct TMH_RequestHandler *rh, (void) hc; if (NULL == response) { - response = TALER_MHD_make_json_pack ("{s:s, s:s, s:s}", - "currency", TMH_currency, - "name", "taler-merchant", - "version", MERCHANT_PROTOCOL_VERSION); + response = TALER_MHD_MAKE_JSON_PACK ( + GNUNET_JSON_pack_string ("currency", + TMH_currency), + GNUNET_JSON_pack_string ("name", + "taler-merchant"), + GNUNET_JSON_pack_string ("version", + MERCHANT_PROTOCOL_VERSION)); } return MHD_queue_response (connection, MHD_HTTP_OK, diff --git a/src/backend/taler-merchant-httpd_helper.c b/src/backend/taler-merchant-httpd_helper.c index cc6288f9..a8de49f0 100644 --- a/src/backend/taler-merchant-httpd_helper.c +++ b/src/backend/taler-merchant-httpd_helper.c @@ -158,9 +158,11 @@ TMH_setup_wire_account (const char *payto_uri) &salt, sizeof (salt)); wm = GNUNET_new (struct TMH_WireMethod); - wm->j_wire = json_pack ("{s:s, s:o}", - "payto_uri", payto_uri, - "salt", GNUNET_JSON_from_data_auto (&salt)); + wm->j_wire = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("payto_uri", + payto_uri), + GNUNET_JSON_pack_data_auto ("salt", + &salt)); GNUNET_assert (NULL != wm->j_wire); /* This also tests for things like the IBAN being malformed */ GNUNET_assert (GNUNET_OK == diff --git a/src/backend/taler-merchant-httpd_helper.h b/src/backend/taler-merchant-httpd_helper.h index cc5b3013..dd5da27a 100644 --- a/src/backend/taler-merchant-httpd_helper.h +++ b/src/backend/taler-merchant-httpd_helper.h @@ -87,4 +87,16 @@ TMH_uuid_from_string (const char *uuids, struct GNUNET_Uuid *uuid); +/** + * Put data from an exchange's HTTP response into + * a JSON reply + * + * @param hr a `TALER_EXCHANGE_HttpResponse` + */ +#define TMH_pack_exchange_reply(hr) \ + GNUNET_JSON_pack_uint64 ("exchange_code", hr->ec), \ + GNUNET_JSON_pack_uint64 ("exchange_http_status", hr->http_status), \ + GNUNET_JSON_pack_allow_null ( \ + GNUNET_JSON_pack_object_incref ("exchange_reply", (json_t *) hr->reply)) + #endif diff --git a/src/backend/taler-merchant-httpd_post-orders-ID-pay.c b/src/backend/taler-merchant-httpd_post-orders-ID-pay.c index 7e7642f0..85a1ed6a 100644 --- a/src/backend/taler-merchant-httpd_post-orders-ID-pay.c +++ b/src/backend/taler-merchant-httpd_post-orders-ID-pay.c @@ -30,6 +30,7 @@ #include #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" +#include "taler-merchant-httpd_helper.h" #include "taler-merchant-httpd_post-orders-ID-pay.h" #include "taler-merchant-httpd_private-get-orders.h" @@ -599,18 +600,10 @@ deposit_cb (void *cls, /* internal server error at exchange */ resume_pay_with_response (pc, MHD_HTTP_BAD_GATEWAY, - TALER_MHD_make_json_pack ( - "{s:s, s:I, s:I, s:I}", - "hint", - TALER_ErrorCode_get_hint ( + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS), - "code", - (json_int_t) - TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS, - "exchange_code", - (json_int_t) hr->ec, - "exchange_http_status", - (json_int_t) hr->http_status)); + TMH_pack_exchange_reply (hr))); } else if (NULL == hr->reply) { @@ -618,17 +611,10 @@ deposit_cb (void *cls, resume_pay_with_response ( pc, MHD_HTTP_BAD_GATEWAY, - TALER_MHD_make_json_pack ( - "{s:s, s:I, s:I, s:I}", - "hint", - TALER_ErrorCode_get_hint ( + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( TALER_EC_MERCHANT_GENERIC_EXCHANGE_REPLY_MALFORMED), - "code", - (json_int_t) TALER_EC_MERCHANT_GENERIC_EXCHANGE_REPLY_MALFORMED, - "exchange_code", - (json_int_t) hr->ec, - "exchange_http_status", - (json_int_t) hr->http_status)); + TMH_pack_exchange_reply (hr))); } else { @@ -639,42 +625,24 @@ deposit_cb (void *cls, resume_pay_with_response ( pc, MHD_HTTP_CONFLICT, - TALER_MHD_make_json_pack ("{s:s, s:I, s:I, s:I, s:o, s:O}", - "hint", - TALER_ErrorCode_get_hint ( - TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_INSUFFICIENT_FUNDS), - "code", - (json_int_t) - TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_INSUFFICIENT_FUNDS, - "exchange_code", - (json_int_t) hr->ec, - "exchange_http_status", - (json_int_t) hr->http_status, - "coin_pub", - GNUNET_JSON_from_data_auto (&dc->coin_pub), - "exchange_reply", - hr->reply)); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( + TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_INSUFFICIENT_FUNDS), + TMH_pack_exchange_reply (hr), + GNUNET_JSON_pack_data_auto ("coin_pub", + &dc->coin_pub))); } else { resume_pay_with_response ( pc, MHD_HTTP_BAD_GATEWAY, - TALER_MHD_make_json_pack ("{s:s, s:I, s:I, s:I, s:o, s:O}", - "hint", - TALER_ErrorCode_get_hint ( - TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS), - "code", - (json_int_t) - TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS, - "exchange_code", - (json_int_t) hr->ec, - "exchange_http_status", - (json_int_t) hr->http_status, - "coin_pub", - GNUNET_JSON_from_data_auto (&dc->coin_pub), - "exchange_reply", - hr->reply)); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( + TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS), + TMH_pack_exchange_reply (hr), + GNUNET_JSON_pack_data_auto ("coin_pub", + &dc->coin_pub))); } } return; @@ -763,13 +731,8 @@ process_pay_with_exchange (void *cls, resume_pay_with_response ( pc, MHD_HTTP_GATEWAY_TIMEOUT, - TALER_MHD_make_json_pack ( - "{s:s, s:I}", - "hint", - TALER_ErrorCode_get_hint ( - TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT), - "code", - (json_int_t) TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT)); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec (TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT))); return; } if ( (MHD_HTTP_OK != hr->http_status) || @@ -778,14 +741,10 @@ process_pay_with_exchange (void *cls, resume_pay_with_response ( pc, MHD_HTTP_BAD_GATEWAY, - TALER_MHD_make_json_pack ( - "{s:s, s:I, s:I, s:I, s:O?}", - "hint", TALER_ErrorCode_get_hint ( + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( TALER_EC_MERCHANT_GENERIC_EXCHANGE_CONNECT_FAILURE), - "code", (json_int_t) TALER_EC_MERCHANT_GENERIC_EXCHANGE_CONNECT_FAILURE, - "exchange_http_status", (json_int_t) hr->http_status, - "exchange_code", (json_int_t) hr->ec, - "exchange_reply", hr->reply)); + TMH_pack_exchange_reply (hr))); return; } keys = TALER_EXCHANGE_get_keys (exchange_handle); @@ -839,15 +798,15 @@ process_pay_with_exchange (void *cls, resume_pay_with_response ( pc, MHD_HTTP_BAD_REQUEST, - TALER_MHD_make_json_pack ( - "{s:s, s:I, s:o, s:o?}", - "hint", - TALER_ErrorCode_get_hint ( + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_DENOMINATION_KEY_NOT_FOUND), - "code", - TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_DENOMINATION_KEY_NOT_FOUND, - "h_denom_pub", GNUNET_JSON_from_data_auto (&dc->h_denom), - "exchange_keys", TALER_EXCHANGE_get_keys_raw (exchange_handle))); + GNUNET_JSON_pack_data_auto ("h_denom_pub", + &dc->h_denom), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_object_incref ( + "exchange_keys", + (json_t *) TALER_EXCHANGE_get_keys_raw (exchange_handle))))); return; } if (GNUNET_OK != @@ -873,14 +832,10 @@ process_pay_with_exchange (void *cls, resume_pay_with_response ( pc, http_status, - TALER_MHD_make_json_pack ( - "{s:s, s:I, s:o}", - "hint", - TALER_ErrorCode_get_hint (ec), - "code", - (json_int_t) ec, - "h_denom_pub", - GNUNET_JSON_from_data_auto (&denom_details->h_key))); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec (ec), + GNUNET_JSON_pack_data_auto ("h_denom_pub", + &denom_details->h_key))); return; } @@ -914,11 +869,10 @@ process_pay_with_exchange (void *cls, resume_pay_with_response ( pc, TALER_ErrorCode_get_http_status_safe (ec), - TALER_MHD_make_json_pack ( - "{s:s, s:I, s:i}", - "hint", TALER_ErrorCode_get_hint (ec), - "code", (json_int_t) ec, - "coin_idx", i)); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec (ec), + GNUNET_JSON_pack_uint64 ("coin_idx", + i))); return; } if (TMH_force_audit) @@ -1552,18 +1506,12 @@ execute_pay_transaction (struct PayContext *pc) } /* Build the response */ - { - json_t *resp; - - resp = json_pack ("{s:o}", - "sig", - GNUNET_JSON_from_data_auto (&sig)); - GNUNET_assert (NULL != resp); - resume_pay_with_response (pc, - MHD_HTTP_OK, - TALER_MHD_make_json (resp)); - json_decref (resp); - } + resume_pay_with_response ( + pc, + MHD_HTTP_OK, + TALER_MHD_MAKE_JSON_PACK ( + GNUNET_JSON_pack_data_auto ("sig", + &sig))); } } diff --git a/src/backend/taler-merchant-httpd_private-patch-instances-ID.c b/src/backend/taler-merchant-httpd_private-patch-instances-ID.c index 482ef048..607c3593 100644 --- a/src/backend/taler-merchant-httpd_private-patch-instances-ID.c +++ b/src/backend/taler-merchant-httpd_private-patch-instances-ID.c @@ -93,6 +93,9 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, bool committed = false; GNUNET_assert (NULL != mi); + memset (&is, + 0, + sizeof (is)); { enum GNUNET_GenericReturnValue res; @@ -177,8 +180,11 @@ patch_instances_ID (struct TMH_MerchantInstance *mi, (mi->settings.default_pay_delay.rel_value_us == is.default_pay_delay.rel_value_us) ) ) { + is.id = mi->settings.id; + is.name = GNUNET_strdup (name); qs = TMH_db->update_instance (TMH_db->cls, - &mi->settings); + &is); + GNUNET_free (is.name); if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) { TMH_db->rollback (TMH_db->cls); diff --git a/src/lib/merchant_api_patch_instance.c b/src/lib/merchant_api_patch_instance.c index 871dbbba..74256330 100644 --- a/src/lib/merchant_api_patch_instance.c +++ b/src/lib/merchant_api_patch_instance.c @@ -105,10 +105,6 @@ handle_patch_instance_finished (void *cls, case MHD_HTTP_BAD_REQUEST: hr.ec = TALER_JSON_get_error_code (json); hr.hint = TALER_JSON_get_error_hint (json); - GNUNET_break_op (0); - /* This should never happen, either us - * or the merchant is buggy (or API version conflict); - * just pass JSON reply to the application */ break; case MHD_HTTP_UNAUTHORIZED: hr.ec = TALER_JSON_get_error_code (json); diff --git a/src/testing/testing_api_cmd_get_instance.c b/src/testing/testing_api_cmd_get_instance.c index 6622b2ec..0bd736f1 100644 --- a/src/testing/testing_api_cmd_get_instance.c +++ b/src/testing/testing_api_cmd_get_instance.c @@ -132,6 +132,7 @@ get_instance_cb (void *cls, case MHD_HTTP_OK: { const char *name; + if (GNUNET_OK != TALER_TESTING_get_trait_string (instance_cmd, 0, @@ -141,7 +142,9 @@ get_instance_cb (void *cls, name)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Instance name does not match\n"); + "Instance name does not match: Got `%s', wanted `%s'\n", + details->name, + name); TALER_TESTING_interpreter_fail (gis->is); return; } diff --git a/src/testing/testing_api_cmd_patch_instance.c b/src/testing/testing_api_cmd_patch_instance.c index 3d4b93d1..44ae355e 100644 --- a/src/testing/testing_api_cmd_patch_instance.c +++ b/src/testing/testing_api_cmd_patch_instance.c @@ -139,6 +139,9 @@ patch_instance_cb (void *cls, { case MHD_HTTP_NO_CONTENT: break; + case MHD_HTTP_BAD_REQUEST: + /* happens also for currency mismatch */ + break; case MHD_HTTP_UNAUTHORIZED: break; case MHD_HTTP_NOT_FOUND: -- cgit v1.2.3