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