Line data Source code
1 : /* 2 : This file is part of TALER 3 : Copyright (C) 2019-2021 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 : 28 : #if TALER_CURL_COMPRESS_BODIES 29 : #include <zlib.h> 30 : #endif 31 : 32 : 33 : enum GNUNET_GenericReturnValue 34 12 : TALER_curl_easy_post (struct TALER_CURL_PostContext *ctx, 35 : CURL *eh, 36 : const json_t *body) 37 : { 38 : char *str; 39 : size_t slen; 40 : 41 12 : str = json_dumps (body, 42 : JSON_COMPACT); 43 12 : if (NULL == str) 44 : { 45 0 : GNUNET_break (0); 46 0 : return GNUNET_SYSERR; 47 : } 48 12 : slen = strlen (str); 49 12 : if (TALER_CURL_COMPRESS_BODIES && 50 12 : (! ctx->disable_compression) ) 51 : { 52 : Bytef *cbuf; 53 : uLongf cbuf_size; 54 : int ret; 55 : 56 12 : cbuf_size = compressBound (slen); 57 12 : cbuf = GNUNET_malloc (cbuf_size); 58 12 : ret = compress (cbuf, 59 : &cbuf_size, 60 : (const Bytef *) str, 61 : slen); 62 12 : if (Z_OK != ret) 63 : { 64 : /* compression failed!? */ 65 0 : GNUNET_break (0); 66 0 : GNUNET_free (cbuf); 67 0 : return GNUNET_SYSERR; 68 : } 69 12 : free (str); 70 12 : slen = (size_t) cbuf_size; 71 12 : ctx->json_enc = (char *) cbuf; 72 12 : GNUNET_assert ( 73 : NULL != 74 : (ctx->headers = curl_slist_append ( 75 : ctx->headers, 76 : "Content-Encoding: deflate"))); 77 : } 78 : else 79 : { 80 0 : ctx->json_enc = str; 81 : } 82 12 : GNUNET_assert ( 83 : NULL != 84 : (ctx->headers = curl_slist_append ( 85 : ctx->headers, 86 : "Content-Type: application/json"))); 87 : 88 12 : GNUNET_assert (CURLE_OK == 89 : curl_easy_setopt (eh, 90 : CURLOPT_POSTFIELDS, 91 : ctx->json_enc)); 92 12 : GNUNET_assert (CURLE_OK == 93 : curl_easy_setopt (eh, 94 : CURLOPT_POSTFIELDSIZE, 95 : slen)); 96 12 : return GNUNET_OK; 97 : } 98 : 99 : 100 : void 101 12 : TALER_curl_easy_post_finished (struct TALER_CURL_PostContext *ctx) 102 : { 103 12 : curl_slist_free_all (ctx->headers); 104 12 : ctx->headers = NULL; 105 12 : GNUNET_free (ctx->json_enc); 106 12 : ctx->json_enc = NULL; 107 12 : }