"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/tool_operate.c" between
curl-7.65.3.tar.xz and curl-7.66.0.tar.xz

About: cURL is a powerful commandline tool to transfer data from or to a server, using one of the supported protocols HTTP(S), FTP(S), SCP, SFTP, TFTP, DICT, TELNET, LDAP(S) or FILE.

tool_operate.c  (curl-7.65.3.tar.xz):tool_operate.c  (curl-7.66.0.tar.xz)
skipping to change at line 32 skipping to change at line 32
#include "tool_setup.h" #include "tool_setup.h"
#ifdef HAVE_FCNTL_H #ifdef HAVE_FCNTL_H
# include <fcntl.h> # include <fcntl.h>
#endif #endif
#ifdef HAVE_LOCALE_H #ifdef HAVE_LOCALE_H
# include <locale.h> # include <locale.h>
#endif #endif
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#ifdef __VMS #ifdef __VMS
# include <fabdef.h> # include <fabdef.h>
#endif #endif
#ifdef __AMIGA__ #ifdef __AMIGA__
# include <proto/dos.h> # include <proto/dos.h>
#endif #endif
#include "strcase.h" #include "strcase.h"
skipping to change at line 78 skipping to change at line 82
#include "tool_parsecfg.h" #include "tool_parsecfg.h"
#include "tool_setopt.h" #include "tool_setopt.h"
#include "tool_sleep.h" #include "tool_sleep.h"
#include "tool_urlglob.h" #include "tool_urlglob.h"
#include "tool_util.h" #include "tool_util.h"
#include "tool_writeout.h" #include "tool_writeout.h"
#include "tool_xattr.h" #include "tool_xattr.h"
#include "tool_vms.h" #include "tool_vms.h"
#include "tool_help.h" #include "tool_help.h"
#include "tool_hugehelp.h" #include "tool_hugehelp.h"
#include "tool_progress.h"
#include "memdebug.h" /* keep this as LAST include */ #include "memdebug.h" /* keep this as LAST include */
#ifdef CURLDEBUG #ifdef CURLDEBUG
/* libcurl's debug builds provide an extra function */ /* libcurl's debug builds provide an extra function */
CURLcode curl_easy_perform_ev(CURL *easy); CURLcode curl_easy_perform_ev(CURL *easy);
#endif #endif
#define CURLseparator "--_curl_--" #define CURLseparator "--_curl_--"
skipping to change at line 101 skipping to change at line 106
# define O_BINARY 0 # define O_BINARY 0
#endif #endif
#define CURL_CA_CERT_ERRORMSG \ #define CURL_CA_CERT_ERRORMSG \
"More details here: https://curl.haxx.se/docs/sslcerts.html\n\n" \ "More details here: https://curl.haxx.se/docs/sslcerts.html\n\n" \
"curl failed to verify the legitimacy of the server and therefore " \ "curl failed to verify the legitimacy of the server and therefore " \
"could not\nestablish a secure connection to it. To learn more about " \ "could not\nestablish a secure connection to it. To learn more about " \
"this situation and\nhow to fix it, please visit the web page mentioned " \ "this situation and\nhow to fix it, please visit the web page mentioned " \
"above.\n" "above.\n"
static CURLcode create_transfers(struct GlobalConfig *global,
struct OperationConfig *config,
CURLSH *share,
bool capath_from_env);
static bool is_fatal_error(CURLcode code) static bool is_fatal_error(CURLcode code)
{ {
switch(code) { switch(code) {
case CURLE_FAILED_INIT: case CURLE_FAILED_INIT:
case CURLE_OUT_OF_MEMORY: case CURLE_OUT_OF_MEMORY:
case CURLE_UNKNOWN_OPTION: case CURLE_UNKNOWN_OPTION:
case CURLE_FUNCTION_NOT_FOUND: case CURLE_FUNCTION_NOT_FOUND:
case CURLE_BAD_FUNCTION_ARGUMENT: case CURLE_BAD_FUNCTION_ARGUMENT:
/* critical error */ /* critical error */
return TRUE; return TRUE;
skipping to change at line 190 skipping to change at line 200
return vms_realfilesize(name, stat_buf); return vms_realfilesize(name, stat_buf);
break; break;
default: default:
return stat_buf->st_size; return stat_buf->st_size;
} }
} }
#endif /* __VMS */ #endif /* __VMS */
#define BUFFER_SIZE (100*1024) #define BUFFER_SIZE (100*1024)
static CURLcode operate_do(struct GlobalConfig *global, struct per_transfer *transfers; /* first node */
struct OperationConfig *config) static struct per_transfer *transfersl; /* last node */
static CURLcode add_transfer(struct per_transfer **per)
{ {
char errorbuffer[CURL_ERROR_SIZE]; struct per_transfer *p;
struct ProgressData progressbar; p = calloc(sizeof(struct per_transfer), 1);
struct getout *urlnode; if(!p)
return CURLE_OUT_OF_MEMORY;
if(!transfers)
/* first entry */
transfersl = transfers = p;
else {
/* make the last node point to the new node */
transfersl->next = p;
/* make the new node point back to the formerly last node */
p->prev = transfersl;
/* move the last node pointer to the new entry */
transfersl = p;
}
*per = p;
all_xfers++; /* count total number of transfers added */
return CURLE_OK;
}
struct HdrCbData hdrcbdata; /* Remove the specified transfer from the list (and free it), return the next
struct OutStruct heads; in line */
static struct per_transfer *del_transfer(struct per_transfer *per)
{
struct per_transfer *n;
struct per_transfer *p;
DEBUGASSERT(transfers);
DEBUGASSERT(transfersl);
DEBUGASSERT(per);
n = per->next;
p = per->prev;
if(p)
p->next = n;
else
transfers = n;
if(n)
n->prev = p;
else
transfersl = p;
metalinkfile *mlfile_last = NULL; free(per);
CURL *curl = config->easy; return n;
char *httpgetfields = NULL; }
static CURLcode pre_transfer(struct GlobalConfig *global,
struct per_transfer *per)
{
curl_off_t uploadfilesize = -1;
struct_stat fileinfo;
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
unsigned long li;
bool capath_from_env;
/* Save the values of noprogress and isatty to restore them later on */ if(per->separator_err)
bool orig_noprogress = global->noprogress; fprintf(global->errors, "%s\n", per->separator_err);
bool orig_isatty = global->isatty; if(per->separator)
printf("%s\n", per->separator);
if(per->uploadfile && !stdin_upload(per->uploadfile)) {
/* VMS Note:
*
* Reading binary from files can be a problem... Only FIXED, VAR
* etc WITHOUT implied CC will work Others need a \n appended to a
* line
*
* - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
* fixed file with implied CC needs to have a byte added for every
* record processed, this can by derived from Filesize & recordsize
* for VARiable record files the records need to be counted! for
* every record add 1 for linefeed and subtract 2 for the record
* header for VARIABLE header files only the bare record data needs
* to be considered with one appended if implied CC
*/
#ifdef __VMS
/* Calculate the real upload size for VMS */
per->infd = -1;
if(stat(per->uploadfile, &fileinfo) == 0) {
fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
switch(fileinfo.st_fab_rfm) {
case FAB$C_VAR:
case FAB$C_VFC:
case FAB$C_STMCR:
per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
break;
default:
per->infd = open(per->uploadfile, O_RDONLY | O_BINARY,
"rfm=stmlf", "ctx=stm");
}
}
if(per->infd == -1)
#else
per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
if((per->infd == -1) || fstat(per->infd, &fileinfo))
#endif
{
helpf(global->errors, "Can't open '%s'!\n", per->uploadfile);
if(per->infd != -1) {
close(per->infd);
per->infd = STDIN_FILENO;
}
return CURLE_READ_ERROR;
}
per->infdopen = TRUE;
/* we ignore file size for char/block devices, sockets, etc. */
if(S_ISREG(fileinfo.st_mode))
uploadfilesize = fileinfo.st_size;
if(uploadfilesize != -1)
my_setopt(per->curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
per->input.fd = per->infd;
}
show_error:
return result;
}
/*
* Call this after a transfer has completed.
*/
static CURLcode post_transfer(struct GlobalConfig *global,
CURLSH *share,
struct per_transfer *per,
CURLcode result,
bool *retryp)
{
struct OutStruct *outs = &per->outs;
CURL *curl = per->curl;
struct OperationConfig *config = per->config;
errorbuffer[0] = '\0'; *retryp = FALSE;
/* default headers output stream is stdout */ if(per->infdopen)
memset(&hdrcbdata, 0, sizeof(struct HdrCbData)); close(per->infd);
memset(&heads, 0, sizeof(struct OutStruct));
heads.stream = stdout;
heads.config = config;
/*
** Beyond this point no return'ing from this function allowed.
** Jump to label 'quit_curl' in order to abandon this function
** from outside of nested loops further down below.
*/
/* Check we have a url */ #ifdef __VMS
if(!config->url_list || !config->url_list->url) { if(is_vms_shell()) {
helpf(global->errors, "no URL specified!\n"); /* VMS DCL shell behavior */
result = CURLE_FAILED_INIT; if(!global->showerror)
goto quit_curl; vms_show = VMSSTS_HIDE;
} }
else
#endif
if(config->synthetic_error) {
;
}
else if(result && global->showerror) {
fprintf(global->errors, "curl: (%d) %s\n", result,
(per->errorbuffer[0]) ? per->errorbuffer :
curl_easy_strerror(result));
if(result == CURLE_PEER_FAILED_VERIFICATION)
fputs(CURL_CA_CERT_ERRORMSG, global->errors);
}
/* On WIN32 we can't set the path to curl-ca-bundle.crt /* Set file extended attributes */
* at compile time. So we look here for the file in two ways: if(!result && config->xattr && outs->fopened && outs->stream) {
* 1: look at the environment variable CURL_CA_BUNDLE for a path int rc = fwrite_xattr(curl, fileno(outs->stream));
* 2: if #1 isn't found, use the windows API function SearchPath() if(rc)
* to find it along the app's path (includes app's dir and CWD) warnf(config->global, "Error setting extended attributes: %s\n",
* strerror(errno));
* We support the environment variable thing for non-Windows platforms }
* too. Just for the sake of it.
*/
capath_from_env = false;
if(!config->cacert &&
!config->capath &&
!config->insecure_ok) {
struct curl_tlssessioninfo *tls_backend_info = NULL;
/* With the addition of CAINFO support for Schannel, this search could find if(!result && !outs->stream && !outs->bytes) {
* a certificate bundle that was previously ignored. To maintain backward /* we have received no data despite the transfer was successful
* compatibility, only perform this search if not using Schannel. ==> force cration of an empty output file (if an output file
*/ was specified) */
result = curl_easy_getinfo(config->easy, long cond_unmet = 0L;
CURLINFO_TLS_SSL_PTR, /* do not create (or even overwrite) the file in case we get no
&tls_backend_info); data because of unmet condition */
if(result) { curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &cond_unmet);
goto quit_curl; if(!cond_unmet && !tool_create_output_file(outs))
result = CURLE_WRITE_ERROR;
}
if(!outs->s_isreg && outs->stream) {
/* Dump standard stream buffered data */
int rc = fflush(outs->stream);
if(!result && rc) {
/* something went wrong in the writing process */
result = CURLE_WRITE_ERROR;
fprintf(global->errors, "(%d) Failed writing body\n", result);
} }
}
/* Set the CA cert locations specified in the environment. For Windows if #ifdef USE_METALINK
* no environment-specified filename is found then check for CA bundle if(per->metalink && !per->metalink_next_res)
* default filename curl-ca-bundle.crt in the user's PATH. fprintf(global->errors, "Metalink: fetching (%s) from (%s) OK\n",
* per->mlfile->filename, per->this_url);
* If Schannel is the selected SSL backend then these locations are
* ignored. We allow setting CA location for schannel only when explicitly if(!per->metalink && config->use_metalink && result == CURLE_OK) {
* specified by the user via CURLOPT_CAINFO / --cacert. int rv = parse_metalink(config, outs, per->this_url);
*/ if(!rv) {
if(tls_backend_info->backend != CURLSSLBACKEND_SCHANNEL) { fprintf(config->global->errors, "Metalink: parsing (%s) OK\n",
char *env; per->this_url);
env = curlx_getenv("CURL_CA_BUNDLE"); }
if(env) { else if(rv == -1)
config->cacert = strdup(env); fprintf(config->global->errors, "Metalink: parsing (%s) FAILED\n",
if(!config->cacert) { per->this_url);
curl_free(env); result = create_transfers(global, config, share, FALSE);
helpf(global->errors, "out of memory\n"); }
result = CURLE_OUT_OF_MEMORY; else if(per->metalink && result == CURLE_OK && !per->metalink_next_res) {
goto quit_curl; int rv;
(void)fflush(outs->stream);
rv = metalink_check_hash(global, per->mlfile, outs->filename);
if(!rv)
per->metalink_next_res = 1;
}
#else
(void)share;
#endif /* USE_METALINK */
#ifdef USE_METALINK
if(outs->metalink_parser)
metalink_parser_context_delete(outs->metalink_parser);
#endif /* USE_METALINK */
if(outs->is_cd_filename && outs->stream && !global->mute &&
outs->filename)
printf("curl: Saved to filename '%s'\n", outs->filename);
/* if retry-max-time is non-zero, make sure we haven't exceeded the
time */
if(per->retry_numretries &&
(!config->retry_maxtime ||
(tvdiff(tvnow(), per->retrystart) <
config->retry_maxtime*1000L)) ) {
enum {
RETRY_NO,
RETRY_TIMEOUT,
RETRY_CONNREFUSED,
RETRY_HTTP,
RETRY_FTP,
RETRY_LAST /* not used */
} retry = RETRY_NO;
long response;
if((CURLE_OPERATION_TIMEDOUT == result) ||
(CURLE_COULDNT_RESOLVE_HOST == result) ||
(CURLE_COULDNT_RESOLVE_PROXY == result) ||
(CURLE_FTP_ACCEPT_TIMEOUT == result))
/* retry timeout always */
retry = RETRY_TIMEOUT;
else if(config->retry_connrefused &&
(CURLE_COULDNT_CONNECT == result)) {
long oserrno;
curl_easy_getinfo(curl, CURLINFO_OS_ERRNO, &oserrno);
if(ECONNREFUSED == oserrno)
retry = RETRY_CONNREFUSED;
}
else if((CURLE_OK == result) ||
(config->failonerror &&
(CURLE_HTTP_RETURNED_ERROR == result))) {
/* If it returned OK. _or_ failonerror was enabled and it
returned due to such an error, check for HTTP transient
errors to retry on. */
long protocol;
curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
if((protocol == CURLPROTO_HTTP) || (protocol == CURLPROTO_HTTPS)) {
/* This was HTTP(S) */
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
switch(response) {
case 500: /* Internal Server Error */
case 502: /* Bad Gateway */
case 503: /* Service Unavailable */
case 504: /* Gateway Timeout */
retry = RETRY_HTTP;
/*
* At this point, we have already written data to the output
* file (or terminal). If we write to a file, we must rewind
* or close/re-open the file so that the next attempt starts
* over from the beginning.
*
* TODO: similar action for the upload case. We might need
* to start over reading from a previous point if we have
* uploaded something when this was returned.
*/
break;
} }
} }
else { } /* if CURLE_OK */
env = curlx_getenv("SSL_CERT_DIR"); else if(result) {
if(env) { long protocol;
config->capath = strdup(env);
if(!config->capath) { curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
curl_free(env); curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
helpf(global->errors, "out of memory\n");
result = CURLE_OUT_OF_MEMORY; if((protocol == CURLPROTO_FTP || protocol == CURLPROTO_FTPS) &&
goto quit_curl; response / 100 == 4)
} /*
capath_from_env = true; * This is typically when the FTP server only allows a certain
} * amount of users and we are not one of them. All 4xx codes
else { * are transient.
env = curlx_getenv("SSL_CERT_FILE"); */
if(env) { retry = RETRY_FTP;
config->cacert = strdup(env); }
if(!config->cacert) {
curl_free(env); if(retry) {
helpf(global->errors, "out of memory\n"); long sleeptime = 0;
result = CURLE_OUT_OF_MEMORY; curl_off_t retry_after = 0;
goto quit_curl; static const char * const m[]={
} NULL,
} "timeout",
"connection refused",
"HTTP error",
"FTP error"
};
sleeptime = per->retry_sleep;
if(RETRY_HTTP == retry) {
curl_easy_getinfo(curl, CURLINFO_RETRY_AFTER, &retry_after);
if(retry_after) {
/* store in a 'long', make sure it doesn't overflow */
if(retry_after > LONG_MAX/1000)
sleeptime = LONG_MAX;
else
sleeptime = (long)retry_after * 1000; /* milliseconds */
} }
} }
warnf(config->global, "Transient problem: %s "
if(env) "Will retry in %ld seconds. "
curl_free(env); "%ld retries left.\n",
#ifdef WIN32 m[retry], per->retry_sleep/1000L, per->retry_numretries);
else {
result = FindWin32CACert(config, tls_backend_info->backend, per->retry_numretries--;
"curl-ca-bundle.crt"); tool_go_sleep(sleeptime);
if(result) if(!config->retry_delay) {
goto quit_curl; per->retry_sleep *= 2;
if(per->retry_sleep > RETRY_SLEEP_MAX)
per->retry_sleep = RETRY_SLEEP_MAX;
} }
if(outs->bytes && outs->filename && outs->stream) {
int rc;
/* We have written data to a output file, we truncate file
*/
if(!global->mute)
fprintf(global->errors, "Throwing away %"
CURL_FORMAT_CURL_OFF_T " bytes\n",
outs->bytes);
fflush(outs->stream);
/* truncate file at the position where we started appending */
#ifdef HAVE_FTRUNCATE
if(ftruncate(fileno(outs->stream), outs->init)) {
/* when truncate fails, we can't just append as then we'll
create something strange, bail out */
if(!global->mute)
fprintf(global->errors,
"failed to truncate, exiting\n");
return CURLE_WRITE_ERROR;
}
/* now seek to the end of the file, the position where we
just truncated the file in a large file-safe way */
rc = fseek(outs->stream, 0, SEEK_END);
#else
/* ftruncate is not available, so just reposition the file
to the location we would have truncated it. This won't
work properly with large files on 32-bit systems, but
most of those will have ftruncate. */
rc = fseek(outs->stream, (long)outs->init, SEEK_SET);
#endif #endif
if(rc) {
if(!global->mute)
fprintf(global->errors,
"failed seeking to end of file, exiting\n");
return CURLE_WRITE_ERROR;
}
outs->bytes = 0; /* clear for next round */
}
*retryp = TRUE; /* curl_easy_perform loop */
return CURLE_OK;
}
} /* if retry_numretries */
else if(per->metalink) {
/* Metalink: Decide to try the next resource or not. Try the next resource
if download was not successful. */
long response;
if(CURLE_OK == result) {
/* TODO We want to try next resource when download was
not successful. How to know that? */
char *effective_url = NULL;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
if(effective_url &&
curl_strnequal(effective_url, "http", 4)) {
/* This was HTTP(S) */
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
if(response != 200 && response != 206) {
per->metalink_next_res = 1;
fprintf(global->errors,
"Metalink: fetching (%s) from (%s) FAILED "
"(HTTP status code %ld)\n",
per->mlfile->filename, per->this_url, response);
}
}
}
else {
per->metalink_next_res = 1;
fprintf(global->errors,
"Metalink: fetching (%s) from (%s) FAILED (%s)\n",
per->mlfile->filename, per->this_url,
curl_easy_strerror(result));
} }
} }
if((global->progressmode == CURL_PROGRESS_BAR) &&
per->progressbar.calls)
/* if the custom progress bar has been displayed, we output a
newline here */
fputs("\n", per->progressbar.out);
if(config->writeout)
ourWriteOut(per->curl, &per->outs, config->writeout);
/* Close the outs file */
if(outs->fopened && outs->stream) {
int rc = fclose(outs->stream);
if(!result && rc) {
/* something went wrong in the writing process */
result = CURLE_WRITE_ERROR;
fprintf(global->errors, "(%d) Failed writing body\n", result);
}
}
/* File time can only be set _after_ the file has been closed */
if(!result && config->remote_time && outs->s_isreg && outs->filename) {
/* Ask libcurl if we got a remote file time */
curl_off_t filetime = -1;
curl_easy_getinfo(curl, CURLINFO_FILETIME_T, &filetime);
setfiletime(filetime, outs->filename, config->global->errors);
}
/* Close function-local opened file descriptors */
if(per->heads.fopened && per->heads.stream)
fclose(per->heads.stream);
if(per->heads.alloc_filename)
Curl_safefree(per->heads.filename);
curl_easy_cleanup(per->curl);
if(outs->alloc_filename)
free(outs->filename);
free(per->this_url);
free(per->separator_err);
free(per->separator);
free(per->outfile);
free(per->uploadfile);
return CURLE_OK;
}
/* go through the list of URLs and configs and add transfers */
static CURLcode create_transfers(struct GlobalConfig *global,
struct OperationConfig *config,
CURLSH *share,
bool capath_from_env)
{
CURLcode result = CURLE_OK;
struct getout *urlnode;
metalinkfile *mlfile_last = NULL;
bool orig_noprogress = global->noprogress;
bool orig_isatty = global->isatty;
char *httpgetfields = NULL;
if(config->postfields) { if(config->postfields) {
if(config->use_httpget) { if(config->use_httpget) {
/* Use the postfields data for a http get */ /* Use the postfields data for a http get */
httpgetfields = strdup(config->postfields); httpgetfields = strdup(config->postfields);
Curl_safefree(config->postfields); Curl_safefree(config->postfields);
if(!httpgetfields) { if(!httpgetfields) {
helpf(global->errors, "out of memory\n"); helpf(global->errors, "out of memory\n");
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
goto quit_curl; goto quit_curl;
} }
skipping to change at line 344 skipping to change at line 693
} }
} }
else { else {
if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq)) { if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq)) {
result = CURLE_FAILED_INIT; result = CURLE_FAILED_INIT;
goto quit_curl; goto quit_curl;
} }
} }
} }
/* Single header file for all URLs */
if(config->headerfile) {
/* open file for output: */
if(strcmp(config->headerfile, "-")) {
FILE *newfile = fopen(config->headerfile, "wb");
if(!newfile) {
warnf(config->global, "Failed to open %s\n", config->headerfile);
result = CURLE_WRITE_ERROR;
goto quit_curl;
}
else {
heads.filename = config->headerfile;
heads.s_isreg = TRUE;
heads.fopened = TRUE;
heads.stream = newfile;
}
}
else {
/* always use binary mode for protocol header output */
set_binmode(heads.stream);
}
}
/*
** Nested loops start here.
*/
/* loop through the list of given URLs */
for(urlnode = config->url_list; urlnode; urlnode = urlnode->next) { for(urlnode = config->url_list; urlnode; urlnode = urlnode->next) {
unsigned long li;
unsigned long up; /* upload file counter within a single upload glob */ unsigned long up; /* upload file counter within a single upload glob */
char *infiles; /* might be a glob pattern */ char *infiles; /* might be a glob pattern */
char *outfiles; char *outfiles;
unsigned long infilenum; unsigned long infilenum;
URLGlob *inglob; URLGlob *inglob;
bool metalink = FALSE; /* metalink download? */
int metalink = 0; /* nonzero for metalink download. */
metalinkfile *mlfile; metalinkfile *mlfile;
metalink_resource *mlres; metalink_resource *mlres;
outfiles = NULL; outfiles = NULL;
infilenum = 1; infilenum = 1;
inglob = NULL; inglob = NULL;
if(urlnode->flags & GETOUT_METALINK) { if(urlnode->flags & GETOUT_METALINK) {
metalink = 1; metalink = 1;
if(mlfile_last == NULL) { if(mlfile_last == NULL) {
skipping to change at line 464 skipping to change at line 783
if(result == CURLE_OUT_OF_MEMORY) if(result == CURLE_OUT_OF_MEMORY)
helpf(global->errors, "out of memory\n"); helpf(global->errors, "out of memory\n");
} }
else if(!up) { else if(!up) {
uploadfile = strdup(infiles); uploadfile = strdup(infiles);
if(!uploadfile) { if(!uploadfile) {
helpf(global->errors, "out of memory\n"); helpf(global->errors, "out of memory\n");
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
} }
} }
else
uploadfile = NULL;
if(!uploadfile) if(!uploadfile)
break; break;
} }
if(metalink) { if(metalink) {
/* For Metalink download, we don't use glob. Instead we use /* For Metalink download, we don't use glob. Instead we use
the number of resources as urlnum. */ the number of resources as urlnum. */
urlnum = count_next_metalink_resource(mlfile); urlnum = count_next_metalink_resource(mlfile);
} }
else if(!config->globoff) { else if(!config->globoff) {
skipping to change at line 493 skipping to change at line 810
} }
} }
else else
urlnum = 1; /* without globbing, this is a single URL */ urlnum = 1; /* without globbing, this is a single URL */
/* if multiple files extracted to stdout, insert separators! */ /* if multiple files extracted to stdout, insert separators! */
separator = ((!outfiles || !strcmp(outfiles, "-")) && urlnum > 1); separator = ((!outfiles || !strcmp(outfiles, "-")) && urlnum > 1);
/* Here's looping around each globbed URL */ /* Here's looping around each globbed URL */
for(li = 0 ; li < urlnum; li++) { for(li = 0 ; li < urlnum; li++) {
struct per_transfer *per;
struct OutStruct *outs;
struct InStruct *input;
struct OutStruct *heads;
struct HdrCbData *hdrcbdata = NULL;
CURL *curl = curl_easy_init();
result = add_transfer(&per);
if(result || !curl) {
free(uploadfile);
curl_easy_cleanup(curl);
result = CURLE_OUT_OF_MEMORY;
goto show_error;
}
per->config = config;
per->curl = curl;
per->uploadfile = uploadfile;
/* default headers output stream is stdout */
heads = &per->heads;
heads->stream = stdout;
heads->config = config;
/* Single header file for all URLs */
if(config->headerfile) {
/* open file for output: */
if(strcmp(config->headerfile, "-")) {
FILE *newfile = fopen(config->headerfile, "wb");
if(!newfile) {
warnf(config->global, "Failed to open %s\n", config->headerfile);
result = CURLE_WRITE_ERROR;
goto quit_curl;
}
else {
heads->filename = config->headerfile;
heads->s_isreg = TRUE;
heads->fopened = TRUE;
heads->stream = newfile;
}
}
else {
/* always use binary mode for protocol header output */
set_binmode(heads->stream);
}
}
hdrcbdata = &per->hdrcbdata;
int infd; outs = &per->outs;
bool infdopen; input = &per->input;
char *outfile;
struct OutStruct outs; per->outfile = NULL;
struct InStruct input; per->infdopen = FALSE;
struct timeval retrystart; per->infd = STDIN_FILENO;
curl_off_t uploadfilesize;
long retry_numretries;
long retry_sleep_default;
long retry_sleep;
char *this_url = NULL;
int metalink_next_res = 0;
outfile = NULL;
infdopen = FALSE;
infd = STDIN_FILENO;
uploadfilesize = -1; /* -1 means unknown */
/* default output stream is stdout */ /* default output stream is stdout */
memset(&outs, 0, sizeof(struct OutStruct)); outs->stream = stdout;
outs.stream = stdout; outs->config = config;
outs.config = config;
if(metalink) { if(metalink) {
/* For Metalink download, use name in Metalink file as /* For Metalink download, use name in Metalink file as
filename. */ filename. */
outfile = strdup(mlfile->filename); per->outfile = strdup(mlfile->filename);
if(!outfile) { if(!per->outfile) {
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
goto show_error; goto show_error;
} }
this_url = strdup(mlres->url); per->this_url = strdup(mlres->url);
if(!this_url) { if(!per->this_url) {
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
goto show_error; goto show_error;
} }
per->mlfile = mlfile;
} }
else { else {
if(urls) { if(urls) {
result = glob_next_url(&this_url, urls); result = glob_next_url(&per->this_url, urls);
if(result) if(result)
goto show_error; goto show_error;
} }
else if(!li) { else if(!li) {
this_url = strdup(urlnode->url); per->this_url = strdup(urlnode->url);
if(!this_url) { if(!per->this_url) {
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
goto show_error; goto show_error;
} }
} }
else else
this_url = NULL; per->this_url = NULL;
if(!this_url) if(!per->this_url)
break; break;
if(outfiles) { if(outfiles) {
outfile = strdup(outfiles); per->outfile = strdup(outfiles);
if(!outfile) { if(!per->outfile) {
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
goto show_error; goto show_error;
} }
} }
} }
if(((urlnode->flags&GETOUT_USEREMOTE) || if(((urlnode->flags&GETOUT_USEREMOTE) ||
(outfile && strcmp("-", outfile))) && (per->outfile && strcmp("-", per->outfile))) &&
(metalink || !config->use_metalink)) { (metalink || !config->use_metalink)) {
/* /*
* We have specified a file name to store the result in, or we have * We have specified a file name to store the result in, or we have
* decided we want to use the remote file name. * decided we want to use the remote file name.
*/ */
if(!outfile) { if(!per->outfile) {
/* extract the file name from the URL */ /* extract the file name from the URL */
result = get_url_file_name(&outfile, this_url); result = get_url_file_name(&per->outfile, per->this_url);
if(result) if(result)
goto show_error; goto show_error;
if(!*outfile && !config->content_disposition) { if(!*per->outfile && !config->content_disposition) {
helpf(global->errors, "Remote file name has no length!\n"); helpf(global->errors, "Remote file name has no length!\n");
result = CURLE_WRITE_ERROR; result = CURLE_WRITE_ERROR;
goto quit_urls; goto quit_urls;
} }
} }
else if(urls) { else if(urls) {
/* fill '#1' ... '#9' terms from URL pattern */ /* fill '#1' ... '#9' terms from URL pattern */
char *storefile = outfile; char *storefile = per->outfile;
result = glob_match_url(&outfile, storefile, urls); result = glob_match_url(&per->outfile, storefile, urls);
Curl_safefree(storefile); Curl_safefree(storefile);
if(result) { if(result) {
/* bad globbing */ /* bad globbing */
warnf(config->global, "bad output glob!\n"); warnf(config->global, "bad output glob!\n");
goto quit_urls; goto quit_urls;
} }
} }
/* Create the directory hierarchy, if not pre-existent to a multiple /* Create the directory hierarchy, if not pre-existent to a multiple
file output call */ file output call */
if(config->create_dirs || metalink) { if(config->create_dirs || metalink) {
result = create_dir_hierarchy(outfile, global->errors); result = create_dir_hierarchy(per->outfile, global->errors);
/* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */ /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
if(result == CURLE_WRITE_ERROR) if(result == CURLE_WRITE_ERROR)
goto quit_urls; goto quit_urls;
if(result) { if(result) {
goto show_error; goto show_error;
} }
} }
if((urlnode->flags & GETOUT_USEREMOTE) if((urlnode->flags & GETOUT_USEREMOTE)
&& config->content_disposition) { && config->content_disposition) {
/* Our header callback MIGHT set the filename */ /* Our header callback MIGHT set the filename */
DEBUGASSERT(!outs.filename); DEBUGASSERT(!outs->filename);
} }
if(config->resume_from_current) { if(config->resume_from_current) {
/* We're told to continue from where we are now. Get the size /* We're told to continue from where we are now. Get the size
of the file as it is now and open it for append instead */ of the file as it is now and open it for append instead */
struct_stat fileinfo; struct_stat fileinfo;
/* VMS -- Danger, the filesize is only valid for stream files */ /* VMS -- Danger, the filesize is only valid for stream files */
if(0 == stat(outfile, &fileinfo)) if(0 == stat(per->outfile, &fileinfo))
/* set offset to current file size: */ /* set offset to current file size: */
config->resume_from = fileinfo.st_size; config->resume_from = fileinfo.st_size;
else else
/* let offset be 0 */ /* let offset be 0 */
config->resume_from = 0; config->resume_from = 0;
} }
if(config->resume_from) { if(config->resume_from) {
#ifdef __VMS #ifdef __VMS
/* open file for output, forcing VMS output format into stream /* open file for output, forcing VMS output format into stream
mode which is needed for stat() call above to always work. */ mode which is needed for stat() call above to always work. */
FILE *file = fopen(outfile, config->resume_from?"ab":"wb", FILE *file = fopen(outfile, config->resume_from?"ab":"wb",
"ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0"); "ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0");
#else #else
/* open file for output: */ /* open file for output: */
FILE *file = fopen(outfile, config->resume_from?"ab":"wb"); FILE *file = fopen(per->outfile, config->resume_from?"ab":"wb");
#endif #endif
if(!file) { if(!file) {
helpf(global->errors, "Can't open '%s'!\n", outfile); helpf(global->errors, "Can't open '%s'!\n", per->outfile);
result = CURLE_WRITE_ERROR; result = CURLE_WRITE_ERROR;
goto quit_urls; goto quit_urls;
} }
outs.fopened = TRUE; outs->fopened = TRUE;
outs.stream = file; outs->stream = file;
outs.init = config->resume_from; outs->init = config->resume_from;
} }
else { else {
outs.stream = NULL; /* open when needed */ outs->stream = NULL; /* open when needed */
} }
outs.filename = outfile; outs->filename = per->outfile;
outs.s_isreg = TRUE; outs->s_isreg = TRUE;
} }
if(uploadfile && !stdin_upload(uploadfile)) { if(per->uploadfile && !stdin_upload(per->uploadfile)) {
/* /*
* We have specified a file to upload and it isn't "-". * We have specified a file to upload and it isn't "-".
*/ */
struct_stat fileinfo; char *nurl = add_file_name_to_url(per->this_url, per->uploadfile);
if(!nurl) {
this_url = add_file_name_to_url(curl, this_url, uploadfile);
if(!this_url) {
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
goto show_error; goto show_error;
} }
/* VMS Note: per->this_url = nurl;
*
* Reading binary from files can be a problem... Only FIXED, VAR
* etc WITHOUT implied CC will work Others need a \n appended to a
* line
*
* - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
* fixed file with implied CC needs to have a byte added for every
* record processed, this can by derived from Filesize & recordsize
* for VARiable record files the records need to be counted! for
* every record add 1 for linefeed and subtract 2 for the record
* header for VARIABLE header files only the bare record data needs
* to be considered with one appended if implied CC
*/
#ifdef __VMS
/* Calculate the real upload size for VMS */
infd = -1;
if(stat(uploadfile, &fileinfo) == 0) {
fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
switch(fileinfo.st_fab_rfm) {
case FAB$C_VAR:
case FAB$C_VFC:
case FAB$C_STMCR:
infd = open(uploadfile, O_RDONLY | O_BINARY);
break;
default:
infd = open(uploadfile, O_RDONLY | O_BINARY,
"rfm=stmlf", "ctx=stm");
}
}
if(infd == -1)
#else
infd = open(uploadfile, O_RDONLY | O_BINARY);
if((infd == -1) || fstat(infd, &fileinfo))
#endif
{
helpf(global->errors, "Can't open '%s'!\n", uploadfile);
if(infd != -1) {
close(infd);
infd = STDIN_FILENO;
}
result = CURLE_READ_ERROR;
goto quit_urls;
}
infdopen = TRUE;
/* we ignore file size for char/block devices, sockets, etc. */
if(S_ISREG(fileinfo.st_mode))
uploadfilesize = fileinfo.st_size;
} }
else if(uploadfile && stdin_upload(uploadfile)) { else if(per->uploadfile && stdin_upload(per->uploadfile)) {
/* count to see if there are more than one auth bit set /* count to see if there are more than one auth bit set
in the authtype field */ in the authtype field */
int authbits = 0; int authbits = 0;
int bitcheck = 0; int bitcheck = 0;
while(bitcheck < 32) { while(bitcheck < 32) {
if(config->authtype & (1UL << bitcheck++)) { if(config->authtype & (1UL << bitcheck++)) {
authbits++; authbits++;
if(authbits > 1) { if(authbits > 1) {
/* more than one, we're done! */ /* more than one, we're done! */
break; break;
skipping to change at line 736 skipping to change at line 1038
* If the user has also selected --anyauth or --proxy-anyauth * If the user has also selected --anyauth or --proxy-anyauth
* we should warn him/her. * we should warn him/her.
*/ */
if(config->proxyanyauth || (authbits>1)) { if(config->proxyanyauth || (authbits>1)) {
warnf(config->global, warnf(config->global,
"Using --anyauth or --proxy-anyauth with upload from stdin" "Using --anyauth or --proxy-anyauth with upload from stdin"
" involves a big risk of it not working. Use a temporary" " involves a big risk of it not working. Use a temporary"
" file or a fixed auth type instead!\n"); " file or a fixed auth type instead!\n");
} }
DEBUGASSERT(infdopen == FALSE); DEBUGASSERT(per->infdopen == FALSE);
DEBUGASSERT(infd == STDIN_FILENO); DEBUGASSERT(per->infd == STDIN_FILENO);
set_binmode(stdin); set_binmode(stdin);
if(!strcmp(uploadfile, ".")) { if(!strcmp(per->uploadfile, ".")) {
if(curlx_nonblock((curl_socket_t)infd, TRUE) < 0) if(curlx_nonblock((curl_socket_t)per->infd, TRUE) < 0)
warnf(config->global, warnf(config->global,
"fcntl failed on fd=%d: %s\n", infd, strerror(errno)); "fcntl failed on fd=%d: %s\n", per->infd, strerror(errno));
} }
} }
if(uploadfile && config->resume_from_current) if(per->uploadfile && config->resume_from_current)
config->resume_from = -1; /* -1 will then force get-it-yourself */ config->resume_from = -1; /* -1 will then force get-it-yourself */
if(output_expected(this_url, uploadfile) && outs.stream && if(output_expected(per->this_url, per->uploadfile) && outs->stream &&
isatty(fileno(outs.stream))) isatty(fileno(outs->stream)))
/* we send the output to a tty, therefore we switch off the progress /* we send the output to a tty, therefore we switch off the progress
meter */ meter */
global->noprogress = global->isatty = TRUE; global->noprogress = global->isatty = TRUE;
else { else {
/* progress meter is per download, so restore config /* progress meter is per download, so restore config
values */ values */
global->noprogress = orig_noprogress; global->noprogress = orig_noprogress;
global->isatty = orig_isatty; global->isatty = orig_isatty;
} }
if(urlnum > 1 && !global->mute) { if(urlnum > 1 && !global->mute) {
fprintf(global->errors, "\n[%lu/%lu]: %s --> %s\n", per->separator_err =
li + 1, urlnum, this_url, outfile ? outfile : "<stdout>"); aprintf("\n[%lu/%lu]: %s --> %s",
li + 1, urlnum, per->this_url,
per->outfile ? per->outfile : "<stdout>");
if(separator) if(separator)
printf("%s%s\n", CURLseparator, this_url); per->separator = aprintf("%s%s", CURLseparator, per->this_url);
} }
if(httpgetfields) { if(httpgetfields) {
char *urlbuffer; char *urlbuffer;
/* Find out whether the url contains a file name */ /* Find out whether the url contains a file name */
const char *pc = strstr(this_url, "://"); const char *pc = strstr(per->this_url, "://");
char sep = '?'; char sep = '?';
if(pc) if(pc)
pc += 3; pc += 3;
else else
pc = this_url; pc = per->this_url;
pc = strrchr(pc, '/'); /* check for a slash */ pc = strrchr(pc, '/'); /* check for a slash */
if(pc) { if(pc) {
/* there is a slash present in the URL */ /* there is a slash present in the URL */
if(strchr(pc, '?')) if(strchr(pc, '?'))
/* Ouch, there's already a question mark in the URL string, we /* Ouch, there's already a question mark in the URL string, we
then append the data with an ampersand separator instead! */ then append the data with an ampersand separator instead! */
sep = '&'; sep = '&';
} }
/* /*
* Then append ? followed by the get fields to the url. * Then append ? followed by the get fields to the url.
*/ */
if(pc) if(pc)
urlbuffer = aprintf("%s%c%s", this_url, sep, httpgetfields); urlbuffer = aprintf("%s%c%s", per->this_url, sep, httpgetfields);
else else
/* Append / before the ? to create a well-formed url /* Append / before the ? to create a well-formed url
if the url contains a hostname only if the url contains a hostname only
*/ */
urlbuffer = aprintf("%s/?%s", this_url, httpgetfields); urlbuffer = aprintf("%s/?%s", per->this_url, httpgetfields);
if(!urlbuffer) { if(!urlbuffer) {
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
goto show_error; goto show_error;
} }
Curl_safefree(this_url); /* free previous URL */ Curl_safefree(per->this_url); /* free previous URL */
this_url = urlbuffer; /* use our new URL instead! */ per->this_url = urlbuffer; /* use our new URL instead! */
} }
if(!global->errors) if(!global->errors)
global->errors = stderr; global->errors = stderr;
if((!outfile || !strcmp(outfile, "-")) && !config->use_ascii) { if((!per->outfile || !strcmp(per->outfile, "-")) &&
!config->use_ascii) {
/* We get the output to stdout and we have not got the ASCII/text /* We get the output to stdout and we have not got the ASCII/text
flag, then set stdout to be binary */ flag, then set stdout to be binary */
set_binmode(stdout); set_binmode(stdout);
} }
/* explicitly passed to stdout means okaying binary gunk */ /* explicitly passed to stdout means okaying binary gunk */
config->terminal_binary_ok = (outfile && !strcmp(outfile, "-")); config->terminal_binary_ok =
(per->outfile && !strcmp(per->outfile, "-"));
/* avoid having this setopt added to the --libcurl source
output */
result = curl_easy_setopt(curl, CURLOPT_SHARE, share);
if(result)
goto show_error;
if(!config->tcp_nodelay) if(!config->tcp_nodelay)
my_setopt(curl, CURLOPT_TCP_NODELAY, 0L); my_setopt(curl, CURLOPT_TCP_NODELAY, 0L);
if(config->tcp_fastopen) if(config->tcp_fastopen)
my_setopt(curl, CURLOPT_TCP_FASTOPEN, 1L); my_setopt(curl, CURLOPT_TCP_FASTOPEN, 1L);
/* where to store */ /* where to store */
my_setopt(curl, CURLOPT_WRITEDATA, &outs); my_setopt(curl, CURLOPT_WRITEDATA, per);
my_setopt(curl, CURLOPT_INTERLEAVEDATA, &outs); my_setopt(curl, CURLOPT_INTERLEAVEDATA, per);
if(metalink || !config->use_metalink) if(metalink || !config->use_metalink)
/* what call to write */ /* what call to write */
my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb); my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
#ifdef USE_METALINK #ifdef USE_METALINK
else else
/* Set Metalink specific write callback function to parse /* Set Metalink specific write callback function to parse
XML data progressively. */ XML data progressively. */
my_setopt(curl, CURLOPT_WRITEFUNCTION, metalink_write_cb); my_setopt(curl, CURLOPT_WRITEFUNCTION, metalink_write_cb);
#endif /* USE_METALINK */ #endif /* USE_METALINK */
/* for uploads */ /* for uploads */
input.fd = infd; input->config = config;
input.config = config;
/* Note that if CURLOPT_READFUNCTION is fread (the default), then /* Note that if CURLOPT_READFUNCTION is fread (the default), then
* lib/telnet.c will Curl_poll() on the input file descriptor * lib/telnet.c will Curl_poll() on the input file descriptor
* rather then calling the READFUNCTION at regular intervals. * rather then calling the READFUNCTION at regular intervals.
* The circumstances in which it is preferable to enable this * The circumstances in which it is preferable to enable this
* behaviour, by omitting to set the READFUNCTION & READDATA options, * behaviour, by omitting to set the READFUNCTION & READDATA options,
* have not been determined. * have not been determined.
*/ */
my_setopt(curl, CURLOPT_READDATA, &input); my_setopt(curl, CURLOPT_READDATA, input);
/* what call to read */ /* what call to read */
my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb); my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
/* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */ CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
my_setopt(curl, CURLOPT_SEEKDATA, &input); my_setopt(curl, CURLOPT_SEEKDATA, input);
my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb); my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
if(config->recvpersecond && if(config->recvpersecond &&
(config->recvpersecond < BUFFER_SIZE)) (config->recvpersecond < BUFFER_SIZE))
/* use a smaller sized buffer for better sleeps */ /* use a smaller sized buffer for better sleeps */
my_setopt(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond); my_setopt(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond);
else else
my_setopt(curl, CURLOPT_BUFFERSIZE, (long)BUFFER_SIZE); my_setopt(curl, CURLOPT_BUFFERSIZE, (long)BUFFER_SIZE);
/* size of uploaded file: */ my_setopt_str(curl, CURLOPT_URL, per->this_url);
if(uploadfilesize != -1)
my_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
my_setopt_str(curl, CURLOPT_URL, this_url); /* what to fetch */
my_setopt(curl, CURLOPT_NOPROGRESS, global->noprogress?1L:0L); my_setopt(curl, CURLOPT_NOPROGRESS, global->noprogress?1L:0L);
if(config->no_body) if(config->no_body)
my_setopt(curl, CURLOPT_NOBODY, 1L); my_setopt(curl, CURLOPT_NOBODY, 1L);
if(config->oauth_bearer) if(config->oauth_bearer)
my_setopt_str(curl, CURLOPT_XOAUTH2_BEARER, config->oauth_bearer); my_setopt_str(curl, CURLOPT_XOAUTH2_BEARER, config->oauth_bearer);
{ {
my_setopt_str(curl, CURLOPT_PROXY, config->proxy); my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
/* new in libcurl 7.5 */ /* new in libcurl 7.5 */
skipping to change at line 918 skipping to change at line 1226
/* new in libcurl 7.19.4 */ /* new in libcurl 7.19.4 */
my_setopt_str(curl, CURLOPT_NOPROXY, config->noproxy); my_setopt_str(curl, CURLOPT_NOPROXY, config->noproxy);
my_setopt(curl, CURLOPT_SUPPRESS_CONNECT_HEADERS, my_setopt(curl, CURLOPT_SUPPRESS_CONNECT_HEADERS,
config->suppress_connect_headers?1L:0L); config->suppress_connect_headers?1L:0L);
} }
my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror?1L:0L); my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror?1L:0L);
my_setopt(curl, CURLOPT_REQUEST_TARGET, config->request_target); my_setopt(curl, CURLOPT_REQUEST_TARGET, config->request_target);
my_setopt(curl, CURLOPT_UPLOAD, uploadfile?1L:0L); my_setopt(curl, CURLOPT_UPLOAD, per->uploadfile?1L:0L);
my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly?1L:0L); my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly?1L:0L);
my_setopt(curl, CURLOPT_APPEND, config->ftp_append?1L:0L); my_setopt(curl, CURLOPT_APPEND, config->ftp_append?1L:0L);
if(config->netrc_opt) if(config->netrc_opt)
my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_OPTIONAL); my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_OPTIONAL);
else if(config->netrc || config->netrc_file) else if(config->netrc || config->netrc_file)
my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_REQUIRED); my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_REQUIRED);
else else
my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_IGNORED); my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_IGNORED);
if(config->netrc_file) if(config->netrc_file)
my_setopt_str(curl, CURLOPT_NETRC_FILE, config->netrc_file); my_setopt_str(curl, CURLOPT_NETRC_FILE, config->netrc_file);
my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii?1L:0L); my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii?1L:0L);
if(config->login_options) if(config->login_options)
my_setopt_str(curl, CURLOPT_LOGIN_OPTIONS, config->login_options); my_setopt_str(curl, CURLOPT_LOGIN_OPTIONS, config->login_options);
my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd); my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
my_setopt_str(curl, CURLOPT_RANGE, config->range); my_setopt_str(curl, CURLOPT_RANGE, config->range);
my_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer); my_setopt(curl, CURLOPT_ERRORBUFFER, per->errorbuffer);
my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000)); my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000));
switch(config->httpreq) { switch(config->httpreq) {
case HTTPREQ_SIMPLEPOST: case HTTPREQ_SIMPLEPOST:
my_setopt_str(curl, CURLOPT_POSTFIELDS, my_setopt_str(curl, CURLOPT_POSTFIELDS,
config->postfields); config->postfields);
my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
config->postfieldsize); config->postfieldsize);
break; break;
case HTTPREQ_MIMEPOST: case HTTPREQ_MIMEPOST:
skipping to change at line 1185 skipping to change at line 1493
if(config->path_as_is) if(config->path_as_is)
my_setopt(curl, CURLOPT_PATH_AS_IS, 1L); my_setopt(curl, CURLOPT_PATH_AS_IS, 1L);
if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) { if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
if(!config->insecure_ok) { if(!config->insecure_ok) {
char *home; char *home;
char *file; char *file;
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
home = homedir(); home = homedir();
if(home) { if(home) {
file = aprintf("%s/%sssh/known_hosts", home, DOT_CHAR); file = aprintf("%s/.ssh/known_hosts", home);
if(file) { if(file) {
/* new in curl 7.19.6 */ /* new in curl 7.19.6 */
result = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file); result = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file);
curl_free(file); curl_free(file);
if(result == CURLE_UNKNOWN_OPTION) if(result == CURLE_UNKNOWN_OPTION)
/* libssh2 version older than 1.1.1 */ /* libssh2 version older than 1.1.1 */
result = CURLE_OK; result = CURLE_OK;
} }
Curl_safefree(home); Curl_safefree(home);
} }
skipping to change at line 1233 skipping to change at line 1541
my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond); my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond);
my_setopt(curl, CURLOPT_TIMEVALUE_LARGE, config->condtime); my_setopt(curl, CURLOPT_TIMEVALUE_LARGE, config->condtime);
my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest); my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
customrequest_helper(config, config->httpreq, config->customrequest); customrequest_helper(config, config->httpreq, config->customrequest);
my_setopt(curl, CURLOPT_STDERR, global->errors); my_setopt(curl, CURLOPT_STDERR, global->errors);
/* three new ones in libcurl 7.3: */ /* three new ones in libcurl 7.3: */
my_setopt_str(curl, CURLOPT_INTERFACE, config->iface); my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel); my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
progressbarinit(&per->progressbar, config);
progressbarinit(&progressbar, config);
if((global->progressmode == CURL_PROGRESS_BAR) && if((global->progressmode == CURL_PROGRESS_BAR) &&
!global->noprogress && !global->mute) { !global->noprogress && !global->mute) {
/* we want the alternative style, then we have to implement it /* we want the alternative style, then we have to implement it
ourselves! */ ourselves! */
my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb); my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb);
my_setopt(curl, CURLOPT_XFERINFODATA, &progressbar); my_setopt(curl, CURLOPT_XFERINFODATA, &per->progressbar);
} }
/* new in libcurl 7.24.0: */ /* new in libcurl 7.24.0: */
if(config->dns_servers) if(config->dns_servers)
my_setopt_str(curl, CURLOPT_DNS_SERVERS, config->dns_servers); my_setopt_str(curl, CURLOPT_DNS_SERVERS, config->dns_servers);
/* new in libcurl 7.33.0: */ /* new in libcurl 7.33.0: */
if(config->dns_interface) if(config->dns_interface)
my_setopt_str(curl, CURLOPT_DNS_INTERFACE, config->dns_interface); my_setopt_str(curl, CURLOPT_DNS_INTERFACE, config->dns_interface);
if(config->dns_ipv4_addr) if(config->dns_ipv4_addr)
skipping to change at line 1423 skipping to change at line 1731
if(config->ftp_pret) if(config->ftp_pret)
my_setopt(curl, CURLOPT_FTP_USE_PRET, 1L); my_setopt(curl, CURLOPT_FTP_USE_PRET, 1L);
if(config->proto_present) if(config->proto_present)
my_setopt_flags(curl, CURLOPT_PROTOCOLS, config->proto); my_setopt_flags(curl, CURLOPT_PROTOCOLS, config->proto);
if(config->proto_redir_present) if(config->proto_redir_present)
my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir); my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
if(config->content_disposition if(config->content_disposition
&& (urlnode->flags & GETOUT_USEREMOTE)) && (urlnode->flags & GETOUT_USEREMOTE))
hdrcbdata.honor_cd_filename = TRUE; hdrcbdata->honor_cd_filename = TRUE;
else else
hdrcbdata.honor_cd_filename = FALSE; hdrcbdata->honor_cd_filename = FALSE;
hdrcbdata.outs = &outs; hdrcbdata->outs = outs;
hdrcbdata.heads = &heads; hdrcbdata->heads = heads;
hdrcbdata.global = global; hdrcbdata->global = global;
hdrcbdata.config = config; hdrcbdata->config = config;
my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb); my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
my_setopt(curl, CURLOPT_HEADERDATA, &hdrcbdata); my_setopt(curl, CURLOPT_HEADERDATA, per);
if(config->resolve) if(config->resolve)
/* new in 7.21.3 */ /* new in 7.21.3 */
my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve); my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
if(config->connect_to) if(config->connect_to)
/* new in 7.49.0 */ /* new in 7.49.0 */
my_setopt_slist(curl, CURLOPT_CONNECT_TO, config->connect_to); my_setopt_slist(curl, CURLOPT_CONNECT_TO, config->connect_to);
/* new in 7.21.4 */ /* new in 7.21.4 */
skipping to change at line 1485 skipping to change at line 1793
my_setopt_bitmask(curl, CURLOPT_SSL_OPTIONS, mask); my_setopt_bitmask(curl, CURLOPT_SSL_OPTIONS, mask);
} }
if(config->proxy_ssl_allow_beast) if(config->proxy_ssl_allow_beast)
my_setopt(curl, CURLOPT_PROXY_SSL_OPTIONS, my_setopt(curl, CURLOPT_PROXY_SSL_OPTIONS,
(long)CURLSSLOPT_ALLOW_BEAST); (long)CURLSSLOPT_ALLOW_BEAST);
if(config->mail_auth) if(config->mail_auth)
my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth); my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
/* new in 7.66.0 */
if(config->sasl_authzid)
my_setopt_str(curl, CURLOPT_SASL_AUTHZID, config->sasl_authzid);
/* new in 7.31.0 */ /* new in 7.31.0 */
if(config->sasl_ir) if(config->sasl_ir)
my_setopt(curl, CURLOPT_SASL_IR, 1L); my_setopt(curl, CURLOPT_SASL_IR, 1L);
if(config->nonpn) { if(config->nonpn) {
my_setopt(curl, CURLOPT_SSL_ENABLE_NPN, 0L); my_setopt(curl, CURLOPT_SSL_ENABLE_NPN, 0L);
} }
if(config->noalpn) { if(config->noalpn) {
my_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L); my_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L);
skipping to change at line 1508 skipping to change at line 1820
if(config->unix_socket_path) { if(config->unix_socket_path) {
if(config->abstract_unix_socket) { if(config->abstract_unix_socket) {
my_setopt_str(curl, CURLOPT_ABSTRACT_UNIX_SOCKET, my_setopt_str(curl, CURLOPT_ABSTRACT_UNIX_SOCKET,
config->unix_socket_path); config->unix_socket_path);
} }
else { else {
my_setopt_str(curl, CURLOPT_UNIX_SOCKET_PATH, my_setopt_str(curl, CURLOPT_UNIX_SOCKET_PATH,
config->unix_socket_path); config->unix_socket_path);
} }
} }
/* new in 7.45.0 */ /* new in 7.45.0 */
if(config->proto_default) if(config->proto_default)
my_setopt_str(curl, CURLOPT_DEFAULT_PROTOCOL, config->proto_default); my_setopt_str(curl, CURLOPT_DEFAULT_PROTOCOL, config->proto_default);
/* new in 7.47.0 */ /* new in 7.47.0 */
if(config->expect100timeout > 0) if(config->expect100timeout > 0)
my_setopt_str(curl, CURLOPT_EXPECT_100_TIMEOUT_MS, my_setopt_str(curl, CURLOPT_EXPECT_100_TIMEOUT_MS,
(long)(config->expect100timeout*1000)); (long)(config->expect100timeout*1000));
/* new in 7.48.0 */ /* new in 7.48.0 */
skipping to change at line 1539 skipping to change at line 1852
if(config->disallow_username_in_url) if(config->disallow_username_in_url)
my_setopt(curl, CURLOPT_DISALLOW_USERNAME_IN_URL, 1L); my_setopt(curl, CURLOPT_DISALLOW_USERNAME_IN_URL, 1L);
#ifdef USE_ALTSVC #ifdef USE_ALTSVC
/* only if explicitly enabled in configure */ /* only if explicitly enabled in configure */
if(config->altsvc) if(config->altsvc)
my_setopt_str(curl, CURLOPT_ALTSVC, config->altsvc); my_setopt_str(curl, CURLOPT_ALTSVC, config->altsvc);
#endif #endif
/* initialize retry vars for loop below */
retry_sleep_default = (config->retry_delay) ?
config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
retry_numretries = config->req_retry;
retry_sleep = retry_sleep_default; /* ms */
retrystart = tvnow();
#ifndef CURL_DISABLE_LIBCURL_OPTION
if(global->libcurl) {
result = easysrc_perform();
if(result)
goto show_error;
}
#endif
for(;;) {
#ifdef USE_METALINK
if(!metalink && config->use_metalink) {
/* If outs.metalink_parser is non-NULL, delete it first. */
if(outs.metalink_parser)
metalink_parser_context_delete(outs.metalink_parser);
outs.metalink_parser = metalink_parser_context_new();
if(outs.metalink_parser == NULL) {
result = CURLE_OUT_OF_MEMORY;
goto show_error;
}
fprintf(config->global->errors,
"Metalink: parsing (%s) metalink/XML...\n", this_url);
}
else if(metalink)
fprintf(config->global->errors,
"Metalink: fetching (%s) from (%s)...\n",
mlfile->filename, this_url);
#endif /* USE_METALINK */
#ifdef CURLDEBUG
if(config->test_event_based)
result = curl_easy_perform_ev(curl);
else
#endif
result = curl_easy_perform(curl);
if(!result && !outs.stream && !outs.bytes) {
/* we have received no data despite the transfer was successful
==> force cration of an empty output file (if an output file
was specified) */
long cond_unmet = 0L;
/* do not create (or even overwrite) the file in case we get no
data because of unmet condition */
curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &cond_unmet);
if(!cond_unmet && !tool_create_output_file(&outs))
result = CURLE_WRITE_ERROR;
}
if(outs.is_cd_filename && outs.stream && !global->mute &&
outs.filename)
printf("curl: Saved to filename '%s'\n", outs.filename);
/* if retry-max-time is non-zero, make sure we haven't exceeded the
time */
if(retry_numretries &&
(!config->retry_maxtime ||
(tvdiff(tvnow(), retrystart) <
config->retry_maxtime*1000L)) ) {
enum {
RETRY_NO,
RETRY_TIMEOUT,
RETRY_CONNREFUSED,
RETRY_HTTP,
RETRY_FTP,
RETRY_LAST /* not used */
} retry = RETRY_NO;
long response;
if((CURLE_OPERATION_TIMEDOUT == result) ||
(CURLE_COULDNT_RESOLVE_HOST == result) ||
(CURLE_COULDNT_RESOLVE_PROXY == result) ||
(CURLE_FTP_ACCEPT_TIMEOUT == result))
/* retry timeout always */
retry = RETRY_TIMEOUT;
else if(config->retry_connrefused &&
(CURLE_COULDNT_CONNECT == result)) {
long oserrno;
curl_easy_getinfo(curl, CURLINFO_OS_ERRNO, &oserrno);
if(ECONNREFUSED == oserrno)
retry = RETRY_CONNREFUSED;
}
else if((CURLE_OK == result) ||
(config->failonerror &&
(CURLE_HTTP_RETURNED_ERROR == result))) {
/* If it returned OK. _or_ failonerror was enabled and it
returned due to such an error, check for HTTP transient
errors to retry on. */
char *effective_url = NULL;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
if(effective_url &&
checkprefix("http", effective_url)) {
/* This was HTTP(S) */
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
switch(response) {
case 408: /* Request Timeout */
case 500: /* Internal Server Error */
case 502: /* Bad Gateway */
case 503: /* Service Unavailable */
case 504: /* Gateway Timeout */
retry = RETRY_HTTP;
/*
* At this point, we have already written data to the output
* file (or terminal). If we write to a file, we must rewind
* or close/re-open the file so that the next attempt starts
* over from the beginning.
*/
break;
}
}
} /* if CURLE_OK */
else if(result) {
long protocol;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
if((protocol == CURLPROTO_FTP || protocol == CURLPROTO_FTPS) &&
response / 100 == 4)
/*
* This is typically when the FTP server only allows a certain
* amount of users and we are not one of them. All 4xx codes
* are transient.
*/
retry = RETRY_FTP;
}
if(retry) {
static const char * const m[]={
NULL,
"timeout",
"connection refused",
"HTTP error",
"FTP error"
};
warnf(config->global, "Transient problem: %s "
"Will retry in %ld seconds. "
"%ld retries left.\n",
m[retry], retry_sleep/1000L, retry_numretries);
tool_go_sleep(retry_sleep);
retry_numretries--;
if(!config->retry_delay) {
retry_sleep *= 2;
if(retry_sleep > RETRY_SLEEP_MAX)
retry_sleep = RETRY_SLEEP_MAX;
}
if(outs.bytes && outs.filename && outs.stream) {
int rc;
/* We have written data to a output file, we truncate file
*/
if(!global->mute)
fprintf(global->errors, "Throwing away %"
CURL_FORMAT_CURL_OFF_T " bytes\n",
outs.bytes);
fflush(outs.stream);
/* truncate file at the position where we started appending */
#ifdef HAVE_FTRUNCATE
if(ftruncate(fileno(outs.stream), outs.init)) {
/* when truncate fails, we can't just append as then we'll
create something strange, bail out */
if(!global->mute)
fprintf(global->errors,
"failed to truncate, exiting\n");
result = CURLE_WRITE_ERROR;
goto quit_urls;
}
/* now seek to the end of the file, the position where we
just truncated the file in a large file-safe way */
rc = fseek(outs.stream, 0, SEEK_END);
#else
/* ftruncate is not available, so just reposition the file
to the location we would have truncated it. This won't
work properly with large files on 32-bit systems, but
most of those will have ftruncate. */
rc = fseek(outs.stream, (long)outs.init, SEEK_SET);
#endif
if(rc) {
if(!global->mute)
fprintf(global->errors,
"failed seeking to end of file, exiting\n");
result = CURLE_WRITE_ERROR;
goto quit_urls;
}
outs.bytes = 0; /* clear for next round */
}
continue; /* curl_easy_perform loop */
}
} /* if retry_numretries */
else if(metalink) {
/* Metalink: Decide to try the next resource or
not. Basically, we want to try the next resource if
download was not successful. */
long response;
if(CURLE_OK == result) {
char *effective_url = NULL;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
if(effective_url &&
curl_strnequal(effective_url, "http", 4)) {
/* This was HTTP(S) */
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
if(response != 200 && response != 206) {
metalink_next_res = 1;
fprintf(global->errors,
"Metalink: fetching (%s) from (%s) FAILED "
"(HTTP status code %ld)\n",
mlfile->filename, this_url, response);
}
}
}
else {
metalink_next_res = 1;
fprintf(global->errors,
"Metalink: fetching (%s) from (%s) FAILED (%s)\n",
mlfile->filename, this_url,
(errorbuffer[0]) ?
errorbuffer : curl_easy_strerror(result));
}
}
if(metalink && !metalink_next_res)
fprintf(global->errors, "Metalink: fetching (%s) from (%s) OK\n",
mlfile->filename, this_url);
/* In all ordinary cases, just break out of loop here */
break; /* curl_easy_perform loop */
}
if((global->progressmode == CURL_PROGRESS_BAR) &&
progressbar.calls)
/* if the custom progress bar has been displayed, we output a
newline here */
fputs("\n", progressbar.out);
if(config->writeout)
ourWriteOut(curl, &outs, config->writeout);
/*
** Code within this loop may jump directly here to label 'show_error'
** in order to display an error message for CURLcode stored in 'res'
** variable and exit loop once that necessary writing and cleanup
** in label 'quit_urls' has been done.
*/
show_error:
#ifdef __VMS
if(is_vms_shell()) {
/* VMS DCL shell behavior */
if(!global->showerror)
vms_show = VMSSTS_HIDE;
}
else
#endif
if(config->synthetic_error) {
;
}
else if(result && global->showerror) {
fprintf(global->errors, "curl: (%d) %s\n", result, (errorbuffer[0]) ?
errorbuffer : curl_easy_strerror(result));
if(result == CURLE_PEER_FAILED_VERIFICATION)
fputs(CURL_CA_CERT_ERRORMSG, global->errors);
}
/* Fall through comment to 'quit_urls' label */
/*
** Upon error condition and always that a message has already been
** displayed, code within this loop may jump directly here to label
** 'quit_urls' otherwise it should jump to 'show_error' label above.
**
** When 'res' variable is _not_ CURLE_OK loop will exit once that
** all code following 'quit_urls' has been executed. Otherwise it
** will loop to the beginning from where it may exit if there are
** no more urls left.
*/
quit_urls:
/* Set file extended attributes */
if(!result && config->xattr && outs.fopened && outs.stream) {
int rc = fwrite_xattr(curl, fileno(outs.stream));
if(rc)
warnf(config->global, "Error setting extended attributes: %s\n",
strerror(errno));
}
/* Close the file */
if(outs.fopened && outs.stream) {
int rc = fclose(outs.stream);
if(!result && rc) {
/* something went wrong in the writing process */
result = CURLE_WRITE_ERROR;
fprintf(global->errors, "(%d) Failed writing body\n", result);
}
}
else if(!outs.s_isreg && outs.stream) {
/* Dump standard stream buffered data */
int rc = fflush(outs.stream);
if(!result && rc) {
/* something went wrong in the writing process */
result = CURLE_WRITE_ERROR;
fprintf(global->errors, "(%d) Failed writing body\n", result);
}
}
#ifdef __AMIGA__
if(!result && outs.s_isreg && outs.filename) {
/* Set the url (up to 80 chars) as comment for the file */
if(strlen(urlnode->url) > 78)
urlnode->url[79] = '\0';
SetComment(outs.filename, urlnode->url);
}
#endif
/* File time can only be set _after_ the file has been closed */
if(!result && config->remote_time && outs.s_isreg && outs.filename) {
/* Ask libcurl if we got a remote file time */
curl_off_t filetime = -1;
curl_easy_getinfo(curl, CURLINFO_FILETIME_T, &filetime);
setfiletime(filetime, outs.filename, config->global->errors);
}
#ifdef USE_METALINK #ifdef USE_METALINK
if(!metalink && config->use_metalink && result == CURLE_OK) { if(!metalink && config->use_metalink) {
int rv = parse_metalink(config, &outs, this_url); outs->metalink_parser = metalink_parser_context_new();
if(rv == 0) if(outs->metalink_parser == NULL) {
fprintf(config->global->errors, "Metalink: parsing (%s) OK\n", result = CURLE_OUT_OF_MEMORY;
this_url); goto show_error;
else if(rv == -1)
fprintf(config->global->errors, "Metalink: parsing (%s) FAILED\n",
this_url);
}
else if(metalink && result == CURLE_OK && !metalink_next_res) {
int rv = metalink_check_hash(global, mlfile, outs.filename);
if(rv == 0) {
metalink_next_res = 1;
} }
fprintf(config->global->errors,
"Metalink: parsing (%s) metalink/XML...\n", per->this_url);
} }
else if(metalink)
fprintf(config->global->errors,
"Metalink: fetching (%s) from (%s)...\n",
mlfile->filename, per->this_url);
#endif /* USE_METALINK */ #endif /* USE_METALINK */
/* No more business with this output struct */ per->metalink = metalink;
if(outs.alloc_filename) /* initialize retry vars for loop below */
Curl_safefree(outs.filename); per->retry_sleep_default = (config->retry_delay) ?
#ifdef USE_METALINK config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
if(outs.metalink_parser) per->retry_numretries = config->req_retry;
metalink_parser_context_delete(outs.metalink_parser); per->retry_sleep = per->retry_sleep_default; /* ms */
#endif /* USE_METALINK */ per->retrystart = tvnow();
memset(&outs, 0, sizeof(struct OutStruct));
hdrcbdata.outs = NULL;
/* Free loop-local allocated memory and close loop-local opened fd */
Curl_safefree(outfile);
Curl_safefree(this_url);
if(infdopen)
close(infd);
if(metalink) {
/* Should exit if error is fatal. */
if(is_fatal_error(result)) {
break;
}
if(!metalink_next_res)
break;
mlres = mlres->next;
if(mlres == NULL)
break;
}
else if(urlnum > 1) {
/* when url globbing, exit loop upon critical error */
if(is_fatal_error(result))
break;
}
else if(result)
/* when not url globbing, exit loop upon any error */
break;
} /* loop to the next URL */ } /* loop to the next URL */
/* Free loop-local allocated memory */ show_error:
quit_urls:
Curl_safefree(uploadfile);
if(urls) { if(urls) {
/* Free list of remaining URLs */ /* Free list of remaining URLs */
glob_cleanup(urls); glob_cleanup(urls);
urls = NULL; urls = NULL;
} }
if(infilenum > 1) { if(infilenum > 1) {
/* when file globbing, exit loop upon critical error */ /* when file globbing, exit loop upon critical error */
if(is_fatal_error(result)) if(is_fatal_error(result))
skipping to change at line 1965 skipping to change at line 1915
inglob = NULL; inglob = NULL;
} }
/* Free this URL node data without destroying the /* Free this URL node data without destroying the
the node itself nor modifying next pointer. */ the node itself nor modifying next pointer. */
Curl_safefree(urlnode->url); Curl_safefree(urlnode->url);
Curl_safefree(urlnode->outfile); Curl_safefree(urlnode->outfile);
Curl_safefree(urlnode->infile); Curl_safefree(urlnode->infile);
urlnode->flags = 0; urlnode->flags = 0;
/*
** Bail out upon critical errors or --fail-early
*/
if(is_fatal_error(result) || (result && global->fail_early))
goto quit_curl;
} /* for-loop through all URLs */ } /* for-loop through all URLs */
/*
** Nested loops end here.
*/
quit_curl: quit_curl:
/* Reset the global config variables */
global->noprogress = orig_noprogress;
global->isatty = orig_isatty;
/* Free function-local referenced allocated memory */ /* Free function-local referenced allocated memory */
Curl_safefree(httpgetfields); Curl_safefree(httpgetfields);
/* Free list of given URLs */ return result;
clean_getout(config); }
hdrcbdata.heads = NULL; static long all_added; /* number of easy handles currently added */
/* Close function-local opened file descriptors */ /*
if(heads.fopened && heads.stream) * add_parallel_transfers() sets 'morep' to TRUE if there are more transfers
fclose(heads.stream); * to add even after this call returns. sets 'addedp' to TRUE if one or more
* transfers were added.
*/
static int add_parallel_transfers(struct GlobalConfig *global,
CURLM *multi,
bool *morep,
bool *addedp)
{
struct per_transfer *per;
CURLcode result;
CURLMcode mcode;
*addedp = FALSE;
*morep = FALSE;
for(per = transfers; per && (all_added < global->parallel_max);
per = per->next) {
if(per->added)
/* already added */
continue;
result = pre_transfer(global, per);
if(result)
break;
(void)curl_easy_setopt(per->curl, CURLOPT_PRIVATE, per);
(void)curl_easy_setopt(per->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
(void)curl_easy_setopt(per->curl, CURLOPT_XFERINFODATA, per);
mcode = curl_multi_add_handle(multi, per->curl);
if(mcode)
return CURLE_OUT_OF_MEMORY;
per->added = TRUE;
all_added++;
*addedp = TRUE;
}
*morep = per ? TRUE : FALSE;
return CURLE_OK;
}
static CURLcode parallel_transfers(struct GlobalConfig *global,
CURLSH *share)
{
CURLM *multi;
bool done = FALSE;
CURLMcode mcode = CURLM_OK;
CURLcode result = CURLE_OK;
int still_running = 1;
struct timeval start = tvnow();
bool more_transfers;
bool added_transfers;
multi = curl_multi_init();
if(!multi)
return CURLE_OUT_OF_MEMORY;
result = add_parallel_transfers(global, multi,
&more_transfers, &added_transfers);
if(result)
return result;
while(!done && !mcode && (still_running || more_transfers)) {
mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(!mcode)
mcode = curl_multi_perform(multi, &still_running);
progress_meter(global, &start, FALSE);
if(!mcode) {
int rc;
CURLMsg *msg;
bool removed = FALSE;
do {
msg = curl_multi_info_read(multi, &rc);
if(msg) {
bool retry;
struct per_transfer *ended;
CURL *easy = msg->easy_handle;
result = msg->data.result;
curl_easy_getinfo(easy, CURLINFO_PRIVATE, (void *)&ended);
curl_multi_remove_handle(multi, easy);
result = post_transfer(global, share, ended, result, &retry);
if(retry)
continue;
progress_finalize(ended); /* before it goes away */
all_added--; /* one fewer added */
removed = TRUE;
(void)del_transfer(ended);
}
} while(msg);
if(removed) {
/* one or more transfers completed, add more! */
(void)add_parallel_transfers(global, multi, &more_transfers,
&added_transfers);
if(added_transfers)
/* we added new ones, make sure the loop doesn't exit yet */
still_running = 1;
}
}
}
(void)progress_meter(global, &start, TRUE);
/* Make sure to return some kind of error if there was a multi problem */
if(mcode) {
result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
/* The other multi errors should never happen, so return
something suitably generic */
CURLE_BAD_FUNCTION_ARGUMENT;
}
curl_multi_cleanup(multi);
return result;
}
static CURLcode serial_transfers(struct GlobalConfig *global,
CURLSH *share)
{
CURLcode returncode = CURLE_OK;
CURLcode result = CURLE_OK;
struct per_transfer *per;
for(per = transfers; per;) {
bool retry;
result = pre_transfer(global, per);
if(result)
break;
#ifndef CURL_DISABLE_LIBCURL_OPTION
if(global->libcurl) {
result = easysrc_perform();
if(result)
break;
}
#endif
#ifdef CURLDEBUG
if(global->test_event_based)
result = curl_easy_perform_ev(per->curl);
else
#endif
result = curl_easy_perform(per->curl);
if(heads.alloc_filename) /* store the result of the actual transfer */
Curl_safefree(heads.filename); returncode = result;
/* Release metalink related resources here */ result = post_transfer(global, share, per, result, &retry);
clean_metalink(config); if(retry)
continue;
per = del_transfer(per);
/* Bail out upon critical errors or --fail-early */
if(result || is_fatal_error(returncode) ||
(returncode && global->fail_early))
break;
}
if(returncode)
/* returncode errors have priority */
result = returncode;
return result;
}
static CURLcode operate_do(struct GlobalConfig *global,
struct OperationConfig *config,
CURLSH *share)
{
CURLcode result = CURLE_OK;
bool capath_from_env;
/* Check we have a url */
if(!config->url_list || !config->url_list->url) {
helpf(global->errors, "no URL specified!\n");
return CURLE_FAILED_INIT;
}
/* On WIN32 we can't set the path to curl-ca-bundle.crt
* at compile time. So we look here for the file in two ways:
* 1: look at the environment variable CURL_CA_BUNDLE for a path
* 2: if #1 isn't found, use the windows API function SearchPath()
* to find it along the app's path (includes app's dir and CWD)
*
* We support the environment variable thing for non-Windows platforms
* too. Just for the sake of it.
*/
capath_from_env = false;
if(!config->cacert &&
!config->capath &&
!config->insecure_ok) {
CURL *curltls = curl_easy_init();
struct curl_tlssessioninfo *tls_backend_info = NULL;
/* With the addition of CAINFO support for Schannel, this search could find
* a certificate bundle that was previously ignored. To maintain backward
* compatibility, only perform this search if not using Schannel.
*/
result = curl_easy_getinfo(curltls, CURLINFO_TLS_SSL_PTR,
&tls_backend_info);
if(result)
return result;
/* Set the CA cert locations specified in the environment. For Windows if
* no environment-specified filename is found then check for CA bundle
* default filename curl-ca-bundle.crt in the user's PATH.
*
* If Schannel is the selected SSL backend then these locations are
* ignored. We allow setting CA location for schannel only when explicitly
* specified by the user via CURLOPT_CAINFO / --cacert.
*/
if(tls_backend_info->backend != CURLSSLBACKEND_SCHANNEL) {
char *env;
env = curlx_getenv("CURL_CA_BUNDLE");
if(env) {
config->cacert = strdup(env);
if(!config->cacert) {
curl_free(env);
helpf(global->errors, "out of memory\n");
return CURLE_OUT_OF_MEMORY;
}
}
else {
env = curlx_getenv("SSL_CERT_DIR");
if(env) {
config->capath = strdup(env);
if(!config->capath) {
curl_free(env);
helpf(global->errors, "out of memory\n");
return CURLE_OUT_OF_MEMORY;
}
capath_from_env = true;
}
else {
env = curlx_getenv("SSL_CERT_FILE");
if(env) {
config->cacert = strdup(env);
if(!config->cacert) {
curl_free(env);
helpf(global->errors, "out of memory\n");
return CURLE_OUT_OF_MEMORY;
}
}
}
}
if(env)
curl_free(env);
#ifdef WIN32
else {
result = FindWin32CACert(config, tls_backend_info->backend,
"curl-ca-bundle.crt");
}
#endif
}
curl_easy_cleanup(curltls);
}
if(!result)
/* loop through the list of given URLs */
result = create_transfers(global, config, share, capath_from_env);
return result;
}
static CURLcode operate_transfers(struct GlobalConfig *global,
CURLSH *share,
CURLcode result)
{
/* Save the values of noprogress and isatty to restore them later on */
bool orig_noprogress = global->noprogress;
bool orig_isatty = global->isatty;
struct per_transfer *per;
/* Time to actually do the transfers */
if(!result) {
if(global->parallel)
result = parallel_transfers(global, share);
else
result = serial_transfers(global, share);
}
/* cleanup if there are any left */
for(per = transfers; per;) {
bool retry;
(void)post_transfer(global, share, per, result, &retry);
/* Free list of given URLs */
clean_getout(per->config);
/* Release metalink related resources here */
clean_metalink(per->config);
per = del_transfer(per);
}
/* Reset the global config variables */
global->noprogress = orig_noprogress;
global->isatty = orig_isatty;
return result; return result;
} }
CURLcode operate(struct GlobalConfig *config, int argc, argv_item_t argv[]) CURLcode operate(struct GlobalConfig *config, int argc, argv_item_t argv[])
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
/* Setup proper locale from environment */ /* Setup proper locale from environment */
#ifdef HAVE_SETLOCALE #ifdef HAVE_SETLOCALE
skipping to change at line 2043 skipping to change at line 2263
if(res == PARAM_HELP_REQUESTED) if(res == PARAM_HELP_REQUESTED)
tool_help(); tool_help();
/* Check if we were asked for the manual */ /* Check if we were asked for the manual */
else if(res == PARAM_MANUAL_REQUESTED) else if(res == PARAM_MANUAL_REQUESTED)
hugehelp(); hugehelp();
/* Check if we were asked for the version information */ /* Check if we were asked for the version information */
else if(res == PARAM_VERSION_INFO_REQUESTED) else if(res == PARAM_VERSION_INFO_REQUESTED)
tool_version_info(); tool_version_info();
/* Check if we were asked to list the SSL engines */ /* Check if we were asked to list the SSL engines */
else if(res == PARAM_ENGINES_REQUESTED) else if(res == PARAM_ENGINES_REQUESTED)
tool_list_engines(config->easy); tool_list_engines();
else if(res == PARAM_LIBCURL_UNSUPPORTED_PROTOCOL) else if(res == PARAM_LIBCURL_UNSUPPORTED_PROTOCOL)
result = CURLE_UNSUPPORTED_PROTOCOL; result = CURLE_UNSUPPORTED_PROTOCOL;
else else
result = CURLE_FAILED_INIT; result = CURLE_FAILED_INIT;
} }
else { else {
#ifndef CURL_DISABLE_LIBCURL_OPTION #ifndef CURL_DISABLE_LIBCURL_OPTION
if(config->libcurl) { if(config->libcurl) {
/* Initialise the libcurl source output */ /* Initialise the libcurl source output */
result = easysrc_init(); result = easysrc_init();
} }
#endif #endif
/* Perform the main operations */ /* Perform the main operations */
if(!result) { if(!result) {
size_t count = 0; size_t count = 0;
struct OperationConfig *operation = config->first; struct OperationConfig *operation = config->first;
CURLSH *share = curl_share_init();
if(!share) {
#ifndef CURL_DISABLE_LIBCURL_OPTION
if(config->libcurl) {
/* Cleanup the libcurl source output */
easysrc_cleanup();
}
#endif
return CURLE_OUT_OF_MEMORY;
}
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
/* Get the required arguments for each operation */ /* Get the required arguments for each operation */
while(!result && operation) { do {
result = get_args(operation, count++); result = get_args(operation, count++);
operation = operation->next; operation = operation->next;
} } while(!result && operation);
/* Set the current operation pointer */ /* Set the current operation pointer */
config->current = config->first; config->current = config->first;
/* Perform each operation */ /* Setup all transfers */
while(!result && config->current) { while(!result && config->current) {
result = operate_do(config, config->current); result = operate_do(config, config->current, share);
config->current = config->current->next; config->current = config->current->next;
if(config->current && config->current->easy)
curl_easy_reset(config->current->easy);
} }
/* now run! */
result = operate_transfers(config, share, result);
curl_share_cleanup(share);
#ifndef CURL_DISABLE_LIBCURL_OPTION #ifndef CURL_DISABLE_LIBCURL_OPTION
if(config->libcurl) { if(config->libcurl) {
/* Cleanup the libcurl source output */ /* Cleanup the libcurl source output */
easysrc_cleanup(); easysrc_cleanup();
/* Dump the libcurl code if previously enabled */ /* Dump the libcurl code if previously enabled */
dumpeasysrc(config); dumpeasysrc(config);
} }
#endif #endif
} }
 End of changes. 108 change blocks. 
701 lines changed or deleted 937 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)