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: 0 88 0.0 %
Date: 2022-08-25 06:15:09 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2022 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 enum GNUNET_GenericReturnValue
      83           0 : handle_reserves_get_ok (struct TALER_EXCHANGE_ReservesGetHandle *rgh,
      84             :                         const json_t *j)
      85             : {
      86           0 :   struct TALER_EXCHANGE_ReserveSummary rs = {
      87             :     .hr.reply = j,
      88             :     .hr.http_status = MHD_HTTP_OK
      89             :   };
      90             :   struct GNUNET_JSON_Specification spec[] = {
      91           0 :     TALER_JSON_spec_amount_any ("balance",
      92             :                                 &rs.details.ok.balance),
      93           0 :     GNUNET_JSON_spec_end ()
      94             :   };
      95             : 
      96           0 :   if (GNUNET_OK !=
      97           0 :       GNUNET_JSON_parse (j,
      98             :                          spec,
      99             :                          NULL,
     100             :                          NULL))
     101             :   {
     102           0 :     GNUNET_break_op (0);
     103           0 :     return GNUNET_SYSERR;
     104             :   }
     105           0 :   rgh->cb (rgh->cb_cls,
     106             :            &rs);
     107           0 :   rgh->cb = NULL;
     108           0 :   return GNUNET_OK;
     109             : }
     110             : 
     111             : 
     112             : /**
     113             :  * Function called when we're done processing the
     114             :  * HTTP /reserves/ GET request.
     115             :  *
     116             :  * @param cls the `struct TALER_EXCHANGE_ReservesGetHandle`
     117             :  * @param response_code HTTP response code, 0 on error
     118             :  * @param response parsed JSON result, NULL on error
     119             :  */
     120             : static void
     121           0 : handle_reserves_get_finished (void *cls,
     122             :                               long response_code,
     123             :                               const void *response)
     124             : {
     125           0 :   struct TALER_EXCHANGE_ReservesGetHandle *rgh = cls;
     126           0 :   const json_t *j = response;
     127           0 :   struct TALER_EXCHANGE_ReserveSummary rs = {
     128             :     .hr.reply = j,
     129           0 :     .hr.http_status = (unsigned int) response_code
     130             :   };
     131             : 
     132           0 :   rgh->job = NULL;
     133           0 :   switch (response_code)
     134             :   {
     135           0 :   case 0:
     136           0 :     rs.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     137           0 :     break;
     138           0 :   case MHD_HTTP_OK:
     139           0 :     if (GNUNET_OK !=
     140           0 :         handle_reserves_get_ok (rgh,
     141             :                                 j))
     142             :     {
     143           0 :       rs.hr.http_status = 0;
     144           0 :       rs.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     145             :     }
     146           0 :     break;
     147           0 :   case MHD_HTTP_BAD_REQUEST:
     148             :     /* This should never happen, either us or the exchange is buggy
     149             :        (or API version conflict); just pass JSON reply to the application */
     150           0 :     rs.hr.ec = TALER_JSON_get_error_code (j);
     151           0 :     rs.hr.hint = TALER_JSON_get_error_hint (j);
     152           0 :     break;
     153           0 :   case MHD_HTTP_NOT_FOUND:
     154             :     /* Nothing really to verify, this should never
     155             :        happen, we should pass the JSON reply to the application */
     156           0 :     rs.hr.ec = TALER_JSON_get_error_code (j);
     157           0 :     rs.hr.hint = TALER_JSON_get_error_hint (j);
     158           0 :     break;
     159           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     160             :     /* Server had an internal issue; we should retry, but this API
     161             :        leaves this to the application */
     162           0 :     rs.hr.ec = TALER_JSON_get_error_code (j);
     163           0 :     rs.hr.hint = TALER_JSON_get_error_hint (j);
     164           0 :     break;
     165           0 :   default:
     166             :     /* unexpected response code */
     167           0 :     GNUNET_break_op (0);
     168           0 :     rs.hr.ec = TALER_JSON_get_error_code (j);
     169           0 :     rs.hr.hint = TALER_JSON_get_error_hint (j);
     170           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     171             :                 "Unexpected response code %u/%d for reserves get\n",
     172             :                 (unsigned int) response_code,
     173             :                 (int) rs.hr.ec);
     174           0 :     break;
     175             :   }
     176           0 :   if (NULL != rgh->cb)
     177             :   {
     178           0 :     rgh->cb (rgh->cb_cls,
     179             :              &rs);
     180           0 :     rgh->cb = NULL;
     181             :   }
     182           0 :   TALER_EXCHANGE_reserves_get_cancel (rgh);
     183           0 : }
     184             : 
     185             : 
     186             : struct TALER_EXCHANGE_ReservesGetHandle *
     187           0 : TALER_EXCHANGE_reserves_get (
     188             :   struct TALER_EXCHANGE_Handle *exchange,
     189             :   const struct TALER_ReservePublicKeyP *reserve_pub,
     190             :   struct GNUNET_TIME_Relative timeout,
     191             :   TALER_EXCHANGE_ReservesGetCallback cb,
     192             :   void *cb_cls)
     193             : {
     194             :   struct TALER_EXCHANGE_ReservesGetHandle *rgh;
     195             :   struct GNUNET_CURL_Context *ctx;
     196             :   CURL *eh;
     197             :   char arg_str[sizeof (struct TALER_ReservePublicKeyP) * 2 + 16 + 32];
     198             : 
     199           0 :   if (GNUNET_YES !=
     200           0 :       TEAH_handle_is_ready (exchange))
     201             :   {
     202           0 :     GNUNET_break (0);
     203           0 :     return NULL;
     204             :   }
     205             :   {
     206             :     char pub_str[sizeof (struct TALER_ReservePublicKeyP) * 2];
     207             :     char *end;
     208             :     char timeout_str[32];
     209             : 
     210           0 :     end = GNUNET_STRINGS_data_to_string (
     211             :       reserve_pub,
     212             :       sizeof (*reserve_pub),
     213             :       pub_str,
     214             :       sizeof (pub_str));
     215           0 :     *end = '\0';
     216           0 :     GNUNET_snprintf (timeout_str,
     217             :                      sizeof (timeout_str),
     218             :                      "%llu",
     219             :                      (unsigned long long)
     220           0 :                      (timeout.rel_value_us
     221           0 :                       / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us));
     222           0 :     if (GNUNET_TIME_relative_is_zero (timeout))
     223           0 :       GNUNET_snprintf (arg_str,
     224             :                        sizeof (arg_str),
     225             :                        "/reserves/%s",
     226             :                        pub_str);
     227             :     else
     228           0 :       GNUNET_snprintf (arg_str,
     229             :                        sizeof (arg_str),
     230             :                        "/reserves/%s?timeout_ms=%s",
     231             :                        pub_str,
     232             :                        timeout_str);
     233             :   }
     234           0 :   rgh = GNUNET_new (struct TALER_EXCHANGE_ReservesGetHandle);
     235           0 :   rgh->exchange = exchange;
     236           0 :   rgh->cb = cb;
     237           0 :   rgh->cb_cls = cb_cls;
     238           0 :   rgh->reserve_pub = *reserve_pub;
     239           0 :   rgh->url = TEAH_path_to_url (exchange,
     240             :                                arg_str);
     241           0 :   if (NULL == rgh->url)
     242             :   {
     243           0 :     GNUNET_free (rgh);
     244           0 :     return NULL;
     245             :   }
     246           0 :   eh = TALER_EXCHANGE_curl_easy_get_ (rgh->url);
     247           0 :   if (NULL == eh)
     248             :   {
     249           0 :     GNUNET_break (0);
     250           0 :     GNUNET_free (rgh->url);
     251           0 :     GNUNET_free (rgh);
     252           0 :     return NULL;
     253             :   }
     254           0 :   ctx = TEAH_handle_to_context (exchange);
     255           0 :   rgh->job = GNUNET_CURL_job_add (ctx,
     256             :                                   eh,
     257             :                                   &handle_reserves_get_finished,
     258             :                                   rgh);
     259           0 :   return rgh;
     260             : }
     261             : 
     262             : 
     263             : void
     264           0 : TALER_EXCHANGE_reserves_get_cancel (
     265             :   struct TALER_EXCHANGE_ReservesGetHandle *rgh)
     266             : {
     267           0 :   if (NULL != rgh->job)
     268             :   {
     269           0 :     GNUNET_CURL_job_cancel (rgh->job);
     270           0 :     rgh->job = NULL;
     271             :   }
     272           0 :   GNUNET_free (rgh->url);
     273           0 :   GNUNET_free (rgh);
     274           0 : }
     275             : 
     276             : 
     277             : /* end of exchange_api_reserves_get.c */

Generated by: LCOV version 1.14