LCOV - code coverage report
Current view: top level - util - url.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 93 105 88.6 %
Date: 2021-08-30 06:43:37 Functions: 9 10 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-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 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 <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file url.c
      18             :  * @brief URL handling utility functions
      19             :  * @author Florian Dold
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler_util.h"
      23             : 
      24             : 
      25             : /**
      26             :  * Check if a character is reserved and should
      27             :  * be urlencoded.
      28             :  *
      29             :  * @param c character to look at
      30             :  * @return true if @a c needs to be urlencoded,
      31             :  *         false otherwise
      32             :  */
      33             : static bool
      34         102 : is_reserved (char c)
      35             : {
      36         102 :   switch (c)
      37             :   {
      38          82 :   case '0': case '1': case '2': case '3': case '4':
      39             :   case '5': case '6': case '7': case '8': case '9':
      40             :   case 'a': case 'b': case 'c': case 'd': case 'e':
      41             :   case 'f': case 'g': case 'h': case 'i': case 'j':
      42             :   case 'k': case 'l': case 'm': case 'n': case 'o':
      43             :   case 'p': case 'q': case 'r': case 's': case 't':
      44             :   case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
      45             :   case 'A': case 'B': case 'C': case 'D': case 'E':
      46             :   case 'F': case 'G': case 'H': case 'I': case 'J':
      47             :   case 'K': case 'L': case 'M': case 'N': case 'O':
      48             :   case 'P': case 'Q': case 'R': case 'S': case 'T':
      49             :   case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
      50             :   case '-': case '.': case '_': case '~':
      51          82 :     return false;
      52          20 :   default:
      53          20 :     break;
      54             :   }
      55          20 :   return true;
      56             : }
      57             : 
      58             : 
      59             : /**
      60             :  * Get the length of a string after it has been
      61             :  * urlencoded.
      62             :  *
      63             :  * @param s the string
      64             :  * @returns the size of the urlencoded @a s
      65             :  */
      66             : static size_t
      67          26 : urlencode_len (const char *s)
      68             : {
      69          26 :   size_t len = 0;
      70          86 :   for (; *s != '\0'; len++, s++)
      71          60 :     if (is_reserved (*s))
      72          11 :       len += 2;
      73          26 :   return len;
      74             : }
      75             : 
      76             : 
      77             : /**
      78             :  * URL-encode a string according to rfc3986.
      79             :  *
      80             :  * @param buf buffer to write the result to
      81             :  * @param s string to encode
      82             :  */
      83             : static void
      84          16 : buffer_write_urlencode (struct GNUNET_Buffer *buf,
      85             :                         const char *s)
      86             : {
      87             :   size_t ulen;
      88             : 
      89          16 :   ulen = urlencode_len (s);
      90          16 :   GNUNET_assert (ulen < ulen + 1);
      91          16 :   GNUNET_buffer_ensure_remaining (buf,
      92             :                                   ulen + 1);
      93          58 :   for (size_t i = 0; i < strlen (s); i++)
      94             :   {
      95          42 :     if (GNUNET_YES == is_reserved (s[i]))
      96           9 :       GNUNET_buffer_write_fstr (buf,
      97             :                                 "%%%02X",
      98           9 :                                 s[i]);
      99             :     else
     100          33 :       buf->mem[buf->position++] = s[i];
     101             :   }
     102          16 : }
     103             : 
     104             : 
     105             : /**
     106             :  * URL-encode a string according to rfc3986.
     107             :  *
     108             :  * @param s string to encode
     109             :  * @returns the urlencoded string, the caller must free it with #GNUNET_free()
     110             :  */
     111             : char *
     112           6 : TALER_urlencode (const char *s)
     113             : {
     114           6 :   struct GNUNET_Buffer buf = { 0 };
     115             : 
     116           6 :   buffer_write_urlencode (&buf,
     117             :                           s);
     118           6 :   return GNUNET_buffer_reap_str (&buf);
     119             : }
     120             : 
     121             : 
     122             : /**
     123             :  * Compute the total length of the @a args given. The args are a
     124             :  * NULL-terminated list of key-value pairs, where the values
     125             :  * must be URL-encoded.  When serializing, the pairs will be separated
     126             :  * via '?' or '&' and an '=' between key and value. Hence each
     127             :  * pair takes an extra 2 characters to encode.  This function computes
     128             :  * how many bytes are needed.  It must match the #serialize_arguments()
     129             :  * function.
     130             :  *
     131             :  * @param args NULL-terminated key-value pairs (char *) for query parameters
     132             :  * @return number of bytes needed (excluding 0-terminator) for the string buffer
     133             :  */
     134             : static size_t
     135         628 : calculate_argument_length (va_list args)
     136             : {
     137         628 :   size_t len = 0;
     138             :   va_list ap;
     139             : 
     140         628 :   va_copy (ap,
     141             :            args);
     142             :   while (1)
     143          11 :   {
     144             :     char *key;
     145             :     char *value;
     146             :     size_t vlen;
     147             :     size_t klen;
     148             : 
     149         639 :     key = va_arg (ap,
     150             :                   char *);
     151         639 :     if (NULL == key)
     152         628 :       break;
     153          11 :     value = va_arg (ap,
     154             :                     char *);
     155          11 :     if (NULL == value)
     156           1 :       continue;
     157          10 :     vlen = urlencode_len (value);
     158          10 :     klen = strlen (key);
     159          10 :     GNUNET_assert ( (len <= len + vlen) &&
     160             :                     (len <= len + vlen + klen) &&
     161             :                     (len < len + vlen + klen + 2) );
     162          10 :     len += vlen + klen + 2;
     163             :   }
     164         628 :   va_end (ap);
     165         628 :   return len;
     166             : }
     167             : 
     168             : 
     169             : /**
     170             :  * Take the key-value pairs in @a args and serialize them into
     171             :  * @a buf, using URL encoding for the values.  If a 'value' is
     172             :  * given as NULL, both the key and the value are skipped. Note
     173             :  * that a NULL value does not terminate the list, only a NULL
     174             :  * key signals the end of the list of arguments.
     175             :  *
     176             :  * @param buf where to write the values
     177             :  * @param args NULL-terminated key-value pairs (char *) for query parameters,
     178             :  *        the value will be url-encoded
     179             :  */
     180             : static void
     181         628 : serialize_arguments (struct GNUNET_Buffer *buf,
     182             :                      va_list args)
     183             : {
     184             :   /* used to indicate if we are processing the initial
     185             :      parameter which starts with '?' or subsequent
     186             :      parameters which are separated with '&' */
     187         628 :   unsigned int iparam = 0;
     188             : 
     189             :   while (1)
     190          11 :   {
     191             :     char *key;
     192             :     char *value;
     193             : 
     194         639 :     key = va_arg (args,
     195             :                   char *);
     196         639 :     if (NULL == key)
     197         628 :       break;
     198          11 :     value = va_arg (args,
     199             :                     char *);
     200          11 :     if (NULL == value)
     201           1 :       continue;
     202          10 :     GNUNET_buffer_write_str (buf,
     203             :                              (0 == iparam) ? "?" : "&");
     204          10 :     iparam = 1;
     205          10 :     GNUNET_buffer_write_str (buf,
     206             :                              key);
     207          10 :     GNUNET_buffer_write_str (buf,
     208             :                              "=");
     209          10 :     buffer_write_urlencode (buf,
     210             :                             value);
     211             :   }
     212         628 : }
     213             : 
     214             : 
     215             : /**
     216             :  * Make an absolute URL with query parameters.
     217             :  *
     218             :  * If a 'value' is given as NULL, both the key and the value are skipped. Note
     219             :  * that a NULL value does not terminate the list, only a NULL key signals the
     220             :  * end of the list of arguments.
     221             :  *
     222             :  * @param base_url absolute base URL to use
     223             :  * @param path path of the url
     224             :  * @param ... NULL-terminated key-value pairs (char *) for query parameters,
     225             :  *        the value will be url-encoded
     226             :  * @returns the URL (must be freed with #GNUNET_free) or
     227             :  *          NULL if an error occurred.
     228             :  */
     229             : char *
     230         627 : TALER_url_join (const char *base_url,
     231             :                 const char *path,
     232             :                 ...)
     233             : {
     234         627 :   struct GNUNET_Buffer buf = { 0 };
     235             :   va_list args;
     236             :   size_t len;
     237             : 
     238         627 :   GNUNET_assert (NULL != base_url);
     239         627 :   GNUNET_assert (NULL != path);
     240         627 :   if (0 == strlen (base_url))
     241             :   {
     242             :     /* base URL can't be empty */
     243           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     244             :                 "Empty base URL specified\n");
     245           0 :     return NULL;
     246             :   }
     247         627 :   if ('/' != base_url[strlen (base_url) - 1])
     248             :   {
     249             :     /* Must be an actual base URL! */
     250           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     251             :                 "Base URL `%s' does not end with '/', cannot join with `%s'\n",
     252             :                 base_url,
     253             :                 path);
     254           0 :     return NULL;
     255             :   }
     256         627 :   if ('/' == path[0])
     257             :   {
     258             :     /* The path must be relative. */
     259           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     260             :                 "Path `%s' is not relative\n",
     261             :                 path);
     262           0 :     return NULL;
     263             :   }
     264             : 
     265         627 :   va_start (args,
     266             :             path);
     267             : 
     268         627 :   len = strlen (base_url) + strlen (path) + 1;
     269         627 :   len += calculate_argument_length (args);
     270             : 
     271         627 :   GNUNET_buffer_prealloc (&buf,
     272             :                           len);
     273         627 :   GNUNET_buffer_write_str (&buf,
     274             :                            base_url);
     275         627 :   GNUNET_buffer_write_str (&buf,
     276             :                            path);
     277         627 :   serialize_arguments (&buf,
     278             :                        args);
     279         627 :   va_end (args);
     280             : 
     281         627 :   return GNUNET_buffer_reap_str (&buf);
     282             : }
     283             : 
     284             : 
     285             : /**
     286             :  * Make an absolute URL for the given parameters.
     287             :  *
     288             :  * If a 'value' is given as NULL, both the key and the value are skipped. Note
     289             :  * that a NULL value does not terminate the list, only a NULL key signals the
     290             :  * end of the list of arguments.
     291             :  *
     292             :  * @param proto protocol for the URL (typically https)
     293             :  * @param host hostname for the URL
     294             :  * @param prefix prefix for the URL
     295             :  * @param path path for the URL
     296             :  * @param args NULL-terminated key-value pairs (char *) for query parameters,
     297             :  *        the value will be url-encoded
     298             :  * @returns the URL, must be freed with #GNUNET_free
     299             :  */
     300             : char *
     301           1 : TALER_url_absolute_raw_va (const char *proto,
     302             :                            const char *host,
     303             :                            const char *prefix,
     304             :                            const char *path,
     305             :                            va_list args)
     306             : {
     307           1 :   struct GNUNET_Buffer buf = { 0 };
     308           1 :   size_t len = 0;
     309             : 
     310           1 :   len += strlen (proto) + strlen ("://") + strlen (host);
     311           1 :   len += strlen (prefix) + strlen (path);
     312           1 :   len += calculate_argument_length (args) + 1; /* 0-terminator */
     313             : 
     314           1 :   GNUNET_buffer_prealloc (&buf,
     315             :                           len);
     316           1 :   GNUNET_buffer_write_str (&buf,
     317             :                            proto);
     318           1 :   GNUNET_buffer_write_str (&buf,
     319             :                            "://");
     320           1 :   GNUNET_buffer_write_str (&buf,
     321             :                            host);
     322           1 :   GNUNET_buffer_write_path (&buf,
     323             :                             prefix);
     324           1 :   GNUNET_buffer_write_path (&buf,
     325             :                             path);
     326           1 :   serialize_arguments (&buf,
     327             :                        args);
     328           1 :   return GNUNET_buffer_reap_str (&buf);
     329             : }
     330             : 
     331             : 
     332             : /**
     333             :  * Make an absolute URL for the given parameters.
     334             :  *
     335             :  * If a 'value' is given as NULL, both the key and the value are skipped. Note
     336             :  * that a NULL value does not terminate the list, only a NULL key signals the
     337             :  * end of the list of arguments.
     338             :  *
     339             :  * @param proto protocol for the URL (typically https)
     340             :  * @param host hostname for the URL
     341             :  * @param prefix prefix for the URL
     342             :  * @param path path for the URL
     343             :  * @param ... NULL-terminated key-value pairs (char *) for query parameters,
     344             :  *        the value will be url-encoded
     345             :  * @return the URL, must be freed with #GNUNET_free
     346             :  */
     347             : char *
     348           1 : TALER_url_absolute_raw (const char *proto,
     349             :                         const char *host,
     350             :                         const char *prefix,
     351             :                         const char *path,
     352             :                         ...)
     353             : {
     354             :   char *result;
     355             :   va_list args;
     356             : 
     357           1 :   va_start (args,
     358             :             path);
     359           1 :   result = TALER_url_absolute_raw_va (proto,
     360             :                                       host,
     361             :                                       prefix,
     362             :                                       path,
     363             :                                       args);
     364           1 :   va_end (args);
     365           1 :   return result;
     366             : }
     367             : 
     368             : 
     369             : bool
     370           0 : TALER_url_valid_charset (const char *url)
     371             : {
     372           0 :   for (unsigned int i = 0; '\0' != url[i]; i++)
     373             :   {
     374             : #define ALLOWED_CHARACTERS \
     375             :   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/:;&?-.,=_~%"
     376           0 :     if (NULL == strchr (ALLOWED_CHARACTERS,
     377           0 :                         (int) url[i]))
     378           0 :       return false;
     379             : #undef ALLOWED_CHARACTERS
     380             :   }
     381           0 :   return true;
     382             : }
     383             : 
     384             : 
     385             : /* end of url.c */

Generated by: LCOV version 1.14