LCOV - code coverage report
Current view: top level - lib - merchant_api_delete_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 48 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) 2014-2018, 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_instance.c
      19             :  * @brief Implementation of the DELETE /instance/$ID request of the merchant's HTTP API
      20             :  * @author Christian Grothoff
      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 /instances/$ID operation.
      35             :  */
      36             : struct TALER_MERCHANT_InstanceDeleteHandle
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Handle for the request.
      45             :    */
      46             :   struct GNUNET_CURL_Job *job;
      47             : 
      48             :   /**
      49             :    * Function to call with the result.
      50             :    */
      51             :   TALER_MERCHANT_InstanceDeleteCallback cb;
      52             : 
      53             :   /**
      54             :    * Closure for @a cb.
      55             :    */
      56             :   void *cb_cls;
      57             : 
      58             :   /**
      59             :    * Reference to the execution context.
      60             :    */
      61             :   struct GNUNET_CURL_Context *ctx;
      62             : 
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Function called when we're done processing the
      68             :  * HTTP GET /instances/$ID request.
      69             :  *
      70             :  * @param cls the `struct TALER_MERCHANT_InstanceDeleteHandle`
      71             :  * @param response_code HTTP response code, 0 on error
      72             :  * @param json response body, NULL if not in JSON
      73             :  */
      74             : static void
      75           0 : handle_delete_instance_finished (void *cls,
      76             :                                  long response_code,
      77             :                                  const void *response)
      78             : {
      79           0 :   struct TALER_MERCHANT_InstanceDeleteHandle *idh = cls;
      80           0 :   const json_t *json = response;
      81           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      82           0 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86           0 :   idh->job = NULL;
      87           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      88             :               "Got /instances/$ID response with status code %u\n",
      89             :               (unsigned int) response_code);
      90           0 :   switch (response_code)
      91             :   {
      92           0 :   case MHD_HTTP_NO_CONTENT:
      93           0 :     break;
      94           0 :   default:
      95             :     /* unexpected response code */
      96           0 :     hr.ec = TALER_JSON_get_error_code (json);
      97           0 :     hr.hint = TALER_JSON_get_error_hint (json);
      98           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      99             :                 "Unexpected response code %u/%d\n",
     100             :                 (unsigned int) response_code,
     101             :                 (int) hr.ec);
     102           0 :     break;
     103             :   }
     104           0 :   idh->cb (idh->cb_cls,
     105             :            &hr);
     106           0 :   TALER_MERCHANT_instance_delete_cancel (idh);
     107           0 : }
     108             : 
     109             : 
     110             : /**
     111             :  * Delete the private key of an instance of a backend, thereby disabling the
     112             :  * instance for future requests.  Will preserve the other instance data
     113             :  * (i.e. for taxation).
     114             :  *
     115             :  * @param ctx the context
     116             :  * @param backend_url HTTP base URL for the backend
     117             :  * @param instance_id which instance should be deleted
     118             :  * @param purge purge instead of just deleting
     119             :  * @param instances_cb function to call with the
     120             :  *        backend's return
     121             :  * @param instances_cb_cls closure for @a config_cb
     122             :  * @return the instances handle; NULL upon error
     123             :  */
     124             : static struct TALER_MERCHANT_InstanceDeleteHandle *
     125           0 : instance_delete (struct GNUNET_CURL_Context *ctx,
     126             :                  const char *backend_url,
     127             :                  const char *instance_id,
     128             :                  bool purge,
     129             :                  TALER_MERCHANT_InstanceDeleteCallback cb,
     130             :                  void *cb_cls)
     131             : {
     132             :   struct TALER_MERCHANT_InstanceDeleteHandle *idh;
     133             : 
     134           0 :   idh = GNUNET_new (struct TALER_MERCHANT_InstanceDeleteHandle);
     135           0 :   idh->ctx = ctx;
     136           0 :   idh->cb = cb;
     137           0 :   idh->cb_cls = cb_cls;
     138             :   {
     139             :     char *path;
     140             : 
     141           0 :     GNUNET_asprintf (&path,
     142             :                      "private/instances/%s",
     143             :                      instance_id);
     144           0 :     if (purge)
     145           0 :       idh->url = TALER_url_join (backend_url,
     146             :                                  path,
     147             :                                  "purge",
     148             :                                  "yes",
     149             :                                  NULL);
     150             :     else
     151           0 :       idh->url = TALER_url_join (backend_url,
     152             :                                  path,
     153             :                                  NULL);
     154           0 :     GNUNET_free (path);
     155             :   }
     156           0 :   if (NULL == idh->url)
     157             :   {
     158           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     159             :                 "Could not construct request URL.\n");
     160           0 :     GNUNET_free (idh);
     161           0 :     return NULL;
     162             :   }
     163           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     164             :               "Requesting URL '%s'\n",
     165             :               idh->url);
     166             :   {
     167             :     CURL *eh;
     168             : 
     169           0 :     eh = curl_easy_init ();
     170           0 :     GNUNET_assert (CURLE_OK ==
     171             :                    curl_easy_setopt (eh,
     172             :                                      CURLOPT_URL,
     173             :                                      idh->url));
     174           0 :     GNUNET_assert (CURLE_OK ==
     175             :                    curl_easy_setopt (eh,
     176             :                                      CURLOPT_CUSTOMREQUEST,
     177             :                                      MHD_HTTP_METHOD_DELETE));
     178           0 :     idh->job = GNUNET_CURL_job_add (ctx,
     179             :                                     eh,
     180             :                                     &handle_delete_instance_finished,
     181             :                                     idh);
     182             :   }
     183           0 :   return idh;
     184             : }
     185             : 
     186             : 
     187             : /**
     188             :  * Delete the private key of an instance of a backend, thereby disabling the
     189             :  * instance for future requests.  Will preserve the other instance data
     190             :  * (i.e. for taxation).
     191             :  *
     192             :  * @param ctx the context
     193             :  * @param backend_url HTTP base URL for the backend
     194             :  * @param instance_id which instance should be deleted
     195             :  * @param instances_cb function to call with the
     196             :  *        backend's return
     197             :  * @param instances_cb_cls closure for @a config_cb
     198             :  * @return the instances handle; NULL upon error
     199             :  */
     200             : struct TALER_MERCHANT_InstanceDeleteHandle *
     201           0 : TALER_MERCHANT_instance_delete (struct GNUNET_CURL_Context *ctx,
     202             :                                 const char *backend_url,
     203             :                                 const char *instance_id,
     204             :                                 TALER_MERCHANT_InstanceDeleteCallback cb,
     205             :                                 void *cb_cls)
     206             : {
     207           0 :   return instance_delete (ctx,
     208             :                           backend_url,
     209             :                           instance_id,
     210             :                           false,
     211             :                           cb,
     212             :                           cb_cls);
     213             : }
     214             : 
     215             : 
     216             : /**
     217             :  * Purge all data associated with an instance. Use with
     218             :  * extreme caution.
     219             :  *
     220             :  * @param ctx the context
     221             :  * @param backend_url HTTP base URL for the backend
     222             :  * @param instance_id which instance should be deleted
     223             :  * @param instances_cb function to call with the
     224             :  *        backend's return
     225             :  * @param instances_cb_cls closure for @a config_cb
     226             :  * @return the instances handle; NULL upon error
     227             :  */
     228             : struct TALER_MERCHANT_InstanceDeleteHandle *
     229           0 : TALER_MERCHANT_instance_purge (struct GNUNET_CURL_Context *ctx,
     230             :                                const char *backend_url,
     231             :                                const char *instance_id,
     232             :                                TALER_MERCHANT_InstanceDeleteCallback cb,
     233             :                                void *cb_cls)
     234             : {
     235           0 :   return instance_delete (ctx,
     236             :                           backend_url,
     237             :                           instance_id,
     238             :                           true,
     239             :                           cb,
     240             :                           cb_cls);
     241             : }
     242             : 
     243             : 
     244             : /**
     245             :  * Cancel DELETE /instance/$ID request.  Must not be called by clients after
     246             :  * the callback was invoked.
     247             :  *
     248             :  * @param idh request to cancel.
     249             :  */
     250             : void
     251           0 : TALER_MERCHANT_instance_delete_cancel (
     252             :   struct TALER_MERCHANT_InstanceDeleteHandle *idh)
     253             : {
     254           0 :   if (NULL != idh->job)
     255           0 :     GNUNET_CURL_job_cancel (idh->job);
     256           0 :   GNUNET_free (idh->url);
     257           0 :   GNUNET_free (idh);
     258           0 : }

Generated by: LCOV version 1.14