LCOV - code coverage report
Current view: top level - lib - merchant_api_get_instances.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 91 0.0 %
Date: 2022-08-25 06:17:04 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-2018, 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_get_instances.c
      19             :  * @brief Implementation of the GET /instances request of the merchant's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * Handle for a GET /instances operation.
      36             :  */
      37             : struct TALER_MERCHANT_InstancesGetHandle
      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_InstancesGetCallback 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             : 
      66             : 
      67             : /**
      68             :  * Parse instance information from @a ia.
      69             :  *
      70             :  * @param ia JSON array (or NULL!) with instance data
      71             :  * @param igh operation handle
      72             :  * @return #GNUNET_OK on success
      73             :  */
      74             : static int
      75           0 : parse_instances (const json_t *ia,
      76             :                  struct TALER_MERCHANT_InstancesGetHandle *igh)
      77           0 : {
      78           0 :   unsigned int iis_len = json_array_size (ia);
      79           0 :   struct TALER_MERCHANT_InstanceInformation iis[iis_len];
      80             :   size_t index;
      81             :   json_t *value;
      82             :   int ret;
      83             : 
      84           0 :   ret = GNUNET_OK;
      85           0 :   json_array_foreach (ia, index, value) {
      86           0 :     struct TALER_MERCHANT_InstanceInformation *ii = &iis[index];
      87             :     struct GNUNET_JSON_Specification spec[] = {
      88           0 :       GNUNET_JSON_spec_string ("name",
      89             :                                &ii->name),
      90           0 :       GNUNET_JSON_spec_string ("id",
      91             :                                &ii->id),
      92           0 :       GNUNET_JSON_spec_fixed_auto ("merchant_pub",
      93             :                                    &ii->merchant_pub),
      94           0 :       GNUNET_JSON_spec_json ("payment_targets",
      95             :                              &ii->payment_targets),
      96           0 :       GNUNET_JSON_spec_end ()
      97             :     };
      98             : 
      99           0 :     if (GNUNET_OK !=
     100           0 :         GNUNET_JSON_parse (value,
     101             :                            spec,
     102             :                            NULL, NULL))
     103             :     {
     104           0 :       GNUNET_break_op (0);
     105           0 :       ret = GNUNET_SYSERR;
     106           0 :       continue;
     107             :     }
     108           0 :     if (! json_is_array (ii->payment_targets))
     109             :     {
     110           0 :       GNUNET_break_op (0);
     111           0 :       ret = GNUNET_SYSERR;
     112           0 :       break;
     113             :     }
     114           0 :     for (unsigned int i = 0; i<json_array_size (ii->payment_targets); i++)
     115             :     {
     116           0 :       if  (! json_is_string (json_array_get (ii->payment_targets,
     117             :                                              i)))
     118             :       {
     119           0 :         GNUNET_break_op (0);
     120           0 :         ret = GNUNET_SYSERR;
     121           0 :         break;
     122             :       }
     123             :     }
     124           0 :     if (GNUNET_SYSERR == ret)
     125           0 :       break;
     126             :   }
     127           0 :   if (GNUNET_OK == ret)
     128             :   {
     129           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     130             :       .http_status = MHD_HTTP_OK
     131             :     };
     132             : 
     133           0 :     igh->cb (igh->cb_cls,
     134             :              &hr,
     135             :              iis_len,
     136             :              iis);
     137           0 :     igh->cb = NULL; /* just to be sure */
     138             :   }
     139           0 :   for (unsigned int i = 0; i<iis_len; i++)
     140           0 :     if (NULL != iis[i].payment_targets)
     141           0 :       json_decref (iis[i].payment_targets);
     142           0 :   return ret;
     143             : }
     144             : 
     145             : 
     146             : /**
     147             :  * Function called when we're done processing the
     148             :  * HTTP /instances request.
     149             :  *
     150             :  * @param cls the `struct TALER_MERCHANT_InstancesGetHandle`
     151             :  * @param response_code HTTP response code, 0 on error
     152             :  * @param response response body, NULL if not in JSON
     153             :  */
     154             : static void
     155           0 : handle_instances_finished (void *cls,
     156             :                            long response_code,
     157             :                            const void *response)
     158             : {
     159           0 :   struct TALER_MERCHANT_InstancesGetHandle *igh = cls;
     160           0 :   const json_t *json = response;
     161           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     162           0 :     .http_status = (unsigned int) response_code,
     163             :     .reply = json
     164             :   };
     165             : 
     166           0 :   igh->job = NULL;
     167           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     168             :               "Got /instances response with status code %u\n",
     169             :               (unsigned int) response_code);
     170           0 :   switch (response_code)
     171             :   {
     172           0 :   case MHD_HTTP_OK:
     173             :     {
     174             :       json_t *instances;
     175             :       struct GNUNET_JSON_Specification spec[] = {
     176           0 :         GNUNET_JSON_spec_json ("instances",
     177             :                                &instances),
     178           0 :         GNUNET_JSON_spec_end ()
     179             :       };
     180             : 
     181           0 :       if (GNUNET_OK !=
     182           0 :           GNUNET_JSON_parse (json,
     183             :                              spec,
     184             :                              NULL, NULL))
     185             :       {
     186           0 :         hr.http_status = 0;
     187           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     188             :       }
     189             :       else
     190             :       {
     191           0 :         if ( (! json_is_array (instances)) ||
     192             :              (GNUNET_OK ==
     193           0 :               parse_instances (instances,
     194             :                                igh)) )
     195             :         {
     196           0 :           GNUNET_JSON_parse_free (spec);
     197           0 :           TALER_MERCHANT_instances_get_cancel (igh);
     198           0 :           return;
     199             :         }
     200             :         else
     201             :         {
     202           0 :           hr.http_status = 0;
     203           0 :           hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     204             :         }
     205             :       }
     206           0 :       GNUNET_JSON_parse_free (spec);
     207           0 :       break;
     208             :     }
     209           0 :   case MHD_HTTP_UNAUTHORIZED:
     210           0 :     hr.ec = TALER_JSON_get_error_code (json);
     211           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     212           0 :     break;
     213           0 :   default:
     214             :     /* unexpected response code */
     215           0 :     hr.ec = TALER_JSON_get_error_code (json);
     216           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     217           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     218             :                 "Unexpected response code %u/%d\n",
     219             :                 (unsigned int) response_code,
     220             :                 (int) hr.ec);
     221           0 :     break;
     222             :   }
     223           0 :   igh->cb (igh->cb_cls,
     224             :            &hr,
     225             :            0,
     226             :            NULL);
     227           0 :   TALER_MERCHANT_instances_get_cancel (igh);
     228             : }
     229             : 
     230             : 
     231             : struct TALER_MERCHANT_InstancesGetHandle *
     232           0 : TALER_MERCHANT_instances_get (struct GNUNET_CURL_Context *ctx,
     233             :                               const char *backend_url,
     234             :                               TALER_MERCHANT_InstancesGetCallback instances_cb,
     235             :                               void *instances_cb_cls)
     236             : {
     237             :   struct TALER_MERCHANT_InstancesGetHandle *igh;
     238             :   CURL *eh;
     239             : 
     240           0 :   igh = GNUNET_new (struct TALER_MERCHANT_InstancesGetHandle);
     241           0 :   igh->ctx = ctx;
     242           0 :   igh->cb = instances_cb;
     243           0 :   igh->cb_cls = instances_cb_cls;
     244           0 :   igh->url = TALER_url_join (backend_url,
     245             :                              "management/instances",
     246             :                              NULL);
     247           0 :   if (NULL == igh->url)
     248             :   {
     249           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     250             :                 "Could not construct request URL.\n");
     251           0 :     GNUNET_free (igh);
     252           0 :     return NULL;
     253             :   }
     254           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     255             :               "Requesting URL '%s'\n",
     256             :               igh->url);
     257           0 :   eh = TALER_MERCHANT_curl_easy_get_ (igh->url);
     258           0 :   igh->job = GNUNET_CURL_job_add (ctx,
     259             :                                   eh,
     260             :                                   &handle_instances_finished,
     261             :                                   igh);
     262           0 :   return igh;
     263             : }
     264             : 
     265             : 
     266             : void
     267           0 : TALER_MERCHANT_instances_get_cancel (
     268             :   struct TALER_MERCHANT_InstancesGetHandle *igh)
     269             : {
     270           0 :   if (NULL != igh->job)
     271           0 :     GNUNET_CURL_job_cancel (igh->job);
     272           0 :   GNUNET_free (igh->url);
     273           0 :   GNUNET_free (igh);
     274           0 : }

Generated by: LCOV version 1.14