quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

tool_ipfs.c (7677B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at https://curl.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 #include "tool_setup.h"
     25 
     26 #ifndef CURL_DISABLE_IPFS
     27 
     28 #include "tool_cfgable.h"
     29 #include "tool_msgs.h"
     30 #include "tool_ipfs.h"
     31 #include "memdebug.h" /* keep this as LAST include */
     32 
     33 /* ensure input ends in slash */
     34 static CURLcode ensure_trailing_slash(char **input)
     35 {
     36   if(*input && **input) {
     37     size_t len = strlen(*input);
     38     if(((*input)[len - 1] != '/')) {
     39       struct dynbuf dyn;
     40       curlx_dyn_init(&dyn, len + 2);
     41 
     42       if(curlx_dyn_addn(&dyn, *input, len)) {
     43         tool_safefree(*input);
     44         return CURLE_OUT_OF_MEMORY;
     45       }
     46 
     47       tool_safefree(*input);
     48 
     49       if(curlx_dyn_addn(&dyn, "/", 1))
     50         return CURLE_OUT_OF_MEMORY;
     51 
     52       *input = curlx_dyn_ptr(&dyn);
     53     }
     54   }
     55 
     56   return CURLE_OK;
     57 }
     58 
     59 static char *ipfs_gateway(void)
     60 {
     61   char *ipfs_path = NULL;
     62   char *gateway_composed_file_path = NULL;
     63   FILE *gateway_file = NULL;
     64   char *gateway = curl_getenv("IPFS_GATEWAY");
     65 
     66   /* Gateway is found from environment variable. */
     67   if(gateway) {
     68     if(ensure_trailing_slash(&gateway))
     69       goto fail;
     70     return gateway;
     71   }
     72 
     73   /* Try to find the gateway in the IPFS data folder. */
     74   ipfs_path = curl_getenv("IPFS_PATH");
     75 
     76   if(!ipfs_path) {
     77     char *home = getenv("HOME");
     78     if(home && *home)
     79       ipfs_path = aprintf("%s/.ipfs/", home);
     80     /* fallback to "~/.ipfs", as that is the default location. */
     81   }
     82 
     83   if(!ipfs_path || ensure_trailing_slash(&ipfs_path))
     84     goto fail;
     85 
     86   gateway_composed_file_path = aprintf("%sgateway", ipfs_path);
     87 
     88   if(!gateway_composed_file_path)
     89     goto fail;
     90 
     91   gateway_file = fopen(gateway_composed_file_path, FOPEN_READTEXT);
     92   tool_safefree(gateway_composed_file_path);
     93 
     94   if(gateway_file) {
     95     int c;
     96     struct dynbuf dyn;
     97     curlx_dyn_init(&dyn, MAX_GATEWAY_URL_LEN);
     98 
     99     /* get the first line of the gateway file, ignore the rest */
    100     while((c = getc(gateway_file)) != EOF && c != '\n' && c != '\r') {
    101       char c_char = (char)c;
    102       if(curlx_dyn_addn(&dyn, &c_char, 1))
    103         goto fail;
    104     }
    105 
    106     fclose(gateway_file);
    107     gateway_file = NULL;
    108 
    109     if(curlx_dyn_len(&dyn))
    110       gateway = curlx_dyn_ptr(&dyn);
    111 
    112     if(gateway)
    113       ensure_trailing_slash(&gateway);
    114 
    115     if(!gateway)
    116       goto fail;
    117 
    118     tool_safefree(ipfs_path);
    119 
    120     return gateway;
    121   }
    122 fail:
    123   if(gateway_file)
    124     fclose(gateway_file);
    125   tool_safefree(gateway);
    126   tool_safefree(ipfs_path);
    127   return NULL;
    128 }
    129 
    130 /*
    131  * Rewrite ipfs://<cid> and ipns://<cid> to an HTTP(S)
    132  * URL that can be handled by an IPFS gateway.
    133  */
    134 CURLcode ipfs_url_rewrite(CURLU *uh, const char *protocol, char **url,
    135                           struct OperationConfig *config)
    136 {
    137   CURLcode result = CURLE_URL_MALFORMAT;
    138   CURLUcode getResult;
    139   char *gateway = NULL;
    140   char *gwhost = NULL;
    141   char *gwpath = NULL;
    142   char *gwquery = NULL;
    143   char *gwscheme = NULL;
    144   char *gwport = NULL;
    145   char *inputpath = NULL;
    146   char *cid = NULL;
    147   char *pathbuffer = NULL;
    148   char *cloneurl;
    149   CURLU *gatewayurl = curl_url();
    150 
    151   if(!gatewayurl) {
    152     result = CURLE_FAILED_INIT;
    153     goto clean;
    154   }
    155 
    156   getResult = curl_url_get(uh, CURLUPART_HOST, &cid, CURLU_URLDECODE);
    157   if(getResult || !cid)
    158     goto clean;
    159 
    160   /* We might have a --ipfs-gateway argument. Check it first and use it. Error
    161    * if we do have something but if it is an invalid url.
    162    */
    163   if(config->ipfs_gateway) {
    164     /* ensure the gateway ends in a trailing / */
    165     if(ensure_trailing_slash(&config->ipfs_gateway) != CURLE_OK) {
    166       result = CURLE_OUT_OF_MEMORY;
    167       goto clean;
    168     }
    169 
    170     if(!curl_url_set(gatewayurl, CURLUPART_URL, config->ipfs_gateway,
    171                     CURLU_GUESS_SCHEME)) {
    172       gateway = strdup(config->ipfs_gateway);
    173       if(!gateway) {
    174         result = CURLE_URL_MALFORMAT;
    175         goto clean;
    176       }
    177 
    178     }
    179     else {
    180       result = CURLE_BAD_FUNCTION_ARGUMENT;
    181       goto clean;
    182     }
    183   }
    184   else {
    185     /* this is ensured to end in a trailing / within ipfs_gateway() */
    186     gateway = ipfs_gateway();
    187     if(!gateway) {
    188       result = CURLE_FILE_COULDNT_READ_FILE;
    189       goto clean;
    190     }
    191 
    192     if(curl_url_set(gatewayurl, CURLUPART_URL, gateway, 0)) {
    193       result = CURLE_URL_MALFORMAT;
    194       goto clean;
    195     }
    196   }
    197 
    198   /* check for unsupported gateway parts */
    199   if(curl_url_get(gatewayurl, CURLUPART_QUERY, &gwquery, 0)
    200                   != CURLUE_NO_QUERY) {
    201     result = CURLE_URL_MALFORMAT;
    202     goto clean;
    203   }
    204 
    205   /* get gateway parts */
    206   if(curl_url_get(gatewayurl, CURLUPART_HOST,
    207                   &gwhost, CURLU_URLDECODE)) {
    208     goto clean;
    209   }
    210 
    211   if(curl_url_get(gatewayurl, CURLUPART_SCHEME,
    212                   &gwscheme, CURLU_URLDECODE)) {
    213     goto clean;
    214   }
    215 
    216   curl_url_get(gatewayurl, CURLUPART_PORT, &gwport, CURLU_URLDECODE);
    217   curl_url_get(gatewayurl, CURLUPART_PATH, &gwpath, CURLU_URLDECODE);
    218 
    219   /* get the path from user input */
    220   curl_url_get(uh, CURLUPART_PATH, &inputpath, CURLU_URLDECODE);
    221   /* inputpath might be NULL or a valid pointer now */
    222 
    223   /* set gateway parts in input url */
    224   if(curl_url_set(uh, CURLUPART_SCHEME, gwscheme, CURLU_URLENCODE) ||
    225      curl_url_set(uh, CURLUPART_HOST, gwhost, CURLU_URLENCODE) ||
    226      curl_url_set(uh, CURLUPART_PORT, gwport, CURLU_URLENCODE))
    227     goto clean;
    228 
    229   /* if the input path is just a slash, clear it */
    230   if(inputpath && (inputpath[0] == '/') && !inputpath[1])
    231     *inputpath = '\0';
    232 
    233   /* ensure the gateway path ends with a trailing slash */
    234   ensure_trailing_slash(&gwpath);
    235 
    236   pathbuffer = aprintf("%s%s/%s%s", gwpath, protocol, cid,
    237                        inputpath ? inputpath : "");
    238   if(!pathbuffer) {
    239     goto clean;
    240   }
    241 
    242   if(curl_url_set(uh, CURLUPART_PATH, pathbuffer, CURLU_URLENCODE)) {
    243     goto clean;
    244   }
    245 
    246   /* Free whatever it has now, rewriting is next */
    247   tool_safefree(*url);
    248 
    249   if(curl_url_get(uh, CURLUPART_URL, &cloneurl, CURLU_URLENCODE)) {
    250     goto clean;
    251   }
    252   /* we need to strdup the URL so that we can call free() on it later */
    253   *url = strdup(cloneurl);
    254   curl_free(cloneurl);
    255   if(!*url)
    256     goto clean;
    257 
    258   result = CURLE_OK;
    259 
    260 clean:
    261   free(gateway);
    262   curl_free(gwhost);
    263   curl_free(gwpath);
    264   curl_free(gwquery);
    265   curl_free(inputpath);
    266   curl_free(gwscheme);
    267   curl_free(gwport);
    268   curl_free(cid);
    269   curl_free(pathbuffer);
    270   curl_url_cleanup(gatewayurl);
    271   {
    272     switch(result) {
    273     case CURLE_URL_MALFORMAT:
    274       helpf(tool_stderr, "malformed target URL");
    275       break;
    276     case CURLE_FILE_COULDNT_READ_FILE:
    277       helpf(tool_stderr, "IPFS automatic gateway detection failed");
    278       break;
    279     case CURLE_BAD_FUNCTION_ARGUMENT:
    280       helpf(tool_stderr, "--ipfs-gateway was given a malformed URL");
    281       break;
    282     default:
    283       break;
    284     }
    285   }
    286   return result;
    287 }
    288 #endif /* !CURL_DISABLE_IPFS */