LCOV - code coverage report
Current view: top level - bank-lib - bank_api_common.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 21 27 77.8 %
Date: 2017-09-17 17:24:28 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2015, 2016, 2017 GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, 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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file bank-lib/bank_api_common.c
      19             :  * @brief Common functions for the bank API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include "bank_api_common.h"
      24             : 
      25             : 
      26             : /**
      27             :  * Append HTTP key-value pair to curl header list.
      28             :  *
      29             :  * @param hdr list to append to, can be NULL
      30             :  * @param key key to append
      31             :  * @param value value to append
      32             :  * @return new list, NULL on error
      33             :  */
      34             : static struct curl_slist *
      35          56 : append (struct curl_slist *hdr,
      36             :         const char *key,
      37             :         const char *value)
      38             : {
      39             :   char *str;
      40             :   struct curl_slist *ret;
      41             : 
      42          56 :   GNUNET_asprintf (&str,
      43             :                    "%s: %s",
      44             :                    key,
      45             :                    value);
      46          56 :   ret = curl_slist_append (hdr,
      47             :                            str);
      48          56 :   GNUNET_free (str);
      49          56 :   if (NULL == ret)
      50             :   {
      51           0 :     GNUNET_break (0);
      52           0 :     curl_slist_free_all (hdr);
      53           0 :     return NULL;
      54             :   }
      55          56 :   return ret;
      56             : }
      57             : 
      58             : 
      59             : /**
      60             :  * Build authentication header from @a auth.
      61             :  *
      62             :  * @param auth authentication data to use
      63             :  * @return NULL on error, otherwise curl headers to use
      64             :  */
      65             : struct curl_slist *
      66          28 : TALER_BANK_make_auth_header_ (const struct TALER_BANK_AuthenticationData *auth)
      67             : {
      68             :   struct curl_slist *authh;
      69             : 
      70          28 :   switch (auth->method)
      71             :   {
      72             :   case TALER_BANK_AUTH_NONE:
      73           0 :     return NULL;
      74             :   case TALER_BANK_AUTH_BASIC:
      75          28 :     authh = append (NULL,
      76             :                     "X-Taler-Bank-Username",
      77          28 :                     auth->details.basic.username);
      78          28 :     if (NULL == authh)
      79           0 :       return NULL;
      80          28 :     authh = append (authh,
      81             :                     "X-Taler-Bank-Password",
      82          28 :                     auth->details.basic.password);
      83          28 :     return authh;    
      84             :   }
      85           0 :   return NULL;
      86             : }
      87             : 
      88             : 
      89             : /**
      90             :  * Obtain the URL to use for an API request.
      91             :  *
      92             :  * @param u base URL of the bank
      93             :  * @param path Taler API path (i.e. "/history")
      94             :  * @return the full URI to use with cURL
      95             :  */
      96             : char *
      97          28 : TALER_BANK_path_to_url_ (const char *u,
      98             :                          const char *path)
      99             : {
     100             :   char *url;
     101             : 
     102          56 :   if ( ('/' == path[0]) &&
     103          56 :        (0 < strlen (u)) &&
     104          28 :        ('/' == u[strlen (u) - 1]) )
     105          20 :     path++; /* avoid generating URL with "//" from concat */
     106          28 :   GNUNET_asprintf (&url,
     107             :                    "%s%s",
     108             :                    u,
     109             :                    path);
     110          28 :   return url;
     111             : }
     112             : 
     113             : 
     114             : 
     115             : /* end of bank_api_common.c */

Generated by: LCOV version 1.13