2011-10-04 18:03:20 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-02-02 08:45:35 -05:00
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-10-04 18:03:20 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-10-04 18:03:20 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-10-04 18:03:20 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "rawstr.h"
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_convert.h"
|
|
|
|
#include "tool_operhlp.h"
|
2013-02-14 05:41:57 -05:00
|
|
|
#include "tool_metalink.h"
|
2012-10-31 05:43:36 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-10-04 18:03:20 -04:00
|
|
|
|
2014-02-23 07:59:59 -05:00
|
|
|
void clean_getout(struct OperationConfig *config)
|
2011-10-04 18:03:20 -04:00
|
|
|
{
|
|
|
|
struct getout *next;
|
|
|
|
struct getout *node = config->url_list;
|
|
|
|
|
|
|
|
while(node) {
|
|
|
|
next = node->next;
|
|
|
|
Curl_safefree(node->url);
|
|
|
|
Curl_safefree(node->outfile);
|
|
|
|
Curl_safefree(node->infile);
|
|
|
|
Curl_safefree(node);
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
config->url_list = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool output_expected(const char *url, const char *uploadfile)
|
|
|
|
{
|
|
|
|
if(!uploadfile)
|
|
|
|
return TRUE; /* download */
|
|
|
|
if(checkprefix("http://", url) || checkprefix("https://", url))
|
|
|
|
return TRUE; /* HTTP(S) upload */
|
|
|
|
|
|
|
|
return FALSE; /* non-HTTP upload, probably no output should be expected */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stdin_upload(const char *uploadfile)
|
|
|
|
{
|
|
|
|
return (curlx_strequal(uploadfile, "-") ||
|
|
|
|
curlx_strequal(uploadfile, ".")) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds the file name to the URL if it doesn't already have one.
|
|
|
|
* url will be freed before return if the returned pointer is different
|
|
|
|
*/
|
|
|
|
char *add_file_name_to_url(CURL *curl, char *url, const char *filename)
|
|
|
|
{
|
|
|
|
/* If no file name part is given in the URL, we add this file name */
|
|
|
|
char *ptr = strstr(url, "://");
|
|
|
|
if(ptr)
|
|
|
|
ptr += 3;
|
|
|
|
else
|
|
|
|
ptr = url;
|
|
|
|
ptr = strrchr(ptr, '/');
|
|
|
|
if(!ptr || !strlen(++ptr)) {
|
|
|
|
/* The URL has no file name part, add the local file name. In order
|
|
|
|
to be able to do so, we have to create a new URL in another
|
|
|
|
buffer.*/
|
|
|
|
|
|
|
|
/* We only want the part of the local path that is on the right
|
|
|
|
side of the rightmost slash and backslash. */
|
|
|
|
const char *filep = strrchr(filename, '/');
|
|
|
|
char *file2 = strrchr(filep?filep:filename, '\\');
|
|
|
|
char *encfile;
|
|
|
|
|
|
|
|
if(file2)
|
|
|
|
filep = file2 + 1;
|
|
|
|
else if(filep)
|
|
|
|
filep++;
|
|
|
|
else
|
|
|
|
filep = filename;
|
|
|
|
|
|
|
|
/* URL encode the file name */
|
|
|
|
encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
|
|
|
|
if(encfile) {
|
2013-03-06 07:27:51 -05:00
|
|
|
char *urlbuffer;
|
2011-10-04 18:03:20 -04:00
|
|
|
if(ptr)
|
|
|
|
/* there is a trailing slash on the URL */
|
2013-03-06 07:27:51 -05:00
|
|
|
urlbuffer = aprintf("%s%s", url, encfile);
|
2011-10-04 18:03:20 -04:00
|
|
|
else
|
|
|
|
/* there is no trailing slash on the URL */
|
2013-03-06 07:27:51 -05:00
|
|
|
urlbuffer = aprintf("%s/%s", url, encfile);
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
curl_free(encfile);
|
|
|
|
Curl_safefree(url);
|
2011-10-05 10:41:04 -04:00
|
|
|
|
2013-03-06 07:27:51 -05:00
|
|
|
if(!urlbuffer)
|
|
|
|
return NULL;
|
|
|
|
|
2011-10-04 18:03:20 -04:00
|
|
|
url = urlbuffer; /* use our new URL instead! */
|
|
|
|
}
|
2013-07-29 12:23:38 -04:00
|
|
|
else
|
|
|
|
Curl_safefree(url);
|
2011-10-04 18:03:20 -04:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extracts the name portion of the URL.
|
2011-10-05 09:06:26 -04:00
|
|
|
* Returns a pointer to a heap-allocated string or NULL if
|
|
|
|
* no name part, at location indicated by first argument.
|
2011-10-04 18:03:20 -04:00
|
|
|
*/
|
2011-10-05 09:06:26 -04:00
|
|
|
CURLcode get_url_file_name(char **filename, const char *url)
|
2011-10-04 18:03:20 -04:00
|
|
|
{
|
2011-10-05 09:06:26 -04:00
|
|
|
const char *pc;
|
|
|
|
|
|
|
|
*filename = NULL;
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
/* Find and get the remote file name */
|
2011-10-05 09:06:26 -04:00
|
|
|
pc = strstr(url, "://");
|
2011-10-04 18:03:20 -04:00
|
|
|
if(pc)
|
|
|
|
pc += 3;
|
|
|
|
else
|
|
|
|
pc = url;
|
|
|
|
pc = strrchr(pc, '/');
|
|
|
|
|
2014-10-09 10:42:17 -04:00
|
|
|
if(pc)
|
2011-10-04 18:03:20 -04:00
|
|
|
/* duplicate the string beyond the slash */
|
|
|
|
pc++;
|
2014-10-09 10:42:17 -04:00
|
|
|
else
|
|
|
|
/* no slash => empty string */
|
|
|
|
pc = "";
|
|
|
|
|
|
|
|
*filename = strdup(pc);
|
|
|
|
if(!*filename)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-11-20 17:33:46 -05:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
|
|
|
|
Curl_safefree(*filename);
|
|
|
|
*filename = strdup(buffer); /* clone the buffer */
|
|
|
|
curl_free(tdir);
|
2014-12-07 05:08:10 -05:00
|
|
|
if(!*filename)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-11-20 17:33:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-05 09:06:26 -04:00
|
|
|
return CURLE_OK;
|
2011-10-04 18:03:20 -04:00
|
|
|
}
|