LCOV - code coverage report
Current view: top level - lib - merchant_api_post_reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 78 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016, 2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file merchant_api_post_reserves.c
      19             :  * @brief Implementation of the POST /reserves request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_curl_lib.h>
      31             : #include <taler/taler_json_lib.h>
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A handle for POSTing reserve data.
      36             :  */
      37             : struct TALER_MERCHANT_PostReservesHandle
      38             : {
      39             : 
      40             :   /**
      41             :    * The url for this request.
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_MERCHANT_PostReservesCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             :   /**
      61             :    * Reference to the execution context.
      62             :    */
      63             :   struct GNUNET_CURL_Context *ctx;
      64             : 
      65             :   /**
      66             :    * Minor context that holds body and headers.
      67             :    */
      68             :   struct TALER_CURL_PostContext post_ctx;
      69             : 
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * Function called when we're done processing the
      75             :  * HTTP POST /reserves request.
      76             :  *
      77             :  * @param cls the `struct TALER_MERCHANT_PostReservesHandle`
      78             :  * @param response_code HTTP response code, 0 on error
      79             :  * @param response response body, NULL if not in JSON
      80             :  */
      81             : static void
      82           0 : handle_post_reserves_finished (void *cls,
      83             :                                long response_code,
      84             :                                const void *response)
      85             : {
      86           0 :   struct TALER_MERCHANT_PostReservesHandle *prh = cls;
      87           0 :   const json_t *json = response;
      88           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      89           0 :     .http_status = (unsigned int) response_code,
      90             :     .reply = json
      91             :   };
      92             : 
      93           0 :   prh->job = NULL;
      94           0 :   switch (response_code)
      95             :   {
      96           0 :   case 0:
      97           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      98           0 :     break;
      99           0 :   case MHD_HTTP_OK:
     100             :     {
     101             :       const char *payto_uri;
     102             :       struct TALER_ReservePublicKeyP reserve_pub;
     103             :       struct GNUNET_JSON_Specification spec[] = {
     104           0 :         GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     105             :                                      &reserve_pub),
     106           0 :         GNUNET_JSON_spec_string ("payto_uri",
     107             :                                  &payto_uri),
     108           0 :         GNUNET_JSON_spec_end ()
     109             :       };
     110             : 
     111           0 :       if (GNUNET_OK !=
     112           0 :           GNUNET_JSON_parse (json,
     113             :                              spec,
     114             :                              NULL, NULL))
     115             :       {
     116           0 :         GNUNET_break_op (0);
     117           0 :         hr.http_status = 0;
     118           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     119           0 :         break;
     120             :       }
     121             :       else
     122             :       {
     123           0 :         prh->cb (prh->cb_cls,
     124             :                  &hr,
     125             :                  &reserve_pub,
     126             :                  payto_uri);
     127           0 :         GNUNET_JSON_parse_free (spec);
     128           0 :         TALER_MERCHANT_reserves_post_cancel (prh);
     129           0 :         return;
     130             :       }
     131             :     }
     132           0 :   case MHD_HTTP_ACCEPTED:
     133           0 :     break;
     134           0 :   case MHD_HTTP_UNAUTHORIZED:
     135           0 :     hr.ec = TALER_JSON_get_error_code (json);
     136           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     137             :     /* Nothing really to verify, merchant says we need to authenticate. */
     138           0 :     break;
     139           0 :   case MHD_HTTP_NOT_FOUND:
     140             :     /* Nothing really to verify, this should never
     141             :        happen, we should pass the JSON reply to the application */
     142           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     143             :                 "Did not find any data\n");
     144           0 :     hr.ec = TALER_JSON_get_error_code (json);
     145           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     146           0 :     break;
     147           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     148             :     /* Server had an internal issue; we should retry, but this API
     149             :        leaves this to the application */
     150           0 :     hr.ec = TALER_JSON_get_error_code (json);
     151           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     152           0 :     break;
     153           0 :   default:
     154             :     /* unexpected response code */
     155           0 :     GNUNET_break_op (0);
     156           0 :     TALER_MERCHANT_parse_error_details_ (json,
     157             :                                          response_code,
     158             :                                          &hr);
     159           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     160             :                 "Unexpected response code %u/%d\n",
     161             :                 (unsigned int) response_code,
     162             :                 (int) hr.ec);
     163           0 :     break;
     164             :   }
     165           0 :   prh->cb (prh->cb_cls,
     166             :            &hr,
     167             :            NULL,
     168             :            NULL);
     169           0 :   TALER_MERCHANT_reserves_post_cancel (prh);
     170             : }
     171             : 
     172             : 
     173             : struct TALER_MERCHANT_PostReservesHandle *
     174           0 : TALER_MERCHANT_reserves_post (
     175             :   struct GNUNET_CURL_Context *ctx,
     176             :   const char *backend_url,
     177             :   const struct TALER_Amount *initial_balance,
     178             :   const char *exchange_url,
     179             :   const char *wire_method,
     180             :   TALER_MERCHANT_PostReservesCallback cb,
     181             :   void *cb_cls)
     182             : {
     183             :   struct TALER_MERCHANT_PostReservesHandle *prh;
     184             :   CURL *eh;
     185             :   json_t *req;
     186             : 
     187           0 :   prh = GNUNET_new (struct TALER_MERCHANT_PostReservesHandle);
     188           0 :   prh->ctx = ctx;
     189           0 :   prh->cb = cb;
     190           0 :   prh->cb_cls = cb_cls;
     191           0 :   prh->url = TALER_url_join (backend_url,
     192             :                              "private/reserves",
     193             :                              NULL);
     194           0 :   if (NULL == prh->url)
     195             :   {
     196           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     197             :                 "Could not construct request URL.\n");
     198           0 :     GNUNET_free (prh);
     199           0 :     return NULL;
     200             :   }
     201           0 :   req = GNUNET_JSON_PACK (
     202             :     TALER_JSON_pack_amount ("initial_balance",
     203             :                             initial_balance),
     204             :     GNUNET_JSON_pack_string ("wire_method",
     205             :                              wire_method),
     206             :     GNUNET_JSON_pack_string ("exchange_url",
     207             :                              exchange_url));
     208           0 :   eh = TALER_MERCHANT_curl_easy_get_ (prh->url);
     209           0 :   if (GNUNET_OK !=
     210           0 :       TALER_curl_easy_post (&prh->post_ctx,
     211             :                             eh,
     212             :                             req))
     213             :   {
     214           0 :     GNUNET_break (0);
     215           0 :     curl_easy_cleanup (eh);
     216           0 :     json_decref (req);
     217           0 :     GNUNET_free (prh->url);
     218           0 :     GNUNET_free (prh);
     219           0 :     return NULL;
     220             :   }
     221           0 :   json_decref (req);
     222           0 :   prh->job = GNUNET_CURL_job_add2 (ctx,
     223             :                                    eh,
     224           0 :                                    prh->post_ctx.headers,
     225             :                                    &handle_post_reserves_finished,
     226             :                                    prh);
     227           0 :   return prh;
     228             : }
     229             : 
     230             : 
     231             : void
     232           0 : TALER_MERCHANT_reserves_post_cancel (
     233             :   struct TALER_MERCHANT_PostReservesHandle *prh)
     234             : {
     235           0 :   if (NULL != prh->job)
     236             :   {
     237           0 :     GNUNET_CURL_job_cancel (prh->job);
     238           0 :     prh->job = NULL;
     239             :   }
     240           0 :   GNUNET_free (prh->url);
     241           0 :   TALER_curl_easy_post_finished (&prh->post_ctx);
     242           0 :   GNUNET_free (prh);
     243           0 : }
     244             : 
     245             : 
     246             : /* end of merchant_api_post_reserves.c */

Generated by: LCOV version 1.14