Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curl/get_url_file_name: use libcurl URL parser #9684

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 48 additions & 44 deletions src/tool_operhlp.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,61 +137,65 @@ char *add_file_name_to_url(char *url, 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 = pc2;
pc = strrchr(path, '/');
pc2 = strrchr(pc ? pc + 1 : path, '\\');
if(pc2)
pc = pc2;

if(pc)
/* duplicate the string beyond the slash */
pc++;
else
/* no slash => empty string */
pc = "";
if(pc)
/* duplicate the string beyond the slash */
pc++;
else
/* no slash => empty string */
pc = "";

*filename = strdup(pc);
if(!*filename)
return CURLE_OUT_OF_MEMORY;
*filename = strdup(pc);
curl_free(path);
if(!*filename)
return CURLE_OUT_OF_MEMORY;

#if defined(MSDOS) || defined(WIN32)
{
char *sanitized;
SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
Curl_safefree(*filename);
if(sc)
return CURLE_URL_MALFORMAT;
*filename = sanitized;
}
{
char *sanitized;
SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
Curl_safefree(*filename);
if(sc)
return CURLE_URL_MALFORMAT;
*filename = sanitized;
}
#endif /* MSDOS || WIN32 */

/* in case we built debug enabled, we allow an environment variable
* named CURL_TESTDIR to prefix the given file name to put it into a
* specific directory
*/
/* in case we built debug enabled, we allow an environment variable
* named CURL_TESTDIR to prefix the given file name to put it into a
* specific directory
*/
#ifdef DEBUGBUILD
{
char *tdir = curlx_getenv("CURL_TESTDIR");
if(tdir) {
char buffer[512]; /* suitably large */
msnprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
Curl_safefree(*filename);
*filename = strdup(buffer); /* clone the buffer */
curl_free(tdir);
if(!*filename)
return CURLE_OUT_OF_MEMORY;
{
char *tdir = curlx_getenv("CURL_TESTDIR");
if(tdir) {
char *alt = aprintf("%s/%s", tdir, *filename);
Curl_safefree(*filename);
*filename = alt;
curl_free(tdir);
if(!*filename)
return CURLE_OUT_OF_MEMORY;
}
}
}
#endif

return CURLE_OK;
return CURLE_OK;
}
curl_url_cleanup(uh);
return CURLE_URL_MALFORMAT;
}
4 changes: 2 additions & 2 deletions tests/data/test1210
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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>

#
# 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: */*
Expand Down