LCOV - code coverage report
Current view: top level - lib - merchant_api_post_reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 74 0.0 %
Date: 2021-04-12 06:04:58 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 <taler/taler_curl_lib.h>
      30             : #include <taler/taler_json_lib.h>
      31             : 
      32             : 
      33             : /**
      34             :  * @brief A handle for POSTing reserve data.
      35             :  */
      36             : struct TALER_MERCHANT_PostReservesHandle
      37             : {
      38             : 
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_MERCHANT_PostReservesCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : 
      64             :   /**
      65             :    * Minor context that holds body and headers.
      66             :    */
      67             :   struct TALER_CURL_PostContext post_ctx;
      68             : 
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Function called when we're done processing the
      74             :  * HTTP POST /reserves request.
      75             :  *
      76             :  * @param cls the `struct TALER_MERCHANT_PostReservesHandle`
      77             :  * @param response_code HTTP response code, 0 on error
      78             :  * @param response response body, NULL if not in JSON
      79             :  */
      80             : static void
      81           0 : handle_post_reserves_finished (void *cls,
      82             :                                long response_code,
      83             :                                const void *response)
      84             : {
      85           0 :   struct TALER_MERCHANT_PostReservesHandle *prh = cls;
      86           0 :   const json_t *json = response;
      87           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      88           0 :     .http_status = (unsigned int) response_code,
      89             :     .reply = json
      90             :   };
      91             : 
      92           0 :   prh->job = NULL;
      93           0 :   switch (response_code)
      94             :   {
      95           0 :   case 0:
      96           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      97           0 :     break;
      98           0 :   case MHD_HTTP_OK:
      99             :     {
     100             :       const char *payto_uri;
     101             :       struct TALER_ReservePublicKeyP reserve_pub;
     102             :       struct GNUNET_JSON_Specification spec[] = {
     103           0 :         GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     104             :                                      &reserve_pub),
     105           0 :         GNUNET_JSON_spec_string ("payto_uri",
     106             :                                  &payto_uri),
     107           0 :         GNUNET_JSON_spec_end ()
     108             :       };
     109             : 
     110           0 :       if (GNUNET_OK !=
     111           0 :           GNUNET_JSON_parse (json,
     112             :                              spec,
     113             :                              NULL, NULL))
     114             :       {
     115           0 :         GNUNET_break_op (0);
     116           0 :         hr.http_status = 0;
     117           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     118           0 :         break;
     119             :       }
     120             :       else
     121             :       {
     122           0 :         prh->cb (prh->cb_cls,
     123             :                  &hr,
     124             :                  &reserve_pub,
     125             :                  payto_uri);
     126           0 :         GNUNET_JSON_parse_free (spec);
     127           0 :         TALER_MERCHANT_reserves_post_cancel (prh);
     128           0 :         return;
     129             :       }
     130             :     }
     131           0 :   case MHD_HTTP_ACCEPTED:
     132           0 :     break;
     133           0 :   case MHD_HTTP_NOT_FOUND:
     134             :     /* Nothing really to verify, this should never
     135             :        happen, we should pass the JSON reply to the application */
     136           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     137             :                 "Did not find any data\n");
     138           0 :     hr.ec = TALER_JSON_get_error_code (json);
     139           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     140           0 :     break;
     141           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     142             :     /* Server had an internal issue; we should retry, but this API
     143             :        leaves this to the application */
     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 :   default:
     148             :     /* unexpected response code */
     149           0 :     GNUNET_break_op (0);
     150           0 :     TALER_MERCHANT_parse_error_details_ (json,
     151             :                                          response_code,
     152             :                                          &hr);
     153           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     154             :                 "Unexpected response code %u/%d\n",
     155             :                 (unsigned int) response_code,
     156             :                 (int) hr.ec);
     157           0 :     break;
     158             :   }
     159           0 :   prh->cb (prh->cb_cls,
     160             :            &hr,
     161             :            NULL,
     162             :            NULL);
     163           0 :   TALER_MERCHANT_reserves_post_cancel (prh);
     164             : }
     165             : 
     166             : 
     167             : struct TALER_MERCHANT_PostReservesHandle *
     168           0 : TALER_MERCHANT_reserves_post (
     169             :   struct GNUNET_CURL_Context *ctx,
     170             :   const char *backend_url,
     171             :   const struct TALER_Amount *initial_balance,
     172             :   const char *exchange_url,
     173             :   const char *wire_method,
     174             :   TALER_MERCHANT_PostReservesCallback cb,
     175             :   void *cb_cls)
     176             : {
     177             :   struct TALER_MERCHANT_PostReservesHandle *prh;
     178             :   CURL *eh;
     179             :   json_t *req;
     180             : 
     181           0 :   prh = GNUNET_new (struct TALER_MERCHANT_PostReservesHandle);
     182           0 :   prh->ctx = ctx;
     183           0 :   prh->cb = cb;
     184           0 :   prh->cb_cls = cb_cls;
     185           0 :   prh->url = TALER_url_join (backend_url,
     186             :                              "private/reserves",
     187             :                              NULL);
     188           0 :   if (NULL == prh->url)
     189             :   {
     190           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     191             :                 "Could not construct request URL.\n");
     192           0 :     GNUNET_free (prh);
     193           0 :     return NULL;
     194             :   }
     195           0 :   req = json_pack ("{s:o, s:s, s:s}",
     196             :                    "initial_balance", TALER_JSON_from_amount (initial_balance),
     197             :                    "wire_method", wire_method,
     198             :                    "exchange_url", exchange_url);
     199           0 :   GNUNET_assert (NULL != req);
     200           0 :   eh = curl_easy_init ();
     201           0 :   GNUNET_assert (NULL != eh);
     202           0 :   if (GNUNET_OK != TALER_curl_easy_post (&prh->post_ctx,
     203             :                                          eh,
     204             :                                          req))
     205             :   {
     206           0 :     GNUNET_break (0);
     207           0 :     json_decref (req);
     208           0 :     GNUNET_free (prh);
     209           0 :     return NULL;
     210             :   }
     211           0 :   json_decref (req);
     212           0 :   GNUNET_assert (CURLE_OK ==
     213             :                  curl_easy_setopt (eh,
     214             :                                    CURLOPT_URL,
     215             :                                    prh->url));
     216           0 :   prh->job = GNUNET_CURL_job_add2 (ctx,
     217             :                                    eh,
     218           0 :                                    prh->post_ctx.headers,
     219             :                                    &handle_post_reserves_finished,
     220             :                                    prh);
     221           0 :   return prh;
     222             : }
     223             : 
     224             : 
     225             : void
     226           0 : TALER_MERCHANT_reserves_post_cancel (
     227             :   struct TALER_MERCHANT_PostReservesHandle *prh)
     228             : {
     229           0 :   if (NULL != prh->job)
     230             :   {
     231           0 :     GNUNET_CURL_job_cancel (prh->job);
     232           0 :     prh->job = NULL;
     233             :   }
     234           0 :   GNUNET_free (prh->url);
     235           0 :   TALER_curl_easy_post_finished (&prh->post_ctx);
     236           0 :   GNUNET_free (prh);
     237           0 : }
     238             : 
     239             : 
     240             : /* end of merchant_api_post_reserves.c */

Generated by: LCOV version 1.14