tool_operate.c 81 KB

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