1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
24 #include <curl/curl.h>
36 #elif defined(HAVE_SYS_UTIME_H)
37 # include <sys/utime.h>
46 #define ENABLE_CURLX_PRINTF
47 /* use our own printf() functions */
55 #ifdef USE_ENVIRONMENT
56 # include "writeenv.h"
59 #include "tool_binmode.h"
60 #include "tool_cfgable.h"
61 #include "tool_cb_dbg.h"
62 #include "tool_cb_hdr.h"
63 #include "tool_cb_prg.h"
64 #include "tool_cb_rea.h"
65 #include "tool_cb_see.h"
66 #include "tool_cb_skt.h"
67 #include "tool_cb_wrt.h"
68 #include "tool_dirhie.h"
69 #include "tool_doswin.h"
70 #include "tool_easysrc.h"
71 #include "tool_getparam.h"
72 #include "tool_helpers.h"
73 #include "tool_libinfo.h"
74 #include "tool_main.h"
75 #include "tool_msgs.h"
76 #include "tool_operate.h"
77 #include "tool_operhlp.h"
78 #include "tool_parsecfg.h"
79 #include "tool_setopt.h"
80 #include "tool_sleep.h"
81 #include "tool_urlglob.h"
83 #include "memdebug.h" /* keep this as LAST include */
85 #define CURLseparator "--_curl_--"
88 /* since O_BINARY as used in bitmasks, setting it to zero makes it usable in
89 source code but yet it doesn't ruin anything */
93 #define CURL_CA_CERT_ERRORMSG1 \
94 "More details here: http://curl.haxx.se/docs/sslcerts.html\n\n" \
95 "curl performs SSL certificate verification by default, " \
96 "using a \"bundle\"\n" \
97 " of Certificate Authority (CA) public keys (CA certs). If the default\n" \
98 " bundle file isn't adequate, you can specify an alternate file\n" \
99 " using the --cacert option.\n"
101 #define CURL_CA_CERT_ERRORMSG2 \
102 "If this HTTPS server uses a certificate signed by a CA represented in\n" \
103 " the bundle, the certificate verification probably failed due to a\n" \
104 " problem with the certificate (it might be expired, or the name might\n" \
105 " not match the domain name in the URL).\n" \
106 "If you'd like to turn off curl's verification of the certificate, use\n" \
107 " the -k (or --insecure) option.\n"
109 int operate(struct Configurable *config, int argc, argv_item_t argv[])
111 char errorbuffer[CURL_ERROR_SIZE];
112 struct ProgressData progressbar;
113 struct getout *urlnode;
115 struct OutStruct heads;
118 char *httpgetfields = NULL;
124 errorbuffer[0] = '\0';
125 /* default headers output stream is stdout */
126 memset(&heads, 0, sizeof(struct OutStruct));
127 heads.stream = stdout;
128 heads.config = config;
130 memory_tracking_init();
133 ** Initialize curl library - do not call any libcurl functions before
134 ** this point. Note that the memory_tracking_init() magic above is an
135 ** exception, but then that's not part of the official public API.
137 if(main_init() != CURLE_OK) {
138 helpf(config->errors, "error initializing curl library\n");
139 return CURLE_FAILED_INIT;
142 /* Get libcurl info right away */
143 if(get_libcurl_info() != CURLE_OK) {
144 helpf(config->errors, "error retrieving curl library information\n");
146 return CURLE_FAILED_INIT;
149 /* Get a curl handle to use for all forthcoming curl transfers */
150 curl = curl_easy_init();
152 helpf(config->errors, "error initializing curl easy handle\n");
154 return CURLE_FAILED_INIT;
159 ** Beyond this point no return'ing from this function allowed.
160 ** Jump to label 'quit_curl' in order to abandon this function
161 ** from outside of nested loops further down below.
164 /* setup proper locale from environment */
165 #ifdef HAVE_SETLOCALE
166 setlocale(LC_ALL, "");
170 config->postfieldsize = -1;
171 config->showerror = TRUE;
172 config->use_httpget = FALSE;
173 config->create_dirs = FALSE;
174 config->maxredirs = DEFAULT_MAXREDIRS;
175 config->proto = CURLPROTO_ALL; /* FIXME: better to read from library */
176 config->proto_present = FALSE;
177 config->proto_redir =
178 CURLPROTO_ALL & ~(CURLPROTO_FILE|CURLPROTO_SCP); /* not FILE or SCP */
179 config->proto_redir_present = FALSE;
182 (!curlx_strnequal("--", argv[1], 2) && (argv[1][0] == '-')) &&
183 strchr(argv[1], 'q')) {
185 * The first flag, that is not a verbose name, but a shortname
186 * and it includes the 'q' flag!
191 parseconfig(NULL, config); /* ignore possible failure */
194 if((argc < 2) && !config->url_list) {
195 helpf(config->errors, NULL);
196 res = CURLE_FAILED_INIT;
201 for(i = 1, stillflags = TRUE; i < argc; i++) {
203 ('-' == argv[i][0])) {
206 char *origopt = argv[i];
208 char *flag = argv[i];
210 if(curlx_strequal("--", argv[i]))
211 /* this indicates the end of the flags and thus enables the
212 following (URL) argument to start with -. */
215 nextarg = (i < (argc-1)) ? argv[i+1] : NULL;
217 res = getparameter(flag, nextarg, &passarg, config);
219 int retval = CURLE_OK;
220 if(res != PARAM_HELP_REQUESTED) {
221 const char *reason = param2text(res);
222 helpf(config->errors, "option %s: %s\n", origopt, reason);
223 retval = CURLE_FAILED_INIT;
229 if(passarg) /* we're supposed to skip this */
235 /* just add the URL please */
236 res = getparameter((char *)"--url", argv[i], &used, config);
242 if((!config->url_list || !config->url_list->url) && !config->list_engines) {
243 helpf(config->errors, "no URL specified!\n");
244 res = CURLE_FAILED_INIT;
248 if(!config->useragent)
249 config->useragent = my_useragent();
250 if(!config->useragent) {
251 helpf(config->errors, "out of memory\n");
252 res = CURLE_OUT_OF_MEMORY;
256 /* On WIN32 we can't set the path to curl-ca-bundle.crt
257 * at compile time. So we look here for the file in two ways:
258 * 1: look at the environment variable CURL_CA_BUNDLE for a path
259 * 2: if #1 isn't found, use the windows API function SearchPath()
260 * to find it along the app's path (includes app's dir and CWD)
262 * We support the environment variable thing for non-Windows platforms
263 * too. Just for the sake of it.
265 if(!config->cacert &&
267 !config->insecure_ok) {
269 env = curlx_getenv("CURL_CA_BUNDLE");
271 config->cacert = strdup(env);
272 if(!config->cacert) {
274 helpf(config->errors, "out of memory\n");
275 res = CURLE_OUT_OF_MEMORY;
280 env = curlx_getenv("SSL_CERT_DIR");
282 config->capath = strdup(env);
283 if(!config->capath) {
285 helpf(config->errors, "out of memory\n");
286 res = CURLE_OUT_OF_MEMORY;
291 env = curlx_getenv("SSL_CERT_FILE");
293 config->cacert = strdup(env);
294 if(!config->cacert) {
296 helpf(config->errors, "out of memory\n");
297 res = CURLE_OUT_OF_MEMORY;
308 res = FindWin32CACert(config, "curl-ca-bundle.crt");
315 if(config->postfields) {
316 if(config->use_httpget) {
317 /* Use the postfields data for a http get */
318 httpgetfields = strdup(config->postfields);
319 Curl_safefree(config->postfields);
321 helpf(config->errors, "out of memory\n");
322 res = CURLE_OUT_OF_MEMORY;
325 if(SetHTTPrequest(config,
326 (config->no_body?HTTPREQ_HEAD:HTTPREQ_GET),
333 if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq)) {
340 /* This is the first entry added to easysrc and it initializes the slist */
341 easysrc = curl_slist_append(easysrc, "CURL *hnd = curl_easy_init();");
343 helpf(config->errors, "out of memory\n");
344 res = CURLE_OUT_OF_MEMORY;
348 if(config->list_engines) {
349 struct curl_slist *engines = NULL;
350 curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
351 list_engines(engines);
352 curl_slist_free_all(engines);
357 /* Single header file for all URLs */
358 if(config->headerfile) {
359 /* open file for output: */
360 if(!curlx_strequal(config->headerfile, "-")) {
361 FILE *newfile = fopen(config->headerfile, "wb");
363 warnf(config, "Failed to open %s\n", config->headerfile);
364 res = CURLE_WRITE_ERROR;
368 heads.filename = config->headerfile;
369 heads.s_isreg = TRUE;
370 heads.fopened = TRUE;
371 heads.stream = newfile;
377 ** Nested loops start here.
380 /* loop through the list of given URLs */
382 for(urlnode = config->url_list; urlnode; urlnode = urlnode->next) {
384 int up; /* upload file counter within a single upload glob */
385 char *infiles; /* might be a glob pattern */
394 /* urlnode->url is the full URL (it might be NULL) */
397 /* This node has no URL. Free node data without destroying the
398 node itself nor modifying next pointer and continue to next */
399 Curl_safefree(urlnode->outfile);
400 Curl_safefree(urlnode->infile);
402 continue; /* next URL please */
405 /* save outfile pattern before expansion */
406 if(urlnode->outfile) {
407 outfiles = strdup(urlnode->outfile);
409 helpf(config->errors, "out of memory\n");
410 res = CURLE_OUT_OF_MEMORY;
415 infiles = urlnode->infile;
417 if(!config->globoff && infiles) {
418 /* Unless explicitly shut off */
419 res = glob_url(&inglob, infiles, &infilenum,
420 config->showerror?config->errors:NULL);
422 Curl_safefree(outfiles);
427 /* Here's the loop for uploading multiple files within the same
428 single globbed string. If no upload, we enter the loop once anyway. */
429 for(up = 0 ;; up++) {
431 char *uploadfile; /* a single file, never a glob */
445 uploadfile = glob_next_url(inglob);
447 uploadfile = strdup(infiles);
454 if(!config->globoff) {
455 /* Unless explicitly shut off, we expand '{...}' and '[...]'
456 expressions and return total number of URLs in pattern set */
457 res = glob_url(&urls, urlnode->url, &urlnum,
458 config->showerror?config->errors:NULL);
460 Curl_safefree(uploadfile);
465 urlnum = 1; /* without globbing, this is a single URL */
467 /* if multiple files extracted to stdout, insert separators! */
468 separator= ((!outfiles || curlx_strequal(outfiles, "-")) && urlnum > 1);
470 /* Here's looping around each globbed URL */
476 struct OutStruct outs;
477 struct InStruct input;
478 struct timeval retrystart;
479 curl_off_t uploadfilesize;
480 long retry_numretries;
481 long retry_sleep_default;
488 uploadfilesize = -1; /* -1 means unknown */
490 /* default output stream is stdout */
491 memset(&outs, 0, sizeof(struct OutStruct));
492 outs.stream = stdout;
493 outs.config = config;
496 this_url = glob_next_url(urls);
498 this_url = strdup(urlnode->url);
506 outfile = strdup(outfiles);
508 res = CURLE_OUT_OF_MEMORY;
513 if((urlnode->flags&GETOUT_USEREMOTE) ||
514 (outfile && !curlx_strequal("-", outfile)) ) {
517 * We have specified a file name to store the result in, or we have
518 * decided we want to use the remote file name.
522 /* extract the file name from the URL */
523 res = get_url_file_name(&outfile, this_url);
526 if((!outfile || !*outfile) && !config->content_disposition) {
527 helpf(config->errors, "Remote file name has no length!\n");
528 res = CURLE_WRITE_ERROR;
531 #if defined(MSDOS) || defined(WIN32)
532 /* For DOS and WIN32, we do some major replacing of
533 bad characters in the file name before using it */
534 outfile = sanitize_dos_name(outfile);
536 res = CURLE_OUT_OF_MEMORY;
539 #endif /* MSDOS || WIN32 */
542 /* fill '#1' ... '#9' terms from URL pattern */
543 char *storefile = outfile;
544 outfile = glob_match_url(storefile, urls);
545 Curl_safefree(storefile);
548 warnf(config, "bad output glob!\n");
549 res = CURLE_FAILED_INIT;
554 /* Create the directory hierarchy, if not pre-existent to a multiple
557 if(config->create_dirs) {
558 res = create_dir_hierarchy(outfile, config->errors);
559 /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
560 if(res == CURLE_WRITE_ERROR)
567 if((urlnode->flags & GETOUT_USEREMOTE)
568 && config->content_disposition) {
569 /* Our header callback sets the filename */
570 DEBUGASSERT(!outs.filename);
573 if(config->resume_from_current) {
574 /* We're told to continue from where we are now. Get the size
575 of the file as it is now and open it for append instead */
576 struct_stat fileinfo;
577 /* VMS -- Danger, the filesize is only valid for stream files */
578 if(0 == stat(outfile, &fileinfo))
579 /* set offset to current file size: */
580 config->resume_from = fileinfo.st_size;
582 /* let offset be 0 */
583 config->resume_from = 0;
586 if(config->resume_from) {
587 /* open file for output: */
588 FILE *file = fopen(outfile, config->resume_from?"ab":"wb");
590 helpf(config->errors, "Can't open '%s'!\n", outfile);
591 res = CURLE_WRITE_ERROR;
596 outs.init = config->resume_from;
599 outs.stream = NULL; /* open when needed */
601 outs.filename = outfile;
606 if(uploadfile && !stdin_upload(uploadfile)) {
608 * We have specified a file to upload and it isn't "-".
610 struct_stat fileinfo;
612 this_url = add_file_name_to_url(curl, this_url, uploadfile);
614 res = CURLE_OUT_OF_MEMORY;
619 * Reading binary from files can be a problem... Only FIXED, VAR
620 * etc WITHOUT implied CC will work Others need a \n appended to a
623 * - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
624 * fixed file with implied CC needs to have a byte added for every
625 * record processed, this can by derived from Filesize & recordsize
626 * for VARiable record files the records need to be counted! for
627 * every record add 1 for linefeed and subtract 2 for the record
628 * header for VARIABLE header files only the bare record data needs
629 * to be considered with one appended if implied CC
632 infd = open(uploadfile, O_RDONLY | O_BINARY);
633 if((infd == -1) || fstat(infd, &fileinfo)) {
634 helpf(config->errors, "Can't open '%s'!\n", uploadfile);
639 res = CURLE_READ_ERROR;
644 /* we ignore file size for char/block devices, sockets, etc. */
645 if(S_ISREG(fileinfo.st_mode))
646 uploadfilesize = fileinfo.st_size;
649 else if(uploadfile && stdin_upload(uploadfile)) {
650 /* count to see if there are more than one auth bit set
651 in the authtype field */
654 while(bitcheck < 32) {
655 if(config->authtype & (1 << bitcheck++)) {
658 /* more than one, we're done! */
665 * If the user has also selected --anyauth or --proxy-anyauth
666 * we should warn him/her.
668 if(config->proxyanyauth || (authbits>1)) {
670 "Using --anyauth or --proxy-anyauth with upload from stdin"
671 " involves a big risk of it not working. Use a temporary"
672 " file or a fixed auth type instead!\n");
675 DEBUGASSERT(infdopen == FALSE);
676 DEBUGASSERT(infd == STDIN_FILENO);
679 if(curlx_strequal(uploadfile, ".")) {
680 if(curlx_nonblock((curl_socket_t)infd, TRUE) < 0)
682 "fcntl failed on fd=%d: %s\n", infd, strerror(errno));
686 if(uploadfile && config->resume_from_current)
687 config->resume_from = -1; /* -1 will then force get-it-yourself */
689 if(output_expected(this_url, uploadfile)
690 && outs.stream && isatty(fileno(outs.stream)))
691 /* we send the output to a tty, therefore we switch off the progress
693 config->noprogress = config->isatty = TRUE;
695 if(urlnum > 1 && !(config->mute)) {
696 fprintf(config->errors, "\n[%d/%d]: %s --> %s\n",
697 i+1, urlnum, this_url, outfile ? outfile : "<stdout>");
699 printf("%s%s\n", CURLseparator, this_url);
703 /* Find out whether the url contains a file name */
704 const char *pc = strstr(this_url, "://");
711 pc = strrchr(pc, '/'); /* check for a slash */
714 /* there is a slash present in the URL */
717 /* Ouch, there's already a question mark in the URL string, we
718 then append the data with an ampersand separator instead! */
722 * Then append ? followed by the get fields to the url.
724 urlbuffer = malloc(strlen(this_url) + strlen(httpgetfields) + 3);
726 res = CURLE_OUT_OF_MEMORY;
730 sprintf(urlbuffer, "%s%c%s", this_url, sep, httpgetfields);
732 /* Append / before the ? to create a well-formed url
733 if the url contains a hostname only
735 sprintf(urlbuffer, "%s/?%s", this_url, httpgetfields);
737 Curl_safefree(this_url); /* free previous URL */
738 this_url = urlbuffer; /* use our new URL instead! */
742 config->errors = stderr;
744 if((!outfile || !strcmp(outfile, "-")) && !config->use_ascii) {
745 /* We get the output to stdout and we have not got the ASCII/text
746 flag, then set stdout to be binary */
750 if(config->tcp_nodelay)
751 my_setopt(curl, CURLOPT_TCP_NODELAY, 1);
754 my_setopt(curl, CURLOPT_WRITEDATA, &outs);
755 /* what call to write */
756 my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
760 input.config = config;
761 my_setopt(curl, CURLOPT_READDATA, &input);
762 /* what call to read */
763 if((outfile && !curlx_strequal("-", outfile)) ||
764 !checkprefix("telnet:", this_url))
765 my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
767 /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
768 CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
769 my_setopt(curl, CURLOPT_SEEKDATA, &input);
770 my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
772 if(config->recvpersecond)
773 /* tell libcurl to use a smaller sized buffer as it allows us to
774 make better sleeps! 7.9.9 stuff! */
775 my_setopt(curl, CURLOPT_BUFFERSIZE, config->recvpersecond);
777 /* size of uploaded file: */
778 if(uploadfilesize != -1)
779 my_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
780 my_setopt_str(curl, CURLOPT_URL, this_url); /* what to fetch */
781 my_setopt(curl, CURLOPT_NOPROGRESS, config->noprogress);
782 if(config->no_body) {
783 my_setopt(curl, CURLOPT_NOBODY, 1);
784 my_setopt(curl, CURLOPT_HEADER, 1);
787 my_setopt(curl, CURLOPT_HEADER, config->include_headers);
789 #if !defined(CURL_DISABLE_PROXY)
791 /* TODO: Make this a run-time check instead of compile-time one. */
793 my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
794 my_setopt_str(curl, CURLOPT_PROXYUSERPWD, config->proxyuserpwd);
796 /* new in libcurl 7.3 */
797 my_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config->proxytunnel);
799 /* new in libcurl 7.5 */
801 my_setopt(curl, CURLOPT_PROXYTYPE, config->proxyver);
803 /* new in libcurl 7.10 */
804 if(config->socksproxy) {
805 my_setopt_str(curl, CURLOPT_PROXY, config->socksproxy);
806 my_setopt(curl, CURLOPT_PROXYTYPE, config->socksver);
809 /* new in libcurl 7.10.6 */
810 if(config->proxyanyauth)
811 my_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
812 else if(config->proxynegotiate)
813 my_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_GSSNEGOTIATE);
814 else if(config->proxyntlm)
815 my_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
816 else if(config->proxydigest)
817 my_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_DIGEST);
818 else if(config->proxybasic)
819 my_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
821 /* new in libcurl 7.19.4 */
822 my_setopt(curl, CURLOPT_NOPROXY, config->noproxy);
826 my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror);
827 my_setopt(curl, CURLOPT_UPLOAD, uploadfile?TRUE:FALSE);
828 my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly);
829 my_setopt(curl, CURLOPT_APPEND, config->ftp_append);
831 if(config->netrc_opt)
832 my_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
833 else if(config->netrc || config->netrc_file)
834 my_setopt(curl, CURLOPT_NETRC, CURL_NETRC_REQUIRED);
836 my_setopt(curl, CURLOPT_NETRC, CURL_NETRC_IGNORED);
838 if(config->netrc_file)
839 my_setopt(curl, CURLOPT_NETRC_FILE, config->netrc_file);
841 my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii);
842 my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
843 my_setopt_str(curl, CURLOPT_RANGE, config->range);
844 my_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
845 my_setopt(curl, CURLOPT_TIMEOUT, config->timeout);
847 if(built_in_protos & CURLPROTO_HTTP) {
849 my_setopt(curl, CURLOPT_FOLLOWLOCATION,
850 config->followlocation);
851 my_setopt(curl, CURLOPT_UNRESTRICTED_AUTH,
852 config->unrestricted_auth);
854 switch(config->httpreq) {
855 case HTTPREQ_SIMPLEPOST:
856 my_setopt_str(curl, CURLOPT_POSTFIELDS,
858 my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
859 config->postfieldsize);
862 my_setopt(curl, CURLOPT_HTTPPOST, config->httppost);
868 my_setopt_str(curl, CURLOPT_REFERER, config->referer);
869 my_setopt(curl, CURLOPT_AUTOREFERER, config->autoreferer);
870 my_setopt_str(curl, CURLOPT_USERAGENT, config->useragent);
871 my_setopt(curl, CURLOPT_HTTPHEADER, config->headers);
873 /* new in libcurl 7.5 */
874 my_setopt(curl, CURLOPT_MAXREDIRS, config->maxredirs);
876 /* new in libcurl 7.9.1 */
877 if(config->httpversion)
878 my_setopt(curl, CURLOPT_HTTP_VERSION, config->httpversion);
880 /* new in libcurl 7.10.6 (default is Basic) */
882 my_setopt(curl, CURLOPT_HTTPAUTH, config->authtype);
884 /* curl 7.19.1 (the 301 version existed in 7.18.2) */
885 my_setopt(curl, CURLOPT_POSTREDIR, config->post301 |
886 (config->post302 ? CURL_REDIR_POST_302 : FALSE));
888 /* new in libcurl 7.21.6 */
890 my_setopt_str(curl, CURLOPT_ACCEPT_ENCODING, "");
892 /* new in libcurl 7.21.6 */
893 if(config->tr_encoding)
894 my_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1);
896 } /* (built_in_protos & CURLPROTO_HTTP) */
898 my_setopt_str(curl, CURLOPT_FTPPORT, config->ftpport);
899 my_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,
900 config->low_speed_limit);
901 my_setopt(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
902 my_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE,
903 config->sendpersecond);
904 my_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE,
905 config->recvpersecond);
906 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE,
907 config->use_resume?config->resume_from:0);
909 my_setopt(curl, CURLOPT_SSLCERT, config->cert);
910 my_setopt_str(curl, CURLOPT_SSLCERTTYPE, config->cert_type);
911 my_setopt(curl, CURLOPT_SSLKEY, config->key);
912 my_setopt_str(curl, CURLOPT_SSLKEYTYPE, config->key_type);
913 my_setopt_str(curl, CURLOPT_KEYPASSWD, config->key_passwd);
915 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
917 /* SSH and SSL private key uses same command-line option */
918 /* new in libcurl 7.16.1 */
919 my_setopt_str(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key);
920 /* new in libcurl 7.16.1 */
921 my_setopt_str(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey);
923 /* new in libcurl 7.17.1: SSH host key md5 checking allows us
924 to fail if we are not talking to who we think we should */
925 my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,
930 my_setopt_str(curl, CURLOPT_CAINFO, config->cacert);
932 my_setopt_str(curl, CURLOPT_CAPATH, config->capath);
934 my_setopt_str(curl, CURLOPT_CRLFILE, config->crlfile);
936 if(curlinfo->features & CURL_VERSION_SSL) {
937 if(config->insecure_ok) {
938 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
939 my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
942 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
943 /* libcurl default is strict verifyhost -> 2L */
944 /* my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); */
948 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
949 if(!config->insecure_ok) {
952 res = CURLE_OUT_OF_MEMORY;
955 file = aprintf("%s/%sssh/known_hosts", home, DOT_CHAR);
957 /* new in curl 7.19.6 */
958 res = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file);
960 if(res == CURLE_UNKNOWN_OPTION)
961 /* libssh2 version older than 1.1.1 */
971 if(config->no_body || config->remote_time) {
972 /* no body or use remote time */
973 my_setopt(curl, CURLOPT_FILETIME, TRUE);
976 my_setopt(curl, CURLOPT_CRLF, config->crlf);
977 my_setopt(curl, CURLOPT_QUOTE, config->quote);
978 my_setopt(curl, CURLOPT_POSTQUOTE, config->postquote);
979 my_setopt(curl, CURLOPT_PREQUOTE, config->prequote);
981 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
983 /* TODO: Make this a run-time check instead of compile-time one. */
986 my_setopt_str(curl, CURLOPT_COOKIE, config->cookie);
988 if(config->cookiefile)
989 my_setopt_str(curl, CURLOPT_COOKIEFILE, config->cookiefile);
991 /* new in libcurl 7.9 */
992 if(config->cookiejar)
993 my_setopt_str(curl, CURLOPT_COOKIEJAR, config->cookiejar);
995 /* new in libcurl 7.9.7 */
996 my_setopt(curl, CURLOPT_COOKIESESSION, config->cookiesession);
1000 my_setopt(curl, CURLOPT_SSLVERSION, config->ssl_version);
1001 my_setopt(curl, CURLOPT_TIMECONDITION, config->timecond);
1002 my_setopt(curl, CURLOPT_TIMEVALUE, config->condtime);
1003 my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
1004 my_setopt(curl, CURLOPT_STDERR, config->errors);
1006 /* three new ones in libcurl 7.3: */
1007 my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
1008 my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
1010 progressbarinit(&progressbar, config);
1011 if((config->progressmode == CURL_PROGRESS_BAR) &&
1012 !config->noprogress && !config->mute) {
1013 /* we want the alternative style, then we have to implement it
1015 my_setopt(curl, CURLOPT_PROGRESSFUNCTION, tool_progress_cb);
1016 my_setopt(curl, CURLOPT_PROGRESSDATA, &progressbar);
1019 /* new in libcurl 7.6.2: */
1020 my_setopt(curl, CURLOPT_TELNETOPTIONS, config->telnet_options);
1022 /* new in libcurl 7.7: */
1023 my_setopt_str(curl, CURLOPT_RANDOM_FILE, config->random_file);
1024 my_setopt(curl, CURLOPT_EGDSOCKET, config->egd_file);
1025 my_setopt(curl, CURLOPT_CONNECTTIMEOUT, config->connecttimeout);
1027 if(config->cipher_list)
1028 my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
1030 /* new in libcurl 7.9.2: */
1031 if(config->disable_epsv)
1033 my_setopt(curl, CURLOPT_FTP_USE_EPSV, FALSE);
1035 /* new in libcurl 7.10.5 */
1036 if(config->disable_eprt)
1038 my_setopt(curl, CURLOPT_FTP_USE_EPRT, FALSE);
1040 if(config->tracetype != TRACE_NONE) {
1041 my_setopt(curl, CURLOPT_DEBUGFUNCTION, tool_debug_cb);
1042 my_setopt(curl, CURLOPT_DEBUGDATA, config);
1043 my_setopt(curl, CURLOPT_VERBOSE, TRUE);
1046 /* new in curl 7.9.3 */
1047 if(config->engine) {
1048 res = res_setopt_str(curl, CURLOPT_SSLENGINE, config->engine);
1051 my_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1);
1054 /* new in curl 7.10.7, extended in 7.19.4 but this only sets 0 or 1 */
1055 my_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
1056 config->ftp_create_dirs);
1058 /* new in curl 7.10.8 */
1059 if(config->max_filesize)
1060 my_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
1061 config->max_filesize);
1063 if(4 == config->ip_version)
1064 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1065 else if(6 == config->ip_version)
1066 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
1068 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER);
1070 /* new in curl 7.15.5 */
1071 if(config->ftp_ssl_reqd)
1072 my_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
1074 /* new in curl 7.11.0 */
1075 else if(config->ftp_ssl)
1076 my_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
1078 /* new in curl 7.16.0 */
1079 else if(config->ftp_ssl_control)
1080 my_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_CONTROL);
1082 /* new in curl 7.16.1 */
1083 if(config->ftp_ssl_ccc)
1084 my_setopt(curl, CURLOPT_FTP_SSL_CCC, config->ftp_ssl_ccc_mode);
1086 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1088 /* TODO: Make this a run-time check instead of compile-time one. */
1090 /* new in curl 7.19.4 */
1091 if(config->socks5_gssapi_service)
1092 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_SERVICE,
1093 config->socks5_gssapi_service);
1095 /* new in curl 7.19.4 */
1096 if(config->socks5_gssapi_nec)
1097 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_NEC,
1098 config->socks5_gssapi_nec);
1102 my_setopt_str(curl, CURLOPT_FTP_ACCOUNT, config->ftp_account);
1104 my_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, config->ignorecl);
1107 my_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, config->ftp_skip_ip);
1110 my_setopt(curl, CURLOPT_FTP_FILEMETHOD, config->ftp_filemethod);
1113 if(config->localport) {
1114 my_setopt(curl, CURLOPT_LOCALPORT, config->localport);
1115 my_setopt_str(curl, CURLOPT_LOCALPORTRANGE,
1116 config->localportrange);
1120 my_setopt_str(curl, CURLOPT_FTP_ALTERNATIVE_TO_USER,
1121 config->ftp_alternative_to_user);
1124 if(config->disable_sessionid)
1125 my_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE,
1126 !config->disable_sessionid);
1130 my_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, FALSE);
1131 my_setopt(curl, CURLOPT_HTTP_TRANSFER_DECODING, FALSE);
1135 if(!config->nokeepalive) {
1136 my_setopt(curl, CURLOPT_SOCKOPTFUNCTION, tool_sockopt_cb);
1137 my_setopt(curl, CURLOPT_SOCKOPTDATA, config);
1141 if(config->tftp_blksize)
1142 my_setopt(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
1144 if(config->mail_from)
1145 my_setopt_str(curl, CURLOPT_MAIL_FROM, config->mail_from);
1147 if(config->mail_rcpt)
1148 my_setopt(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
1151 if(config->ftp_pret)
1152 my_setopt(curl, CURLOPT_FTP_USE_PRET, TRUE);
1154 if(config->proto_present)
1155 my_setopt(curl, CURLOPT_PROTOCOLS, config->proto);
1156 if(config->proto_redir_present)
1157 my_setopt(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
1159 if((urlnode->flags & GETOUT_USEREMOTE)
1160 && config->content_disposition) {
1161 my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
1162 my_setopt(curl, CURLOPT_HEADERDATA, &outs);
1165 /* if HEADERFUNCTION was set to something in the previous loop, it
1166 is important that we set it (back) to NULL now */
1167 my_setopt(curl, CURLOPT_HEADERFUNCTION, NULL);
1168 my_setopt(curl, CURLOPT_HEADERDATA, config->headerfile?&heads:NULL);
1173 my_setopt(curl, CURLOPT_RESOLVE, config->resolve);
1176 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1177 if(config->tls_username)
1178 my_setopt_str(curl, CURLOPT_TLSAUTH_USERNAME,
1179 config->tls_username);
1180 if(config->tls_password)
1181 my_setopt_str(curl, CURLOPT_TLSAUTH_PASSWORD,
1182 config->tls_password);
1183 if(config->tls_authtype)
1184 my_setopt_str(curl, CURLOPT_TLSAUTH_TYPE,
1185 config->tls_authtype);
1189 if(config->gssapi_delegation)
1190 my_setopt_str(curl, CURLOPT_GSSAPI_DELEGATION,
1191 config->gssapi_delegation);
1193 /* initialize retry vars for loop below */
1194 retry_sleep_default = (config->retry_delay) ?
1195 config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
1197 retry_numretries = config->req_retry;
1198 retry_sleep = retry_sleep_default; /* ms */
1199 retrystart = cutil_tvnow();
1202 res = curl_easy_perform(curl);
1203 if(!curl_slist_append(easysrc, "ret = curl_easy_perform(hnd);")) {
1204 res = CURLE_OUT_OF_MEMORY;
1208 if(config->content_disposition && outs.stream && !config->mute &&
1210 printf("curl: Saved to filename '%s'\n", outs.filename);
1212 /* if retry-max-time is non-zero, make sure we haven't exceeded the
1214 if(retry_numretries &&
1215 (!config->retry_maxtime ||
1216 (cutil_tvdiff(cutil_tvnow(), retrystart)<
1217 config->retry_maxtime*1000L)) ) {
1223 RETRY_LAST /* not used */
1226 if(CURLE_OPERATION_TIMEDOUT == res)
1227 /* retry timeout always */
1228 retry = RETRY_TIMEOUT;
1229 else if((CURLE_OK == res) ||
1230 (config->failonerror &&
1231 (CURLE_HTTP_RETURNED_ERROR == res))) {
1232 /* If it returned OK. _or_ failonerror was enabled and it
1233 returned due to such an error, check for HTTP transient
1234 errors to retry on. */
1235 char *effective_url = NULL;
1236 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1238 checkprefix("http", effective_url)) {
1239 /* This was HTTP(S) */
1240 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1243 case 500: /* Internal Server Error */
1244 case 502: /* Bad Gateway */
1245 case 503: /* Service Unavailable */
1246 case 504: /* Gateway Timeout */
1249 * At this point, we have already written data to the output
1250 * file (or terminal). If we write to a file, we must rewind
1251 * or close/re-open the file so that the next attempt starts
1252 * over from the beginning.
1254 * TODO: similar action for the upload case. We might need
1255 * to start over reading from a previous point if we have
1256 * uploaded something when this was returned.
1263 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1265 if(response/100 == 4)
1267 * This is typically when the FTP server only allows a certain
1268 * amount of users and we are not one of them. All 4xx codes
1275 static const char * const m[]={
1276 NULL, "timeout", "HTTP error", "FTP error"
1278 warnf(config, "Transient problem: %s "
1279 "Will retry in %ld seconds. "
1280 "%ld retries left.\n",
1281 m[retry], retry_sleep/1000L, retry_numretries);
1283 tool_go_sleep(retry_sleep);
1285 if(!config->retry_delay) {
1287 if(retry_sleep > RETRY_SLEEP_MAX)
1288 retry_sleep = RETRY_SLEEP_MAX;
1290 if(outs.bytes && outs.filename) {
1291 /* We have written data to a output file, we truncate file
1294 fprintf(config->errors, "Throwing away %"
1295 CURL_FORMAT_CURL_OFF_T " bytes\n",
1297 fflush(outs.stream);
1298 /* truncate file at the position where we started appending */
1299 #ifdef HAVE_FTRUNCATE
1300 if(ftruncate( fileno(outs.stream), outs.init)) {
1301 /* when truncate fails, we can't just append as then we'll
1302 create something strange, bail out */
1304 fprintf(config->errors,
1305 "failed to truncate, exiting\n");
1306 res = CURLE_WRITE_ERROR;
1309 /* now seek to the end of the file, the position where we
1310 just truncated the file in a large file-safe way */
1311 fseek(outs.stream, 0, SEEK_END);
1313 /* ftruncate is not available, so just reposition the file
1314 to the location we would have truncated it. This won't
1315 work properly with large files on 32-bit systems, but
1316 most of those will have ftruncate. */
1317 fseek(outs.stream, (long)outs.init, SEEK_SET);
1319 outs.bytes = 0; /* clear for next round */
1321 continue; /* curl_easy_perform loop */
1323 } /* if retry_numretries */
1325 /* In all ordinary cases, just break out of loop here */
1326 break; /* curl_easy_perform loop */
1330 if((config->progressmode == CURL_PROGRESS_BAR) &&
1332 /* if the custom progress bar has been displayed, we output a
1334 fputs("\n", progressbar.out);
1336 if(config->writeout)
1337 ourWriteOut(curl, config->writeout);
1338 #ifdef USE_ENVIRONMENT
1339 if(config->writeenv)
1344 ** Code within this loop may jump directly here to label 'show_error'
1345 ** in order to display an error message for CURLcode stored in 'res'
1346 ** variable and exit loop once that necessary writing and cleanup
1347 ** in label 'quit_urls' has been done.
1353 if(is_vms_shell()) {
1354 /* VMS DCL shell behavior */
1355 if(!config->showerror)
1356 vms_show = VMSSTS_HIDE;
1360 if(res && config->showerror) {
1361 fprintf(config->errors, "curl: (%d) %s\n", res, (errorbuffer[0]) ?
1362 errorbuffer : curl_easy_strerror((CURLcode)res));
1363 if(res == CURLE_SSL_CACERT)
1364 fprintf(config->errors, "%s%s",
1365 CURL_CA_CERT_ERRORMSG1, CURL_CA_CERT_ERRORMSG2);
1368 /* Fall through comment to 'quit_urls' label */
1371 ** Upon error condition and always that a message has already been
1372 ** displayed, code within this loop may jump directly here to label
1373 ** 'quit_urls' otherwise it should jump to 'show_error' label above.
1375 ** When 'res' variable is _not_ CURLE_OK loop will exit once that
1376 ** all code following 'quit_urls' has been executed. Otherwise it
1377 ** will loop to the beginning from where it may exit if there are
1378 ** no more urls left.
1383 /* Set file extended attributes */
1384 if(!res && config->xattr && outs.fopened && outs.stream) {
1385 int rc = fwrite_xattr(curl, fileno(outs.stream));
1387 warnf(config, "Error setting extended attributes: %s\n",
1391 /* Close the file */
1392 if(outs.fopened && outs.stream) {
1393 int rc = fclose(outs.stream);
1395 /* something went wrong in the writing process */
1396 res = CURLE_WRITE_ERROR;
1397 fprintf(config->errors, "(%d) Failed writing body\n", res);
1400 else if(!outs.s_isreg && outs.stream) {
1401 /* Dump standard stream buffered data */
1402 int rc = fflush(outs.stream);
1404 /* something went wrong in the writing process */
1405 res = CURLE_WRITE_ERROR;
1406 fprintf(config->errors, "(%d) Failed writing body\n", res);
1411 if(!res && outs.s_isreg && outs.filename) {
1412 /* Set the url (up to 80 chars) as comment for the file */
1413 if(strlen(url) > 78)
1415 SetComment(outs.filename, url);
1420 /* File time can only be set _after_ the file has been closed */
1421 if(!res && config->remote_time && outs.s_isreg && outs.filename) {
1422 /* Ask libcurl if we got a remote file time */
1424 curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
1426 struct utimbuf times;
1427 times.actime = (time_t)filetime;
1428 times.modtime = (time_t)filetime;
1429 utime(outs.filename, ×); /* set the time we got */
1433 /* No more business with this output struct */
1434 if(outs.alloc_filename)
1435 Curl_safefree(outs.filename);
1436 memset(&outs, 0, sizeof(struct OutStruct));
1438 /* Free loop-local allocated memory and close loop-local opened fd */
1440 Curl_safefree(outfile);
1441 Curl_safefree(this_url);
1446 infd = STDIN_FILENO;
1449 /* upon error exit loop */
1453 } /* loop to the next URL */
1455 /* Free loop-local allocated memory */
1457 Curl_safefree(uploadfile);
1460 /* Free list of remaining URLs */
1465 /* upon error exit loop */
1469 } /* loop to the next globbed upload file */
1471 /* Free loop-local allocated memory */
1473 Curl_safefree(outfiles);
1476 /* Free list of globbed upload files */
1477 glob_cleanup(inglob);
1481 /* Free this URL node data without destroying the
1482 the node itself nor modifying next pointer. */
1483 Curl_safefree(urlnode->url);
1484 Curl_safefree(urlnode->outfile);
1485 Curl_safefree(urlnode->infile);
1488 /* TODO: Should CURLE_SSL_CACERT be included as critical error ? */
1491 ** Bail out upon critical errors
1494 case CURLE_FAILED_INIT:
1495 case CURLE_OUT_OF_MEMORY:
1496 case CURLE_FUNCTION_NOT_FOUND:
1497 case CURLE_BAD_FUNCTION_ARGUMENT:
1500 /* Merrily loop to next URL */
1504 } /* for-loop through all URLs */
1507 ** Nested loops end here.
1512 /* Free function-local referenced allocated memory */
1513 Curl_safefree(httpgetfields);
1515 /* Free list of given URLs */
1516 clean_getout(config);
1518 /* Cleanup the curl handle now that our
1519 progressbar struct is still in scope */
1521 curl_easy_cleanup(curl);
1522 config->easy = curl = NULL;
1525 curl_slist_append(easysrc, "curl_easy_cleanup(hnd);");
1527 /* Close function-local opened file descriptors */
1529 if(heads.fopened && heads.stream)
1530 fclose(heads.stream);
1531 if(heads.alloc_filename)
1532 Curl_safefree(heads.filename);
1534 if(config->trace_fopened && config->trace_stream)
1535 fclose(config->trace_stream);
1537 /* Dump the libcurl code if previously enabled.
1538 NOTE: that this function relies on config->errors amongst other things
1539 so not everything can be closed and cleaned before this is called */
1540 dumpeasysrc(config);
1542 if(config->errors_fopened && config->errors)
1543 fclose(config->errors);
1545 main_free(); /* cleanup */