From 6296b9d383850bf603fb750e727dc2064b80fe69 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 17 Oct 2025 13:05:58 +0200 Subject: [PATCH] tool_ipfs: simplify the ipfs gateway logic - make sure memory allocated by libcurl is freed with curl_free() - drop the ensure_trailing_slash complexity Closes #19097 --- src/tool_ipfs.c | 108 ++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 73 deletions(-) diff --git a/src/tool_ipfs.c b/src/tool_ipfs.c index 13124133ea..1f77dad47f 100644 --- a/src/tool_ipfs.c +++ b/src/tool_ipfs.c @@ -30,100 +30,70 @@ #include "tool_ipfs.h" #include "memdebug.h" /* keep this as LAST include */ -/* ensure input ends in slash */ -static CURLcode ensure_trailing_slash(char **input) +/* input string ends in slash? */ +static bool has_trailing_slash(const char *input) { - if(*input && **input) { - size_t len = strlen(*input); - if(((*input)[len - 1] != '/')) { - struct dynbuf dyn; - curlx_dyn_init(&dyn, len + 2); - - if(curlx_dyn_addn(&dyn, *input, len)) { - tool_safefree(*input); - return CURLE_OUT_OF_MEMORY; - } - - tool_safefree(*input); - - if(curlx_dyn_addn(&dyn, "/", 1)) - return CURLE_OUT_OF_MEMORY; - - *input = curlx_dyn_ptr(&dyn); - } - } - - return CURLE_OK; + size_t len = strlen(input); + return (len && input[len - 1] == '/'); } static char *ipfs_gateway(void) { - char *ipfs_path = NULL; - char *gateway_composed_file_path = NULL; - FILE *gateway_file = NULL; - char *gateway = curl_getenv("IPFS_GATEWAY"); - - /* Gateway is found from environment variable. */ - if(gateway) { - if(ensure_trailing_slash(&gateway)) - goto fail; - return gateway; - } + char *ipfs_path_c = NULL; + char *gateway_composed_c = NULL; + FILE *gfile = NULL; + char *gateway_env = getenv("IPFS_GATEWAY"); + + if(gateway_env) + return strdup(gateway_env); /* Try to find the gateway in the IPFS data folder. */ - ipfs_path = curl_getenv("IPFS_PATH"); + ipfs_path_c = curl_getenv("IPFS_PATH"); - if(!ipfs_path) { + if(!ipfs_path_c) { char *home = getenv("HOME"); - if(home && *home) - ipfs_path = curl_maprintf("%s/.ipfs/", home); /* fallback to "~/.ipfs", as that is the default location. */ + if(home && *home) + ipfs_path_c = curl_maprintf("%s/.ipfs/", home); + if(!ipfs_path_c) + goto fail; } - if(!ipfs_path || ensure_trailing_slash(&ipfs_path)) - goto fail; - - gateway_composed_file_path = curl_maprintf("%sgateway", ipfs_path); + gateway_composed_c = + curl_maprintf("%s%sgateway", ipfs_path_c, + has_trailing_slash(ipfs_path_c) ? "" : "/"); - if(!gateway_composed_file_path) + if(!gateway_composed_c) goto fail; - gateway_file = curlx_fopen(gateway_composed_file_path, FOPEN_READTEXT); - tool_safefree(gateway_composed_file_path); + gfile = curlx_fopen(gateway_composed_c, FOPEN_READTEXT); + curl_free(gateway_composed_c); - if(gateway_file) { + if(gfile) { int c; struct dynbuf dyn; + char *gateway = NULL; curlx_dyn_init(&dyn, MAX_GATEWAY_URL_LEN); /* get the first line of the gateway file, ignore the rest */ - while((c = getc(gateway_file)) != EOF && c != '\n' && c != '\r') { + while((c = getc(gfile)) != EOF && c != '\n' && c != '\r') { char c_char = (char)c; if(curlx_dyn_addn(&dyn, &c_char, 1)) goto fail; } - curlx_fclose(gateway_file); - gateway_file = NULL; - if(curlx_dyn_len(&dyn)) gateway = curlx_dyn_ptr(&dyn); - if(gateway) - ensure_trailing_slash(&gateway); - - if(!gateway) - goto fail; - - tool_safefree(ipfs_path); + curl_free(ipfs_path_c); + curlx_fclose(gfile); return gateway; } fail: - if(gateway_file) - curlx_fclose(gateway_file); - tool_safefree(gateway); - tool_safefree(ipfs_path); + if(gfile) + curlx_fclose(gfile); + curl_free(ipfs_path_c); return NULL; } @@ -161,20 +131,13 @@ CURLcode ipfs_url_rewrite(CURLU *uh, const char *protocol, char **url, * if we do have something but if it is an invalid url. */ if(config->ipfs_gateway) { - /* ensure the gateway ends in a trailing / */ - if(ensure_trailing_slash(&config->ipfs_gateway) != CURLE_OK) { - result = CURLE_OUT_OF_MEMORY; - goto clean; - } - if(!curl_url_set(gatewayurl, CURLUPART_URL, config->ipfs_gateway, - CURLU_GUESS_SCHEME)) { + CURLU_GUESS_SCHEME)) { gateway = strdup(config->ipfs_gateway); if(!gateway) { result = CURLE_URL_MALFORMAT; goto clean; } - } else { result = CURLE_BAD_FUNCTION_ARGUMENT; @@ -182,7 +145,6 @@ CURLcode ipfs_url_rewrite(CURLU *uh, const char *protocol, char **url, } } else { - /* this is ensured to end in a trailing / within ipfs_gateway() */ gateway = ipfs_gateway(); if(!gateway) { result = CURLE_FILE_COULDNT_READ_FILE; @@ -230,10 +192,10 @@ CURLcode ipfs_url_rewrite(CURLU *uh, const char *protocol, char **url, if(inputpath && (inputpath[0] == '/') && !inputpath[1]) *inputpath = '\0'; - /* ensure the gateway path ends with a trailing slash */ - ensure_trailing_slash(&gwpath); - pathbuffer = curl_maprintf("%s%s/%s%s", gwpath, protocol, cid, + pathbuffer = curl_maprintf("%s%s%s/%s%s", gwpath, + has_trailing_slash(gwpath) ? "" : "/", + protocol, cid, inputpath ? inputpath : ""); if(!pathbuffer) { goto clean; -- 2.47.3