LCOV - code coverage report
Current view: top level - curl - curl.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 22 27 81.5 %
Date: 2021-08-30 06:43:37 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2019-2020 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 "platform.h"
      25             : #include "taler_curl_lib.h"
      26             : 
      27             : #if COMPRESS_BODIES
      28             : #include <zlib.h>
      29             : #endif
      30             : 
      31             : 
      32             : /**
      33             :  * Add the @a body as POST data to the easy handle in @a ctx.
      34             :  *
      35             :  * @param[in,out] ctx a request context (updated)
      36             :  * @param eh easy handle to use
      37             :  * @param body JSON body to add to @e ctx
      38             :  * @return #GNUNET_OK on success #GNUNET_SYSERR on failure
      39             :  */
      40             : int
      41         281 : TALER_curl_easy_post (struct TALER_CURL_PostContext *ctx,
      42             :                       CURL *eh,
      43             :                       const json_t *body)
      44             : {
      45             :   char *str;
      46             :   size_t slen;
      47             : 
      48         281 :   str = json_dumps (body,
      49             :                     JSON_COMPACT);
      50         281 :   if (NULL == str)
      51             :   {
      52           0 :     GNUNET_break (0);
      53           0 :     return GNUNET_SYSERR;
      54             :   }
      55         281 :   slen = strlen (str);
      56             : #if COMPRESS_BODIES
      57             :   {
      58             :     Bytef *cbuf;
      59             :     uLongf cbuf_size;
      60             :     int ret;
      61             : 
      62         281 :     cbuf_size = compressBound (slen);
      63         281 :     cbuf = GNUNET_malloc (cbuf_size);
      64         281 :     ret = compress (cbuf,
      65             :                     &cbuf_size,
      66             :                     (const Bytef *) str,
      67             :                     slen);
      68         281 :     if (Z_OK != ret)
      69             :     {
      70             :       /* compression failed!? */
      71           0 :       GNUNET_break (0);
      72           0 :       GNUNET_free (cbuf);
      73           0 :       return GNUNET_SYSERR;
      74             :     }
      75         281 :     free (str);
      76         281 :     slen = (size_t) cbuf_size;
      77         281 :     ctx->json_enc = (char *) cbuf;
      78             :   }
      79         281 :   GNUNET_assert
      80             :     (NULL != (ctx->headers = curl_slist_append
      81             :                                (ctx->headers,
      82             :                                "Content-Encoding: deflate")));
      83             : #else
      84             :   ctx->json_enc = str;
      85             : #endif
      86             : 
      87         281 :   GNUNET_assert
      88             :     (NULL != (ctx->headers = curl_slist_append
      89             :                                (ctx->headers,
      90             :                                "Content-Type: application/json")));
      91             : 
      92         281 :   GNUNET_assert (CURLE_OK ==
      93             :                  curl_easy_setopt (eh,
      94             :                                    CURLOPT_POSTFIELDS,
      95             :                                    ctx->json_enc));
      96         281 :   GNUNET_assert (CURLE_OK ==
      97             :                  curl_easy_setopt (eh,
      98             :                                    CURLOPT_POSTFIELDSIZE,
      99             :                                    slen));
     100         281 :   return GNUNET_OK;
     101             : }
     102             : 
     103             : 
     104             : /**
     105             :  * Free the data in @a ctx.
     106             :  *
     107             :  * @param[in] ctx a request context (updated)
     108             :  */
     109             : void
     110         284 : TALER_curl_easy_post_finished (struct TALER_CURL_PostContext *ctx)
     111             : {
     112         284 :   curl_slist_free_all (ctx->headers);
     113         284 :   ctx->headers = NULL;
     114         284 :   GNUNET_free (ctx->json_enc);
     115         284 :   ctx->json_enc = NULL;
     116         284 : }

Generated by: LCOV version 1.14