LCOV - code coverage report
Current view: top level - lib - merchant_api_delete_reserve.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 49 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/merchant_api_delete_reserve.c
      19             :  * @brief Implementation of the DELETE /reserves/$RESERVE_PUB request of the merchant's HTTP API
      20             :  * @author Jonathan Buchanan
      21             :  */
      22             :  #include "platform.h"
      23             :  #include <curl/curl.h>
      24             :  #include <jansson.h>
      25             :  #include <microhttpd.h> /* just for HTTP status codes */
      26             :  #include <gnunet/gnunet_util_lib.h>
      27             :  #include <gnunet/gnunet_curl_lib.h>
      28             :  #include "taler_merchant_service.h"
      29             :  #include <taler/taler_json_lib.h>
      30             :  #include <taler/taler_signatures.h>
      31             : 
      32             : 
      33             : /**
      34             :  * Handle for a DELETE /reserves/$RESERVE_PUB operation.
      35             :  */
      36             : struct TALER_MERCHANT_ReserveDeleteHandle
      37             : {
      38             : 
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      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_MERCHANT_ReserveDeleteCallback 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             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP DELETE /reserves/$RESERVE_PUB request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_InstanceDeleteHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param json response body, NULL if not in JSON
      74             :  */
      75             : static void
      76           0 : handle_delete_reserve_finished (void *cls,
      77             :                                 long response_code,
      78             :                                 const void *response)
      79             : {
      80           0 :   struct TALER_MERCHANT_ReserveDeleteHandle *rdh = cls;
      81           0 :   const json_t *json = response;
      82           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      83           0 :     .http_status = (unsigned int) response_code,
      84             :     .reply = json
      85             :   };
      86             : 
      87           0 :   rdh->job = NULL;
      88           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      89             :               "Got /reserves/$ID response with status code %u\n",
      90             :               (unsigned int) response_code);
      91           0 :   switch (response_code)
      92             :   {
      93           0 :   case MHD_HTTP_NO_CONTENT:
      94           0 :     break;
      95           0 :   default:
      96             :     /* unexpected response code */
      97           0 :     hr.ec = TALER_JSON_get_error_code (json);
      98           0 :     hr.hint = TALER_JSON_get_error_hint (json);
      99           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     100             :                 "Unexpected response code %u/%d\n",
     101             :                 (unsigned int) response_code,
     102             :                 (int) hr.ec);
     103           0 :     break;
     104             :   }
     105           0 :   rdh->cb (rdh->cb_cls,
     106             :            &hr);
     107           0 :   TALER_MERCHANT_reserve_delete_cancel (rdh);
     108           0 : }
     109             : 
     110             : 
     111             : /**
     112             :  * Delete the private key of a reserve.
     113             :  *
     114             :  * @param ctx the context
     115             :  * @param backend_url HTTP base URL for the backend
     116             :  * @param instance_id which instance should be deleted
     117             :  * @param purge purge instead of just deleting
     118             :  * @param instances_cb function to call with the
     119             :  *        backend's return
     120             :  * @param instances_cb_cls closure for @a config_cb
     121             :  * @return the instances handle; NULL upon error
     122             :  */
     123             : static struct TALER_MERCHANT_ReserveDeleteHandle *
     124           0 : reserve_delete (struct GNUNET_CURL_Context *ctx,
     125             :                 const char *backend_url,
     126             :                 const struct TALER_ReservePublicKeyP *reserve_pub,
     127             :                 bool purge,
     128             :                 TALER_MERCHANT_ReserveDeleteCallback cb,
     129             :                 void *cb_cls)
     130             : {
     131             :   struct TALER_MERCHANT_ReserveDeleteHandle *rdh;
     132             : 
     133           0 :   rdh = GNUNET_new (struct TALER_MERCHANT_ReserveDeleteHandle);
     134           0 :   rdh->ctx = ctx;
     135           0 :   rdh->cb = cb;
     136           0 :   rdh->cb_cls = cb_cls;
     137             :   {
     138             :     char res_str[sizeof (*reserve_pub) * 2];
     139             :     char arg_str[sizeof (res_str) + 32];
     140             :     char *end;
     141             : 
     142           0 :     end = GNUNET_STRINGS_data_to_string (reserve_pub,
     143             :                                          sizeof (*reserve_pub),
     144             :                                          res_str,
     145             :                                          sizeof (res_str));
     146           0 :     *end = '\0';
     147           0 :     GNUNET_snprintf (arg_str,
     148             :                      sizeof (arg_str),
     149             :                      "private/reserves/%s",
     150             :                      res_str);
     151           0 :     if (purge)
     152           0 :       rdh->url = TALER_url_join (backend_url,
     153             :                                  arg_str,
     154             :                                  "purge",
     155             :                                  "yes",
     156             :                                  NULL);
     157             :     else
     158           0 :       rdh->url = TALER_url_join (backend_url,
     159             :                                  arg_str,
     160             :                                  NULL);
     161             :   }
     162           0 :   if (NULL == rdh->url)
     163             :   {
     164           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     165             :                 "Could not construct request URL.\n");
     166           0 :     GNUNET_free (rdh);
     167           0 :     return NULL;
     168             :   }
     169           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     170             :               "Requesting URL '%s'\n",
     171             :               rdh->url);
     172             :   {
     173             :     CURL *eh;
     174             : 
     175           0 :     eh = curl_easy_init ();
     176           0 :     GNUNET_assert (CURLE_OK ==
     177             :                    curl_easy_setopt (eh,
     178             :                                      CURLOPT_URL,
     179             :                                      rdh->url));
     180           0 :     GNUNET_assert (CURLE_OK ==
     181             :                    curl_easy_setopt (eh,
     182             :                                      CURLOPT_CUSTOMREQUEST,
     183             :                                      MHD_HTTP_METHOD_DELETE));
     184           0 :     rdh->job = GNUNET_CURL_job_add (ctx,
     185             :                                     eh,
     186             :                                     &handle_delete_reserve_finished,
     187             :                                     rdh);
     188             :   }
     189           0 :   return rdh;
     190             : }
     191             : 
     192             : 
     193             : /**
     194             :  * Issue a DELETE /reserve/$RESERVE_ID request to the backend.  Only
     195             :  * deletes the private key of the reserve, preserves tipping data.
     196             :  *
     197             :  * @param ctx execution context
     198             :  * @param backend_url base URL of the merchant backend
     199             :  * @param reserve_pub which reserve should be queried
     200             :  * @param cb function to call with the result
     201             :  * @param cb_cls closure for @a cb
     202             :  * @return handle for this operation, NULL upon errors
     203             :  */
     204             : struct TALER_MERCHANT_ReserveDeleteHandle *
     205           0 : TALER_MERCHANT_reserve_delete (
     206             :   struct GNUNET_CURL_Context *ctx,
     207             :   const char *backend_url,
     208             :   const struct TALER_ReservePublicKeyP *reserve_pub,
     209             :   TALER_MERCHANT_ReserveDeleteCallback cb,
     210             :   void *cb_cls)
     211             : {
     212           0 :   return reserve_delete (ctx,
     213             :                          backend_url,
     214             :                          reserve_pub,
     215             :                          false,
     216             :                          cb,
     217             :                          cb_cls);
     218             : }
     219             : 
     220             : 
     221             : /**
     222             :  * Issue a DELETE /reserve/$RESERVE_ID request to the backend.
     223             :  * Purges the reserve, deleting all associated data. DANGEROUS.
     224             :  *
     225             :  * @param ctx execution context
     226             :  * @param backend_url base URL of the merchant backend
     227             :  * @param reserve_pub which reserve should be queried
     228             :  * @param cb function to call with the result
     229             :  * @param cb_cls closure for @a cb
     230             :  * @return handle for this operation, NULL upon errors
     231             :  */
     232             : struct TALER_MERCHANT_ReserveDeleteHandle *
     233           0 : TALER_MERCHANT_reserve_purge (struct GNUNET_CURL_Context *ctx,
     234             :                               const char *backend_url,
     235             :                               const struct TALER_ReservePublicKeyP *reserve_pub,
     236             :                               TALER_MERCHANT_ReserveDeleteCallback cb,
     237             :                               void *cb_cls)
     238             : {
     239           0 :   return reserve_delete (ctx,
     240             :                          backend_url,
     241             :                          reserve_pub,
     242             :                          true,
     243             :                          cb,
     244             :                          cb_cls);
     245             : }
     246             : 
     247             : 
     248             : /**
     249             :  * Cancel a DELETE (or purge) /reserve/$RESERVE_ID request.
     250             :  *
     251             :  * @param rdh handle to the request to be canceled
     252             :  */
     253             : void
     254           0 : TALER_MERCHANT_reserve_delete_cancel (
     255             :   struct TALER_MERCHANT_ReserveDeleteHandle *rdh)
     256             : {
     257           0 :   if (NULL != rdh->job)
     258           0 :     GNUNET_CURL_job_cancel (rdh->job);
     259           0 :   GNUNET_free (rdh->url);
     260           0 :   GNUNET_free (rdh);
     261           0 : }

Generated by: LCOV version 1.14