merchant

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

commit 49129bbcb160f01833e1c328b58ea5d7eb548423
parent bee130c6cad86b55514b680ecc02a887aada68a2
Author: Christian Grothoff <christian@grothoff.org>
Date:   Fri, 17 Apr 2020 20:30:45 +0200

implement GET /instances

Diffstat:
Msrc/backend/Makefile.am | 3++-
Msrc/backend/taler-merchant-httpd.c | 34+++++++++++++++++-----------------
Msrc/backend/taler-merchant-httpd.h | 7++++++-
Msrc/backend/taler-merchant-httpd_private-get-instances.c | 121++++++++++++++++++++++++++++---------------------------------------------------
Asrc/backend/taler-merchant-httpd_private-get-instances.h | 41+++++++++++++++++++++++++++++++++++++++++
5 files changed, 109 insertions(+), 97 deletions(-)

diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am @@ -22,7 +22,8 @@ taler_merchant_httpd_SOURCES = \ taler-merchant-httpd_auditors.c taler-merchant-httpd_auditors.h \ taler-merchant-httpd_config.c taler-merchant-httpd_config.h \ taler-merchant-httpd_exchanges.c taler-merchant-httpd_exchanges.h \ - taler-merchant-httpd_mhd.c taler-merchant-httpd_mhd.h + taler-merchant-httpd_mhd.c taler-merchant-httpd_mhd.h \ + taler-merchant-httpd_private-get-instances.c taler-merchant-httpd_private-get-instances.h DEAD = \ taler-merchant-httpd_check-payment.c taler-merchant-httpd_check-payment.h \ diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c @@ -22,19 +22,13 @@ * @author Florian Dold */ #include "platform.h" -#include <microhttpd.h> -#include <gnunet/gnunet_util_lib.h> -#include <taler/taler_util.h> -#include <taler/taler_json_lib.h> -#include <taler/taler_mhd_lib.h> #include <taler/taler_bank_service.h> #include <taler/taler_exchange_service.h> -#include "taler_merchantdb_lib.h" -#include "taler-merchant-httpd.h" #include "taler-merchant-httpd_auditors.h" +#include "taler-merchant-httpd_config.h" #include "taler-merchant-httpd_exchanges.h" #include "taler-merchant-httpd_mhd.h" -#include "taler-merchant-httpd_config.h" +#include "taler-merchant-httpd_private-get-instances.h" /** * Backlog for listen operation on unix-domain sockets. @@ -62,7 +56,7 @@ struct TALER_MERCHANTDB_Plugin *TMH_db; * just a string that identifies a merchant instance. When a frontend * needs to specify an instance to the backend, it does so by 'id' */ -static struct GNUNET_CONTAINER_MultiHashMap *by_id_map; +struct GNUNET_CONTAINER_MultiHashMap *TMH_by_id_map; /** * The port we are running on @@ -443,13 +437,13 @@ do_shutdown (void *cls) GNUNET_CONTAINER_multihashmap_destroy (payment_trigger_map); payment_trigger_map = NULL; } - if (NULL != by_id_map) + if (NULL != TMH_by_id_map) { - GNUNET_CONTAINER_multihashmap_iterate (by_id_map, + GNUNET_CONTAINER_multihashmap_iterate (TMH_by_id_map, &instance_free_cb, NULL); - GNUNET_CONTAINER_multihashmap_destroy (by_id_map); - by_id_map = NULL; + GNUNET_CONTAINER_multihashmap_destroy (TMH_by_id_map); + TMH_by_id_map = NULL; } } @@ -622,7 +616,7 @@ lookup_instance (const char *instance_id) instance_id); /* We're fine if that returns NULL, the calling routine knows how to handle that */ - return GNUNET_CONTAINER_multihashmap_get (by_id_map, + return GNUNET_CONTAINER_multihashmap_get (TMH_by_id_map, &h_instance); } @@ -650,7 +644,7 @@ TMH_add_instance (struct TMH_MerchantInstance *mi) "Looking for by-id key %s of `%s' in hashmap\n", GNUNET_h2s (&h_instance), id); - ret = GNUNET_CONTAINER_multihashmap_put (by_id_map, + ret = GNUNET_CONTAINER_multihashmap_put (TMH_by_id_map, &h_instance, mi, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); @@ -728,6 +722,12 @@ url_handler (void *cls, .handler = &TMH_MHD_handler_agpl_redirect }, { + .url_prefix = "/instances", + .method = MHD_HTTP_METHOD_GET, + .skip_instance = true, + .handler = &TMH_private_get_instances + }, + { NULL } }; @@ -1109,8 +1109,8 @@ run (void *cls, return; } if (NULL == - (by_id_map = GNUNET_CONTAINER_multihashmap_create (1, - GNUNET_NO))) + (TMH_by_id_map = GNUNET_CONTAINER_multihashmap_create (1, + GNUNET_NO))) { GNUNET_SCHEDULER_shutdown (); return; diff --git a/src/backend/taler-merchant-httpd.h b/src/backend/taler-merchant-httpd.h @@ -23,7 +23,6 @@ #include "platform.h" #include "taler_merchantdb_lib.h" -#include <microhttpd.h> #include <taler/taler_mhd_lib.h> #include <gnunet/gnunet_mhd_compat.h> @@ -359,6 +358,12 @@ extern int TMH_force_audit; */ extern struct TALER_MERCHANTDB_Plugin *TMH_db; +/** + * Hashmap pointing at merchant instances by 'id'. An 'id' is + * just a string that identifies a merchant instance. When a frontend + * needs to specify an instance to the backend, it does so by 'id' + */ +extern struct GNUNET_CONTAINER_MultiHashMap *TMH_by_id_map; /** * Kick MHD to run now, to be called after MHD_resume_connection(). diff --git a/src/backend/taler-merchant-httpd_private-get-instances.c b/src/backend/taler-merchant-httpd_private-get-instances.c @@ -14,68 +14,52 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ /** - * @file backend/taler-merchant-httpd_config.c - * @brief implement API for querying configuration data of the backend - * @author Florian Dold + * @file backend/taler-merchant-httpd_private-get-instances.c + * @brief implement GET /instances + * @author Christian Grothoff */ #include "platform.h" -#include <jansson.h> -#include <taler/taler_util.h> -#include <taler/taler_json_lib.h> -#include "taler-merchant-httpd.h" -#include "taler-merchant-httpd_mhd.h" -#include "taler-merchant-httpd_exchanges.h" - +#include "taler-merchant-httpd_private-get-instances.h" /** - * Taler protocol version in the format CURRENT:REVISION:AGE - * as used by GNU libtool. See - * https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html - * - * Please be very careful when updating and follow - * https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info - * precisely. Note that this version has NOTHING to do with the - * release version, and the format is NOT the same that semantic - * versioning uses either. + * Add merchant instance to our JSON array. * - * When changing this version, you likely want to also update - * #MERCHANT_PROTOCOL_CURRENT and #MERCHANT_PROTOCOL_AGE in - * merchant_api_config.c! + * @param cls a `json_t *` JSON array to build + * @param key unused + * @param value a `struct TMH_MerchantInstance *` + * @return #GNUNET_OK (continue to iterate) */ -#define MERCHANT_PROTOCOL_VERSION "0:0:0" - - static int add_instance (void *cls, const struct GNUNET_HashCode *key, void *value) { json_t *ja = cls; - struct MerchantInstance *mi = value; - char *url; + struct TMH_MerchantInstance *mi = value; json_t *pta; + (void) key; /* Compile array of all unique wire methods supported by this instance */ pta = json_array (); GNUNET_assert (NULL != pta); - for (struct WireMethod *wm = mi->wm_head; + for (struct TMH_WireMethod *wm = mi->wm_head; NULL != wm; wm = wm->next) { - int duplicate = GNUNET_NO; + bool duplicate = false; if (! wm->active) break; /* Yes, O(n^2), but really how many bank accounts can an instance realistically have for this to matter? */ - for (struct WireMethod *pm = mi->wm_head; + for (struct TMH_WireMethod *pm = mi->wm_head; pm != wm; pm = pm->next) if (0 == strcasecmp (pm->wire_method, wm->wire_method)) { - duplicate = GNUNET_YES; + duplicate = true; break; } if (duplicate) @@ -84,77 +68,58 @@ add_instance (void *cls, json_array_append_new (pta, json_string (wm->wire_method))); } - GNUNET_asprintf (&url, - "/%s/", - mi->id); GNUNET_assert (0 == json_array_append_new ( ja, json_pack ( - (NULL != mi->tip_exchange) - ? "{s:s, s:s, s:o, s:o, s:s}" - : "{s:s, s:s, s:o, s:o}", + "{s:s, s:s, s:o, s:o}", "name", mi->name, - "backend_base_url", - url, + "instance", + mi->id, "merchant_pub", GNUNET_JSON_from_data_auto (&mi->pubkey), "payment_targets", - pta, - /* optional: */ - "tipping_exchange_baseurl", - mi->tip_exchange))); - GNUNET_free (url); + pta))); return GNUNET_OK; } /** - * Handle a "/config" request. + * Handle a GET "/instances" request. * * @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 + * @param[in,out] hc context with further information about the request * @return MHD result code */ MHD_RESULT -MH_handler_config (struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size, - struct MerchantInstance *mi) +TMH_private_get_instances (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) { - static struct MHD_Response *response; - - (void) rh; - (void) connection_cls; - (void) upload_data; - (void) upload_data_size; - (void) mi; - if (NULL == response) + struct MHD_Response *response; + json_t *ia; + + (void) hc; + ia = json_array (); + GNUNET_assert (NULL != ia); + GNUNET_CONTAINER_multihashmap_iterate (TMH_by_id_map, + &add_instance, + ia); + response = TALER_MHD_make_json_pack ("{s:o}", + "instances", ia); + GNUNET_assert (NULL != response); { - json_t *ia; - - ia = json_array (); - GNUNET_assert (NULL != ia); - GNUNET_CONTAINER_multihashmap_iterate (by_id_map, - &add_instance, - ia); - response = TALER_MHD_make_json_pack ("{s:s, s:s, s:o}", - "currency", TMH_currency, - "version", MERCHANT_PROTOCOL_VERSION, - "instances", ia); + MHD_RESULT ret; + ret = MHD_queue_response (connection, + MHD_HTTP_OK, + response); + MHD_destroy_response (response); + return ret; } - return MHD_queue_response (connection, - MHD_HTTP_OK, - response); } -/* end of taler-merchant-httpd_config.c */ +/* end of taler-merchant-httpd_private-get-instances.c */ diff --git a/src/backend/taler-merchant-httpd_private-get-instances.h b/src/backend/taler-merchant-httpd_private-get-instances.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-get-instances.h + * @brief implement GET /instances + * @author Christian Grothoff + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_GET_INSTANCES_H +#define TALER_MERCHANT_HTTPD_PRIVATE_GET_INSTANCES_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a GET "/instances" 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_get_instances (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-get-instances.h */ +#endif