1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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: http://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");
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;
208 bool orig_noprogress;
211 errorbuffer[0] = '\0';
213 /* default headers output stream is stdout */
214 memset(&hdrcbdata, 0, sizeof(struct HdrCbData));
215 memset(&heads, 0, sizeof(struct OutStruct));
216 heads.stream = stdout;
217 heads.config = config;
220 ** Beyond this point no return'ing from this function allowed.
221 ** Jump to label 'quit_curl' in order to abandon this function
222 ** from outside of nested loops further down below.
225 /* Check we have a url */
226 if(!config->url_list || !config->url_list->url) {
227 helpf(global->errors, "no URL specified!\n");
228 res = CURLE_FAILED_INIT;
232 /* On WIN32 we can't set the path to curl-ca-bundle.crt
233 * at compile time. So we look here for the file in two ways:
234 * 1: look at the environment variable CURL_CA_BUNDLE for a path
235 * 2: if #1 isn't found, use the windows API function SearchPath()
236 * to find it along the app's path (includes app's dir and CWD)
238 * We support the environment variable thing for non-Windows platforms
239 * too. Just for the sake of it.
241 if(!config->cacert &&
243 !config->insecure_ok) {
245 env = curlx_getenv("CURL_CA_BUNDLE");
247 config->cacert = strdup(env);
248 if(!config->cacert) {
250 helpf(global->errors, "out of memory\n");
251 res = CURLE_OUT_OF_MEMORY;
256 env = curlx_getenv("SSL_CERT_DIR");
258 config->capath = strdup(env);
259 if(!config->capath) {
261 helpf(global->errors, "out of memory\n");
262 res = CURLE_OUT_OF_MEMORY;
267 env = curlx_getenv("SSL_CERT_FILE");
269 config->cacert = strdup(env);
270 if(!config->cacert) {
272 helpf(global->errors, "out of memory\n");
273 res = CURLE_OUT_OF_MEMORY;
284 res = FindWin32CACert(config, "curl-ca-bundle.crt");
291 if(config->postfields) {
292 if(config->use_httpget) {
293 /* Use the postfields data for a http get */
294 httpgetfields = strdup(config->postfields);
295 Curl_safefree(config->postfields);
297 helpf(global->errors, "out of memory\n");
298 res = CURLE_OUT_OF_MEMORY;
301 if(SetHTTPrequest(config,
302 (config->no_body?HTTPREQ_HEAD:HTTPREQ_GET),
304 res = CURLE_FAILED_INIT;
309 if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq)) {
310 res = CURLE_FAILED_INIT;
316 #ifndef CURL_DISABLE_LIBCURL_OPTION
317 res = easysrc_init();
319 helpf(global->errors, "out of memory\n");
324 /* Single header file for all URLs */
325 if(config->headerfile) {
326 /* open file for output: */
327 if(!curlx_strequal(config->headerfile, "-")) {
328 FILE *newfile = fopen(config->headerfile, "wb");
330 warnf(config, "Failed to open %s\n", config->headerfile);
331 res = CURLE_WRITE_ERROR;
335 heads.filename = config->headerfile;
336 heads.s_isreg = TRUE;
337 heads.fopened = TRUE;
338 heads.stream = newfile;
342 /* always use binary mode for protocol header output */
343 set_binmode(heads.stream);
347 /* save the values of noprogress and isatty to restore them later on */
348 orig_noprogress = config->noprogress;
349 orig_isatty = config->isatty;
352 ** Nested loops start here.
355 /* loop through the list of given URLs */
357 for(urlnode = config->url_list; urlnode; urlnode = urlnode->next) {
359 unsigned long up; /* upload file counter within a single upload glob */
360 char *infiles; /* might be a glob pattern */
362 unsigned long infilenum;
365 int metalink = 0; /* nonzero for metalink download. */
366 metalinkfile *mlfile;
367 metalink_resource *mlres;
373 if(urlnode->flags & GETOUT_METALINK) {
375 if(mlfile_last == NULL) {
376 mlfile_last = config->metalinkfile_list;
378 mlfile = mlfile_last;
379 mlfile_last = mlfile_last->next;
380 mlres = mlfile->resource;
387 /* urlnode->url is the full URL (it might be NULL) */
390 /* This node has no URL. Free node data without destroying the
391 node itself nor modifying next pointer and continue to next */
392 Curl_safefree(urlnode->outfile);
393 Curl_safefree(urlnode->infile);
395 continue; /* next URL please */
398 /* save outfile pattern before expansion */
399 if(urlnode->outfile) {
400 outfiles = strdup(urlnode->outfile);
402 helpf(global->errors, "out of memory\n");
403 res = CURLE_OUT_OF_MEMORY;
408 infiles = urlnode->infile;
410 if(!config->globoff && infiles) {
411 /* Unless explicitly shut off */
412 res = glob_url(&inglob, infiles, &infilenum,
413 global->showerror?global->errors:NULL);
415 Curl_safefree(outfiles);
420 /* Here's the loop for uploading multiple files within the same
421 single globbed string. If no upload, we enter the loop once anyway. */
422 for(up = 0 ; up < infilenum; up++) {
424 char *uploadfile; /* a single file, never a glob */
427 unsigned long urlnum;
437 res = glob_next_url(&uploadfile, inglob);
438 if(res == CURLE_OUT_OF_MEMORY)
439 helpf(global->errors, "out of memory\n");
442 uploadfile = strdup(infiles);
444 helpf(global->errors, "out of memory\n");
445 res = CURLE_OUT_OF_MEMORY;
455 /* For Metalink download, we don't use glob. Instead we use
456 the number of resources as urlnum. */
457 urlnum = count_next_metalink_resource(mlfile);
460 if(!config->globoff) {
461 /* Unless explicitly shut off, we expand '{...}' and '[...]'
462 expressions and return total number of URLs in pattern set */
463 res = glob_url(&urls, urlnode->url, &urlnum,
464 global->showerror?global->errors:NULL);
466 Curl_safefree(uploadfile);
471 urlnum = 1; /* without globbing, this is a single URL */
473 /* if multiple files extracted to stdout, insert separators! */
474 separator= ((!outfiles || curlx_strequal(outfiles, "-")) && urlnum > 1);
476 /* Here's looping around each globbed URL */
477 for(li = 0 ; li < urlnum; li++) {
482 struct OutStruct outs;
483 struct InStruct input;
484 struct timeval retrystart;
485 curl_off_t uploadfilesize;
486 long retry_numretries;
487 long retry_sleep_default;
489 char *this_url = NULL;
490 int metalink_next_res = 0;
495 uploadfilesize = -1; /* -1 means unknown */
497 /* default output stream is stdout */
498 memset(&outs, 0, sizeof(struct OutStruct));
499 outs.stream = stdout;
500 outs.config = config;
503 /* For Metalink download, use name in Metalink file as
505 outfile = strdup(mlfile->filename);
507 res = CURLE_OUT_OF_MEMORY;
510 this_url = strdup(mlres->url);
512 res = CURLE_OUT_OF_MEMORY;
518 res = glob_next_url(&this_url, urls);
523 this_url = strdup(urlnode->url);
525 res = CURLE_OUT_OF_MEMORY;
535 outfile = strdup(outfiles);
537 res = CURLE_OUT_OF_MEMORY;
543 if(((urlnode->flags&GETOUT_USEREMOTE) ||
544 (outfile && !curlx_strequal("-", outfile))) &&
545 (metalink || !config->use_metalink)) {
548 * We have specified a file name to store the result in, or we have
549 * decided we want to use the remote file name.
553 /* extract the file name from the URL */
554 res = get_url_file_name(&outfile, this_url);
557 if((!outfile || !*outfile) && !config->content_disposition) {
558 helpf(global->errors, "Remote file name has no length!\n");
559 res = CURLE_WRITE_ERROR;
562 #if defined(MSDOS) || defined(WIN32)
563 /* For DOS and WIN32, we do some major replacing of
564 bad characters in the file name before using it */
565 outfile = sanitize_dos_name(outfile);
567 res = CURLE_OUT_OF_MEMORY;
570 #endif /* MSDOS || WIN32 */
573 /* fill '#1' ... '#9' terms from URL pattern */
574 char *storefile = outfile;
575 res = glob_match_url(&outfile, storefile, urls);
576 Curl_safefree(storefile);
579 warnf(config, "bad output glob!\n");
584 /* Create the directory hierarchy, if not pre-existent to a multiple
587 if(config->create_dirs || metalink) {
588 res = create_dir_hierarchy(outfile, global->errors);
589 /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
590 if(res == CURLE_WRITE_ERROR)
597 if((urlnode->flags & GETOUT_USEREMOTE)
598 && config->content_disposition) {
599 /* Our header callback MIGHT set the filename */
600 DEBUGASSERT(!outs.filename);
603 if(config->resume_from_current) {
604 /* We're told to continue from where we are now. Get the size
605 of the file as it is now and open it for append instead */
606 struct_stat fileinfo;
607 /* VMS -- Danger, the filesize is only valid for stream files */
608 if(0 == stat(outfile, &fileinfo))
609 /* set offset to current file size: */
610 config->resume_from = fileinfo.st_size;
612 /* let offset be 0 */
613 config->resume_from = 0;
616 if(config->resume_from) {
618 /* open file for output, forcing VMS output format into stream
619 mode which is needed for stat() call above to always work. */
620 FILE *file = fopen(outfile, config->resume_from?"ab":"wb",
621 "ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0");
623 /* open file for output: */
624 FILE *file = fopen(outfile, config->resume_from?"ab":"wb");
627 helpf(global->errors, "Can't open '%s'!\n", outfile);
628 res = CURLE_WRITE_ERROR;
633 outs.init = config->resume_from;
636 outs.stream = NULL; /* open when needed */
638 outs.filename = outfile;
642 if(uploadfile && !stdin_upload(uploadfile)) {
644 * We have specified a file to upload and it isn't "-".
646 struct_stat fileinfo;
648 this_url = add_file_name_to_url(curl, this_url, uploadfile);
650 res = CURLE_OUT_OF_MEMORY;
655 * Reading binary from files can be a problem... Only FIXED, VAR
656 * etc WITHOUT implied CC will work Others need a \n appended to a
659 * - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
660 * fixed file with implied CC needs to have a byte added for every
661 * record processed, this can by derived from Filesize & recordsize
662 * for VARiable record files the records need to be counted! for
663 * every record add 1 for linefeed and subtract 2 for the record
664 * header for VARIABLE header files only the bare record data needs
665 * to be considered with one appended if implied CC
668 /* Calculate the real upload site for VMS */
670 if(stat(uploadfile, &fileinfo) == 0) {
671 fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
672 switch (fileinfo.st_fab_rfm) {
676 infd = open(uploadfile, O_RDONLY | O_BINARY);
679 infd = open(uploadfile, O_RDONLY | O_BINARY,
680 "rfm=stmlf", "ctx=stm");
685 infd = open(uploadfile, O_RDONLY | O_BINARY);
686 if((infd == -1) || fstat(infd, &fileinfo))
689 helpf(global->errors, "Can't open '%s'!\n", uploadfile);
694 res = CURLE_READ_ERROR;
699 /* we ignore file size for char/block devices, sockets, etc. */
700 if(S_ISREG(fileinfo.st_mode))
701 uploadfilesize = fileinfo.st_size;
704 else if(uploadfile && stdin_upload(uploadfile)) {
705 /* count to see if there are more than one auth bit set
706 in the authtype field */
709 while(bitcheck < 32) {
710 if(config->authtype & (1UL << bitcheck++)) {
713 /* more than one, we're done! */
720 * If the user has also selected --anyauth or --proxy-anyauth
721 * we should warn him/her.
723 if(config->proxyanyauth || (authbits>1)) {
725 "Using --anyauth or --proxy-anyauth with upload from stdin"
726 " involves a big risk of it not working. Use a temporary"
727 " file or a fixed auth type instead!\n");
730 DEBUGASSERT(infdopen == FALSE);
731 DEBUGASSERT(infd == STDIN_FILENO);
734 if(curlx_strequal(uploadfile, ".")) {
735 if(curlx_nonblock((curl_socket_t)infd, TRUE) < 0)
737 "fcntl failed on fd=%d: %s\n", infd, strerror(errno));
741 if(uploadfile && config->resume_from_current)
742 config->resume_from = -1; /* -1 will then force get-it-yourself */
744 if(output_expected(this_url, uploadfile)
745 && outs.stream && isatty(fileno(outs.stream)))
746 /* we send the output to a tty, therefore we switch off the progress
748 config->noprogress = config->isatty = TRUE;
750 /* progress meter is per download, so restore config
752 config->noprogress = orig_noprogress;
753 config->isatty = orig_isatty;
756 if(urlnum > 1 && !global->mute) {
757 fprintf(global->errors, "\n[%lu/%lu]: %s --> %s\n",
758 li+1, urlnum, this_url, outfile ? outfile : "<stdout>");
760 printf("%s%s\n", CURLseparator, this_url);
764 /* Find out whether the url contains a file name */
765 const char *pc = strstr(this_url, "://");
772 pc = strrchr(pc, '/'); /* check for a slash */
775 /* there is a slash present in the URL */
778 /* Ouch, there's already a question mark in the URL string, we
779 then append the data with an ampersand separator instead! */
783 * Then append ? followed by the get fields to the url.
786 urlbuffer = aprintf("%s%c%s", this_url, sep, httpgetfields);
788 /* Append / before the ? to create a well-formed url
789 if the url contains a hostname only
791 urlbuffer = aprintf("%s/?%s", this_url, httpgetfields);
794 res = CURLE_OUT_OF_MEMORY;
798 Curl_safefree(this_url); /* free previous URL */
799 this_url = urlbuffer; /* use our new URL instead! */
803 global->errors = stderr;
805 if((!outfile || !strcmp(outfile, "-")) && !config->use_ascii) {
806 /* We get the output to stdout and we have not got the ASCII/text
807 flag, then set stdout to be binary */
811 if(config->tcp_nodelay)
812 my_setopt(curl, CURLOPT_TCP_NODELAY, 1L);
815 my_setopt(curl, CURLOPT_WRITEDATA, &outs);
816 if(metalink || !config->use_metalink)
817 /* what call to write */
818 my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
821 /* Set Metalink specific write callback function to parse
822 XML data progressively. */
823 my_setopt(curl, CURLOPT_WRITEFUNCTION, metalink_write_cb);
824 #endif /* USE_METALINK */
828 input.config = config;
829 /* Note that if CURLOPT_READFUNCTION is fread (the default), then
830 * lib/telnet.c will Curl_poll() on the input file descriptor
831 * rather then calling the READFUNCTION at regular intervals.
832 * The circumstances in which it is preferable to enable this
833 * behaviour, by omitting to set the READFUNCTION & READDATA options,
834 * have not been determined.
836 my_setopt(curl, CURLOPT_READDATA, &input);
837 /* what call to read */
838 my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
840 /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
841 CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
842 my_setopt(curl, CURLOPT_SEEKDATA, &input);
843 my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
845 if(config->recvpersecond)
846 /* tell libcurl to use a smaller sized buffer as it allows us to
847 make better sleeps! 7.9.9 stuff! */
848 my_setopt(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond);
850 /* size of uploaded file: */
851 if(uploadfilesize != -1)
852 my_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
853 my_setopt_str(curl, CURLOPT_URL, this_url); /* what to fetch */
854 my_setopt(curl, CURLOPT_NOPROGRESS, config->noprogress?1L:0L);
855 if(config->no_body) {
856 my_setopt(curl, CURLOPT_NOBODY, 1L);
857 my_setopt(curl, CURLOPT_HEADER, 1L);
859 /* If --metalink is used, we ignore --include (headers in
860 output) option because mixing headers to the body will
861 confuse XML parser and/or hash check will fail. */
862 else if(!config->use_metalink)
863 my_setopt(curl, CURLOPT_HEADER, config->include_headers?1L:0L);
865 if(config->xoauth2_bearer)
866 my_setopt_str(curl, CURLOPT_XOAUTH2_BEARER, config->xoauth2_bearer);
868 #if !defined(CURL_DISABLE_PROXY)
870 /* TODO: Make this a run-time check instead of compile-time one. */
872 my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
873 my_setopt_str(curl, CURLOPT_PROXYUSERPWD, config->proxyuserpwd);
875 /* new in libcurl 7.3 */
876 my_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config->proxytunnel?1L:0L);
878 /* new in libcurl 7.5 */
880 my_setopt_enum(curl, CURLOPT_PROXYTYPE, (long)config->proxyver);
882 /* new in libcurl 7.10 */
883 if(config->socksproxy) {
884 my_setopt_str(curl, CURLOPT_PROXY, config->socksproxy);
885 my_setopt_enum(curl, CURLOPT_PROXYTYPE, (long)config->socksver);
888 /* new in libcurl 7.10.6 */
889 if(config->proxyanyauth)
890 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
892 else if(config->proxynegotiate)
893 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
894 (long)CURLAUTH_GSSNEGOTIATE);
895 else if(config->proxyntlm)
896 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
897 (long)CURLAUTH_NTLM);
898 else if(config->proxydigest)
899 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
900 (long)CURLAUTH_DIGEST);
901 else if(config->proxybasic)
902 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
903 (long)CURLAUTH_BASIC);
905 /* new in libcurl 7.19.4 */
906 my_setopt(curl, CURLOPT_NOPROXY, config->noproxy);
910 my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror?1L:0L);
911 my_setopt(curl, CURLOPT_UPLOAD, uploadfile?1L:0L);
912 my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly?1L:0L);
913 my_setopt(curl, CURLOPT_APPEND, config->ftp_append?1L:0L);
915 if(config->netrc_opt)
916 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_OPTIONAL);
917 else if(config->netrc || config->netrc_file)
918 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_REQUIRED);
920 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_IGNORED);
922 if(config->netrc_file)
923 my_setopt(curl, CURLOPT_NETRC_FILE, config->netrc_file);
925 my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii?1L:0L);
926 if(config->login_options)
927 my_setopt_str(curl, CURLOPT_LOGIN_OPTIONS, config->login_options);
928 my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
929 my_setopt_str(curl, CURLOPT_RANGE, config->range);
930 my_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
931 my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000));
933 if(built_in_protos & CURLPROTO_HTTP) {
937 my_setopt(curl, CURLOPT_FOLLOWLOCATION,
938 config->followlocation?1L:0L);
939 my_setopt(curl, CURLOPT_UNRESTRICTED_AUTH,
940 config->unrestricted_auth?1L:0L);
942 switch(config->httpreq) {
943 case HTTPREQ_SIMPLEPOST:
944 my_setopt_str(curl, CURLOPT_POSTFIELDS,
946 my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
947 config->postfieldsize);
950 my_setopt_httppost(curl, CURLOPT_HTTPPOST, config->httppost);
956 my_setopt_str(curl, CURLOPT_REFERER, config->referer);
957 my_setopt(curl, CURLOPT_AUTOREFERER, config->autoreferer?1L:0L);
958 my_setopt_str(curl, CURLOPT_USERAGENT, config->useragent);
959 my_setopt_slist(curl, CURLOPT_HTTPHEADER, config->headers);
961 /* new in libcurl 7.5 */
962 my_setopt(curl, CURLOPT_MAXREDIRS, config->maxredirs);
964 /* new in libcurl 7.9.1 */
965 if(config->httpversion)
966 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion);
968 /* new in libcurl 7.10.6 (default is Basic) */
970 my_setopt_bitmask(curl, CURLOPT_HTTPAUTH, (long)config->authtype);
972 /* curl 7.19.1 (the 301 version existed in 7.18.2),
973 303 was added in 7.26.0 */
975 postRedir |= CURL_REDIR_POST_301;
977 postRedir |= CURL_REDIR_POST_302;
979 postRedir |= CURL_REDIR_POST_303;
980 my_setopt(curl, CURLOPT_POSTREDIR, postRedir);
982 /* new in libcurl 7.21.6 */
984 my_setopt_str(curl, CURLOPT_ACCEPT_ENCODING, "");
986 /* new in libcurl 7.21.6 */
987 if(config->tr_encoding)
988 my_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1L);
990 } /* (built_in_protos & CURLPROTO_HTTP) */
992 my_setopt_str(curl, CURLOPT_FTPPORT, config->ftpport);
993 my_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,
994 config->low_speed_limit);
995 my_setopt(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
996 my_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE,
997 config->sendpersecond);
998 my_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE,
999 config->recvpersecond);
1001 if(config->use_resume)
1002 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, config->resume_from);
1004 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, CURL_OFF_T_C(0));
1006 my_setopt_str(curl, CURLOPT_SSLCERT, config->cert);
1007 my_setopt_str(curl, CURLOPT_SSLCERTTYPE, config->cert_type);
1008 my_setopt_str(curl, CURLOPT_SSLKEY, config->key);
1009 my_setopt_str(curl, CURLOPT_SSLKEYTYPE, config->key_type);
1010 my_setopt_str(curl, CURLOPT_KEYPASSWD, config->key_passwd);
1012 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1014 /* SSH and SSL private key uses same command-line option */
1015 /* new in libcurl 7.16.1 */
1016 my_setopt_str(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key);
1017 /* new in libcurl 7.16.1 */
1018 my_setopt_str(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey);
1020 /* new in libcurl 7.17.1: SSH host key md5 checking allows us
1021 to fail if we are not talking to who we think we should */
1022 my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,
1023 config->hostpubmd5);
1027 my_setopt_str(curl, CURLOPT_CAINFO, config->cacert);
1029 my_setopt_str(curl, CURLOPT_CAPATH, config->capath);
1031 my_setopt_str(curl, CURLOPT_CRLFILE, config->crlfile);
1033 if(curlinfo->features & CURL_VERSION_SSL) {
1034 if(config->insecure_ok) {
1035 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
1036 my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
1039 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1040 /* libcurl default is strict verifyhost -> 2L */
1041 /* my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); */
1045 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1046 if(!config->insecure_ok) {
1049 res = CURLE_OUT_OF_MEMORY;
1052 file = aprintf("%s/%sssh/known_hosts", home, DOT_CHAR);
1054 /* new in curl 7.19.6 */
1055 res = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file);
1057 if(res == CURLE_UNKNOWN_OPTION)
1058 /* libssh2 version older than 1.1.1 */
1061 Curl_safefree(home);
1068 if(config->no_body || config->remote_time) {
1069 /* no body or use remote time */
1070 my_setopt(curl, CURLOPT_FILETIME, 1L);
1073 my_setopt(curl, CURLOPT_CRLF, config->crlf?1L:0L);
1074 my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
1075 my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
1076 my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
1078 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
1080 /* TODO: Make this a run-time check instead of compile-time one. */
1083 my_setopt_str(curl, CURLOPT_COOKIE, config->cookie);
1085 if(config->cookiefile)
1086 my_setopt_str(curl, CURLOPT_COOKIEFILE, config->cookiefile);
1088 /* new in libcurl 7.9 */
1089 if(config->cookiejar)
1090 my_setopt_str(curl, CURLOPT_COOKIEJAR, config->cookiejar);
1092 /* new in libcurl 7.9.7 */
1093 my_setopt(curl, CURLOPT_COOKIESESSION, config->cookiesession?1L:0L);
1097 my_setopt_enum(curl, CURLOPT_SSLVERSION, config->ssl_version);
1098 my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond);
1099 my_setopt(curl, CURLOPT_TIMEVALUE, (long)config->condtime);
1100 my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
1101 my_setopt(curl, CURLOPT_STDERR, global->errors);
1103 /* three new ones in libcurl 7.3: */
1104 my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
1105 my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
1107 progressbarinit(&progressbar, config);
1108 if((config->progressmode == CURL_PROGRESS_BAR) &&
1109 !config->noprogress && !global->mute) {
1110 /* we want the alternative style, then we have to implement it
1112 my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb);
1113 my_setopt(curl, CURLOPT_XFERINFODATA, &progressbar);
1116 /* new in libcurl 7.24.0: */
1117 if(config->dns_servers)
1118 my_setopt_str(curl, CURLOPT_DNS_SERVERS, config->dns_servers);
1120 /* new in libcurl 7.33.0: */
1121 if(config->dns_interface)
1122 my_setopt_str(curl, CURLOPT_DNS_INTERFACE, config->dns_interface);
1123 if(config->dns_ipv4_addr)
1124 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP4, config->dns_ipv4_addr);
1125 if(config->dns_ipv6_addr)
1126 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP6, config->dns_ipv6_addr);
1128 /* new in libcurl 7.6.2: */
1129 my_setopt_slist(curl, CURLOPT_TELNETOPTIONS, config->telnet_options);
1131 /* new in libcurl 7.7: */
1132 my_setopt_str(curl, CURLOPT_RANDOM_FILE, config->random_file);
1133 my_setopt_str(curl, CURLOPT_EGDSOCKET, config->egd_file);
1134 my_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
1135 (long)(config->connecttimeout * 1000));
1137 if(config->cipher_list)
1138 my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
1140 /* new in libcurl 7.9.2: */
1141 if(config->disable_epsv)
1143 my_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);
1145 /* new in libcurl 7.10.5 */
1146 if(config->disable_eprt)
1148 my_setopt(curl, CURLOPT_FTP_USE_EPRT, 0L);
1150 if(global->tracetype != TRACE_NONE) {
1151 my_setopt(curl, CURLOPT_DEBUGFUNCTION, tool_debug_cb);
1152 my_setopt(curl, CURLOPT_DEBUGDATA, config);
1153 my_setopt(curl, CURLOPT_VERBOSE, 1L);
1156 /* new in curl 7.9.3 */
1157 if(config->engine) {
1158 res = res_setopt_str(curl, CURLOPT_SSLENGINE, config->engine);
1161 my_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
1164 /* new in curl 7.10.7, extended in 7.19.4 but this only sets 0 or 1 */
1165 my_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
1166 config->ftp_create_dirs?1L:0L);
1168 /* new in curl 7.10.8 */
1169 if(config->max_filesize)
1170 my_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
1171 config->max_filesize);
1173 if(4 == config->ip_version)
1174 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1175 else if(6 == config->ip_version)
1176 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
1178 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER);
1180 /* new in curl 7.15.5 */
1181 if(config->ftp_ssl_reqd)
1182 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
1184 /* new in curl 7.11.0 */
1185 else if(config->ftp_ssl)
1186 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
1188 /* new in curl 7.16.0 */
1189 else if(config->ftp_ssl_control)
1190 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_CONTROL);
1192 /* new in curl 7.16.1 */
1193 if(config->ftp_ssl_ccc)
1194 my_setopt_enum(curl, CURLOPT_FTP_SSL_CCC,
1195 (long)config->ftp_ssl_ccc_mode);
1197 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1199 /* TODO: Make this a run-time check instead of compile-time one. */
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);
1213 my_setopt_str(curl, CURLOPT_FTP_ACCOUNT, config->ftp_account);
1215 my_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, config->ignorecl?1L:0L);
1218 my_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, config->ftp_skip_ip?1L:0L);
1221 my_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long)config->ftp_filemethod);
1224 if(config->localport) {
1225 my_setopt(curl, CURLOPT_LOCALPORT, (long)config->localport);
1226 my_setopt_str(curl, CURLOPT_LOCALPORTRANGE,
1227 (long)config->localportrange);
1231 my_setopt_str(curl, CURLOPT_FTP_ALTERNATIVE_TO_USER,
1232 config->ftp_alternative_to_user);
1235 if(config->disable_sessionid)
1237 my_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0L);
1241 my_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 0L);
1242 my_setopt(curl, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
1246 if(!config->nokeepalive) {
1247 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
1248 if(config->alivetime != 0) {
1249 #if !defined(TCP_KEEPIDLE) || !defined(TCP_KEEPINTVL)
1250 warnf(config, "Keep-alive functionality somewhat crippled due to "
1251 "missing support in your operating system!\n");
1253 my_setopt(curl, CURLOPT_TCP_KEEPIDLE, config->alivetime);
1254 my_setopt(curl, CURLOPT_TCP_KEEPINTVL, config->alivetime);
1258 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
1261 if(config->tftp_blksize)
1262 my_setopt(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
1264 if(config->mail_from)
1265 my_setopt_str(curl, CURLOPT_MAIL_FROM, config->mail_from);
1267 if(config->mail_rcpt)
1268 my_setopt_slist(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
1271 if(config->ftp_pret)
1272 my_setopt(curl, CURLOPT_FTP_USE_PRET, 1L);
1274 if(config->proto_present)
1275 my_setopt_flags(curl, CURLOPT_PROTOCOLS, config->proto);
1276 if(config->proto_redir_present)
1277 my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
1279 if(config->content_disposition
1280 && (urlnode->flags & GETOUT_USEREMOTE)
1281 && (checkprefix("http://", this_url) ||
1282 checkprefix("https://", this_url)))
1283 hdrcbdata.honor_cd_filename = TRUE;
1285 hdrcbdata.honor_cd_filename = FALSE;
1287 hdrcbdata.outs = &outs;
1288 hdrcbdata.heads = &heads;
1290 my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
1291 my_setopt(curl, CURLOPT_HEADERDATA, &hdrcbdata);
1295 my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
1298 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1299 if(config->tls_username)
1300 my_setopt_str(curl, CURLOPT_TLSAUTH_USERNAME,
1301 config->tls_username);
1302 if(config->tls_password)
1303 my_setopt_str(curl, CURLOPT_TLSAUTH_PASSWORD,
1304 config->tls_password);
1305 if(config->tls_authtype)
1306 my_setopt_str(curl, CURLOPT_TLSAUTH_TYPE,
1307 config->tls_authtype);
1311 if(config->gssapi_delegation)
1312 my_setopt_str(curl, CURLOPT_GSSAPI_DELEGATION,
1313 config->gssapi_delegation);
1316 if(config->ssl_allow_beast)
1317 my_setopt(curl, CURLOPT_SSL_OPTIONS, (long)CURLSSLOPT_ALLOW_BEAST);
1319 if(config->mail_auth)
1320 my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
1324 my_setopt(curl, CURLOPT_SASL_IR, 1L);
1327 my_setopt(curl, CURLOPT_SSL_ENABLE_NPN, 0L);
1330 if(config->noalpn) {
1331 my_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L);
1334 /* initialize retry vars for loop below */
1335 retry_sleep_default = (config->retry_delay) ?
1336 config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
1338 retry_numretries = config->req_retry;
1339 retry_sleep = retry_sleep_default; /* ms */
1340 retrystart = tvnow();
1342 #ifndef CURL_DISABLE_LIBCURL_OPTION
1343 res = easysrc_perform();
1351 if(!metalink && config->use_metalink) {
1352 /* If outs.metalink_parser is non-NULL, delete it first. */
1353 if(outs.metalink_parser)
1354 metalink_parser_context_delete(outs.metalink_parser);
1355 outs.metalink_parser = metalink_parser_context_new();
1356 if(outs.metalink_parser == NULL) {
1357 res = CURLE_OUT_OF_MEMORY;
1360 fprintf(config->global->errors,
1361 "Metalink: parsing (%s) metalink/XML...\n", this_url);
1364 fprintf(config->global->errors,
1365 "Metalink: fetching (%s) from (%s)...\n",
1366 mlfile->filename, this_url);
1367 #endif /* USE_METALINK */
1370 if(config->test_event_based)
1371 res = curl_easy_perform_ev(curl);
1374 res = curl_easy_perform(curl);
1376 if(outs.is_cd_filename && outs.stream && !global->mute &&
1378 printf("curl: Saved to filename '%s'\n", outs.filename);
1380 /* if retry-max-time is non-zero, make sure we haven't exceeded the
1382 if(retry_numretries &&
1383 (!config->retry_maxtime ||
1384 (tvdiff(tvnow(), retrystart) <
1385 config->retry_maxtime*1000L)) ) {
1391 RETRY_LAST /* not used */
1394 if((CURLE_OPERATION_TIMEDOUT == res) ||
1395 (CURLE_COULDNT_RESOLVE_HOST == res) ||
1396 (CURLE_COULDNT_RESOLVE_PROXY == res) ||
1397 (CURLE_FTP_ACCEPT_TIMEOUT == res))
1398 /* retry timeout always */
1399 retry = RETRY_TIMEOUT;
1400 else if((CURLE_OK == res) ||
1401 (config->failonerror &&
1402 (CURLE_HTTP_RETURNED_ERROR == res))) {
1403 /* If it returned OK. _or_ failonerror was enabled and it
1404 returned due to such an error, check for HTTP transient
1405 errors to retry on. */
1406 char *effective_url = NULL;
1407 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1409 checkprefix("http", effective_url)) {
1410 /* This was HTTP(S) */
1411 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1414 case 500: /* Internal Server Error */
1415 case 502: /* Bad Gateway */
1416 case 503: /* Service Unavailable */
1417 case 504: /* Gateway Timeout */
1420 * At this point, we have already written data to the output
1421 * file (or terminal). If we write to a file, we must rewind
1422 * or close/re-open the file so that the next attempt starts
1423 * over from the beginning.
1425 * TODO: similar action for the upload case. We might need
1426 * to start over reading from a previous point if we have
1427 * uploaded something when this was returned.
1434 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1436 if(response/100 == 4)
1438 * This is typically when the FTP server only allows a certain
1439 * amount of users and we are not one of them. All 4xx codes
1446 static const char * const m[]={
1447 NULL, "timeout", "HTTP error", "FTP error"
1449 warnf(config, "Transient problem: %s "
1450 "Will retry in %ld seconds. "
1451 "%ld retries left.\n",
1452 m[retry], retry_sleep/1000L, retry_numretries);
1454 tool_go_sleep(retry_sleep);
1456 if(!config->retry_delay) {
1458 if(retry_sleep > RETRY_SLEEP_MAX)
1459 retry_sleep = RETRY_SLEEP_MAX;
1461 if(outs.bytes && outs.filename) {
1462 /* We have written data to a output file, we truncate file
1465 fprintf(global->errors, "Throwing away %"
1466 CURL_FORMAT_CURL_OFF_T " bytes\n",
1468 fflush(outs.stream);
1469 /* truncate file at the position where we started appending */
1470 #ifdef HAVE_FTRUNCATE
1471 if(ftruncate( fileno(outs.stream), outs.init)) {
1472 /* when truncate fails, we can't just append as then we'll
1473 create something strange, bail out */
1475 fprintf(global->errors,
1476 "failed to truncate, exiting\n");
1477 res = CURLE_WRITE_ERROR;
1480 /* now seek to the end of the file, the position where we
1481 just truncated the file in a large file-safe way */
1482 fseek(outs.stream, 0, SEEK_END);
1484 /* ftruncate is not available, so just reposition the file
1485 to the location we would have truncated it. This won't
1486 work properly with large files on 32-bit systems, but
1487 most of those will have ftruncate. */
1488 fseek(outs.stream, (long)outs.init, SEEK_SET);
1490 outs.bytes = 0; /* clear for next round */
1492 continue; /* curl_easy_perform loop */
1494 } /* if retry_numretries */
1496 /* Metalink: Decide to try the next resource or
1497 not. Basically, we want to try the next resource if
1498 download was not successful. */
1500 if(CURLE_OK == res) {
1501 /* TODO We want to try next resource when download was
1502 not successful. How to know that? */
1503 char *effective_url = NULL;
1504 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1506 curlx_strnequal(effective_url, "http", 4)) {
1507 /* This was HTTP(S) */
1508 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1509 if(response != 200 && response != 206) {
1510 metalink_next_res = 1;
1511 fprintf(global->errors,
1512 "Metalink: fetching (%s) from (%s) FAILED "
1513 "(HTTP status code %d)\n",
1514 mlfile->filename, this_url, response);
1519 metalink_next_res = 1;
1520 fprintf(global->errors,
1521 "Metalink: fetching (%s) from (%s) FAILED (%s)\n",
1522 mlfile->filename, this_url,
1524 errorbuffer : curl_easy_strerror((CURLcode)res));
1527 if(metalink && !metalink_next_res)
1528 fprintf(global->errors, "Metalink: fetching (%s) from (%s) OK\n",
1529 mlfile->filename, this_url);
1531 /* In all ordinary cases, just break out of loop here */
1532 break; /* curl_easy_perform loop */
1536 if((config->progressmode == CURL_PROGRESS_BAR) &&
1538 /* if the custom progress bar has been displayed, we output a
1540 fputs("\n", progressbar.out);
1542 if(config->writeout)
1543 ourWriteOut(curl, &outs, config->writeout);
1545 if(config->writeenv)
1549 ** Code within this loop may jump directly here to label 'show_error'
1550 ** in order to display an error message for CURLcode stored in 'res'
1551 ** variable and exit loop once that necessary writing and cleanup
1552 ** in label 'quit_urls' has been done.
1558 if(is_vms_shell()) {
1559 /* VMS DCL shell behavior */
1560 if(!global->showerror)
1561 vms_show = VMSSTS_HIDE;
1565 if(res && global->showerror) {
1566 fprintf(global->errors, "curl: (%d) %s\n", res, (errorbuffer[0]) ?
1567 errorbuffer : curl_easy_strerror((CURLcode)res));
1568 if(res == CURLE_SSL_CACERT)
1569 fprintf(global->errors, "%s%s",
1570 CURL_CA_CERT_ERRORMSG1, CURL_CA_CERT_ERRORMSG2);
1573 /* Fall through comment to 'quit_urls' label */
1576 ** Upon error condition and always that a message has already been
1577 ** displayed, code within this loop may jump directly here to label
1578 ** 'quit_urls' otherwise it should jump to 'show_error' label above.
1580 ** When 'res' variable is _not_ CURLE_OK loop will exit once that
1581 ** all code following 'quit_urls' has been executed. Otherwise it
1582 ** will loop to the beginning from where it may exit if there are
1583 ** no more urls left.
1588 /* Set file extended attributes */
1589 if(!res && config->xattr && outs.fopened && outs.stream) {
1590 int rc = fwrite_xattr(curl, fileno(outs.stream));
1592 warnf(config, "Error setting extended attributes: %s\n",
1596 /* Close the file */
1597 if(outs.fopened && outs.stream) {
1598 int rc = fclose(outs.stream);
1600 /* something went wrong in the writing process */
1601 res = CURLE_WRITE_ERROR;
1602 fprintf(global->errors, "(%d) Failed writing body\n", res);
1605 else if(!outs.s_isreg && outs.stream) {
1606 /* Dump standard stream buffered data */
1607 int rc = fflush(outs.stream);
1609 /* something went wrong in the writing process */
1610 res = CURLE_WRITE_ERROR;
1611 fprintf(global->errors, "(%d) Failed writing body\n", res);
1616 if(!res && outs.s_isreg && outs.filename) {
1617 /* Set the url (up to 80 chars) as comment for the file */
1618 if(strlen(url) > 78)
1620 SetComment(outs.filename, url);
1625 /* File time can only be set _after_ the file has been closed */
1626 if(!res && config->remote_time && outs.s_isreg && outs.filename) {
1627 /* Ask libcurl if we got a remote file time */
1629 curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
1631 struct utimbuf times;
1632 times.actime = (time_t)filetime;
1633 times.modtime = (time_t)filetime;
1634 utime(outs.filename, ×); /* set the time we got */
1640 if(!metalink && config->use_metalink && res == CURLE_OK) {
1641 int rv = parse_metalink(config, &outs, this_url);
1643 fprintf(config->global->errors, "Metalink: parsing (%s) OK\n",
1646 fprintf(config->global->errors, "Metalink: parsing (%s) FAILED\n",
1649 else if(metalink && res == CURLE_OK && !metalink_next_res) {
1650 int rv = metalink_check_hash(global, mlfile, outs.filename);
1652 metalink_next_res = 1;
1655 #endif /* USE_METALINK */
1657 /* No more business with this output struct */
1658 if(outs.alloc_filename)
1659 Curl_safefree(outs.filename);
1661 if(outs.metalink_parser)
1662 metalink_parser_context_delete(outs.metalink_parser);
1663 #endif /* USE_METALINK */
1664 memset(&outs, 0, sizeof(struct OutStruct));
1665 hdrcbdata.outs = NULL;
1667 /* Free loop-local allocated memory and close loop-local opened fd */
1669 Curl_safefree(outfile);
1670 Curl_safefree(this_url);
1676 /* Should exit if error is fatal. */
1677 if(is_fatal_error(res)) {
1680 if(!metalink_next_res)
1682 mlres = mlres->next;
1684 /* TODO If metalink_next_res is 1 and mlres is NULL,
1685 * set res to error code
1691 /* when url globbing, exit loop upon critical error */
1692 if(is_fatal_error(res))
1696 /* when not url globbing, exit loop upon any error */
1699 } /* loop to the next URL */
1701 /* Free loop-local allocated memory */
1703 Curl_safefree(uploadfile);
1706 /* Free list of remaining URLs */
1712 /* when file globbing, exit loop upon critical error */
1713 if(is_fatal_error(res))
1717 /* when not file globbing, exit loop upon any error */
1720 } /* loop to the next globbed upload file */
1722 /* Free loop-local allocated memory */
1724 Curl_safefree(outfiles);
1727 /* Free list of globbed upload files */
1728 glob_cleanup(inglob);
1732 /* Free this URL node data without destroying the
1733 the node itself nor modifying next pointer. */
1734 Curl_safefree(urlnode->url);
1735 Curl_safefree(urlnode->outfile);
1736 Curl_safefree(urlnode->infile);
1740 ** Bail out upon critical errors
1742 if(is_fatal_error(res))
1745 } /* for-loop through all URLs */
1748 ** Nested loops end here.
1753 /* Free function-local referenced allocated memory */
1754 Curl_safefree(httpgetfields);
1756 /* Free list of given URLs */
1757 clean_getout(config);
1759 #ifndef CURL_DISABLE_LIBCURL_OPTION
1763 hdrcbdata.heads = NULL;
1765 /* Close function-local opened file descriptors */
1767 if(heads.fopened && heads.stream)
1768 fclose(heads.stream);
1769 if(heads.alloc_filename)
1770 Curl_safefree(heads.filename);
1772 #ifndef CURL_DISABLE_LIBCURL_OPTION
1773 /* Dump the libcurl code if previously enabled.
1774 NOTE: that this function relies on config->errors amongst other things
1775 so not everything can be closed and cleaned before this is called */
1776 dumpeasysrc(config);
1779 /* Release metalink related resources here */
1780 clean_metalink(config);
1782 return (CURLcode)res;
1785 CURLcode operate(struct GlobalConfig *config, int argc, argv_item_t argv[])
1787 CURLcode result = CURLE_OK;
1789 /* Setup proper locale from environment */
1790 #ifdef HAVE_SETLOCALE
1791 setlocale(LC_ALL, "");
1794 /* Parse .curlrc if necessary */
1795 if((argc == 1) || (!curlx_strequal(argv[1], "-q"))) {
1796 parseconfig(NULL, config); /* ignore possible failure */
1798 /* If we had no arguments then make sure a url was specified in .curlrc */
1799 if((argc < 2) && (!config->first->url_list)) {
1800 helpf(config->errors, NULL);
1801 result = CURLE_FAILED_INIT;
1806 /* Parse the command line arguments */
1807 ParameterError res = parse_args(config, argc, argv);
1811 /* Check if we were asked for the help */
1812 if(res == PARAM_HELP_REQUESTED)
1814 /* Check if we were asked for the manual */
1815 else if(res == PARAM_MANUAL_REQUESTED)
1817 /* Check if we were asked for the version information */
1818 else if(res == PARAM_VERSION_INFO_REQUESTED)
1819 tool_version_info();
1820 /* Check if we were asked to list the SSL engines */
1821 else if(res == PARAM_ENGINES_REQUESTED)
1822 tool_list_engines(config->easy);
1824 result = CURLE_FAILED_INIT;
1826 /* Perform the main operations */
1829 struct OperationConfig *operation = config->first;
1831 /* Get the required aguments for each operation */
1832 while(!result && operation) {
1833 result = get_args(operation, count++);
1835 operation = operation->next;
1838 /* Set the current operation pointer */
1839 config->current = config->first;
1841 /* Perform each operation */
1842 while(!result && config->current) {
1843 result = operate_do(config, config->current);
1845 config->current = config->current->next;