merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

commit f0ba12d35661256827ce578ab9bd0420d7a7b7f5
parent d5f14ec8e71bf4bc983535dc74b7af04d651da45
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sun, 19 Apr 2020 11:51:39 +0200

implement DELETE /instances/$ID

Diffstat:
Asrc/backend/taler-merchant-httpd_private-delete-instances-ID.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/backend/taler-merchant-httpd_private-delete-instances-ID.h | 41+++++++++++++++++++++++++++++++++++++++++
Msrc/backenddb/plugin_merchantdb_postgres.c | 56+++++++++++++++++++++++++++++++++++++++++++++++---------
Msrc/include/taler_merchant_service.h | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/include/taler_merchantdb_plugin.h | 8++++----
Msrc/lib/Makefile.am | 7++++---
Dsrc/lib/merchant_api_config_get.c | 254-------------------------------------------------------------------------------
Asrc/lib/merchant_api_delete_instances_ID.c | 260+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/merchant_api_get_config.c | 254+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/merchant_api_get_instances.c | 292+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/lib/merchant_api_instances_get.c | 292-------------------------------------------------------------------------------
11 files changed, 1075 insertions(+), 564 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_private-delete-instances-ID.c b/src/backend/taler-merchant-httpd_private-delete-instances-ID.c @@ -0,0 +1,89 @@ +/* + This file is part of TALER + (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file backend/taler-merchant-httpd_private-delete-instances-ID.c + * @brief implement DELETE /instances/$ID + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler-merchant-httpd_private-delete-instances-ID.h" +#include <taler/taler_json_lib.h> + + +/** + * Handle a DELETE "/instances/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_delete_instances_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + struct TMH_MerchantInstance *mi = hc->instance; + const char *purge; + enum GNUNET_DB_QueryStatus qs; + + GNUNET_assert (NULL != mi); + purge = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "purge"); + if ( (NULL != purge) && + (0 == strcmp (purge, + "yes")) ) + qs = TMH_db->purge_instance (TMH_db->cls, + mi->settings.id); + else + qs = TMH_db->delete_instance_private_key (TMH_db->cls, + mi->settings.id); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_DELETE_INSTANCES_ID_DB_HARD_FAILURE, + "Transaction failed"); + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_INVARIANT_FAILURE, + "Serialization error for single SQL statement"); + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_DELETE_INSTANCES_ID_NO_SUCH_INSTANCE, + ( (NULL != purge) && + (0 == strcmp (purge, + "yes")) ) + ? "Instance unknown" + : "Private key unknown"); + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + return TALER_MHD_reply_static (connection, + MHD_HTTP_NO_CONTENT, + NULL, + NULL, + 0); + } + GNUNET_assert (0); + return MHD_NO; +} + + +/* end of taler-merchant-httpd_private-delete-instances-ID.c */ diff --git a/src/backend/taler-merchant-httpd_private-delete-instances-ID.h b/src/backend/taler-merchant-httpd_private-delete-instances-ID.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file backend/taler-merchant-httpd_private-delete-instances-ID.h + * @brief implement DELETE /instances/$ID/ + * @author Christian Grothoff + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_DELETE_INSTANCES_ID_H +#define TALER_MERCHANT_HTTPD_PRIVATE_DELETE_INSTANCES_ID_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a DELETE "/instances/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_delete_instances_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-delete-instances-ID.h */ +#endif diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c @@ -41,7 +41,8 @@ * @param field name of the database field to fetch amount from * @param amountp[out] pointer to amount to set */ -#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) TALER_PQ_result_spec_amount ( \ +#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) \ + TALER_PQ_result_spec_amount ( \ field,pg->currency,amountp) /** @@ -51,8 +52,8 @@ * @param field name of the database field to fetch amount from * @param amountp[out] pointer to amount to set */ -#define TALER_PQ_RESULT_SPEC_AMOUNT_NBO(field, \ - amountp) TALER_PQ_result_spec_amount_nbo ( \ +#define TALER_PQ_RESULT_SPEC_AMOUNT_NBO(field, amountp) \ + TALER_PQ_result_spec_amount_nbo ( \ field,pg->currency,amountp) @@ -63,7 +64,8 @@ * @param field name of the database field to fetch amount from * @param amountp[out] pointer to amount to set */ -#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) TALER_PQ_result_spec_amount ( \ +#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) \ + TALER_PQ_result_spec_amount ( \ field,pg->currency,amountp) @@ -556,7 +558,8 @@ postgres_insert_instance (void *cls, }; struct GNUNET_PQ_QueryParam params_priv[] = { GNUNET_PQ_query_param_auto_from_type (merchant_priv), - GNUNET_PQ_query_param_string (is->id) + GNUNET_PQ_query_param_string (is->id), + GNUNET_PQ_query_param_end }; enum GNUNET_DB_QueryStatus qs; @@ -610,14 +613,24 @@ postgres_insert_account ( * Delete private key of an instance from our database. * * @param cls closure - * @param merchant_pub public key of the instance + * @param merchant_id identifier of the instance * @return database result code */ static enum GNUNET_DB_QueryStatus postgres_delete_instance_private_key ( void *cls, - const struct TALER_MerchantPublicKeyP *merchant_pub) + const char *merchant_id) { + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (merchant_id), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "delete_key", + params); } @@ -626,13 +639,23 @@ postgres_delete_instance_private_key ( * Highly likely to cause undesired data loss. Use with caution. * * @param cls closure - * @param merchant_pub public key of the instance + * @param merchant_id identifier of the instance * @return database result code */ static enum GNUNET_DB_QueryStatus postgres_purge_instance (void *cls, - const struct TALER_MerchantPublicKeyP *merchant_pub) + const char *merchant_id) { + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (merchant_id), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "purge_instance", + params); } @@ -3585,6 +3608,19 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) " FROM merchant_instances" " WHERE merchant_id=$1", 5), + /* for postgres_delete_instance_private_key() */ + GNUNET_PQ_make_prepare ("delete_key", + "DELETE FROM merchant_keys" + " USING merchant_instances" + " WHERE merchant_keys.merchant_serial" + " = merchant_instances.merchant_serial" + " AND merchant_instances.merchant_id = $1", + 1), + /* for postgres_purge_instance() */ + GNUNET_PQ_make_prepare ("purge_keys", + "DELETE FROM merchant_instances" + " WHERE merchant_instances.merchant_id = $1", + 1), /* OLD API: */ #if 0 GNUNET_PQ_make_prepare ("insert_deposit", @@ -4084,6 +4120,8 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) plugin->lookup_instances = &postgres_lookup_instances; plugin->insert_instance = &postgres_insert_instance; plugin->insert_account = &postgres_insert_account; + plugin->delete_instance_private_key = &postgres_delete_instance_private_key; + plugin->purge_instance = &postgres_purge_instance; /* old API: */ plugin->store_deposit = &postgres_store_deposit; diff --git a/src/include/taler_merchant_service.h b/src/include/taler_merchant_service.h @@ -296,7 +296,7 @@ struct TALER_MERCHANT_InstancesGetHandle; * @param iis array with instance information of length @a iis_length */ typedef void -(*TALER_MERCHANT_InstancesCallback)( +(*TALER_MERCHANT_InstancesGetCallback)( void *cls, const struct TALER_MERCHANT_HttpResponse *hr, unsigned int iis_length, @@ -318,7 +318,7 @@ typedef void struct TALER_MERCHANT_InstancesGetHandle * TALER_MERCHANT_instances_get (struct GNUNET_CURL_Context *ctx, const char *backend_url, - TALER_MERCHANT_InstancesCallback instances_cb, + TALER_MERCHANT_InstancesGetCallback instances_cb, void *instances_cb_cls); @@ -333,6 +333,88 @@ TALER_MERCHANT_instances_get_cancel ( struct TALER_MERCHANT_InstancesGetHandle *igh); +/** + * Handle for a DELETE /instances operation. + */ +struct TALER_MERCHANT_InstancesDeleteHandle; + + +/** + * Function called with the result of the DELETE /instances operation. + * + * @param cls closure + * @param hr HTTP response data + */ +typedef void +(*TALER_MERCHANT_InstancesDeleteCallback)( + void *cls, + const struct TALER_MERCHANT_HttpResponse *hr); + + +/** + * Get the private key of an instance of a backend, thereby disabling the + * instance for future requests. Will preserve the other instance data + * (i.e. for taxation). + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param instance_id which instance should be deleted + * @param instances_cb function to call with the + * backend's return + * @param instances_cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +struct TALER_MERCHANT_InstancesDeleteHandle * +TALER_MERCHANT_instances_delete_instance ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance_id, + TALER_MERCHANT_InstancesDeleteCallback instances_cb, + void *instances_cb_cls); + + +/** + * Purge all data associated with an instance. Use with + * extreme caution. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param instance_id which instance should be deleted + * @param instances_cb function to call with the + * backend's return + * @param instances_cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +struct TALER_MERCHANT_InstancesDeleteHandle * +TALER_MERCHANT_instances_purge_instance ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance_id, + TALER_MERCHANT_InstancesDeleteCallback instances_cb, + void *instances_cb_cls); + + +/** + * Cancel /instances DELETE request. Must not be called by clients after + * the callback was invoked. + * + * @param idh request to cancel. + */ +void +TALER_MERCHANT_instances_delete_cancel ( + struct TALER_MERCHANT_InstancesDeleteHandle *idh); + + +/** + * Cancel /instances DELETE request. Must not be called by clients after + * the callback was invoked. + * + * @param arg request to cancel. + */ +#define TALER_MERCHANT_instances_purge_cancel(arg) \ + TALER_MERCHANT_instances_delete_cancel (arg) + + /* ********************* /refund ************************** */ /** diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h @@ -388,13 +388,13 @@ struct TALER_MERCHANTDB_Plugin * Delete private key of an instance from our database. * * @param cls closure - * @param merchant_pub public key of the instance + * @param merchant_id identifier of the instance * @return database result code */ enum GNUNET_DB_QueryStatus (*delete_instance_private_key)( void *cls, - const struct TALER_MerchantPublicKeyP *merchant_pub); + const char *merchant_id); /** @@ -402,12 +402,12 @@ struct TALER_MERCHANTDB_Plugin * Highly likely to cause undesired data loss. Use with caution. * * @param cls closure - * @param merchant_pub public key of the instance + * @param merchant_id identifier of the instance * @return database result code */ enum GNUNET_DB_QueryStatus (*purge_instance)(void *cls, - const struct TALER_MerchantPublicKeyP *merchant_pub); + const char *merchant_id); /* ****************** OLD API ******************** */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am @@ -19,10 +19,11 @@ libtalermerchanttesting_la_LDFLAGS = \ -no-undefined libtalermerchant_la_SOURCES = \ - merchant_api_check_payment.c \ merchant_api_common.c \ - merchant_api_config_get.c \ - merchant_api_instances_get.c \ + merchant_api_delete_instances_ID.c \ + merchant_api_get_config.c \ + merchant_api_get_instances.c \ + merchant_api_check_payment.c \ merchant_api_history.c \ merchant_api_proposal.c \ merchant_api_proposal_lookup.c \ diff --git a/src/lib/merchant_api_config_get.c b/src/lib/merchant_api_config_get.c @@ -1,254 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014-2018, 2020 Taler Systems SA - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Lesser General Public License as published by the Free Software - Foundation; either version 2.1, 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License along with - TALER; see the file COPYING.LGPL. If not, see - <http://www.gnu.org/licenses/> -*/ -/** - * @file lib/merchant_api_config_get.c - * @brief Implementation of the /config request of the merchant's HTTP API - * @author Christian Grothoff - */ -#include "platform.h" -#include <curl/curl.h> -#include <jansson.h> -#include <microhttpd.h> /* just for HTTP status codes */ -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_curl_lib.h> -#include "taler_merchant_service.h" -#include <taler/taler_json_lib.h> -#include <taler/taler_signatures.h> - -/** - * Which version of the Taler protocol is implemented - * by this library? Used to determine compatibility. - */ -#define MERCHANT_PROTOCOL_CURRENT 0 - -/** - * How many configs are we backwards compatible with? - */ -#define MERCHANT_PROTOCOL_AGE 0 - - -/** - * @brief A handle for /config operations - */ -struct TALER_MERCHANT_ConfigGetHandle -{ - /** - * The url for this request. - */ - char *url; - - /** - * Handle for the request. - */ - struct GNUNET_CURL_Job *job; - - /** - * Function to call with the result. - */ - TALER_MERCHANT_ConfigCallback cb; - - /** - * Closure for @a cb. - */ - void *cb_cls; - - /** - * Reference to the execution context. - */ - struct GNUNET_CURL_Context *ctx; - -}; - - -/** - * Function called when we're done processing the - * HTTP /config request. - * - * @param cls the `struct TALER_MERCHANT_ConfigGetHandle` - * @param response_code HTTP response code, 0 on error - * @param json response body, NULL if not in JSON - */ -static void -handle_config_finished (void *cls, - long response_code, - const void *response) -{ - struct TALER_MERCHANT_ConfigGetHandle *vgh = cls; - const json_t *json = response; - struct TALER_MERCHANT_HttpResponse hr = { - .http_status = (unsigned int) response_code, - .reply = json - }; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got /config response with status code %u\n", - (unsigned int) response_code); - - vgh->job = NULL; - switch (response_code) - { - case MHD_HTTP_OK: - { - struct TALER_MERCHANT_ConfigInformation vi; - enum TALER_MERCHANT_VersionCompatibility vc = - TALER_MERCHANT_VC_PROTOCOL_ERROR; - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_string ("currency", - &vi.currency), - GNUNET_JSON_spec_string ("version", - &vi.version), - GNUNET_JSON_spec_end () - }; - - if (GNUNET_OK != - GNUNET_JSON_parse (json, - spec, - NULL, NULL)) - { - hr.http_status = 0; - hr.ec = TALER_EC_INVALID_RESPONSE; - } - else - { - unsigned int age; - unsigned int revision; - unsigned int current; - - if (3 != sscanf (vi.version, - "%u:%u:%u", - &current, - &revision, - &age)) - { - hr.http_status = 0; - hr.ec = TALER_EC_INVALID_RESPONSE; - } - else - { - vc = TALER_MERCHANT_VC_MATCH; - if (MERCHANT_PROTOCOL_CURRENT < current) - { - vc |= TALER_MERCHANT_VC_NEWER; - if (MERCHANT_PROTOCOL_CURRENT < current - age) - vc |= TALER_MERCHANT_VC_INCOMPATIBLE; - } - if (MERCHANT_PROTOCOL_CURRENT > current) - { - vc |= TALER_MERCHANT_VC_OLDER; - if (MERCHANT_PROTOCOL_CURRENT - MERCHANT_PROTOCOL_AGE > current) - vc |= TALER_MERCHANT_VC_INCOMPATIBLE; - } - } - } - vgh->cb (vgh->cb_cls, - &hr, - &vi, - vc); - TALER_MERCHANT_config_get_cancel (vgh); - return; - } - default: - /* unexpected response code */ - hr.ec = TALER_JSON_get_error_code (json); - hr.hint = TALER_JSON_get_error_hint (json); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Unexpected response code %u/%d\n", - (unsigned int) response_code, - (int) hr.ec); - vgh->cb (vgh->cb_cls, - &hr, - NULL, - TALER_MERCHANT_VC_PROTOCOL_ERROR); - break; - } - TALER_MERCHANT_config_get_cancel (vgh); -} - - -/** - * Get the config data of a merchant. Will connect to the merchant backend - * and obtain information about the backend. The respective information will - * be passed to the @a config_cb once available. - * - * @param ctx the context - * @param backend_url HTTP base URL for the backend - * @param config_cb function to call with the - * backend's config information - * @param config_cb_cls closure for @a config_cb - * @return the config check handle; NULL upon error - */ -struct TALER_MERCHANT_ConfigGetHandle * -TALER_MERCHANT_config_get (struct GNUNET_CURL_Context *ctx, - const char *backend_url, - TALER_MERCHANT_ConfigCallback config_cb, - void *config_cb_cls) -{ - struct TALER_MERCHANT_ConfigGetHandle *vgh; - CURL *eh; - - vgh = GNUNET_new (struct TALER_MERCHANT_ConfigGetHandle); - vgh->ctx = ctx; - vgh->cb = config_cb; - vgh->cb_cls = config_cb_cls; - vgh->url = TALER_url_join (backend_url, - "config", - NULL); - if (NULL == vgh->url) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Could not construct request URL.\n"); - GNUNET_free (vgh); - return NULL; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Requesting URL '%s'\n", - vgh->url); - eh = curl_easy_init (); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_URL, - vgh->url)); - - vgh->job = GNUNET_CURL_job_add (ctx, - eh, - GNUNET_YES, - &handle_config_finished, - vgh); - return vgh; -} - - -/** - * Cancel /config request. Must not be called by clients after - * the callback was invoked. - * - * @param vgh request to cancel. - */ -void -TALER_MERCHANT_config_get_cancel (struct TALER_MERCHANT_ConfigGetHandle *vgh) -{ - if (NULL != vgh->job) - { - GNUNET_CURL_job_cancel (vgh->job); - vgh->job = NULL; - } - GNUNET_free (vgh->url); - GNUNET_free (vgh); -} - - -/* end of merchant_api_config_get.c */ diff --git a/src/lib/merchant_api_delete_instances_ID.c b/src/lib/merchant_api_delete_instances_ID.c @@ -0,0 +1,260 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Lesser General Public License as published by the Free Software + Foundation; either version 2.1, 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with + TALER; see the file COPYING.LGPL. If not, see + <http://www.gnu.org/licenses/> +*/ +/** + * @file lib/merchant_api_delete_instances_ID.c + * @brief Implementation of the GET /instances request of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_merchant_service.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a DELETE /instances/$ID operation. + */ +struct TALER_MERCHANT_InstancesDeleteHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_InstancesDeleteCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP DELETE /instances/$ID request. + * + * @param cls the `struct TALER_MERCHANT_InstancesDeleteHandle` + * @param response_code HTTP response code, 0 on error + * @param json response body, NULL if not in JSON + */ +static void +handle_instances_delete_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_InstancesDeleteHandle *idh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + idh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /instances response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_OK: + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + break; + } + idh->cb (idh->cb_cls, + &hr); + TALER_MERCHANT_instances_delete_cancel (idh); +} + + +/** + * Purge all data associated with an instance. Use with + * extreme caution. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param instance_id which instance should be deleted + * @param instances_cb function to call with the + * backend's return + * @param instances_cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +static struct TALER_MERCHANT_InstancesDeleteHandle * +instances_delete ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance_id, + bool purge, + TALER_MERCHANT_InstancesDeleteCallback instances_cb, + void *instances_cb_cls) +{ + struct TALER_MERCHANT_InstancesDeleteHandle *idh; + CURL *eh; + + idh = GNUNET_new (struct TALER_MERCHANT_InstancesDeleteHandle); + idh->ctx = ctx; + idh->cb = instances_cb; + idh->cb_cls = instances_cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "instances/%s", + instance_id); + if (purge) + idh->url = TALER_url_join (backend_url, + path, + "purge", + "true", + NULL); + else + idh->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == idh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (idh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + idh->url); + eh = curl_easy_init (); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_URL, + idh->url)); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); + idh->job = GNUNET_CURL_job_add (ctx, + eh, + GNUNET_YES, + &handle_instances_delete_finished, + idh); + return idh; +} + + +/** + * Purge all data associated with an instance. Use with + * extreme caution. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param instance_id which instance should be deleted + * @param instances_cb function to call with the + * backend's return + * @param instances_cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +struct TALER_MERCHANT_InstancesDeleteHandle * +TALER_MERCHANT_instances_delete_instance ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance_id, + TALER_MERCHANT_InstancesDeleteCallback instances_cb, + void *instances_cb_cls) +{ + return instances_delete (ctx, + backend_url, + instance_id, + false, + instances_cb, + instances_cb_cls); +} + + +/** + * Purge all data associated with an instance. Use with + * extreme caution. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param instance_id which instance should be deleted + * @param instances_cb function to call with the + * backend's return + * @param instances_cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +struct TALER_MERCHANT_InstancesDeleteHandle * +TALER_MERCHANT_instances_purge_instance ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance_id, + TALER_MERCHANT_InstancesDeleteCallback instances_cb, + void *instances_cb_cls) +{ + return instances_delete (ctx, + backend_url, + instance_id, + true, + instances_cb, + instances_cb_cls); +} + + +/** + * Cancel /instances DELETE request. Must not be called by clients after the + * callback was invoked. + * + * @param idh request to cancel. + */ +void +TALER_MERCHANT_instances_delete_cancel ( + struct TALER_MERCHANT_InstancesDeleteHandle *idh) +{ + if (NULL != idh->job) + GNUNET_CURL_job_cancel (idh->job); + GNUNET_free (idh->url); + GNUNET_free (idh); +} diff --git a/src/lib/merchant_api_get_config.c b/src/lib/merchant_api_get_config.c @@ -0,0 +1,254 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Lesser General Public License as published by the Free Software + Foundation; either version 2.1, 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with + TALER; see the file COPYING.LGPL. If not, see + <http://www.gnu.org/licenses/> +*/ +/** + * @file lib/merchant_api_get_config.c + * @brief Implementation of the /config request of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_merchant_service.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + +/** + * Which version of the Taler protocol is implemented + * by this library? Used to determine compatibility. + */ +#define MERCHANT_PROTOCOL_CURRENT 0 + +/** + * How many configs are we backwards compatible with? + */ +#define MERCHANT_PROTOCOL_AGE 0 + + +/** + * @brief A handle for /config operations + */ +struct TALER_MERCHANT_ConfigGetHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_ConfigCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP /config request. + * + * @param cls the `struct TALER_MERCHANT_ConfigGetHandle` + * @param response_code HTTP response code, 0 on error + * @param json response body, NULL if not in JSON + */ +static void +handle_config_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_ConfigGetHandle *vgh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /config response with status code %u\n", + (unsigned int) response_code); + + vgh->job = NULL; + switch (response_code) + { + case MHD_HTTP_OK: + { + struct TALER_MERCHANT_ConfigInformation vi; + enum TALER_MERCHANT_VersionCompatibility vc = + TALER_MERCHANT_VC_PROTOCOL_ERROR; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("currency", + &vi.currency), + GNUNET_JSON_spec_string ("version", + &vi.version), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) + { + hr.http_status = 0; + hr.ec = TALER_EC_INVALID_RESPONSE; + } + else + { + unsigned int age; + unsigned int revision; + unsigned int current; + + if (3 != sscanf (vi.version, + "%u:%u:%u", + &current, + &revision, + &age)) + { + hr.http_status = 0; + hr.ec = TALER_EC_INVALID_RESPONSE; + } + else + { + vc = TALER_MERCHANT_VC_MATCH; + if (MERCHANT_PROTOCOL_CURRENT < current) + { + vc |= TALER_MERCHANT_VC_NEWER; + if (MERCHANT_PROTOCOL_CURRENT < current - age) + vc |= TALER_MERCHANT_VC_INCOMPATIBLE; + } + if (MERCHANT_PROTOCOL_CURRENT > current) + { + vc |= TALER_MERCHANT_VC_OLDER; + if (MERCHANT_PROTOCOL_CURRENT - MERCHANT_PROTOCOL_AGE > current) + vc |= TALER_MERCHANT_VC_INCOMPATIBLE; + } + } + } + vgh->cb (vgh->cb_cls, + &hr, + &vi, + vc); + TALER_MERCHANT_config_get_cancel (vgh); + return; + } + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + vgh->cb (vgh->cb_cls, + &hr, + NULL, + TALER_MERCHANT_VC_PROTOCOL_ERROR); + break; + } + TALER_MERCHANT_config_get_cancel (vgh); +} + + +/** + * Get the config data of a merchant. Will connect to the merchant backend + * and obtain information about the backend. The respective information will + * be passed to the @a config_cb once available. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param config_cb function to call with the + * backend's config information + * @param config_cb_cls closure for @a config_cb + * @return the config check handle; NULL upon error + */ +struct TALER_MERCHANT_ConfigGetHandle * +TALER_MERCHANT_config_get (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + TALER_MERCHANT_ConfigCallback config_cb, + void *config_cb_cls) +{ + struct TALER_MERCHANT_ConfigGetHandle *vgh; + CURL *eh; + + vgh = GNUNET_new (struct TALER_MERCHANT_ConfigGetHandle); + vgh->ctx = ctx; + vgh->cb = config_cb; + vgh->cb_cls = config_cb_cls; + vgh->url = TALER_url_join (backend_url, + "config", + NULL); + if (NULL == vgh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (vgh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + vgh->url); + eh = curl_easy_init (); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_URL, + vgh->url)); + + vgh->job = GNUNET_CURL_job_add (ctx, + eh, + GNUNET_YES, + &handle_config_finished, + vgh); + return vgh; +} + + +/** + * Cancel /config request. Must not be called by clients after + * the callback was invoked. + * + * @param vgh request to cancel. + */ +void +TALER_MERCHANT_config_get_cancel (struct TALER_MERCHANT_ConfigGetHandle *vgh) +{ + if (NULL != vgh->job) + { + GNUNET_CURL_job_cancel (vgh->job); + vgh->job = NULL; + } + GNUNET_free (vgh->url); + GNUNET_free (vgh); +} + + +/* end of merchant_api_config_get.c */ diff --git a/src/lib/merchant_api_get_instances.c b/src/lib/merchant_api_get_instances.c @@ -0,0 +1,292 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Lesser General Public License as published by the Free Software + Foundation; either version 2.1, 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with + TALER; see the file COPYING.LGPL. If not, see + <http://www.gnu.org/licenses/> +*/ +/** + * @file lib/merchant_api_get_instances.c + * @brief Implementation of the GET /instances request of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_merchant_service.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a GET /instances operation. + */ +struct TALER_MERCHANT_InstancesGetHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_InstancesGetCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Parse instance information from @a ia. + * + * @param ia JSON array (or NULL!) with instance data + * @param igh operation handle + * @return #GNUNET_OK on success + */ +static int +parse_instances (const json_t *ia, + struct TALER_MERCHANT_InstancesGetHandle *igh) +{ + unsigned int iis_len = json_array_size (ia); + struct TALER_MERCHANT_InstanceInformation iis[iis_len]; + size_t index; + json_t *value; + int ret; + + ret = GNUNET_OK; + json_array_foreach (ia, index, value) { + struct TALER_MERCHANT_InstanceInformation *ii = &iis[index]; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("name", + &ii->name), + GNUNET_JSON_spec_string ("instance", + &ii->id), + GNUNET_JSON_spec_fixed_auto ("merchant_pub", + &ii->merchant_pub), + GNUNET_JSON_spec_json ("payment_targets", + &ii->payment_targets), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (value, + spec, + NULL, NULL)) + { + GNUNET_break_op (0); + ret = GNUNET_SYSERR; + continue; + } + if (! json_is_array (ii->payment_targets)) + { + GNUNET_break_op (0); + ret = GNUNET_SYSERR; + break; + } + for (unsigned int i = 0; i<json_array_size (ii->payment_targets); i++) + { + if (! json_is_string (json_array_get (ii->payment_targets, + i))) + { + GNUNET_break_op (0); + ret = GNUNET_SYSERR; + break; + } + } + if (GNUNET_SYSERR == ret) + break; + } + if (GNUNET_OK == ret) + { + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = MHD_HTTP_OK + }; + + igh->cb (igh->cb_cls, + &hr, + iis_len, + iis); + igh->cb = NULL; /* just to be sure */ + } + for (unsigned int i = 0; i<iis_len; i++) + if (NULL != iis[i].payment_targets) + json_decref (iis[i].payment_targets); + return ret; +} + + +/** + * Function called when we're done processing the + * HTTP /instances request. + * + * @param cls the `struct TALER_MERCHANT_InstancesGetHandle` + * @param response_code HTTP response code, 0 on error + * @param json response body, NULL if not in JSON + */ +static void +handle_instances_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_InstancesGetHandle *igh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + igh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /instances response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_OK: + { + json_t *instances; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_json ("instances", + &instances), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) + { + hr.http_status = 0; + hr.ec = TALER_EC_INVALID_RESPONSE; + } + else + { + if ( (! json_is_array (instances)) || + (GNUNET_OK == + parse_instances (instances, + igh)) ) + { + GNUNET_JSON_parse_free (spec); + TALER_MERCHANT_instances_get_cancel (igh); + return; + } + else + { + hr.http_status = 0; + hr.ec = TALER_EC_INVALID_RESPONSE; + } + } + GNUNET_JSON_parse_free (spec); + break; + } + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + break; + } + igh->cb (igh->cb_cls, + &hr, + 0, + NULL); + TALER_MERCHANT_instances_get_cancel (igh); +} + + +/** + * Get the instance data of a backend. Will connect to the merchant backend + * and obtain information about the instances. The respective information will + * be passed to the @a instances_cb once available. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param instances_cb function to call with the + * backend's instances information + * @param instances_cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +struct TALER_MERCHANT_InstancesGetHandle * +TALER_MERCHANT_instances_get (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + TALER_MERCHANT_InstancesGetCallback instances_cb, + void *instances_cb_cls) +{ + struct TALER_MERCHANT_InstancesGetHandle *igh; + CURL *eh; + + igh = GNUNET_new (struct TALER_MERCHANT_InstancesGetHandle); + igh->ctx = ctx; + igh->cb = instances_cb; + igh->cb_cls = instances_cb_cls; + igh->url = TALER_url_join (backend_url, + "instances", + NULL); + if (NULL == igh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (igh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + igh->url); + eh = curl_easy_init (); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_URL, + igh->url)); + igh->job = GNUNET_CURL_job_add (ctx, + eh, + GNUNET_YES, + &handle_instances_finished, + igh); + return igh; +} + + +/** + * Cancel /instances request. Must not be called by clients after + * the callback was invoked. + * + * @param igh request to cancel. + */ +void +TALER_MERCHANT_instances_get_cancel ( + struct TALER_MERCHANT_InstancesGetHandle *igh) +{ + if (NULL != igh->job) + GNUNET_CURL_job_cancel (igh->job); + GNUNET_free (igh->url); + GNUNET_free (igh); +} diff --git a/src/lib/merchant_api_instances_get.c b/src/lib/merchant_api_instances_get.c @@ -1,292 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014-2018, 2020 Taler Systems SA - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Lesser General Public License as published by the Free Software - Foundation; either version 2.1, 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License along with - TALER; see the file COPYING.LGPL. If not, see - <http://www.gnu.org/licenses/> -*/ -/** - * @file lib/merchant_api_instances_get.c - * @brief Implementation of the GET /instances request of the merchant's HTTP API - * @author Christian Grothoff - */ -#include "platform.h" -#include <curl/curl.h> -#include <jansson.h> -#include <microhttpd.h> /* just for HTTP status codes */ -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_curl_lib.h> -#include "taler_merchant_service.h" -#include <taler/taler_json_lib.h> -#include <taler/taler_signatures.h> - - -/** - * Handle for a GET /instances operation. - */ -struct TALER_MERCHANT_InstancesGetHandle -{ - /** - * The url for this request. - */ - char *url; - - /** - * Handle for the request. - */ - struct GNUNET_CURL_Job *job; - - /** - * Function to call with the result. - */ - TALER_MERCHANT_InstancesCallback cb; - - /** - * Closure for @a cb. - */ - void *cb_cls; - - /** - * Reference to the execution context. - */ - struct GNUNET_CURL_Context *ctx; - -}; - - -/** - * Parse instance information from @a ia. - * - * @param ia JSON array (or NULL!) with instance data - * @param igh operation handle - * @return #GNUNET_OK on success - */ -static int -parse_instances (const json_t *ia, - struct TALER_MERCHANT_InstancesGetHandle *igh) -{ - unsigned int iis_len = json_array_size (ia); - struct TALER_MERCHANT_InstanceInformation iis[iis_len]; - size_t index; - json_t *value; - int ret; - - ret = GNUNET_OK; - json_array_foreach (ia, index, value) { - struct TALER_MERCHANT_InstanceInformation *ii = &iis[index]; - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_string ("name", - &ii->name), - GNUNET_JSON_spec_string ("instance", - &ii->id), - GNUNET_JSON_spec_fixed_auto ("merchant_pub", - &ii->merchant_pub), - GNUNET_JSON_spec_json ("payment_targets", - &ii->payment_targets), - GNUNET_JSON_spec_end () - }; - - if (GNUNET_OK != - GNUNET_JSON_parse (value, - spec, - NULL, NULL)) - { - GNUNET_break_op (0); - ret = GNUNET_SYSERR; - continue; - } - if (! json_is_array (ii->payment_targets)) - { - GNUNET_break_op (0); - ret = GNUNET_SYSERR; - break; - } - for (unsigned int i = 0; i<json_array_size (ii->payment_targets); i++) - { - if (! json_is_string (json_array_get (ii->payment_targets, - i))) - { - GNUNET_break_op (0); - ret = GNUNET_SYSERR; - break; - } - } - if (GNUNET_SYSERR == ret) - break; - } - if (GNUNET_OK == ret) - { - struct TALER_MERCHANT_HttpResponse hr = { - .http_status = MHD_HTTP_OK - }; - - igh->cb (igh->cb_cls, - &hr, - iis_len, - iis); - igh->cb = NULL; /* just to be sure */ - } - for (unsigned int i = 0; i<iis_len; i++) - if (NULL != iis[i].payment_targets) - json_decref (iis[i].payment_targets); - return ret; -} - - -/** - * Function called when we're done processing the - * HTTP /instances request. - * - * @param cls the `struct TALER_MERCHANT_InstancesGetHandle` - * @param response_code HTTP response code, 0 on error - * @param json response body, NULL if not in JSON - */ -static void -handle_instances_finished (void *cls, - long response_code, - const void *response) -{ - struct TALER_MERCHANT_InstancesGetHandle *igh = cls; - const json_t *json = response; - struct TALER_MERCHANT_HttpResponse hr = { - .http_status = (unsigned int) response_code, - .reply = json - }; - - igh->job = NULL; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got /instances response with status code %u\n", - (unsigned int) response_code); - switch (response_code) - { - case MHD_HTTP_OK: - { - json_t *instances; - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("instances", - &instances), - GNUNET_JSON_spec_end () - }; - - if (GNUNET_OK != - GNUNET_JSON_parse (json, - spec, - NULL, NULL)) - { - hr.http_status = 0; - hr.ec = TALER_EC_INVALID_RESPONSE; - } - else - { - if ( (! json_is_array (instances)) || - (GNUNET_OK == - parse_instances (instances, - igh)) ) - { - GNUNET_JSON_parse_free (spec); - TALER_MERCHANT_instances_get_cancel (igh); - return; - } - else - { - hr.http_status = 0; - hr.ec = TALER_EC_INVALID_RESPONSE; - } - } - GNUNET_JSON_parse_free (spec); - break; - } - default: - /* unexpected response code */ - hr.ec = TALER_JSON_get_error_code (json); - hr.hint = TALER_JSON_get_error_hint (json); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Unexpected response code %u/%d\n", - (unsigned int) response_code, - (int) hr.ec); - break; - } - igh->cb (igh->cb_cls, - &hr, - 0, - NULL); - TALER_MERCHANT_instances_get_cancel (igh); -} - - -/** - * Get the instance data of a backend. Will connect to the merchant backend - * and obtain information about the instances. The respective information will - * be passed to the @a instances_cb once available. - * - * @param ctx the context - * @param backend_url HTTP base URL for the backend - * @param instances_cb function to call with the - * backend's instances information - * @param instances_cb_cls closure for @a config_cb - * @return the instances handle; NULL upon error - */ -struct TALER_MERCHANT_InstancesGetHandle * -TALER_MERCHANT_instances_get (struct GNUNET_CURL_Context *ctx, - const char *backend_url, - TALER_MERCHANT_InstancesCallback instances_cb, - void *instances_cb_cls) -{ - struct TALER_MERCHANT_InstancesGetHandle *igh; - CURL *eh; - - igh = GNUNET_new (struct TALER_MERCHANT_InstancesGetHandle); - igh->ctx = ctx; - igh->cb = instances_cb; - igh->cb_cls = instances_cb_cls; - igh->url = TALER_url_join (backend_url, - "instances", - NULL); - if (NULL == igh->url) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Could not construct request URL.\n"); - GNUNET_free (igh); - return NULL; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Requesting URL '%s'\n", - igh->url); - eh = curl_easy_init (); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_URL, - igh->url)); - igh->job = GNUNET_CURL_job_add (ctx, - eh, - GNUNET_YES, - &handle_instances_finished, - igh); - return igh; -} - - -/** - * Cancel /instances request. Must not be called by clients after - * the callback was invoked. - * - * @param igh request to cancel. - */ -void -TALER_MERCHANT_instances_get_cancel ( - struct TALER_MERCHANT_InstancesGetHandle *igh) -{ - if (NULL != igh->job) - GNUNET_CURL_job_cancel (igh->job); - GNUNET_free (igh->url); - GNUNET_free (igh); -}