LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-post-reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 64 113 56.6 %
Date: 2021-08-30 06:54:17 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2021 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU Affero General Public License as
       7             :   published by the Free Software Foundation; either version 3,
       8             :   or (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU General Public
      16             :   License along with TALER; see the file COPYING.  If not,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : 
      20             : /**
      21             :  * @file taler-merchant-httpd_private-post-reserves.c
      22             :  * @brief implementing POST /reserves request handling
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler-merchant-httpd_exchanges.h"
      27             : #include "taler-merchant-httpd_private-post-reserves.h"
      28             : #include "taler-merchant-httpd_reserves.h"
      29             : #include <taler/taler_json_lib.h>
      30             : 
      31             : 
      32             : /**
      33             :  * How long to wait before giving up processing with the exchange?
      34             :  */
      35             : #define EXCHANGE_GENERIC_TIMEOUT (GNUNET_TIME_relative_multiply ( \
      36             :                                     GNUNET_TIME_UNIT_SECONDS, \
      37             :                                     15))
      38             : 
      39             : 
      40             : /**
      41             :  * Information we keep for an individual call to the POST /reserves handler.
      42             :  */
      43             : struct PostReserveContext
      44             : {
      45             : 
      46             :   /**
      47             :    * Stored in a DLL.
      48             :    */
      49             :   struct PostReserveContext *next;
      50             : 
      51             :   /**
      52             :    * Stored in a DLL.
      53             :    */
      54             :   struct PostReserveContext *prev;
      55             : 
      56             :   /**
      57             :    * Array with @e coins_cnt coins we are despositing.
      58             :    */
      59             :   struct DepositConfirmation *dc;
      60             : 
      61             :   /**
      62             :    * MHD connection to return to
      63             :    */
      64             :   struct MHD_Connection *connection;
      65             : 
      66             :   /**
      67             :    * Details about the client's request.
      68             :    */
      69             :   struct TMH_HandlerContext *hc;
      70             : 
      71             :   /**
      72             :    * URL of the exchange.
      73             :    */
      74             :   const char *exchange_url;
      75             : 
      76             :   /**
      77             :    * URI of the exchange where the payment needs to be made to.
      78             :    */
      79             :   char *payto_uri;
      80             : 
      81             :   /**
      82             :    * Handle for contacting the exchange.
      83             :    */
      84             :   struct TMH_EXCHANGES_FindOperation *fo;
      85             : 
      86             :   /**
      87             :    * Task run on timeout.
      88             :    */
      89             :   struct GNUNET_SCHEDULER_Task *timeout_task;
      90             : 
      91             :   /**
      92             :    * Initial balance of the reserve.
      93             :    */
      94             :   struct TALER_Amount initial_balance;
      95             : 
      96             :   /**
      97             :    * When will the reserve expire.
      98             :    */
      99             :   struct GNUNET_TIME_Absolute reserve_expiration;
     100             : 
     101             :   /**
     102             :    * Which HTTP status should we return?
     103             :    */
     104             :   unsigned int http_status;
     105             : 
     106             :   /**
     107             :    * Which error code should we return?
     108             :    */
     109             :   enum TALER_ErrorCode ec;
     110             : 
     111             :   /**
     112             :    * Are we suspended?
     113             :    */
     114             :   bool suspended;
     115             : };
     116             : 
     117             : 
     118             : /**
     119             :  * Stored in a DLL.
     120             :  */
     121             : static struct PostReserveContext *rc_head;
     122             : 
     123             : /**
     124             :  * Stored in a DLL.
     125             :  */
     126             : static struct PostReserveContext *rc_tail;
     127             : 
     128             : 
     129             : /**
     130             :  * Force all post reserve contexts to be resumed as we are about
     131             :  * to shut down MHD.
     132             :  */
     133             : void
     134          16 : TMH_force_rc_resume ()
     135             : {
     136             :   struct PostReserveContext *rcn;
     137             : 
     138          16 :   for (struct PostReserveContext *rc = rc_head;
     139             :        NULL != rc;
     140           0 :        rc = rcn)
     141             :   {
     142           0 :     rcn = rc->next;
     143           0 :     if (NULL != rc->timeout_task)
     144             :     {
     145           0 :       GNUNET_SCHEDULER_cancel (rc->timeout_task);
     146           0 :       rc->timeout_task = NULL;
     147             :     }
     148           0 :     if (rc->suspended)
     149             :     {
     150           0 :       rc->suspended = false;
     151           0 :       MHD_resume_connection (rc->connection);
     152           0 :       GNUNET_CONTAINER_DLL_remove (rc_head,
     153             :                                    rc_tail,
     154             :                                    rc);
     155             :     }
     156           0 :     if (NULL != rc->fo)
     157             :     {
     158           0 :       TMH_EXCHANGES_find_exchange_cancel (rc->fo);
     159           0 :       rc->fo = NULL;
     160             :     }
     161             :   }
     162          16 : }
     163             : 
     164             : 
     165             : /**
     166             :  * Custom cleanup routine for a `struct PostReserveContext`.
     167             :  *
     168             :  * @param cls the `struct PostReserveContext` to clean up.
     169             :  */
     170             : static void
     171           2 : reserve_context_cleanup (void *cls)
     172             : {
     173           2 :   struct PostReserveContext *rc = cls;
     174             : 
     175           2 :   if (NULL != rc->fo)
     176             :   {
     177           0 :     TMH_EXCHANGES_find_exchange_cancel (rc->fo);
     178           0 :     rc->fo = NULL;
     179             :   }
     180           2 :   if (NULL != rc->timeout_task)
     181             :   {
     182           0 :     GNUNET_SCHEDULER_cancel (rc->timeout_task);
     183           0 :     rc->timeout_task = NULL;
     184             :   }
     185           2 :   GNUNET_assert (! rc->suspended);
     186           2 :   GNUNET_free (rc->payto_uri);
     187           2 :   GNUNET_free (rc);
     188           2 : }
     189             : 
     190             : 
     191             : /**
     192             :  * Function called with the result of a #TMH_EXCHANGES_find_exchange()
     193             :  * operation.
     194             :  *
     195             :  * @param cls closure with our `struct PostReserveContext *`
     196             :  * @param hr HTTP response details
     197             :  * @param payto_uri URI of the exchange for the wire transfer, NULL on errors
     198             :  * @param eh handle to the exchange context
     199             :  * @param wire_fee current applicable wire fee for dealing with @a eh, NULL if not available
     200             :  * @param exchange_trusted true if this exchange is trusted by config
     201             :  */
     202             : static void
     203           2 : handle_exchange (void *cls,
     204             :                  const struct TALER_EXCHANGE_HttpResponse *hr,
     205             :                  struct TALER_EXCHANGE_Handle *eh,
     206             :                  const char *payto_uri,
     207             :                  const struct TALER_Amount *wire_fee,
     208             :                  bool exchange_trusted)
     209             : {
     210           2 :   struct PostReserveContext *rc = cls;
     211             :   const struct TALER_EXCHANGE_Keys *keys;
     212             : 
     213           2 :   rc->fo = NULL;
     214           2 :   rc->suspended = false;
     215           2 :   if (NULL != rc->timeout_task)
     216             :   {
     217           2 :     GNUNET_SCHEDULER_cancel (rc->timeout_task);
     218           2 :     rc->timeout_task = NULL;
     219             :   }
     220           2 :   MHD_resume_connection (rc->connection);
     221           2 :   GNUNET_CONTAINER_DLL_remove (rc_head,
     222             :                                rc_tail,
     223             :                                rc);
     224           2 :   if (NULL == hr)
     225             :   {
     226           0 :     rc->ec = TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT;
     227           0 :     rc->http_status = MHD_HTTP_GATEWAY_TIMEOUT;
     228           0 :     TALER_MHD_daemon_trigger ();   /* we resumed, kick MHD */
     229           0 :     return;
     230             :   }
     231           2 :   if (NULL == eh)
     232             :   {
     233           0 :     rc->ec = TALER_EC_MERCHANT_GENERIC_EXCHANGE_CONNECT_FAILURE;
     234           0 :     rc->http_status = MHD_HTTP_BAD_GATEWAY;
     235           0 :     TALER_MHD_daemon_trigger ();   /* we resumed, kick MHD */
     236           0 :     return;
     237             :   }
     238           2 :   keys = TALER_EXCHANGE_get_keys (eh);
     239           2 :   if (NULL == keys)
     240             :   {
     241           0 :     rc->ec = TALER_EC_MERCHANT_GENERIC_EXCHANGE_KEYS_FAILURE;
     242           0 :     rc->http_status = MHD_HTTP_BAD_GATEWAY;
     243           0 :     TALER_MHD_daemon_trigger ();   /* we resumed, kick MHD */
     244           0 :     return;
     245             :   }
     246           2 :   if (NULL == payto_uri)
     247             :   {
     248           0 :     rc->ec = TALER_EC_MERCHANT_PRIVATE_POST_RESERVES_UNSUPPORTED_WIRE_METHOD;
     249           0 :     rc->http_status = MHD_HTTP_CONFLICT;
     250           0 :     TALER_MHD_daemon_trigger ();   /* we resumed, kick MHD */
     251           0 :     return;
     252             :   }
     253             :   rc->reserve_expiration
     254           2 :     = GNUNET_TIME_relative_to_absolute (keys->reserve_closing_delay);
     255           2 :   rc->payto_uri = GNUNET_strdup (payto_uri);
     256           2 :   TALER_MHD_daemon_trigger ();   /* we resumed, kick MHD */
     257             : }
     258             : 
     259             : 
     260             : /**
     261             :  * Handle a timeout for the processing of the wire request.
     262             :  *
     263             :  * @param cls closure
     264             :  */
     265             : static void
     266           0 : handle_exchange_timeout (void *cls)
     267             : {
     268           0 :   struct PostReserveContext *rc = cls;
     269             : 
     270           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     271             :               "Resuming POST /private/reserves with error after timeout\n");
     272           0 :   rc->timeout_task = NULL;
     273           0 :   if (NULL != rc->fo)
     274             :   {
     275           0 :     TMH_EXCHANGES_find_exchange_cancel (rc->fo);
     276           0 :     rc->fo = NULL;
     277             :   }
     278           0 :   rc->suspended = false;
     279           0 :   MHD_resume_connection (rc->connection);
     280           0 :   GNUNET_CONTAINER_DLL_remove (rc_head,
     281             :                                rc_tail,
     282             :                                rc);
     283           0 :   rc->ec = TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT;
     284           0 :   rc->http_status = MHD_HTTP_GATEWAY_TIMEOUT;
     285           0 :   TALER_MHD_daemon_trigger ();   /* we resumed, kick MHD */
     286           0 : }
     287             : 
     288             : 
     289             : MHD_RESULT
     290           4 : TMH_private_post_reserves (const struct TMH_RequestHandler *rh,
     291             :                            struct MHD_Connection *connection,
     292             :                            struct TMH_HandlerContext *hc)
     293             : {
     294           4 :   struct PostReserveContext *rc = hc->ctx;
     295           4 :   struct TMH_MerchantInstance *mi = hc->instance;
     296             : 
     297           4 :   GNUNET_assert (NULL != mi);
     298           4 :   if (NULL == rc)
     299             :   {
     300             :     const char *wire_method;
     301             : 
     302           2 :     rc = GNUNET_new (struct PostReserveContext);
     303           2 :     rc->connection = connection;
     304           2 :     rc->hc = hc;
     305           2 :     hc->ctx = rc;
     306           2 :     hc->cc = &reserve_context_cleanup;
     307             : 
     308             :     {
     309             :       enum GNUNET_GenericReturnValue res;
     310             :       struct GNUNET_JSON_Specification spec[] = {
     311           2 :         GNUNET_JSON_spec_string ("exchange_url",
     312             :                                  &rc->exchange_url),
     313           2 :         GNUNET_JSON_spec_string ("wire_method",
     314             :                                  &wire_method),
     315           2 :         TALER_JSON_spec_amount ("initial_balance",
     316             :                                 TMH_currency,
     317             :                                 &rc->initial_balance),
     318           2 :         GNUNET_JSON_spec_end ()
     319             :       };
     320           2 :       res = TALER_MHD_parse_json_data (connection,
     321           2 :                                        hc->request_body,
     322             :                                        spec);
     323           2 :       if (GNUNET_OK != res)
     324             :         return (GNUNET_NO == res)
     325             :                ? MHD_YES
     326           0 :                : MHD_NO;
     327             :     }
     328           2 :     rc->fo = TMH_EXCHANGES_find_exchange (rc->exchange_url,
     329             :                                           wire_method,
     330             :                                           GNUNET_NO,
     331             :                                           &handle_exchange,
     332             :                                           rc);
     333             :     rc->timeout_task
     334           2 :       = GNUNET_SCHEDULER_add_delayed (EXCHANGE_GENERIC_TIMEOUT,
     335             :                                       &handle_exchange_timeout,
     336             :                                       rc);
     337           2 :     rc->suspended = true;
     338           2 :     GNUNET_CONTAINER_DLL_insert (rc_head,
     339             :                                  rc_tail,
     340             :                                  rc);
     341           2 :     MHD_suspend_connection (connection);
     342           2 :     return MHD_YES;
     343             :   }
     344             : 
     345           2 :   GNUNET_assert (! rc->suspended);
     346           2 :   if (NULL == rc->payto_uri)
     347             :   {
     348           0 :     return TALER_MHD_reply_with_error (connection,
     349             :                                        rc->http_status,
     350             :                                        rc->ec,
     351             :                                        NULL);
     352             :   }
     353             :   {
     354             :     struct TALER_ReservePublicKeyP reserve_pub;
     355             :     struct TALER_ReservePrivateKeyP reserve_priv;
     356             :     enum GNUNET_DB_QueryStatus qs;
     357             : 
     358           2 :     GNUNET_CRYPTO_eddsa_key_create (&reserve_priv.eddsa_priv);
     359           2 :     GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv.eddsa_priv,
     360             :                                         &reserve_pub.eddsa_pub);
     361           4 :     qs = TMH_db->insert_reserve (TMH_db->cls,
     362           2 :                                  mi->settings.id,
     363             :                                  &reserve_priv,
     364             :                                  &reserve_pub,
     365             :                                  rc->exchange_url,
     366           2 :                                  rc->payto_uri,
     367           2 :                                  &rc->initial_balance,
     368             :                                  rc->reserve_expiration);
     369           2 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
     370           2 :     TMH_RESERVES_check (mi->settings.id,
     371             :                         rc->exchange_url,
     372             :                         &reserve_pub,
     373           2 :                         &rc->initial_balance);
     374           2 :     if (qs < 0)
     375           0 :       return TALER_MHD_reply_with_error (connection,
     376             :                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     377             :                                          TALER_EC_GENERIC_DB_STORE_FAILED,
     378             :                                          "reserve");
     379           2 :     return TALER_MHD_REPLY_JSON_PACK (
     380             :       connection,
     381             :       MHD_HTTP_OK,
     382             :       GNUNET_JSON_pack_data_auto ("reserve_pub",
     383             :                                   &reserve_pub),
     384             :       GNUNET_JSON_pack_string ("payto_uri",
     385             :                                rc->payto_uri));
     386             :   }
     387             : }
     388             : 
     389             : 
     390             : /* end of taler-merchant-httpd_private-post-reserves.c */

Generated by: LCOV version 1.14