curl/get_url_file_name: use libcurl URL parser

To avoid URL tricks, use the URL parser for this.

This update changes curl's behavior slightly in that it will ignore the
possible query part from the URL and only use the file name from the
actual path from the URL. I consider it a bugfix.

"curl -O localhost/name?giveme-giveme" will now save the output in the
local file named 'name'

Updated test 1210 to verify

Assisted-by: Jay Satiro

Closes #9684
This commit is contained in:
Daniel Stenberg 2022-10-10 11:10:12 +02:00
parent c96462addc
commit 671adfa493
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 50 additions and 46 deletions

View File

@ -149,19 +149,21 @@ CURLcode add_file_name_to_url(CURL *curl, char **inurlp, const char *filename)
CURLcode get_url_file_name(char **filename, const char *url)
{
const char *pc, *pc2;
CURLU *uh = curl_url();
char *path = NULL;
if(!uh)
return CURLE_OUT_OF_MEMORY;
*filename = NULL;
/* Find and get the remote file name */
pc = strstr(url, "://");
if(pc)
pc += 3;
else
pc = url;
if(!curl_url_set(uh, CURLUPART_URL, url, CURLU_GUESS_SCHEME) &&
!curl_url_get(uh, CURLUPART_PATH, &path, 0)) {
curl_url_cleanup(uh);
pc2 = strrchr(pc, '\\');
pc = strrchr(pc, '/');
if(pc2 && (!pc || pc < pc2))
pc = strrchr(path, '/');
pc2 = strrchr(pc ? pc + 1 : path, '\\');
if(pc2)
pc = pc2;
if(pc)
@ -172,6 +174,7 @@ CURLcode get_url_file_name(char **filename, const char *url)
pc = "";
*filename = strdup(pc);
curl_free(path);
if(!*filename)
return CURLE_OUT_OF_MEMORY;
@ -194,16 +197,17 @@ CURLcode get_url_file_name(char **filename, const char *url)
{
char *tdir = curlx_getenv("CURL_TESTDIR");
if(tdir) {
char buffer[512]; /* suitably large */
msnprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
char *alt = aprintf("%s/%s", tdir, *filename);
Curl_safefree(*filename);
*filename = strdup(buffer); /* clone the buffer */
*filename = alt;
curl_free(tdir);
if(!*filename)
return CURLE_OUT_OF_MEMORY;
}
}
#endif
return CURLE_OK;
}
curl_url_cleanup(uh);
return CURLE_URL_MALFORMAT;
}

View File

@ -39,7 +39,7 @@ HTTP GET with -J without Content-Disposition
CURL_TESTDIR=%PWD/log
</setenv>
<command option="no-output,no-include">
http://%HOSTIP:%HTTPPORT/%TESTNUMBER -J -O
http://%HOSTIP:%HTTPPORT/%TESTNUMBER?junk -J -O
</command>
</client>
@ -47,7 +47,7 @@ http://%HOSTIP:%HTTPPORT/%TESTNUMBER -J -O
# Verify data after the test has been "shot"
<verify>
<protocol>
GET /%TESTNUMBER HTTP/1.1
GET /%TESTNUMBER?junk HTTP/1.1
Host: %HOSTIP:%HTTPPORT
User-Agent: curl/%VERSION
Accept: */*