LCOV - code coverage report
Current view: top level - lib - sync_api_upload.c (source / functions) Hit Total Coverage
Test: GNU Taler sync coverage report Lines: 113 160 70.6 %
Date: 2021-05-09 06:21:04 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2019 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU Lesser 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             :   TALER 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 Lesser General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU Lesser General Public
      16             :   License along with TALER; see the file COPYING.LGPL.  If not,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : 
      20             : /**
      21             :  * @file lib/sync_api_upload.c
      22             :  * @brief Implementation of the upload POST
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include <curl/curl.h>
      27             : #include <jansson.h>
      28             : #include <microhttpd.h> /* just for HTTP status codes */
      29             : #include <gnunet/gnunet_util_lib.h>
      30             : #include <gnunet/gnunet_curl_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : #include <taler/taler_json_lib.h>
      33             : #include "sync_service.h"
      34             : #include "sync_api_curl_defaults.h"
      35             : 
      36             : 
      37             : /**
      38             :  * @brief Handle for an upload operation.
      39             :  */
      40             : struct SYNC_UploadOperation
      41             : {
      42             : 
      43             :   /**
      44             :    * The url for this request.
      45             :    */
      46             :   char *url;
      47             : 
      48             :   /**
      49             :    * Handle for the request.
      50             :    */
      51             :   struct GNUNET_CURL_Job *job;
      52             : 
      53             :   /**
      54             :    * Reference to the execution context.
      55             :    */
      56             :   struct GNUNET_CURL_Context *ctx;
      57             : 
      58             :   /**
      59             :    * Function to call with the result.
      60             :    */
      61             :   SYNC_UploadCallback cb;
      62             : 
      63             :   /**
      64             :    * Closure for @e cb.
      65             :    */
      66             :   void *cb_cls;
      67             : 
      68             :   /**
      69             :    * Payment URI we received from the service, or NULL.
      70             :    */
      71             :   char *pay_uri;
      72             : 
      73             :   /**
      74             :    * Hash of the data we are uploading.
      75             :    */
      76             :   struct GNUNET_HashCode new_upload_hash;
      77             : };
      78             : 
      79             : 
      80             : /**
      81             :  * Function called when we're done processing the
      82             :  * HTTP /backup request.
      83             :  *
      84             :  * @param cls the `struct SYNC_UploadOperation`
      85             :  * @param response_code HTTP response code, 0 on error
      86             :  * @param response
      87             :  */
      88             : static void
      89           5 : handle_upload_finished (void *cls,
      90             :                         long response_code,
      91             :                         const void *data,
      92             :                         size_t data_size)
      93             : {
      94           5 :   struct SYNC_UploadOperation *uo = cls;
      95           5 :   enum TALER_ErrorCode ec = TALER_EC_INVALID;
      96             :   struct SYNC_UploadDetails ud;
      97             :   struct SYNC_UploadDetails *udp;
      98             : 
      99           5 :   uo->job = NULL;
     100           5 :   udp = NULL;
     101           5 :   memset (&ud, 0, sizeof (ud));
     102           5 :   switch (response_code)
     103             :   {
     104           0 :   case 0:
     105           0 :     break;
     106           2 :   case MHD_HTTP_NO_CONTENT:
     107           2 :     ud.us = SYNC_US_SUCCESS;
     108           2 :     ud.details.curr_backup_hash = &uo->new_upload_hash;
     109           2 :     udp = &ud;
     110           2 :     ec = TALER_EC_NONE;
     111           2 :     break;
     112           0 :   case MHD_HTTP_NOT_MODIFIED:
     113           0 :     ud.us = SYNC_US_SUCCESS;
     114           0 :     ud.details.curr_backup_hash = &uo->new_upload_hash;
     115           0 :     udp = &ud;
     116           0 :     ec = TALER_EC_NONE;
     117           0 :     break;
     118           0 :   case MHD_HTTP_BAD_REQUEST:
     119           0 :     GNUNET_break (0);
     120           0 :     ec = TALER_JSON_get_error_code2 (data,
     121             :                                      data_size);
     122           0 :     break;
     123           2 :   case MHD_HTTP_PAYMENT_REQUIRED:
     124           2 :     ud.us = SYNC_US_PAYMENT_REQUIRED;
     125           2 :     ud.details.payment_request = uo->pay_uri;
     126           2 :     udp = &ud;
     127           2 :     ec = TALER_EC_NONE;
     128           2 :     break;
     129           0 :   case MHD_HTTP_FORBIDDEN:
     130           0 :     GNUNET_break (0);
     131           0 :     ec = TALER_JSON_get_error_code2 (data,
     132             :                                      data_size);
     133           0 :     break;
     134           1 :   case MHD_HTTP_CONFLICT:
     135           1 :     ud.us = SYNC_US_CONFLICTING_BACKUP;
     136           1 :     GNUNET_CRYPTO_hash (data,
     137             :                         data_size,
     138             :                         &ud.details.recovered_backup.existing_backup_hash);
     139             :     ud.details.recovered_backup.existing_backup_size
     140           1 :       = data_size;
     141             :     ud.details.recovered_backup.existing_backup
     142           1 :       = data;
     143           1 :     udp = &ud;
     144           1 :     ec = TALER_EC_NONE;
     145           1 :     break;
     146           0 :   case MHD_HTTP_GONE:
     147           0 :     ec = TALER_JSON_get_error_code2 (data,
     148             :                                      data_size);
     149           0 :     break;
     150           0 :   case MHD_HTTP_LENGTH_REQUIRED:
     151           0 :     GNUNET_break (0);
     152           0 :     break;
     153           0 :   case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE:
     154           0 :     ec = TALER_JSON_get_error_code2 (data,
     155             :                                      data_size);
     156           0 :     break;
     157           0 :   case MHD_HTTP_TOO_MANY_REQUESTS:
     158           0 :     ec = TALER_JSON_get_error_code2 (data,
     159             :                                      data_size);
     160           0 :     break;
     161           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     162           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     163             :                 "Internal server error: `%.*s\n",
     164             :                 (int) data_size,
     165             :                 (const char *) data);
     166           0 :     break;
     167             :   }
     168           5 :   if (NULL != uo->cb)
     169             :   {
     170           5 :     uo->cb (uo->cb_cls,
     171             :             ec,
     172             :             response_code,
     173             :             udp);
     174           5 :     uo->cb = NULL;
     175             :   }
     176           5 :   SYNC_upload_cancel (uo);
     177           5 : }
     178             : 
     179             : 
     180             : /**
     181             :  * Handle HTTP header received by curl.
     182             :  *
     183             :  * @param buffer one line of HTTP header data
     184             :  * @param size size of an item
     185             :  * @param nitems number of items passed
     186             :  * @param userdata our `struct SYNC_DownloadOperation *`
     187             :  * @return `size * nitems`
     188             :  */
     189             : static size_t
     190          33 : handle_header (char *buffer,
     191             :                size_t size,
     192             :                size_t nitems,
     193             :                void *userdata)
     194             : {
     195          33 :   struct SYNC_UploadOperation *uo = userdata;
     196          33 :   size_t total = size * nitems;
     197             :   char *ndup;
     198             :   const char *hdr_type;
     199             :   char *hdr_val;
     200             :   char *sp;
     201             : 
     202          33 :   ndup = GNUNET_strndup (buffer,
     203             :                          total);
     204          33 :   hdr_type = strtok_r (ndup,
     205             :                        ":",
     206             :                        &sp);
     207          33 :   if (NULL == hdr_type)
     208             :   {
     209           0 :     GNUNET_free (ndup);
     210           0 :     return total;
     211             :   }
     212          33 :   hdr_val = strtok_r (NULL,
     213             :                       "",
     214             :                       &sp);
     215          33 :   if (NULL == hdr_val)
     216             :   {
     217          10 :     GNUNET_free (ndup);
     218          10 :     return total;
     219             :   }
     220          23 :   if (' ' == *hdr_val)
     221          23 :     hdr_val++;
     222          23 :   if (0 == strcasecmp (hdr_type,
     223             :                        "Taler"))
     224             :   {
     225             :     size_t len;
     226             : 
     227             :     /* found payment URI we care about! */
     228           2 :     uo->pay_uri = GNUNET_strdup (hdr_val);
     229           2 :     len = strlen (uo->pay_uri);
     230           6 :     while ( (len > 0) &&
     231           6 :             ( ('\n' == uo->pay_uri[len - 1]) ||
     232           4 :               ('\r' == uo->pay_uri[len - 1]) ) )
     233             :     {
     234           4 :       len--;
     235           4 :       uo->pay_uri[len] = '\0';
     236             :     }
     237             :   }
     238          23 :   GNUNET_free (ndup);
     239          23 :   return total;
     240             : }
     241             : 
     242             : 
     243             : struct SYNC_UploadOperation *
     244           5 : SYNC_upload (struct GNUNET_CURL_Context *ctx,
     245             :              const char *base_url,
     246             :              struct SYNC_AccountPrivateKeyP *priv,
     247             :              const struct GNUNET_HashCode *prev_backup_hash,
     248             :              size_t backup_size,
     249             :              const void *backup,
     250             :              enum SYNC_PaymentOptions po,
     251             :              const char *paid_order_id,
     252             :              SYNC_UploadCallback cb,
     253             :              void *cb_cls)
     254             : {
     255             :   struct SYNC_AccountSignatureP account_sig;
     256             :   struct SYNC_UploadOperation *uo;
     257             :   CURL *eh;
     258             :   struct curl_slist *job_headers;
     259           5 :   struct SYNC_UploadSignaturePS usp = {
     260           5 :     .purpose.purpose = htonl (TALER_SIGNATURE_SYNC_BACKUP_UPLOAD),
     261           5 :     .purpose.size = htonl (sizeof (usp))
     262             :   };
     263             : 
     264           5 :   if (NULL != prev_backup_hash)
     265           3 :     usp.old_backup_hash = *prev_backup_hash;
     266           5 :   GNUNET_CRYPTO_hash (backup,
     267             :                       backup_size,
     268             :                       &usp.new_backup_hash);
     269           5 :   GNUNET_CRYPTO_eddsa_sign (&priv->eddsa_priv,
     270             :                             &usp,
     271             :                             &account_sig.eddsa_sig);
     272             : 
     273             :   /* setup our HTTP headers */
     274           5 :   job_headers = NULL;
     275             :   {
     276             :     struct curl_slist *ext;
     277             :     char *val;
     278             :     char *hdr;
     279             : 
     280             :     /* Set Sync-Signature header */
     281           5 :     val = GNUNET_STRINGS_data_to_string_alloc (&account_sig,
     282             :                                                sizeof (account_sig));
     283           5 :     GNUNET_asprintf (&hdr,
     284             :                      "Sync-Signature: %s",
     285             :                      val);
     286           5 :     GNUNET_free (val);
     287           5 :     ext = curl_slist_append (job_headers,
     288             :                              hdr);
     289           5 :     GNUNET_free (hdr);
     290           5 :     if (NULL == ext)
     291             :     {
     292           0 :       GNUNET_break (0);
     293           0 :       curl_slist_free_all (job_headers);
     294           0 :       return NULL;
     295             :     }
     296           5 :     job_headers = ext;
     297             : 
     298             :     /* set If-None-Match header */
     299           5 :     val = GNUNET_STRINGS_data_to_string_alloc (&usp.new_backup_hash,
     300             :                                                sizeof (struct GNUNET_HashCode));
     301           5 :     GNUNET_asprintf (&hdr,
     302             :                      "%s: %s",
     303             :                      MHD_HTTP_HEADER_IF_NONE_MATCH,
     304             :                      val);
     305           5 :     GNUNET_free (val);
     306           5 :     ext = curl_slist_append (job_headers,
     307             :                              hdr);
     308           5 :     GNUNET_free (hdr);
     309           5 :     if (NULL == ext)
     310             :     {
     311           0 :       GNUNET_break (0);
     312           0 :       curl_slist_free_all (job_headers);
     313           0 :       return NULL;
     314             :     }
     315           5 :     job_headers = ext;
     316             : 
     317             :     /* Setup If-Match header */
     318           5 :     if (NULL != prev_backup_hash)
     319             :     {
     320           3 :       val = GNUNET_STRINGS_data_to_string_alloc (&usp.old_backup_hash,
     321             :                                                  sizeof (struct
     322             :                                                          GNUNET_HashCode));
     323           3 :       GNUNET_asprintf (&hdr,
     324             :                        "If-Match: %s",
     325             :                        val);
     326           3 :       GNUNET_free (val);
     327           3 :       ext = curl_slist_append (job_headers,
     328             :                                hdr);
     329           3 :       GNUNET_free (hdr);
     330           3 :       if (NULL == ext)
     331             :       {
     332           0 :         GNUNET_break (0);
     333           0 :         curl_slist_free_all (job_headers);
     334           0 :         return NULL;
     335             :       }
     336           3 :       job_headers = ext;
     337             :     }
     338             :   }
     339             :   /* Finished setting up headers */
     340             : 
     341           5 :   uo = GNUNET_new (struct SYNC_UploadOperation);
     342           5 :   uo->new_upload_hash = usp.new_backup_hash;
     343             :   {
     344             :     char *path;
     345             :     char *account_s;
     346             :     struct SYNC_AccountPublicKeyP pub;
     347             : 
     348           5 :     GNUNET_CRYPTO_eddsa_key_get_public (&priv->eddsa_priv,
     349             :                                         &pub.eddsa_pub);
     350           5 :     account_s = GNUNET_STRINGS_data_to_string_alloc (&pub,
     351             :                                                      sizeof (pub));
     352           5 :     GNUNET_asprintf (&path,
     353             :                      "backups/%s",
     354             :                      account_s);
     355           5 :     GNUNET_free (account_s);
     356           5 :     if  (0 != (po & SYNC_PO_FRESH_ORDER))
     357             :     {
     358           0 :       uo->url = (0 != (po & SYNC_PO_FORCE_PAYMENT))
     359           0 :       ? TALER_url_join (base_url,
     360             :                         path,
     361             :                         "fresh",
     362             :                         "y",
     363             :                         "pay",
     364             :                         "y",
     365             :                         (NULL != paid_order_id)
     366             :                                 ? "paying"
     367             :                                 : NULL,
     368             :                         paid_order_id,
     369             :                         NULL)
     370           0 :               : TALER_url_join (base_url,
     371             :                                 path,
     372             :                                 "fresh",
     373             :                                 "y",
     374             :                                 (NULL != paid_order_id)
     375             :                                 ? "paying"
     376             :                                 : NULL,
     377             :                                 paid_order_id,
     378             :                                 NULL);
     379             :     }
     380             :     else
     381             :     {
     382           5 :       uo->url = (0 != (po & SYNC_PO_FORCE_PAYMENT))
     383           1 :       ? TALER_url_join (base_url,
     384             :                         path,
     385             :                         "pay",
     386             :                         "y",
     387             :                         (NULL != paid_order_id)
     388             :                                 ? "paying"
     389             :                                 : NULL,
     390             :                         paid_order_id,
     391             :                         NULL)
     392           5 :               : TALER_url_join (base_url,
     393             :                                 path,
     394             :                                 (NULL != paid_order_id)
     395             :                                 ? "paying"
     396             :                                 : NULL,
     397             :                                 paid_order_id,
     398             :                                 NULL);
     399             :     }
     400             : 
     401           5 :     GNUNET_free (path);
     402             :   }
     403           5 :   uo->ctx = ctx;
     404           5 :   uo->cb = cb;
     405           5 :   uo->cb_cls = cb_cls;
     406           5 :   eh = SYNC_curl_easy_get_ (uo->url);
     407           5 :   GNUNET_assert (CURLE_OK ==
     408             :                  curl_easy_setopt (eh,
     409             :                                    CURLOPT_POSTFIELDS,
     410             :                                    backup));
     411           5 :   GNUNET_assert (CURLE_OK ==
     412             :                  curl_easy_setopt (eh,
     413             :                                    CURLOPT_POSTFIELDSIZE,
     414             :                                    (long) backup_size));
     415           5 :   GNUNET_assert (CURLE_OK ==
     416             :                  curl_easy_setopt (eh,
     417             :                                    CURLOPT_HEADERFUNCTION,
     418             :                                    &handle_header));
     419           5 :   GNUNET_assert (CURLE_OK ==
     420             :                  curl_easy_setopt (eh,
     421             :                                    CURLOPT_HEADERDATA,
     422             :                                    uo));
     423           5 :   uo->job = GNUNET_CURL_job_add_raw (ctx,
     424             :                                      eh,
     425             :                                      job_headers,
     426             :                                      &handle_upload_finished,
     427             :                                      uo);
     428           5 :   curl_slist_free_all (job_headers);
     429           5 :   return uo;
     430             : }
     431             : 
     432             : 
     433             : /**
     434             :  * Cancel the upload.  Note that aborting an upload does NOT guarantee
     435             :  * that it did not complete, it is possible that the server did
     436             :  * receive the full request before the upload is aborted.
     437             :  *
     438             :  * @param uo operation to cancel.
     439             :  */
     440             : void
     441           5 : SYNC_upload_cancel (struct SYNC_UploadOperation *uo)
     442             : {
     443           5 :   if (NULL != uo->job)
     444             :   {
     445           0 :     GNUNET_CURL_job_cancel (uo->job);
     446           0 :     uo->job = NULL;
     447             :   }
     448           5 :   GNUNET_free (uo->pay_uri);
     449           5 :   GNUNET_free (uo->url);
     450           5 :   GNUNET_free (uo);
     451           5 : }
     452             : 
     453             : 
     454             : /* end of sync_api_upload.c */

Generated by: LCOV version 1.14