blob: 6f1525e8b1d3e215a07b9e22801131afc009e90c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22#include "tool_setup.h"
23
24#ifdef HAVE_FCNTL_H
25# include <fcntl.h>
26#endif
27
28#ifdef HAVE_UTIME_H
29# include <utime.h>
30#elif defined(HAVE_SYS_UTIME_H)
31# include <sys/utime.h>
32#endif
33
34#ifdef HAVE_LOCALE_H
35# include <locale.h>
36#endif
37
38#ifdef __VMS
39# include <fabdef.h>
40#endif
41
42#include "strcase.h"
43
44#define ENABLE_CURLX_PRINTF
45/* use our own printf() functions */
46#include "curlx.h"
47
48#include "tool_binmode.h"
49#include "tool_cfgable.h"
50#include "tool_cb_dbg.h"
51#include "tool_cb_hdr.h"
52#include "tool_cb_prg.h"
53#include "tool_cb_rea.h"
54#include "tool_cb_see.h"
55#include "tool_cb_wrt.h"
56#include "tool_dirhie.h"
57#include "tool_doswin.h"
58#include "tool_easysrc.h"
59#include "tool_getparam.h"
60#include "tool_helpers.h"
61#include "tool_homedir.h"
62#include "tool_libinfo.h"
63#include "tool_main.h"
64#include "tool_metalink.h"
65#include "tool_msgs.h"
66#include "tool_operate.h"
67#include "tool_operhlp.h"
68#include "tool_paramhlp.h"
69#include "tool_parsecfg.h"
70#include "tool_setopt.h"
71#include "tool_sleep.h"
72#include "tool_urlglob.h"
73#include "tool_util.h"
74#include "tool_writeout.h"
75#include "tool_xattr.h"
76#include "tool_vms.h"
77#include "tool_help.h"
78#include "tool_hugehelp.h"
79
80#include "memdebug.h" /* keep this as LAST include */
81
82#ifdef CURLDEBUG
83/* libcurl's debug builds provide an extra function */
84CURLcode curl_easy_perform_ev(CURL *easy);
85#endif
86
87#define CURLseparator "--_curl_--"
88
89#ifndef O_BINARY
90/* since O_BINARY as used in bitmasks, setting it to zero makes it usable in
91 source code but yet it doesn't ruin anything */
92# define O_BINARY 0
93#endif
94
95#define CURL_CA_CERT_ERRORMSG1 \
96 "More details here: https://curl.haxx.se/docs/sslcerts.html\n\n" \
97 "curl performs SSL certificate verification by default, " \
98 "using a \"bundle\"\n" \
99 " of Certificate Authority (CA) public keys (CA certs). If the default\n" \
100 " bundle file isn't adequate, you can specify an alternate file\n" \
101 " using the --cacert option.\n"
102
103#define CURL_CA_CERT_ERRORMSG2 \
104 "If this HTTPS server uses a certificate signed by a CA represented in\n" \
105 " the bundle, the certificate verification probably failed due to a\n" \
106 " problem with the certificate (it might be expired, or the name might\n" \
107 " not match the domain name in the URL).\n" \
108 "If you'd like to turn off curl's verification of the certificate, use\n" \
109 " the -k (or --insecure) option.\n"
110
111static bool is_fatal_error(CURLcode code)
112{
113 switch(code) {
114 /* TODO: Should CURLE_SSL_CACERT be included as critical error ? */
115 case CURLE_FAILED_INIT:
116 case CURLE_OUT_OF_MEMORY:
117 case CURLE_UNKNOWN_OPTION:
118 case CURLE_FUNCTION_NOT_FOUND:
119 case CURLE_BAD_FUNCTION_ARGUMENT:
120 /* critical error */
121 return TRUE;
122 default:
123 break;
124 }
125
126 /* no error or not critical */
127 return FALSE;
128}
129
130#ifdef __VMS
131/*
132 * get_vms_file_size does what it takes to get the real size of the file
133 *
134 * For fixed files, find out the size of the EOF block and adjust.
135 *
136 * For all others, have to read the entire file in, discarding the contents.
137 * Most posted text files will be small, and binary files like zlib archives
138 * and CD/DVD images should be either a STREAM_LF format or a fixed format.
139 *
140 */
141static curl_off_t vms_realfilesize(const char *name,
142 const struct_stat *stat_buf)
143{
144 char buffer[8192];
145 curl_off_t count;
146 int ret_stat;
147 FILE * file;
148
149 /* !checksrc! disable FOPENMODE 1 */
150 file = fopen(name, "r"); /* VMS */
151 if(file == NULL) {
152 return 0;
153 }
154 count = 0;
155 ret_stat = 1;
156 while(ret_stat > 0) {
157 ret_stat = fread(buffer, 1, sizeof(buffer), file);
158 if(ret_stat != 0)
159 count += ret_stat;
160 }
161 fclose(file);
162
163 return count;
164}
165
166/*
167 *
168 * VmsSpecialSize checks to see if the stat st_size can be trusted and
169 * if not to call a routine to get the correct size.
170 *
171 */
172static curl_off_t VmsSpecialSize(const char *name,
173 const struct_stat *stat_buf)
174{
175 switch(stat_buf->st_fab_rfm) {
176 case FAB$C_VAR:
177 case FAB$C_VFC:
178 return vms_realfilesize(name, stat_buf);
179 break;
180 default:
181 return stat_buf->st_size;
182 }
183}
184#endif /* __VMS */
185
186#if defined(HAVE_UTIME) || \
187 (defined(WIN32) && (CURL_SIZEOF_CURL_OFF_T >= 8))
188static void setfiletime(long filetime, const char *filename,
189 FILE *error_stream)
190{
191 if(filetime >= 0) {
192/* Windows utime() may attempt to adjust our unix gmt 'filetime' by a daylight
193 saving time offset and since it's GMT that is bad behavior. When we have
194 access to a 64-bit type we can bypass utime and set the times directly. */
195#if defined(WIN32) && (CURL_SIZEOF_CURL_OFF_T >= 8)
196 HANDLE hfile;
197
198#if (CURL_SIZEOF_LONG >= 8)
199 /* 910670515199 is the maximum unix filetime that can be used as a
200 Windows FILETIME without overflow: 30827-12-31T23:59:59. */
201 if(filetime > CURL_OFF_T_C(910670515199)) {
202 fprintf(error_stream,
203 "Failed to set filetime %ld on outfile: overflow\n",
204 filetime);
205 return;
206 }
207#endif /* CURL_SIZEOF_LONG >= 8 */
208
209 hfile = CreateFileA(filename, FILE_WRITE_ATTRIBUTES,
210 (FILE_SHARE_READ | FILE_SHARE_WRITE |
211 FILE_SHARE_DELETE),
212 NULL, OPEN_EXISTING, 0, NULL);
213 if(hfile != INVALID_HANDLE_VALUE) {
214 curl_off_t converted = ((curl_off_t)filetime * 10000000) +
215 CURL_OFF_T_C(116444736000000000);
216 FILETIME ft;
217 ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
218 ft.dwHighDateTime = (DWORD)(converted >> 32);
219 if(!SetFileTime(hfile, NULL, &ft, &ft)) {
220 fprintf(error_stream,
221 "Failed to set filetime %ld on outfile: "
222 "SetFileTime failed: GetLastError %u\n",
223 filetime, GetLastError());
224 }
225 CloseHandle(hfile);
226 }
227 else {
228 fprintf(error_stream,
229 "Failed to set filetime %ld on outfile: "
230 "CreateFile failed: GetLastError %u\n",
231 filetime, GetLastError());
232 }
233
234#elif defined(HAVE_UTIMES)
235 struct timeval times[2];
236 times[0].tv_sec = times[1].tv_sec = filetime;
237 times[0].tv_usec = times[1].tv_usec = 0;
238 if(utimes(filename, times)) {
239 fprintf(error_stream,
240 "Failed to set filetime %ld on outfile: errno %d\n",
241 filetime, errno);
242 }
243
244#elif defined(HAVE_UTIME)
245 struct utimbuf times;
246 times.actime = (time_t)filetime;
247 times.modtime = (time_t)filetime;
248 if(utime(filename, &times)) {
249 fprintf(error_stream,
250 "Failed to set filetime %ld on outfile: errno %d\n",
251 filetime, errno);
252 }
253#endif
254 }
255}
256#endif /* defined(HAVE_UTIME) || \
257 (defined(WIN32) && (CURL_SIZEOF_CURL_OFF_T >= 8)) */
258
259#define BUFFER_SIZE (100*1024)
260
261static CURLcode operate_do(struct GlobalConfig *global,
262 struct OperationConfig *config)
263{
264 char errorbuffer[CURL_ERROR_SIZE];
265 struct ProgressData progressbar;
266 struct getout *urlnode;
267
268 struct HdrCbData hdrcbdata;
269 struct OutStruct heads;
270
271 metalinkfile *mlfile_last = NULL;
272
273 CURL *curl = config->easy;
274 char *httpgetfields = NULL;
275
276 CURLcode result = CURLE_OK;
277 unsigned long li;
278 bool capath_from_env;
279
280 /* Save the values of noprogress and isatty to restore them later on */
281 bool orig_noprogress = global->noprogress;
282 bool orig_isatty = global->isatty;
283
284 errorbuffer[0] = '\0';
285
286 /* default headers output stream is stdout */
287 memset(&hdrcbdata, 0, sizeof(struct HdrCbData));
288 memset(&heads, 0, sizeof(struct OutStruct));
289 heads.stream = stdout;
290 heads.config = config;
291
292 /*
293 ** Beyond this point no return'ing from this function allowed.
294 ** Jump to label 'quit_curl' in order to abandon this function
295 ** from outside of nested loops further down below.
296 */
297
298 /* Check we have a url */
299 if(!config->url_list || !config->url_list->url) {
300 helpf(global->errors, "no URL specified!\n");
301 result = CURLE_FAILED_INIT;
302 goto quit_curl;
303 }
304
305 /* On WIN32 we can't set the path to curl-ca-bundle.crt
306 * at compile time. So we look here for the file in two ways:
307 * 1: look at the environment variable CURL_CA_BUNDLE for a path
308 * 2: if #1 isn't found, use the windows API function SearchPath()
309 * to find it along the app's path (includes app's dir and CWD)
310 *
311 * We support the environment variable thing for non-Windows platforms
312 * too. Just for the sake of it.
313 */
314 capath_from_env = false;
315 if(!config->cacert &&
316 !config->capath &&
317 !config->insecure_ok) {
318 char *env;
319 env = curlx_getenv("CURL_CA_BUNDLE");
320 if(env) {
321 config->cacert = strdup(env);
322 if(!config->cacert) {
323 curl_free(env);
324 helpf(global->errors, "out of memory\n");
325 result = CURLE_OUT_OF_MEMORY;
326 goto quit_curl;
327 }
328 }
329 else {
330 env = curlx_getenv("SSL_CERT_DIR");
331 if(env) {
332 config->capath = strdup(env);
333 if(!config->capath) {
334 curl_free(env);
335 helpf(global->errors, "out of memory\n");
336 result = CURLE_OUT_OF_MEMORY;
337 goto quit_curl;
338 }
339 capath_from_env = true;
340 }
341 else {
342 env = curlx_getenv("SSL_CERT_FILE");
343 if(env) {
344 config->cacert = strdup(env);
345 if(!config->cacert) {
346 curl_free(env);
347 helpf(global->errors, "out of memory\n");
348 result = CURLE_OUT_OF_MEMORY;
349 goto quit_curl;
350 }
351 }
352 }
353 }
354
355 if(env)
356 curl_free(env);
357#ifdef WIN32
358 else {
359 result = FindWin32CACert(config, "curl-ca-bundle.crt");
360 if(result)
361 goto quit_curl;
362 }
363#endif
364 }
365
366 if(config->postfields) {
367 if(config->use_httpget) {
368 /* Use the postfields data for a http get */
369 httpgetfields = strdup(config->postfields);
370 Curl_safefree(config->postfields);
371 if(!httpgetfields) {
372 helpf(global->errors, "out of memory\n");
373 result = CURLE_OUT_OF_MEMORY;
374 goto quit_curl;
375 }
376 if(SetHTTPrequest(config,
377 (config->no_body?HTTPREQ_HEAD:HTTPREQ_GET),
378 &config->httpreq)) {
379 result = CURLE_FAILED_INIT;
380 goto quit_curl;
381 }
382 }
383 else {
384 if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq)) {
385 result = CURLE_FAILED_INIT;
386 goto quit_curl;
387 }
388 }
389 }
390
391 /* Single header file for all URLs */
392 if(config->headerfile) {
393 /* open file for output: */
394 if(strcmp(config->headerfile, "-")) {
395 FILE *newfile = fopen(config->headerfile, "wb");
396 if(!newfile) {
397 warnf(config->global, "Failed to open %s\n", config->headerfile);
398 result = CURLE_WRITE_ERROR;
399 goto quit_curl;
400 }
401 else {
402 heads.filename = config->headerfile;
403 heads.s_isreg = TRUE;
404 heads.fopened = TRUE;
405 heads.stream = newfile;
406 }
407 }
408 else {
409 /* always use binary mode for protocol header output */
410 set_binmode(heads.stream);
411 }
412 }
413
414 /*
415 ** Nested loops start here.
416 */
417
418 /* loop through the list of given URLs */
419
420 for(urlnode = config->url_list; urlnode; urlnode = urlnode->next) {
421
422 unsigned long up; /* upload file counter within a single upload glob */
423 char *infiles; /* might be a glob pattern */
424 char *outfiles;
425 unsigned long infilenum;
426 URLGlob *inglob;
427
428 int metalink = 0; /* nonzero for metalink download. */
429 metalinkfile *mlfile;
430 metalink_resource *mlres;
431
432 outfiles = NULL;
433 infilenum = 1;
434 inglob = NULL;
435
436 if(urlnode->flags & GETOUT_METALINK) {
437 metalink = 1;
438 if(mlfile_last == NULL) {
439 mlfile_last = config->metalinkfile_list;
440 }
441 mlfile = mlfile_last;
442 mlfile_last = mlfile_last->next;
443 mlres = mlfile->resource;
444 }
445 else {
446 mlfile = NULL;
447 mlres = NULL;
448 }
449
450 /* urlnode->url is the full URL (it might be NULL) */
451
452 if(!urlnode->url) {
453 /* This node has no URL. Free node data without destroying the
454 node itself nor modifying next pointer and continue to next */
455 Curl_safefree(urlnode->outfile);
456 Curl_safefree(urlnode->infile);
457 urlnode->flags = 0;
458 continue; /* next URL please */
459 }
460
461 /* save outfile pattern before expansion */
462 if(urlnode->outfile) {
463 outfiles = strdup(urlnode->outfile);
464 if(!outfiles) {
465 helpf(global->errors, "out of memory\n");
466 result = CURLE_OUT_OF_MEMORY;
467 break;
468 }
469 }
470
471 infiles = urlnode->infile;
472
473 if(!config->globoff && infiles) {
474 /* Unless explicitly shut off */
475 result = glob_url(&inglob, infiles, &infilenum,
476 global->showerror?global->errors:NULL);
477 if(result) {
478 Curl_safefree(outfiles);
479 break;
480 }
481 }
482
483 /* Here's the loop for uploading multiple files within the same
484 single globbed string. If no upload, we enter the loop once anyway. */
485 for(up = 0 ; up < infilenum; up++) {
486
487 char *uploadfile; /* a single file, never a glob */
488 int separator;
489 URLGlob *urls;
490 unsigned long urlnum;
491
492 uploadfile = NULL;
493 urls = NULL;
494 urlnum = 0;
495
496 if(!up && !infiles)
497 Curl_nop_stmt;
498 else {
499 if(inglob) {
500 result = glob_next_url(&uploadfile, inglob);
501 if(result == CURLE_OUT_OF_MEMORY)
502 helpf(global->errors, "out of memory\n");
503 }
504 else if(!up) {
505 uploadfile = strdup(infiles);
506 if(!uploadfile) {
507 helpf(global->errors, "out of memory\n");
508 result = CURLE_OUT_OF_MEMORY;
509 }
510 }
511 else
512 uploadfile = NULL;
513 if(!uploadfile)
514 break;
515 }
516
517 if(metalink) {
518 /* For Metalink download, we don't use glob. Instead we use
519 the number of resources as urlnum. */
520 urlnum = count_next_metalink_resource(mlfile);
521 }
522 else
523 if(!config->globoff) {
524 /* Unless explicitly shut off, we expand '{...}' and '[...]'
525 expressions and return total number of URLs in pattern set */
526 result = glob_url(&urls, urlnode->url, &urlnum,
527 global->showerror?global->errors:NULL);
528 if(result) {
529 Curl_safefree(uploadfile);
530 break;
531 }
532 }
533 else
534 urlnum = 1; /* without globbing, this is a single URL */
535
536 /* if multiple files extracted to stdout, insert separators! */
537 separator= ((!outfiles || !strcmp(outfiles, "-")) && urlnum > 1);
538
539 /* Here's looping around each globbed URL */
540 for(li = 0 ; li < urlnum; li++) {
541
542 int infd;
543 bool infdopen;
544 char *outfile;
545 struct OutStruct outs;
546 struct InStruct input;
547 struct timeval retrystart;
548 curl_off_t uploadfilesize;
549 long retry_numretries;
550 long retry_sleep_default;
551 long retry_sleep;
552 char *this_url = NULL;
553 int metalink_next_res = 0;
554
555 outfile = NULL;
556 infdopen = FALSE;
557 infd = STDIN_FILENO;
558 uploadfilesize = -1; /* -1 means unknown */
559
560 /* default output stream is stdout */
561 memset(&outs, 0, sizeof(struct OutStruct));
562 outs.stream = stdout;
563 outs.config = config;
564
565 if(metalink) {
566 /* For Metalink download, use name in Metalink file as
567 filename. */
568 outfile = strdup(mlfile->filename);
569 if(!outfile) {
570 result = CURLE_OUT_OF_MEMORY;
571 goto show_error;
572 }
573 this_url = strdup(mlres->url);
574 if(!this_url) {
575 result = CURLE_OUT_OF_MEMORY;
576 goto show_error;
577 }
578 }
579 else {
580 if(urls) {
581 result = glob_next_url(&this_url, urls);
582 if(result)
583 goto show_error;
584 }
585 else if(!li) {
586 this_url = strdup(urlnode->url);
587 if(!this_url) {
588 result = CURLE_OUT_OF_MEMORY;
589 goto show_error;
590 }
591 }
592 else
593 this_url = NULL;
594 if(!this_url)
595 break;
596
597 if(outfiles) {
598 outfile = strdup(outfiles);
599 if(!outfile) {
600 result = CURLE_OUT_OF_MEMORY;
601 goto show_error;
602 }
603 }
604 }
605
606 if(((urlnode->flags&GETOUT_USEREMOTE) ||
607 (outfile && strcmp("-", outfile))) &&
608 (metalink || !config->use_metalink)) {
609
610 /*
611 * We have specified a file name to store the result in, or we have
612 * decided we want to use the remote file name.
613 */
614
615 if(!outfile) {
616 /* extract the file name from the URL */
617 result = get_url_file_name(&outfile, this_url);
618 if(result)
619 goto show_error;
620 if(!*outfile && !config->content_disposition) {
621 helpf(global->errors, "Remote file name has no length!\n");
622 result = CURLE_WRITE_ERROR;
623 goto quit_urls;
624 }
625 }
626 else if(urls) {
627 /* fill '#1' ... '#9' terms from URL pattern */
628 char *storefile = outfile;
629 result = glob_match_url(&outfile, storefile, urls);
630 Curl_safefree(storefile);
631 if(result) {
632 /* bad globbing */
633 warnf(config->global, "bad output glob!\n");
634 goto quit_urls;
635 }
636 }
637
638 /* Create the directory hierarchy, if not pre-existent to a multiple
639 file output call */
640
641 if(config->create_dirs || metalink) {
642 result = create_dir_hierarchy(outfile, global->errors);
643 /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
644 if(result == CURLE_WRITE_ERROR)
645 goto quit_urls;
646 if(result) {
647 goto show_error;
648 }
649 }
650
651 if((urlnode->flags & GETOUT_USEREMOTE)
652 && config->content_disposition) {
653 /* Our header callback MIGHT set the filename */
654 DEBUGASSERT(!outs.filename);
655 }
656
657 if(config->resume_from_current) {
658 /* We're told to continue from where we are now. Get the size
659 of the file as it is now and open it for append instead */
660 struct_stat fileinfo;
661 /* VMS -- Danger, the filesize is only valid for stream files */
662 if(0 == stat(outfile, &fileinfo))
663 /* set offset to current file size: */
664 config->resume_from = fileinfo.st_size;
665 else
666 /* let offset be 0 */
667 config->resume_from = 0;
668 }
669
670 if(config->resume_from) {
671#ifdef __VMS
672 /* open file for output, forcing VMS output format into stream
673 mode which is needed for stat() call above to always work. */
674 FILE *file = fopen(outfile, config->resume_from?"ab":"wb",
675 "ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0");
676#else
677 /* open file for output: */
678 FILE *file = fopen(outfile, config->resume_from?"ab":"wb");
679#endif
680 if(!file) {
681 helpf(global->errors, "Can't open '%s'!\n", outfile);
682 result = CURLE_WRITE_ERROR;
683 goto quit_urls;
684 }
685 outs.fopened = TRUE;
686 outs.stream = file;
687 outs.init = config->resume_from;
688 }
689 else {
690 outs.stream = NULL; /* open when needed */
691 }
692 outs.filename = outfile;
693 outs.s_isreg = TRUE;
694 }
695
696 if(uploadfile && !stdin_upload(uploadfile)) {
697 /*
698 * We have specified a file to upload and it isn't "-".
699 */
700 struct_stat fileinfo;
701
702 this_url = add_file_name_to_url(curl, this_url, uploadfile);
703 if(!this_url) {
704 result = CURLE_OUT_OF_MEMORY;
705 goto show_error;
706 }
707 /* VMS Note:
708 *
709 * Reading binary from files can be a problem... Only FIXED, VAR
710 * etc WITHOUT implied CC will work Others need a \n appended to a
711 * line
712 *
713 * - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
714 * fixed file with implied CC needs to have a byte added for every
715 * record processed, this can by derived from Filesize & recordsize
716 * for VARiable record files the records need to be counted! for
717 * every record add 1 for linefeed and subtract 2 for the record
718 * header for VARIABLE header files only the bare record data needs
719 * to be considered with one appended if implied CC
720 */
721#ifdef __VMS
722 /* Calculate the real upload site for VMS */
723 infd = -1;
724 if(stat(uploadfile, &fileinfo) == 0) {
725 fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
726 switch(fileinfo.st_fab_rfm) {
727 case FAB$C_VAR:
728 case FAB$C_VFC:
729 case FAB$C_STMCR:
730 infd = open(uploadfile, O_RDONLY | O_BINARY);
731 break;
732 default:
733 infd = open(uploadfile, O_RDONLY | O_BINARY,
734 "rfm=stmlf", "ctx=stm");
735 }
736 }
737 if(infd == -1)
738#else
739 infd = open(uploadfile, O_RDONLY | O_BINARY);
740 if((infd == -1) || fstat(infd, &fileinfo))
741#endif
742 {
743 helpf(global->errors, "Can't open '%s'!\n", uploadfile);
744 if(infd != -1) {
745 close(infd);
746 infd = STDIN_FILENO;
747 }
748 result = CURLE_READ_ERROR;
749 goto quit_urls;
750 }
751 infdopen = TRUE;
752
753 /* we ignore file size for char/block devices, sockets, etc. */
754 if(S_ISREG(fileinfo.st_mode))
755 uploadfilesize = fileinfo.st_size;
756
757 }
758 else if(uploadfile && stdin_upload(uploadfile)) {
759 /* count to see if there are more than one auth bit set
760 in the authtype field */
761 int authbits = 0;
762 int bitcheck = 0;
763 while(bitcheck < 32) {
764 if(config->authtype & (1UL << bitcheck++)) {
765 authbits++;
766 if(authbits > 1) {
767 /* more than one, we're done! */
768 break;
769 }
770 }
771 }
772
773 /*
774 * If the user has also selected --anyauth or --proxy-anyauth
775 * we should warn him/her.
776 */
777 if(config->proxyanyauth || (authbits>1)) {
778 warnf(config->global,
779 "Using --anyauth or --proxy-anyauth with upload from stdin"
780 " involves a big risk of it not working. Use a temporary"
781 " file or a fixed auth type instead!\n");
782 }
783
784 DEBUGASSERT(infdopen == FALSE);
785 DEBUGASSERT(infd == STDIN_FILENO);
786
787 set_binmode(stdin);
788 if(!strcmp(uploadfile, ".")) {
789 if(curlx_nonblock((curl_socket_t)infd, TRUE) < 0)
790 warnf(config->global,
791 "fcntl failed on fd=%d: %s\n", infd, strerror(errno));
792 }
793 }
794
795 if(uploadfile && config->resume_from_current)
796 config->resume_from = -1; /* -1 will then force get-it-yourself */
797
798 if(output_expected(this_url, uploadfile) && outs.stream &&
799 isatty(fileno(outs.stream)))
800 /* we send the output to a tty, therefore we switch off the progress
801 meter */
802 global->noprogress = global->isatty = TRUE;
803 else {
804 /* progress meter is per download, so restore config
805 values */
806 global->noprogress = orig_noprogress;
807 global->isatty = orig_isatty;
808 }
809
810 if(urlnum > 1 && !global->mute) {
811 fprintf(global->errors, "\n[%lu/%lu]: %s --> %s\n",
812 li+1, urlnum, this_url, outfile ? outfile : "<stdout>");
813 if(separator)
814 printf("%s%s\n", CURLseparator, this_url);
815 }
816 if(httpgetfields) {
817 char *urlbuffer;
818 /* Find out whether the url contains a file name */
819 const char *pc = strstr(this_url, "://");
820 char sep = '?';
821 if(pc)
822 pc += 3;
823 else
824 pc = this_url;
825
826 pc = strrchr(pc, '/'); /* check for a slash */
827
828 if(pc) {
829 /* there is a slash present in the URL */
830
831 if(strchr(pc, '?'))
832 /* Ouch, there's already a question mark in the URL string, we
833 then append the data with an ampersand separator instead! */
834 sep='&';
835 }
836 /*
837 * Then append ? followed by the get fields to the url.
838 */
839 if(pc)
840 urlbuffer = aprintf("%s%c%s", this_url, sep, httpgetfields);
841 else
842 /* Append / before the ? to create a well-formed url
843 if the url contains a hostname only
844 */
845 urlbuffer = aprintf("%s/?%s", this_url, httpgetfields);
846
847 if(!urlbuffer) {
848 result = CURLE_OUT_OF_MEMORY;
849 goto show_error;
850 }
851
852 Curl_safefree(this_url); /* free previous URL */
853 this_url = urlbuffer; /* use our new URL instead! */
854 }
855
856 if(!global->errors)
857 global->errors = stderr;
858
859 if((!outfile || !strcmp(outfile, "-")) && !config->use_ascii) {
860 /* We get the output to stdout and we have not got the ASCII/text
861 flag, then set stdout to be binary */
862 set_binmode(stdout);
863 }
864
865 if(!config->tcp_nodelay)
866 my_setopt(curl, CURLOPT_TCP_NODELAY, 0L);
867
868 if(config->tcp_fastopen)
869 my_setopt(curl, CURLOPT_TCP_FASTOPEN, 1L);
870
871 /* where to store */
872 my_setopt(curl, CURLOPT_WRITEDATA, &outs);
873 my_setopt(curl, CURLOPT_INTERLEAVEDATA, &outs);
874 if(metalink || !config->use_metalink)
875 /* what call to write */
876 my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
877#ifdef USE_METALINK
878 else
879 /* Set Metalink specific write callback function to parse
880 XML data progressively. */
881 my_setopt(curl, CURLOPT_WRITEFUNCTION, metalink_write_cb);
882#endif /* USE_METALINK */
883
884 /* for uploads */
885 input.fd = infd;
886 input.config = config;
887 /* Note that if CURLOPT_READFUNCTION is fread (the default), then
888 * lib/telnet.c will Curl_poll() on the input file descriptor
889 * rather then calling the READFUNCTION at regular intervals.
890 * The circumstances in which it is preferable to enable this
891 * behaviour, by omitting to set the READFUNCTION & READDATA options,
892 * have not been determined.
893 */
894 my_setopt(curl, CURLOPT_READDATA, &input);
895 /* what call to read */
896 my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
897
898 /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
899 CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
900 my_setopt(curl, CURLOPT_SEEKDATA, &input);
901 my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
902
903 if(config->recvpersecond &&
904 (config->recvpersecond < BUFFER_SIZE))
905 /* use a smaller sized buffer for better sleeps */
906 my_setopt(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond);
907 else
908 my_setopt(curl, CURLOPT_BUFFERSIZE, (long)BUFFER_SIZE);
909
910 /* size of uploaded file: */
911 if(uploadfilesize != -1)
912 my_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
913 my_setopt_str(curl, CURLOPT_URL, this_url); /* what to fetch */
914 my_setopt(curl, CURLOPT_NOPROGRESS, global->noprogress?1L:0L);
915 if(config->no_body) {
916 my_setopt(curl, CURLOPT_NOBODY, 1L);
917 my_setopt(curl, CURLOPT_HEADER, 1L);
918 }
919 /* If --metalink is used, we ignore --include (headers in
920 output) option because mixing headers to the body will
921 confuse XML parser and/or hash check will fail. */
922 else if(!config->use_metalink)
923 my_setopt(curl, CURLOPT_HEADER, config->include_headers?1L:0L);
924
925 if(config->oauth_bearer)
926 my_setopt_str(curl, CURLOPT_XOAUTH2_BEARER, config->oauth_bearer);
927
928#if !defined(CURL_DISABLE_PROXY)
929 {
930 /* TODO: Make this a run-time check instead of compile-time one. */
931
932 my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
933 /* new in libcurl 7.5 */
934 if(config->proxy)
935 my_setopt_enum(curl, CURLOPT_PROXYTYPE, config->proxyver);
936
937 my_setopt_str(curl, CURLOPT_PROXYUSERPWD, config->proxyuserpwd);
938
939 /* new in libcurl 7.3 */
940 my_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config->proxytunnel?1L:0L);
941
942 /* new in libcurl 7.52.0 */
943 if(config->preproxy)
944 my_setopt_str(curl, CURLOPT_PRE_PROXY, config->preproxy);
945
946 /* new in libcurl 7.10.6 */
947 if(config->proxyanyauth)
948 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
949 (long)CURLAUTH_ANY);
950 else if(config->proxynegotiate)
951 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
952 (long)CURLAUTH_GSSNEGOTIATE);
953 else if(config->proxyntlm)
954 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
955 (long)CURLAUTH_NTLM);
956 else if(config->proxydigest)
957 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
958 (long)CURLAUTH_DIGEST);
959 else if(config->proxybasic)
960 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
961 (long)CURLAUTH_BASIC);
962
963 /* new in libcurl 7.19.4 */
964 my_setopt_str(curl, CURLOPT_NOPROXY, config->noproxy);
965
966 my_setopt(curl, CURLOPT_SUPPRESS_CONNECT_HEADERS,
967 config->suppress_connect_headers?1L:0L);
968 }
969#endif /* !CURL_DISABLE_PROXY */
970
971 my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror?1L:0L);
972 my_setopt(curl, CURLOPT_UPLOAD, uploadfile?1L:0L);
973 my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly?1L:0L);
974 my_setopt(curl, CURLOPT_APPEND, config->ftp_append?1L:0L);
975
976 if(config->netrc_opt)
977 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_OPTIONAL);
978 else if(config->netrc || config->netrc_file)
979 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_REQUIRED);
980 else
981 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_IGNORED);
982
983 if(config->netrc_file)
984 my_setopt_str(curl, CURLOPT_NETRC_FILE, config->netrc_file);
985
986 my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii?1L:0L);
987 if(config->login_options)
988 my_setopt_str(curl, CURLOPT_LOGIN_OPTIONS, config->login_options);
989 my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
990 my_setopt_str(curl, CURLOPT_RANGE, config->range);
991 my_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
992 my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000));
993
994 if(built_in_protos & CURLPROTO_HTTP) {
995
996 long postRedir = 0;
997
998 my_setopt(curl, CURLOPT_FOLLOWLOCATION,
999 config->followlocation?1L:0L);
1000 my_setopt(curl, CURLOPT_UNRESTRICTED_AUTH,
1001 config->unrestricted_auth?1L:0L);
1002
1003 switch(config->httpreq) {
1004 case HTTPREQ_SIMPLEPOST:
1005 my_setopt_str(curl, CURLOPT_POSTFIELDS,
1006 config->postfields);
1007 my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
1008 config->postfieldsize);
1009 break;
1010 case HTTPREQ_FORMPOST:
1011 my_setopt_httppost(curl, CURLOPT_HTTPPOST, config->httppost);
1012 break;
1013 default:
1014 break;
1015 }
1016
1017 my_setopt_str(curl, CURLOPT_REFERER, config->referer);
1018 my_setopt(curl, CURLOPT_AUTOREFERER, config->autoreferer?1L:0L);
1019 my_setopt_str(curl, CURLOPT_USERAGENT, config->useragent);
1020 my_setopt_slist(curl, CURLOPT_HTTPHEADER, config->headers);
1021
1022 /* new in libcurl 7.36.0 */
1023 if(config->proxyheaders) {
1024 my_setopt_slist(curl, CURLOPT_PROXYHEADER, config->proxyheaders);
1025 my_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE);
1026 }
1027
1028 /* new in libcurl 7.5 */
1029 my_setopt(curl, CURLOPT_MAXREDIRS, config->maxredirs);
1030
1031 if(config->httpversion)
1032 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion);
1033 else if(curlinfo->features & CURL_VERSION_HTTP2) {
1034 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
1035 }
1036
1037 /* new in libcurl 7.10.6 (default is Basic) */
1038 if(config->authtype)
1039 my_setopt_bitmask(curl, CURLOPT_HTTPAUTH, (long)config->authtype);
1040
1041 /* curl 7.19.1 (the 301 version existed in 7.18.2),
1042 303 was added in 7.26.0 */
1043 if(config->post301)
1044 postRedir |= CURL_REDIR_POST_301;
1045 if(config->post302)
1046 postRedir |= CURL_REDIR_POST_302;
1047 if(config->post303)
1048 postRedir |= CURL_REDIR_POST_303;
1049 my_setopt(curl, CURLOPT_POSTREDIR, postRedir);
1050
1051 /* new in libcurl 7.21.6 */
1052 if(config->encoding)
1053 my_setopt_str(curl, CURLOPT_ACCEPT_ENCODING, "");
1054
1055 /* new in libcurl 7.21.6 */
1056 if(config->tr_encoding)
1057 my_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1L);
1058
1059 } /* (built_in_protos & CURLPROTO_HTTP) */
1060
1061 my_setopt_str(curl, CURLOPT_FTPPORT, config->ftpport);
1062 my_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,
1063 config->low_speed_limit);
1064 my_setopt(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
1065 my_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE,
1066 config->sendpersecond);
1067 my_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE,
1068 config->recvpersecond);
1069
1070 if(config->use_resume)
1071 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, config->resume_from);
1072 else
1073 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, CURL_OFF_T_C(0));
1074
1075 my_setopt_str(curl, CURLOPT_KEYPASSWD, config->key_passwd);
1076 my_setopt_str(curl, CURLOPT_PROXY_KEYPASSWD, config->proxy_key_passwd);
1077
1078 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1079
1080 /* SSH and SSL private key uses same command-line option */
1081 /* new in libcurl 7.16.1 */
1082 my_setopt_str(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key);
1083 /* new in libcurl 7.16.1 */
1084 my_setopt_str(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey);
1085
1086 /* new in libcurl 7.17.1: SSH host key md5 checking allows us
1087 to fail if we are not talking to who we think we should */
1088 my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,
1089 config->hostpubmd5);
1090 }
1091
1092 if(config->cacert)
1093 my_setopt_str(curl, CURLOPT_CAINFO, config->cacert);
1094 if(config->proxy_cacert)
1095 my_setopt_str(curl, CURLOPT_PROXY_CAINFO, config->proxy_cacert);
1096
1097 if(config->capath) {
1098 result = res_setopt_str(curl, CURLOPT_CAPATH, config->capath);
1099 if(result == CURLE_NOT_BUILT_IN) {
1100 warnf(config->global, "ignoring %s, not supported by libcurl\n",
1101 capath_from_env?
1102 "SSL_CERT_DIR environment variable":"--capath");
1103 }
1104 else if(result)
1105 goto show_error;
1106 }
1107 /* For the time being if --proxy-capath is not set then we use the
1108 --capath value for it, if any. See #1257 */
1109 if(config->proxy_capath || config->capath) {
1110 result = res_setopt_str(curl, CURLOPT_PROXY_CAPATH,
1111 (config->proxy_capath ?
1112 config->proxy_capath :
1113 config->capath));
1114 if(result == CURLE_NOT_BUILT_IN) {
1115 if(config->proxy_capath) {
1116 warnf(config->global,
1117 "ignoring --proxy-capath, not supported by libcurl\n");
1118 }
1119 }
1120 else if(result)
1121 goto show_error;
1122 }
1123
1124 if(config->crlfile)
1125 my_setopt_str(curl, CURLOPT_CRLFILE, config->crlfile);
1126 if(config->proxy_crlfile)
1127 my_setopt_str(curl, CURLOPT_PROXY_CRLFILE, config->proxy_crlfile);
1128 else if(config->crlfile) /* CURLOPT_PROXY_CRLFILE default is crlfile */
1129 my_setopt_str(curl, CURLOPT_PROXY_CRLFILE, config->crlfile);
1130
1131 if(config->pinnedpubkey)
1132 my_setopt_str(curl, CURLOPT_PINNEDPUBLICKEY, config->pinnedpubkey);
1133
1134 if(curlinfo->features & CURL_VERSION_SSL) {
1135 my_setopt_str(curl, CURLOPT_SSLCERT, config->cert);
1136 my_setopt_str(curl, CURLOPT_PROXY_SSLCERT, config->proxy_cert);
1137 my_setopt_str(curl, CURLOPT_SSLCERTTYPE, config->cert_type);
1138 my_setopt_str(curl, CURLOPT_PROXY_SSLCERTTYPE,
1139 config->proxy_cert_type);
1140 my_setopt_str(curl, CURLOPT_SSLKEY, config->key);
1141 my_setopt_str(curl, CURLOPT_PROXY_SSLKEY, config->proxy_key);
1142 my_setopt_str(curl, CURLOPT_SSLKEYTYPE, config->key_type);
1143 my_setopt_str(curl, CURLOPT_PROXY_SSLKEYTYPE,
1144 config->proxy_key_type);
1145
1146 if(config->insecure_ok) {
1147 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
1148 my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
1149 }
1150 else {
1151 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1152 /* libcurl default is strict verifyhost -> 2L */
1153 /* my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); */
1154 }
1155 if(config->proxy_insecure_ok) {
1156 my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);
1157 my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);
1158 }
1159 else {
1160 my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 1L);
1161 }
1162
1163 if(config->verifystatus)
1164 my_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1L);
1165
1166 if(config->falsestart)
1167 my_setopt(curl, CURLOPT_SSL_FALSESTART, 1L);
1168
1169 my_setopt_enum(curl, CURLOPT_SSLVERSION,
1170 config->ssl_version | config->ssl_version_max);
1171 my_setopt_enum(curl, CURLOPT_PROXY_SSLVERSION,
1172 config->proxy_ssl_version);
1173 }
1174 if(config->path_as_is)
1175 my_setopt(curl, CURLOPT_PATH_AS_IS, 1L);
1176
1177 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1178 if(!config->insecure_ok) {
1179 char *home;
1180 char *file;
1181 result = CURLE_OUT_OF_MEMORY;
1182 home = homedir();
1183 if(home) {
1184 file = aprintf("%s/%sssh/known_hosts", home, DOT_CHAR);
1185 if(file) {
1186 /* new in curl 7.19.6 */
1187 result = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file);
1188 curl_free(file);
1189 if(result == CURLE_UNKNOWN_OPTION)
1190 /* libssh2 version older than 1.1.1 */
1191 result = CURLE_OK;
1192 }
1193 Curl_safefree(home);
1194 }
1195 if(result)
1196 goto show_error;
1197 }
1198 }
1199
1200 if(config->no_body || config->remote_time) {
1201 /* no body or use remote time */
1202 my_setopt(curl, CURLOPT_FILETIME, 1L);
1203 }
1204
1205 my_setopt(curl, CURLOPT_CRLF, config->crlf?1L:0L);
1206 my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
1207 my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
1208 my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
1209
1210#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
1211 if(config->cookie)
1212 my_setopt_str(curl, CURLOPT_COOKIE, config->cookie);
1213
1214 if(config->cookiefile)
1215 my_setopt_str(curl, CURLOPT_COOKIEFILE, config->cookiefile);
1216
1217 /* new in libcurl 7.9 */
1218 if(config->cookiejar)
1219 my_setopt_str(curl, CURLOPT_COOKIEJAR, config->cookiejar);
1220
1221 /* new in libcurl 7.9.7 */
1222 my_setopt(curl, CURLOPT_COOKIESESSION, config->cookiesession?1L:0L);
1223#else
1224 if(config->cookie || config->cookiefile || config->cookiejar) {
1225 warnf(config->global, "cookie option(s) used even though cookie "
1226 "support is disabled!\n");
1227 return CURLE_NOT_BUILT_IN;
1228 }
1229#endif
1230
1231 my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond);
1232 my_setopt(curl, CURLOPT_TIMEVALUE, (long)config->condtime);
1233 my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
1234 customrequest_helper(config, config->httpreq, config->customrequest);
1235 my_setopt(curl, CURLOPT_STDERR, global->errors);
1236
1237 /* three new ones in libcurl 7.3: */
1238 my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
1239 my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
1240
1241 progressbarinit(&progressbar, config);
1242 if((global->progressmode == CURL_PROGRESS_BAR) &&
1243 !global->noprogress && !global->mute) {
1244 /* we want the alternative style, then we have to implement it
1245 ourselves! */
1246 my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb);
1247 my_setopt(curl, CURLOPT_XFERINFODATA, &progressbar);
1248 }
1249
1250 /* new in libcurl 7.24.0: */
1251 if(config->dns_servers)
1252 my_setopt_str(curl, CURLOPT_DNS_SERVERS, config->dns_servers);
1253
1254 /* new in libcurl 7.33.0: */
1255 if(config->dns_interface)
1256 my_setopt_str(curl, CURLOPT_DNS_INTERFACE, config->dns_interface);
1257 if(config->dns_ipv4_addr)
1258 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP4, config->dns_ipv4_addr);
1259 if(config->dns_ipv6_addr)
1260 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP6, config->dns_ipv6_addr);
1261
1262 /* new in libcurl 7.6.2: */
1263 my_setopt_slist(curl, CURLOPT_TELNETOPTIONS, config->telnet_options);
1264
1265 /* new in libcurl 7.7: */
1266 my_setopt_str(curl, CURLOPT_RANDOM_FILE, config->random_file);
1267 my_setopt_str(curl, CURLOPT_EGDSOCKET, config->egd_file);
1268 my_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
1269 (long)(config->connecttimeout * 1000));
1270
1271 if(config->cipher_list)
1272 my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
1273
1274 if(config->proxy_cipher_list)
1275 my_setopt_str(curl, CURLOPT_PROXY_SSL_CIPHER_LIST,
1276 config->proxy_cipher_list);
1277
1278 /* new in libcurl 7.9.2: */
1279 if(config->disable_epsv)
1280 /* disable it */
1281 my_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);
1282
1283 /* new in libcurl 7.10.5 */
1284 if(config->disable_eprt)
1285 /* disable it */
1286 my_setopt(curl, CURLOPT_FTP_USE_EPRT, 0L);
1287
1288 if(global->tracetype != TRACE_NONE) {
1289 my_setopt(curl, CURLOPT_DEBUGFUNCTION, tool_debug_cb);
1290 my_setopt(curl, CURLOPT_DEBUGDATA, config);
1291 my_setopt(curl, CURLOPT_VERBOSE, 1L);
1292 }
1293
1294 /* new in curl 7.9.3 */
1295 if(config->engine) {
1296 result = res_setopt_str(curl, CURLOPT_SSLENGINE, config->engine);
1297 if(result)
1298 goto show_error;
1299 }
1300
1301 /* new in curl 7.10.7, extended in 7.19.4. Modified to use
1302 CREATE_DIR_RETRY in 7.49.0 */
1303 my_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
1304 (long)(config->ftp_create_dirs?
1305 CURLFTP_CREATE_DIR_RETRY:
1306 CURLFTP_CREATE_DIR_NONE));
1307
1308 /* new in curl 7.10.8 */
1309 if(config->max_filesize)
1310 my_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
1311 config->max_filesize);
1312
1313 if(4 == config->ip_version)
1314 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1315 else if(6 == config->ip_version)
1316 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
1317 else
1318 my_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER);
1319
1320 /* new in curl 7.15.5 */
1321 if(config->ftp_ssl_reqd)
1322 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
1323
1324 /* new in curl 7.11.0 */
1325 else if(config->ftp_ssl)
1326 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
1327
1328 /* new in curl 7.16.0 */
1329 else if(config->ftp_ssl_control)
1330 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_CONTROL);
1331
1332 /* new in curl 7.16.1 */
1333 if(config->ftp_ssl_ccc)
1334 my_setopt_enum(curl, CURLOPT_FTP_SSL_CCC,
1335 (long)config->ftp_ssl_ccc_mode);
1336
1337 /* new in curl 7.19.4 */
1338 if(config->socks5_gssapi_nec)
1339 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_NEC,
1340 config->socks5_gssapi_nec);
1341
1342 /* new in curl 7.43.0 */
1343 if(config->proxy_service_name)
1344 my_setopt_str(curl, CURLOPT_PROXY_SERVICE_NAME,
1345 config->proxy_service_name);
1346
1347 /* new in curl 7.43.0 */
1348 if(config->service_name)
1349 my_setopt_str(curl, CURLOPT_SERVICE_NAME,
1350 config->service_name);
1351
1352 /* curl 7.13.0 */
1353 my_setopt_str(curl, CURLOPT_FTP_ACCOUNT, config->ftp_account);
1354
1355 my_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, config->ignorecl?1L:0L);
1356
1357 /* curl 7.14.2 */
1358 my_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, config->ftp_skip_ip?1L:0L);
1359
1360 /* curl 7.15.1 */
1361 my_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long)config->ftp_filemethod);
1362
1363 /* curl 7.15.2 */
1364 if(config->localport) {
1365 my_setopt(curl, CURLOPT_LOCALPORT, (long)config->localport);
1366 my_setopt_str(curl, CURLOPT_LOCALPORTRANGE,
1367 (long)config->localportrange);
1368 }
1369
1370 /* curl 7.15.5 */
1371 my_setopt_str(curl, CURLOPT_FTP_ALTERNATIVE_TO_USER,
1372 config->ftp_alternative_to_user);
1373
1374 /* curl 7.16.0 */
1375 if(config->disable_sessionid)
1376 /* disable it */
1377 my_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0L);
1378
1379 /* curl 7.16.2 */
1380 if(config->raw) {
1381 my_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 0L);
1382 my_setopt(curl, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
1383 }
1384
1385 /* curl 7.17.1 */
1386 if(!config->nokeepalive) {
1387 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
1388 if(config->alivetime != 0) {
1389 my_setopt(curl, CURLOPT_TCP_KEEPIDLE, config->alivetime);
1390 my_setopt(curl, CURLOPT_TCP_KEEPINTVL, config->alivetime);
1391 }
1392 }
1393 else
1394 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
1395
1396 /* curl 7.20.0 */
1397 if(config->tftp_blksize)
1398 my_setopt(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
1399
1400 if(config->mail_from)
1401 my_setopt_str(curl, CURLOPT_MAIL_FROM, config->mail_from);
1402
1403 if(config->mail_rcpt)
1404 my_setopt_slist(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
1405
1406 /* curl 7.20.x */
1407 if(config->ftp_pret)
1408 my_setopt(curl, CURLOPT_FTP_USE_PRET, 1L);
1409
1410 if(config->proto_present)
1411 my_setopt_flags(curl, CURLOPT_PROTOCOLS, config->proto);
1412 if(config->proto_redir_present)
1413 my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
1414
1415 if(config->content_disposition
1416 && (urlnode->flags & GETOUT_USEREMOTE))
1417 hdrcbdata.honor_cd_filename = TRUE;
1418 else
1419 hdrcbdata.honor_cd_filename = FALSE;
1420
1421 hdrcbdata.outs = &outs;
1422 hdrcbdata.heads = &heads;
1423
1424 my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
1425 my_setopt(curl, CURLOPT_HEADERDATA, &hdrcbdata);
1426
1427 if(config->resolve)
1428 /* new in 7.21.3 */
1429 my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
1430
1431 if(config->connect_to)
1432 /* new in 7.49.0 */
1433 my_setopt_slist(curl, CURLOPT_CONNECT_TO, config->connect_to);
1434
1435 /* new in 7.21.4 */
1436 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1437 if(config->tls_username)
1438 my_setopt_str(curl, CURLOPT_TLSAUTH_USERNAME,
1439 config->tls_username);
1440 if(config->tls_password)
1441 my_setopt_str(curl, CURLOPT_TLSAUTH_PASSWORD,
1442 config->tls_password);
1443 if(config->tls_authtype)
1444 my_setopt_str(curl, CURLOPT_TLSAUTH_TYPE,
1445 config->tls_authtype);
1446 if(config->proxy_tls_username)
1447 my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_USERNAME,
1448 config->proxy_tls_username);
1449 if(config->proxy_tls_password)
1450 my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_PASSWORD,
1451 config->proxy_tls_password);
1452 if(config->proxy_tls_authtype)
1453 my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_TYPE,
1454 config->proxy_tls_authtype);
1455 }
1456
1457 /* new in 7.22.0 */
1458 if(config->gssapi_delegation)
1459 my_setopt_str(curl, CURLOPT_GSSAPI_DELEGATION,
1460 config->gssapi_delegation);
1461
1462 /* new in 7.25.0 and 7.44.0 */
1463 {
1464 long mask = (config->ssl_allow_beast ? CURLSSLOPT_ALLOW_BEAST : 0) |
1465 (config->ssl_no_revoke ? CURLSSLOPT_NO_REVOKE : 0);
1466 if(mask)
1467 my_setopt_bitmask(curl, CURLOPT_SSL_OPTIONS, mask);
1468 }
1469
1470 if(config->proxy_ssl_allow_beast)
1471 my_setopt(curl, CURLOPT_PROXY_SSL_OPTIONS,
1472 (long)CURLSSLOPT_ALLOW_BEAST);
1473
1474 if(config->mail_auth)
1475 my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
1476
1477 /* new in 7.31.0 */
1478 if(config->sasl_ir)
1479 my_setopt(curl, CURLOPT_SASL_IR, 1L);
1480
1481 if(config->nonpn) {
1482 my_setopt(curl, CURLOPT_SSL_ENABLE_NPN, 0L);
1483 }
1484
1485 if(config->noalpn) {
1486 my_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L);
1487 }
1488
1489 /* new in 7.40.0, abstract support added in 7.53.0 */
1490 if(config->unix_socket_path) {
1491 if(config->abstract_unix_socket) {
1492 my_setopt_str(curl, CURLOPT_ABSTRACT_UNIX_SOCKET,
1493 config->unix_socket_path);
1494 }
1495 else {
1496 my_setopt_str(curl, CURLOPT_UNIX_SOCKET_PATH,
1497 config->unix_socket_path);
1498 }
1499 }
1500 /* new in 7.45.0 */
1501 if(config->proto_default)
1502 my_setopt_str(curl, CURLOPT_DEFAULT_PROTOCOL, config->proto_default);
1503
1504 /* new in 7.47.0 */
1505 if(config->expect100timeout > 0)
1506 my_setopt_str(curl, CURLOPT_EXPECT_100_TIMEOUT_MS,
1507 (long)(config->expect100timeout*1000));
1508
1509 /* new in 7.48.0 */
1510 if(config->tftp_no_options)
1511 my_setopt(curl, CURLOPT_TFTP_NO_OPTIONS, 1L);
1512
1513 /* initialize retry vars for loop below */
1514 retry_sleep_default = (config->retry_delay) ?
1515 config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
1516
1517 retry_numretries = config->req_retry;
1518 retry_sleep = retry_sleep_default; /* ms */
1519 retrystart = tvnow();
1520
1521#ifndef CURL_DISABLE_LIBCURL_OPTION
1522 if(global->libcurl) {
1523 result = easysrc_perform();
1524 if(result)
1525 goto show_error;
1526 }
1527#endif
1528
1529 for(;;) {
1530#ifdef USE_METALINK
1531 if(!metalink && config->use_metalink) {
1532 /* If outs.metalink_parser is non-NULL, delete it first. */
1533 if(outs.metalink_parser)
1534 metalink_parser_context_delete(outs.metalink_parser);
1535 outs.metalink_parser = metalink_parser_context_new();
1536 if(outs.metalink_parser == NULL) {
1537 result = CURLE_OUT_OF_MEMORY;
1538 goto show_error;
1539 }
1540 fprintf(config->global->errors,
1541 "Metalink: parsing (%s) metalink/XML...\n", this_url);
1542 }
1543 else if(metalink)
1544 fprintf(config->global->errors,
1545 "Metalink: fetching (%s) from (%s)...\n",
1546 mlfile->filename, this_url);
1547#endif /* USE_METALINK */
1548
1549#ifdef CURLDEBUG
1550 if(config->test_event_based)
1551 result = curl_easy_perform_ev(curl);
1552 else
1553#endif
1554 result = curl_easy_perform(curl);
1555
1556 if(!result && !outs.stream && !outs.bytes) {
1557 /* we have received no data despite the transfer was successful
1558 ==> force cration of an empty output file (if an output file
1559 was specified) */
1560 long cond_unmet = 0L;
1561 /* do not create (or even overwrite) the file in case we get no
1562 data because of unmet condition */
1563 curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &cond_unmet);
1564 if(!cond_unmet && !tool_create_output_file(&outs))
1565 result = CURLE_WRITE_ERROR;
1566 }
1567
1568 if(outs.is_cd_filename && outs.stream && !global->mute &&
1569 outs.filename)
1570 printf("curl: Saved to filename '%s'\n", outs.filename);
1571
1572 /* if retry-max-time is non-zero, make sure we haven't exceeded the
1573 time */
1574 if(retry_numretries &&
1575 (!config->retry_maxtime ||
1576 (tvdiff(tvnow(), retrystart) <
1577 config->retry_maxtime*1000L)) ) {
1578 enum {
1579 RETRY_NO,
1580 RETRY_TIMEOUT,
1581 RETRY_CONNREFUSED,
1582 RETRY_HTTP,
1583 RETRY_FTP,
1584 RETRY_LAST /* not used */
1585 } retry = RETRY_NO;
1586 long response;
1587 if((CURLE_OPERATION_TIMEDOUT == result) ||
1588 (CURLE_COULDNT_RESOLVE_HOST == result) ||
1589 (CURLE_COULDNT_RESOLVE_PROXY == result) ||
1590 (CURLE_FTP_ACCEPT_TIMEOUT == result))
1591 /* retry timeout always */
1592 retry = RETRY_TIMEOUT;
1593 else if(config->retry_connrefused &&
1594 (CURLE_COULDNT_CONNECT == result)) {
1595 long oserrno;
1596 curl_easy_getinfo(curl, CURLINFO_OS_ERRNO, &oserrno);
1597 if(ECONNREFUSED == oserrno)
1598 retry = RETRY_CONNREFUSED;
1599 }
1600 else if((CURLE_OK == result) ||
1601 (config->failonerror &&
1602 (CURLE_HTTP_RETURNED_ERROR == result))) {
1603 /* If it returned OK. _or_ failonerror was enabled and it
1604 returned due to such an error, check for HTTP transient
1605 errors to retry on. */
1606 char *effective_url = NULL;
1607 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1608 if(effective_url &&
1609 checkprefix("http", effective_url)) {
1610 /* This was HTTP(S) */
1611 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1612
1613 switch(response) {
1614 case 500: /* Internal Server Error */
1615 case 502: /* Bad Gateway */
1616 case 503: /* Service Unavailable */
1617 case 504: /* Gateway Timeout */
1618 retry = RETRY_HTTP;
1619 /*
1620 * At this point, we have already written data to the output
1621 * file (or terminal). If we write to a file, we must rewind
1622 * or close/re-open the file so that the next attempt starts
1623 * over from the beginning.
1624 *
1625 * TODO: similar action for the upload case. We might need
1626 * to start over reading from a previous point if we have
1627 * uploaded something when this was returned.
1628 */
1629 break;
1630 }
1631 }
1632 } /* if CURLE_OK */
1633 else if(result) {
1634 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1635
1636 if(response/100 == 4)
1637 /*
1638 * This is typically when the FTP server only allows a certain
1639 * amount of users and we are not one of them. All 4xx codes
1640 * are transient.
1641 */
1642 retry = RETRY_FTP;
1643 }
1644
1645 if(retry) {
1646 static const char * const m[]={
1647 NULL,
1648 "timeout",
1649 "connection refused",
1650 "HTTP error",
1651 "FTP error"
1652 };
1653
1654 warnf(config->global, "Transient problem: %s "
1655 "Will retry in %ld seconds. "
1656 "%ld retries left.\n",
1657 m[retry], retry_sleep/1000L, retry_numretries);
1658
1659 tool_go_sleep(retry_sleep);
1660 retry_numretries--;
1661 if(!config->retry_delay) {
1662 retry_sleep *= 2;
1663 if(retry_sleep > RETRY_SLEEP_MAX)
1664 retry_sleep = RETRY_SLEEP_MAX;
1665 }
1666 if(outs.bytes && outs.filename && outs.stream) {
1667 /* We have written data to a output file, we truncate file
1668 */
1669 if(!global->mute)
1670 fprintf(global->errors, "Throwing away %"
1671 CURL_FORMAT_CURL_OFF_T " bytes\n",
1672 outs.bytes);
1673 fflush(outs.stream);
1674 /* truncate file at the position where we started appending */
1675#ifdef HAVE_FTRUNCATE
1676 if(ftruncate(fileno(outs.stream), outs.init)) {
1677 /* when truncate fails, we can't just append as then we'll
1678 create something strange, bail out */
1679 if(!global->mute)
1680 fprintf(global->errors,
1681 "failed to truncate, exiting\n");
1682 result = CURLE_WRITE_ERROR;
1683 goto quit_urls;
1684 }
1685 /* now seek to the end of the file, the position where we
1686 just truncated the file in a large file-safe way */
1687 fseek(outs.stream, 0, SEEK_END);
1688#else
1689 /* ftruncate is not available, so just reposition the file
1690 to the location we would have truncated it. This won't
1691 work properly with large files on 32-bit systems, but
1692 most of those will have ftruncate. */
1693 fseek(outs.stream, (long)outs.init, SEEK_SET);
1694#endif
1695 outs.bytes = 0; /* clear for next round */
1696 }
1697 continue; /* curl_easy_perform loop */
1698 }
1699 } /* if retry_numretries */
1700 else if(metalink) {
1701 /* Metalink: Decide to try the next resource or
1702 not. Basically, we want to try the next resource if
1703 download was not successful. */
1704 long response;
1705 if(CURLE_OK == result) {
1706 /* TODO We want to try next resource when download was
1707 not successful. How to know that? */
1708 char *effective_url = NULL;
1709 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
1710 if(effective_url &&
1711 curl_strnequal(effective_url, "http", 4)) {
1712 /* This was HTTP(S) */
1713 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
1714 if(response != 200 && response != 206) {
1715 metalink_next_res = 1;
1716 fprintf(global->errors,
1717 "Metalink: fetching (%s) from (%s) FAILED "
1718 "(HTTP status code %ld)\n",
1719 mlfile->filename, this_url, response);
1720 }
1721 }
1722 }
1723 else {
1724 metalink_next_res = 1;
1725 fprintf(global->errors,
1726 "Metalink: fetching (%s) from (%s) FAILED (%s)\n",
1727 mlfile->filename, this_url,
1728 (errorbuffer[0]) ?
1729 errorbuffer : curl_easy_strerror(result));
1730 }
1731 }
1732 if(metalink && !metalink_next_res)
1733 fprintf(global->errors, "Metalink: fetching (%s) from (%s) OK\n",
1734 mlfile->filename, this_url);
1735
1736 /* In all ordinary cases, just break out of loop here */
1737 break; /* curl_easy_perform loop */
1738
1739 }
1740
1741 if((global->progressmode == CURL_PROGRESS_BAR) &&
1742 progressbar.calls)
1743 /* if the custom progress bar has been displayed, we output a
1744 newline here */
1745 fputs("\n", progressbar.out);
1746
1747 if(config->writeout)
1748 ourWriteOut(curl, &outs, config->writeout);
1749
1750 /*
1751 ** Code within this loop may jump directly here to label 'show_error'
1752 ** in order to display an error message for CURLcode stored in 'res'
1753 ** variable and exit loop once that necessary writing and cleanup
1754 ** in label 'quit_urls' has been done.
1755 */
1756
1757 show_error:
1758
1759#ifdef __VMS
1760 if(is_vms_shell()) {
1761 /* VMS DCL shell behavior */
1762 if(!global->showerror)
1763 vms_show = VMSSTS_HIDE;
1764 }
1765 else
1766#endif
1767 if(result && global->showerror) {
1768 fprintf(global->errors, "curl: (%d) %s\n", result, (errorbuffer[0]) ?
1769 errorbuffer : curl_easy_strerror(result));
1770 if(result == CURLE_SSL_CACERT)
1771 fprintf(global->errors, "%s%s%s",
1772 CURL_CA_CERT_ERRORMSG1, CURL_CA_CERT_ERRORMSG2,
1773 ((curlinfo->features & CURL_VERSION_HTTPS_PROXY) ?
1774 "HTTPS-proxy has similar options --proxy-cacert "
1775 "and --proxy-insecure.\n" :
1776 ""));
1777 }
1778
1779 /* Fall through comment to 'quit_urls' label */
1780
1781 /*
1782 ** Upon error condition and always that a message has already been
1783 ** displayed, code within this loop may jump directly here to label
1784 ** 'quit_urls' otherwise it should jump to 'show_error' label above.
1785 **
1786 ** When 'res' variable is _not_ CURLE_OK loop will exit once that
1787 ** all code following 'quit_urls' has been executed. Otherwise it
1788 ** will loop to the beginning from where it may exit if there are
1789 ** no more urls left.
1790 */
1791
1792 quit_urls:
1793
1794 /* Set file extended attributes */
1795 if(!result && config->xattr && outs.fopened && outs.stream) {
1796 int rc = fwrite_xattr(curl, fileno(outs.stream));
1797 if(rc)
1798 warnf(config->global, "Error setting extended attributes: %s\n",
1799 strerror(errno));
1800 }
1801
1802 /* Close the file */
1803 if(outs.fopened && outs.stream) {
1804 int rc = fclose(outs.stream);
1805 if(!result && rc) {
1806 /* something went wrong in the writing process */
1807 result = CURLE_WRITE_ERROR;
1808 fprintf(global->errors, "(%d) Failed writing body\n", result);
1809 }
1810 }
1811 else if(!outs.s_isreg && outs.stream) {
1812 /* Dump standard stream buffered data */
1813 int rc = fflush(outs.stream);
1814 if(!result && rc) {
1815 /* something went wrong in the writing process */
1816 result = CURLE_WRITE_ERROR;
1817 fprintf(global->errors, "(%d) Failed writing body\n", result);
1818 }
1819 }
1820
1821#ifdef __AMIGA__
1822 if(!result && outs.s_isreg && outs.filename) {
1823 /* Set the url (up to 80 chars) as comment for the file */
1824 if(strlen(url) > 78)
1825 url[79] = '\0';
1826 SetComment(outs.filename, url);
1827 }
1828#endif
1829
1830#if defined(HAVE_UTIME) || \
1831 (defined(WIN32) && (CURL_SIZEOF_CURL_OFF_T >= 8))
1832 /* File time can only be set _after_ the file has been closed */
1833 if(!result && config->remote_time && outs.s_isreg && outs.filename) {
1834 /* Ask libcurl if we got a remote file time */
1835 long filetime = -1;
1836 curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
1837 if(filetime >= 0)
1838 setfiletime(filetime, outs.filename, config->global->errors);
1839 }
1840#endif /* defined(HAVE_UTIME) || \
1841 (defined(WIN32) && (CURL_SIZEOF_CURL_OFF_T >= 8)) */
1842
1843#ifdef USE_METALINK
1844 if(!metalink && config->use_metalink && result == CURLE_OK) {
1845 int rv = parse_metalink(config, &outs, this_url);
1846 if(rv == 0)
1847 fprintf(config->global->errors, "Metalink: parsing (%s) OK\n",
1848 this_url);
1849 else if(rv == -1)
1850 fprintf(config->global->errors, "Metalink: parsing (%s) FAILED\n",
1851 this_url);
1852 }
1853 else if(metalink && result == CURLE_OK && !metalink_next_res) {
1854 int rv = metalink_check_hash(global, mlfile, outs.filename);
1855 if(rv == 0) {
1856 metalink_next_res = 1;
1857 }
1858 }
1859#endif /* USE_METALINK */
1860
1861 /* No more business with this output struct */
1862 if(outs.alloc_filename)
1863 Curl_safefree(outs.filename);
1864#ifdef USE_METALINK
1865 if(outs.metalink_parser)
1866 metalink_parser_context_delete(outs.metalink_parser);
1867#endif /* USE_METALINK */
1868 memset(&outs, 0, sizeof(struct OutStruct));
1869 hdrcbdata.outs = NULL;
1870
1871 /* Free loop-local allocated memory and close loop-local opened fd */
1872
1873 Curl_safefree(outfile);
1874 Curl_safefree(this_url);
1875
1876 if(infdopen)
1877 close(infd);
1878
1879 if(metalink) {
1880 /* Should exit if error is fatal. */
1881 if(is_fatal_error(result)) {
1882 break;
1883 }
1884 if(!metalink_next_res)
1885 break;
1886 mlres = mlres->next;
1887 if(mlres == NULL)
1888 /* TODO If metalink_next_res is 1 and mlres is NULL,
1889 * set res to error code
1890 */
1891 break;
1892 }
1893 else
1894 if(urlnum > 1) {
1895 /* when url globbing, exit loop upon critical error */
1896 if(is_fatal_error(result))
1897 break;
1898 }
1899 else if(result)
1900 /* when not url globbing, exit loop upon any error */
1901 break;
1902
1903 } /* loop to the next URL */
1904
1905 /* Free loop-local allocated memory */
1906
1907 Curl_safefree(uploadfile);
1908
1909 if(urls) {
1910 /* Free list of remaining URLs */
1911 glob_cleanup(urls);
1912 urls = NULL;
1913 }
1914
1915 if(infilenum > 1) {
1916 /* when file globbing, exit loop upon critical error */
1917 if(is_fatal_error(result))
1918 break;
1919 }
1920 else if(result)
1921 /* when not file globbing, exit loop upon any error */
1922 break;
1923
1924 } /* loop to the next globbed upload file */
1925
1926 /* Free loop-local allocated memory */
1927
1928 Curl_safefree(outfiles);
1929
1930 if(inglob) {
1931 /* Free list of globbed upload files */
1932 glob_cleanup(inglob);
1933 inglob = NULL;
1934 }
1935
1936 /* Free this URL node data without destroying the
1937 the node itself nor modifying next pointer. */
1938 Curl_safefree(urlnode->url);
1939 Curl_safefree(urlnode->outfile);
1940 Curl_safefree(urlnode->infile);
1941 urlnode->flags = 0;
1942
1943 /*
1944 ** Bail out upon critical errors or --fail-early
1945 */
1946 if(is_fatal_error(result) || (result && global->fail_early))
1947 goto quit_curl;
1948
1949 } /* for-loop through all URLs */
1950
1951 /*
1952 ** Nested loops end here.
1953 */
1954
1955 quit_curl:
1956
1957 /* Reset the global config variables */
1958 global->noprogress = orig_noprogress;
1959 global->isatty = orig_isatty;
1960
1961 /* Free function-local referenced allocated memory */
1962 Curl_safefree(httpgetfields);
1963
1964 /* Free list of given URLs */
1965 clean_getout(config);
1966
1967 hdrcbdata.heads = NULL;
1968
1969 /* Close function-local opened file descriptors */
1970 if(heads.fopened && heads.stream)
1971 fclose(heads.stream);
1972
1973 if(heads.alloc_filename)
1974 Curl_safefree(heads.filename);
1975
1976 /* Release metalink related resources here */
1977 clean_metalink(config);
1978
1979 return result;
1980}
1981
1982CURLcode operate(struct GlobalConfig *config, int argc, argv_item_t argv[])
1983{
1984 CURLcode result = CURLE_OK;
1985
1986 /* Setup proper locale from environment */
1987#ifdef HAVE_SETLOCALE
1988 setlocale(LC_ALL, "");
1989#endif
1990
1991 /* Parse .curlrc if necessary */
1992 if((argc == 1) ||
1993 (!curl_strequal(argv[1], "-q") &&
1994 !curl_strequal(argv[1], "--disable"))) {
1995 parseconfig(NULL, config); /* ignore possible failure */
1996
1997 /* If we had no arguments then make sure a url was specified in .curlrc */
1998 if((argc < 2) && (!config->first->url_list)) {
1999 helpf(config->errors, NULL);
2000 result = CURLE_FAILED_INIT;
2001 }
2002 }
2003
2004 if(!result) {
2005 /* Parse the command line arguments */
2006 ParameterError res = parse_args(config, argc, argv);
2007 if(res) {
2008 result = CURLE_OK;
2009
2010 /* Check if we were asked for the help */
2011 if(res == PARAM_HELP_REQUESTED)
2012 tool_help();
2013 /* Check if we were asked for the manual */
2014 else if(res == PARAM_MANUAL_REQUESTED)
2015 hugehelp();
2016 /* Check if we were asked for the version information */
2017 else if(res == PARAM_VERSION_INFO_REQUESTED)
2018 tool_version_info();
2019 /* Check if we were asked to list the SSL engines */
2020 else if(res == PARAM_ENGINES_REQUESTED)
2021 tool_list_engines(config->easy);
2022 else if(res == PARAM_LIBCURL_UNSUPPORTED_PROTOCOL)
2023 result = CURLE_UNSUPPORTED_PROTOCOL;
2024 else
2025 result = CURLE_FAILED_INIT;
2026 }
2027 else {
2028#ifndef CURL_DISABLE_LIBCURL_OPTION
2029 if(config->libcurl) {
2030 /* Initialise the libcurl source output */
2031 result = easysrc_init();
2032 }
2033#endif
2034
2035 /* Perform the main operations */
2036 if(!result) {
2037 size_t count = 0;
2038 struct OperationConfig *operation = config->first;
2039
2040 /* Get the required aguments for each operation */
2041 while(!result && operation) {
2042 result = get_args(operation, count++);
2043
2044 operation = operation->next;
2045 }
2046
2047 /* Set the current operation pointer */
2048 config->current = config->first;
2049
2050 /* Perform each operation */
2051 while(!result && config->current) {
2052 result = operate_do(config, config->current);
2053
2054 config->current = config->current->next;
2055
2056 if(config->current && config->current->easy)
2057 curl_easy_reset(config->current->easy);
2058 }
2059
2060#ifndef CURL_DISABLE_LIBCURL_OPTION
2061 if(config->libcurl) {
2062 /* Cleanup the libcurl source output */
2063 easysrc_cleanup();
2064
2065 /* Dump the libcurl code if previously enabled */
2066 dumpeasysrc(config);
2067 }
2068#endif
2069 }
2070 else
2071 helpf(config->errors, "out of memory\n");
2072 }
2073 }
2074
2075 return result;
2076}