more libtalermhd refactoring

This commit is contained in:
Christian Grothoff 2019-11-23 19:29:50 +01:00
parent e8a88392da
commit 7c11a822ba
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
4 changed files with 178 additions and 145 deletions

View File

@ -27,6 +27,7 @@
#include <microhttpd.h>
#include <pthread.h>
#include "taler_json_lib.h"
#include "taler_mhd_lib.h"
#include "taler-exchange-httpd_parsing.h"
#include "taler-exchange-httpd_payback.h"
#include "taler-exchange-httpd_responses.h"
@ -68,11 +69,12 @@ reply_payback_refresh_success (struct MHD_Connection *connection,
&pub,
&sig))
{
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_EXCHANGE_BAD_CONFIGURATION,
"no keys");
}
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:o, s:o, s:o, s:o, s:o}",
"old_coin_pub",
@ -122,11 +124,12 @@ reply_payback_success (struct MHD_Connection *connection,
&pub,
&sig))
{
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_EXCHANGE_BAD_CONFIGURATION,
"no keys");
}
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:o, s:o, s:o, s:o, s:o}",
"reserve_pub",

View File

@ -29,6 +29,7 @@
#include <microhttpd.h>
#include <pthread.h>
#include "taler_json_lib.h"
#include "taler_mhd_lib.h"
#include "taler-exchange-httpd_parsing.h"
#include "taler-exchange-httpd_refund.h"
#include "taler-exchange-httpd_responses.h"
@ -66,17 +67,17 @@ reply_refund_success (struct MHD_Connection *connection,
&pub,
&sig))
{
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_EXCHANGE_BAD_CONFIGURATION,
"no keys");
}
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:s, s:o, s:o}",
"status", "REFUND_OK",
"sig", GNUNET_JSON_from_data_auto (&sig),
"pub", GNUNET_JSON_from_data_auto (
&pub));
"pub", GNUNET_JSON_from_data_auto (&pub));
}
@ -94,10 +95,10 @@ reply_refund_failure (struct MHD_Connection *connection,
unsigned int response_code,
enum TALER_ErrorCode ec)
{
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
response_code,
"{s:s, s:I}",
"status", "refund failure",
"hint", "refund failure",
"code", (json_int_t) ec);
}
@ -114,10 +115,10 @@ static int
reply_refund_conflict (struct MHD_Connection *connection,
const struct TALER_EXCHANGEDB_TransactionList *tl)
{
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_CONFLICT,
"{s:s, s:I, s:o}",
"status", "conflicting refund",
"hint", "conflicting refund",
"code",
(json_int_t) TALER_EC_REFUND_CONFLICT,
"history",
@ -259,8 +260,10 @@ refund_transaction (void *cls,
TALER_LOG_WARNING ("Deposit to /refund was not found\n");
TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
tl);
*mhd_ret = TEH_RESPONSE_reply_transaction_unknown (connection,
TALER_EC_REFUND_DEPOSIT_NOT_FOUND);
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_NOT_FOUND,
TALER_EC_REFUND_DEPOSIT_NOT_FOUND,
"deposit unknown");
return GNUNET_DB_STATUS_HARD_ERROR;
}
/* handle if conflicting refund found */
@ -309,7 +312,8 @@ refund_transaction (void *cls,
GNUNET_break (0);
TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
tl);
*mhd_ret = TEH_RESPONSE_reply_internal_error (connection,
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_REFUND_DB_INCONSISTENT,
"database inconsistent");
return qs;
@ -349,7 +353,8 @@ refund_transaction (void *cls,
TALER_LOG_ERROR ("Lacking keys to operate\n");
TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
tl);
*mhd_ret = TEH_RESPONSE_reply_internal_error (connection,
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_EXCHANGE_BAD_CONFIGURATION,
"no keys");
return GNUNET_DB_STATUS_HARD_ERROR;
@ -367,9 +372,10 @@ refund_transaction (void *cls,
TEH_KS_release (mks);
TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
tl);
*mhd_ret = TEH_RESPONSE_reply_with_error (connection,
*mhd_ret = TALER_MHD_reply_with_error (connection,
ec,
hc);
hc,
"denomination not found, but coin known");
return GNUNET_DB_STATUS_HARD_ERROR;
}
TALER_amount_ntoh (&expect_fee,
@ -382,7 +388,8 @@ refund_transaction (void *cls,
{
TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
tl);
*mhd_ret = TEH_RESPONSE_reply_arg_invalid (connection,
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_REFUND_FEE_TOO_LOW,
"refund_fee");
return GNUNET_DB_STATUS_HARD_ERROR;
@ -402,8 +409,10 @@ refund_transaction (void *cls,
if (GNUNET_DB_STATUS_HARD_ERROR == qs)
{
TALER_LOG_WARNING ("Failed to store /refund information in database\n");
*mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_REFUND_STORE_DB_ERROR);
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_REFUND_STORE_DB_ERROR,
"could not persist store information");
return qs;
}
/* Success or soft failure */
@ -443,7 +452,8 @@ verify_and_execute_refund (struct MHD_Connection *connection,
&refund->refund_fee) )
{
GNUNET_break_op (0);
return TEH_RESPONSE_reply_arg_invalid (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_REFUND_FEE_CURRENCY_MISSMATCH,
"refund_fee");
}
@ -451,7 +461,8 @@ verify_and_execute_refund (struct MHD_Connection *connection,
&refund->refund_fee) )
{
GNUNET_break_op (0);
return TEH_RESPONSE_reply_arg_invalid (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_REFUND_FEE_ABOVE_AMOUNT,
"refund_amount");
}
@ -462,7 +473,8 @@ verify_and_execute_refund (struct MHD_Connection *connection,
&refund->merchant_pub.eddsa_pub))
{
TALER_LOG_WARNING ("Invalid signature on /refund request\n");
return TEH_RESPONSE_reply_signature_invalid (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_REFUND_MERCHANT_SIGNATURE_INVALID,
"merchant_sig");
}

