mirror of
https://github.com/curl/curl.git
synced 2026-08-25 16:33:38 +03:00
curl: move IPFS code into src/tool_ipfs.[ch]
- convert ensure_trailing into ensure_trailing_slash - strdup the URL string to own it proper - use shorter variable names - combine some expressions - simplify error handling in ipfs_gateway() - add MAX_GATEWAY_URL_LEN + proper bailout if maximum is reached - ipfs-gateway.d polish and simplification - shorten ipfs error message + make them "synthetic" Closes #12281
This commit is contained in:
parent
fd7ef00f43
commit
01d9b8bc7c
5 changed files with 346 additions and 317 deletions
|
|
@ -81,6 +81,7 @@
|
|||
#include "tool_help.h"
|
||||
#include "tool_hugehelp.h"
|
||||
#include "tool_progress.h"
|
||||
#include "tool_ipfs.h"
|
||||
#include "dynbuf.h"
|
||||
|
||||
#include "memdebug.h" /* keep this as LAST include */
|
||||
|
|
@ -697,296 +698,6 @@ noretry:
|
|||
return result;
|
||||
}
|
||||
|
||||
/* helper function to ensure input ends in val_to_ensure */
|
||||
static CURLcode ensure_trailing(char **input, const char val_to_ensure)
|
||||
{
|
||||
if(*input && **input) {
|
||||
size_t len = strlen(*input);
|
||||
if(((*input)[len - 1] != val_to_ensure)) {
|
||||
struct curlx_dynbuf dyn;
|
||||
curlx_dyn_init(&dyn, len + 2);
|
||||
|
||||
if(curlx_dyn_addn(&dyn, *input, len)) {
|
||||
Curl_safefree(*input);
|
||||
return CURLE_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
Curl_safefree(*input);
|
||||
|
||||
if(curlx_dyn_addn(&dyn, &val_to_ensure, 1))
|
||||
return CURLE_OUT_OF_MEMORY;
|
||||
|
||||
*input = curlx_dyn_ptr(&dyn);
|
||||
}
|
||||
}
|
||||
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
||||
static char *ipfs_gateway(void)
|
||||
{
|
||||
char *gateway = NULL;
|
||||
char *ipfs_path = NULL;
|
||||
char *gateway_composed_file_path = NULL;
|
||||
FILE *gateway_file = NULL;
|
||||
|
||||
gateway = curlx_getenv("IPFS_GATEWAY");
|
||||
|
||||
/* Gateway is found from environment variable. */
|
||||
if(gateway) {
|
||||
if(ensure_trailing(&gateway, '/')) {
|
||||
Curl_safefree(gateway);
|
||||
return NULL;
|
||||
}
|
||||
return gateway;
|
||||
}
|
||||
|
||||
/* Try to find the gateway in the IPFS data folder. */
|
||||
ipfs_path = curlx_getenv("IPFS_PATH");
|
||||
|
||||
if(!ipfs_path) {
|
||||
char *home = curlx_getenv("HOME");
|
||||
if(home && *home)
|
||||
ipfs_path = aprintf("%s/.ipfs/", home);
|
||||
/* fallback to "~/.ipfs", as that's the default location. */
|
||||
|
||||
Curl_safefree(home);
|
||||
}
|
||||
|
||||
if(!ipfs_path) {
|
||||
Curl_safefree(gateway);
|
||||
Curl_safefree(ipfs_path);
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
if(ensure_trailing(&ipfs_path, '/')) {
|
||||
Curl_safefree(gateway);
|
||||
Curl_safefree(ipfs_path);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
gateway_composed_file_path = aprintf("%sgateway", ipfs_path);
|
||||
|
||||
if(!gateway_composed_file_path) {
|
||||
Curl_safefree(gateway);
|
||||
Curl_safefree(ipfs_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gateway_file = fopen(gateway_composed_file_path, FOPEN_READTEXT);
|
||||
Curl_safefree(gateway_composed_file_path);
|
||||
|
||||
if(gateway_file) {
|
||||
int c;
|
||||
struct curlx_dynbuf dyn;
|
||||
curlx_dyn_init(&dyn, INT_MAX);
|
||||
|
||||
/* get the first line of the gateway file, ignore the rest */
|
||||
while((c = getc(gateway_file)) != EOF && c != '\n' && c != '\r') {
|
||||
if(curlx_dyn_addn(&dyn, &c, 1))
|
||||
break;
|
||||
}
|
||||
|
||||
if(gateway_file)
|
||||
fclose(gateway_file);
|
||||
|
||||
if(curlx_dyn_len(&dyn) > 0)
|
||||
gateway = curlx_dyn_ptr(&dyn);
|
||||
|
||||
if(gateway)
|
||||
ensure_trailing(&gateway, '/');
|
||||
|
||||
if(!gateway) {
|
||||
Curl_safefree(ipfs_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Curl_safefree(ipfs_path);
|
||||
|
||||
return gateway;
|
||||
}
|
||||
|
||||
Curl_safefree(gateway);
|
||||
Curl_safefree(ipfs_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Rewrite ipfs://<cid> and ipns://<cid> to a HTTP(S)
|
||||
* URL that can be handled by an IPFS gateway.
|
||||
*/
|
||||
static CURLcode ipfs_url_rewrite(CURLU *uh, const char *protocol, char **url,
|
||||
struct OperationConfig *config)
|
||||
{
|
||||
CURLcode result = CURLE_URL_MALFORMAT;
|
||||
CURLUcode getResult;
|
||||
char *gateway = NULL;
|
||||
char *gatewayhost = NULL;
|
||||
char *gatewaypath = NULL;
|
||||
char *gatewayquery = NULL;
|
||||
char *inputpath = NULL;
|
||||
char *gatewayscheme = NULL;
|
||||
char *gatewayport = NULL;
|
||||
char *cid = NULL;
|
||||
char *pathbuffer = NULL;
|
||||
CURLU *gatewaysurl = curl_url();
|
||||
|
||||
if(!gatewaysurl) {
|
||||
result = CURLE_FAILED_INIT;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
getResult = curl_url_get(uh, CURLUPART_HOST, &cid, CURLU_URLDECODE);
|
||||
|
||||
if(getResult) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
if(!cid) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* We might have a --ipfs-gateway argument. Check it first and use it. Error
|
||||
* if we do have something but if it's an invalid url.
|
||||
*/
|
||||
if(config->ipfs_gateway) {
|
||||
/* ensure the gateway ends in a trailing / */
|
||||
if(ensure_trailing(&config->ipfs_gateway, '/') != CURLE_OK) {
|
||||
result = CURLE_OUT_OF_MEMORY;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
if(!curl_url_set(gatewaysurl, CURLUPART_URL, config->ipfs_gateway,
|
||||
CURLU_GUESS_SCHEME)) {
|
||||
gateway = strdup(config->ipfs_gateway);
|
||||
if(!gateway) {
|
||||
result = CURLE_URL_MALFORMAT;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
result = CURLE_BAD_FUNCTION_ARGUMENT;
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* this is ensured to end in a trailing / within ipfs_gateway() */
|
||||
gateway = ipfs_gateway();
|
||||
if(!gateway) {
|
||||
result = CURLE_FILE_COULDNT_READ_FILE;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
if(curl_url_set(gatewaysurl, CURLUPART_URL, gateway, 0)) {
|
||||
result = CURLE_URL_MALFORMAT;
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* check for unsupported gateway parts */
|
||||
if(curl_url_get(gatewaysurl, CURLUPART_QUERY, &gatewayquery, 0)
|
||||
!= CURLUE_NO_QUERY) {
|
||||
result = CURLE_URL_MALFORMAT;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* get gateway parts */
|
||||
if(curl_url_get(gatewaysurl, CURLUPART_HOST,
|
||||
&gatewayhost, CURLU_URLDECODE)) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
if(curl_url_get(gatewaysurl, CURLUPART_SCHEME,
|
||||
&gatewayscheme, CURLU_URLDECODE)) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
curl_url_get(gatewaysurl, CURLUPART_PORT, &gatewayport, CURLU_URLDECODE);
|
||||
curl_url_get(gatewaysurl, CURLUPART_PATH, &gatewaypath, CURLU_URLDECODE);
|
||||
|
||||
/* get the path from user input */
|
||||
curl_url_get(uh, CURLUPART_PATH, &inputpath, CURLU_URLDECODE);
|
||||
/* inputpath might be NULL or a valid pointer now */
|
||||
|
||||
/* set gateway parts in input url */
|
||||
if(curl_url_set(uh, CURLUPART_SCHEME, gatewayscheme, CURLU_URLENCODE)) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
if(curl_url_set(uh, CURLUPART_HOST, gatewayhost, CURLU_URLENCODE)) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
if(curl_url_set(uh, CURLUPART_PORT, gatewayport, CURLU_URLENCODE)) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* if the input path is just a slash, clear it */
|
||||
if(inputpath && (inputpath[0] == '/') && !inputpath[1])
|
||||
*inputpath = '\0';
|
||||
|
||||
/* ensure the gateway path ends with a trailing slash */
|
||||
ensure_trailing(&gatewaypath, '/');
|
||||
|
||||
pathbuffer = aprintf("%s%s/%s%s", gatewaypath, protocol, cid,
|
||||
inputpath ? inputpath : "");
|
||||
if(!pathbuffer) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
if(curl_url_set(uh, CURLUPART_PATH, pathbuffer, CURLU_URLENCODE)) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* Free whatever it has now, rewriting is next */
|
||||
Curl_safefree(*url);
|
||||
|
||||
if(curl_url_get(uh, CURLUPART_URL, url, CURLU_URLENCODE)) {
|
||||
goto clean;
|
||||
}
|
||||
|
||||
result = CURLE_OK;
|
||||
|
||||
clean:
|
||||
free(gateway);
|
||||
curl_free(gatewayhost);
|
||||
curl_free(gatewaypath);
|
||||
curl_free(gatewayquery);
|
||||
curl_free(inputpath);
|
||||
curl_free(gatewayscheme);
|
||||
curl_free(gatewayport);
|
||||
curl_free(cid);
|
||||
curl_free(pathbuffer);
|
||||
curl_url_cleanup(gatewaysurl);
|
||||
|
||||
switch(result) {
|
||||
case CURLE_URL_MALFORMAT:
|
||||
helpf(tool_stderr, "malformed URL. Visit https://curl.se/"
|
||||
"docs/ipfs.html#gateway-file-and-"
|
||||
"environment-variable for more "
|
||||
"information");
|
||||
break;
|
||||
case CURLE_FILE_COULDNT_READ_FILE:
|
||||
helpf(tool_stderr, "IPFS automatic gateway detection "
|
||||
"failure. Visit https://curl.se/docs/"
|
||||
"ipfs.html#malformed-gateway-url for "
|
||||
"more information");
|
||||
break;
|
||||
case CURLE_BAD_FUNCTION_ARGUMENT:
|
||||
helpf(tool_stderr, "--ipfs-gateway argument results in "
|
||||
"malformed URL. Visit https://curl.se/"
|
||||
"docs/ipfs.html#malformed-gateway-url "
|
||||
"for more information");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the protocol token for the scheme used in the given URL
|
||||
*/
|
||||
|
|
@ -1010,13 +721,13 @@ static CURLcode url_proto(char **url,
|
|||
if(curl_strequal(schemep, proto_ipfs) ||
|
||||
curl_strequal(schemep, proto_ipns)) {
|
||||
result = ipfs_url_rewrite(uh, schemep, url, config);
|
||||
|
||||
/* short-circuit proto_token, we know it's ipfs or ipns */
|
||||
if(curl_strequal(schemep, proto_ipfs))
|
||||
proto = proto_ipfs;
|
||||
else if(curl_strequal(schemep, proto_ipns))
|
||||
proto = proto_ipns;
|
||||
|
||||
if(result)
|
||||
config->synthetic_error = TRUE;
|
||||
}
|
||||
else
|
||||
proto = proto_token(schemep);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue