tool_operate.c 89 KB

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