LCOV - code coverage report
Current view: top level - lib - merchant_api_get_config.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 61 0.0 %
Date: 2020-08-15 06:12:35 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-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_config.c
      19             :  * @brief Implementation of the /config 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             :  * Which version of the Taler protocol is implemented
      34             :  * by this library?  Used to determine compatibility.
      35             :  */
      36             : #define MERCHANT_PROTOCOL_CURRENT 1
      37             : 
      38             : /**
      39             :  * How many configs are we backwards compatible with?
      40             :  */
      41             : #define MERCHANT_PROTOCOL_AGE 0
      42             : 
      43             : 
      44             : /**
      45             :  * @brief A handle for /config operations
      46             :  */
      47             : struct TALER_MERCHANT_ConfigGetHandle
      48             : {
      49             :   /**
      50             :    * The url for this request.
      51             :    */
      52             :   char *url;
      53             : 
      54             :   /**
      55             :    * Handle for the request.
      56             :    */
      57             :   struct GNUNET_CURL_Job *job;
      58             : 
      59             :   /**
      60             :    * Function to call with the result.
      61             :    */
      62             :   TALER_MERCHANT_ConfigCallback cb;
      63             : 
      64             :   /**
      65             :    * Closure for @a cb.
      66             :    */
      67             :   void *cb_cls;
      68             : 
      69             :   /**
      70             :    * Reference to the execution context.
      71             :    */
      72             :   struct GNUNET_CURL_Context *ctx;
      73             : 
      74             : };
      75             : 
      76             : 
      77             : /**
      78             :  * Function called when we're done processing the
      79             :  * HTTP /config request.
      80             :  *
      81             :  * @param cls the `struct TALER_MERCHANT_ConfigGetHandle`
      82             :  * @param response_code HTTP response code, 0 on error
      83             :  * @param json response body, NULL if not in JSON
      84             :  */
      85             : static void
      86           0 : handle_config_finished (void *cls,
      87             :                         long response_code,
      88             :                         const void *response)
      89             : {
      90           0 :   struct TALER_MERCHANT_ConfigGetHandle *vgh = cls;
      91           0 :   const json_t *json = response;
      92           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      93           0 :     .http_status = (unsigned int) response_code,
      94             :     .reply = json
      95             :   };
      96             : 
      97           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      98             :               "Got /config response with status code %u\n",
      99             :               (unsigned int) response_code);
     100             : 
     101           0 :   vgh->job = NULL;
     102           0 :   switch (response_code)
     103             :   {
     104           0 :   case MHD_HTTP_OK:
     105             :     {
     106             :       struct TALER_MERCHANT_ConfigInformation vi;
     107           0 :       enum TALER_MERCHANT_VersionCompatibility vc =
     108             :         TALER_MERCHANT_VC_PROTOCOL_ERROR;
     109             :       struct GNUNET_JSON_Specification spec[] = {
     110           0 :         GNUNET_JSON_spec_string ("currency",
     111             :                                  &vi.currency),
     112           0 :         GNUNET_JSON_spec_string ("version",
     113             :                                  &vi.version),
     114           0 :         GNUNET_JSON_spec_end ()
     115             :       };
     116             : 
     117           0 :       if (GNUNET_OK !=
     118           0 :           GNUNET_JSON_parse (json,
     119             :                              spec,
     120             :                              NULL, NULL))
     121             :       {
     122           0 :         hr.http_status = 0;
     123           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     124             :       }
     125             :       else
     126             :       {
     127             :         unsigned int age;
     128             :         unsigned int revision;
     129             :         unsigned int current;
     130             : 
     131           0 :         if (3 != sscanf (vi.version,
     132             :                          "%u:%u:%u",
     133             :                          &current,
     134             :                          &revision,
     135             :                          &age))
     136             :         {
     137           0 :           hr.http_status = 0;
     138           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     139             :         }
     140             :         else
     141             :         {
     142           0 :           vc = TALER_MERCHANT_VC_MATCH;
     143           0 :           if (MERCHANT_PROTOCOL_CURRENT < current)
     144             :           {
     145           0 :             vc |= TALER_MERCHANT_VC_NEWER;
     146           0 :             if (MERCHANT_PROTOCOL_CURRENT < current - age)
     147           0 :               vc |= TALER_MERCHANT_VC_INCOMPATIBLE;
     148             :           }
     149           0 :           if (MERCHANT_PROTOCOL_CURRENT > current)
     150             :           {
     151           0 :             vc |= TALER_MERCHANT_VC_OLDER;
     152           0 :             if (MERCHANT_PROTOCOL_CURRENT - MERCHANT_PROTOCOL_AGE > current)
     153           0 :               vc |= TALER_MERCHANT_VC_INCOMPATIBLE;
     154             :           }
     155             :         }
     156             :       }
     157           0 :       vgh->cb (vgh->cb_cls,
     158             :                &hr,
     159             :                &vi,
     160             :                vc);
     161           0 :       TALER_MERCHANT_config_get_cancel (vgh);
     162           0 :       return;
     163             :     }
     164           0 :   default:
     165             :     /* unexpected response code */
     166           0 :     hr.ec = TALER_JSON_get_error_code (json);
     167           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     168           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     169             :                 "Unexpected response code %u/%d\n",
     170             :                 (unsigned int) response_code,
     171             :                 (int) hr.ec);
     172           0 :     vgh->cb (vgh->cb_cls,
     173             :              &hr,
     174             :              NULL,
     175             :              TALER_MERCHANT_VC_PROTOCOL_ERROR);
     176           0 :     break;
     177             :   }
     178           0 :   TALER_MERCHANT_config_get_cancel (vgh);
     179             : }
     180             : 
     181             : 
     182             : /**
     183             :  * Get the config data of a merchant. Will connect to the merchant backend
     184             :  * and obtain information about the backend.  The respective information will
     185             :  * be passed to the @a config_cb once available.
     186             :  *
     187             :  * @param ctx the context
     188             :  * @param backend_url HTTP base URL for the backend
     189             :  * @param config_cb function to call with the
     190             :  *        backend's config information
     191             :  * @param config_cb_cls closure for @a config_cb
     192             :  * @return the config check handle; NULL upon error
     193             :  */
     194             : struct TALER_MERCHANT_ConfigGetHandle *
     195           0 : TALER_MERCHANT_config_get (struct GNUNET_CURL_Context *ctx,
     196             :                            const char *backend_url,
     197             :                            TALER_MERCHANT_ConfigCallback config_cb,
     198             :                            void *config_cb_cls)
     199             : {
     200             :   struct TALER_MERCHANT_ConfigGetHandle *vgh;
     201             :   CURL *eh;
     202             : 
     203           0 :   vgh = GNUNET_new (struct TALER_MERCHANT_ConfigGetHandle);
     204           0 :   vgh->ctx = ctx;
     205           0 :   vgh->cb = config_cb;
     206           0 :   vgh->cb_cls = config_cb_cls;
     207           0 :   vgh->url = TALER_url_join (backend_url,
     208             :                              "config",
     209             :                              NULL);
     210           0 :   if (NULL == vgh->url)
     211             :   {
     212           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     213             :                 "Could not construct request URL.\n");
     214           0 :     GNUNET_free (vgh);
     215           0 :     return NULL;
     216             :   }
     217           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     218             :               "Requesting URL '%s'\n",
     219             :               vgh->url);
     220           0 :   eh = curl_easy_init ();
     221           0 :   GNUNET_assert (CURLE_OK ==
     222             :                  curl_easy_setopt (eh,
     223             :                                    CURLOPT_URL,
     224             :                                    vgh->url));
     225             : 
     226           0 :   vgh->job = GNUNET_CURL_job_add (ctx,
     227             :                                   eh,
     228             :                                   &handle_config_finished,
     229             :                                   vgh);
     230           0 :   return vgh;
     231             : }
     232             : 
     233             : 
     234             : /**
     235             :  * Cancel /config request.  Must not be called by clients after
     236             :  * the callback was invoked.
     237             :  *
     238             :  * @param vgh request to cancel.
     239             :  */
     240             : void
     241           0 : TALER_MERCHANT_config_get_cancel (struct TALER_MERCHANT_ConfigGetHandle *vgh)
     242             : {
     243           0 :   if (NULL != vgh->job)
     244             :   {
     245           0 :     GNUNET_CURL_job_cancel (vgh->job);
     246           0 :     vgh->job = NULL;
     247             :   }
     248           0 :   GNUNET_free (vgh->url);
     249           0 :   GNUNET_free (vgh);
     250           0 : }
     251             : 
     252             : 
     253             : /* end of merchant_api_config_get.c */

Generated by: LCOV version 1.14