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