LCOV - code coverage report
Current view: top level - lib - exchange_api_auditor_add_denomination.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 81 0.0 %
Date: 2021-08-30 06:43:37 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2015-2021 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
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/exchange_api_auditor_add_denomination.c
      19             :  * @brief functions for the auditor to add its signature for denomination at the exchange
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include "taler_json_lib.h"
      24             : #include <gnunet/gnunet_curl_lib.h>
      25             : #include "taler_exchange_service.h"
      26             : #include "taler_signatures.h"
      27             : #include "taler_curl_lib.h"
      28             : #include "taler_json_lib.h"
      29             : 
      30             : 
      31             : struct TALER_EXCHANGE_AuditorAddDenominationHandle
      32             : {
      33             : 
      34             :   /**
      35             :    * The url for this request.
      36             :    */
      37             :   char *url;
      38             : 
      39             :   /**
      40             :    * Minor context that holds body and headers.
      41             :    */
      42             :   struct TALER_CURL_PostContext post_ctx;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_EXCHANGE_AuditorAddDenominationCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Function called when we're done processing the
      68             :  * HTTP POST /auditor/$AUDITOR_PUB/$H_DENOM_PUB request.
      69             :  *
      70             :  * @param cls the `struct TALER_EXCHANGE_AuditorAddDenominationHandle *`
      71             :  * @param response_code HTTP response code, 0 on error
      72             :  * @param response response body, NULL if not in JSON
      73             :  */
      74             : static void
      75           0 : handle_auditor_add_denomination_finished (void *cls,
      76             :                                           long response_code,
      77             :                                           const void *response)
      78             : {
      79           0 :   struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah = cls;
      80           0 :   const json_t *json = response;
      81           0 :   struct TALER_EXCHANGE_HttpResponse hr = {
      82           0 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86           0 :   ah->job = NULL;
      87           0 :   switch (response_code)
      88             :   {
      89           0 :   case MHD_HTTP_NO_CONTENT:
      90           0 :     break;
      91           0 :   case MHD_HTTP_FORBIDDEN:
      92           0 :     hr.ec = TALER_JSON_get_error_code (json);
      93           0 :     hr.hint = TALER_JSON_get_error_hint (json);
      94           0 :     break;
      95           0 :   case MHD_HTTP_NOT_FOUND:
      96           0 :     hr.ec = TALER_JSON_get_error_code (json);
      97           0 :     hr.hint = TALER_JSON_get_error_hint (json);
      98           0 :     break;
      99           0 :   case MHD_HTTP_GONE:
     100           0 :     hr.ec = TALER_JSON_get_error_code (json);
     101           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     102           0 :     break;
     103           0 :   case MHD_HTTP_PRECONDITION_FAILED:
     104           0 :     hr.ec = TALER_JSON_get_error_code (json);
     105           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     106           0 :     break;
     107           0 :   default:
     108             :     /* unexpected response code */
     109           0 :     if (NULL != json)
     110             :     {
     111           0 :       hr.ec = TALER_JSON_get_error_code (json);
     112           0 :       hr.hint = TALER_JSON_get_error_hint (json);
     113           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     114             :                   "Unexpected response code %u/%d for exchange auditor-add-denomination at URL `%s'\n",
     115             :                   (unsigned int) response_code,
     116             :                   (int) hr.ec,
     117             :                   ah->url);
     118             :     }
     119             :     else
     120             :     {
     121           0 :       hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     122           0 :       hr.hint = NULL;
     123           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     124             :                   "Unexpected HTTP response code %u (no JSON returned) at URL `%s'\n",
     125             :                   (unsigned int) response_code,
     126             :                   ah->url);
     127             :     }
     128           0 :     break;
     129             :   }
     130           0 :   if (NULL != ah->cb)
     131             :   {
     132           0 :     ah->cb (ah->cb_cls,
     133             :             &hr);
     134           0 :     ah->cb = NULL;
     135             :   }
     136           0 :   TALER_EXCHANGE_add_auditor_denomination_cancel (ah);
     137           0 : }
     138             : 
     139             : 
     140             : struct TALER_EXCHANGE_AuditorAddDenominationHandle *
     141           0 : TALER_EXCHANGE_add_auditor_denomination (
     142             :   struct GNUNET_CURL_Context *ctx,
     143             :   const char *url,
     144             :   const struct GNUNET_HashCode *h_denom_pub,
     145             :   const struct TALER_AuditorPublicKeyP *auditor_pub,
     146             :   const struct TALER_AuditorSignatureP *auditor_sig,
     147             :   TALER_EXCHANGE_AuditorAddDenominationCallback cb,
     148             :   void *cb_cls)
     149             : {
     150             :   struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah;
     151             :   CURL *eh;
     152             :   json_t *body;
     153             : 
     154           0 :   ah = GNUNET_new (struct TALER_EXCHANGE_AuditorAddDenominationHandle);
     155           0 :   ah->cb = cb;
     156           0 :   ah->cb_cls = cb_cls;
     157           0 :   ah->ctx = ctx;
     158             :   {
     159             :     char apub_str[sizeof (*auditor_pub) * 2];
     160             :     char denom_str[sizeof (*h_denom_pub) * 2];
     161             :     char arg_str[sizeof (apub_str) + sizeof (denom_str) + 32];
     162             :     char *end;
     163             : 
     164           0 :     end = GNUNET_STRINGS_data_to_string (auditor_pub,
     165             :                                          sizeof (*auditor_pub),
     166             :                                          apub_str,
     167             :                                          sizeof (apub_str));
     168           0 :     *end = '\0';
     169           0 :     end = GNUNET_STRINGS_data_to_string (h_denom_pub,
     170             :                                          sizeof (*h_denom_pub),
     171             :                                          denom_str,
     172             :                                          sizeof (denom_str));
     173           0 :     *end = '\0';
     174           0 :     GNUNET_snprintf (arg_str,
     175             :                      sizeof (arg_str),
     176             :                      "auditors/%s/%s",
     177             :                      apub_str,
     178             :                      denom_str);
     179           0 :     ah->url = TALER_url_join (url,
     180             :                               arg_str,
     181             :                               NULL);
     182             :   }
     183           0 :   if (NULL == ah->url)
     184             :   {
     185           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     186             :                 "Could not construct request URL.\n");
     187           0 :     GNUNET_free (ah);
     188           0 :     return NULL;
     189             :   }
     190           0 :   body = GNUNET_JSON_PACK (
     191             :     GNUNET_JSON_pack_data_auto ("auditor_sig",
     192             :                                 auditor_sig));
     193           0 :   eh = curl_easy_init ();
     194           0 :   GNUNET_assert (NULL != eh);
     195           0 :   if (GNUNET_OK !=
     196           0 :       TALER_curl_easy_post (&ah->post_ctx,
     197             :                             eh,
     198             :                             body))
     199             :   {
     200           0 :     GNUNET_break (0);
     201           0 :     json_decref (body);
     202           0 :     GNUNET_free (ah->url);
     203           0 :     GNUNET_free (eh);
     204           0 :     return NULL;
     205             :   }
     206           0 :   json_decref (body);
     207           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     208             :               "Requesting URL '%s'\n",
     209             :               ah->url);
     210           0 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     211             :                                                CURLOPT_URL,
     212             :                                                ah->url));
     213           0 :   ah->job = GNUNET_CURL_job_add2 (ctx,
     214             :                                   eh,
     215           0 :                                   ah->post_ctx.headers,
     216             :                                   &handle_auditor_add_denomination_finished,
     217             :                                   ah);
     218           0 :   if (NULL == ah->job)
     219             :   {
     220           0 :     TALER_EXCHANGE_add_auditor_denomination_cancel (ah);
     221           0 :     return NULL;
     222             :   }
     223           0 :   return ah;
     224             : }
     225             : 
     226             : 
     227             : void
     228           0 : TALER_EXCHANGE_add_auditor_denomination_cancel (
     229             :   struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah)
     230             : {
     231           0 :   if (NULL != ah->job)
     232             :   {
     233           0 :     GNUNET_CURL_job_cancel (ah->job);
     234           0 :     ah->job = NULL;
     235             :   }
     236           0 :   TALER_curl_easy_post_finished (&ah->post_ctx);
     237           0 :   GNUNET_free (ah->url);
     238           0 :   GNUNET_free (ah);
     239           0 : }

Generated by: LCOV version 1.14