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