From 0e2148d63b85025c37d4212162a522e47445acab Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 4 Jun 2023 12:28:55 +0200 Subject: -code cleanup --- src/lib/merchant_api_get_instance.c | 36 +++++++-------- src/lib/merchant_api_get_instances.c | 77 +++++++++---------------------- src/lib/merchant_api_get_kyc.c | 31 ++++++------- src/lib/merchant_api_get_orders.c | 29 +++++------- src/lib/merchant_api_get_products.c | 32 +++++-------- src/lib/merchant_api_get_template.c | 8 ++-- src/lib/merchant_api_merchant_get_order.c | 46 ++++++------------ src/lib/merchant_api_post_order_abort.c | 16 ++----- src/lib/merchant_api_post_order_pay.c | 8 ++-- 9 files changed, 99 insertions(+), 184 deletions(-) (limited to 'src/lib') diff --git a/src/lib/merchant_api_get_instance.c b/src/lib/merchant_api_get_instance.c index 6224e1b4..cf58f828 100644 --- a/src/lib/merchant_api_get_instance.c +++ b/src/lib/merchant_api_get_instance.c @@ -93,13 +93,13 @@ handle_get_instance_finished (void *cls, { case MHD_HTTP_OK: { - json_t *accounts; + const json_t *accounts; const char *uts; - json_t *address; - json_t *jurisdiction; + const json_t *address; + const json_t *jurisdiction; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("accounts", - &accounts), + GNUNET_JSON_spec_array_const ("accounts", + &accounts), GNUNET_JSON_spec_string ("name", &igr.details.ok.details.name), GNUNET_JSON_spec_string ("user_type", @@ -107,10 +107,10 @@ handle_get_instance_finished (void *cls, GNUNET_JSON_spec_fixed_auto ( "merchant_pub", &igr.details.ok.details.merchant_pub), - GNUNET_JSON_spec_json ("address", - &address), - GNUNET_JSON_spec_json ("jurisdiction", - &jurisdiction), + GNUNET_JSON_spec_object_const ("address", + &address), + GNUNET_JSON_spec_object_const ("jurisdiction", + &jurisdiction), TALER_JSON_spec_amount_any ( "default_max_wire_fee", &igr.details.ok.details.default_max_wire_fee), @@ -129,14 +129,13 @@ handle_get_instance_finished (void *cls, GNUNET_JSON_spec_end () }; - if ( (GNUNET_OK == - GNUNET_JSON_parse (json, - spec, - NULL, NULL)) && - (json_is_array (accounts)) ) + if (GNUNET_OK == + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { unsigned int accounts_length = json_array_size (accounts); - struct TALER_MERCHANT_Account aa[accounts_length]; + struct TALER_MERCHANT_Account aa[GNUNET_NZL (accounts_length)]; size_t index; json_t *value; int ret = GNUNET_OK; @@ -146,7 +145,7 @@ handle_get_instance_finished (void *cls, sizeof (aa)); json_array_foreach (accounts, index, value) { - struct GNUNET_JSON_Specification spec[] = { + struct GNUNET_JSON_Specification ispec[] = { GNUNET_JSON_spec_fixed_auto ("salt", &aa[index].salt), GNUNET_JSON_spec_mark_optional ( @@ -164,7 +163,7 @@ handle_get_instance_finished (void *cls, if (GNUNET_OK != GNUNET_JSON_parse (value, - spec, + ispec, NULL, NULL)) { GNUNET_break_op (0); @@ -188,14 +187,12 @@ handle_get_instance_finished (void *cls, ret = GNUNET_SYSERR; igr.hr.http_status = 0; igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; - GNUNET_JSON_parse_free (spec); break; } igr.details.ok.accounts_length = accounts_length; igr.details.ok.accounts = aa; igh->cb (igh->cb_cls, &igr); - GNUNET_JSON_parse_free (spec); TALER_MERCHANT_instance_get_cancel (igh); return; } @@ -203,7 +200,6 @@ handle_get_instance_finished (void *cls, GNUNET_break_op (0); igr.hr.http_status = 0; igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; - GNUNET_JSON_parse_free (spec); break; } case MHD_HTTP_UNAUTHORIZED: diff --git a/src/lib/merchant_api_get_instances.c b/src/lib/merchant_api_get_instances.c index 5f26eb48..c0553941 100644 --- a/src/lib/merchant_api_get_instances.c +++ b/src/lib/merchant_api_get_instances.c @@ -81,7 +81,6 @@ parse_instances (const json_t *json, struct TALER_MERCHANT_InstanceInformation iis[GNUNET_NZL (iis_len)]; size_t index; json_t *value; - enum GNUNET_GenericReturnValue ret; struct TALER_MERCHANT_InstancesGetResponse igr = { .hr.http_status = MHD_HTTP_OK, .hr.reply = json, @@ -89,7 +88,6 @@ parse_instances (const json_t *json, .details.ok.iis = iis }; - ret = GNUNET_OK; json_array_foreach (ia, index, value) { struct TALER_MERCHANT_InstanceInformation *ii = &iis[index]; const char *uts; @@ -102,8 +100,8 @@ parse_instances (const json_t *json, &ii->id), GNUNET_JSON_spec_fixed_auto ("merchant_pub", &ii->merchant_pub), - GNUNET_JSON_spec_json ("payment_targets", - &ii->payment_targets), + GNUNET_JSON_spec_array_const ("payment_targets", + &ii->payment_targets), GNUNET_JSON_spec_end () }; @@ -113,24 +111,14 @@ parse_instances (const json_t *json, NULL, NULL)) { GNUNET_break_op (0); - ret = GNUNET_SYSERR; - continue; + return GNUNET_SYSERR; } if (GNUNET_OK != TALER_KYCLOGIC_kyc_user_type_from_string (uts, &ii->ut)) { GNUNET_break_op (0); - ret = GNUNET_SYSERR; - GNUNET_JSON_parse_free (spec); - break; - } - if (! json_is_array (ii->payment_targets)) - { - GNUNET_break_op (0); - ret = GNUNET_SYSERR; - GNUNET_JSON_parse_free (spec); - break; + return GNUNET_SYSERR; } for (unsigned int i = 0; ipayment_targets); i++) { @@ -138,27 +126,14 @@ parse_instances (const json_t *json, i))) { GNUNET_break_op (0); - ret = GNUNET_SYSERR; - GNUNET_JSON_parse_free (spec); - break; + return GNUNET_SYSERR; } } - if (GNUNET_SYSERR == ret) - { - GNUNET_JSON_parse_free (spec); - break; - } - } - if (GNUNET_OK == ret) - { - igh->cb (igh->cb_cls, - &igr); - igh->cb = NULL; /* just to be sure */ - } - for (unsigned int i = 0; icb (igh->cb_cls, + &igr); + igh->cb = NULL; /* just to be sure */ + return GNUNET_OK; } @@ -190,10 +165,10 @@ handle_instances_finished (void *cls, { case MHD_HTTP_OK: { - json_t *instances; + const json_t *instances; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("instances", - &instances), + GNUNET_JSON_spec_array_const ("instances", + &instances), GNUNET_JSON_spec_end () }; @@ -204,26 +179,18 @@ handle_instances_finished (void *cls, { igr.hr.http_status = 0; igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; } - else + if (GNUNET_OK == + parse_instances (json, + instances, + igh)) { - if ( (! json_is_array (instances)) || - (GNUNET_OK == - parse_instances (json, - instances, - igh)) ) - { - GNUNET_JSON_parse_free (spec); - TALER_MERCHANT_instances_get_cancel (igh); - return; - } - else - { - igr.hr.http_status = 0; - igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; - } + TALER_MERCHANT_instances_get_cancel (igh); + return; } - GNUNET_JSON_parse_free (spec); + igr.hr.http_status = 0; + igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; } case MHD_HTTP_UNAUTHORIZED: diff --git a/src/lib/merchant_api_get_kyc.c b/src/lib/merchant_api_get_kyc.c index 0b62d2cf..c394590a 100644 --- a/src/lib/merchant_api_get_kyc.c +++ b/src/lib/merchant_api_get_kyc.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2021 Taler Systems SA + Copyright (C) 2023 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software @@ -76,8 +76,8 @@ struct TALER_MERCHANT_KycGetHandle static enum GNUNET_GenericReturnValue parse_kyc (struct TALER_MERCHANT_KycGetHandle *kyc, struct TALER_MERCHANT_KycResponse *kr, - json_t *pends, - json_t *touts) + const json_t *pends, + const json_t *touts) { unsigned int num_pends = json_array_size (pends); unsigned int num_touts = json_array_size (touts); @@ -186,13 +186,13 @@ handle_get_kyc_finished (void *cls, case MHD_HTTP_BAD_GATEWAY: case MHD_HTTP_GATEWAY_TIMEOUT: { - json_t *pends; - json_t *touts; + const json_t *pends; + const json_t *touts; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("pending_kycs", - &pends), - GNUNET_JSON_spec_json ("timeout_kycs", - &touts), + GNUNET_JSON_spec_array_const ("pending_kycs", + &pends), + GNUNET_JSON_spec_array_const ("timeout_kycs", + &touts), GNUNET_JSON_spec_end () }; @@ -205,20 +205,17 @@ handle_get_kyc_finished (void *cls, kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; } - if ( (! json_is_array (pends)) || - (! json_is_array (touts)) || - (GNUNET_OK != - parse_kyc (kyc, - &kr, - pends, - touts)) ) + if (GNUNET_OK != + parse_kyc (kyc, + &kr, + pends, + touts)) { kr.hr.http_status = 0; kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; } /* parse_kyc called the continuation already */ - GNUNET_JSON_parse_free (spec); TALER_MERCHANT_kyc_get_cancel (kyc); return; } diff --git a/src/lib/merchant_api_get_orders.c b/src/lib/merchant_api_get_orders.c index 5bad47e0..966ecb15 100644 --- a/src/lib/merchant_api_get_orders.c +++ b/src/lib/merchant_api_get_orders.c @@ -148,10 +148,10 @@ handle_get_orders_finished (void *cls, { case MHD_HTTP_OK: { - json_t *orders; + const json_t *orders; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("orders", - &orders), + GNUNET_JSON_spec_array_const ("orders", + &orders), GNUNET_JSON_spec_end () }; @@ -162,23 +162,18 @@ handle_get_orders_finished (void *cls, { ogr.hr.http_status = 0; ogr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; } - else + if (GNUNET_OK == + parse_orders (orders, + &ogr, + ogh)) { - if ( (! json_is_array (orders)) || - (GNUNET_OK == - parse_orders (orders, - &ogr, - ogh)) ) - { - GNUNET_JSON_parse_free (spec); - TALER_MERCHANT_orders_get_cancel (ogh); - return; - } - ogr.hr.http_status = 0; - ogr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + TALER_MERCHANT_orders_get_cancel (ogh); + return; } - GNUNET_JSON_parse_free (spec); + ogr.hr.http_status = 0; + ogr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; } case MHD_HTTP_UNAUTHORIZED: diff --git a/src/lib/merchant_api_get_products.c b/src/lib/merchant_api_get_products.c index 98976237..01115094 100644 --- a/src/lib/merchant_api_get_products.c +++ b/src/lib/merchant_api_get_products.c @@ -149,10 +149,10 @@ handle_get_products_finished (void *cls, { case MHD_HTTP_OK: { - json_t *products; + const json_t *products; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("products", - &products), + GNUNET_JSON_spec_array_const ("products", + &products), GNUNET_JSON_spec_end () }; @@ -163,26 +163,18 @@ handle_get_products_finished (void *cls, { gpr.hr.http_status = 0; gpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; } - else + if (GNUNET_OK == + parse_products (json, + products, + pgh)) { - if ( (! json_is_array (products)) || - (GNUNET_OK == - parse_products (json, - products, - pgh)) ) - { - GNUNET_JSON_parse_free (spec); - TALER_MERCHANT_products_get_cancel (pgh); - return; - } - else - { - gpr.hr.http_status = 0; - gpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; - } + TALER_MERCHANT_products_get_cancel (pgh); + return; } - GNUNET_JSON_parse_free (spec); + gpr.hr.http_status = 0; + gpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; } case MHD_HTTP_UNAUTHORIZED: diff --git a/src/lib/merchant_api_get_template.c b/src/lib/merchant_api_get_template.c index 386026cf..3e4a23a8 100644 --- a/src/lib/merchant_api_get_template.c +++ b/src/lib/merchant_api_get_template.c @@ -93,7 +93,7 @@ handle_get_template_finished (void *cls, case MHD_HTTP_OK: { uint32_t alg32; - json_t *contract; + const json_t *contract; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_string ("template_description", &tgr.details.ok.template_description), @@ -103,8 +103,8 @@ handle_get_template_finished (void *cls, GNUNET_JSON_spec_string ("pos_key", &tgr.details.ok.pos_key), NULL), - GNUNET_JSON_spec_json ("template_contract", - &contract), + GNUNET_JSON_spec_object_const ("template_contract", + &contract), GNUNET_JSON_spec_end () }; @@ -118,13 +118,11 @@ handle_get_template_finished (void *cls, tgr.details.ok.template_contract = contract; tgh->cb (tgh->cb_cls, &tgr); - GNUNET_JSON_parse_free (spec); TALER_MERCHANT_template_get_cancel (tgh); return; } tgr.hr.http_status = 0; tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; - GNUNET_JSON_parse_free (spec); break; } case MHD_HTTP_UNAUTHORIZED: diff --git a/src/lib/merchant_api_merchant_get_order.c b/src/lib/merchant_api_merchant_get_order.c index a0c1995b..a8a50738 100644 --- a/src/lib/merchant_api_merchant_get_order.c +++ b/src/lib/merchant_api_merchant_get_order.c @@ -130,9 +130,9 @@ handle_claimed (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, struct TALER_MERCHANT_OrderStatusResponse *osr) { struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("contract_terms", - (json_t **) &osr->details.ok.details.claimed. - contract_terms), + GNUNET_JSON_spec_object_const ( + "contract_terms", + &osr->details.ok.details.claimed.contract_terms), GNUNET_JSON_spec_end () }; @@ -151,7 +151,6 @@ handle_claimed (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, osr->details.ok.status = TALER_MERCHANT_OSC_CLAIMED; omgh->cb (omgh->cb_cls, osr); - GNUNET_JSON_parse_free (spec); } @@ -169,9 +168,9 @@ handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, { uint32_t ec32; uint32_t hc32; - json_t *wire_details; - json_t *wire_reports; - json_t *refund_details; + const json_t *wire_details; + const json_t *wire_reports; + const json_t *refund_details; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_bool ("refunded", &osr->details.ok.details.paid.refunded), @@ -187,15 +186,15 @@ handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, &hc32), TALER_JSON_spec_amount_any ("refund_amount", &osr->details.ok.details.paid.refund_amount), - GNUNET_JSON_spec_json ( + GNUNET_JSON_spec_object_const ( "contract_terms", - (json_t **) &osr->details.ok.details.paid.contract_terms), - GNUNET_JSON_spec_json ("wire_details", - &wire_details), - GNUNET_JSON_spec_json ("wire_reports", - &wire_reports), - GNUNET_JSON_spec_json ("refund_details", - &refund_details), + &osr->details.ok.details.paid.contract_terms), + GNUNET_JSON_spec_array_const ("wire_details", + &wire_details), + GNUNET_JSON_spec_array_const ("wire_reports", + &wire_reports), + GNUNET_JSON_spec_array_const ("refund_details", + &refund_details), GNUNET_JSON_spec_end () }; @@ -211,19 +210,6 @@ handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, osr); return; } - if (! (json_is_array (wire_details) && - json_is_array (wire_reports) && - json_is_array (refund_details) && - json_is_object (osr->details.ok.details.paid.contract_terms)) ) - { - GNUNET_break_op (0); - osr->hr.http_status = 0; - osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; - omgh->cb (omgh->cb_cls, - osr); - GNUNET_JSON_parse_free (spec); - return; - } osr->details.ok.status = TALER_MERCHANT_OSC_PAID; osr->details.ok.details.paid.exchange_ec = (enum TALER_ErrorCode) ec32; osr->details.ok.details.paid.exchange_hc = (unsigned int) hc32; @@ -264,7 +250,6 @@ handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; omgh->cb (omgh->cb_cls, osr); - GNUNET_JSON_parse_free (spec); return; } } @@ -300,7 +285,6 @@ handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; omgh->cb (omgh->cb_cls, osr); - GNUNET_JSON_parse_free (spec); return; } wr->code = (enum TALER_ErrorCode) c32; @@ -333,7 +317,6 @@ handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; omgh->cb (omgh->cb_cls, osr); - GNUNET_JSON_parse_free (spec); return; } } @@ -347,7 +330,6 @@ handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh, omgh->cb (omgh->cb_cls, osr); } - GNUNET_JSON_parse_free (spec); } diff --git a/src/lib/merchant_api_post_order_abort.c b/src/lib/merchant_api_post_order_abort.c index a924119b..3c9665c6 100644 --- a/src/lib/merchant_api_post_order_abort.c +++ b/src/lib/merchant_api_post_order_abort.c @@ -111,11 +111,11 @@ check_abort_refund (struct TALER_MERCHANT_OrderAbortHandle *oah, struct TALER_MERCHANT_AbortResponse *ar, const json_t *json) { - json_t *refunds; + const json_t *refunds; unsigned int num_refunds; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("refunds", - &refunds), + GNUNET_JSON_spec_array_const ("refunds", + &refunds), GNUNET_JSON_spec_end () }; @@ -127,12 +127,6 @@ check_abort_refund (struct TALER_MERCHANT_OrderAbortHandle *oah, GNUNET_break_op (0); return GNUNET_SYSERR; } - if (! json_is_array (refunds)) - { - GNUNET_break_op (0); - GNUNET_JSON_parse_free (spec); - return GNUNET_SYSERR; - } num_refunds = json_array_size (refunds); { struct TALER_MERCHANT_AbortedCoin res[GNUNET_NZL (num_refunds)]; @@ -153,7 +147,6 @@ check_abort_refund (struct TALER_MERCHANT_OrderAbortHandle *oah, NULL, NULL)) { GNUNET_break_op (0); - GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } if (MHD_HTTP_OK == exchange_status) @@ -172,7 +165,6 @@ check_abort_refund (struct TALER_MERCHANT_OrderAbortHandle *oah, NULL, NULL)) { GNUNET_break_op (0); - GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } @@ -187,7 +179,6 @@ check_abort_refund (struct TALER_MERCHANT_OrderAbortHandle *oah, &res[i].exchange_sig)) { GNUNET_break_op (0); - GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } } @@ -199,7 +190,6 @@ check_abort_refund (struct TALER_MERCHANT_OrderAbortHandle *oah, ar); oah->abort_cb = NULL; } - GNUNET_JSON_parse_free (spec); return GNUNET_OK; } diff --git a/src/lib/merchant_api_post_order_pay.c b/src/lib/merchant_api_post_order_pay.c index 6cfeb846..4b674337 100644 --- a/src/lib/merchant_api_post_order_pay.c +++ b/src/lib/merchant_api_post_order_pay.c @@ -281,11 +281,11 @@ parse_conflict (struct TALER_MERCHANT_OrderPayHandle *oph, struct TALER_MERCHANT_PayResponse *pr, const json_t *json) { - json_t *ereply; + const json_t *ereply; const char *exchange_url; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("exchange_reply", - &ereply), + GNUNET_JSON_spec_object_const ("exchange_reply", + &ereply), GNUNET_JSON_spec_string ("exchange_url", &exchange_url), GNUNET_JSON_spec_end () @@ -339,10 +339,8 @@ parse_conflict (struct TALER_MERCHANT_OrderPayHandle *oph, NULL, NULL)) { GNUNET_break_op (0); - GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } - GNUNET_JSON_parse_free (spec); for (unsigned int i = 0; inum_coins; i++) { -- cgit v1.2.3