View File

@ -23,6 +23,7 @@
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include <jansson.h>
#include "taler_mhd_lib.h"
#include "taler-exchange-httpd_reserve_status.h"
#include "taler-exchange-httpd_parsing.h"
#include "taler-exchange-httpd_responses.h"
@ -47,11 +48,12 @@ reply_reserve_status_success (struct MHD_Connection *connection,
json_history = TEH_RESPONSE_compile_reserve_history (rh,
&balance);
if (NULL == json_history)
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_RESERVE_STATUS_DB_ERROR,
"balance calculation failure");
json_balance = TALER_JSON_from_amount (&balance);
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:o, s:o}",
"balance", json_balance,
@ -155,7 +157,7 @@ TEH_RESERVE_handler_reserve_status (struct TEH_RequestHandler *rh,
/* generate proper response */
if (NULL == rsc.rh)
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_NOT_FOUND,
"{s:s, s:s, s:I}",
"error", "Reserve not found",

View File

@ -1,6 +1,6 @@
/*
This file is part of TALER
Copyright (C) 2014-2017 GNUnet e.V.
Copyright (C) 2014-2019 Taler Systems SA
TALER is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@ -16,7 +16,6 @@
Public License along with TALER; see the file COPYING. If not,
see <http://www.gnu.org/licenses/>
*/
/**
* @file taler-exchange-httpd_reserve_withdraw.c
* @brief Handle /reserve/withdraw requests
@ -27,6 +26,7 @@
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include <jansson.h>
#include "taler_mhd_lib.h"
#include "taler-exchange-httpd_reserve_withdraw.h"
#include "taler-exchange-httpd_parsing.h"
#include "taler-exchange-httpd_responses.h"
@ -65,12 +65,13 @@ reply_reserve_withdraw_insufficient_funds (struct MHD_Connection *connection,
/* Address the case where the ptr is not null, but
* it fails "internally" to dump as string (= corrupted). */
|| (0 == json_dumpb (json_history, NULL, 0, 0)))
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_HISTORY_DB_ERROR_INSUFFICIENT_FUNDS,
"balance calculation failure");
json_balance = TALER_JSON_from_amount (&balance);
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_FORBIDDEN,
"{s:s, s:I, s:o, s:o}",
"error", "Insufficient funds",
@ -98,7 +99,7 @@ reply_reserve_withdraw_success (struct MHD_Connection *connection,
json_t *sig_json;
sig_json = GNUNET_JSON_from_rsa_signature (collectable->sig.rsa_signature);
return TEH_RESPONSE_reply_json_pack (connection,
return TALER_MHD_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:o}",
"ev_sig", sig_json);
@ -209,8 +210,10 @@ withdraw_transaction (void *cls,
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
if (GNUNET_DB_STATUS_HARD_ERROR == qs)
*mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_WITHDRAW_DB_FETCH_ERROR);
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_DB_FETCH_ERROR,
"failed to fetch withdraw data");
wc->collectable.sig = denom_sig;
return qs;
}
@ -252,13 +255,16 @@ withdraw_transaction (void *cls,
if (0 > qs)
{
if (GNUNET_DB_STATUS_HARD_ERROR == qs)
*mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_WITHDRAW_DB_FETCH_ERROR);
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_DB_FETCH_ERROR,
"failed to fetch reserve data");
return qs;
}
if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
{
*mhd_ret = TEH_RESPONSE_reply_arg_unknown (connection,
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_NOT_FOUND,
TALER_EC_WITHDRAW_RESERVE_UNKNOWN,
"reserve_pub");
return GNUNET_DB_STATUS_HARD_ERROR;
@ -286,8 +292,10 @@ withdraw_transaction (void *cls,
if (NULL == rh)
{
if (GNUNET_DB_STATUS_HARD_ERROR == qs)
*mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_WITHDRAW_DB_FETCH_ERROR);
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_DB_FETCH_ERROR,
"failed to fetch reserve history");
return GNUNET_DB_STATUS_HARD_ERROR;
}
*mhd_ret = reply_reserve_withdraw_insufficient_funds (connection,
@ -308,9 +316,10 @@ withdraw_transaction (void *cls,
if (NULL == wc->collectable.sig.rsa_signature)
{
GNUNET_break (0);
*mhd_ret = TEH_RESPONSE_reply_internal_error (connection,
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_SIGNATURE_FAILED,
"Internal error");
"Failed to create signature");
return GNUNET_DB_STATUS_HARD_ERROR;
}
}
@ -330,8 +339,10 @@ withdraw_transaction (void *cls,
{
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
if (GNUNET_DB_STATUS_HARD_ERROR == qs)
*mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_WITHDRAW_DB_STORE_ERROR);
*mhd_ret = TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_DB_STORE_ERROR,
"failed to persist withdraw data");
return qs;
}
return qs;
@ -403,7 +414,8 @@ TEH_RESERVE_handler_reserve_withdraw (struct TEH_RequestHandler *rh,
{
TALER_LOG_ERROR ("Lacking keys to operate\n");
GNUNET_JSON_parse_free (spec);
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_EXCHANGE_BAD_CONFIGURATION,
"no keys");
}
@ -416,9 +428,10 @@ TEH_RESERVE_handler_reserve_withdraw (struct TEH_RequestHandler *rh,
{
GNUNET_JSON_parse_free (spec);
TEH_KS_release (wc.key_state);
return TEH_RESPONSE_reply_with_error (connection,
return TALER_MHD_reply_with_error (connection,
ec,
hc);
hc,
"could not find denomination key");
}
GNUNET_assert (NULL != wc.dki->denom_priv.rsa_private_key);
TALER_amount_ntoh (&amount,
@ -432,7 +445,8 @@ TEH_RESERVE_handler_reserve_withdraw (struct TEH_RequestHandler *rh,
{
GNUNET_JSON_parse_free (spec);
TEH_KS_release (wc.key_state);
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_AMOUNT_FEE_OVERFLOW,
"amount overflow for value plus withdraw fee");
}
@ -460,7 +474,8 @@ TEH_RESERVE_handler_reserve_withdraw (struct TEH_RequestHandler *rh,
"Client supplied invalid signature for /reserve/withdraw request\n");
GNUNET_JSON_parse_free (spec);
TEH_KS_release (wc.key_state);
return TEH_RESPONSE_reply_signature_invalid (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_WITHDRAW_RESERVE_SIGNATURE_INVALID,
"reserve_sig");
}
@ -476,9 +491,10 @@ TEH_RESERVE_handler_reserve_withdraw (struct TEH_RequestHandler *rh,
GNUNET_break (0);
GNUNET_JSON_parse_free (spec);
TEH_KS_release (wc.key_state);
return TEH_RESPONSE_reply_internal_error (connection,
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_WITHDRAW_SIGNATURE_FAILED,
"Internal error");
"Failed to sign");
}
#endif