From cf6ba0c77196b77f5c54f0533ac1be3d1d5ee6d6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 23 Nov 2019 21:03:05 +0100 Subject: [PATCH] more refactoring for libtalermhd --- src/exchange/taler-exchange-httpd_deposit.c | 14 +- src/exchange/taler-exchange-httpd_keystate.h | 1 + src/exchange/taler-exchange-httpd_mhd.c | 12 +- src/exchange/taler-exchange-httpd_test.c | 134 ++++++++++-------- .../taler-exchange-httpd_track_transaction.c | 73 +++++----- .../taler-exchange-httpd_track_transfer.c | 75 +++++----- src/exchange/taler-exchange-httpd_wire.c | 7 +- 7 files changed, 174 insertions(+), 142 deletions(-) diff --git a/src/exchange/taler-exchange-httpd_deposit.c b/src/exchange/taler-exchange-httpd_deposit.c index 7e71dcaa6..1db8d7621 100644 --- a/src/exchange/taler-exchange-httpd_deposit.c +++ b/src/exchange/taler-exchange-httpd_deposit.c @@ -291,9 +291,10 @@ verify_and_execute_deposit (struct MHD_Connection *connection, if (NULL == mks) { TALER_LOG_ERROR ("Lacking keys to operate\n"); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_EXCHANGE_BAD_CONFIGURATION, - "no keys"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_EXCHANGE_BAD_CONFIGURATION, + "no keys"); } dki = TEH_KS_denomination_key_lookup_by_hash (mks, &deposit->coin.denom_pub_hash, @@ -465,9 +466,10 @@ TEH_DEPOSIT_handler_deposit (struct TEH_RequestHandler *rh, &emsg))) { GNUNET_JSON_parse_free (spec); - res = TEH_RESPONSE_reply_external_error (connection, - ec, - emsg); + res = TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + ec, + emsg); GNUNET_free (emsg); return res; } diff --git a/src/exchange/taler-exchange-httpd_keystate.h b/src/exchange/taler-exchange-httpd_keystate.h index 708b043b5..5d92af2b2 100644 --- a/src/exchange/taler-exchange-httpd_keystate.h +++ b/src/exchange/taler-exchange-httpd_keystate.h @@ -26,6 +26,7 @@ #include #include #include "taler-exchange-httpd.h" +#include "taler_error_codes.h" #include "taler_exchangedb_lib.h" diff --git a/src/exchange/taler-exchange-httpd_mhd.c b/src/exchange/taler-exchange-httpd_mhd.c index 36d10bae3..825a1b310 100644 --- a/src/exchange/taler-exchange-httpd_mhd.c +++ b/src/exchange/taler-exchange-httpd_mhd.c @@ -67,7 +67,7 @@ TEH_MHD_handler_static_response (struct TEH_RequestHandler *rh, GNUNET_break (0); return MHD_NO; } - TEH_RESPONSE_add_global_headers (response); + TALER_MHD_add_global_headers (response); if (NULL != rh->mime_type) (void) MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, @@ -128,11 +128,11 @@ TEH_MHD_handler_send_json_pack_error (struct TEH_RequestHandler *rh, (void) connection_cls; (void) upload_data; (void) upload_data_size; - return TEH_RESPONSE_reply_json_pack (connection, - rh->response_code, - "{s:s}", - "error", - rh->data); + return TALER_MHD_reply_json_pack (connection, + rh->response_code, + "{s:s}", + "error", + rh->data); } diff --git a/src/exchange/taler-exchange-httpd_test.c b/src/exchange/taler-exchange-httpd_test.c index 0a64f267f..4d0e7d691 100644 --- a/src/exchange/taler-exchange-httpd_test.c +++ b/src/exchange/taler-exchange-httpd_test.c @@ -24,6 +24,7 @@ #include #include #include +#include "taler_mhd_lib.h" #include "taler_signatures.h" #include "taler-exchange-httpd_test.h" #include "taler-exchange-httpd_parsing.h" @@ -88,11 +89,11 @@ TEH_TEST_handler_test_base32 (struct TEH_RequestHandler *rh, &hc); GNUNET_JSON_parse_free (spec); json_decref (json); - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "output", - GNUNET_JSON_from_data_auto (&hc)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "output", + GNUNET_JSON_from_data_auto (&hc)); } @@ -172,11 +173,11 @@ TEH_TEST_handler_test_encrypt (struct TEH_RequestHandler *rh, in_ptr_size); GNUNET_free (out); GNUNET_JSON_parse_free (spec); - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "output", - json); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "output", + json); } @@ -237,11 +238,11 @@ TEH_TEST_handler_test_hkdf (struct TEH_RequestHandler *rh, NULL, 0)); GNUNET_JSON_parse_free (spec); json = GNUNET_JSON_from_data_auto (&hc); - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "output", - json); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "output", + json); } @@ -298,16 +299,17 @@ TEH_TEST_handler_test_ecdhe (struct TEH_RequestHandler *rh, &hc)) { GNUNET_JSON_parse_free (spec); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_TEST_ECDH_ERROR, - "Failed to perform ECDH"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TEST_ECDH_ERROR, + "Failed to perform ECDH"); } GNUNET_JSON_parse_free (spec); - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "ecdh_hash", - GNUNET_JSON_from_data_auto (&hc)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "ecdh_hash", + GNUNET_JSON_from_data_auto (&hc)); } @@ -369,9 +371,10 @@ TEH_TEST_handler_test_eddsa (struct TEH_RequestHandler *rh, &pub)) { GNUNET_JSON_parse_free (spec); - return TEH_RESPONSE_reply_signature_invalid (connection, - TALER_EC_TEST_EDDSA_INVALID, - "eddsa_sig"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_FORBIDDEN, + TALER_EC_TEST_EDDSA_INVALID, + "eddsa_sig"); } GNUNET_JSON_parse_free (spec); pk = GNUNET_CRYPTO_eddsa_key_create (); @@ -382,20 +385,21 @@ TEH_TEST_handler_test_eddsa (struct TEH_RequestHandler *rh, &sig)) { GNUNET_free (pk); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_TEST_EDDSA_ERROR, - "Failed to EdDSA-sign"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TEST_EDDSA_ERROR, + "Failed to EdDSA-sign"); } GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub); GNUNET_free (pk); - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:o}", - "eddsa_pub", - GNUNET_JSON_from_data_auto (&pub), - "eddsa_sig", - GNUNET_JSON_from_data_auto (&sig)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:o}", + "eddsa_pub", + GNUNET_JSON_from_data_auto (&pub), + "eddsa_sig", + GNUNET_JSON_from_data_auto (&sig)); } @@ -425,23 +429,25 @@ TEH_TEST_handler_test_rsa_get (struct TEH_RequestHandler *rh, if (NULL == rsa_pk) { GNUNET_break (0); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_TEST_RSA_GEN_ERROR, - "Failed to create RSA key"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TEST_RSA_GEN_ERROR, + "Failed to create RSA key"); } pub = GNUNET_CRYPTO_rsa_private_key_get_public (rsa_pk); if (NULL == pub) { GNUNET_break (0); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_TEST_RSA_PUB_ERROR, - "Failed to get public RSA key"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TEST_RSA_PUB_ERROR, + "Failed to get public RSA key"); } - res = TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "rsa_pub", - GNUNET_JSON_from_rsa_public_key (pub)); + res = TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "rsa_pub", + GNUNET_JSON_from_rsa_public_key (pub)); GNUNET_CRYPTO_rsa_public_key_free (pub); return res; } @@ -497,9 +503,10 @@ TEH_TEST_handler_test_rsa_sign (struct TEH_RequestHandler *rh, { GNUNET_break (0); GNUNET_JSON_parse_free (spec); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_TEST_RSA_GEN_ERROR, - "Failed to create RSA key"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TEST_RSA_GEN_ERROR, + "Failed to create RSA key"); } sig = GNUNET_CRYPTO_rsa_sign_blinded (rsa_pk, in_ptr, @@ -508,16 +515,17 @@ TEH_TEST_handler_test_rsa_sign (struct TEH_RequestHandler *rh, { GNUNET_break (0); GNUNET_JSON_parse_free (spec); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_TEST_RSA_SIGN_ERROR, - "Failed to RSA-sign"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TEST_RSA_SIGN_ERROR, + "Failed to RSA-sign"); } GNUNET_JSON_parse_free (spec); - res = TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "rsa_blind_sig", - GNUNET_JSON_from_rsa_signature (sig)); + res = TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "rsa_blind_sig", + GNUNET_JSON_from_rsa_signature (sig)); GNUNET_CRYPTO_rsa_signature_free (sig); return res; } @@ -573,11 +581,11 @@ TEH_TEST_handler_test_transfer (struct TEH_RequestHandler *rh, TALER_link_reveal_transfer_secret (&trans_priv, &coin_pub, &secret); - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "secret", - GNUNET_JSON_from_data_auto (&secret)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "secret", + GNUNET_JSON_from_data_auto (&secret)); } diff --git a/src/exchange/taler-exchange-httpd_track_transaction.c b/src/exchange/taler-exchange-httpd_track_transaction.c index 3b8d14a01..5a057d665 100644 --- a/src/exchange/taler-exchange-httpd_track_transaction.c +++ b/src/exchange/taler-exchange-httpd_track_transaction.c @@ -23,6 +23,7 @@ #include #include #include +#include "taler_mhd_lib.h" #include "taler_signatures.h" #include "taler-exchange-httpd_parsing.h" #include "taler-exchange-httpd_keystate.h" @@ -42,12 +43,12 @@ static int reply_transfer_pending (struct MHD_Connection *connection, struct GNUNET_TIME_Absolute planned_exec_time) { - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_ACCEPTED, - "{s:o}", - "execution_time", - GNUNET_JSON_from_time_abs ( - planned_exec_time)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_ACCEPTED, + "{s:o}", + "execution_time", + GNUNET_JSON_from_time_abs ( + planned_exec_time)); } @@ -92,24 +93,25 @@ reply_track_transaction (struct MHD_Connection *connection, &pub, &sig)) { - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_EXCHANGE_BAD_CONFIGURATION, - "no keys"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_EXCHANGE_BAD_CONFIGURATION, + "no keys"); } - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:o, s:o, s:o, s:o}", - "wtid", GNUNET_JSON_from_data_auto ( - wtid), - "execution_time", - GNUNET_JSON_from_time_abs (exec_time), - "coin_contribution", - TALER_JSON_from_amount ( - coin_contribution), - "exchange_sig", - GNUNET_JSON_from_data_auto (&sig), - "exchange_pub", - GNUNET_JSON_from_data_auto (&pub)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:o, s:o, s:o, s:o}", + "wtid", GNUNET_JSON_from_data_auto ( + wtid), + "execution_time", + GNUNET_JSON_from_time_abs (exec_time), + "coin_contribution", + TALER_JSON_from_amount ( + coin_contribution), + "exchange_sig", + GNUNET_JSON_from_data_auto (&sig), + "exchange_pub", + GNUNET_JSON_from_data_auto (&pub)); } @@ -249,15 +251,19 @@ track_transaction_transaction (void *cls, if (GNUNET_DB_STATUS_HARD_ERROR == qs) { GNUNET_break (0); - *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection, - TALER_EC_TRACK_TRANSACTION_DB_FETCH_FAILED); + *mhd_ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSACTION_DB_FETCH_FAILED, + "failed to fetch transaction data"); } return qs; } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { - *mhd_ret = TEH_RESPONSE_reply_transaction_unknown (connection, - TALER_EC_TRACK_TRANSACTION_NOT_FOUND); + *mhd_ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_TRACK_TRANSACTION_NOT_FOUND, + "transaction unknown"); return GNUNET_DB_STATUS_HARD_ERROR; } return qs; @@ -295,9 +301,10 @@ check_and_handle_track_transaction_request (struct MHD_Connection *connection, &merchant_pub->eddsa_pub)) { GNUNET_break_op (0); - return TEH_RESPONSE_reply_signature_invalid (connection, - TALER_EC_TRACK_TRANSACTION_MERCHANT_SIGNATURE_INVALID, - "merchant_sig"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_FORBIDDEN, + TALER_EC_TRACK_TRANSACTION_MERCHANT_SIGNATURE_INVALID, + "merchant_sig"); } ctx.pending = GNUNET_NO; ctx.tps = tps; @@ -314,8 +321,10 @@ check_and_handle_track_transaction_request (struct MHD_Connection *connection, return reply_transfer_pending (connection, ctx.execution_time); if (GNUNET_SYSERR == ctx.pending) - return TEH_RESPONSE_reply_internal_db_error (connection, - TALER_EC_TRACK_TRANSACTION_DB_FEE_INCONSISTENT); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSACTION_DB_FEE_INCONSISTENT, + "fees are inconsistent"); return reply_track_transaction (connection, &tps->h_contract_terms, &tps->h_wire, diff --git a/src/exchange/taler-exchange-httpd_track_transfer.c b/src/exchange/taler-exchange-httpd_track_transfer.c index cacbfaa85..83521f736 100644 --- a/src/exchange/taler-exchange-httpd_track_transfer.c +++ b/src/exchange/taler-exchange-httpd_track_transfer.c @@ -28,6 +28,7 @@ #include "taler-exchange-httpd_keystate.h" #include "taler-exchange-httpd_track_transfer.h" #include "taler-exchange-httpd_responses.h" +#include "taler_mhd_lib.h" #include "taler_wire_lib.h" @@ -148,29 +149,30 @@ reply_track_transfer_details (struct MHD_Connection *connection, &sig)) { json_decref (deposits); - return TEH_RESPONSE_reply_internal_error (connection, - TALER_EC_EXCHANGE_BAD_CONFIGURATION, - "no keys"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_EXCHANGE_BAD_CONFIGURATION, + "no keys"); } - return TEH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o}", - "total", TALER_JSON_from_amount (total), - "wire_fee", TALER_JSON_from_amount ( - wire_fee), - "merchant_pub", - GNUNET_JSON_from_data_auto ( - merchant_pub), - "H_wire", GNUNET_JSON_from_data_auto ( - h_wire), - "execution_time", - GNUNET_JSON_from_time_abs (exec_time), - "deposits", deposits, - "exchange_sig", - GNUNET_JSON_from_data_auto (&sig), - "exchange_pub", - GNUNET_JSON_from_data_auto (&pub)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o}", + "total", TALER_JSON_from_amount (total), + "wire_fee", TALER_JSON_from_amount ( + wire_fee), + "merchant_pub", + GNUNET_JSON_from_data_auto ( + merchant_pub), + "H_wire", GNUNET_JSON_from_data_auto ( + h_wire), + "execution_time", + GNUNET_JSON_from_time_abs (exec_time), + "deposits", deposits, + "exchange_sig", + GNUNET_JSON_from_data_auto (&sig), + "exchange_pub", + GNUNET_JSON_from_data_auto (&pub)); } @@ -396,23 +398,28 @@ track_transfer_transaction (void *cls, if (GNUNET_DB_STATUS_HARD_ERROR == qs) { GNUNET_break (0); - *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection, - TALER_EC_TRACK_TRANSFER_DB_FETCH_FAILED); + *mhd_ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSFER_DB_FETCH_FAILED, + "failed to fetch transaction data"); } return qs; } if (GNUNET_SYSERR == ctx->is_valid) { GNUNET_break (0); - *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection, - TALER_EC_TRACK_TRANSFER_DB_INCONSISTENT); + *mhd_ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSFER_DB_INCONSISTENT, + "exchange database internally inconsistent"); return GNUNET_DB_STATUS_HARD_ERROR; } if (GNUNET_NO == ctx->is_valid) { - *mhd_ret = TEH_RESPONSE_reply_arg_unknown (connection, - TALER_EC_TRACK_TRANSFER_WTID_NOT_FOUND, - "wtid"); + *mhd_ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_TRACK_TRANSFER_WTID_NOT_FOUND, + "wtid"); return GNUNET_DB_STATUS_HARD_ERROR; } qs = TEH_plugin->get_wire_fee (TEH_plugin->cls, @@ -430,8 +437,10 @@ track_transfer_transaction (void *cls, (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS) ) { GNUNET_break (0); - *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection, - TALER_EC_TRACK_TRANSFER_WIRE_FEE_NOT_FOUND); + *mhd_ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSFER_WIRE_FEE_NOT_FOUND, + "did not find wire fee"); } return qs; } @@ -441,8 +450,10 @@ track_transfer_transaction (void *cls, &ctx->wire_fee)) { GNUNET_break (0); - *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection, - TALER_EC_TRACK_TRANSFER_WIRE_FEE_INCONSISTENT); + *mhd_ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSFER_WIRE_FEE_INCONSISTENT, + "could not subtract wire fee"); return GNUNET_DB_STATUS_HARD_ERROR; } return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; diff --git a/src/exchange/taler-exchange-httpd_wire.c b/src/exchange/taler-exchange-httpd_wire.c index 91c928f94..4e4c34db5 100644 --- a/src/exchange/taler-exchange-httpd_wire.c +++ b/src/exchange/taler-exchange-httpd_wire.c @@ -25,6 +25,7 @@ #include "taler-exchange-httpd_validation.h" #include "taler-exchange-httpd_wire.h" #include "taler_json_lib.h" +#include "taler_mhd_lib.h" #include /** @@ -140,9 +141,9 @@ TEH_WIRE_handler_wire (struct TEH_RequestHandler *rh, (void) upload_data; (void) upload_data_size; GNUNET_assert (NULL != wire_methods); - return TEH_RESPONSE_reply_json (connection, - wire_methods, - MHD_HTTP_OK); + return TALER_MHD_reply_json (connection, + wire_methods, + MHD_HTTP_OK); }