LCOV - code coverage report
Current view: top level - lib - exchange_api_reserves_get.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 61 107 57.0 %
Date: 2021-08-30 06:43:37 Functions: 4 4 100.0 %
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 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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/exchange_api_reserves_get.c
      19             :  * @brief Implementation of the GET /reserves/$RESERVE_PUB requests
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <jansson.h>
      24             : #include <microhttpd.h> /* just for HTTP status codes */
      25             : #include <gnunet/gnunet_util_lib.h>
      26             : #include <gnunet/gnunet_json_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_exchange_service.h"
      29             : #include "taler_json_lib.h"
      30             : #include "exchange_api_handle.h"
      31             : #include "taler_signatures.h"
      32             : #include "exchange_api_curl_defaults.h"
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A /reserves/ GET Handle
      37             :  */
      38             : struct TALER_EXCHANGE_ReservesGetHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The connection to exchange this request handle will use
      43             :    */
      44             :   struct TALER_EXCHANGE_Handle *exchange;
      45             : 
      46             :   /**
      47             :    * The url for this request.
      48             :    */
      49             :   char *url;
      50             : 
      51             :   /**
      52             :    * Handle for the request.
      53             :    */
      54             :   struct GNUNET_CURL_Job *job;
      55             : 
      56             :   /**
      57             :    * Function to call with the result.
      58             :    */
      59             :   TALER_EXCHANGE_ReservesGetCallback cb;
      60             : 
      61             :   /**
      62             :    * Public key of the reserve we are querying.
      63             :    */
      64             :   struct TALER_ReservePublicKeyP reserve_pub;
      65             : 
      66             :   /**
      67             :    * Closure for @a cb.
      68             :    */
      69             :   void *cb_cls;
      70             : 
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * We received an #MHD_HTTP_OK status code. Handle the JSON
      76             :  * response.
      77             :  *
      78             :  * @param rgh handle of the request
      79             :  * @param j JSON response
      80             :  * @return #GNUNET_OK on success
      81             :  */
      82             : static int
      83           8 : handle_reserves_get_ok (struct TALER_EXCHANGE_ReservesGetHandle *rgh,
      84             :                         const json_t *j)
      85             : {
      86             :   json_t *history;
      87             :   unsigned int len;
      88             :   struct TALER_Amount balance;
      89             :   struct TALER_Amount balance_from_history;
      90             :   struct GNUNET_JSON_Specification spec[] = {
      91           8 :     TALER_JSON_spec_amount_any ("balance", &balance),
      92           8 :     GNUNET_JSON_spec_end ()
      93             :   };
      94           8 :   struct TALER_EXCHANGE_HttpResponse hr = {
      95             :     .reply = j,
      96             :     .http_status = MHD_HTTP_OK
      97             :   };
      98             : 
      99           8 :   if (GNUNET_OK !=
     100           8 :       GNUNET_JSON_parse (j,
     101             :                          spec,
     102             :                          NULL,
     103             :                          NULL))
     104             :   {
     105           0 :     GNUNET_break_op (0);
     106           0 :     return GNUNET_SYSERR;
     107             :   }
     108           8 :   history = json_object_get (j,
     109             :                              "history");
     110           8 :   if (NULL == history)
     111             :   {
     112           0 :     GNUNET_break_op (0);
     113           0 :     return GNUNET_SYSERR;
     114             :   }
     115           8 :   len = json_array_size (history);
     116             :   {
     117             :     struct TALER_EXCHANGE_ReserveHistory *rhistory;
     118             : 
     119           8 :     rhistory = GNUNET_new_array (len,
     120             :                                  struct TALER_EXCHANGE_ReserveHistory);
     121           8 :     if (GNUNET_OK !=
     122           8 :         TALER_EXCHANGE_parse_reserve_history (rgh->exchange,
     123             :                                               history,
     124           8 :                                               &rgh->reserve_pub,
     125             :                                               balance.currency,
     126             :                                               &balance_from_history,
     127             :                                               len,
     128             :                                               rhistory))
     129             :     {
     130           0 :       GNUNET_break_op (0);
     131           0 :       TALER_EXCHANGE_free_reserve_history (rhistory,
     132             :                                            len);
     133           0 :       return GNUNET_SYSERR;
     134             :     }
     135           8 :     if (0 !=
     136           8 :         TALER_amount_cmp (&balance_from_history,
     137             :                           &balance))
     138             :     {
     139             :       /* exchange cannot add up balances!? */
     140           0 :       GNUNET_break_op (0);
     141           0 :       TALER_EXCHANGE_free_reserve_history (rhistory,
     142             :                                            len);
     143           0 :       return GNUNET_SYSERR;
     144             :     }
     145           8 :     if (NULL != rgh->cb)
     146             :     {
     147           8 :       rgh->cb (rgh->cb_cls,
     148             :                &hr,
     149             :                &balance,
     150             :                len,
     151             :                rhistory);
     152           8 :       rgh->cb = NULL;
     153             :     }
     154           8 :     TALER_EXCHANGE_free_reserve_history (rhistory,
     155             :                                          len);
     156             :   }
     157           8 :   return GNUNET_OK;
     158             : }
     159             : 
     160             : 
     161             : /**
     162             :  * Function called when we're done processing the
     163             :  * HTTP /reserves/ GET request.
     164             :  *
     165             :  * @param cls the `struct TALER_EXCHANGE_ReservesGetHandle`
     166             :  * @param response_code HTTP response code, 0 on error
     167             :  * @param response parsed JSON result, NULL on error
     168             :  */
     169             : static void
     170           8 : handle_reserves_get_finished (void *cls,
     171             :                               long response_code,
     172             :                               const void *response)
     173             : {
     174           8 :   struct TALER_EXCHANGE_ReservesGetHandle *rgh = cls;
     175           8 :   const json_t *j = response;
     176           8 :   struct TALER_EXCHANGE_HttpResponse hr = {
     177             :     .reply = j,
     178           8 :     .http_status = (unsigned int) response_code
     179             :   };
     180             : 
     181           8 :   rgh->job = NULL;
     182           8 :   switch (response_code)
     183             :   {
     184           0 :   case 0:
     185           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     186           0 :     break;
     187           8 :   case MHD_HTTP_OK:
     188           8 :     if (GNUNET_OK !=
     189           8 :         handle_reserves_get_ok (rgh,
     190             :                                 j))
     191             :     {
     192           0 :       hr.http_status = 0;
     193           0 :       hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     194             :     }
     195           8 :     break;
     196           0 :   case MHD_HTTP_BAD_REQUEST:
     197             :     /* This should never happen, either us or the exchange is buggy
     198             :        (or API version conflict); just pass JSON reply to the application */
     199           0 :     hr.ec = TALER_JSON_get_error_code (j);
     200           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     201           0 :     break;
     202           0 :   case MHD_HTTP_NOT_FOUND:
     203             :     /* Nothing really to verify, this should never
     204             :        happen, we should pass the JSON reply to the application */
     205           0 :     hr.ec = TALER_JSON_get_error_code (j);
     206           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     207           0 :     break;
     208           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     209             :     /* Server had an internal issue; we should retry, but this API
     210             :        leaves this to the application */
     211           0 :     hr.ec = TALER_JSON_get_error_code (j);
     212           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     213           0 :     break;
     214           0 :   default:
     215             :     /* unexpected response code */
     216           0 :     GNUNET_break_op (0);
     217           0 :     hr.ec = TALER_JSON_get_error_code (j);
     218           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     219           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     220             :                 "Unexpected response code %u/%d for reserves get\n",
     221             :                 (unsigned int) response_code,
     222             :                 (int) hr.ec);
     223           0 :     break;
     224             :   }
     225           8 :   if (NULL != rgh->cb)
     226             :   {
     227           0 :     rgh->cb (rgh->cb_cls,
     228             :              &hr,
     229             :              NULL,
     230             :              0, NULL);
     231           0 :     rgh->cb = NULL;
     232             :   }
     233           8 :   TALER_EXCHANGE_reserves_get_cancel (rgh);
     234           8 : }
     235             : 
     236             : 
     237             : struct TALER_EXCHANGE_ReservesGetHandle *
     238           8 : TALER_EXCHANGE_reserves_get (
     239             :   struct TALER_EXCHANGE_Handle *exchange,
     240             :   const struct TALER_ReservePublicKeyP *reserve_pub,
     241             :   struct GNUNET_TIME_Relative timeout,
     242             :   TALER_EXCHANGE_ReservesGetCallback cb,
     243             :   void *cb_cls)
     244             : {
     245             :   struct TALER_EXCHANGE_ReservesGetHandle *rgh;
     246             :   struct GNUNET_CURL_Context *ctx;
     247             :   CURL *eh;
     248             :   char arg_str[sizeof (struct TALER_ReservePublicKeyP) * 2 + 16 + 32];
     249             : 
     250           8 :   if (GNUNET_YES !=
     251           8 :       TEAH_handle_is_ready (exchange))
     252             :   {
     253           0 :     GNUNET_break (0);
     254           0 :     return NULL;
     255             :   }
     256             :   {
     257             :     char pub_str[sizeof (struct TALER_ReservePublicKeyP) * 2];
     258             :     char *end;
     259             :     char timeout_str[32];
     260             : 
     261           8 :     end = GNUNET_STRINGS_data_to_string (
     262             :       reserve_pub,
     263             :       sizeof (struct TALER_ReservePublicKeyP),
     264             :       pub_str,
     265             :       sizeof (pub_str));
     266           8 :     *end = '\0';
     267           8 :     GNUNET_snprintf (timeout_str,
     268             :                      sizeof (timeout_str),
     269             :                      "%llu",
     270             :                      (unsigned long long)
     271           8 :                      (timeout.rel_value_us
     272           8 :                       / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us));
     273           8 :     if (GNUNET_TIME_relative_is_zero (timeout))
     274           8 :       GNUNET_snprintf (arg_str,
     275             :                        sizeof (arg_str),
     276             :                        "/reserves/%s",
     277             :                        pub_str);
     278             :     else
     279           0 :       GNUNET_snprintf (arg_str,
     280             :                        sizeof (arg_str),
     281             :                        "/reserves/%s?timeout_ms=%s",
     282             :                        pub_str,
     283             :                        timeout_str);
     284             :   }
     285           8 :   rgh = GNUNET_new (struct TALER_EXCHANGE_ReservesGetHandle);
     286           8 :   rgh->exchange = exchange;
     287           8 :   rgh->cb = cb;
     288           8 :   rgh->cb_cls = cb_cls;
     289           8 :   rgh->reserve_pub = *reserve_pub;
     290           8 :   rgh->url = TEAH_path_to_url (exchange,
     291             :                                arg_str);
     292           8 :   if (NULL == rgh->url)
     293             :   {
     294           0 :     GNUNET_free (rgh);
     295           0 :     return NULL;
     296             :   }
     297           8 :   eh = TALER_EXCHANGE_curl_easy_get_ (rgh->url);
     298           8 :   if (NULL == eh)
     299             :   {
     300           0 :     GNUNET_break (0);
     301           0 :     GNUNET_free (rgh->url);
     302           0 :     GNUNET_free (rgh);
     303           0 :     return NULL;
     304             :   }
     305           8 :   ctx = TEAH_handle_to_context (exchange);
     306           8 :   rgh->job = GNUNET_CURL_job_add (ctx,
     307             :                                   eh,
     308             :                                   &handle_reserves_get_finished,
     309             :                                   rgh);
     310           8 :   return rgh;
     311             : }
     312             : 
     313             : 
     314             : void
     315           8 : TALER_EXCHANGE_reserves_get_cancel (
     316             :   struct TALER_EXCHANGE_ReservesGetHandle *rgh)
     317             : {
     318           8 :   if (NULL != rgh->job)
     319             :   {
     320           0 :     GNUNET_CURL_job_cancel (rgh->job);
     321           0 :     rgh->job = NULL;
     322             :   }
     323           8 :   GNUNET_free (rgh->url);
     324           8 :   GNUNET_free (rgh);
     325           8 : }
     326             : 
     327             : 
     328             : /* end of exchange_api_reserves_get.c */

Generated by: LCOV version 1.14