1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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"
34 #elif defined(HAVE_SYS_UTIME_H)
35 # include <sys/utime.h>
42 #ifdef HAVE_NETINET_TCP_H
43 # 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_msgs.h"
69 #include "tool_operate.h"
70 #include "tool_operhlp.h"
71 #include "tool_parsecfg.h"
72 #include "tool_setopt.h"
73 #include "tool_sleep.h"
74 #include "tool_urlglob.h"
75 #include "tool_util.h"
76 #include "tool_writeenv.h"
77 #include "tool_writeout.h"
78 #include "tool_xattr.h"
80 #include "memdebug.h" /* keep this as LAST include */
82 #define CURLseparator "--_curl_--"
85 /* since O_BINARY as used in bitmasks, setting it to zero makes it usable in
86 source code but yet it doesn't ruin anything */
90 #define CURL_CA_CERT_ERRORMSG1 \
91 "More details here: http://curl.haxx.se/docs/sslcerts.html\n\n" \
92 "curl performs SSL certificate verification by default, " \
93 "using a \"bundle\"\n" \
94 " of Certificate Authority (CA) public keys (CA certs). If the default\n" \
95 " bundle file isn't adequate, you can specify an alternate file\n" \
96 " using the --cacert option.\n"
98 #define CURL_CA_CERT_ERRORMSG2 \
99 "If this HTTPS server uses a certificate signed by a CA represented in\n" \
100 " the bundle, the certificate verification probably failed due to a\n" \
101 " problem with the certificate (it might be expired, or the name might\n" \
102 " not match the domain name in the URL).\n" \
103 "If you'd like to turn off curl's verification of the certificate, use\n" \
104 " the -k (or --insecure) option.\n"
106 static int is_fatal_error(int code)
109 /* TODO: Should CURLE_SSL_CACERT be included as critical error ? */
110 case CURLE_FAILED_INIT:
111 case CURLE_OUT_OF_MEMORY:
112 case CURLE_UNKNOWN_OPTION:
113 case CURLE_FUNCTION_NOT_FOUND:
114 case CURLE_BAD_FUNCTION_ARGUMENT:
120 /* no error or not critical */
124 int operate(struct Configurable *config, int argc, argv_item_t argv[])
126 char errorbuffer[CURL_ERROR_SIZE];
127 struct ProgressData progressbar;
128 struct getout *urlnode;
130 struct OutStruct heads;
132 #ifdef HAVE_LIBMETALINK
133 metalinkfile *mlfile_last = NULL;
134 #endif /* HAVE_LIBMETALINK */
137 char *httpgetfields = NULL;
143 errorbuffer[0] = '\0';
144 /* default headers output stream is stdout */
145 memset(&heads, 0, sizeof(struct OutStruct));
146 heads.stream = stdout;
147 heads.config = config;
149 memory_tracking_init();
152 ** Initialize curl library - do not call any libcurl functions before
153 ** this point. Note that the memory_tracking_init() magic above is an
154 ** exception, but then that's not part of the official public API.
156 if(main_init() != CURLE_OK) {
157 helpf(config->errors, "error initializing curl library\n");
158 return CURLE_FAILED_INIT;
161 /* Get libcurl info right away */
162 if(get_libcurl_info() != CURLE_OK) {
163 helpf(config->errors, "error retrieving curl library information\n");
165 return CURLE_FAILED_INIT;
168 /* Get a curl handle to use for all forthcoming curl transfers */
169 curl = curl_easy_init();
171 helpf(config->errors, "error initializing curl easy handle\n");
173 return CURLE_FAILED_INIT;
178 ** Beyond this point no return'ing from this function allowed.
179 ** Jump to label 'quit_curl' in order to abandon this function
180 ** from outside of nested loops further down below.
183 /* setup proper locale from environment */
184 #ifdef HAVE_SETLOCALE
185 setlocale(LC_ALL, "");
189 config->postfieldsize = -1;
190 config->showerror = -1; /* will show errors */
191 config->use_httpget = FALSE;
192 config->create_dirs = FALSE;
193 config->maxredirs = DEFAULT_MAXREDIRS;
194 config->proto = CURLPROTO_ALL; /* FIXME: better to read from library */
195 config->proto_present = FALSE;
196 config->proto_redir =
197 CURLPROTO_ALL & ~(CURLPROTO_FILE|CURLPROTO_SCP); /* not FILE or SCP */
198 config->proto_redir_present = FALSE;
201 (!curlx_strnequal("--", argv[1], 2) && (argv[1][0] == '-')) &&
202 strchr(argv[1], 'q')) {
204 * The first flag, that is not a verbose name, but a shortname
205 * and it includes the 'q' flag!
210 parseconfig(NULL, config); /* ignore possible failure */
213 if((argc < 2) && !config->url_list) {
214 helpf(config->errors, NULL);
215 res = CURLE_FAILED_INIT;
220 for(i = 1, stillflags = TRUE; i < argc; i++) {
222 ('-' == argv[i][0])) {
225 char *origopt = argv[i];
227 char *flag = argv[i];
229 if(curlx_strequal("--", argv[i]))
230 /* this indicates the end of the flags and thus enables the
231 following (URL) argument to start with -. */
234 nextarg = (i < (argc-1)) ? argv[i+1] : NULL;
236 res = getparameter(flag, nextarg, &passarg, config);
238 int retval = CURLE_OK;
239 if(res != PARAM_HELP_REQUESTED) {
240 const char *reason = param2text(res);
241 helpf(config->errors, "option %s: %s\n", origopt, reason);
242 retval = CURLE_FAILED_INIT;
248 if(passarg) /* we're supposed to skip this */
254 /* just add the URL please */
255 res = getparameter((char *)"--url", argv[i], &used, config);
261 if((!config->url_list || !config->url_list->url) && !config->list_engines) {
262 helpf(config->errors, "no URL specified!\n");
263 res = CURLE_FAILED_INIT;
267 if(!config->useragent)
268 config->useragent = my_useragent();
269 if(!config->useragent) {
270 helpf(config->errors, "out of memory\n");
271 res = CURLE_OUT_OF_MEMORY;
275 /* On WIN32 we can't set the path to curl-ca-bundle.crt
276 * at compile time. So we look here for the file in two ways:
277 * 1: look at the environment variable CURL_CA_BUNDLE for a path
278 * 2: if #1 isn't found, use the windows API function SearchPath()
279 * to find it along the app's path (includes app's dir and CWD)
281 * We support the environment variable thing for non-Windows platforms
282 * too. Just for the sake of it.
284 if(!config->cacert &&
286 !config->insecure_ok) {
288 env = curlx_getenv("CURL_CA_BUNDLE");
290 config->cacert = strdup(env);
291 if(!config->cacert) {
293 helpf(config->errors, "out of memory\n");
294 res = CURLE_OUT_OF_MEMORY;
299 env = curlx_getenv("SSL_CERT_DIR");
301 config->capath = strdup(env);
302 if(!config->capath) {
304 helpf(config->errors, "out of memory\n");
305 res = CURLE_OUT_OF_MEMORY;
310 env = curlx_getenv("SSL_CERT_FILE");
312 config->cacert = strdup(env);
313 if(!config->cacert) {
315 helpf(config->errors, "out of memory\n");
316 res = CURLE_OUT_OF_MEMORY;
327 res = FindWin32CACert(config, "curl-ca-bundle.crt");
334 if(config->postfields) {
335 if(config->use_httpget) {
336 /* Use the postfields data for a http get */
337 httpgetfields = strdup(config->postfields);
338 Curl_safefree(config->postfields);
340 helpf(config->errors, "out of memory\n");
341 res = CURLE_OUT_OF_MEMORY;
344 if(SetHTTPrequest(config,
345 (config->no_body?HTTPREQ_HEAD:HTTPREQ_GET),
352 if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq)) {
359 #ifndef CURL_DISABLE_LIBCURL_OPTION
360 res = easysrc_init();
362 helpf(config->errors, "out of memory\n");
367 if(config->list_engines) {
368 struct curl_slist *engines = NULL;
369 curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
370 list_engines(engines);
371 curl_slist_free_all(engines);
376 /* Single header file for all URLs */
377 if(config->headerfile) {
378 /* open file for output: */
379 if(!curlx_strequal(config->headerfile, "-")) {
380 FILE *newfile = fopen(config->headerfile, "wb");
382 warnf(config, "Failed to open %s\n", config->headerfile);
383 res = CURLE_WRITE_ERROR;
387 heads.filename = config->headerfile;
388 heads.s_isreg = TRUE;
389 heads.fopened = TRUE;
390 heads.stream = newfile;
396 ** Nested loops start here.
399 /* loop through the list of given URLs */
401 for(urlnode = config->url_list; urlnode; urlnode = urlnode->next) {
403 int up; /* upload file counter within a single upload glob */
404 char *infiles; /* might be a glob pattern */
409 int metalink = 0; /* nonzero for metalink download. Put outside of
410 HAVE_LIBMETALINK to reduce #ifdef */
411 #ifdef HAVE_LIBMETALINK
412 metalinkfile *mlfile;
413 metalink_resource *mlres;
414 #endif /* HAVE_LIBMETALINK */
420 #ifdef HAVE_LIBMETALINK
421 if(urlnode->flags & GETOUT_METALINK) {
423 if(mlfile_last == NULL) {
424 mlfile_last = config->metalinkfile_list;
426 mlfile = mlfile_last;
427 mlfile_last = mlfile_last->next;
428 mlres = mlfile->resource;
434 #endif /* HAVE_LIBMETALINK */
436 /* urlnode->url is the full URL (it might be NULL) */
439 /* This node has no URL. Free node data without destroying the
440 node itself nor modifying next pointer and continue to next */
441 Curl_safefree(urlnode->outfile);
442 Curl_safefree(urlnode->infile);
444 continue; /* next URL please */
447 /* save outfile pattern before expansion */
448 if(urlnode->outfile) {
449 outfiles = strdup(urlnode->outfile);
451 helpf(config->errors, "out of memory\n");
452 res = CURLE_OUT_OF_MEMORY;
457 infiles = urlnode->infile;
459 if(!config->globoff && infiles) {
460 /* Unless explicitly shut off */
461 res = glob_url(&inglob, infiles, &infilenum,
462 config->showerror?config->errors:NULL);
464 Curl_safefree(outfiles);
469 /* Here's the loop for uploading multiple files within the same
470 single globbed string. If no upload, we enter the loop once anyway. */
471 for(up = 0 ; up < infilenum; up++) {
473 char *uploadfile; /* a single file, never a glob */
487 res = glob_next_url(&uploadfile, inglob);
488 if(res == CURLE_OUT_OF_MEMORY)
489 helpf(config->errors, "out of memory\n");
492 uploadfile = strdup(infiles);
494 helpf(config->errors, "out of memory\n");
495 res = CURLE_OUT_OF_MEMORY;
504 #ifdef HAVE_LIBMETALINK
506 /* For Metalink download, we don't use glob. Instead we use
507 the number of resources as urlnum. */
508 urlnum = count_next_metalink_resource(mlfile);
511 #endif /* HAVE_LIBMETALINK */
512 if(!config->globoff) {
513 /* Unless explicitly shut off, we expand '{...}' and '[...]'
514 expressions and return total number of URLs in pattern set */
515 res = glob_url(&urls, urlnode->url, &urlnum,
516 config->showerror?config->errors:NULL);
518 Curl_safefree(uploadfile);
523 urlnum = 1; /* without globbing, this is a single URL */
525 /* if multiple files extracted to stdout, insert separators! */
526 separator= ((!outfiles || curlx_strequal(outfiles, "-")) && urlnum > 1);
528 /* Here's looping around each globbed URL */
529 for(i = 0 ; i < urlnum; i++) {
534 struct OutStruct outs;
535 struct InStruct input;
536 struct timeval retrystart;
537 curl_off_t uploadfilesize;
538 long retry_numretries;
539 long retry_sleep_default;
543 #ifdef HAVE_LIBMETALINK
544 int metalink_next_res = 0;
545 #endif /* HAVE_LIBMETALINK */
550 uploadfilesize = -1; /* -1 means unknown */
552 /* default output stream is stdout */
553 memset(&outs, 0, sizeof(struct OutStruct));
554 outs.stream = stdout;
555 outs.config = config;
557 #ifdef HAVE_LIBMETALINK
559 /* For Metalink download, use name in Metalink file as
561 outfile = strdup(mlfile->filename);
563 res = CURLE_OUT_OF_MEMORY;
566 this_url = strdup(mlres->url);
568 res = CURLE_OUT_OF_MEMORY;
573 #endif /* HAVE_LIBMETALINK */
575 res = glob_next_url(&this_url, urls);
580 this_url = strdup(urlnode->url);
582 res = CURLE_OUT_OF_MEMORY;
592 outfile = strdup(outfiles);
594 res = CURLE_OUT_OF_MEMORY;
598 #ifdef HAVE_LIBMETALINK
600 #endif /* HAVE_LIBMETALINK */
602 if((urlnode->flags&GETOUT_USEREMOTE) ||
603 (outfile && !curlx_strequal("-", outfile)) ) {
606 * We have specified a file name to store the result in, or we have
607 * decided we want to use the remote file name.
611 /* extract the file name from the URL */
612 res = get_url_file_name(&outfile, this_url);
615 if((!outfile || !*outfile) && !config->content_disposition) {
616 helpf(config->errors, "Remote file name has no length!\n");
617 res = CURLE_WRITE_ERROR;
620 #if defined(MSDOS) || defined(WIN32)
621 /* For DOS and WIN32, we do some major replacing of
622 bad characters in the file name before using it */
623 outfile = sanitize_dos_name(outfile);
625 res = CURLE_OUT_OF_MEMORY;
628 #endif /* MSDOS || WIN32 */
631 /* fill '#1' ... '#9' terms from URL pattern */
632 char *storefile = outfile;
633 res = glob_match_url(&outfile, storefile, urls);
634 Curl_safefree(storefile);
637 warnf(config, "bad output glob!\n");
642 /* Create the directory hierarchy, if not pre-existent to a multiple
645 if(config->create_dirs || metalink) {
646 res = create_dir_hierarchy(outfile, config->errors);
647 /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
648 if(res == CURLE_WRITE_ERROR)
655 if((urlnode->flags & GETOUT_USEREMOTE)
656 && config->content_disposition) {
657 /* Our header callback MIGHT set the filename */
658 DEBUGASSERT(!outs.filename);
661 if(config->resume_from_current) {
662 /* We're told to continue from where we are now. Get the size
663 of the file as it is now and open it for append instead */
664 struct_stat fileinfo;
665 /* VMS -- Danger, the filesize is only valid for stream files */
666 if(0 == stat(outfile, &fileinfo))
667 /* set offset to current file size: */
668 config->resume_from = fileinfo.st_size;
670 /* let offset be 0 */
671 config->resume_from = 0;
674 if(config->resume_from) {
675 /* open file for output: */
676 FILE *file = fopen(outfile, config->resume_from?"ab":"wb");
678 helpf(config->errors, "Can't open '%s'!\n", outfile);
679 res = CURLE_WRITE_ERROR;
684 outs.init = config->resume_from;
687 outs.stream = NULL; /* open when needed */
689 outs.filename = outfile;
693 if(uploadfile && !stdin_upload(uploadfile)) {
695 * We have specified a file to upload and it isn't "-".
697 struct_stat fileinfo;
699 this_url = add_file_name_to_url(curl, this_url, uploadfile);
701 res = CURLE_OUT_OF_MEMORY;
706 * Reading binary from files can be a problem... Only FIXED, VAR
707 * etc WITHOUT implied CC will work Others need a \n appended to a
710 * - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
711 * fixed file with implied CC needs to have a byte added for every
712 * record processed, this can by derived from Filesize & recordsize
713 * for VARiable record files the records need to be counted! for
714 * every record add 1 for linefeed and subtract 2 for the record
715 * header for VARIABLE header files only the bare record data needs
716 * to be considered with one appended if implied CC
719 infd = open(uploadfile, O_RDONLY | O_BINARY);
720 if((infd == -1) || fstat(infd, &fileinfo)) {
721 helpf(config->errors, "Can't open '%s'!\n", uploadfile);
726 res = CURLE_READ_ERROR;
731 /* we ignore file size for char/block devices, sockets, etc. */
732 if(S_ISREG(fileinfo.st_mode))
733 uploadfilesize = fileinfo.st_size;
736 else if(uploadfile && stdin_upload(uploadfile)) {
737 /* count to see if there are more than one auth bit set
738 in the authtype field */
741 while(bitcheck < 32) {
742 if(config->authtype & (1UL << bitcheck++)) {
745 /* more than one, we're done! */
752 * If the user has also selected --anyauth or --proxy-anyauth
753 * we should warn him/her.
755 if(config->proxyanyauth || (authbits>1)) {
757 "Using --anyauth or --proxy-anyauth with upload from stdin"
758 " involves a big risk of it not working. Use a temporary"
759 " file or a fixed auth type instead!\n");
762 DEBUGASSERT(infdopen == FALSE);
763 DEBUGASSERT(infd == STDIN_FILENO);
766 if(curlx_strequal(uploadfile, ".")) {
767 if(curlx_nonblock((curl_socket_t)infd, TRUE) < 0)
769 "fcntl failed on fd=%d: %s\n", infd, strerror(errno));
773 if(uploadfile && config->resume_from_current)
774 config->resume_from = -1; /* -1 will then force get-it-yourself */
776 if(output_expected(this_url, uploadfile)
777 && outs.stream && isatty(fileno(outs.stream)))
778 /* we send the output to a tty, therefore we switch off the progress
780 config->noprogress = config->isatty = TRUE;
782 if(urlnum > 1 && !(config->mute)) {
783 fprintf(config->errors, "\n[%d/%d]: %s --> %s\n",
784 i+1, urlnum, this_url, outfile ? outfile : "<stdout>");
786 printf("%s%s\n", CURLseparator, this_url);
790 /* Find out whether the url contains a file name */
791 const char *pc = strstr(this_url, "://");
798 pc = strrchr(pc, '/'); /* check for a slash */
801 /* there is a slash present in the URL */
804 /* Ouch, there's already a question mark in the URL string, we
805 then append the data with an ampersand separator instead! */
809 * Then append ? followed by the get fields to the url.
811 urlbuffer = malloc(strlen(this_url) + strlen(httpgetfields) + 3);
813 res = CURLE_OUT_OF_MEMORY;
817 sprintf(urlbuffer, "%s%c%s", this_url, sep, httpgetfields);
819 /* Append / before the ? to create a well-formed url
820 if the url contains a hostname only
822 sprintf(urlbuffer, "%s/?%s", this_url, httpgetfields);
824 Curl_safefree(this_url); /* free previous URL */
825 this_url = urlbuffer; /* use our new URL instead! */
829 config->errors = stderr;
831 if((!outfile || !strcmp(outfile, "-")) && !config->use_ascii) {
832 /* We get the output to stdout and we have not got the ASCII/text
833 flag, then set stdout to be binary */
837 if(config->tcp_nodelay)
838 my_setopt(curl, CURLOPT_TCP_NODELAY, 1);
841 my_setopt(curl, CURLOPT_WRITEDATA, &outs);
842 /* what call to write */
843 my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
847 input.config = config;
848 /* Note that if CURLOPT_READFUNCTION is fread (the default), then
849 * lib/telnet.c will Curl_poll() on the input file descriptor
850 * rather then calling the READFUNCTION at regular intervals.
851 * The circumstances in which it is preferable to enable this
852 * behaviour, by omitting to set the READFUNCTION & READDATA options,
853 * have not been determined.
855 my_setopt(curl, CURLOPT_READDATA, &input);
856 /* what call to read */
857 my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
859 /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
860 CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
861 my_setopt(curl, CURLOPT_SEEKDATA, &input);
862 my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
864 if(config->recvpersecond)
865 /* tell libcurl to use a smaller sized buffer as it allows us to
866 make better sleeps! 7.9.9 stuff! */
867 my_setopt(curl, CURLOPT_BUFFERSIZE, config->recvpersecond);
869 /* size of uploaded file: */
870 if(uploadfilesize != -1)
871 my_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
872 my_setopt_str(curl, CURLOPT_URL, this_url); /* what to fetch */
873 my_setopt(curl, CURLOPT_NOPROGRESS, config->noprogress);
874 if(config->no_body) {
875 my_setopt(curl, CURLOPT_NOBODY, 1);
876 my_setopt(curl, CURLOPT_HEADER, 1);
879 my_setopt(curl, CURLOPT_HEADER, config->include_headers);
881 #if !defined(CURL_DISABLE_PROXY)
883 /* TODO: Make this a run-time check instead of compile-time one. */
885 my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
886 my_setopt_str(curl, CURLOPT_PROXYUSERPWD, config->proxyuserpwd);
888 /* new in libcurl 7.3 */
889 my_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config->proxytunnel);
891 /* new in libcurl 7.5 */
893 my_setopt_enum(curl, CURLOPT_PROXYTYPE, config->proxyver);
895 /* new in libcurl 7.10 */
896 if(config->socksproxy) {
897 my_setopt_str(curl, CURLOPT_PROXY, config->socksproxy);
898 my_setopt_enum(curl, CURLOPT_PROXYTYPE, config->socksver);
901 /* new in libcurl 7.10.6 */
902 if(config->proxyanyauth)
903 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
904 (long) CURLAUTH_ANY);
905 else if(config->proxynegotiate)
906 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
907 (long) CURLAUTH_GSSNEGOTIATE);
908 else if(config->proxyntlm)
909 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
910 (long) CURLAUTH_NTLM);
911 else if(config->proxydigest)
912 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
913 (long) CURLAUTH_DIGEST);
914 else if(config->proxybasic)
915 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
916 (long) CURLAUTH_BASIC);
918 /* new in libcurl 7.19.4 */
919 my_setopt(curl, CURLOPT_NOPROXY, config->noproxy);
923 my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror);
924 my_setopt(curl, CURLOPT_UPLOAD, uploadfile?TRUE:FALSE);
925 my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly);
926 my_setopt(curl, CURLOPT_APPEND, config->ftp_append);
928 if(config->netrc_opt)
929 my_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
930 else if(config->netrc || config->netrc_file)
931 my_setopt(curl, CURLOPT_NETRC, CURL_NETRC_REQUIRED);
933 my_setopt(curl, CURLOPT_NETRC, CURL_NETRC_IGNORED);
935 if(config->netrc_file)
936 my_setopt(curl, CURLOPT_NETRC_FILE, config->netrc_file);
938 my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii);
939 my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
940 my_setopt_str(curl, CURLOPT_RANGE, config->range);
941 my_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
942 my_setopt(curl, CURLOPT_TIMEOUT, config->timeout);
944 if(built_in_protos & CURLPROTO_HTTP) {
948 my_setopt(curl, CURLOPT_FOLLOWLOCATION,
949 config->followlocation);
950 my_setopt(curl, CURLOPT_UNRESTRICTED_AUTH,
951 config->unrestricted_auth);
953 switch(config->httpreq) {
954 case HTTPREQ_SIMPLEPOST:
955 my_setopt_str(curl, CURLOPT_POSTFIELDS,
957 my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
958 config->postfieldsize);
961 my_setopt_httppost(curl, CURLOPT_HTTPPOST, config->httppost);
967 my_setopt_str(curl, CURLOPT_REFERER, config->referer);
968 my_setopt(curl, CURLOPT_AUTOREFERER, config->autoreferer);
969 my_setopt_str(curl, CURLOPT_USERAGENT, config->useragent);
970 my_setopt_slist(curl, CURLOPT_HTTPHEADER, config->headers);
972 /* new in libcurl 7.5 */
973 my_setopt(curl, CURLOPT_MAXREDIRS, config->maxredirs);
975 /* new in libcurl 7.9.1 */
976 if(config->httpversion)
977 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion);
979 /* new in libcurl 7.10.6 (default is Basic) */
981 my_setopt_bitmask(curl, CURLOPT_HTTPAUTH, (long) config->authtype);
983 /* curl 7.19.1 (the 301 version existed in 7.18.2),
984 303 was added in 7.26.0 */
986 postRedir |= CURL_REDIR_POST_301;
988 postRedir |= CURL_REDIR_POST_302;
990 postRedir |= CURL_REDIR_POST_303;
991 my_setopt(curl, CURLOPT_POSTREDIR, postRedir);
993 /* new in libcurl 7.21.6 */
995 my_setopt_str(curl, CURLOPT_ACCEPT_ENCODING, "");
997 /* new in libcurl 7.21.6 */
998 if(config->tr_encoding)
999 my_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1);
1001 } /* (built_in_protos & CURLPROTO_HTTP) */
1003 my_setopt_str(curl, CURLOPT_FTPPORT, config->ftpport);
1004 my_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,
1005 config->low_speed_limit);
1006 my_setopt(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
1007 my_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE,
1008 config->sendpersecond);
1009 my_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE,
1010 config->recvpersecond);
1011 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE,
1012 config->use_resume?config->resume_from:0);
1014 my_setopt(curl, CURLOPT_SSLCERT, config->cert);
1015 my_setopt_str(curl, CURLOPT_SSLCERTTYPE, config->cert_type);
1016 my_setopt(curl, CURLOPT_SSLKEY, config->key);
1017 my_setopt_str(curl, CURLOPT_SSLKEYTYPE, config->key_type);
1018 my_setopt_str(curl, CURLOPT_KEYPASSWD, config->key_passwd);
1020 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1022 /* SSH and SSL private key uses same command-line option */
1023 /* new in libcurl 7.16.1 */
1024 my_setopt_str(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key);
1025 /* new in libcurl 7.16.1 */
1026 my_setopt_str(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey);
1028 /* new in libcurl 7.17.1: SSH host key md5 checking allows us
1029 to fail if we are not talking to who we think we should */
1030 my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,
1031 config->hostpubmd5);
1035 my_setopt_str(curl, CURLOPT_CAINFO, config->cacert);
1037 my_setopt_str(curl, CURLOPT_CAPATH, config->capath);
1039 my_setopt_str(curl, CURLOPT_CRLFILE, config->crlfile);
1041 if(curlinfo->features & CURL_VERSION_SSL) {
1042 if(config->insecure_ok) {
1043 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
1044 my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
1047 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1048 /* libcurl default is strict verifyhost -> 2L */
1049 /* my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); */
1053 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1054 if(!config->insecure_ok) {
1057 res = CURLE_OUT_OF_MEMORY;
1060 file = aprintf("%s/%sssh/known_hosts", home, DOT_CHAR);
1062 /* new in curl 7.19.6 */
1063 res = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file);
1065 if(res == CURLE_UNKNOWN_OPTION)
1066 /* libssh2 version older than 1.1.1 */
1069 Curl_safefree(home);
1076 if(config->no_body || config->remote_time) {
1077 /* no body or use remote time */
1078 my_setopt(curl, CURLOPT_FILETIME, TRUE);
1081 my_setopt(curl, CURLOPT_CRLF, config->crlf);
1082 my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
1083 my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
1084 my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
1086 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
1088 /* TODO: Make this a run-time check instead of compile-time one. */
1091 my_setopt_str(curl, CURLOPT_COOKIE, config->cookie);
1093 if(config->cookiefile)
1094 my_setopt_str(curl, CURLOPT_COOKIEFILE, config->cookiefile);
1096 /* new in libcurl 7.9 */
1097 if(config->cookiejar)
1098 my_setopt_str(curl, CURLOPT_COOKIEJAR, config->cookiejar);
1100 /* new in libcurl 7.9.7 */
1101 my_setopt(curl, CURLOPT_COOKIESESSION, config->cookiesession);
1105 my_setopt_enum(curl, CURLOPT_SSLVERSION, config->ssl_version);
1106 my_setopt_enum(curl, CURLOPT_TIMECONDITION, config->timecond);
1107 my_setopt(curl, CURLOPT_TIMEVALUE, config->condtime);
1108 my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
1109 my_setopt(curl, CURLOPT_STDERR, config->errors);
1111 /* three new ones in libcurl 7.3: */
1112 my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
1113 my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
1115 progressbarinit(&progressbar, config);
1116 if((config->progressmode == CURL_PROGRESS_BAR) &&
1117 !config->noprogress && !config->mute) {
1118 /* we want the alternative style, then we have to implement it
1120 my_setopt(curl, CURLOPT_PROGRESSFUNCTION, tool_progress_cb);
1121 my_setopt(curl, CURLOPT_PROGRESSDATA, &progressbar);
1124 /* new in libcurl 7.6.2: */
1125 my_setopt_slist(curl, CURLOPT_TELNETOPTIONS, config->telnet_options);
1127 /* new in libcurl 7.7: */
1128 my_setopt_str(curl, CURLOPT_RANDOM_FILE, config->random_file);
1129 my_setopt(curl, CURLOPT_EGDSOCKET, config->egd_file);
1130 my_setopt(curl, CURLOPT_CONNECTTIMEOUT, config->connecttimeout);
1132 if(config->cipher_list)
1133 my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
1135 /* new in libcurl 7.9.2: */
1136 if(config->disable_epsv)
1138 my_setopt(curl, CURLOPT_FTP_USE_EPSV, FALSE);
1140 /* new in libcurl 7.10.5 */
1141 if(config->disable_eprt)
1143 my_setopt(curl, CURLOPT_FTP_USE_EPRT, FALSE);
1145 if(config->tracetype != TRACE_NONE) {
1146 my_setopt(curl, CURLOPT_DEBUGFUNCTION, tool_debug_cb);
1147 my_setopt(curl, CURLOPT_DEBUGDATA, config);
1148 my_setopt(curl, CURLOPT_VERBOSE, TRUE);
1151 /* new in curl 7.9.3 */
1152 if(config->engine) {
1153 res = res_setopt_str(curl, CURLOPT_SSLENGINE, config->engine);
1156 my_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1);
1159 /* new in curl 7.10.7, extended in 7.19.4 but this only sets 0 or 1 */
1160 my_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
1161 config->ftp_create_dirs);
1163 /* new in curl 7.10.8 */
1164 if(config->max_filesize)
1165 my_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
1166 config->max_filesize);
1168 if(4 == config->ip_version)
1169 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1170 else if(6 == config->ip_version)
1171 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
1173 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER);
1175 /* new in curl 7.15.5 */
1176 if(config->ftp_ssl_reqd)
1177 my_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
1179 /* new in curl 7.11.0 */
1180 else if(config->ftp_ssl)
1181 my_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
1183 /* new in curl 7.16.0 */
1184 else if(config->ftp_ssl_control)
1185 my_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_CONTROL);
1187 /* new in curl 7.16.1 */
1188 if(config->ftp_ssl_ccc)
1189 my_setopt_enum(curl, CURLOPT_FTP_SSL_CCC, config->ftp_ssl_ccc_mode);
1191 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1193 /* TODO: Make this a run-time check instead of compile-time one. */
1195 /* new in curl 7.19.4 */
1196 if(config->socks5_gssapi_service)
1197 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_SERVICE,
1198 config->socks5_gssapi_service);
1200 /* new in curl 7.19.4 */
1201 if(config->socks5_gssapi_nec)
1202 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_NEC,
1203 config->socks5_gssapi_nec);
1207 my_setopt_str(curl, CURLOPT_FTP_ACCOUNT, config->ftp_account);
1209 my_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, config->ignorecl);
1212 my_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, config->ftp_skip_ip);
1215 my_setopt(curl, CURLOPT_FTP_FILEMETHOD, config->ftp_filemethod);
1218 if(config->localport) {
1219 my_setopt(curl, CURLOPT_LOCALPORT, config->localport);
1220 my_setopt_str(curl, CURLOPT_LOCALPORTRANGE,
1221 config->localportrange);
1225 my_setopt_str(curl, CURLOPT_FTP_ALTERNATIVE_TO_USER,
1226 config->ftp_alternative_to_user);
1229 if(config->disable_sessionid)
1230 my_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE,
1231 !config->disable_sessionid);
1235 my_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, FALSE);
1236 my_setopt(curl, CURLOPT_HTTP_TRANSFER_DECODING, FALSE);
1240 if(!config->nokeepalive) {
1241 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
1242 if(config->alivetime != 0) {
1243 #if !defined(TCP_KEEPIDLE) || !defined(TCP_KEEPINTVL)
1244 warnf(config, "Keep-alive functionality somewhat crippled due to "
1245 "missing support in your operating system!\n");
1247 my_setopt(curl, CURLOPT_TCP_KEEPIDLE, config->alivetime);
1248 my_setopt(curl, CURLOPT_TCP_KEEPINTVL, config->alivetime);
1252 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
1255 if(config->tftp_blksize)
1256 my_setopt(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
1258 if(config->mail_from)
1259 my_setopt_str(curl, CURLOPT_MAIL_FROM, config->mail_from);
1261 if(config->mail_rcpt)
1262 my_setopt_slist(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
1265 if(config->ftp_pret)
1266 my_setopt(curl, CURLOPT_FTP_USE_PRET, TRUE);
1268 if(config->proto_present)
1269 my_setopt_flags(curl, CURLOPT_PROTOCOLS, config->proto);
1270 if(config->proto_redir_present)
1271 my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
1273 hdrdata.urlnode = urlnode;
1274 hdrdata.outs = &outs;
1275 hdrdata.heads = &heads;
1277 my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
1278 my_setopt(curl, CURLOPT_HEADERDATA, &hdrdata);
1282 my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
1285 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1286 if(config->tls_username)
1287 my_setopt_str(curl, CURLOPT_TLSAUTH_USERNAME,
1288 config->tls_username);
1289 if(config->tls_password)
1290 my_setopt_str(curl, CURLOPT_TLSAUTH_PASSWORD,
1291 config->tls_password);
1292 if(config->tls_authtype)
1293 my_setopt_str(curl, CURLOPT_TLSAUTH_TYPE,
1294 config->tls_authtype);
1298 if(config->gssapi_delegation)
1299 my_setopt_str(curl, CURLOPT_GSSAPI_DELEGATION,
1300 config->gssapi_delegation);
1303 if(config->ssl_allow_beast)
1304 my_setopt(curl, CURLOPT_SSL_OPTIONS, (long)CURLSSLOPT_ALLOW_BEAST);
1306 if(config->mail_auth)
1307 my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
1309 /* initialize retry vars for loop below */
1310 retry_sleep_default = (config->retry_delay) ?
1311 config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
1313 retry_numretries = config->req_retry;
1314 retry_sleep = retry_sleep_default; /* ms */
1315 retrystart = tvnow();
1317 #ifndef CURL_DISABLE_LIBCURL_OPTION
1318 res = easysrc_perform();
1325 res = curl_easy_perform(curl);
1327 if(config->content_disposition && outs.stream && !config->mute &&
1329 printf("curl: Saved to filename '%s'\n", outs.filename);
1331 /* if retry-max-time is non-zero, make sure we haven't exceeded the
1333 if(retry_numretries &&
1334 (!config->retry_maxtime ||
1335 (tvdiff(tvnow(), retrystart) <
1336 config->retry_maxtime*1000L)) ) {
1342 RETRY_LAST /* not used */
1345 if((CURLE_OPERATION_TIMEDOUT == res) ||
1346 (CURLE_COULDNT_RESOLVE_HOST == res) ||
1347 (CURLE_COULDNT_RESOLVE_PROXY == res) ||
1348 (CURLE_FTP_ACCEPT_TIMEOUT == res))
1349 /* retry timeout always */
1350 retry = RETRY_TIMEOUT;
1351 else if((CURLE_OK == res) ||
1352 (config->failonerror &&
1353 (CURLE_HTTP_RETURNED_ERROR == res))) {
1354 /* If it returned OK. _or_ failonerror was enabled and it
1355 returned due to such an error, check for HTTP transient
1356 errors to retry on. */
1357 char *effective_url = NULL;
1358 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1360 checkprefix("http", effective_url)) {
1361 /* This was HTTP(S) */
1362 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1365 case 500: /* Internal Server Error */
1366 case 502: /* Bad Gateway */
1367 case 503: /* Service Unavailable */
1368 case 504: /* Gateway Timeout */
1371 * At this point, we have already written data to the output
1372 * file (or terminal). If we write to a file, we must rewind
1373 * or close/re-open the file so that the next attempt starts
1374 * over from the beginning.
1376 * TODO: similar action for the upload case. We might need
1377 * to start over reading from a previous point if we have
1378 * uploaded something when this was returned.
1385 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1387 if(response/100 == 4)
1389 * This is typically when the FTP server only allows a certain
1390 * amount of users and we are not one of them. All 4xx codes
1397 static const char * const m[]={
1398 NULL, "timeout", "HTTP error", "FTP error"
1400 warnf(config, "Transient problem: %s "
1401 "Will retry in %ld seconds. "
1402 "%ld retries left.\n",
1403 m[retry], retry_sleep/1000L, retry_numretries);
1405 tool_go_sleep(retry_sleep);
1407 if(!config->retry_delay) {
1409 if(retry_sleep > RETRY_SLEEP_MAX)
1410 retry_sleep = RETRY_SLEEP_MAX;
1412 if(outs.bytes && outs.filename) {
1413 /* We have written data to a output file, we truncate file
1416 fprintf(config->errors, "Throwing away %"
1417 CURL_FORMAT_CURL_OFF_T " bytes\n",
1419 fflush(outs.stream);
1420 /* truncate file at the position where we started appending */
1421 #ifdef HAVE_FTRUNCATE
1422 if(ftruncate( fileno(outs.stream), outs.init)) {
1423 /* when truncate fails, we can't just append as then we'll
1424 create something strange, bail out */
1426 fprintf(config->errors,
1427 "failed to truncate, exiting\n");
1428 res = CURLE_WRITE_ERROR;
1431 /* now seek to the end of the file, the position where we
1432 just truncated the file in a large file-safe way */
1433 fseek(outs.stream, 0, SEEK_END);
1435 /* ftruncate is not available, so just reposition the file
1436 to the location we would have truncated it. This won't
1437 work properly with large files on 32-bit systems, but
1438 most of those will have ftruncate. */
1439 fseek(outs.stream, (long)outs.init, SEEK_SET);
1441 outs.bytes = 0; /* clear for next round */
1443 continue; /* curl_easy_perform loop */
1445 } /* if retry_numretries */
1446 #ifdef HAVE_LIBMETALINK
1448 /* Metalink: Decide to try the next resource or
1449 not. Basically, we want to try the next resource if
1450 download was not successful. */
1452 if(CURLE_OK == res) {
1453 /* TODO We want to try next resource when download was
1454 not successful. How to know that? */
1455 char *effective_url = NULL;
1456 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1458 curlx_strnequal(effective_url, "http", 4)) {
1459 /* This was HTTP(S) */
1460 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1461 if(response != 200 && response != 206) {
1462 metalink_next_res = 1;
1467 metalink_next_res = 1;
1469 #endif /* HAVE_LIBMETALINK */
1471 /* In all ordinary cases, just break out of loop here */
1472 break; /* curl_easy_perform loop */
1476 if((config->progressmode == CURL_PROGRESS_BAR) &&
1478 /* if the custom progress bar has been displayed, we output a
1480 fputs("\n", progressbar.out);
1482 if(config->writeout)
1483 ourWriteOut(curl, &outs, config->writeout);
1485 if(config->writeenv)
1489 ** Code within this loop may jump directly here to label 'show_error'
1490 ** in order to display an error message for CURLcode stored in 'res'
1491 ** variable and exit loop once that necessary writing and cleanup
1492 ** in label 'quit_urls' has been done.
1498 if(is_vms_shell()) {
1499 /* VMS DCL shell behavior */
1500 if(!config->showerror)
1501 vms_show = VMSSTS_HIDE;
1505 if(res && config->showerror) {
1506 fprintf(config->errors, "curl: (%d) %s\n", res, (errorbuffer[0]) ?
1507 errorbuffer : curl_easy_strerror((CURLcode)res));
1508 if(res == CURLE_SSL_CACERT)
1509 fprintf(config->errors, "%s%s",
1510 CURL_CA_CERT_ERRORMSG1, CURL_CA_CERT_ERRORMSG2);
1513 /* Fall through comment to 'quit_urls' label */
1516 ** Upon error condition and always that a message has already been
1517 ** displayed, code within this loop may jump directly here to label
1518 ** 'quit_urls' otherwise it should jump to 'show_error' label above.
1520 ** When 'res' variable is _not_ CURLE_OK loop will exit once that
1521 ** all code following 'quit_urls' has been executed. Otherwise it
1522 ** will loop to the beginning from where it may exit if there are
1523 ** no more urls left.
1528 /* Set file extended attributes */
1529 if(!res && config->xattr && outs.fopened && outs.stream) {
1530 int rc = fwrite_xattr(curl, fileno(outs.stream));
1532 warnf(config, "Error setting extended attributes: %s\n",
1536 /* Close the file */
1537 if(outs.fopened && outs.stream) {
1538 int rc = fclose(outs.stream);
1540 /* something went wrong in the writing process */
1541 res = CURLE_WRITE_ERROR;
1542 fprintf(config->errors, "(%d) Failed writing body\n", res);
1545 else if(!outs.s_isreg && outs.stream) {
1546 /* Dump standard stream buffered data */
1547 int rc = fflush(outs.stream);
1549 /* something went wrong in the writing process */
1550 res = CURLE_WRITE_ERROR;
1551 fprintf(config->errors, "(%d) Failed writing body\n", res);
1556 if(!res && outs.s_isreg && outs.filename) {
1557 /* Set the url (up to 80 chars) as comment for the file */
1558 if(strlen(url) > 78)
1560 SetComment(outs.filename, url);
1565 /* File time can only be set _after_ the file has been closed */
1566 if(!res && config->remote_time && outs.s_isreg && outs.filename) {
1567 /* Ask libcurl if we got a remote file time */
1569 curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
1571 struct utimbuf times;
1572 times.actime = (time_t)filetime;
1573 times.modtime = (time_t)filetime;
1574 utime(outs.filename, ×); /* set the time we got */
1579 #ifdef HAVE_LIBMETALINK
1580 if(!metalink && res == CURLE_OK && outs.filename) {
1581 /* Check the content-type header field and if it indicates
1582 Metalink file, parse it and add getout for them. */
1584 curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &content_type);
1585 if(content_type && check_metalink_content_type(content_type)) {
1586 if(!(config->mute)) {
1587 fprintf(config->errors, "\nParsing Metalink file: %s\n",
1590 if(parse_metalink(config, outs.filename) == 0)
1591 fprintf(config->errors,
1592 "Metalink file is parsed successfully\n");
1594 fprintf(config->errors, "Could not parse Metalink file.\n");
1597 # ifdef METALINK_HASH_CHECK
1598 else if(metalink && res == CURLE_OK && !metalink_next_res) {
1599 int rv = metalink_check_hash(config, mlfile, outs.filename);
1601 metalink_next_res = 1;
1604 # endif /* METALINK_HASH_CHECK */
1605 #endif /* HAVE_LIBMETALINK */
1607 /* No more business with this output struct */
1608 if(outs.alloc_filename)
1609 Curl_safefree(outs.filename);
1610 memset(&outs, 0, sizeof(struct OutStruct));
1612 /* Free loop-local allocated memory and close loop-local opened fd */
1614 Curl_safefree(outfile);
1615 Curl_safefree(this_url);
1620 infd = STDIN_FILENO;
1623 #ifdef HAVE_LIBMETALINK
1625 /* Should exit if error is fatal. */
1626 if(is_fatal_error(res)) {
1629 if(!metalink_next_res)
1631 mlres = mlres->next;
1633 /* TODO If metalink_next_res is 1 and mlres is NULL,
1634 * set res to error code
1639 #endif /* HAVE_LIBMETALINK */
1641 /* when url globbing, exit loop upon critical error */
1642 if(is_fatal_error(res))
1646 /* when not url globbing, exit loop upon any error */
1649 } /* loop to the next URL */
1651 /* Free loop-local allocated memory */
1653 Curl_safefree(uploadfile);
1656 /* Free list of remaining URLs */
1662 /* when file globbing, exit loop upon critical error */
1663 if(is_fatal_error(res))
1667 /* when not file globbing, exit loop upon any error */
1670 } /* loop to the next globbed upload file */
1672 /* Free loop-local allocated memory */
1674 Curl_safefree(outfiles);
1677 /* Free list of globbed upload files */
1678 glob_cleanup(inglob);
1682 /* Free this URL node data without destroying the
1683 the node itself nor modifying next pointer. */
1684 Curl_safefree(urlnode->url);
1685 Curl_safefree(urlnode->outfile);
1686 Curl_safefree(urlnode->infile);
1690 ** Bail out upon critical errors
1692 if(is_fatal_error(res))
1695 } /* for-loop through all URLs */
1698 ** Nested loops end here.
1703 /* Free function-local referenced allocated memory */
1704 Curl_safefree(httpgetfields);
1706 /* Free list of given URLs */
1707 clean_getout(config);
1709 /* Cleanup the curl handle now that our
1710 progressbar struct is still in scope */
1712 curl_easy_cleanup(curl);
1713 config->easy = curl = NULL;
1715 #ifndef CURL_DISABLE_LIBCURL_OPTION
1719 /* Close function-local opened file descriptors */
1721 if(heads.fopened && heads.stream)
1722 fclose(heads.stream);
1723 if(heads.alloc_filename)
1724 Curl_safefree(heads.filename);
1726 if(config->trace_fopened && config->trace_stream)
1727 fclose(config->trace_stream);
1729 #ifndef CURL_DISABLE_LIBCURL_OPTION
1730 /* Dump the libcurl code if previously enabled.
1731 NOTE: that this function relies on config->errors amongst other things
1732 so not everything can be closed and cleaned before this is called */
1733 dumpeasysrc(config);
1736 if(config->errors_fopened && config->errors)
1737 fclose(config->errors);
1739 #ifdef HAVE_LIBMETALINK
1740 /* Release metalink related resources here */
1741 clean_metalink(config);
1742 #endif /* HAVE_LIBMETALINK */
1744 main_free(); /* cleanup */