frosix

Multiparty signature service (experimental)
Log | Files | Refs | README | LICENSE

frosix_api_dkg-key_delete.c (6411B)


      1 /*
      2   This file is part of ANASTASIS
      3   Copyright (C) 2014-2019 Anastasis SARL
      4 
      5   ANASTASIS is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 2.1,
      8   or (at your option) any later version.
      9 
     10   ANASTASIS 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 ANASTASIS; see the file COPYING.LGPL.  If not,
     17   see <http://www.gnu.org/licenses/>
     18 */
     19 
     20 /**
     21  * @file restclient/frosix_api_dkg-key_delete.c
     22  * @brief Implementation of the /dkg-key Delete
     23  * @author Christian Grothoff
     24  * @author Dennis Neufeld
     25  * @author Dominik Meister
     26  * @author Joel Urech
     27  */
     28 #include "platform.h"
     29 #include <curl/curl.h>
     30 #include <microhttpd.h> /* just for HTTP status codes */
     31 #include "frosix_service.h"
     32 #include "frost_high.h"
     33 #include "frosix_api_curl_defaults.h"
     34 #include <gnunet/gnunet_json_lib.h>
     35 #include <taler/taler_json_lib.h>
     36 
     37 
     38 /**
     39  * @brief A Contract Operation Handle
     40  */
     41 struct FROSIX_KeyDeleteOperation
     42 {
     43   /**
     44    * The url for this request, including parameters.
     45    */
     46   char *url;
     47 
     48   /**
     49    * Handle for the request.
     50    */
     51   struct GNUNET_CURL_Job *job;
     52 
     53   /**
     54    * The CURL context to connect to the backend
     55   */
     56   struct GNUNET_CURL_Context *ctx;
     57 
     58   /**
     59    * Function to call with the result.
     60    */
     61   FROSIX_KeyDeleteCallback cb;
     62 
     63   /**
     64    * Closure for @a cb.
     65    */
     66   void *cb_cls;
     67 };
     68 
     69 
     70 void
     71 FROSIX_key_delete_cancel (
     72   struct FROSIX_KeyDeleteOperation *kdo)
     73 {
     74   if (NULL != kdo->job)
     75   {
     76     GNUNET_CURL_job_cancel (kdo->job);
     77     kdo->job = NULL;
     78   }
     79   GNUNET_free (kdo->url);
     80   GNUNET_free (kdo);
     81 }
     82 
     83 
     84 /**
     85  * Callback to process DELETE /dkg-key response
     86  *
     87  * @param cls the `struct FROSIX_DkgKeyStoreOperation`
     88  * @param response_code HTTP response code, 0 on error
     89  * @param data response body
     90  * @param data_size number of byte in @a data
     91 */
     92 static void
     93 handle_key_delete_finished (void *cls,
     94                             long response_code,
     95                             const void *response)
     96 {
     97   struct FROSIX_KeyDeleteOperation *kdo = cls;
     98   struct FROSIX_KeyDeleteDetails kdd;
     99   const json_t *json_response = response;
    100 
    101   kdo->job = NULL;
    102   kdd.http_status = response_code;
    103   kdd.ec = TALER_EC_NONE;
    104 
    105   switch (response_code)
    106   {
    107   case 0:
    108     /* Hard error */
    109     GNUNET_break (0);
    110     kdd.kds = FROSIX_KDS_HTTP_ERROR;
    111     kdd.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    112     break;
    113   case MHD_HTTP_NO_CONTENT:
    114   case MHD_HTTP_OK:
    115     {
    116       kdd.kds = FROSIX_KDS_SUCCESS;
    117       break;
    118     }
    119   case MHD_HTTP_BAD_REQUEST:
    120     /* We have a conflict with the API */
    121     GNUNET_break (0);
    122     kdd.kds = FROSIX_KDS_CLIENT_ERROR;
    123     kdd.ec = TALER_JSON_get_error_code2 (json_response,
    124                                          json_object_size (json_response));
    125     break;
    126   case MHD_HTTP_INTERNAL_SERVER_ERROR:
    127     /* The provider has a problem! */
    128     GNUNET_break (0);
    129     kdd.kds = FROSIX_KDS_SERVER_ERROR;
    130     kdd.ec = TALER_JSON_get_error_code2 (json_response,
    131                                          json_object_size (json_response));
    132     break;
    133   default:
    134     /* Unexpected response code */
    135     GNUNET_break (0);
    136     kdd.ec = TALER_JSON_get_error_code2 (json_response,
    137                                          json_object_size (json_response));
    138     break;
    139   }
    140 
    141   /* return to callback function with the data from the response */
    142   kdo->cb (kdo->cb_cls,
    143            &kdd);
    144   kdo->cb = NULL;
    145 
    146   FROSIX_key_delete_cancel (kdo);
    147 }
    148 
    149 
    150 /**
    151  * Handle HTTP header received by curl.
    152  *
    153  * @param buffer one line of HTTP header data
    154  * @param size size of an item
    155  * @param userdata our `struct FROSIX_DkgKeyStoreOperation`
    156  * @return `size * nitems`
    157 */
    158 static size_t
    159 handle_header (char *buffer,
    160                size_t size,
    161                size_t nitems,
    162                void *userdata)
    163 {
    164   // struct FROSIX_DkgKeyStoreOperation *dko = userdata;
    165   size_t total = size * nitems;
    166   char *ndup;
    167   const char *hdr_type;
    168   char *hdr_val;
    169   char *sp;
    170 
    171   ndup = GNUNET_strndup (buffer,
    172                          total);
    173 
    174   hdr_type = strtok_r (ndup,
    175                        ":",
    176                        &sp);
    177   if (NULL == hdr_type)
    178   {
    179     GNUNET_free (ndup);
    180     return total;
    181   }
    182   hdr_val = strtok_r (NULL,
    183                       "\n\r",
    184                       &sp);
    185   if (NULL == hdr_val)
    186   {
    187     GNUNET_free (ndup);
    188     return total;
    189   }
    190   if (' ' == *hdr_val)
    191     hdr_val++;
    192   /* ... FIXME */
    193   return total;
    194 }
    195 
    196 
    197 struct FROSIX_KeyDeleteOperation *
    198 FROSIX_key_delete (
    199   struct GNUNET_CURL_Context *ctx,
    200   const char *backend_url,
    201   const struct FROSIX_DkgRequestIdP *uuid,
    202   FROSIX_KeyDeleteCallback cb,
    203   void *cb_cls)
    204 {
    205   struct FROSIX_KeyDeleteOperation *kdo;
    206   CURL *eh;
    207 
    208   /* check if we got a callback function, abort if not */
    209   GNUNET_assert (NULL != cb);
    210 
    211   kdo = GNUNET_new (struct FROSIX_KeyDeleteOperation);
    212   {
    213     /* prepare URL */
    214     char *uuid_str;
    215     char *path;
    216 
    217     uuid_str = GNUNET_STRINGS_data_to_string_alloc (uuid,
    218                                                     sizeof (*uuid));
    219 
    220     GNUNET_asprintf (&path,
    221                      "dkg-key/%s",
    222                      uuid_str);
    223 
    224     kdo->url = TALER_url_join (backend_url,
    225                                path,
    226                                NULL);
    227 
    228     GNUNET_free (path);
    229     GNUNET_free (uuid_str);
    230   }
    231 
    232   /* prepare curl options and fire the request */
    233   kdo->ctx = ctx;
    234   kdo->cb = cb;
    235   kdo->cb_cls = cb_cls;
    236 
    237   eh = FROSIX_curl_easy_get_ (kdo->url);
    238 
    239   GNUNET_assert (CURLE_OK ==
    240                  curl_easy_setopt (eh,
    241                                    CURLOPT_CUSTOMREQUEST,
    242                                    "DELETE"));
    243   GNUNET_assert (CURLE_OK ==
    244                  curl_easy_setopt (eh,
    245                                    CURLOPT_HEADERDATA,
    246                                    kdo));
    247   GNUNET_assert (CURLE_OK ==
    248                  curl_easy_setopt (eh,
    249                                    CURLOPT_HEADERFUNCTION,
    250                                    &handle_header));
    251   kdo->job = GNUNET_CURL_job_add (ctx,
    252                                   eh,
    253                                   &handle_key_delete_finished,
    254                                   kdo);
    255 
    256   return kdo;
    257 }