1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
30 #elif defined(HAVE_SYS_UTIME_H)
31 # include <sys/utime.h>
38 #ifdef HAVE_NETINET_TCP_H
39 # include <netinet/tcp.h>
48 #define ENABLE_CURLX_PRINTF
49 /* use our own printf() functions */
52 #include "tool_binmode.h"
53 #include "tool_cfgable.h"
54 #include "tool_cb_dbg.h"
55 #include "tool_cb_hdr.h"
56 #include "tool_cb_prg.h"
57 #include "tool_cb_rea.h"
58 #include "tool_cb_see.h"
59 #include "tool_cb_wrt.h"
60 #include "tool_dirhie.h"
61 #include "tool_doswin.h"
62 #include "tool_easysrc.h"
63 #include "tool_getparam.h"
64 #include "tool_helpers.h"
65 #include "tool_homedir.h"
66 #include "tool_libinfo.h"
67 #include "tool_main.h"
68 #include "tool_metalink.h"
69 #include "tool_msgs.h"
70 #include "tool_operate.h"
71 #include "tool_operhlp.h"
72 #include "tool_paramhlp.h"
73 #include "tool_parsecfg.h"
74 #include "tool_setopt.h"
75 #include "tool_sleep.h"
76 #include "tool_urlglob.h"
77 #include "tool_util.h"
78 #include "tool_writeenv.h"
79 #include "tool_writeout.h"
80 #include "tool_xattr.h"
82 #include "tool_help.h"
83 #include "tool_hugehelp.h"
85 #include "memdebug.h" /* keep this as LAST include */
88 /* libcurl's debug builds provide an extra function */
89 CURLcode curl_easy_perform_ev(CURL *easy);
92 #define CURLseparator "--_curl_--"
95 /* since O_BINARY as used in bitmasks, setting it to zero makes it usable in
96 source code but yet it doesn't ruin anything */
100 #define CURL_CA_CERT_ERRORMSG1 \
101 "More details here: https://curl.haxx.se/docs/sslcerts.html\n\n" \
102 "curl performs SSL certificate verification by default, " \
103 "using a \"bundle\"\n" \
104 " of Certificate Authority (CA) public keys (CA certs). If the default\n" \
105 " bundle file isn't adequate, you can specify an alternate file\n" \
106 " using the --cacert option.\n"
108 #define CURL_CA_CERT_ERRORMSG2 \
109 "If this HTTPS server uses a certificate signed by a CA represented in\n" \
110 " the bundle, the certificate verification probably failed due to a\n" \
111 " problem with the certificate (it might be expired, or the name might\n" \
112 " not match the domain name in the URL).\n" \
113 "If you'd like to turn off curl's verification of the certificate, use\n" \
114 " the -k (or --insecure) option.\n"
116 static bool is_fatal_error(CURLcode code)
119 /* TODO: Should CURLE_SSL_CACERT be included as critical error ? */
120 case CURLE_FAILED_INIT:
121 case CURLE_OUT_OF_MEMORY:
122 case CURLE_UNKNOWN_OPTION:
123 case CURLE_FUNCTION_NOT_FOUND:
124 case CURLE_BAD_FUNCTION_ARGUMENT:
131 /* no error or not critical */
137 * get_vms_file_size does what it takes to get the real size of the file
139 * For fixed files, find out the size of the EOF block and adjust.
141 * For all others, have to read the entire file in, discarding the contents.
142 * Most posted text files will be small, and binary files like zlib archives
143 * and CD/DVD images should be either a STREAM_LF format or a fixed format.
146 static curl_off_t vms_realfilesize(const char * name,
147 const struct_stat * stat_buf)
154 file = fopen(name, "r"); /* VMS */
160 while(ret_stat > 0) {
161 ret_stat = fread(buffer, 1, sizeof(buffer), file);
172 * VmsSpecialSize checks to see if the stat st_size can be trusted and
173 * if not to call a routine to get the correct size.
176 static curl_off_t VmsSpecialSize(const char * name,
177 const struct_stat * stat_buf)
179 switch(stat_buf->st_fab_rfm) {
182 return vms_realfilesize(name, stat_buf);
185 return stat_buf->st_size;
190 static CURLcode operate_do(struct GlobalConfig *global,
191 struct OperationConfig *config)
193 char errorbuffer[CURL_ERROR_SIZE];
194 struct ProgressData progressbar;
195 struct getout *urlnode;
197 struct HdrCbData hdrcbdata;
198 struct OutStruct heads;
200 metalinkfile *mlfile_last = NULL;
202 CURL *curl = config->easy;
203 char *httpgetfields = NULL;
205 CURLcode result = CURLE_OK;
208 /* Save the values of noprogress and isatty to restore them later on */
209 bool orig_noprogress = global->noprogress;
210 bool orig_isatty = global->isatty;
212 errorbuffer[0] = '\0';
214 /* default headers output stream is stdout */
215 memset(&hdrcbdata, 0, sizeof(struct HdrCbData));
216 memset(&heads, 0, sizeof(struct OutStruct));
217 heads.stream = stdout;
218 heads.config = config;
221 ** Beyond this point no return'ing from this function allowed.
222 ** Jump to label 'quit_curl' in order to abandon this function
223 ** from outside of nested loops further down below.
226 /* Check we have a url */
227 if(!config->url_list || !config->url_list->url) {
228 helpf(global->errors, "no URL specified!\n");
229 result = CURLE_FAILED_INIT;
233 /* On WIN32 we can't set the path to curl-ca-bundle.crt
234 * at compile time. So we look here for the file in two ways:
235 * 1: look at the environment variable CURL_CA_BUNDLE for a path
236 * 2: if #1 isn't found, use the windows API function SearchPath()
237 * to find it along the app's path (includes app's dir and CWD)
239 * We support the environment variable thing for non-Windows platforms
240 * too. Just for the sake of it.
242 if(!config->cacert &&
244 !config->insecure_ok) {
246 env = curlx_getenv("CURL_CA_BUNDLE");
248 config->cacert = strdup(env);
249 if(!config->cacert) {
251 helpf(global->errors, "out of memory\n");
252 result = CURLE_OUT_OF_MEMORY;
257 env = curlx_getenv("SSL_CERT_DIR");
259 config->capath = strdup(env);
260 if(!config->capath) {
262 helpf(global->errors, "out of memory\n");
263 result = CURLE_OUT_OF_MEMORY;
268 env = curlx_getenv("SSL_CERT_FILE");
270 config->cacert = strdup(env);
271 if(!config->cacert) {
273 helpf(global->errors, "out of memory\n");
274 result = CURLE_OUT_OF_MEMORY;
285 result = FindWin32CACert(config, "curl-ca-bundle.crt");
292 if(config->postfields) {
293 if(config->use_httpget) {
294 /* Use the postfields data for a http get */
295 httpgetfields = strdup(config->postfields);
296 Curl_safefree(config->postfields);
298 helpf(global->errors, "out of memory\n");
299 result = CURLE_OUT_OF_MEMORY;
302 if(SetHTTPrequest(config,
303 (config->no_body?HTTPREQ_HEAD:HTTPREQ_GET),
305 result = CURLE_FAILED_INIT;
310 if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq)) {
311 result = CURLE_FAILED_INIT;
317 /* Single header file for all URLs */
318 if(config->headerfile) {
319 /* open file for output: */
320 if(!curlx_strequal(config->headerfile, "-")) {
321 FILE *newfile = fopen(config->headerfile, "wb");
323 warnf(config->global, "Failed to open %s\n", config->headerfile);
324 result = CURLE_WRITE_ERROR;
328 heads.filename = config->headerfile;
329 heads.s_isreg = TRUE;
330 heads.fopened = TRUE;
331 heads.stream = newfile;
335 /* always use binary mode for protocol header output */
336 set_binmode(heads.stream);
341 ** Nested loops start here.
344 /* loop through the list of given URLs */
346 for(urlnode = config->url_list; urlnode; urlnode = urlnode->next) {
348 unsigned long up; /* upload file counter within a single upload glob */
349 char *infiles; /* might be a glob pattern */
351 unsigned long infilenum;
354 int metalink = 0; /* nonzero for metalink download. */
355 metalinkfile *mlfile;
356 metalink_resource *mlres;
362 if(urlnode->flags & GETOUT_METALINK) {
364 if(mlfile_last == NULL) {
365 mlfile_last = config->metalinkfile_list;
367 mlfile = mlfile_last;
368 mlfile_last = mlfile_last->next;
369 mlres = mlfile->resource;
376 /* urlnode->url is the full URL (it might be NULL) */
379 /* This node has no URL. Free node data without destroying the
380 node itself nor modifying next pointer and continue to next */
381 Curl_safefree(urlnode->outfile);
382 Curl_safefree(urlnode->infile);
384 continue; /* next URL please */
387 /* save outfile pattern before expansion */
388 if(urlnode->outfile) {
389 outfiles = strdup(urlnode->outfile);
391 helpf(global->errors, "out of memory\n");
392 result = CURLE_OUT_OF_MEMORY;
397 infiles = urlnode->infile;
399 if(!config->globoff && infiles) {
400 /* Unless explicitly shut off */
401 result = glob_url(&inglob, infiles, &infilenum,
402 global->showerror?global->errors:NULL);
404 Curl_safefree(outfiles);
409 /* Here's the loop for uploading multiple files within the same
410 single globbed string. If no upload, we enter the loop once anyway. */
411 for(up = 0 ; up < infilenum; up++) {
413 char *uploadfile; /* a single file, never a glob */
416 unsigned long urlnum;
426 result = glob_next_url(&uploadfile, inglob);
427 if(result == CURLE_OUT_OF_MEMORY)
428 helpf(global->errors, "out of memory\n");
431 uploadfile = strdup(infiles);
433 helpf(global->errors, "out of memory\n");
434 result = CURLE_OUT_OF_MEMORY;
444 /* For Metalink download, we don't use glob. Instead we use
445 the number of resources as urlnum. */
446 urlnum = count_next_metalink_resource(mlfile);
449 if(!config->globoff) {
450 /* Unless explicitly shut off, we expand '{...}' and '[...]'
451 expressions and return total number of URLs in pattern set */
452 result = glob_url(&urls, urlnode->url, &urlnum,
453 global->showerror?global->errors:NULL);
455 Curl_safefree(uploadfile);
460 urlnum = 1; /* without globbing, this is a single URL */
462 /* if multiple files extracted to stdout, insert separators! */
463 separator= ((!outfiles || curlx_strequal(outfiles, "-")) && urlnum > 1);
465 /* Here's looping around each globbed URL */
466 for(li = 0 ; li < urlnum; li++) {
471 struct OutStruct outs;
472 struct InStruct input;
473 struct timeval retrystart;
474 curl_off_t uploadfilesize;
475 long retry_numretries;
476 long retry_sleep_default;
478 char *this_url = NULL;
479 int metalink_next_res = 0;
484 uploadfilesize = -1; /* -1 means unknown */
486 /* default output stream is stdout */
487 memset(&outs, 0, sizeof(struct OutStruct));
488 outs.stream = stdout;
489 outs.config = config;
492 /* For Metalink download, use name in Metalink file as
494 outfile = strdup(mlfile->filename);
496 result = CURLE_OUT_OF_MEMORY;
499 this_url = strdup(mlres->url);
501 result = CURLE_OUT_OF_MEMORY;
507 result = glob_next_url(&this_url, urls);
512 this_url = strdup(urlnode->url);
514 result = CURLE_OUT_OF_MEMORY;
524 outfile = strdup(outfiles);
526 result = CURLE_OUT_OF_MEMORY;
532 if(((urlnode->flags&GETOUT_USEREMOTE) ||
533 (outfile && !curlx_strequal("-", outfile))) &&
534 (metalink || !config->use_metalink)) {
537 * We have specified a file name to store the result in, or we have
538 * decided we want to use the remote file name.
542 /* extract the file name from the URL */
543 result = get_url_file_name(&outfile, this_url);
546 if(!*outfile && !config->content_disposition) {
547 helpf(global->errors, "Remote file name has no length!\n");
548 result = CURLE_WRITE_ERROR;
553 /* fill '#1' ... '#9' terms from URL pattern */
554 char *storefile = outfile;
555 result = glob_match_url(&outfile, storefile, urls);
556 Curl_safefree(storefile);
559 warnf(config->global, "bad output glob!\n");
564 /* Create the directory hierarchy, if not pre-existent to a multiple
567 if(config->create_dirs || metalink) {
568 result = create_dir_hierarchy(outfile, global->errors);
569 /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
570 if(result == CURLE_WRITE_ERROR)
577 if((urlnode->flags & GETOUT_USEREMOTE)
578 && config->content_disposition) {
579 /* Our header callback MIGHT set the filename */
580 DEBUGASSERT(!outs.filename);
583 if(config->resume_from_current) {
584 /* We're told to continue from where we are now. Get the size
585 of the file as it is now and open it for append instead */
586 struct_stat fileinfo;
587 /* VMS -- Danger, the filesize is only valid for stream files */
588 if(0 == stat(outfile, &fileinfo))
589 /* set offset to current file size: */
590 config->resume_from = fileinfo.st_size;
592 /* let offset be 0 */
593 config->resume_from = 0;
596 if(config->resume_from) {
598 /* open file for output, forcing VMS output format into stream
599 mode which is needed for stat() call above to always work. */
600 FILE *file = fopen(outfile, config->resume_from?"ab":"wb",
601 "ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0");
603 /* open file for output: */
604 FILE *file = fopen(outfile, config->resume_from?"ab":"wb");
607 helpf(global->errors, "Can't open '%s'!\n", outfile);
608 result = CURLE_WRITE_ERROR;
613 outs.init = config->resume_from;
616 outs.stream = NULL; /* open when needed */
618 outs.filename = outfile;
622 if(uploadfile && !stdin_upload(uploadfile)) {
624 * We have specified a file to upload and it isn't "-".
626 struct_stat fileinfo;
628 this_url = add_file_name_to_url(curl, this_url, uploadfile);
630 result = CURLE_OUT_OF_MEMORY;
635 * Reading binary from files can be a problem... Only FIXED, VAR
636 * etc WITHOUT implied CC will work Others need a \n appended to a
639 * - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
640 * fixed file with implied CC needs to have a byte added for every
641 * record processed, this can by derived from Filesize & recordsize
642 * for VARiable record files the records need to be counted! for
643 * every record add 1 for linefeed and subtract 2 for the record
644 * header for VARIABLE header files only the bare record data needs
645 * to be considered with one appended if implied CC
648 /* Calculate the real upload site for VMS */
650 if(stat(uploadfile, &fileinfo) == 0) {
651 fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
652 switch (fileinfo.st_fab_rfm) {
656 infd = open(uploadfile, O_RDONLY | O_BINARY);
659 infd = open(uploadfile, O_RDONLY | O_BINARY,
660 "rfm=stmlf", "ctx=stm");
665 infd = open(uploadfile, O_RDONLY | O_BINARY);
666 if((infd == -1) || fstat(infd, &fileinfo))
669 helpf(global->errors, "Can't open '%s'!\n", uploadfile);
674 result = CURLE_READ_ERROR;
679 /* we ignore file size for char/block devices, sockets, etc. */
680 if(S_ISREG(fileinfo.st_mode))
681 uploadfilesize = fileinfo.st_size;
684 else if(uploadfile && stdin_upload(uploadfile)) {
685 /* count to see if there are more than one auth bit set
686 in the authtype field */
689 while(bitcheck < 32) {
690 if(config->authtype & (1UL << bitcheck++)) {
693 /* more than one, we're done! */
700 * If the user has also selected --anyauth or --proxy-anyauth
701 * we should warn him/her.
703 if(config->proxyanyauth || (authbits>1)) {
704 warnf(config->global,
705 "Using --anyauth or --proxy-anyauth with upload from stdin"
706 " involves a big risk of it not working. Use a temporary"
707 " file or a fixed auth type instead!\n");
710 DEBUGASSERT(infdopen == FALSE);
711 DEBUGASSERT(infd == STDIN_FILENO);
714 if(curlx_strequal(uploadfile, ".")) {
715 if(curlx_nonblock((curl_socket_t)infd, TRUE) < 0)
716 warnf(config->global,
717 "fcntl failed on fd=%d: %s\n", infd, strerror(errno));
721 if(uploadfile && config->resume_from_current)
722 config->resume_from = -1; /* -1 will then force get-it-yourself */
724 if(output_expected(this_url, uploadfile) && outs.stream &&
725 isatty(fileno(outs.stream)))
726 /* we send the output to a tty, therefore we switch off the progress
728 global->noprogress = global->isatty = TRUE;
730 /* progress meter is per download, so restore config
732 global->noprogress = orig_noprogress;
733 global->isatty = orig_isatty;
736 if(urlnum > 1 && !global->mute) {
737 fprintf(global->errors, "\n[%lu/%lu]: %s --> %s\n",
738 li+1, urlnum, this_url, outfile ? outfile : "<stdout>");
740 printf("%s%s\n", CURLseparator, this_url);
744 /* Find out whether the url contains a file name */
745 const char *pc = strstr(this_url, "://");
752 pc = strrchr(pc, '/'); /* check for a slash */
755 /* there is a slash present in the URL */
758 /* Ouch, there's already a question mark in the URL string, we
759 then append the data with an ampersand separator instead! */
763 * Then append ? followed by the get fields to the url.
766 urlbuffer = aprintf("%s%c%s", this_url, sep, httpgetfields);
768 /* Append / before the ? to create a well-formed url
769 if the url contains a hostname only
771 urlbuffer = aprintf("%s/?%s", this_url, httpgetfields);
774 result = CURLE_OUT_OF_MEMORY;
778 Curl_safefree(this_url); /* free previous URL */
779 this_url = urlbuffer; /* use our new URL instead! */
783 global->errors = stderr;
785 if((!outfile || !strcmp(outfile, "-")) && !config->use_ascii) {
786 /* We get the output to stdout and we have not got the ASCII/text
787 flag, then set stdout to be binary */
791 if(config->tcp_nodelay)
792 my_setopt(curl, CURLOPT_TCP_NODELAY, 1L);
795 my_setopt(curl, CURLOPT_WRITEDATA, &outs);
796 if(metalink || !config->use_metalink)
797 /* what call to write */
798 my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
801 /* Set Metalink specific write callback function to parse
802 XML data progressively. */
803 my_setopt(curl, CURLOPT_WRITEFUNCTION, metalink_write_cb);
804 #endif /* USE_METALINK */
808 input.config = config;
809 /* Note that if CURLOPT_READFUNCTION is fread (the default), then
810 * lib/telnet.c will Curl_poll() on the input file descriptor
811 * rather then calling the READFUNCTION at regular intervals.
812 * The circumstances in which it is preferable to enable this
813 * behaviour, by omitting to set the READFUNCTION & READDATA options,
814 * have not been determined.
816 my_setopt(curl, CURLOPT_READDATA, &input);
817 /* what call to read */
818 my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
820 /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
821 CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
822 my_setopt(curl, CURLOPT_SEEKDATA, &input);
823 my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
825 if(config->recvpersecond)
826 /* tell libcurl to use a smaller sized buffer as it allows us to
827 make better sleeps! 7.9.9 stuff! */
828 my_setopt(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond);
830 /* size of uploaded file: */
831 if(uploadfilesize != -1)
832 my_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
833 my_setopt_str(curl, CURLOPT_URL, this_url); /* what to fetch */
834 my_setopt(curl, CURLOPT_NOPROGRESS, global->noprogress?1L:0L);
835 if(config->no_body) {
836 my_setopt(curl, CURLOPT_NOBODY, 1L);
837 my_setopt(curl, CURLOPT_HEADER, 1L);
839 /* If --metalink is used, we ignore --include (headers in
840 output) option because mixing headers to the body will
841 confuse XML parser and/or hash check will fail. */
842 else if(!config->use_metalink)
843 my_setopt(curl, CURLOPT_HEADER, config->include_headers?1L:0L);
845 if(config->oauth_bearer)
846 my_setopt_str(curl, CURLOPT_XOAUTH2_BEARER, config->oauth_bearer);
848 #if !defined(CURL_DISABLE_PROXY)
850 /* TODO: Make this a run-time check instead of compile-time one. */
852 my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
853 my_setopt_str(curl, CURLOPT_PROXYUSERPWD, config->proxyuserpwd);
855 /* new in libcurl 7.3 */
856 my_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config->proxytunnel?1L:0L);
858 /* new in libcurl 7.5 */
860 my_setopt_enum(curl, CURLOPT_PROXYTYPE, (long)config->proxyver);
862 /* new in libcurl 7.10 */
863 if(config->socksproxy) {
864 my_setopt_str(curl, CURLOPT_PROXY, config->socksproxy);
865 my_setopt_enum(curl, CURLOPT_PROXYTYPE, (long)config->socksver);
868 /* new in libcurl 7.10.6 */
869 if(config->proxyanyauth)
870 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
872 else if(config->proxynegotiate)
873 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
874 (long)CURLAUTH_GSSNEGOTIATE);
875 else if(config->proxyntlm)
876 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
877 (long)CURLAUTH_NTLM);
878 else if(config->proxydigest)
879 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
880 (long)CURLAUTH_DIGEST);
881 else if(config->proxybasic)
882 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
883 (long)CURLAUTH_BASIC);
885 /* new in libcurl 7.19.4 */
886 my_setopt_str(curl, CURLOPT_NOPROXY, config->noproxy);
890 my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror?1L:0L);
891 my_setopt(curl, CURLOPT_UPLOAD, uploadfile?1L:0L);
892 my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly?1L:0L);
893 my_setopt(curl, CURLOPT_APPEND, config->ftp_append?1L:0L);
895 if(config->netrc_opt)
896 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_OPTIONAL);
897 else if(config->netrc || config->netrc_file)
898 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_REQUIRED);
900 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_IGNORED);
902 if(config->netrc_file)
903 my_setopt_str(curl, CURLOPT_NETRC_FILE, config->netrc_file);
905 my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii?1L:0L);
906 if(config->login_options)
907 my_setopt_str(curl, CURLOPT_LOGIN_OPTIONS, config->login_options);
908 my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
909 my_setopt_str(curl, CURLOPT_RANGE, config->range);
910 my_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
911 my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000));
913 if(built_in_protos & CURLPROTO_HTTP) {
917 my_setopt(curl, CURLOPT_FOLLOWLOCATION,
918 config->followlocation?1L:0L);
919 my_setopt(curl, CURLOPT_UNRESTRICTED_AUTH,
920 config->unrestricted_auth?1L:0L);
922 switch(config->httpreq) {
923 case HTTPREQ_SIMPLEPOST:
924 my_setopt_str(curl, CURLOPT_POSTFIELDS,
926 my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
927 config->postfieldsize);
929 case HTTPREQ_FORMPOST:
930 my_setopt_httppost(curl, CURLOPT_HTTPPOST, config->httppost);
936 my_setopt_str(curl, CURLOPT_REFERER, config->referer);
937 my_setopt(curl, CURLOPT_AUTOREFERER, config->autoreferer?1L:0L);
938 my_setopt_str(curl, CURLOPT_USERAGENT, config->useragent);
939 my_setopt_slist(curl, CURLOPT_HTTPHEADER, config->headers);
941 /* new in libcurl 7.36.0 */
942 if(config->proxyheaders) {
943 my_setopt_slist(curl, CURLOPT_PROXYHEADER, config->proxyheaders);
944 my_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE);
947 /* new in libcurl 7.5 */
948 my_setopt(curl, CURLOPT_MAXREDIRS, config->maxredirs);
950 if(config->httpversion)
951 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion);
952 else if(curlinfo->features & CURL_VERSION_HTTP2) {
953 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
956 /* new in libcurl 7.10.6 (default is Basic) */
958 my_setopt_bitmask(curl, CURLOPT_HTTPAUTH, (long)config->authtype);
960 /* curl 7.19.1 (the 301 version existed in 7.18.2),
961 303 was added in 7.26.0 */
963 postRedir |= CURL_REDIR_POST_301;
965 postRedir |= CURL_REDIR_POST_302;
967 postRedir |= CURL_REDIR_POST_303;
968 my_setopt(curl, CURLOPT_POSTREDIR, postRedir);
970 /* new in libcurl 7.21.6 */
972 my_setopt_str(curl, CURLOPT_ACCEPT_ENCODING, "");
974 /* new in libcurl 7.21.6 */
975 if(config->tr_encoding)
976 my_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1L);
978 } /* (built_in_protos & CURLPROTO_HTTP) */
980 my_setopt_str(curl, CURLOPT_FTPPORT, config->ftpport);
981 my_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,
982 config->low_speed_limit);
983 my_setopt(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
984 my_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE,
985 config->sendpersecond);
986 my_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE,
987 config->recvpersecond);
989 if(config->use_resume)
990 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, config->resume_from);
992 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, CURL_OFF_T_C(0));
994 my_setopt_str(curl, CURLOPT_KEYPASSWD, config->key_passwd);
996 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
998 /* SSH and SSL private key uses same command-line option */
999 /* new in libcurl 7.16.1 */
1000 my_setopt_str(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key);
1001 /* new in libcurl 7.16.1 */
1002 my_setopt_str(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey);
1004 /* new in libcurl 7.17.1: SSH host key md5 checking allows us
1005 to fail if we are not talking to who we think we should */
1006 my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,
1007 config->hostpubmd5);
1011 my_setopt_str(curl, CURLOPT_CAINFO, config->cacert);
1013 my_setopt_str(curl, CURLOPT_CAPATH, config->capath);
1015 my_setopt_str(curl, CURLOPT_CRLFILE, config->crlfile);
1017 if(config->pinnedpubkey)
1018 my_setopt_str(curl, CURLOPT_PINNEDPUBLICKEY, config->pinnedpubkey);
1020 if(curlinfo->features & CURL_VERSION_SSL) {
1021 my_setopt_str(curl, CURLOPT_SSLCERT, config->cert);
1022 my_setopt_str(curl, CURLOPT_SSLCERTTYPE, config->cert_type);
1023 my_setopt_str(curl, CURLOPT_SSLKEY, config->key);
1024 my_setopt_str(curl, CURLOPT_SSLKEYTYPE, config->key_type);
1026 if(config->insecure_ok) {
1027 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
1028 my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
1031 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1032 /* libcurl default is strict verifyhost -> 2L */
1033 /* my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); */
1036 if(config->verifystatus)
1037 my_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1L);
1039 if(config->falsestart)
1040 my_setopt(curl, CURLOPT_SSL_FALSESTART, 1L);
1042 my_setopt_enum(curl, CURLOPT_SSLVERSION, config->ssl_version);
1044 if(config->path_as_is)
1045 my_setopt(curl, CURLOPT_PATH_AS_IS, 1L);
1047 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1048 if(!config->insecure_ok) {
1051 result = CURLE_OUT_OF_MEMORY;
1054 file = aprintf("%s/%sssh/known_hosts", home, DOT_CHAR);
1056 /* new in curl 7.19.6 */
1057 result = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file);
1059 if(result == CURLE_UNKNOWN_OPTION)
1060 /* libssh2 version older than 1.1.1 */
1063 Curl_safefree(home);
1070 if(config->no_body || config->remote_time) {
1071 /* no body or use remote time */
1072 my_setopt(curl, CURLOPT_FILETIME, 1L);
1075 my_setopt(curl, CURLOPT_CRLF, config->crlf?1L:0L);
1076 my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
1077 my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
1078 my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
1080 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
1082 my_setopt_str(curl, CURLOPT_COOKIE, config->cookie);
1084 if(config->cookiefile)
1085 my_setopt_str(curl, CURLOPT_COOKIEFILE, config->cookiefile);
1087 /* new in libcurl 7.9 */
1088 if(config->cookiejar)
1089 my_setopt_str(curl, CURLOPT_COOKIEJAR, config->cookiejar);
1091 /* new in libcurl 7.9.7 */
1092 my_setopt(curl, CURLOPT_COOKIESESSION, config->cookiesession?1L:0L);
1094 if(config->cookie || config->cookiefile || config->cookiejar) {
1095 warnf(config->global, "cookie option(s) used even though cookie "
1096 "support is disabled!\n");
1097 return CURLE_NOT_BUILT_IN;
1101 my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond);
1102 my_setopt(curl, CURLOPT_TIMEVALUE, (long)config->condtime);
1103 my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
1104 customrequest_helper(config, config->httpreq, config->customrequest);
1105 my_setopt(curl, CURLOPT_STDERR, global->errors);
1107 /* three new ones in libcurl 7.3: */
1108 my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
1109 my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
1111 progressbarinit(&progressbar, config);
1112 if((global->progressmode == CURL_PROGRESS_BAR) &&
1113 !global->noprogress && !global->mute) {
1114 /* we want the alternative style, then we have to implement it
1116 my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb);
1117 my_setopt(curl, CURLOPT_XFERINFODATA, &progressbar);
1120 /* new in libcurl 7.24.0: */
1121 if(config->dns_servers)
1122 my_setopt_str(curl, CURLOPT_DNS_SERVERS, config->dns_servers);
1124 /* new in libcurl 7.33.0: */
1125 if(config->dns_interface)
1126 my_setopt_str(curl, CURLOPT_DNS_INTERFACE, config->dns_interface);
1127 if(config->dns_ipv4_addr)
1128 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP4, config->dns_ipv4_addr);
1129 if(config->dns_ipv6_addr)
1130 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP6, config->dns_ipv6_addr);
1132 /* new in libcurl 7.6.2: */
1133 my_setopt_slist(curl, CURLOPT_TELNETOPTIONS, config->telnet_options);
1135 /* new in libcurl 7.7: */
1136 my_setopt_str(curl, CURLOPT_RANDOM_FILE, config->random_file);
1137 my_setopt_str(curl, CURLOPT_EGDSOCKET, config->egd_file);
1138 my_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
1139 (long)(config->connecttimeout * 1000));
1141 if(config->cipher_list)
1142 my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
1144 /* new in libcurl 7.9.2: */
1145 if(config->disable_epsv)
1147 my_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);
1149 /* new in libcurl 7.10.5 */
1150 if(config->disable_eprt)
1152 my_setopt(curl, CURLOPT_FTP_USE_EPRT, 0L);
1154 if(global->tracetype != TRACE_NONE) {
1155 my_setopt(curl, CURLOPT_DEBUGFUNCTION, tool_debug_cb);
1156 my_setopt(curl, CURLOPT_DEBUGDATA, config);
1157 my_setopt(curl, CURLOPT_VERBOSE, 1L);
1160 /* new in curl 7.9.3 */
1161 if(config->engine) {
1162 result = res_setopt_str(curl, CURLOPT_SSLENGINE, config->engine);
1165 my_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
1168 /* new in curl 7.10.7, extended in 7.19.4 but this only sets 0 or 1 */
1169 my_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
1170 config->ftp_create_dirs?1L:0L);
1172 /* new in curl 7.10.8 */
1173 if(config->max_filesize)
1174 my_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
1175 config->max_filesize);
1177 if(4 == config->ip_version)
1178 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1179 else if(6 == config->ip_version)
1180 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
1182 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER);
1184 /* new in curl 7.15.5 */
1185 if(config->ftp_ssl_reqd)
1186 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
1188 /* new in curl 7.11.0 */
1189 else if(config->ftp_ssl)
1190 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
1192 /* new in curl 7.16.0 */
1193 else if(config->ftp_ssl_control)
1194 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_CONTROL);
1196 /* new in curl 7.16.1 */
1197 if(config->ftp_ssl_ccc)
1198 my_setopt_enum(curl, CURLOPT_FTP_SSL_CCC,
1199 (long)config->ftp_ssl_ccc_mode);
1201 /* new in curl 7.19.4 */
1202 if(config->socks5_gssapi_service)
1203 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_SERVICE,
1204 config->socks5_gssapi_service);
1206 /* new in curl 7.19.4 */
1207 if(config->socks5_gssapi_nec)
1208 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_NEC,
1209 config->socks5_gssapi_nec);
1211 /* new in curl 7.43.0 */
1212 if(config->proxy_service_name)
1213 my_setopt_str(curl, CURLOPT_PROXY_SERVICE_NAME,
1214 config->proxy_service_name);
1216 /* new in curl 7.43.0 */
1217 if(config->service_name)
1218 my_setopt_str(curl, CURLOPT_SERVICE_NAME,
1219 config->service_name);
1222 my_setopt_str(curl, CURLOPT_FTP_ACCOUNT, config->ftp_account);
1224 my_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, config->ignorecl?1L:0L);
1227 my_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, config->ftp_skip_ip?1L:0L);
1230 my_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long)config->ftp_filemethod);
1233 if(config->localport) {
1234 my_setopt(curl, CURLOPT_LOCALPORT, (long)config->localport);
1235 my_setopt_str(curl, CURLOPT_LOCALPORTRANGE,
1236 (long)config->localportrange);
1240 my_setopt_str(curl, CURLOPT_FTP_ALTERNATIVE_TO_USER,
1241 config->ftp_alternative_to_user);
1244 if(config->disable_sessionid)
1246 my_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0L);
1250 my_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 0L);
1251 my_setopt(curl, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
1255 if(!config->nokeepalive) {
1256 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
1257 if(config->alivetime != 0) {
1258 my_setopt(curl, CURLOPT_TCP_KEEPIDLE, config->alivetime);
1259 my_setopt(curl, CURLOPT_TCP_KEEPINTVL, config->alivetime);
1263 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
1266 if(config->tftp_blksize)
1267 my_setopt(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
1269 if(config->mail_from)
1270 my_setopt_str(curl, CURLOPT_MAIL_FROM, config->mail_from);
1272 if(config->mail_rcpt)
1273 my_setopt_slist(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
1276 if(config->ftp_pret)
1277 my_setopt(curl, CURLOPT_FTP_USE_PRET, 1L);
1279 if(config->proto_present)
1280 my_setopt_flags(curl, CURLOPT_PROTOCOLS, config->proto);
1281 if(config->proto_redir_present)
1282 my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
1284 if(config->content_disposition
1285 && (urlnode->flags & GETOUT_USEREMOTE)
1286 && (checkprefix("http://", this_url) ||
1287 checkprefix("https://", this_url)))
1288 hdrcbdata.honor_cd_filename = TRUE;
1290 hdrcbdata.honor_cd_filename = FALSE;
1292 hdrcbdata.outs = &outs;
1293 hdrcbdata.heads = &heads;
1295 my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
1296 my_setopt(curl, CURLOPT_HEADERDATA, &hdrcbdata);
1300 my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
1303 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1304 if(config->tls_username)
1305 my_setopt_str(curl, CURLOPT_TLSAUTH_USERNAME,
1306 config->tls_username);
1307 if(config->tls_password)
1308 my_setopt_str(curl, CURLOPT_TLSAUTH_PASSWORD,
1309 config->tls_password);
1310 if(config->tls_authtype)
1311 my_setopt_str(curl, CURLOPT_TLSAUTH_TYPE,
1312 config->tls_authtype);
1316 if(config->gssapi_delegation)
1317 my_setopt_str(curl, CURLOPT_GSSAPI_DELEGATION,
1318 config->gssapi_delegation);
1320 /* new in 7.25.0 and 7.44.0 */
1322 long mask = (config->ssl_allow_beast ? CURLSSLOPT_ALLOW_BEAST : 0) |
1323 (config->ssl_no_revoke ? CURLSSLOPT_NO_REVOKE : 0);
1325 my_setopt_bitmask(curl, CURLOPT_SSL_OPTIONS, mask);
1328 if(config->mail_auth)
1329 my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
1333 my_setopt(curl, CURLOPT_SASL_IR, 1L);
1336 my_setopt(curl, CURLOPT_SSL_ENABLE_NPN, 0L);
1339 if(config->noalpn) {
1340 my_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L);
1344 if(config->unix_socket_path)
1345 my_setopt_str(curl, CURLOPT_UNIX_SOCKET_PATH,
1346 config->unix_socket_path);
1349 if(config->proto_default)
1350 my_setopt_str(curl, CURLOPT_DEFAULT_PROTOCOL, config->proto_default);
1353 if(config->expect100timeout > 0)
1354 my_setopt_str(curl, CURLOPT_EXPECT_100_TIMEOUT_MS,
1355 (long)(config->expect100timeout*1000));
1358 if(config->tftp_no_options)
1359 my_setopt(curl, CURLOPT_TFTP_NO_OPTIONS, 1L);
1361 /* initialize retry vars for loop below */
1362 retry_sleep_default = (config->retry_delay) ?
1363 config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
1365 retry_numretries = config->req_retry;
1366 retry_sleep = retry_sleep_default; /* ms */
1367 retrystart = tvnow();
1369 #ifndef CURL_DISABLE_LIBCURL_OPTION
1370 if(global->libcurl) {
1371 result = easysrc_perform();
1379 if(!metalink && config->use_metalink) {
1380 /* If outs.metalink_parser is non-NULL, delete it first. */
1381 if(outs.metalink_parser)
1382 metalink_parser_context_delete(outs.metalink_parser);
1383 outs.metalink_parser = metalink_parser_context_new();
1384 if(outs.metalink_parser == NULL) {
1385 result = CURLE_OUT_OF_MEMORY;
1388 fprintf(config->global->errors,
1389 "Metalink: parsing (%s) metalink/XML...\n", this_url);
1392 fprintf(config->global->errors,
1393 "Metalink: fetching (%s) from (%s)...\n",
1394 mlfile->filename, this_url);
1395 #endif /* USE_METALINK */
1398 if(config->test_event_based)
1399 result = curl_easy_perform_ev(curl);
1402 result = curl_easy_perform(curl);
1404 if(!result && !outs.stream && !outs.bytes) {
1405 /* we have received no data despite the transfer was successful
1406 ==> force cration of an empty output file (if an output file
1408 long cond_unmet = 0L;
1409 /* do not create (or even overwrite) the file in case we get no
1410 data because of unmet condition */
1411 curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &cond_unmet);
1412 if(!cond_unmet && !tool_create_output_file(&outs))
1413 result = CURLE_WRITE_ERROR;
1416 if(outs.is_cd_filename && outs.stream && !global->mute &&
1418 printf("curl: Saved to filename '%s'\n", outs.filename);
1420 /* if retry-max-time is non-zero, make sure we haven't exceeded the
1422 if(retry_numretries &&
1423 (!config->retry_maxtime ||
1424 (tvdiff(tvnow(), retrystart) <
1425 config->retry_maxtime*1000L)) ) {
1431 RETRY_LAST /* not used */
1434 if((CURLE_OPERATION_TIMEDOUT == result) ||
1435 (CURLE_COULDNT_RESOLVE_HOST == result) ||
1436 (CURLE_COULDNT_RESOLVE_PROXY == result) ||
1437 (CURLE_FTP_ACCEPT_TIMEOUT == result))
1438 /* retry timeout always */
1439 retry = RETRY_TIMEOUT;
1440 else if((CURLE_OK == result) ||
1441 (config->failonerror &&
1442 (CURLE_HTTP_RETURNED_ERROR == result))) {
1443 /* If it returned OK. _or_ failonerror was enabled and it
1444 returned due to such an error, check for HTTP transient
1445 errors to retry on. */
1446 char *effective_url = NULL;
1447 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1449 checkprefix("http", effective_url)) {
1450 /* This was HTTP(S) */
1451 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1454 case 500: /* Internal Server Error */
1455 case 502: /* Bad Gateway */
1456 case 503: /* Service Unavailable */
1457 case 504: /* Gateway Timeout */
1460 * At this point, we have already written data to the output
1461 * file (or terminal). If we write to a file, we must rewind
1462 * or close/re-open the file so that the next attempt starts
1463 * over from the beginning.
1465 * TODO: similar action for the upload case. We might need
1466 * to start over reading from a previous point if we have
1467 * uploaded something when this was returned.
1474 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1476 if(response/100 == 4)
1478 * This is typically when the FTP server only allows a certain
1479 * amount of users and we are not one of them. All 4xx codes
1486 static const char * const m[]={
1487 NULL, "timeout", "HTTP error", "FTP error"
1490 warnf(config->global, "Transient problem: %s "
1491 "Will retry in %ld seconds. "
1492 "%ld retries left.\n",
1493 m[retry], retry_sleep/1000L, retry_numretries);
1495 tool_go_sleep(retry_sleep);
1497 if(!config->retry_delay) {
1499 if(retry_sleep > RETRY_SLEEP_MAX)
1500 retry_sleep = RETRY_SLEEP_MAX;
1502 if(outs.bytes && outs.filename && outs.stream) {
1503 /* We have written data to a output file, we truncate file
1506 fprintf(global->errors, "Throwing away %"
1507 CURL_FORMAT_CURL_OFF_T " bytes\n",
1509 fflush(outs.stream);
1510 /* truncate file at the position where we started appending */
1511 #ifdef HAVE_FTRUNCATE
1512 if(ftruncate( fileno(outs.stream), outs.init)) {
1513 /* when truncate fails, we can't just append as then we'll
1514 create something strange, bail out */
1516 fprintf(global->errors,
1517 "failed to truncate, exiting\n");
1518 result = CURLE_WRITE_ERROR;
1521 /* now seek to the end of the file, the position where we
1522 just truncated the file in a large file-safe way */
1523 fseek(outs.stream, 0, SEEK_END);
1525 /* ftruncate is not available, so just reposition the file
1526 to the location we would have truncated it. This won't
1527 work properly with large files on 32-bit systems, but
1528 most of those will have ftruncate. */
1529 fseek(outs.stream, (long)outs.init, SEEK_SET);
1531 outs.bytes = 0; /* clear for next round */
1533 continue; /* curl_easy_perform loop */
1535 } /* if retry_numretries */
1537 /* Metalink: Decide to try the next resource or
1538 not. Basically, we want to try the next resource if
1539 download was not successful. */
1541 if(CURLE_OK == result) {
1542 /* TODO We want to try next resource when download was
1543 not successful. How to know that? */
1544 char *effective_url = NULL;
1545 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1547 curlx_strnequal(effective_url, "http", 4)) {
1548 /* This was HTTP(S) */
1549 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1550 if(response != 200 && response != 206) {
1551 metalink_next_res = 1;
1552 fprintf(global->errors,
1553 "Metalink: fetching (%s) from (%s) FAILED "
1554 "(HTTP status code %d)\n",
1555 mlfile->filename, this_url, response);
1560 metalink_next_res = 1;
1561 fprintf(global->errors,
1562 "Metalink: fetching (%s) from (%s) FAILED (%s)\n",
1563 mlfile->filename, this_url,
1565 errorbuffer : curl_easy_strerror(result));
1568 if(metalink && !metalink_next_res)
1569 fprintf(global->errors, "Metalink: fetching (%s) from (%s) OK\n",
1570 mlfile->filename, this_url);
1572 /* In all ordinary cases, just break out of loop here */
1573 break; /* curl_easy_perform loop */
1577 if((global->progressmode == CURL_PROGRESS_BAR) &&
1579 /* if the custom progress bar has been displayed, we output a
1581 fputs("\n", progressbar.out);
1583 if(config->writeout)
1584 ourWriteOut(curl, &outs, config->writeout);
1586 if(config->writeenv)
1590 ** Code within this loop may jump directly here to label 'show_error'
1591 ** in order to display an error message for CURLcode stored in 'res'
1592 ** variable and exit loop once that necessary writing and cleanup
1593 ** in label 'quit_urls' has been done.
1599 if(is_vms_shell()) {
1600 /* VMS DCL shell behavior */
1601 if(!global->showerror)
1602 vms_show = VMSSTS_HIDE;
1606 if(result && global->showerror) {
1607 fprintf(global->errors, "curl: (%d) %s\n", result, (errorbuffer[0]) ?
1608 errorbuffer : curl_easy_strerror(result));
1609 if(result == CURLE_SSL_CACERT)
1610 fprintf(global->errors, "%s%s",
1611 CURL_CA_CERT_ERRORMSG1, CURL_CA_CERT_ERRORMSG2);
1614 /* Fall through comment to 'quit_urls' label */
1617 ** Upon error condition and always that a message has already been
1618 ** displayed, code within this loop may jump directly here to label
1619 ** 'quit_urls' otherwise it should jump to 'show_error' label above.
1621 ** When 'res' variable is _not_ CURLE_OK loop will exit once that
1622 ** all code following 'quit_urls' has been executed. Otherwise it
1623 ** will loop to the beginning from where it may exit if there are
1624 ** no more urls left.
1629 /* Set file extended attributes */
1630 if(!result && config->xattr && outs.fopened && outs.stream) {
1631 int rc = fwrite_xattr(curl, fileno(outs.stream));
1633 warnf(config->global, "Error setting extended attributes: %s\n",
1637 /* Close the file */
1638 if(outs.fopened && outs.stream) {
1639 int rc = fclose(outs.stream);
1641 /* something went wrong in the writing process */
1642 result = CURLE_WRITE_ERROR;
1643 fprintf(global->errors, "(%d) Failed writing body\n", result);
1646 else if(!outs.s_isreg && outs.stream) {
1647 /* Dump standard stream buffered data */
1648 int rc = fflush(outs.stream);
1650 /* something went wrong in the writing process */
1651 result = CURLE_WRITE_ERROR;
1652 fprintf(global->errors, "(%d) Failed writing body\n", result);
1657 if(!result && outs.s_isreg && outs.filename) {
1658 /* Set the url (up to 80 chars) as comment for the file */
1659 if(strlen(url) > 78)
1661 SetComment(outs.filename, url);
1666 /* File time can only be set _after_ the file has been closed */
1667 if(!result && config->remote_time && outs.s_isreg && outs.filename) {
1668 /* Ask libcurl if we got a remote file time */
1670 curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
1672 struct utimbuf times;
1673 times.actime = (time_t)filetime;
1674 times.modtime = (time_t)filetime;
1675 utime(outs.filename, ×); /* set the time we got */
1681 if(!metalink && config->use_metalink && result == CURLE_OK) {
1682 int rv = parse_metalink(config, &outs, this_url);
1684 fprintf(config->global->errors, "Metalink: parsing (%s) OK\n",
1687 fprintf(config->global->errors, "Metalink: parsing (%s) FAILED\n",
1690 else if(metalink && result == CURLE_OK && !metalink_next_res) {
1691 int rv = metalink_check_hash(global, mlfile, outs.filename);
1693 metalink_next_res = 1;
1696 #endif /* USE_METALINK */
1698 /* No more business with this output struct */
1699 if(outs.alloc_filename)
1700 Curl_safefree(outs.filename);
1702 if(outs.metalink_parser)
1703 metalink_parser_context_delete(outs.metalink_parser);
1704 #endif /* USE_METALINK */
1705 memset(&outs, 0, sizeof(struct OutStruct));
1706 hdrcbdata.outs = NULL;
1708 /* Free loop-local allocated memory and close loop-local opened fd */
1710 Curl_safefree(outfile);
1711 Curl_safefree(this_url);
1717 /* Should exit if error is fatal. */
1718 if(is_fatal_error(result)) {
1721 if(!metalink_next_res)
1723 mlres = mlres->next;
1725 /* TODO If metalink_next_res is 1 and mlres is NULL,
1726 * set res to error code
1732 /* when url globbing, exit loop upon critical error */
1733 if(is_fatal_error(result))
1737 /* when not url globbing, exit loop upon any error */
1740 } /* loop to the next URL */
1742 /* Free loop-local allocated memory */
1744 Curl_safefree(uploadfile);
1747 /* Free list of remaining URLs */
1753 /* when file globbing, exit loop upon critical error */
1754 if(is_fatal_error(result))
1758 /* when not file globbing, exit loop upon any error */
1761 } /* loop to the next globbed upload file */
1763 /* Free loop-local allocated memory */
1765 Curl_safefree(outfiles);
1768 /* Free list of globbed upload files */
1769 glob_cleanup(inglob);
1773 /* Free this URL node data without destroying the
1774 the node itself nor modifying next pointer. */
1775 Curl_safefree(urlnode->url);
1776 Curl_safefree(urlnode->outfile);
1777 Curl_safefree(urlnode->infile);
1781 ** Bail out upon critical errors
1783 if(is_fatal_error(result))
1786 } /* for-loop through all URLs */
1789 ** Nested loops end here.
1794 /* Reset the global config variables */
1795 global->noprogress = orig_noprogress;
1796 global->isatty = orig_isatty;
1798 /* Free function-local referenced allocated memory */
1799 Curl_safefree(httpgetfields);
1801 /* Free list of given URLs */
1802 clean_getout(config);
1804 hdrcbdata.heads = NULL;
1806 /* Close function-local opened file descriptors */
1807 if(heads.fopened && heads.stream)
1808 fclose(heads.stream);
1810 if(heads.alloc_filename)
1811 Curl_safefree(heads.filename);
1813 /* Release metalink related resources here */
1814 clean_metalink(config);
1819 CURLcode operate(struct GlobalConfig *config, int argc, argv_item_t argv[])
1821 CURLcode result = CURLE_OK;
1823 /* Setup proper locale from environment */
1824 #ifdef HAVE_SETLOCALE
1825 setlocale(LC_ALL, "");
1828 /* Parse .curlrc if necessary */
1829 if((argc == 1) || (!curlx_strequal(argv[1], "-q"))) {
1830 parseconfig(NULL, config); /* ignore possible failure */
1832 /* If we had no arguments then make sure a url was specified in .curlrc */
1833 if((argc < 2) && (!config->first->url_list)) {
1834 helpf(config->errors, NULL);
1835 result = CURLE_FAILED_INIT;
1840 /* Parse the command line arguments */
1841 ParameterError res = parse_args(config, argc, argv);
1845 /* Check if we were asked for the help */
1846 if(res == PARAM_HELP_REQUESTED)
1848 /* Check if we were asked for the manual */
1849 else if(res == PARAM_MANUAL_REQUESTED)
1851 /* Check if we were asked for the version information */
1852 else if(res == PARAM_VERSION_INFO_REQUESTED)
1853 tool_version_info();
1854 /* Check if we were asked to list the SSL engines */
1855 else if(res == PARAM_ENGINES_REQUESTED)
1856 tool_list_engines(config->easy);
1857 else if(res == PARAM_LIBCURL_UNSUPPORTED_PROTOCOL)
1858 result = CURLE_UNSUPPORTED_PROTOCOL;
1860 result = CURLE_FAILED_INIT;
1863 #ifndef CURL_DISABLE_LIBCURL_OPTION
1864 if(config->libcurl) {
1865 /* Initialise the libcurl source output */
1866 result = easysrc_init();
1870 /* Perform the main operations */
1873 struct OperationConfig *operation = config->first;
1875 /* Get the required aguments for each operation */
1876 while(!result && operation) {
1877 result = get_args(operation, count++);
1879 operation = operation->next;
1882 /* Set the current operation pointer */
1883 config->current = config->first;
1885 /* Perform each operation */
1886 while(!result && config->current) {
1887 result = operate_do(config, config->current);
1889 config->current = config->current->next;
1892 #ifndef CURL_DISABLE_LIBCURL_OPTION
1893 if(config->libcurl) {
1894 /* Cleanup the libcurl source output */
1897 /* Dump the libcurl code if previously enabled */
1898 dumpeasysrc(config);
1903 helpf(config->errors, "out of memory\n");