exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

curl.c (4964B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2019-2024 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it
      6   under the terms of the GNU General Public License as published
      7   by the Free Software Foundation; either version 3, or (at your
      8   option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file curl/curl.c
     21  * @brief Helper routines for interactions with libcurl
     22  * @author Christian Grothoff
     23  */
     24 #include "taler/taler_curl_lib.h"
     25 
     26 
     27 #if TALER_CURL_COMPRESS_BODIES
     28 #include <zlib.h>
     29 #endif
     30 
     31 
     32 void
     33 TALER_curl_set_secure_redirect_policy (CURL *eh,
     34                                        const char *url)
     35 {
     36   GNUNET_assert (CURLE_OK ==
     37                  curl_easy_setopt (eh,
     38                                    CURLOPT_FOLLOWLOCATION,
     39                                    1L));
     40   GNUNET_assert ( (0 == strncasecmp (url,
     41                                      "https://",
     42                                      strlen ("https://"))) ||
     43                   (0 == strncasecmp (url,
     44                                      "http://",
     45                                      strlen ("http://"))) );
     46 #ifdef CURLOPT_REDIR_PROTOCOLS_STR
     47   if (0 == strncasecmp (url,
     48                         "https://",
     49                         strlen ("https://")))
     50     GNUNET_assert (CURLE_OK ==
     51                    curl_easy_setopt (eh,
     52                                      CURLOPT_REDIR_PROTOCOLS_STR,
     53                                      "https"));
     54   else
     55     GNUNET_assert (CURLE_OK ==
     56                    curl_easy_setopt (eh,
     57                                      CURLOPT_REDIR_PROTOCOLS_STR,
     58                                      "http,https"));
     59 #else
     60 #ifdef CURLOPT_REDIR_PROTOCOLS
     61   if (0 == strncasecmp (url,
     62                         "https://",
     63                         strlen ("https://")))
     64     GNUNET_assert (CURLE_OK ==
     65                    curl_easy_setopt (eh,
     66                                      CURLOPT_REDIR_PROTOCOLS,
     67                                      CURLPROTO_HTTPS));
     68   else
     69     GNUNET_assert (CURLE_OK ==
     70                    curl_easy_setopt (eh,
     71                                      CURLOPT_REDIR_PROTOCOLS,
     72                                      CURLPROTO_HTTP | CURLPROTO_HTTPS));
     73 #endif
     74 #endif
     75   /* limit MAXREDIRS to 5 as a simple security measure against
     76      a potential infinite loop caused by a malicious target */
     77   GNUNET_assert (CURLE_OK ==
     78                  curl_easy_setopt (eh,
     79                                    CURLOPT_MAXREDIRS,
     80                                    5L));
     81 }
     82 
     83 
     84 enum GNUNET_GenericReturnValue
     85 TALER_curl_easy_post (struct TALER_CURL_PostContext *ctx,
     86                       CURL *eh,
     87                       const json_t *body)
     88 {
     89   char *str;
     90   size_t slen;
     91 
     92   str = json_dumps (body,
     93                     JSON_COMPACT);
     94   if (NULL == str)
     95   {
     96     GNUNET_break (0);
     97     return GNUNET_SYSERR;
     98   }
     99   slen = strlen (str);
    100   if (TALER_CURL_COMPRESS_BODIES &&
    101       (! ctx->disable_compression) )
    102   {
    103     Bytef *cbuf;
    104     uLongf cbuf_size;
    105     int ret;
    106 
    107     cbuf_size = compressBound (slen);
    108     cbuf = GNUNET_malloc (cbuf_size);
    109     ret = compress (cbuf,
    110                     &cbuf_size,
    111                     (const Bytef *) str,
    112                     slen);
    113     if (Z_OK != ret)
    114     {
    115       /* compression failed!? */
    116       GNUNET_break (0);
    117       GNUNET_free (cbuf);
    118       free (str);
    119       return GNUNET_SYSERR;
    120     }
    121     free (str);
    122     slen = (size_t) cbuf_size;
    123     ctx->json_enc = (char *) cbuf;
    124     GNUNET_assert (
    125       NULL !=
    126       (ctx->headers = curl_slist_append (
    127          ctx->headers,
    128          "Content-Encoding: deflate")));
    129   }
    130   else
    131   {
    132     ctx->json_enc = str;
    133   }
    134   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    135               "Uploading JSON of %d bytes (%s)\n",
    136               (int) slen,
    137               (TALER_CURL_COMPRESS_BODIES &&
    138                (! ctx->disable_compression) )
    139               ? "compressed"
    140               : "uncompressed");
    141   GNUNET_assert (
    142     NULL !=
    143     (ctx->headers = curl_slist_append (
    144        ctx->headers,
    145        "Content-Type: application/json")));
    146 
    147   GNUNET_assert (CURLE_OK ==
    148                  curl_easy_setopt (eh,
    149                                    CURLOPT_POSTFIELDS,
    150                                    ctx->json_enc));
    151   GNUNET_assert (CURLE_OK ==
    152                  curl_easy_setopt (eh,
    153                                    CURLOPT_POSTFIELDSIZE,
    154                                    (long) slen));
    155   return GNUNET_OK;
    156 }
    157 
    158 
    159 void
    160 TALER_curl_easy_post_finished (struct TALER_CURL_PostContext *ctx)
    161 {
    162   curl_slist_free_all (ctx->headers);
    163   ctx->headers = NULL;
    164   GNUNET_free (ctx->json_enc);
    165   ctx->json_enc = NULL;
    166 }