From a16c32a4745634b77439200ee4831fed2811fd8a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Fri, 3 May 2019 17:36:58 +0200 Subject: add compression support for bodies of POST/PUT operations --- ChangeLog | 4 ++ src/lib/Makefile.am | 6 +- src/lib/auditor_api_deposit_confirmation.c | 39 +++++------ src/lib/auditor_api_handle.c | 12 +++- src/lib/auditor_api_handle.h | 2 +- src/lib/exchange_api_deposit.c | 30 +++++---- src/lib/exchange_api_handle.c | 8 +++ src/lib/exchange_api_handle.h | 2 +- src/lib/exchange_api_payback.c | 29 ++++---- src/lib/exchange_api_refresh.c | 60 +++++++++-------- src/lib/exchange_api_refund.c | 30 +++++---- src/lib/exchange_api_reserve.c | 32 ++++----- src/lib/exchange_api_track_transaction.c | 32 ++++----- src/lib/teah_common.c | 105 +++++++++++++++++++++++++++++ src/lib/teah_common.h | 74 ++++++++++++++++++++ 15 files changed, 338 insertions(+), 127 deletions(-) create mode 100644 src/lib/teah_common.c create mode 100644 src/lib/teah_common.h diff --git a/ChangeLog b/ChangeLog index c7bf8b7f9..a5ab93af3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Fri 03 May 2019 05:36:10 PM CEST + Add support for compressing bodies during HTTP uploads to + exchange and auditor. -CG + Thu 02 May 2019 09:15:37 PM CEST Pass hash of denomination public key in operations from clients to exchange instead of the (larger) full public key. diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 8b20860b7..0aa78d7b8 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -27,7 +27,8 @@ libtalerexchange_la_SOURCES = \ exchange_api_reserve.c \ exchange_api_track_transaction.c \ exchange_api_track_transfer.c \ - exchange_api_wire.c + exchange_api_wire.c \ + teah_common.c teah_common.h libtalerexchange_la_LIBADD = \ libtalerauditor.la \ $(top_builddir)/src/json/libtalerjson.la \ @@ -46,7 +47,8 @@ libtalerauditor_la_SOURCES = \ auditor_api_curl_defaults.c auditor_api_curl_defaults.h \ auditor_api_handle.c auditor_api_handle.h \ auditor_api_deposit_confirmation.c \ - auditor_api_exchanges.c + auditor_api_exchanges.c \ + teah_common.c teah_common.h libtalerauditor_la_LIBADD = \ $(top_builddir)/src/json/libtalerjson.la \ $(top_builddir)/src/util/libtalerutil.la \ diff --git a/src/lib/auditor_api_deposit_confirmation.c b/src/lib/auditor_api_deposit_confirmation.c index c34890e63..bb637797a 100644 --- a/src/lib/auditor_api_deposit_confirmation.c +++ b/src/lib/auditor_api_deposit_confirmation.c @@ -49,9 +49,10 @@ struct TALER_AUDITOR_DepositConfirmationHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Handle for the request. @@ -338,26 +339,26 @@ TALER_AUDITOR_deposit_confirmation (struct TALER_AUDITOR_Handle *auditor, dh->url = MAH_path_to_url (auditor, "/deposit-confirmation"); eh = TAL_curl_easy_get (dh->url); - GNUNET_assert (NULL != (dh->json_enc = - json_dumps (deposit_confirmation_obj, - JSON_COMPACT))); - json_decref (deposit_confirmation_obj); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "URL for deposit-confirmation: `%s'\n", - dh->url); - GNUNET_assert (CURLE_OK == curl_easy_setopt (eh, CURLOPT_CUSTOMREQUEST, "PUT")); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - dh->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (dh->json_enc))); + if (GNUNET_OK != + TEAH_curl_easy_post (&dh->ctx, + eh, + deposit_confirmation_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (deposit_confirmation_obj); + GNUNET_free (dh->url); + GNUNET_free (dh); + return NULL; + } + json_decref (deposit_confirmation_obj); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "URL for deposit-confirmation: `%s'\n", + dh->url); ctx = MAH_handle_to_context (auditor); dh->job = GNUNET_CURL_job_add (ctx, eh, @@ -383,7 +384,7 @@ TALER_AUDITOR_deposit_confirmation_cancel (struct TALER_AUDITOR_DepositConfirmat deposit_confirmation->job = NULL; } GNUNET_free (deposit_confirmation->url); - GNUNET_free (deposit_confirmation->json_enc); + TEAH_curl_easy_post_finished (&deposit_confirmation->ctx); GNUNET_free (deposit_confirmation); } diff --git a/src/lib/auditor_api_handle.c b/src/lib/auditor_api_handle.c index 70bf6f715..870eed83d 100644 --- a/src/lib/auditor_api_handle.c +++ b/src/lib/auditor_api_handle.c @@ -15,7 +15,7 @@ */ /** - * @file auditor-lib/auditor_api_handle.c + * @file lib/auditor_api_handle.c * @brief Implementation of the "handle" component of the auditor's HTTP API * @author Sree Harsha Totakura * @author Christian Grothoff @@ -462,6 +462,16 @@ TALER_AUDITOR_connect (struct GNUNET_CURL_Context *ctx, GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to auditor at URL `%s'.\n", url); + /* Disable 100 continue processing */ + GNUNET_break (GNUNET_OK == + GNUNET_CURL_append_header (ctx, + "Expect:")); +#if COMPRESS_BODIES + /* Tell auditor we compress bodies */ + GNUNET_break (GNUNET_OK == + GNUNET_CURL_append_header (ctx, + "Content-encoding: deflate")); +#endif auditor = GNUNET_new (struct TALER_AUDITOR_Handle); auditor->ctx = ctx; auditor->url = GNUNET_strdup (url); diff --git a/src/lib/auditor_api_handle.h b/src/lib/auditor_api_handle.h index c3c73f5c3..c053cbbc0 100644 --- a/src/lib/auditor_api_handle.h +++ b/src/lib/auditor_api_handle.h @@ -22,7 +22,7 @@ #include "platform.h" #include #include "taler_auditor_service.h" - +#include "teah_common.h" /** * Get the context of a auditor. diff --git a/src/lib/exchange_api_deposit.c b/src/lib/exchange_api_deposit.c index 4033cc2ee..b99c7a93f 100644 --- a/src/lib/exchange_api_deposit.c +++ b/src/lib/exchange_api_deposit.c @@ -60,9 +60,10 @@ struct TALER_EXCHANGE_DepositHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Handle for the request. @@ -581,21 +582,22 @@ TALER_EXCHANGE_deposit (struct TALER_EXCHANGE_Handle *exchange, dh->coin_value = dki->value; eh = TEL_curl_easy_get (dh->url); - GNUNET_assert (NULL != (dh->json_enc = - json_dumps (deposit_obj, - JSON_COMPACT))); + if (GNUNET_OK != + TEAH_curl_easy_post (&dh->ctx, + eh, + deposit_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (deposit_obj); + GNUNET_free (dh->url); + GNUNET_free (dh); + return NULL; + } json_decref (deposit_obj); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URL for deposit: `%s'\n", dh->url); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - dh->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (dh->json_enc))); ctx = TEAH_handle_to_context (exchange); dh->job = GNUNET_CURL_job_add (ctx, eh, @@ -621,7 +623,7 @@ TALER_EXCHANGE_deposit_cancel (struct TALER_EXCHANGE_DepositHandle *deposit) deposit->job = NULL; } GNUNET_free (deposit->url); - GNUNET_free (deposit->json_enc); + TEAH_curl_easy_post_finished (&deposit->ctx); GNUNET_free (deposit); } diff --git a/src/lib/exchange_api_handle.c b/src/lib/exchange_api_handle.c index 93fe798c3..0f3cfe0ae 100644 --- a/src/lib/exchange_api_handle.c +++ b/src/lib/exchange_api_handle.c @@ -33,6 +33,7 @@ #include "exchange_api_handle.h" #include "exchange_api_curl_defaults.h" #include "backoff.h" +#include "teah_common.h" /** * Which revision of the Taler protocol is implemented @@ -1783,6 +1784,12 @@ TALER_EXCHANGE_connect GNUNET_break (GNUNET_OK == GNUNET_CURL_append_header (ctx, "Expect:")); +#if COMPRESS_BODIES + /* Tell exchange we compress bodies */ + GNUNET_break (GNUNET_OK == + GNUNET_CURL_append_header (ctx, + "Content-encoding: deflate")); +#endif exchange = GNUNET_new (struct TALER_EXCHANGE_Handle); exchange->ctx = ctx; exchange->url = GNUNET_strdup (url); @@ -1816,6 +1823,7 @@ TALER_EXCHANGE_connect } + /** * Initiate download of /keys from the exchange. * diff --git a/src/lib/exchange_api_handle.h b/src/lib/exchange_api_handle.h index 2c01e319a..6bb3fff28 100644 --- a/src/lib/exchange_api_handle.h +++ b/src/lib/exchange_api_handle.h @@ -24,7 +24,7 @@ #include "taler_auditor_service.h" #include "taler_exchange_service.h" #include "taler_crypto_lib.h" - +#include "teah_common.h" /** * Entry in DLL of auditors used by an exchange. diff --git a/src/lib/exchange_api_payback.c b/src/lib/exchange_api_payback.c index b3624f737..325263d71 100644 --- a/src/lib/exchange_api_payback.c +++ b/src/lib/exchange_api_payback.c @@ -49,9 +49,10 @@ struct TALER_EXCHANGE_PaybackHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Denomination key of the coin. @@ -321,29 +322,23 @@ TALER_EXCHANGE_payback (struct TALER_EXCHANGE_Handle *exchange, ph->cb = payback_cb; ph->cb_cls = payback_cb_cls; ph->url = TEAH_path_to_url (exchange, "/payback"); - - ph->json_enc = json_dumps (payback_obj, - JSON_COMPACT); - json_decref (payback_obj); - if (NULL == ph->json_enc) + eh = TEL_curl_easy_get (ph->url); + if (GNUNET_OK != + TEAH_curl_easy_post (&ph->ctx, + eh, + payback_obj)) { GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (payback_obj); GNUNET_free (ph->url); GNUNET_free (ph); return NULL; } - eh = TEL_curl_easy_get (ph->url); + json_decref (payback_obj); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URL for payback: `%s'\n", ph->url); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - ph->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (ph->json_enc))); ctx = TEAH_handle_to_context (exchange); ph->job = GNUNET_CURL_job_add (ctx, eh, @@ -369,7 +364,7 @@ TALER_EXCHANGE_payback_cancel (struct TALER_EXCHANGE_PaybackHandle *ph) ph->job = NULL; } GNUNET_free (ph->url); - GNUNET_free (ph->json_enc); + TEAH_curl_easy_post_finished (&ph->ctx); GNUNET_free (ph); } diff --git a/src/lib/exchange_api_refresh.c b/src/lib/exchange_api_refresh.c index 4b2de2f45..30025d778 100644 --- a/src/lib/exchange_api_refresh.c +++ b/src/lib/exchange_api_refresh.c @@ -834,9 +834,10 @@ struct TALER_EXCHANGE_RefreshMeltHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Handle for the request. @@ -1206,18 +1207,19 @@ TALER_EXCHANGE_refresh_melt (struct TALER_EXCHANGE_Handle *exchange, rmh->url = TEAH_path_to_url (exchange, "/refresh/melt"); eh = TEL_curl_easy_get (rmh->url); - GNUNET_assert (NULL != (rmh->json_enc = - json_dumps (melt_obj, - JSON_COMPACT))); + if (GNUNET_OK != + TEAH_curl_easy_post (&rmh->ctx, + eh, + melt_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (melt_obj); + GNUNET_free (rmh->url); + GNUNET_free (rmh); + return NULL; + } json_decref (melt_obj); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - rmh->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (rmh->json_enc))); ctx = TEAH_handle_to_context (exchange); rmh->job = GNUNET_CURL_job_add (ctx, eh, @@ -1245,7 +1247,7 @@ TALER_EXCHANGE_refresh_melt_cancel (struct TALER_EXCHANGE_RefreshMeltHandle *rmh free_melt_data (rmh->md); /* does not free 'md' itself */ GNUNET_free (rmh->md); GNUNET_free (rmh->url); - GNUNET_free (rmh->json_enc); + TEAH_curl_easy_post_finished (&rmh->ctx); GNUNET_free (rmh); } @@ -1270,9 +1272,10 @@ struct TALER_EXCHANGE_RefreshRevealHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Handle for the request. @@ -1634,18 +1637,19 @@ TALER_EXCHANGE_refresh_reveal (struct TALER_EXCHANGE_Handle *exchange, "/refresh/reveal"); eh = TEL_curl_easy_get (rrh->url); - GNUNET_assert (NULL != (rrh->json_enc = - json_dumps (reveal_obj, - JSON_COMPACT))); + if (GNUNET_OK != + TEAH_curl_easy_post (&rrh->ctx, + eh, + reveal_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (reveal_obj); + GNUNET_free (rrh->url); + GNUNET_free (rrh); + return NULL; + } json_decref (reveal_obj); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - rrh->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (rrh->json_enc))); ctx = TEAH_handle_to_context (rrh->exchange); rrh->job = GNUNET_CURL_job_add (ctx, eh, @@ -1671,7 +1675,7 @@ TALER_EXCHANGE_refresh_reveal_cancel (struct TALER_EXCHANGE_RefreshRevealHandle rrh->job = NULL; } GNUNET_free (rrh->url); - GNUNET_free (rrh->json_enc); + TEAH_curl_easy_post_finished (&rrh->ctx); free_melt_data (rrh->md); /* does not free 'md' itself */ GNUNET_free (rrh->md); GNUNET_free (rrh); diff --git a/src/lib/exchange_api_refund.c b/src/lib/exchange_api_refund.c index 75ebdc4e3..142213177 100644 --- a/src/lib/exchange_api_refund.c +++ b/src/lib/exchange_api_refund.c @@ -49,9 +49,10 @@ struct TALER_EXCHANGE_RefundHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Handle for the request. @@ -368,21 +369,22 @@ refund_obj = json_pack ("{s:o, s:o," /* amount/fee */ refund_fee); eh = TEL_curl_easy_get (rh->url); - GNUNET_assert (NULL != (rh->json_enc = - json_dumps (refund_obj, - JSON_COMPACT))); + if (GNUNET_OK != + TEAH_curl_easy_post (&rh->ctx, + eh, + refund_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (refund_obj); + GNUNET_free (rh->url); + GNUNET_free (rh); + return NULL; + } json_decref (refund_obj); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URL for refund: `%s'\n", rh->url); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - rh->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (rh->json_enc))); ctx = TEAH_handle_to_context (exchange); rh->job = GNUNET_CURL_job_add (ctx, eh, @@ -408,7 +410,7 @@ TALER_EXCHANGE_refund_cancel (struct TALER_EXCHANGE_RefundHandle *refund) refund->job = NULL; } GNUNET_free (refund->url); - GNUNET_free (refund->json_enc); + TEAH_curl_easy_post_finished (&refund->ctx); GNUNET_free (refund); } diff --git a/src/lib/exchange_api_reserve.c b/src/lib/exchange_api_reserve.c index 66509bc53..ae0bd01d0 100644 --- a/src/lib/exchange_api_reserve.c +++ b/src/lib/exchange_api_reserve.c @@ -677,9 +677,10 @@ struct TALER_EXCHANGE_ReserveWithdrawHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Handle for the request. @@ -1022,25 +1023,26 @@ reserve_withdraw_internal (struct TALER_EXCHANGE_Handle *exchange, if (NULL == withdraw_obj) { GNUNET_break (0); + GNUNET_free (wsh); return NULL; } wsh->ps = *ps; wsh->url = TEAH_path_to_url (exchange, "/reserve/withdraw"); - eh = TEL_curl_easy_get (wsh->url); - GNUNET_assert (NULL != (wsh->json_enc = - json_dumps (withdraw_obj, - JSON_COMPACT))); + if (GNUNET_OK != + TEAH_curl_easy_post (&wsh->ctx, + eh, + withdraw_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (withdraw_obj); + GNUNET_free (wsh->url); + GNUNET_free (wsh); + return NULL; + } json_decref (withdraw_obj); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - wsh->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (wsh->json_enc))); ctx = TEAH_handle_to_context (exchange); wsh->job = GNUNET_CURL_job_add (ctx, eh, @@ -1199,7 +1201,7 @@ TALER_EXCHANGE_reserve_withdraw_cancel (struct TALER_EXCHANGE_ReserveWithdrawHan sign->job = NULL; } GNUNET_free (sign->url); - GNUNET_free (sign->json_enc); + TEAH_curl_easy_post_finished (&sign->ctx); GNUNET_free (sign); } diff --git a/src/lib/exchange_api_track_transaction.c b/src/lib/exchange_api_track_transaction.c index 0942ce84a..67efd77af 100644 --- a/src/lib/exchange_api_track_transaction.c +++ b/src/lib/exchange_api_track_transaction.c @@ -49,9 +49,10 @@ struct TALER_EXCHANGE_TrackTransactionHandle char *url; /** - * JSON encoding of the request to POST. + * Context for #TEH_curl_easy_post(). Keeps the data that must + * persist for Curl to make the upload. */ - char *json_enc; + struct TEAH_PostContext ctx; /** * Handle for the request. @@ -322,18 +323,19 @@ TALER_EXCHANGE_track_transaction (struct TALER_EXCHANGE_Handle *exchange, dwh->depconf.coin_pub = *coin_pub; eh = TEL_curl_easy_get (dwh->url); - GNUNET_assert (NULL != (dwh->json_enc = - json_dumps (deposit_wtid_obj, - JSON_COMPACT))); + if (GNUNET_OK != + TEAH_curl_easy_post (&dwh->ctx, + eh, + deposit_wtid_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (deposit_wtid_obj); + GNUNET_free (dwh->url); + GNUNET_free (dwh); + return NULL; + } json_decref (deposit_wtid_obj); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - dwh->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (dwh->json_enc))); ctx = TEAH_handle_to_context (exchange); dwh->job = GNUNET_CURL_job_add (ctx, eh, @@ -359,9 +361,9 @@ TALER_EXCHANGE_track_transaction_cancel (struct TALER_EXCHANGE_TrackTransactionH dwh->job = NULL; } GNUNET_free (dwh->url); - GNUNET_free (dwh->json_enc); + TEAH_curl_easy_post_finished (&dwh->ctx); GNUNET_free (dwh); } -/* end of exchange_api_deposit_wtid.c */ +/* end of exchange_api_track_transaction.c */ diff --git a/src/lib/teah_common.c b/src/lib/teah_common.c new file mode 100644 index 000000000..8f994ef36 --- /dev/null +++ b/src/lib/teah_common.c @@ -0,0 +1,105 @@ +/* + This file is part of TALER + Copyright (C) 2019 GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + TALER is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public + License along with TALER; see the file COPYING. If not, see + +*/ + +/** + * @file lib/teah_common.c + * @brief Helper routines shared by libtalerexchange and libtalerauditor + * @author Christian Grothoff + */ +#include "platform.h" +#include "teah_common.h" + +#if COMPRESS_BODIES +#include +#endif + + +/** + * Add the @a body as POST data to the easy handle in + * @a ctx. + * + * @param ctx[in,out] a request context (updated) + * @param eh easy handle to use + * @param body JSON body to add to @e ctx + * @return #GNUNET_OK on success #GNUNET_SYSERR on failure + */ +int +TEAH_curl_easy_post (struct TEAH_PostContext *ctx, + CURL *eh, + const json_t *body) +{ + char *str; + size_t slen; + + str = json_dumps (body, + JSON_COMPACT); + if (NULL == str) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + slen = strlen (str); +#if COMPRESS_BODIES + { + Bytef *cbuf; + uLongf cbuf_size; + int ret; + + cbuf_size = compressBound (slen); + cbuf = GNUNET_malloc (cbuf_size); + ret = compress (cbuf, + &cbuf_size, + (const Bytef *) str, + slen); + if (Z_OK != ret) + { + /* compression failed!? */ + GNUNET_break (0); + GNUNET_free (cbuf); + return GNUNET_SYSERR; + } + free (str); + slen = (size_t) cbuf_size; + ctx->json_enc = (char *) cbuf; + } +#else + ctx->json_enc = str; +#endif + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_POSTFIELDS, + ctx->json_enc)); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_POSTFIELDSIZE, + slen)); + return GNUNET_OK; +} + + +/** + * Free the data in @a ctx. + * + * @param ctx[in] a request context (updated) + */ +void +TEAH_curl_easy_post_finished (struct TEAH_PostContext *ctx) +{ + GNUNET_free_non_null (ctx->json_enc); +} diff --git a/src/lib/teah_common.h b/src/lib/teah_common.h new file mode 100644 index 000000000..66937a261 --- /dev/null +++ b/src/lib/teah_common.h @@ -0,0 +1,74 @@ +/* + This file is part of TALER + Copyright (C) 2019 GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + TALER is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public + License along with TALER; see the file COPYING. If not, see + +*/ +/** + * @file lib/teah_common.h + * @brief Helper routines shared by libtalerexchange and libtalerauditor + * @author Christian Grothoff + */ +#ifndef TEAH_COMMON_H +#define TEAH_COMMON_H + +#include +#include "taler_json_lib.h" + +/** + * Should we compress PUT/POST bodies with 'deflate' encoding? + */ +#define COMPRESS_BODIES 0 + +/** + * State used for #TEAL_curl_easy_post() and + * #TEAL_curl_easy_post_finished(). + */ +struct TEAH_PostContext +{ + /** + * JSON encoding of the request to POST. + */ + char *json_enc; + +}; + + +/** + * Add the @a body as POST data to the easy handle in + * @a ctx. + * + * @param ctx[in,out] a request context (updated) + * @param eh easy handle to use + * @param body JSON body to add to @e ctx + * @return #GNUNET_OK on success #GNUNET_SYSERR on failure + */ +int +TEAH_curl_easy_post (struct TEAH_PostContext *ctx, + CURL *eh, + const json_t *body); + + +/** + * Free the data in @a ctx. + * + * @param ctx[in] a request context (updated) + */ +void +TEAH_curl_easy_post_finished (struct TEAH_PostContext *ctx); + + + +#endif -- cgit v1.2.3