diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-11-24 13:00:12 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-11-24 13:00:12 +0100 |
commit | 80c44a5279babccf1626534dc362f4d11a6bbc7a (patch) | |
tree | c622cdc80f5e383e8eb21af61d0fcbc76221b4b7 | |
parent | 6362ade0e6ee926beab51000ad27ae3d2bcb190c (diff) |
more deduplication via libtalermhd
20 files changed, 633 insertions, 1275 deletions
diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am index 6ade68f4..bcd75a30 100644 --- a/src/backend/Makefile.am +++ b/src/backend/Makefile.am @@ -25,7 +25,6 @@ taler_merchant_httpd_SOURCES = \ taler-merchant-httpd_poll-payment.c taler-merchant-httpd_poll-payment.h \ taler-merchant-httpd_proposal.c taler-merchant-httpd_proposal.h \ taler-merchant-httpd_refund.c taler-merchant-httpd_refund.h \ - taler-merchant-httpd_responses.c taler-merchant-httpd_responses.h \ taler-merchant-httpd_tip-authorize.c taler-merchant-httpd_tip-authorize.h \ taler-merchant-httpd_tip-pickup.c taler-merchant-httpd_tip-pickup.h \ taler-merchant-httpd_tip-query.c taler-merchant-httpd_tip-query.h \ diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index f94613d8..aa07a03b 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -30,7 +30,6 @@ #include <taler/taler_exchange_service.h> #include <taler/taler_wire_plugin.h> #include <taler/taler_wire_lib.h> -#include "taler-merchant-httpd_responses.h" #include "taler_merchantdb_lib.h" #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" @@ -1475,13 +1474,13 @@ url_handler (void *cls, } } if (NULL == instance) - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_NOT_FOUND, - "{s:I, s:s}", - "code", - (json_int_t) TALER_EC_INSTANCE_UNKNOWN, - "error", - "merchant instance unknown"); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_NOT_FOUND, + "{s:I, s:s}", + "code", + (json_int_t) TALER_EC_INSTANCE_UNKNOWN, + "error", + "merchant instance unknown"); if (GNUNET_NO == is_public) { @@ -1523,11 +1522,11 @@ url_handler (void *cls, "invalid request: method '%s' for '%s' not allowed\n", method, url); - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_METHOD_NOT_ALLOWED, - "{s:s}", - "error", - "method not allowed"); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_METHOD_NOT_ALLOWED, + "{s:s}", + "error", + "method not allowed"); } GNUNET_log (GNUNET_ERROR_TYPE_INFO, "invalid request: URL '%s' not handled\n", diff --git a/src/backend/taler-merchant-httpd_check-payment.c b/src/backend/taler-merchant-httpd_check-payment.c index f988c541..4199c04a 100644 --- a/src/backend/taler-merchant-httpd_check-payment.c +++ b/src/backend/taler-merchant-httpd_check-payment.c @@ -28,7 +28,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_check-payment.h" /** @@ -217,23 +216,24 @@ send_pay_request (struct CheckPaymentRequestContext *cprc) GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (cprc->sc.con, - TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, - "db error fetching pay session info"); + return TALER_MHD_reply_with_error (cprc->sc.con, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, + "db error fetching pay session info"); } } taler_pay_uri = TMH_make_taler_pay_uri (cprc->sc.con, cprc->order_id, cprc->session_id, cprc->mi->id); - ret = TMH_RESPONSE_reply_json_pack (cprc->sc.con, - MHD_HTTP_OK, - "{s:s, s:s, s:b, s:s?}", - "taler_pay_uri", taler_pay_uri, - "contract_url", cprc->final_contract_url, - "paid", 0, - "already_paid_order_id", - already_paid_order_id); + ret = TALER_MHD_reply_json_pack (cprc->sc.con, + MHD_HTTP_OK, + "{s:s, s:s, s:b, s:s?}", + "taler_pay_uri", taler_pay_uri, + "contract_url", cprc->final_contract_url, + "paid", 0, + "already_paid_order_id", + already_paid_order_id); GNUNET_free (taler_pay_uri); GNUNET_free_non_null (already_paid_order_id); return ret; @@ -267,9 +267,10 @@ parse_contract_terms (struct CheckPaymentRequestContext *cprc) { GNUNET_break (0); cprc->ret - = TMH_RESPONSE_reply_internal_error (cprc->sc.con, - TALER_EC_CHECK_PAYMENT_DB_FETCH_CONTRACT_TERMS_ERROR, - "Merchant database error (contract terms corrupted)"); + = TALER_MHD_reply_with_error (cprc->sc.con, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_CONTRACT_TERMS_ERROR, + "Merchant database error (contract terms corrupted)"); return GNUNET_SYSERR; } if (GNUNET_OK != @@ -278,9 +279,10 @@ parse_contract_terms (struct CheckPaymentRequestContext *cprc) { GNUNET_break (0); cprc->ret - = TMH_RESPONSE_reply_internal_error (cprc->sc.con, - TALER_EC_CHECK_PAYMENT_FAILED_COMPUTE_PROPOSAL_HASH, - "Failed to hash proposal"); + = TALER_MHD_reply_with_error (cprc->sc.con, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_FAILED_COMPUTE_PROPOSAL_HASH, + "Failed to hash proposal"); return GNUNET_SYSERR; } return GNUNET_OK; @@ -317,15 +319,17 @@ check_order_and_request_payment (struct CheckPaymentRequestContext *cprc) GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (cprc->sc.con, - TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, - "db error fetching order"); + return TALER_MHD_reply_with_error (cprc->sc.con, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, + "db error fetching order"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { - return TMH_RESPONSE_reply_not_found (cprc->sc.con, - TALER_EC_CHECK_PAYMENT_ORDER_ID_UNKNOWN, - "unknown order_id"); + return TALER_MHD_reply_with_error (cprc->sc.con, + MHD_HTTP_NOT_FOUND, + TALER_EC_CHECK_PAYMENT_ORDER_ID_UNKNOWN, + "unknown order_id"); } if (GNUNET_OK != @@ -380,9 +384,10 @@ MH_handler_check_payment (struct TMH_RequestHandler *rh, { /* order_id is required but missing */ GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MISSING, - "order_id required"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "order_id required"); } cprc->contract_url = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, @@ -416,9 +421,10 @@ MH_handler_check_payment (struct TMH_RequestHandler *rh, &timeout)) { GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MALFORMED, - "timeout must be non-negative number"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "timeout must be non-negative number"); } cprc->sc.long_poll_timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply ( @@ -451,9 +457,10 @@ MH_handler_check_payment (struct TMH_RequestHandler *rh, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_CHECK_PAYMENT_DB_FETCH_CONTRACT_TERMS_ERROR, - "db error fetching contract terms"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_CONTRACT_TERMS_ERROR, + "db error fetching contract terms"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) @@ -490,9 +497,10 @@ MH_handler_check_payment (struct TMH_RequestHandler *rh, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, - "db error fetching pay session info"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, + "db error fetching pay session info"); } else if (0 == qs) { @@ -522,9 +530,10 @@ MH_handler_check_payment (struct TMH_RequestHandler *rh, { /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, - "Merchant database error"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, + "Merchant database error"); } if (0 == qs) { @@ -555,24 +564,25 @@ MH_handler_check_payment (struct TMH_RequestHandler *rh, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Database hard error on refunds_from_contract_terms_hash lookup: %s\n", GNUNET_h2s (&cprc->h_contract_terms)); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, - "Merchant database error"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, + "Merchant database error"); } if (cprc->refunded) - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:b, s:b, s:o}", - "contract_terms", cprc->contract_terms, - "paid", 1, - "refunded", cprc->refunded, - "refund_amount", - TALER_JSON_from_amount ( - &cprc->refund_amount)); - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:b, s:b }", - "contract_terms", cprc->contract_terms, - "paid", 1, - "refunded", 0); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:b, s:b, s:o}", + "contract_terms", cprc->contract_terms, + "paid", 1, + "refunded", cprc->refunded, + "refund_amount", + TALER_JSON_from_amount ( + &cprc->refund_amount)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:b, s:b }", + "contract_terms", cprc->contract_terms, + "paid", 1, + "refunded", 0); } diff --git a/src/backend/taler-merchant-httpd_config.c b/src/backend/taler-merchant-httpd_config.c index bfd2abcb..8fc62708 100644 --- a/src/backend/taler-merchant-httpd_config.c +++ b/src/backend/taler-merchant-httpd_config.c @@ -25,7 +25,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_tip-query.h" #include "taler-merchant-httpd_tip-reserve-helper.h" @@ -67,11 +66,16 @@ MH_handler_config (struct TMH_RequestHandler *rh, size_t *upload_data_size, struct MerchantInstance *mi) { - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:s, s:s}", - "currency", TMH_currency, - "version", TALER_PROTOCOL_VERSION); + (void) rh; + (void) connection_cls; + (void) upload_data; + (void) upload_data_size; + (void) mi; + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:s, s:s}", + "currency", TMH_currency, + "version", TALER_PROTOCOL_VERSION); } diff --git a/src/backend/taler-merchant-httpd_history.c b/src/backend/taler-merchant-httpd_history.c index e4ba5092..fafb51d9 100644 --- a/src/backend/taler-merchant-httpd_history.c +++ b/src/backend/taler-merchant-httpd_history.c @@ -23,7 +23,6 @@ #include <taler/taler_signatures.h> #include <taler/taler_json_lib.h> #include "taler-merchant-httpd.h" -#include "taler-merchant-httpd_responses.h" /** @@ -159,17 +158,19 @@ MH_handler_history (struct TMH_RequestHandler *rh, &seconds)) { json_decref (response); - return TMH_RESPONSE_reply_arg_invalid (connection, - TALER_EC_PARAMETER_MALFORMED, - "date"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "date"); } date.abs_value_us = seconds * 1000LL * 1000LL; if (date.abs_value_us / 1000LL / 1000LL != seconds) { json_decref (response); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_HISTORY_TIMESTAMP_OVERFLOW, - "Timestamp overflowed"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_HISTORY_TIMESTAMP_OVERFLOW, + "Timestamp overflowed"); } } @@ -198,13 +199,14 @@ MH_handler_history (struct TMH_RequestHandler *rh, (GNUNET_SYSERR == pcc.failure) ) { json_decref (response); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_HISTORY_DB_FETCH_ERROR, - "db error to get history"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_HISTORY_DB_FETCH_ERROR, + "db error to get history"); } - ret = TMH_RESPONSE_reply_json (connection, - response, - MHD_HTTP_OK); + ret = TALER_MHD_reply_json (connection, + response, + MHD_HTTP_OK); json_decref (response); return ret; } @@ -221,9 +223,10 @@ MH_handler_history (struct TMH_RequestHandler *rh, &start)) { json_decref (response); - return TMH_RESPONSE_reply_arg_invalid (connection, - TALER_EC_PARAMETER_MALFORMED, - "start"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "start"); } } @@ -236,9 +239,10 @@ MH_handler_history (struct TMH_RequestHandler *rh, if (1 != sscanf (str, "%lld", &delta)) - return TMH_RESPONSE_reply_arg_invalid (connection, - TALER_EC_PARAMETER_MALFORMED, - "delta"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "delta"); } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying history back to %s, start: %llu, delta: %lld\n", @@ -276,15 +280,16 @@ MH_handler_history (struct TMH_RequestHandler *rh, /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); json_decref (response); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_HISTORY_DB_FETCH_ERROR, - "db error to get history"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_HISTORY_DB_FETCH_ERROR, + "db error to get history"); } - ret = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{ s:o }", - "history", - response /* consumes 'response' */); + ret = TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{ s:o }", + "history", + response /* consumes 'response' */); LOG_INFO ("/history, http code: %d\n", MHD_HTTP_OK); return ret; diff --git a/src/backend/taler-merchant-httpd_mhd.c b/src/backend/taler-merchant-httpd_mhd.c index a80c8339..fcd12989 100644 --- a/src/backend/taler-merchant-httpd_mhd.c +++ b/src/backend/taler-merchant-httpd_mhd.c @@ -25,7 +25,6 @@ #include "platform.h" #include <jansson.h> #include "taler-merchant-httpd_mhd.h" -#include "taler-merchant-httpd_responses.h" /** @@ -48,30 +47,18 @@ TMH_MHD_handler_static_response (struct TMH_RequestHandler *rh, size_t *upload_data_size, struct MerchantInstance *instance) { - struct MHD_Response *response; - int ret; - + (void) instance; + (void) connection_cls; + (void) upload_data; + (void) upload_data_size; (void) instance; if (0 == rh->data_size) rh->data_size = strlen ((const char *) rh->data); - response = MHD_create_response_from_buffer (rh->data_size, - (void *) rh->data, - MHD_RESPMEM_PERSISTENT); - if (NULL == response) - { - GNUNET_break (0); - return MHD_NO; - } - TMH_RESPONSE_add_global_headers (response); - if (NULL != rh->mime_type) - (void) MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - rh->mime_type); - ret = MHD_queue_response (connection, - rh->response_code, - response); - MHD_destroy_response (response); - return ret; + return TALER_MHD_reply_static (connection, + rh->response_code, + rh->mime_type, + rh->data, + rh->data_size); } @@ -95,69 +82,12 @@ TMH_MHD_handler_agpl_redirect (struct TMH_RequestHandler *rh, size_t *upload_data_size, struct MerchantInstance *mi) { - const char *agpl = - "This server is licensed under the Affero GPL. You will now be redirected to the source code."; - struct MHD_Response *response; - int ret; - (void) mi; - response = MHD_create_response_from_buffer (strlen (agpl), - (void *) agpl, - MHD_RESPMEM_PERSISTENT); - if (NULL == response) - { - GNUNET_break (0); - return MHD_NO; - } - TMH_RESPONSE_add_global_headers (response); - if (NULL != rh->mime_type) - (void) MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - rh->mime_type); - if (MHD_NO == - MHD_add_response_header (response, - MHD_HTTP_HEADER_LOCATION, - "http://www.git.taler.net/?p=exchange.git")) - { - GNUNET_break (0); - ret = MHD_NO; - } - else - { - ret = MHD_queue_response (connection, - rh->response_code, - response); - } - MHD_destroy_response (response); - return ret; -} - - -/** - * Function to call to handle the request by building a JSON - * reply with an error message from @a rh. - * - * @param rh context of the handler - * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data - * @param mi merchant backend instance, never NULL - * @return MHD result code - */ -int -TMH_MHD_handler_send_json_pack_error (struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size, - struct MerchantInstance *mi) -{ - return TMH_RESPONSE_reply_json_pack (connection, - rh->response_code, - "{s:s}", - "error", - rh->data); + (void) connection_cls; + (void) upload_data; + (void) upload_data_size; + return TALER_MHD_reply_agpl (connection, + "http://www.git.taler.net/?p=merchant.git"); } diff --git a/src/backend/taler-merchant-httpd_order.c b/src/backend/taler-merchant-httpd_order.c index 985926ad..06f03878 100644 --- a/src/backend/taler-merchant-httpd_order.c +++ b/src/backend/taler-merchant-httpd_order.c @@ -30,7 +30,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" /** @@ -263,8 +262,9 @@ proposal_put (struct MHD_Connection *connection, tm_info = localtime (&timer); if (NULL == tm_info) { - return TMH_RESPONSE_reply_internal_error + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_PROPOSAL_NO_LOCALTIME, "failed to determine local time"); } @@ -466,8 +466,9 @@ proposal_put (struct MHD_Connection *connection, /* other internal errors might have occurred */ if (GNUNET_SYSERR == res) { - return TMH_RESPONSE_reply_internal_error + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_PROPOSAL_ORDER_PARSE_ERROR, "Impossible to parse the order"); } @@ -486,8 +487,9 @@ proposal_put (struct MHD_Connection *connection, wire_transfer_deadline)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "refund_deadline: %s\n", GNUNET_STRINGS_absolute_time_to_string (refund_deadline)); - return TMH_RESPONSE_reply_arg_invalid + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, TALER_EC_PARAMETER_MALFORMED, "order:wire_transfer_deadline;order:refund_deadline"); } @@ -497,8 +499,9 @@ proposal_put (struct MHD_Connection *connection, if (GNUNET_OK != check_products (products)) { GNUNET_JSON_parse_free (spec); - return TMH_RESPONSE_reply_arg_invalid + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, TALER_EC_PARAMETER_MALFORMED, "order:products"); } @@ -520,9 +523,10 @@ proposal_put (struct MHD_Connection *connection, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No wire method available for instance '%s'\n", mi->id); GNUNET_JSON_parse_free (spec); - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_PROPOSAL_INSTANCE_CONFIGURATION_LACKS_WIRE, - "No wire method configured for instance"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_PROPOSAL_INSTANCE_CONFIGURATION_LACKS_WIRE, + "No wire method configured for instance"); } json_object_set_new (order, "H_wire", @@ -556,8 +560,9 @@ proposal_put (struct MHD_Connection *connection, if (GNUNET_DB_STATUS_SOFT_ERROR == qs) { GNUNET_break (0); - return TMH_RESPONSE_reply_internal_error + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_PROPOSAL_STORE_DB_ERROR_SOFT, "db error: could not check for existing order" " due to repeated soft transaction failure"); @@ -600,8 +605,9 @@ proposal_put (struct MHD_Connection *connection, /* contract_terms may be private, only expose * duplicate order_id to the network */ - rv = TMH_RESPONSE_reply_external_error + rv = TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, /* or conflict? */ TALER_EC_PROPOSAL_STORE_DB_ERROR_ALREADY_EXISTS, msg); GNUNET_free (msg); @@ -611,18 +617,19 @@ proposal_put (struct MHD_Connection *connection, /* Other hard transaction error (disk full, etc.) */ GNUNET_JSON_parse_free (spec); - return TMH_RESPONSE_reply_internal_error + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_PROPOSAL_STORE_DB_ERROR_HARD, "db error: could not store this proposal's data into db"); } /* DB transaction succeeded, generate positive response */ - res = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:s}", - "order_id", - order_id); + res = TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:s}", + "order_id", + order_id); GNUNET_JSON_parse_free (spec); return res; } @@ -685,8 +692,9 @@ MH_handler_order_post (struct TMH_RequestHandler *rh, "order"); if (NULL == order) { - res = TMH_RESPONSE_reply_arg_missing + res = TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, TALER_EC_PARAMETER_MISSING, "order"); } diff --git a/src/backend/taler-merchant-httpd_pay.c b/src/backend/taler-merchant-httpd_pay.c index a3690eca..7e363c1f 100644 --- a/src/backend/taler-merchant-httpd_pay.c +++ b/src/backend/taler-merchant-httpd_pay.c @@ -31,7 +31,6 @@ #include <taler/taler_json_lib.h> #include <taler/taler_exchange_service.h> #include "taler-merchant-httpd.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" #include "taler-merchant-httpd_refund.h" @@ -545,8 +544,8 @@ sign_success_response (struct PayContext *pc) &ec, &errmsg); if (NULL == refunds) - return TMH_RESPONSE_make_error (ec, - errmsg); + return TALER_MHD_make_error (ec, + errmsg); { struct PaymentResponsePS mr = { .purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_PAYMENT_OK), @@ -569,7 +568,7 @@ sign_success_response (struct PayContext *pc) "refund_permissions", refunds); - mret = TMH_RESPONSE_make_json (resp); + mret = TALER_MHD_make_json (resp); json_decref (resp); return mret; } @@ -591,8 +590,8 @@ resume_pay_with_error (struct PayContext *pc, { resume_pay_with_response (pc, http_status, - TMH_RESPONSE_make_error (ec, - msg)); + TALER_MHD_make_error (ec, + msg)); } @@ -1037,7 +1036,7 @@ deposit_cb (void *cls, /* We can't do anything meaningful here, the exchange did something wrong */ resume_pay_with_response (pc, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_json_pack ( + TALER_MHD_make_json_pack ( "{s:s, s:I, s:I, s:I, s:s}", "error", "exchange failed", @@ -1062,7 +1061,7 @@ deposit_cb (void *cls, GNUNET_JSON_from_data_auto (&dc->coin_pub)); resume_pay_with_response (pc, http_status, - TMH_RESPONSE_make_json (eproof)); + TALER_MHD_make_json (eproof)); json_decref (eproof); } return; @@ -1186,7 +1185,7 @@ process_pay_with_exchange (void *cls, resume_pay_with_response (pc, MHD_HTTP_BAD_REQUEST, - TMH_RESPONSE_make_json_pack + TALER_MHD_make_json_pack ("{s:s, s:I, s:o, s:o}", "error", "denomination not found", "code", TALER_EC_PAY_DENOMINATION_KEY_NOT_FOUND, @@ -1204,7 +1203,7 @@ process_pay_with_exchange (void *cls, resume_pay_with_response (pc, MHD_HTTP_BAD_REQUEST, - TMH_RESPONSE_make_json_pack + TALER_MHD_make_json_pack ("{s:s, s:I, s:o}", "error", "invalid denomination", "code", (json_int_t) @@ -1251,7 +1250,7 @@ process_pay_with_exchange (void *cls, resume_pay_with_response (pc, MHD_HTTP_UNAUTHORIZED, - TMH_RESPONSE_make_json_pack + TALER_MHD_make_json_pack ("{s:s, s:I, s:i}", "hint", "Coin signature invalid.", "code", (json_int_t) @@ -1468,11 +1467,13 @@ parse_pay (struct MHD_Connection *connection, { TALER_LOG_INFO ( "Unknown merchant public key included in payment (usually wrong instance chosen)\n"); - TMH_RESPONSE_reply_rc (connection, - MHD_HTTP_NOT_FOUND, - TALER_EC_PAY_WRONG_INSTANCE, - "Payment sent to wrong instance (merchant_pub unknown to the merchant)"); - return GNUNET_NO; + if (MHD_YES == + TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_PAY_WRONG_INSTANCE, + "Payment sent to wrong instance (merchant_pub unknown to the merchant)")) + return GNUNET_NO; + return GNUNET_SYSERR; } if (NULL != session_id) @@ -1490,18 +1491,20 @@ parse_pay (struct MHD_Connection *connection, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_DB_FETCH_PAY_ERROR, - "db error to previous /pay data"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_DB_FETCH_PAY_ERROR, + "db error to previous /pay data"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { GNUNET_JSON_parse_free (spec); if (MHD_YES != - TMH_RESPONSE_reply_not_found (connection, - TALER_EC_PAY_DB_STORE_PAY_ERROR, - "Proposal not found")) + TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_PAY_DB_STORE_PAY_ERROR, + "Proposal not found")) { GNUNET_break (0); return GNUNET_SYSERR; @@ -1516,9 +1519,10 @@ parse_pay (struct MHD_Connection *connection, GNUNET_break (0); GNUNET_JSON_parse_free (spec); if (MHD_YES != - TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_FAILED_COMPUTE_PROPOSAL_HASH, - "Failed to hash proposal")) + TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_FAILED_COMPUTE_PROPOSAL_HASH, + "Failed to hash proposal")) { GNUNET_break (0); return GNUNET_SYSERR; @@ -1538,9 +1542,10 @@ parse_pay (struct MHD_Connection *connection, GNUNET_break (0); GNUNET_JSON_parse_free (spec); if (MHD_YES != - TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_MERCHANT_FIELD_MISSING, - "No merchant field in proposal")) + TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_MERCHANT_FIELD_MISSING, + "No merchant field in proposal")) { GNUNET_break (0); return GNUNET_SYSERR; @@ -1595,9 +1600,10 @@ parse_pay (struct MHD_Connection *connection, /* This should already have been checked when creating the order! */ GNUNET_break (0); GNUNET_JSON_parse_free (spec); - return TMH_RESPONSE_reply_external_error (connection, - TALER_EC_PAY_REFUND_DEADLINE_PAST_WIRE_TRANSFER_DEADLINE, - "refund deadline after wire transfer deadline"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PAY_REFUND_DEADLINE_PAST_WIRE_TRANSFER_DEADLINE, + "refund deadline after wire transfer deadline"); } } @@ -1613,9 +1619,10 @@ parse_pay (struct MHD_Connection *connection, { GNUNET_break (0); GNUNET_JSON_parse_free (spec); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_WIRE_HASH_UNKNOWN, - "Did not find matching wire details"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_WIRE_HASH_UNKNOWN, + "Did not find matching wire details"); } pc->wm = wm; } @@ -1678,9 +1685,10 @@ parse_pay (struct MHD_Connection *connection, if (0 == pc->coins_cnt) { GNUNET_JSON_parse_free (spec); - return TMH_RESPONSE_reply_arg_invalid (connection, - TALER_EC_PAY_COINS_ARRAY_EMPTY, - "coins"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_PAY_COINS_ARRAY_EMPTY, + "coins"); } /* note: 1 coin = 1 deposit confirmation expected */ pc->dc = GNUNET_new_array (pc->coins_cnt, @@ -1785,12 +1793,12 @@ begin_transaction (struct PayContext *pc) GNUNET_break (0); resume_pay_with_response (pc, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_json_pack ("{s:I, s:s}", - "code", - (json_int_t) - TALER_EC_PAY_DB_STORE_TRANSACTION_ERROR, - "hint", - "Soft merchant database error: retry counter exceeded")); + TALER_MHD_make_json_pack ("{s:I, s:s}", + "code", + (json_int_t) + TALER_EC_PAY_DB_STORE_TRANSACTION_ERROR, + "hint", + "Soft merchant database error: retry counter exceeded")); return; } @@ -2034,7 +2042,7 @@ begin_transaction (struct PayContext *pc) resume_pay_with_response (pc, MHD_HTTP_OK, - TMH_RESPONSE_make_json_pack + TALER_MHD_make_json_pack ("{s:o, s:o, s:o}", /* Refunds pack. */ "refund_permissions", refunds, @@ -2253,7 +2261,10 @@ MH_handler_pay (struct TMH_RequestHandler *rh, if (GNUNET_SYSERR == res) { GNUNET_break (0); - return TMH_RESPONSE_reply_invalid_json (connection); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_JSON_INVALID, + "could not parse JSON"); } if ( (GNUNET_NO == res) || (NULL == root) ) diff --git a/src/backend/taler-merchant-httpd_poll-payment.c b/src/backend/taler-merchant-httpd_poll-payment.c index ae40e91c..8e28ec6c 100644 --- a/src/backend/taler-merchant-httpd_poll-payment.c +++ b/src/backend/taler-merchant-httpd_poll-payment.c @@ -28,7 +28,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_poll-payment.h" /** @@ -217,9 +216,10 @@ send_pay_request (struct PollPaymentRequestContext *pprc) GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (pprc->sc.con, - TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, - "db error fetching pay session info"); + return TALER_MHD_reply_with_error (pprc->sc.con, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, + "db error fetching pay session info"); } } GNUNET_log (GNUNET_ERROR_TYPE_INFO, @@ -228,14 +228,14 @@ send_pay_request (struct PollPaymentRequestContext *pprc) pprc->order_id, pprc->session_id, pprc->mi->id); - ret = TMH_RESPONSE_reply_json_pack (pprc->sc.con, - MHD_HTTP_OK, - "{s:s, s:s, s:b, s:s?}", - "taler_pay_uri", taler_pay_uri, - "contract_url", pprc->final_contract_url, - "paid", 0, - "already_paid_order_id", - already_paid_order_id); + ret = TALER_MHD_reply_json_pack (pprc->sc.con, + MHD_HTTP_OK, + "{s:s, s:s, s:b, s:s?}", + "taler_pay_uri", taler_pay_uri, + "contract_url", pprc->final_contract_url, + "paid", 0, + "already_paid_order_id", + already_paid_order_id); GNUNET_free (taler_pay_uri); GNUNET_free_non_null (already_paid_order_id); return ret; @@ -287,9 +287,10 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, { /* order_id is required but missing */ GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MISSING, - "order_id required"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "order_id required"); } cts = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, @@ -298,9 +299,10 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, { /* h_contract required but missing */ GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MISSING, - "h_contract required"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "h_contract required"); } if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cts, @@ -308,9 +310,10 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, { /* cts has wrong encoding */ GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MALFORMED, - "h_contract malformed"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "h_contract malformed"); } long_poll_timeout_s = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, @@ -324,9 +327,10 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, &timeout)) { GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MALFORMED, - "timeout must be non-negative number"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "timeout must be non-negative number"); } pprc->sc.long_poll_timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply ( @@ -369,15 +373,17 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, { /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, - "Merchant database error"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, + "Merchant database error"); } if (0 == qs) { - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_POLL_PAYMENT_CONTRACT_NOT_FOUND, - "Given order_id doesn't map to any proposal"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_POLL_PAYMENT_CONTRACT_NOT_FOUND, + "Given order_id doesn't map to any proposal"); } GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs); @@ -394,9 +400,10 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, NULL, NULL)) { GNUNET_break (0); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_CHECK_PAYMENT_DB_FETCH_CONTRACT_TERMS_ERROR, - "Merchant database error (contract terms corrupted)"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_CONTRACT_TERMS_ERROR, + "Merchant database error (contract terms corrupted)"); } } } /* end of first-time initialization / sanity checks */ @@ -422,9 +429,10 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, - "db error fetching pay session info"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_CHECK_PAYMENT_DB_FETCH_ORDER_ERROR, + "db error fetching pay session info"); } else if (0 == qs) { @@ -450,9 +458,10 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, { /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, - "Merchant database error"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, + "Merchant database error"); } if (0 == qs) { @@ -479,22 +488,23 @@ MH_handler_poll_payment (struct TMH_RequestHandler *rh, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Database hard error on refunds_from_contract_terms_hash lookup: %s\n", GNUNET_h2s (&pprc->h_contract_terms)); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, - "Merchant database error"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR, + "Merchant database error"); } if (pprc->refunded) - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:b, s:b, s:o}", - "paid", 1, - "refunded", pprc->refunded, - "refund_amount", - TALER_JSON_from_amount ( - &pprc->refund_amount)); - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:b, s:b }", - "paid", 1, - "refunded", 0); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:b, s:b, s:o}", + "paid", 1, + "refunded", pprc->refunded, + "refund_amount", + TALER_JSON_from_amount ( + &pprc->refund_amount)); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:b, s:b }", + "paid", 1, + "refunded", 0); } diff --git a/src/backend/taler-merchant-httpd_proposal.c b/src/backend/taler-merchant-httpd_proposal.c index 95c9caaf..c8365b92 100644 --- a/src/backend/taler-merchant-httpd_proposal.c +++ b/src/backend/taler-merchant-httpd_proposal.c @@ -30,7 +30,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" /** @@ -63,7 +62,6 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, { const char *order_id; const char *nonce; - int res; enum GNUNET_DB_QueryStatus qs; json_t *contract_terms; struct GNUNET_CRYPTO_EddsaSignature merchant_sig; @@ -73,16 +71,18 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, MHD_GET_ARGUMENT_KIND, "order_id"); if (NULL == order_id) - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "order_id"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "order_id"); nonce = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "nonce"); if (NULL == nonce) - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "nonce"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "nonce"); db->preflight (db->cls); qs = db->find_contract_terms (db->cls, &contract_terms, @@ -95,9 +95,10 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PROPOSAL_LOOKUP_DB_ERROR, - "An error occurred while retrieving proposal data from db"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PROPOSAL_LOOKUP_DB_ERROR, + "An error occurred while retrieving proposal data from db"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { @@ -106,6 +107,7 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, GNUNET_JSON_spec_absolute_time ("timestamp", ×tamp), GNUNET_JSON_spec_end () }; + int res; db->preflight (db->cls); qs = db->find_order (db->cls, @@ -114,9 +116,10 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, &mi->pubkey); if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_PROPOSAL_LOOKUP_NOT_FOUND, - "unknown order id"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_PROPOSAL_LOOKUP_NOT_FOUND, + "unknown order id"); } GNUNET_assert (NULL != contract_terms); json_object_set_new (contract_terms, @@ -133,9 +136,10 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, } if (GNUNET_SYSERR == res) { - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PROPOSAL_ORDER_PARSE_ERROR, - "Impossible to parse the order"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PROPOSAL_ORDER_PARSE_ERROR, + "Impossible to parse the order"); } for (unsigned int i = 0; i<MAX_RETRIES; i++) @@ -155,9 +159,10 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PROPOSAL_STORE_DB_ERROR, - "db error: could not store this proposal's data into db"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PROPOSAL_STORE_DB_ERROR, + "db error: could not store this proposal's data into db"); } // FIXME: now we can delete (merchant_pub, order_id) from the merchant_orders table } @@ -171,17 +176,19 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, if (NULL == stored_nonce) { GNUNET_break (0); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_PROPOSAL_ORDER_PARSE_ERROR, - "existing proposal has no nonce"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PROPOSAL_ORDER_PARSE_ERROR, + "existing proposal has no nonce"); } if (0 != strcmp (stored_nonce, nonce)) { - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PROPOSAL_LOOKUP_NOT_FOUND, - "mismatched nonce"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PROPOSAL_LOOKUP_NOT_FOUND, + "mismatched nonce"); } @@ -197,24 +204,24 @@ MH_handler_proposal_lookup (struct TMH_RequestHandler *rh, &pdps.hash)) { GNUNET_break (0); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_LOGIC_ERROR, - "Could not hash order"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_LOGIC_ERROR, + "Could not hash order"); } GNUNET_CRYPTO_eddsa_sign (&mi->privkey.eddsa_priv, &pdps.purpose, &merchant_sig); } - res = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{ s:o, s:o }", - "contract_terms", - contract_terms, - "sig", - GNUNET_JSON_from_data_auto ( - &merchant_sig)); - return res; + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{ s:o, s:o }", + "contract_terms", + contract_terms, + "sig", + GNUNET_JSON_from_data_auto ( + &merchant_sig)); } diff --git a/src/backend/taler-merchant-httpd_refund.c b/src/backend/taler-merchant-httpd_refund.c index c40644ce..6051f2bf 100644 --- a/src/backend/taler-merchant-httpd_refund.c +++ b/src/backend/taler-merchant-httpd_refund.c @@ -23,7 +23,6 @@ #include <taler/taler_signatures.h> #include <taler/taler_json_lib.h> #include "taler-merchant-httpd.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_refund.h" /** @@ -238,9 +237,10 @@ MH_handler_refund_increase (struct TMH_RequestHandler *rh, /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); json_decref (root); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_REFUND_LOOKUP_DB_ERROR, - "An error occurred while retrieving payment data from db"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_REFUND_LOOKUP_DB_ERROR, + "An error occurred while retrieving payment data from db"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { @@ -248,9 +248,10 @@ MH_handler_refund_increase (struct TMH_RequestHandler *rh, "Unknown order id given: %s\n", order_id); json_decref (root); - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_REFUND_ORDER_ID_UNKNOWN, - "Order id not found in database"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_REFUND_ORDER_ID_UNKNOWN, + "Order id not found in database"); } if (GNUNET_OK != @@ -263,9 +264,10 @@ MH_handler_refund_increase (struct TMH_RequestHandler *rh, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not hash contract terms\n"); json_decref (root); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_LOGIC_ERROR, - "Could not hash contract terms"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_LOGIC_ERROR, + "Could not hash contract terms"); } for (unsigned int i = 0; i<MAX_RETRIES; i++) { @@ -323,9 +325,10 @@ MH_handler_refund_increase (struct TMH_RequestHandler *rh, GNUNET_JSON_parse_free (spec); json_decref (contract_terms); json_decref (root); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_REFUND_MERCHANT_DB_COMMIT_ERROR, - "Internal database error or refund amount too big"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_REFUND_MERCHANT_DB_COMMIT_ERROR, + "Internal database error or refund amount too big"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { @@ -335,9 +338,10 @@ MH_handler_refund_increase (struct TMH_RequestHandler *rh, GNUNET_JSON_parse_free (spec); json_decref (contract_terms); json_decref (root); - return TMH_RESPONSE_reply_external_error (connection, - TALER_EC_REFUND_INCONSISTENT_AMOUNT, - "Amount incorrect: not larger than the previous one"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_REFUND_INCONSISTENT_AMOUNT, + "Amount incorrect: not larger than the previous one"); } /** @@ -365,9 +369,10 @@ MH_handler_refund_increase (struct TMH_RequestHandler *rh, json_decref (contract_terms); GNUNET_JSON_parse_free (spec); json_decref (root); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_REFUND_MERCHANT_SIGNING_FAILED, - "Refund done, but failed to sign confirmation"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_REFUND_MERCHANT_SIGNING_FAILED, + "Refund done, but failed to sign confirmation"); } @@ -376,15 +381,14 @@ MH_handler_refund_increase (struct TMH_RequestHandler *rh, char *taler_refund_uri; taler_refund_uri = make_taler_refund_uri (connection, mi->id, order_id); - - ret = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:o, s:s}", - "sig", - GNUNET_JSON_from_data_auto (&sig), - "contract_terms", - contract_terms, - "taler_refund_uri", taler_refund_uri); + ret = TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:o, s:s}", + "sig", + GNUNET_JSON_from_data_auto (&sig), + "contract_terms", + contract_terms, + "taler_refund_uri", taler_refund_uri); GNUNET_free (taler_refund_uri); GNUNET_JSON_parse_free (spec); json_decref (root); @@ -500,9 +504,10 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Argument 'order_id' not given\n"); - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "order_id"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "order_id"); } /* Convert order id to h_contract_terms */ @@ -519,9 +524,10 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_REFUND_LOOKUP_DB_ERROR, - "database error looking up order_id from merchant_contract_terms table"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_REFUND_LOOKUP_DB_ERROR, + "database error looking up order_id from merchant_contract_terms table"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) @@ -529,9 +535,10 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown order id given: %s\n", order_id); - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_REFUND_ORDER_ID_UNKNOWN, - "Order id not found in database"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_REFUND_ORDER_ID_UNKNOWN, + "Order id not found in database"); } if (GNUNET_OK != @@ -542,9 +549,10 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not hash contract terms\n"); json_decref (contract_terms); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_LOGIC_ERROR, - "Could not hash contract terms"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_LOGIC_ERROR, + "Could not hash contract terms"); } json_decref (contract_terms); @@ -558,19 +566,20 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, &ec, &errmsg); if (NULL == response) - return TMH_RESPONSE_reply_internal_error (connection, - ec, - errmsg); - return TMH_RESPONSE_reply_json_pack (connection, MHD_HTTP_OK, - "{s:o, s:o, s:o}", - "refund_permissions", - response, - "merchant_pub", - GNUNET_JSON_from_data_auto ( - &mi->pubkey), - "h_contract_terms", - GNUNET_JSON_from_data_auto ( - &h_contract_terms)); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + ec, + errmsg); + return TALER_MHD_reply_json_pack (connection, MHD_HTTP_OK, + "{s:o, s:o, s:o}", + "refund_permissions", + response, + "merchant_pub", + GNUNET_JSON_from_data_auto ( + &mi->pubkey), + "h_contract_terms", + GNUNET_JSON_from_data_auto ( + &h_contract_terms)); } } diff --git a/src/backend/taler-merchant-httpd_responses.c b/src/backend/taler-merchant-httpd_responses.c deleted file mode 100644 index 3be9cab5..00000000 --- a/src/backend/taler-merchant-httpd_responses.c +++ /dev/null @@ -1,404 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014-2017 GNUnet e.V. - - 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 - Foundation; either version 3, or (at your option) any later version. - - TALER is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file taler-merchant-httpd_responses.c - * @brief API for generating the various replies of the exchange; these - * functions are called TMH_RESPONSE_reply_ and they generate - * and queue MHD response objects for a given connection. - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#include "platform.h" -#include "taler-merchant-httpd.h" -#include "taler-merchant-httpd_responses.h" -#include <taler/taler_util.h> -#include <taler/taler_json_lib.h> -#include <gnunet/gnunet_util_lib.h> - - -/** - * Make JSON response object. - * - * @param json the json object - * @return MHD response object - */ -struct MHD_Response * -TMH_RESPONSE_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; - } - 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 - * @param json the json object - * @param response_code the http response code - * @return MHD result code - */ -int -TMH_RESPONSE_reply_json (struct MHD_Connection *connection, - const json_t *json, - unsigned int response_code) -{ - struct MHD_Response *resp; - int ret; - - resp = TMH_RESPONSE_make_json (json); - if (NULL == resp) - return MHD_NO; - ret = MHD_queue_response (connection, - response_code, - resp); - MHD_destroy_response (resp); - return ret; -} - - -/** - * Make JSON response object. - * - * @param fmt format string for pack - * @param ... varargs - * @return MHD response object - */ -struct MHD_Response * -TMH_RESPONSE_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 = TMH_RESPONSE_make_json (json); - json_decref (json); - return ret; -} - - -/** - * 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 - */ -int -TMH_RESPONSE_reply_json_pack (struct MHD_Connection *connection, - unsigned int response_code, - const char *fmt, - ...) -{ - json_t *json; - va_list argp; - int 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 = TMH_RESPONSE_reply_json (connection, - json, - response_code); - 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 * -TMH_RESPONSE_make_error (enum TALER_ErrorCode ec, - const char *hint) -{ - return TMH_RESPONSE_make_json_pack ("{s:I, s:s}", - "code", (json_int_t) ec, - "hint", hint); -} - - -/** - * Send a response indicating an internal error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the internal error's nature - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_internal_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint) -{ - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - "{s:I, s:s}", - "code", (json_int_t) ec, - "hint", hint); -} - - -/** - * Send a response indicating that the request was too big. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_request_too_large (struct MHD_Connection *connection) -{ - struct MHD_Response *resp; - int ret; - - resp = MHD_create_response_from_buffer (0, - NULL, - MHD_RESPMEM_PERSISTENT); - if (NULL == resp) - return MHD_NO; - ret = MHD_queue_response (connection, - MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, - resp); - MHD_destroy_response (resp); - return ret; -} - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param response_code response code to use - * @param ec error code to return - * @param msg human-readable diagnostic - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_rc (struct MHD_Connection *connection, - unsigned int response_code, - enum TALER_ErrorCode ec, - const char *msg) -{ - return TMH_RESPONSE_reply_json_pack (connection, - response_code, - "{s:I, s:s}", - "code", (json_int_t) ec, - "error", msg); -} - - -/** - * Send a response indicating that the JSON was malformed. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_invalid_json (struct MHD_Connection *connection) -{ - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:I, s:s}", - "code", - (json_int_t) TALER_EC_JSON_INVALID, - "error", "invalid json"); -} - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param object name of the object we did not find - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_not_found (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *object) -{ - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_NOT_FOUND, - "{s:I, s:s}", - "code", (json_int_t) ec, - "error", object); -} - - -/** - * Send a response indicating that the request was malformed. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param issue description of what was wrong with the request - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_bad_request (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *issue) -{ - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:I, s:s}", - "code", (json_int_t) ec, - "error", issue); -} - - -/** - * 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 -TMH_RESPONSE_add_global_headers (struct MHD_Response *response) -{ - TALER_MHD_add_global_headers (response); -} - - -/** - * Send a response indicating an external error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the error's nature - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_external_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint) -{ - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:I, s:s}", - "code", (json_int_t) ec, - "hint", hint); -} - - -/** - * Send a response indicating a missing argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is missing - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_arg_missing (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name) -{ - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:I, s:s}", - "error", "missing parameter", - "code", (json_int_t) ec, - "parameter", param_name); -} - - -/** - * Send a response indicating an invalid argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is invalid - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_arg_invalid (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name) -{ - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:I, s:s}", - "error", "invalid parameter", - "code", (json_int_t) ec, - "parameter", param_name); -} - - -/* end of taler-exchange-httpd_responses.c */ diff --git a/src/backend/taler-merchant-httpd_responses.h b/src/backend/taler-merchant-httpd_responses.h deleted file mode 100644 index 708185c9..00000000 --- a/src/backend/taler-merchant-httpd_responses.h +++ /dev/null @@ -1,245 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014-2017 GNUnet e.V. - - 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 - Foundation; either version 3, or (at your option) any later version. - - TALER is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> -*/ - -/** - * @file taler-merchant-httpd_responses.h - * @brief API for generating the various replies of the exchange; these - * functions are called TMH_RESPONSE_reply_ and they generate - * and queue MHD response objects for a given connection. - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#ifndef TALER_EXCHANGE_HTTPD_RESPONSES_H -#define TALER_EXCHANGE_HTTPD_RESPONSES_H -#include <gnunet/gnunet_util_lib.h> -#include <jansson.h> -#include <microhttpd.h> -#include <pthread.h> -/* just need some structs, not the actual API */ -#include <taler/taler_error_codes.h> -#include "taler_merchant_service.h" - -/** - * Make JSON response object. - * - * @param json the json object - * @return MHD response object - */ -struct MHD_Response * -TMH_RESPONSE_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 - */ -int -TMH_RESPONSE_reply_json (struct MHD_Connection *connection, - const json_t *json, - unsigned int response_code); - - -/** - * Make JSON response object. - * - * @param fmt format string for pack - * @param ... varargs - * @return MHD response object - */ -struct MHD_Response * -TMH_RESPONSE_make_json_pack (const char *fmt, - ...); - - -/** - * 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 - */ -int -TMH_RESPONSE_reply_json_pack (struct MHD_Connection *connection, - unsigned int response_code, - const char *fmt, - ...); - - -/** - * Send a response indicating that the JSON was malformed. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_invalid_json (struct MHD_Connection *connection); - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param response_code response code to use - * @param ec error code to return - * @param msg human-readable diagnostic - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_rc (struct MHD_Connection *connection, - unsigned int response_code, - enum TALER_ErrorCode ec, - const char *msg); - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param object name of the object we did not find - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_not_found (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *object); - - -/** - * Send a response indicating that the request was malformed. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param issue description of what was wrong with the request - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_bad_request (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *issue); - - -/** - * 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 * -TMH_RESPONSE_make_internal_error (enum TALER_ErrorCode ec, - const char *hint); - - -/** - * Send a response indicating an external error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the error's nature - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_external_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint); - - -/** - * Send a response indicating an internal error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the internal error's nature - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_internal_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint); - - -/** - * Create a response indicating an error. - * - * @param ec error code to return - * @param hint hint about the internal error's nature - * @return a MHD response object - */ -struct MHD_Response * -TMH_RESPONSE_make_error (enum TALER_ErrorCode ec, - const char *hint); - - -/** - * Send a response indicating that the request was too big. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_request_too_large (struct MHD_Connection *connection); - - -/** - * 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 -TMH_RESPONSE_add_global_headers (struct MHD_Response *response); - - -/** - * Send a response indicating a missing argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is missing - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_arg_missing (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name); - - -/** - * Send a response indicating an invalid argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is invalid - * @return a MHD result code - */ -int -TMH_RESPONSE_reply_arg_invalid (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name); - -#endif diff --git a/src/backend/taler-merchant-httpd_tip-authorize.c b/src/backend/taler-merchant-httpd_tip-authorize.c index c02d8bd0..5b4e4b0f 100644 --- a/src/backend/taler-merchant-httpd_tip-authorize.c +++ b/src/backend/taler-merchant-httpd_tip-authorize.c @@ -25,7 +25,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_tip-authorize.h" #include "taler-merchant-httpd_tip-reserve-helper.h" @@ -180,9 +179,10 @@ MH_handler_tip_authorize (struct TMH_RequestHandler *rh, GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Instance `%s' not configured for tipping\n", mi->id); - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_TIP_AUTHORIZE_INSTANCE_DOES_NOT_TIP, - "exchange for tipping not configured for the instance"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_TIP_AUTHORIZE_INSTANCE_DOES_NOT_TIP, + "exchange for tipping not configured for the instance"); } tac->ctr.reserve_priv = mi->tip_reserve; extra = json_object_get (tac->root, "extra"); @@ -241,10 +241,10 @@ MH_handler_tip_authorize (struct TMH_RequestHandler *rh, break; } - return TMH_RESPONSE_reply_rc (connection, - rc, - ec, - msg); + return TALER_MHD_reply_with_error (connection, + rc, + ec, + msg); } /* generate success response */ @@ -272,8 +272,10 @@ MH_handler_tip_authorize (struct TMH_RequestHandler *rh, { /* Should never happen, at last the host header should be defined */ GNUNET_break (0); - return TMH_RESPONSE_reply_internal_error (connection, 0, - "unable to identify backend host"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_INVARIANT_FAILURE, + "unable to identify backend host"); } if (0 == strcmp (mi->id, "default")) @@ -291,11 +293,11 @@ MH_handler_tip_authorize (struct TMH_RequestHandler *rh, hash_enc.encoding)); - res = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:s, s:s}", - "taler_tip_uri", taler_tip_uri, - "tip_id", hash_enc.encoding); + res = TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:s, s:s}", + "taler_tip_uri", taler_tip_uri, + "tip_id", hash_enc.encoding); return res; } } diff --git a/src/backend/taler-merchant-httpd_tip-pickup.c b/src/backend/taler-merchant-httpd_tip-pickup.c index e54b69d3..2fb7d2ac 100644 --- a/src/backend/taler-merchant-httpd_tip-pickup.c +++ b/src/backend/taler-merchant-httpd_tip-pickup.c @@ -26,7 +26,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_tip-pickup.h" @@ -189,10 +188,10 @@ run_pickup (struct MHD_Connection *connection, if (TALER_EC_NONE != pc->ec) { - return TMH_RESPONSE_reply_rc (connection, - pc->response_code, - pc->ec, - pc->error_hint); + return TALER_MHD_reply_with_error (connection, + pc->response_code, + pc->ec, + pc->error_hint); } db->preflight (db->cls); ec = db->pickup_tip_TR (db->cls, @@ -220,19 +219,19 @@ run_pickup (struct MHD_Connection *connection, human = "database failure"; break; } - return TMH_RESPONSE_reply_rc (connection, - response_code, - ec, - human); + return TALER_MHD_reply_with_error (connection, + response_code, + ec, + human); } sigs = json_array (); if (NULL == sigs) { GNUNET_break (0); - return TMH_RESPONSE_reply_rc (connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - TALER_EC_JSON_ALLOCATION_FAILURE, - "could not create JSON array"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_JSON_ALLOCATION_FAILURE, + "could not create JSON array"); } GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv.eddsa_priv, &reserve_pub.eddsa_pub); @@ -255,19 +254,19 @@ run_pickup (struct MHD_Connection *connection, { GNUNET_break (0); json_decref (sigs); - return TMH_RESPONSE_reply_rc (connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - TALER_EC_JSON_ALLOCATION_FAILURE, - "could not add element to JSON array"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_JSON_ALLOCATION_FAILURE, + "could not add element to JSON array"); } } - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o, s:o}", - "reserve_pub", - GNUNET_JSON_from_data_auto ( - &reserve_pub), - "reserve_sigs", sigs); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o, s:o}", + "reserve_pub", + GNUNET_JSON_from_data_auto ( + &reserve_pub), + "reserve_sigs", sigs); } @@ -428,10 +427,10 @@ prepare_pickup (struct PickupContext *pc) response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; break; } - return TMH_RESPONSE_reply_rc (pc->connection, - response_code, - ec, - "Could not determine exchange URL for the given tip id"); + return TALER_MHD_reply_with_error (pc->connection, + response_code, + ec, + "Could not determine exchange URL for the given tip id"); } pc->fo = TMH_EXCHANGES_find_exchange (pc->exchange_url, @@ -440,10 +439,10 @@ prepare_pickup (struct PickupContext *pc) pc); if (NULL == pc->fo) { - return TMH_RESPONSE_reply_rc (pc->connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - TALER_EC_INTERNAL_INVARIANT_FAILURE, - "consult server logs"); + return TALER_MHD_reply_with_error (pc->connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_INVARIANT_FAILURE, + "consult server logs"); } MHD_suspend_connection (pc->connection); return MHD_YES; @@ -566,19 +565,19 @@ MH_handler_tip_pickup (struct TMH_RequestHandler *rh, { GNUNET_JSON_parse_free (spec); json_decref (root); - return TMH_RESPONSE_reply_rc (connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_TIP_PICKUP_EXCHANGE_TOO_MANY_PLANCHETS, - "limit of 1024 planchets exceeded by request"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_TIP_PICKUP_EXCHANGE_TOO_MANY_PLANCHETS, + "limit of 1024 planchets exceeded by request"); } if (0 == pc->planchets_len) { GNUNET_JSON_parse_free (spec); json_decref (root); - return TMH_RESPONSE_reply_rc (connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_PARAMETER_MALFORMED, - "no planchets specified"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "no planchets specified"); } db->preflight (db->cls); pc->planchets = GNUNET_new_array (pc->planchets_len, @@ -643,9 +642,10 @@ MH_handler_tip_pickup_get (struct TMH_RequestHandler *rh, { /* tip_id is required but missing */ GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MISSING, - "tip_id required"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "tip_id required"); } if (GNUNET_OK != @@ -654,9 +654,10 @@ MH_handler_tip_pickup_get (struct TMH_RequestHandler *rh, { /* tip_id has wrong encoding */ GNUNET_break_op (0); - return TMH_RESPONSE_reply_bad_request (connection, - TALER_EC_PARAMETER_MALFORMED, - "tip_id malformed"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "tip_id malformed"); } db->preflight (db->cls); @@ -693,29 +694,29 @@ MH_handler_tip_pickup_get (struct TMH_RequestHandler *rh, response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; break; } - return TMH_RESPONSE_reply_rc (connection, - response_code, - ec, - "Could not determine exchange URL for the given tip id"); + return TALER_MHD_reply_with_error (connection, + response_code, + ec, + "Could not determine exchange URL for the given tip id"); } timestamp_expire = GNUNET_TIME_absolute_add (timestamp, GNUNET_TIME_UNIT_DAYS); - ret = TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:s, s:o, s:o, s:o, s:o, s:o}", - "exchange_url", exchange_url, - "amount", TALER_JSON_from_amount ( - &tip_amount), - "amount_left", TALER_JSON_from_amount ( - &tip_amount_left), - "stamp_created", - GNUNET_JSON_from_time_abs (timestamp), - "stamp_expire", - GNUNET_JSON_from_time_abs ( - timestamp_expire), - "extra", extra); + ret = TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:s, s:o, s:o, s:o, s:o, s:o}", + "exchange_url", exchange_url, + "amount", TALER_JSON_from_amount ( + &tip_amount), + "amount_left", TALER_JSON_from_amount ( + &tip_amount_left), + "stamp_created", + GNUNET_JSON_from_time_abs (timestamp), + "stamp_expire", + GNUNET_JSON_from_time_abs ( + timestamp_expire), + "extra", extra); GNUNET_free (exchange_url); json_decref (extra); diff --git a/src/backend/taler-merchant-httpd_tip-query.c b/src/backend/taler-merchant-httpd_tip-query.c index aad183a7..649d1d31 100644 --- a/src/backend/taler-merchant-httpd_tip-query.c +++ b/src/backend/taler-merchant-httpd_tip-query.c @@ -26,7 +26,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_tip-query.h" #include "taler-merchant-httpd_tip-reserve-helper.h" @@ -103,28 +102,29 @@ generate_final_response (struct TipQueryContext *tqc) "amount overflow, deposited %s but withdrawn %s\n", TALER_amount_to_string (&tqc->ctr.amount_deposited), TALER_amount_to_string (&tqc->ctr.amount_withdrawn)); - return TMH_RESPONSE_reply_internal_error (tqc->ctr.connection, - TALER_EC_TIP_QUERY_RESERVE_HISTORY_ARITHMETIC_ISSUE_INCONSISTENT, - "Exchange returned invalid reserve history (amount overflow)"); + return TALER_MHD_reply_with_error (tqc->ctr.connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TIP_QUERY_RESERVE_HISTORY_ARITHMETIC_ISSUE_INCONSISTENT, + "Exchange returned invalid reserve history (amount overflow)"); } - return TMH_RESPONSE_reply_json_pack (tqc->ctr.connection, - MHD_HTTP_OK, - "{s:o, s:o, s:o, s:o, s:o}", - "reserve_pub", - GNUNET_JSON_from_data_auto ( - &reserve_pub), - "reserve_expiration", - GNUNET_JSON_from_time_abs ( - tqc->ctr.reserve_expiration), - "amount_authorized", - TALER_JSON_from_amount ( - &tqc->ctr.amount_authorized), - "amount_picked_up", - TALER_JSON_from_amount ( - &tqc->ctr.amount_withdrawn), - "amount_available", - TALER_JSON_from_amount ( - &amount_available)); + return TALER_MHD_reply_json_pack (tqc->ctr.connection, + MHD_HTTP_OK, + "{s:o, s:o, s:o, s:o, s:o}", + "reserve_pub", + GNUNET_JSON_from_data_auto ( + &reserve_pub), + "reserve_expiration", + GNUNET_JSON_from_time_abs ( + tqc->ctr.reserve_expiration), + "amount_authorized", + TALER_JSON_from_amount ( + &tqc->ctr.amount_authorized), + "amount_picked_up", + TALER_JSON_from_amount ( + &tqc->ctr.amount_withdrawn), + "amount_available", + TALER_JSON_from_amount ( + &amount_available)); } @@ -195,9 +195,10 @@ MH_handler_tip_query (struct TMH_RequestHandler *rh, GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Instance `%s' not configured for tipping\n", mi->id); - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_TIP_AUTHORIZE_INSTANCE_DOES_NOT_TIP, - "exchange for tipping not configured for the instance"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_TIP_AUTHORIZE_INSTANCE_DOES_NOT_TIP, + "exchange for tipping not configured for the instance"); } tqc->ctr.reserve_priv = mi->tip_reserve; @@ -216,9 +217,10 @@ MH_handler_tip_query (struct TMH_RequestHandler *rh, { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Database hard error on get_authorized_tip_amount\n"); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_TIP_QUERY_DB_ERROR, - "Merchant database error"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TIP_QUERY_DB_ERROR, + "Merchant database error"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { diff --git a/src/backend/taler-merchant-httpd_tip-reserve-helper.c b/src/backend/taler-merchant-httpd_tip-reserve-helper.c index 413eefc8..59f69078 100644 --- a/src/backend/taler-merchant-httpd_tip-reserve-helper.c +++ b/src/backend/taler-merchant-httpd_tip-reserve-helper.c @@ -102,8 +102,8 @@ handle_status (void *cls, { resume_with_response (ctr, MHD_HTTP_NOT_FOUND, - TMH_RESPONSE_make_error (ec, - "Reserve unknown at exchange")); + TALER_MHD_make_error (ec, + "Reserve unknown at exchange")); return; } if (MHD_HTTP_OK != http_status) @@ -111,8 +111,8 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error (ec, - "Exchange returned error code for reserve status")); + TALER_MHD_make_error (ec, + "Exchange returned error code for reserve status")); return; } @@ -121,7 +121,7 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_HISTORY_FAILED_EMPTY, "Exchange returned empty reserve history")); return; @@ -132,7 +132,7 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_HISTORY_INVALID_NO_DEPOSIT, "Exchange returned invalid reserve history")); return; @@ -145,7 +145,7 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_HISTORY_INVALID_CURRENCY, "Exchange returned invalid reserve history")); return; @@ -157,7 +157,7 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_CURRENCY_MISSMATCH, "Exchange currency unexpected")); return; @@ -204,7 +204,7 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_HISTORY_ARITHMETIC_ISSUE_DEPOSIT, "Exchange returned invalid reserve history (amount overflow)")); return; @@ -228,7 +228,7 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_HISTORY_ARITHMETIC_ISSUE_WITHDRAW, "Exchange returned invalid reserve history (amount overflow)")); return; @@ -250,7 +250,7 @@ handle_status (void *cls, GNUNET_break_op (0); resume_with_response (ctr, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_HISTORY_ARITHMETIC_ISSUE_CLOSED, "Exchange returned invalid reserve history (amount overflow)")); return; @@ -293,7 +293,7 @@ exchange_cont (void *cls, _ ("Failed to contact exchange configured for tipping!\n")); resume_with_response (ctr, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TIP_QUERY_RESERVE_STATUS_FAILED_EXCHANGE_DOWN, "Unable to obtain /keys from exchange")); return; @@ -339,7 +339,7 @@ TMH_check_tip_reserve (struct CheckTipReserve *ctr, GNUNET_break (0); resume_with_response (ctr, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_INTERNAL_INVARIANT_FAILURE, "Unable to find exchange handle")); } diff --git a/src/backend/taler-merchant-httpd_tip-reserve-helper.h b/src/backend/taler-merchant-httpd_tip-reserve-helper.h index 54f81ca5..c7a763ff 100644 --- a/src/backend/taler-merchant-httpd_tip-reserve-helper.h +++ b/src/backend/taler-merchant-httpd_tip-reserve-helper.h @@ -26,7 +26,6 @@ #include "taler-merchant-httpd.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_tip-reserve-helper.h" diff --git a/src/backend/taler-merchant-httpd_track-transaction.c b/src/backend/taler-merchant-httpd_track-transaction.c index 7c2c58d0..49eea32d 100644 --- a/src/backend/taler-merchant-httpd_track-transaction.c +++ b/src/backend/taler-merchant-httpd_track-transaction.c @@ -24,10 +24,10 @@ #include <taler/taler_signatures.h> #include <taler/taler_json_lib.h> #include "taler-merchant-httpd.h" +#include "taler_merchant_service.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_track-transaction.h" @@ -129,7 +129,7 @@ make_track_transaction_ok (unsigned int num_transfers, TALER_JSON_from_amount ( &sum)))); } - ret = TMH_RESPONSE_make_json (j_transfers); + ret = TALER_MHD_make_json (j_transfers); json_decref (j_transfers); return ret; } @@ -470,14 +470,14 @@ wire_deposits_cb (void *cls, resume_track_transaction_with_response (tctx, MHD_HTTP_FAILED_DEPENDENCY, - TMH_RESPONSE_make_json_pack ("{s:I, s:I, s:I, s:O}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSACTION_WIRE_TRANSFER_TRACE_ERROR, - "exchange-http-status", - (json_int_t) http_status, - "exchange-code", (json_int_t) ec, - "details", json)); + TALER_MHD_make_json_pack ("{s:I, s:I, s:I, s:O}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSACTION_WIRE_TRANSFER_TRACE_ERROR, + "exchange-http-status", + (json_int_t) http_status, + "exchange-code", (json_int_t) ec, + "details", json)); return; } for (unsigned int i = 0; i<MAX_RETRIES; i++) @@ -625,7 +625,7 @@ wtid_cb (void *cls, (tcc->tctx, MHD_HTTP_ACCEPTED, /* Return verbatim what the exchange said. */ - TMH_RESPONSE_make_json (json)); + TALER_MHD_make_json (json)); return; } @@ -635,7 +635,7 @@ wtid_cb (void *cls, resume_track_transaction_with_response (tcc->tctx, MHD_HTTP_FAILED_DEPENDENCY, - TMH_RESPONSE_make_json_pack + TALER_MHD_make_json_pack ("{s:I, s:I, s:I, s:O}", "code", (json_int_t) TALER_EC_TRACK_TRANSACTION_COIN_TRACE_ERROR, @@ -670,7 +670,7 @@ wtid_cb (void *cls, resume_track_transaction_with_response (tcc->tctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_error + TALER_MHD_make_error (TALER_EC_TRACK_TRANSACTION_DB_FETCH_FAILED, "Fail to query database about proofs")); return; @@ -686,16 +686,16 @@ wtid_cb (void *cls, resume_track_transaction_with_response (tcc->tctx, MHD_HTTP_FAILED_DEPENDENCY, - TMH_RESPONSE_make_json_pack ("{s:I, s:s, s:O, s:o, s:o}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSACTION_CONFLICTING_REPORTS, - "error", - "conflicting transfer data from exchange", - "transaction_tracking_claim", json, - "wtid_tracking_claim", pcc.p_ret, - "coin_pub", GNUNET_JSON_from_data_auto ( - &tcc->coin_pub))); + TALER_MHD_make_json_pack ("{s:I, s:s, s:O, s:o, s:o}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSACTION_CONFLICTING_REPORTS, + "error", + "conflicting transfer data from exchange", + "transaction_tracking_claim", json, + "wtid_tracking_claim", pcc.p_ret, + "coin_pub", GNUNET_JSON_from_data_auto ( + &tcc->coin_pub))); return; } @@ -920,7 +920,7 @@ handle_track_transaction_timeout (void *cls) } resume_track_transaction_with_response (tctx, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_PAY_EXCHANGE_TIMEOUT, "exchange not reachable")); } @@ -1119,9 +1119,10 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, MHD_GET_ARGUMENT_KIND, "order_id"); if (NULL == order_id) - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "order_id"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "order_id"); tctx->mi = mi; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -1140,23 +1141,26 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, if (0 > qs) { GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_TRACK_TRANSACTION_DB_FETCH_TRANSACTION_ERROR, - "Database error finding contract terms"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSACTION_DB_FETCH_TRANSACTION_ERROR, + "Database error finding contract terms"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_PROPOSAL_LOOKUP_NOT_FOUND, - "Given order_id doesn't map to any proposal"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_PROPOSAL_LOOKUP_NOT_FOUND, + "Given order_id doesn't map to any proposal"); if (GNUNET_OK != TALER_JSON_hash (contract_terms, &tctx->h_contract_terms)) { json_decref (contract_terms); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_LOGIC_ERROR, - "Failed to hash contract terms"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_LOGIC_ERROR, + "Failed to hash contract terms"); } { @@ -1180,9 +1184,10 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, GNUNET_break (0); GNUNET_JSON_parse_free (spec); json_decref (contract_terms); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_LOGIC_ERROR, - "Failed to parse contract terms from DB"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_LOGIC_ERROR, + "Failed to parse contract terms from DB"); } json_decref (contract_terms); } @@ -1202,15 +1207,17 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, { GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != tctx->qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_TRACK_TRANSACTION_DB_FETCH_PAYMENT_ERROR, - "Database error: failed to find payment data"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSACTION_DB_FETCH_PAYMENT_ERROR, + "Database error: failed to find payment data"); } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) { - return TMH_RESPONSE_reply_not_found (connection, - TALER_EC_TRACK_TRANSACTION_DB_NO_DEPOSITS_ERROR, - "deposit data not found"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_TRACK_TRANSACTION_DB_NO_DEPOSITS_ERROR, + "deposit data not found"); } *connection_cls = tctx; diff --git a/src/backend/taler-merchant-httpd_track-transfer.c b/src/backend/taler-merchant-httpd_track-transfer.c index f734866b..d647daee 100644 --- a/src/backend/taler-merchant-httpd_track-transfer.c +++ b/src/backend/taler-merchant-httpd_track-transfer.c @@ -27,7 +27,6 @@ #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" -#include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_track-transfer.h" @@ -470,7 +469,7 @@ check_transfer (void *cls, rctx->check_transfer_result = GNUNET_SYSERR; /* Build the `TrackTransferConflictDetails` */ rctx->response - = TMH_RESPONSE_make_json_pack ( + = TALER_MHD_make_json_pack ( "{s:I, s:s, s:o, s:I, s:o, s:o, s:s, s:o, s:o}", "code", (json_int_t) TALER_EC_TRACK_TRANSFER_CONFLICTING_REPORTS, @@ -565,7 +564,7 @@ check_wire_fee (struct TrackTransferContext *rctx, resume_track_transfer_with_response (rctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_json_pack ( + TALER_MHD_make_json_pack ( "{s:I, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:O}", "code", (json_int_t) TALER_EC_TRACK_TRANSFER_JSON_BAD_WIRE_FEE, @@ -633,14 +632,14 @@ wire_transfer_cb (void *cls, resume_track_transfer_with_response (rctx, MHD_HTTP_FAILED_DEPENDENCY, - TMH_RESPONSE_make_json_pack ("{s:I, s:I, s:I, s:O}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSFER_EXCHANGE_ERROR, - "exchange-code", (json_int_t) ec, - "exchange-http-status", - (json_int_t) http_status, - "details", json)); + TALER_MHD_make_json_pack ("{s:I, s:I, s:I, s:O}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSFER_EXCHANGE_ERROR, + "exchange-code", (json_int_t) ec, + "exchange-http-status", + (json_int_t) http_status, + "details", json)); return; } for (unsigned int i = 0; i<MAX_RETRIES; i++) @@ -664,12 +663,12 @@ wire_transfer_cb (void *cls, resume_track_transfer_with_response (rctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_json_pack ("{s:I, s:s}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSFER_DB_STORE_TRANSFER_ERROR, - "details", - "failed to store response from exchange to local database")); + TALER_MHD_make_json_pack ("{s:I, s:s}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSFER_DB_STORE_TRANSFER_ERROR, + "details", + "failed to store response from exchange to local database")); return; } rctx->original_response = json; @@ -711,12 +710,12 @@ wire_transfer_cb (void *cls, resume_track_transfer_with_response (rctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_json_pack ("{s:I, s:s}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSFER_DB_FETCH_DEPOSIT_ERROR, - "details", - "failed to obtain deposit data from local database")); + TALER_MHD_make_json_pack ("{s:I, s:s}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSFER_DB_FETCH_DEPOSIT_ERROR, + "details", + "failed to obtain deposit data from local database")); return; } if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) @@ -736,13 +735,13 @@ wire_transfer_cb (void *cls, resume_track_transfer_with_response (rctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_json_pack ("{s:I, s:s, s:I, s:s}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSFER_DB_INTERNAL_LOGIC_ERROR, - "details", "internal logic error", - "line", (json_int_t) __LINE__, - "file", __FILE__)); + TALER_MHD_make_json_pack ("{s:I, s:s, s:I, s:s}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSFER_DB_INTERNAL_LOGIC_ERROR, + "details", "internal logic error", + "line", (json_int_t) __LINE__, + "file", __FILE__)); return; } if (GNUNET_SYSERR == rctx->check_transfer_result) @@ -778,12 +777,12 @@ wire_transfer_cb (void *cls, resume_track_transfer_with_response (rctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_json_pack ("{s:I, s:s}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSFER_DB_STORE_COIN_ERROR, - "details", - "failed to store response from exchange to local database")); + TALER_MHD_make_json_pack ("{s:I, s:s}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSFER_DB_STORE_COIN_ERROR, + "details", + "failed to store response from exchange to local database")); return; } } @@ -799,14 +798,14 @@ wire_transfer_cb (void *cls, resume_track_transfer_with_response (rctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_error (TALER_EC_TRACK_TRANSFER_JSON_RESPONSE_ERROR, - "Fail to elaborate the response.")); + TALER_MHD_make_error (TALER_EC_TRACK_TRANSFER_JSON_RESPONSE_ERROR, + "Fail to elaborate the response.")); return; } resume_track_transfer_with_response (rctx, MHD_HTTP_OK, - TMH_RESPONSE_make_json (jresponse)); + TALER_MHD_make_json (jresponse)); json_decref (jresponse); } @@ -839,12 +838,12 @@ process_track_transfer_with_exchange (void *cls, resume_track_transfer_with_response (rctx, MHD_HTTP_INTERNAL_SERVER_ERROR, - TMH_RESPONSE_make_json_pack ("{s:I, s:s}", - "code", - (json_int_t) - TALER_EC_TRACK_TRANSFER_REQUEST_ERROR, - "error", - "failed to run /track/transfer on exchange")); + TALER_MHD_make_json_pack ("{s:I, s:s}", + "code", + (json_int_t) + TALER_EC_TRACK_TRANSFER_REQUEST_ERROR, + "error", + "failed to run /track/transfer on exchange")); } } @@ -870,7 +869,7 @@ handle_track_transfer_timeout (void *cls) } resume_track_transfer_with_response (rctx, MHD_HTTP_SERVICE_UNAVAILABLE, - TMH_RESPONSE_make_error ( + TALER_MHD_make_error ( TALER_EC_TRACK_TRANSFER_EXCHANGE_TIMEOUT, "exchange not reachable")); } @@ -898,13 +897,13 @@ proof_cb (void *cls, { rctx->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; rctx->response - = TMH_RESPONSE_make_error (TALER_EC_TRACK_TRANSFER_JSON_RESPONSE_ERROR, - "Fail to elaborate response."); + = TALER_MHD_make_error (TALER_EC_TRACK_TRANSFER_JSON_RESPONSE_ERROR, + "Fail to elaborate response."); return; } rctx->response_code = MHD_HTTP_OK; - rctx->response = TMH_RESPONSE_make_json (transformed_response); + rctx->response = TALER_MHD_make_json (transformed_response); json_decref (transformed_response); } @@ -985,9 +984,10 @@ MH_handler_track_transfer (struct TMH_RequestHandler *rh, MHD_GET_ARGUMENT_KIND, "exchange"); if (NULL == url) - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "exchange"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "exchange"); rctx->url = GNUNET_strdup (url); /* FIXME: change again: we probably don't want the wire_method @@ -1006,9 +1006,10 @@ MH_handler_track_transfer (struct TMH_RequestHandler *rh, "Client needs fixing, see API change for #4943!\n"); } else - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "wire_method"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "wire_method"); } rctx->wire_method = GNUNET_strdup (wire_method); rctx->mi = mi; @@ -1016,18 +1017,20 @@ MH_handler_track_transfer (struct TMH_RequestHandler *rh, MHD_GET_ARGUMENT_KIND, "wtid"); if (NULL == str) - return TMH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - "wtid"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MISSING, + "wtid"); if (GNUNET_OK != GNUNET_STRINGS_string_to_data (str, strlen (str), &rctx->wtid, sizeof (rctx->wtid))) { - return TMH_RESPONSE_reply_arg_invalid (connection, - TALER_EC_PARAMETER_MALFORMED, - "wtid"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "wtid"); } /* Check if reply is already in database! */ @@ -1043,9 +1046,10 @@ MH_handler_track_transfer (struct TMH_RequestHandler *rh, GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); /* Always report on hard error as well to enable diagnostics */ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_TRACK_TRANSFER_DB_FETCH_FAILED, - "Fail to query database about proofs"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_TRACK_TRANSFER_DB_FETCH_FAILED, + "Fail to query database about proofs"); } if (0 != rctx->response_code) { |