tool_operate.c 90 KB

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