tool_operate.c 78 KB

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