tool_operate.c 92 KB

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