tool_operate.c 87 KB

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