tool_operate.c 88 KB

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