LCOV - code coverage report
Current view: top level - lib - merchant_api_get_instances.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 88 0.0 %
Date: 2020-08-15 06:12:35 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 lib/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 <taler/taler_json_lib.h>
      30             : #include <taler/taler_signatures.h>
      31             : 
      32             : 
      33             : /**
      34             :  * Handle for a GET /instances operation.
      35             :  */
      36             : struct TALER_MERCHANT_InstancesGetHandle
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Handle for the request.
      45             :    */
      46             :   struct GNUNET_CURL_Job *job;
      47             : 
      48             :   /**
      49             :    * Function to call with the result.
      50             :    */
      51             :   TALER_MERCHANT_InstancesGetCallback cb;
      52             : 
      53             :   /**
      54             :    * Closure for @a cb.
      55             :    */
      56             :   void *cb_cls;
      57             : 
      58             :   /**
      59             :    * Reference to the execution context.
      60             :    */
      61             :   struct GNUNET_CURL_Context *ctx;
      62             : 
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Parse instance information from @a ia.
      68             :  *
      69             :  * @param ia JSON array (or NULL!) with instance data
      70             :  * @param igh operation handle
      71             :  * @return #GNUNET_OK on success
      72             :  */
      73             : static int
      74           0 : parse_instances (const json_t *ia,
      75             :                  struct TALER_MERCHANT_InstancesGetHandle *igh)
      76           0 : {
      77           0 :   unsigned int iis_len = json_array_size (ia);
      78           0 :   struct TALER_MERCHANT_InstanceInformation iis[iis_len];
      79             :   size_t index;
      80             :   json_t *value;
      81             :   int ret;
      82             : 
      83           0 :   ret = GNUNET_OK;
      84           0 :   json_array_foreach (ia, index, value) {
      85           0 :     struct TALER_MERCHANT_InstanceInformation *ii = &iis[index];
      86             :     struct GNUNET_JSON_Specification spec[] = {
      87           0 :       GNUNET_JSON_spec_string ("name",
      88             :                                &ii->name),
      89           0 :       GNUNET_JSON_spec_string ("id",
      90             :                                &ii->id),
      91           0 :       GNUNET_JSON_spec_fixed_auto ("merchant_pub",
      92             :                                    &ii->merchant_pub),
      93           0 :       GNUNET_JSON_spec_json ("payment_targets",
      94             :                              &ii->payment_targets),
      95           0 :       GNUNET_JSON_spec_end ()
      96             :     };
      97             : 
      98           0 :     if (GNUNET_OK !=
      99           0 :         GNUNET_JSON_parse (value,
     100             :                            spec,
     101             :                            NULL, NULL))
     102             :     {
     103           0 :       GNUNET_break_op (0);
     104           0 :       ret = GNUNET_SYSERR;
     105           0 :       continue;
     106             :     }
     107           0 :     if (! json_is_array (ii->payment_targets))
     108             :     {
     109           0 :       GNUNET_break_op (0);
     110           0 :       ret = GNUNET_SYSERR;
     111           0 :       break;
     112             :     }
     113           0 :     for (unsigned int i = 0; i<json_array_size (ii->payment_targets); i++)
     114             :     {
     115           0 :       if  (! json_is_string (json_array_get (ii->payment_targets,
     116             :                                              i)))
     117             :       {
     118           0 :         GNUNET_break_op (0);
     119           0 :         ret = GNUNET_SYSERR;
     120           0 :         break;
     121             :       }
     122             :     }
     123           0 :     if (GNUNET_SYSERR == ret)
     124           0 :       break;
     125             :   }
     126           0 :   if (GNUNET_OK == ret)
     127             :   {
     128           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     129             :       .http_status = MHD_HTTP_OK
     130             :     };
     131             : 
     132           0 :     igh->cb (igh->cb_cls,
     133             :              &hr,
     134             :              iis_len,
     135             :              iis);
     136           0 :     igh->cb = NULL; /* just to be sure */
     137             :   }
     138           0 :   for (unsigned int i = 0; i<iis_len; i++)
     139           0 :     if (NULL != iis[i].payment_targets)
     140           0 :       json_decref (iis[i].payment_targets);
     141           0 :   return ret;
     142             : }
     143             : 
     144             : 
     145             : /**
     146             :  * Function called when we're done processing the
     147             :  * HTTP /instances request.
     148             :  *
     149             :  * @param cls the `struct TALER_MERCHANT_InstancesGetHandle`
     150             :  * @param response_code HTTP response code, 0 on error
     151             :  * @param json response body, NULL if not in JSON
     152             :  */
     153             : static void
     154           0 : handle_instances_finished (void *cls,
     155             :                            long response_code,
     156             :                            const void *response)
     157             : {
     158           0 :   struct TALER_MERCHANT_InstancesGetHandle *igh = cls;
     159           0 :   const json_t *json = response;
     160           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     161           0 :     .http_status = (unsigned int) response_code,
     162             :     .reply = json
     163             :   };
     164             : 
     165           0 :   igh->job = NULL;
     166           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     167             :               "Got /instances response with status code %u\n",
     168             :               (unsigned int) response_code);
     169           0 :   switch (response_code)
     170             :   {
     171           0 :   case MHD_HTTP_OK:
     172             :     {
     173             :       json_t *instances;
     174             :       struct GNUNET_JSON_Specification spec[] = {
     175           0 :         GNUNET_JSON_spec_json ("instances",
     176             :                                &instances),
     177           0 :         GNUNET_JSON_spec_end ()
     178             :       };
     179             : 
     180           0 :       if (GNUNET_OK !=
     181           0 :           GNUNET_JSON_parse (json,
     182             :                              spec,
     183             :                              NULL, NULL))
     184             :       {
     185           0 :         hr.http_status = 0;
     186           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     187             :       }
     188             :       else
     189             :       {
     190           0 :         if ( (! json_is_array (instances)) ||
     191             :              (GNUNET_OK ==
     192           0 :               parse_instances (instances,
     193             :                                igh)) )
     194             :         {
     195           0 :           GNUNET_JSON_parse_free (spec);
     196           0 :           TALER_MERCHANT_instances_get_cancel (igh);
     197           0 :           return;
     198             :         }
     199             :         else
     200             :         {
     201           0 :           hr.http_status = 0;
     202           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     203             :         }
     204             :       }
     205           0 :       GNUNET_JSON_parse_free (spec);
     206           0 :       break;
     207             :     }
     208           0 :   default:
     209             :     /* unexpected response code */
     210           0 :     hr.ec = TALER_JSON_get_error_code (json);
     211           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     212           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     213             :                 "Unexpected response code %u/%d\n",
     214             :                 (unsigned int) response_code,
     215             :                 (int) hr.ec);
     216           0 :     break;
     217             :   }
     218           0 :   igh->cb (igh->cb_cls,
     219             :            &hr,
     220             :            0,
     221             :            NULL);
     222           0 :   TALER_MERCHANT_instances_get_cancel (igh);
     223             : }
     224             : 
     225             : 
     226             : /**
     227             :  * Get the instance data of a backend. Will connect to the merchant backend
     228             :  * and obtain information about the instances.  The respective information will
     229             :  * be passed to the @a instances_cb once available.
     230             :  *
     231             :  * @param ctx the context
     232             :  * @param backend_url HTTP base URL for the backend
     233             :  * @param instances_cb function to call with the
     234             :  *        backend's instances information
     235             :  * @param instances_cb_cls closure for @a config_cb
     236             :  * @return the instances handle; NULL upon error
     237             :  */
     238             : struct TALER_MERCHANT_InstancesGetHandle *
     239           0 : TALER_MERCHANT_instances_get (struct GNUNET_CURL_Context *ctx,
     240             :                               const char *backend_url,
     241             :                               TALER_MERCHANT_InstancesGetCallback instances_cb,
     242             :                               void *instances_cb_cls)
     243             : {
     244             :   struct TALER_MERCHANT_InstancesGetHandle *igh;
     245             :   CURL *eh;
     246             : 
     247           0 :   igh = GNUNET_new (struct TALER_MERCHANT_InstancesGetHandle);
     248           0 :   igh->ctx = ctx;
     249           0 :   igh->cb = instances_cb;
     250           0 :   igh->cb_cls = instances_cb_cls;
     251           0 :   igh->url = TALER_url_join (backend_url,
     252             :                              "private/instances",
     253             :                              NULL);
     254           0 :   if (NULL == igh->url)
     255             :   {
     256           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     257             :                 "Could not construct request URL.\n");
     258           0 :     GNUNET_free (igh);
     259           0 :     return NULL;
     260             :   }
     261           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     262             :               "Requesting URL '%s'\n",
     263             :               igh->url);
     264           0 :   eh = curl_easy_init ();
     265           0 :   GNUNET_assert (CURLE_OK ==
     266             :                  curl_easy_setopt (eh,
     267             :                                    CURLOPT_URL,
     268             :                                    igh->url));
     269           0 :   igh->job = GNUNET_CURL_job_add (ctx,
     270             :                                   eh,
     271             :                                   &handle_instances_finished,
     272             :                                   igh);
     273           0 :   return igh;
     274             : }
     275             : 
     276             : 
     277             : /**
     278             :  * Cancel /instances request.  Must not be called by clients after
     279             :  * the callback was invoked.
     280             :  *
     281             :  * @param igh request to cancel.
     282             :  */
     283             : void
     284           0 : TALER_MERCHANT_instances_get_cancel (
     285             :   struct TALER_MERCHANT_InstancesGetHandle *igh)
     286             : {
     287           0 :   if (NULL != igh->job)
     288           0 :     GNUNET_CURL_job_cancel (igh->job);
     289           0 :   GNUNET_free (igh->url);
     290           0 :   GNUNET_free (igh);
     291           0 : }

Generated by: LCOV version 1.14