LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_reserves_get.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 28 87 32.2 %
Date: 2021-08-30 06:43:37 Functions: 4 6 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2021 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Affero General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-exchange-httpd_reserves_get.c
      18             :  * @brief Handle /reserves/$RESERVE_PUB GET requests
      19             :  * @author Florian Dold
      20             :  * @author Benedikt Mueller
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include <jansson.h>
      26             : #include "taler_mhd_lib.h"
      27             : #include "taler_json_lib.h"
      28             : #include "taler_dbevents.h"
      29             : #include "taler-exchange-httpd_reserves_get.h"
      30             : #include "taler-exchange-httpd_responses.h"
      31             : 
      32             : 
      33             : /**
      34             :  * Reserve GET request that is long-polling.
      35             :  */
      36             : struct ReservePoller
      37             : {
      38             :   /**
      39             :    * Kept in a DLL.
      40             :    */
      41             :   struct ReservePoller *next;
      42             : 
      43             :   /**
      44             :    * Kept in a DLL.
      45             :    */
      46             :   struct ReservePoller *prev;
      47             : 
      48             :   /**
      49             :    * Connection we are handling.
      50             :    */
      51             :   struct MHD_Connection *connection;
      52             : 
      53             :   /**
      54             :    * Subscription for the database event we are
      55             :    * waiting for.
      56             :    */
      57             :   struct GNUNET_DB_EventHandler *eh;
      58             : 
      59             :   /**
      60             :    * When will this request time out?
      61             :    */
      62             :   struct GNUNET_TIME_Absolute timeout;
      63             : 
      64             :   /**
      65             :    * True if we are still suspended.
      66             :    */
      67             :   bool suspended;
      68             : 
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Head of list of requests in long polling.
      74             :  */
      75             : static struct ReservePoller *rp_head;
      76             : 
      77             : /**
      78             :  * Tail of list of requests in long polling.
      79             :  */
      80             : static struct ReservePoller *rp_tail;
      81             : 
      82             : 
      83             : void
      84          12 : TEH_reserves_get_cleanup ()
      85             : {
      86             :   struct ReservePoller *rp;
      87             : 
      88          12 :   while (NULL != (rp = rp_head))
      89             :   {
      90           0 :     GNUNET_CONTAINER_DLL_remove (rp_head,
      91             :                                  rp_tail,
      92             :                                  rp);
      93           0 :     if (rp->suspended)
      94             :     {
      95           0 :       rp->suspended = false;
      96           0 :       MHD_resume_connection (rp->connection);
      97             :     }
      98             :   }
      99          12 : }
     100             : 
     101             : 
     102             : /**
     103             :  * Function called once a connection is done to
     104             :  * clean up the `struct ReservePoller` state.
     105             :  *
     106             :  * @param rc context to clean up for
     107             :  */
     108             : static void
     109           0 : rp_cleanup (struct TEH_RequestContext *rc)
     110             : {
     111           0 :   struct ReservePoller *rp = rc->rh_ctx;
     112             : 
     113           0 :   GNUNET_assert (! rp->suspended);
     114           0 :   if (NULL != rp->eh)
     115             :   {
     116           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     117             :                 "Cancelling DB event listening\n");
     118           0 :     TEH_plugin->event_listen_cancel (TEH_plugin->cls,
     119             :                                      rp->eh);
     120           0 :     rp->eh = NULL;
     121             :   }
     122           0 :   GNUNET_free (rp);
     123           0 : }
     124             : 
     125             : 
     126             : /**
     127             :  * Function called on events received from Postgres.
     128             :  * Wakes up long pollers.
     129             :  *
     130             :  * @param cls the `struct TEH_RequestContext *`
     131             :  * @param extra additional event data provided
     132             :  * @param extra_size number of bytes in @a extra
     133             :  */
     134             : static void
     135           0 : db_event_cb (void *cls,
     136             :              const void *extra,
     137             :              size_t extra_size)
     138             : {
     139           0 :   struct TEH_RequestContext *rc = cls;
     140           0 :   struct ReservePoller *rp = rc->rh_ctx;
     141             :   struct GNUNET_AsyncScopeSave old_scope;
     142             : 
     143             :   (void) extra;
     144             :   (void) extra_size;
     145           0 :   if (NULL == rp)
     146           0 :     return; /* event triggered while main transaction
     147             :                was still running */
     148           0 :   if (! rp->suspended)
     149           0 :     return; /* might get multiple wake-up events */
     150           0 :   rp->suspended = false;
     151           0 :   GNUNET_async_scope_enter (&rc->async_scope_id,
     152             :                             &old_scope);
     153           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     154             :               "Resuming from long-polling on reserve\n");
     155           0 :   GNUNET_CONTAINER_DLL_remove (rp_head,
     156             :                                rp_tail,
     157             :                                rp);
     158           0 :   MHD_resume_connection (rp->connection);
     159           0 :   TALER_MHD_daemon_trigger ();
     160           0 :   GNUNET_async_scope_restore (&old_scope);
     161             : }
     162             : 
     163             : 
     164             : /**
     165             :  * Send reserve history to client.
     166             :  *
     167             :  * @param connection connection to the client
     168             :  * @param rh reserve history to return
     169             :  * @return MHD result code
     170             :  */
     171             : static MHD_RESULT
     172           8 : reply_reserve_history_success (struct MHD_Connection *connection,
     173             :                                const struct TALER_EXCHANGEDB_ReserveHistory *rh)
     174             : {
     175             :   json_t *json_history;
     176             :   struct TALER_Amount balance;
     177             : 
     178           8 :   json_history = TEH_RESPONSE_compile_reserve_history (rh,
     179             :                                                        &balance);
     180           8 :   if (NULL == json_history)
     181           0 :     return TALER_MHD_reply_with_error (connection,
     182             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     183             :                                        TALER_EC_GENERIC_JSON_ALLOCATION_FAILURE,
     184             :                                        NULL);
     185           8 :   return TALER_MHD_REPLY_JSON_PACK (
     186             :     connection,
     187             :     MHD_HTTP_OK,
     188             :     TALER_JSON_pack_amount ("balance",
     189             :                             &balance),
     190             :     GNUNET_JSON_pack_array_steal ("history",
     191             :                                   json_history));
     192             : }
     193             : 
     194             : 
     195             : /**
     196             :  * Closure for #reserve_history_transaction.
     197             :  */
     198             : struct ReserveHistoryContext
     199             : {
     200             :   /**
     201             :    * Public key of the reserve the inquiry is about.
     202             :    */
     203             :   struct TALER_ReservePublicKeyP reserve_pub;
     204             : 
     205             :   /**
     206             :    * History of the reserve, set in the callback.
     207             :    */
     208             :   struct TALER_EXCHANGEDB_ReserveHistory *rh;
     209             : 
     210             : };
     211             : 
     212             : 
     213             : /**
     214             :  * Function implementing /reserves/ GET transaction.
     215             :  * Execute a /reserves/ GET.  Given the public key of a reserve,
     216             :  * return the associated transaction history.  Runs the
     217             :  * transaction logic; IF it returns a non-error code, the transaction
     218             :  * logic MUST NOT queue a MHD response.  IF it returns an hard error,
     219             :  * the transaction logic MUST queue a MHD response and set @a mhd_ret.
     220             :  * IF it returns the soft error code, the function MAY be called again
     221             :  * to retry and MUST not queue a MHD response.
     222             :  *
     223             :  * @param cls a `struct ReserveHistoryContext *`
     224             :  * @param connection MHD request which triggered the transaction
     225             :  * @param[out] mhd_ret set to MHD response status for @a connection,
     226             :  *             if transaction failed (!); unused
     227             :  * @return transaction status
     228             :  */
     229             : static enum GNUNET_DB_QueryStatus
     230           8 : reserve_history_transaction (void *cls,
     231             :                              struct MHD_Connection *connection,
     232             :                              MHD_RESULT *mhd_ret)
     233             : {
     234           8 :   struct ReserveHistoryContext *rsc = cls;
     235             : 
     236             :   (void) connection;
     237             :   (void) mhd_ret;
     238          16 :   return TEH_plugin->get_reserve_history (TEH_plugin->cls,
     239           8 :                                           &rsc->reserve_pub,
     240             :                                           &rsc->rh);
     241             : }
     242             : 
     243             : 
     244             : MHD_RESULT
     245           8 : TEH_handler_reserves_get (struct TEH_RequestContext *rc,
     246             :                           const char *const args[1])
     247             : {
     248             :   struct ReserveHistoryContext rsc;
     249             :   MHD_RESULT mhd_ret;
     250           8 :   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_ZERO;
     251           8 :   struct GNUNET_DB_EventHandler *eh = NULL;
     252             : 
     253           8 :   if (GNUNET_OK !=
     254           8 :       GNUNET_STRINGS_string_to_data (args[0],
     255             :                                      strlen (args[0]),
     256             :                                      &rsc.reserve_pub,
     257             :                                      sizeof (rsc.reserve_pub)))
     258             :   {
     259           0 :     GNUNET_break_op (0);
     260           0 :     return TALER_MHD_reply_with_error (rc->connection,
     261             :                                        MHD_HTTP_BAD_REQUEST,
     262             :                                        TALER_EC_MERCHANT_GENERIC_RESERVE_PUB_MALFORMED,
     263             :                                        args[0]);
     264             :   }
     265             :   {
     266             :     const char *long_poll_timeout_ms;
     267             : 
     268             :     long_poll_timeout_ms
     269           8 :       = MHD_lookup_connection_value (rc->connection,
     270             :                                      MHD_GET_ARGUMENT_KIND,
     271             :                                      "timeout_ms");
     272           8 :     if (NULL != long_poll_timeout_ms)
     273             :     {
     274             :       unsigned int timeout_ms;
     275             :       char dummy;
     276             : 
     277           0 :       if (1 != sscanf (long_poll_timeout_ms,
     278             :                        "%u%c",
     279             :                        &timeout_ms,
     280             :                        &dummy))
     281             :       {
     282           0 :         GNUNET_break_op (0);
     283           0 :         return TALER_MHD_reply_with_error (rc->connection,
     284             :                                            MHD_HTTP_BAD_REQUEST,
     285             :                                            TALER_EC_GENERIC_PARAMETER_MALFORMED,
     286             :                                            "timeout_ms (must be non-negative number)");
     287             :       }
     288           0 :       timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
     289             :                                                timeout_ms);
     290             :     }
     291             :   }
     292           8 :   if ( (! GNUNET_TIME_relative_is_zero (timeout)) &&
     293           0 :        (NULL == rc->rh_ctx) )
     294             :   {
     295           0 :     struct TALER_ReserveEventP rep = {
     296           0 :       .header.size = htons (sizeof (rep)),
     297           0 :       .header.type = htons (TALER_DBEVENT_EXCHANGE_RESERVE_INCOMING),
     298             :       .reserve_pub = rsc.reserve_pub
     299             :     };
     300             : 
     301           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     302             :                 "Starting DB event listening\n");
     303           0 :     eh = TEH_plugin->event_listen (TEH_plugin->cls,
     304             :                                    timeout,
     305             :                                    &rep.header,
     306             :                                    &db_event_cb,
     307             :                                    rc);
     308             :   }
     309           8 :   rsc.rh = NULL;
     310           8 :   if (GNUNET_OK !=
     311           8 :       TEH_DB_run_transaction (rc->connection,
     312             :                               "get reserve history",
     313             :                               &mhd_ret,
     314             :                               &reserve_history_transaction,
     315             :                               &rsc))
     316             :   {
     317           0 :     if (NULL != eh)
     318           0 :       TEH_plugin->event_listen_cancel (TEH_plugin->cls,
     319             :                                        eh);
     320           0 :     return mhd_ret;
     321             :   }
     322             :   /* generate proper response */
     323           8 :   if (NULL == rsc.rh)
     324             :   {
     325           0 :     struct ReservePoller *rp = rc->rh_ctx;
     326             : 
     327           0 :     if ( (NULL != rp) ||
     328           0 :          (GNUNET_TIME_relative_is_zero (timeout)) )
     329             :     {
     330           0 :       return TALER_MHD_reply_with_error (rc->connection,
     331             :                                          MHD_HTTP_NOT_FOUND,
     332             :                                          TALER_EC_EXCHANGE_RESERVES_GET_STATUS_UNKNOWN,
     333             :                                          args[0]);
     334             :     }
     335           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     336             :                 "Long-polling on reserve for %s\n",
     337             :                 GNUNET_STRINGS_relative_time_to_string (timeout,
     338             :                                                         GNUNET_YES));
     339           0 :     rp = GNUNET_new (struct ReservePoller);
     340           0 :     rp->connection = rc->connection;
     341           0 :     rp->timeout = GNUNET_TIME_relative_to_absolute (timeout);
     342           0 :     rp->eh = eh;
     343           0 :     rc->rh_ctx = rp;
     344           0 :     rc->rh_cleaner = &rp_cleanup;
     345           0 :     rp->suspended = true;
     346           0 :     GNUNET_CONTAINER_DLL_insert (rp_head,
     347             :                                  rp_tail,
     348             :                                  rp);
     349           0 :     MHD_suspend_connection (rc->connection);
     350           0 :     return MHD_YES;
     351             :   }
     352           8 :   if (NULL != eh)
     353           0 :     TEH_plugin->event_listen_cancel (TEH_plugin->cls,
     354             :                                      eh);
     355          16 :   mhd_ret = reply_reserve_history_success (rc->connection,
     356           8 :                                            rsc.rh);
     357           8 :   TEH_plugin->free_reserve_history (TEH_plugin->cls,
     358             :                                     rsc.rh);
     359           8 :   return mhd_ret;
     360             : }
     361             : 
     362             : 
     363             : /* end of taler-exchange-httpd_reserves_get.c */

Generated by: LCOV version 1.14