tool_operate.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  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
  245. * a line
  246. *
  247. * - Stat gives a size but this is UNRELIABLE in VMS. E.g.
  248. * a fixed file with implied CC needs to have a byte added for every
  249. * record processed, this can be derived from Filesize & recordsize
  250. * for VARiable record files the records need to be counted! for
  251. * every record add 1 for linefeed and subtract 2 for the record
  252. * header for VARIABLE header files only the bare record data needs
  253. * to be considered with one appended if implied CC
  254. */
  255. #ifdef __VMS
  256. /* Calculate the real upload size for VMS */
  257. per->infd = -1;
  258. if(stat(per->uploadfile, &fileinfo) == 0) {
  259. fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
  260. switch(fileinfo.st_fab_rfm) {
  261. case FAB$C_VAR:
  262. case FAB$C_VFC:
  263. case FAB$C_STMCR:
  264. per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
  265. break;
  266. default:
  267. per->infd = open(per->uploadfile, O_RDONLY | O_BINARY,
  268. "rfm=stmlf", "ctx=stm");
  269. }
  270. }
  271. if(per->infd == -1)
  272. #else
  273. per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
  274. if((per->infd == -1) || fstat(per->infd, &fileinfo))
  275. #endif
  276. {
  277. helpf(global->errors, "Can't open '%s'!\n", per->uploadfile);
  278. if(per->infd != -1) {
  279. close(per->infd);
  280. per->infd = STDIN_FILENO;
  281. }
  282. return CURLE_READ_ERROR;
  283. }
  284. per->infdopen = TRUE;
  285. /* we ignore file size for char/block devices, sockets, etc. */
  286. if(S_ISREG(fileinfo.st_mode))
  287. uploadfilesize = fileinfo.st_size;
  288. if(uploadfilesize != -1) {
  289. struct OperationConfig *config = per->config; /* for the macro below */
  290. #ifdef CURL_DISABLE_LIBCURL_OPTION
  291. (void)config;
  292. #endif
  293. my_setopt(per->curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
  294. }
  295. per->input.fd = per->infd;
  296. }
  297. 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. void single_transfer_cleanup(struct OperationConfig *config)
  317. {
  318. if(config) {
  319. struct State *state = &config->state;
  320. if(state->urls) {
  321. /* Free list of remaining URLs */
  322. glob_cleanup(state->urls);
  323. state->urls = NULL;
  324. }
  325. Curl_safefree(state->outfiles);
  326. Curl_safefree(state->httpgetfields);
  327. Curl_safefree(state->uploadfile);
  328. if(state->inglob) {
  329. /* Free list of globbed upload files */
  330. glob_cleanup(state->inglob);
  331. state->inglob = NULL;
  332. }
  333. }
  334. }
  335. /*
  336. * Call this after a transfer has completed.
  337. */
  338. static CURLcode post_per_transfer(struct GlobalConfig *global,
  339. struct per_transfer *per,
  340. CURLcode result,
  341. bool *retryp,
  342. long *delay) /* milliseconds! */
  343. {
  344. struct OutStruct *outs = &per->outs;
  345. CURL *curl = per->curl;
  346. struct OperationConfig *config = per->config;
  347. if(!curl || !config)
  348. return result;
  349. *retryp = FALSE;
  350. *delay = 0; /* for no retry, keep it zero */
  351. if(per->infdopen)
  352. close(per->infd);
  353. #ifdef __VMS
  354. if(is_vms_shell()) {
  355. /* VMS DCL shell behavior */
  356. if(!global->showerror)
  357. vms_show = VMSSTS_HIDE;
  358. }
  359. else
  360. #endif
  361. if(!config->synthetic_error && result && global->showerror) {
  362. const char *msg = per->errorbuffer;
  363. fprintf(global->errors, "curl: (%d) %s\n", result,
  364. (msg && msg[0]) ? msg : curl_easy_strerror(result));
  365. if(result == CURLE_PEER_FAILED_VERIFICATION)
  366. fputs(CURL_CA_CERT_ERRORMSG, global->errors);
  367. }
  368. else if(config->failwithbody) {
  369. /* if HTTP response >= 400, return error */
  370. long code = 0;
  371. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  372. if(code >= 400) {
  373. if(global->showerror)
  374. fprintf(global->errors,
  375. "curl: (%d) The requested URL returned error: %ld\n",
  376. CURLE_HTTP_RETURNED_ERROR, code);
  377. result = CURLE_HTTP_RETURNED_ERROR;
  378. }
  379. }
  380. /* Set file extended attributes */
  381. if(!result && config->xattr && outs->fopened && outs->stream) {
  382. int rc = fwrite_xattr(curl, per->this_url, fileno(outs->stream));
  383. if(rc)
  384. warnf(config->global, "Error setting extended attributes on '%s': %s\n",
  385. outs->filename, strerror(errno));
  386. }
  387. if(!result && !outs->stream && !outs->bytes) {
  388. /* we have received no data despite the transfer was successful
  389. ==> force creation of an empty output file (if an output file
  390. was specified) */
  391. long cond_unmet = 0L;
  392. /* do not create (or even overwrite) the file in case we get no
  393. data because of unmet condition */
  394. curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &cond_unmet);
  395. if(!cond_unmet && !tool_create_output_file(outs, config))
  396. result = CURLE_WRITE_ERROR;
  397. }
  398. if(!outs->s_isreg && outs->stream) {
  399. /* Dump standard stream buffered data */
  400. int rc = fflush(outs->stream);
  401. if(!result && rc) {
  402. /* something went wrong in the writing process */
  403. result = CURLE_WRITE_ERROR;
  404. if(global->showerror)
  405. fprintf(global->errors, "curl: (%d) Failed writing body\n", result);
  406. }
  407. }
  408. /* if retry-max-time is non-zero, make sure we haven't exceeded the
  409. time */
  410. if(per->retry_numretries &&
  411. (!config->retry_maxtime ||
  412. (tvdiff(tvnow(), per->retrystart) <
  413. config->retry_maxtime*1000L)) ) {
  414. enum {
  415. RETRY_NO,
  416. RETRY_ALL_ERRORS,
  417. RETRY_TIMEOUT,
  418. RETRY_CONNREFUSED,
  419. RETRY_HTTP,
  420. RETRY_FTP,
  421. RETRY_LAST /* not used */
  422. } retry = RETRY_NO;
  423. long response = 0;
  424. if((CURLE_OPERATION_TIMEDOUT == result) ||
  425. (CURLE_COULDNT_RESOLVE_HOST == result) ||
  426. (CURLE_COULDNT_RESOLVE_PROXY == result) ||
  427. (CURLE_FTP_ACCEPT_TIMEOUT == result))
  428. /* retry timeout always */
  429. retry = RETRY_TIMEOUT;
  430. else if(config->retry_connrefused &&
  431. (CURLE_COULDNT_CONNECT == result)) {
  432. long oserrno = 0;
  433. curl_easy_getinfo(curl, CURLINFO_OS_ERRNO, &oserrno);
  434. if(ECONNREFUSED == oserrno)
  435. retry = RETRY_CONNREFUSED;
  436. }
  437. else if((CURLE_OK == result) ||
  438. ((config->failonerror || config->failwithbody) &&
  439. (CURLE_HTTP_RETURNED_ERROR == result))) {
  440. /* If it returned OK. _or_ failonerror was enabled and it
  441. returned due to such an error, check for HTTP transient
  442. errors to retry on. */
  443. const char *scheme;
  444. curl_easy_getinfo(curl, CURLINFO_SCHEME, &scheme);
  445. scheme = proto_token(scheme);
  446. if(scheme == proto_http || scheme == proto_https) {
  447. /* This was HTTP(S) */
  448. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
  449. switch(response) {
  450. case 408: /* Request Timeout */
  451. case 429: /* Too Many Requests (RFC6585) */
  452. case 500: /* Internal Server Error */
  453. case 502: /* Bad Gateway */
  454. case 503: /* Service Unavailable */
  455. case 504: /* Gateway Timeout */
  456. retry = RETRY_HTTP;
  457. /*
  458. * At this point, we have already written data to the output
  459. * file (or terminal). If we write to a file, we must rewind
  460. * or close/re-open the file so that the next attempt starts
  461. * over from the beginning.
  462. *
  463. * TODO: similar action for the upload case. We might need
  464. * to start over reading from a previous point if we have
  465. * uploaded something when this was returned.
  466. */
  467. break;
  468. }
  469. }
  470. } /* if CURLE_OK */
  471. else if(result) {
  472. const char *scheme;
  473. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
  474. curl_easy_getinfo(curl, CURLINFO_SCHEME, &scheme);
  475. scheme = proto_token(scheme);
  476. if((scheme == proto_ftp || scheme == proto_ftps) && response / 100 == 4)
  477. /*
  478. * This is typically when the FTP server only allows a certain
  479. * amount of users and we are not one of them. All 4xx codes
  480. * are transient.
  481. */
  482. retry = RETRY_FTP;
  483. }
  484. if(result && !retry && config->retry_all_errors)
  485. retry = RETRY_ALL_ERRORS;
  486. if(retry) {
  487. long sleeptime = 0;
  488. curl_off_t retry_after = 0;
  489. static const char * const m[]={
  490. NULL,
  491. "(retrying all errors)",
  492. ": timeout",
  493. ": connection refused",
  494. ": HTTP error",
  495. ": FTP error"
  496. };
  497. sleeptime = per->retry_sleep;
  498. if(RETRY_HTTP == retry) {
  499. curl_easy_getinfo(curl, CURLINFO_RETRY_AFTER, &retry_after);
  500. if(retry_after) {
  501. /* store in a 'long', make sure it doesn't overflow */
  502. if(retry_after > LONG_MAX/1000)
  503. sleeptime = LONG_MAX;
  504. else
  505. sleeptime = (long)retry_after * 1000; /* milliseconds */
  506. /* if adding retry_after seconds to the process would exceed the
  507. maximum time allowed for retrying, then exit the retries right
  508. away */
  509. if(config->retry_maxtime) {
  510. curl_off_t seconds = tvdiff(tvnow(), per->retrystart)/1000;
  511. if((CURL_OFF_T_MAX - retry_after < seconds) ||
  512. (seconds + retry_after > config->retry_maxtime)) {
  513. warnf(config->global, "The Retry-After: time would "
  514. "make this command line exceed the maximum allowed time "
  515. "for retries.");
  516. goto noretry;
  517. }
  518. }
  519. }
  520. }
  521. warnf(config->global, "Problem %s. "
  522. "Will retry in %ld seconds. "
  523. "%ld retries left.\n",
  524. m[retry], sleeptime/1000L, per->retry_numretries);
  525. per->retry_numretries--;
  526. if(!config->retry_delay) {
  527. per->retry_sleep *= 2;
  528. if(per->retry_sleep > RETRY_SLEEP_MAX)
  529. per->retry_sleep = RETRY_SLEEP_MAX;
  530. }
  531. if(outs->bytes && outs->filename && outs->stream) {
  532. int rc;
  533. /* We have written data to an output file, we truncate file
  534. */
  535. if(!global->mute)
  536. fprintf(global->errors, "Throwing away %"
  537. CURL_FORMAT_CURL_OFF_T " bytes\n",
  538. outs->bytes);
  539. fflush(outs->stream);
  540. /* truncate file at the position where we started appending */
  541. #ifdef HAVE_FTRUNCATE
  542. if(ftruncate(fileno(outs->stream), outs->init)) {
  543. /* when truncate fails, we can't just append as then we'll
  544. create something strange, bail out */
  545. if(global->showerror)
  546. fprintf(global->errors,
  547. "curl: (23) Failed to truncate file\n");
  548. return CURLE_WRITE_ERROR;
  549. }
  550. /* now seek to the end of the file, the position where we
  551. just truncated the file in a large file-safe way */
  552. rc = fseek(outs->stream, 0, SEEK_END);
  553. #else
  554. /* ftruncate is not available, so just reposition the file
  555. to the location we would have truncated it. This won't
  556. work properly with large files on 32-bit systems, but
  557. most of those will have ftruncate. */
  558. rc = fseek(outs->stream, (long)outs->init, SEEK_SET);
  559. #endif
  560. if(rc) {
  561. if(global->showerror)
  562. fprintf(global->errors,
  563. "curl: (23) Failed seeking to end of file\n");
  564. return CURLE_WRITE_ERROR;
  565. }
  566. outs->bytes = 0; /* clear for next round */
  567. }
  568. *retryp = TRUE;
  569. *delay = sleeptime;
  570. return CURLE_OK;
  571. }
  572. } /* if retry_numretries */
  573. noretry:
  574. if((global->progressmode == CURL_PROGRESS_BAR) &&
  575. per->progressbar.calls)
  576. /* if the custom progress bar has been displayed, we output a
  577. newline here */
  578. fputs("\n", per->progressbar.out);
  579. /* Close the outs file */
  580. if(outs->fopened && outs->stream) {
  581. int rc = fclose(outs->stream);
  582. if(!result && rc) {
  583. /* something went wrong in the writing process */
  584. result = CURLE_WRITE_ERROR;
  585. if(global->showerror)
  586. fprintf(global->errors, "curl: (%d) Failed writing body\n", result);
  587. }
  588. if(result && config->rm_partial) {
  589. notef(global, "Removing output file: %s\n", outs->filename);
  590. unlink(outs->filename);
  591. }
  592. }
  593. AmigaSetComment(per, result);
  594. /* File time can only be set _after_ the file has been closed */
  595. if(!result && config->remote_time && outs->s_isreg && outs->filename) {
  596. /* Ask libcurl if we got a remote file time */
  597. curl_off_t filetime = -1;
  598. curl_easy_getinfo(curl, CURLINFO_FILETIME_T, &filetime);
  599. setfiletime(filetime, outs->filename, global);
  600. }
  601. /* Write the --write-out data before cleanup but after result is final */
  602. if(config->writeout)
  603. ourWriteOut(config->writeout, per, result);
  604. /* Close function-local opened file descriptors */
  605. if(per->heads.fopened && per->heads.stream)
  606. fclose(per->heads.stream);
  607. if(per->heads.alloc_filename)
  608. Curl_safefree(per->heads.filename);
  609. if(per->etag_save.fopened && per->etag_save.stream)
  610. fclose(per->etag_save.stream);
  611. if(per->etag_save.alloc_filename)
  612. Curl_safefree(per->etag_save.filename);
  613. curl_easy_cleanup(per->curl);
  614. if(outs->alloc_filename)
  615. free(outs->filename);
  616. free(per->this_url);
  617. free(per->outfile);
  618. free(per->uploadfile);
  619. if(global->parallel)
  620. free(per->errorbuffer);
  621. return result;
  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. result = add_file_name_to_url(per->curl, &per->this_url,
  1030. per->uploadfile);
  1031. if(result)
  1032. break;
  1033. }
  1034. else if(per->uploadfile && stdin_upload(per->uploadfile)) {
  1035. /* count to see if there are more than one auth bit set
  1036. in the authtype field */
  1037. int authbits = 0;
  1038. int bitcheck = 0;
  1039. while(bitcheck < 32) {
  1040. if(config->authtype & (1UL << bitcheck++)) {
  1041. authbits++;
  1042. if(authbits > 1) {
  1043. /* more than one, we're done! */
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. /*
  1049. * If the user has also selected --anyauth or --proxy-anyauth
  1050. * we should warn them.
  1051. */
  1052. if(config->proxyanyauth || (authbits>1)) {
  1053. warnf(global,
  1054. "Using --anyauth or --proxy-anyauth with upload from stdin"
  1055. " involves a big risk of it not working. Use a temporary"
  1056. " file or a fixed auth type instead!\n");
  1057. }
  1058. DEBUGASSERT(per->infdopen == FALSE);
  1059. DEBUGASSERT(per->infd == STDIN_FILENO);
  1060. set_binmode(stdin);
  1061. if(!strcmp(per->uploadfile, ".")) {
  1062. if(curlx_nonblock((curl_socket_t)per->infd, TRUE) < 0)
  1063. warnf(global,
  1064. "fcntl failed on fd=%d: %s\n", per->infd, strerror(errno));
  1065. }
  1066. }
  1067. if(per->uploadfile && config->resume_from_current)
  1068. config->resume_from = -1; /* -1 will then force get-it-yourself */
  1069. if(output_expected(per->this_url, per->uploadfile) && outs->stream &&
  1070. isatty(fileno(outs->stream)))
  1071. /* we send the output to a tty, therefore we switch off the progress
  1072. meter */
  1073. per->noprogress = global->noprogress = global->isatty = TRUE;
  1074. else {
  1075. /* progress meter is per download, so restore config
  1076. values */
  1077. per->noprogress = global->noprogress = orig_noprogress;
  1078. global->isatty = orig_isatty;
  1079. }
  1080. if(httpgetfields) {
  1081. CURLU *uh = curl_url();
  1082. if(uh) {
  1083. char *updated;
  1084. if(curl_url_set(uh, CURLUPART_URL, per->this_url,
  1085. CURLU_GUESS_SCHEME) ||
  1086. curl_url_set(uh, CURLUPART_QUERY, httpgetfields,
  1087. CURLU_APPENDQUERY) ||
  1088. curl_url_get(uh, CURLUPART_URL, &updated, CURLU_GUESS_SCHEME)) {
  1089. curl_url_cleanup(uh);
  1090. result = CURLE_OUT_OF_MEMORY;
  1091. break;
  1092. }
  1093. Curl_safefree(per->this_url); /* free previous URL */
  1094. per->this_url = updated; /* use our new URL instead! */
  1095. curl_url_cleanup(uh);
  1096. }
  1097. }
  1098. if(!global->errors)
  1099. global->errors = stderr;
  1100. if((!per->outfile || !strcmp(per->outfile, "-")) &&
  1101. !config->use_ascii) {
  1102. /* We get the output to stdout and we have not got the ASCII/text
  1103. flag, then set stdout to be binary */
  1104. set_binmode(stdout);
  1105. }
  1106. /* explicitly passed to stdout means okaying binary gunk */
  1107. config->terminal_binary_ok =
  1108. (per->outfile && !strcmp(per->outfile, "-"));
  1109. /* Avoid having this setopt added to the --libcurl source output. */
  1110. result = curl_easy_setopt(curl, CURLOPT_SHARE, share);
  1111. if(result)
  1112. break;
  1113. use_proto = url_proto(per->this_url);
  1114. if(!config->tcp_nodelay)
  1115. my_setopt(curl, CURLOPT_TCP_NODELAY, 0L);
  1116. if(config->tcp_fastopen)
  1117. my_setopt(curl, CURLOPT_TCP_FASTOPEN, 1L);
  1118. /* where to store */
  1119. my_setopt(curl, CURLOPT_WRITEDATA, per);
  1120. my_setopt(curl, CURLOPT_INTERLEAVEDATA, per);
  1121. /* what call to write */
  1122. my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
  1123. /* for uploads */
  1124. input->config = config;
  1125. /* Note that if CURLOPT_READFUNCTION is fread (the default), then
  1126. * lib/telnet.c will Curl_poll() on the input file descriptor
  1127. * rather than calling the READFUNCTION at regular intervals.
  1128. * The circumstances in which it is preferable to enable this
  1129. * behavior, by omitting to set the READFUNCTION & READDATA options,
  1130. * have not been determined.
  1131. */
  1132. my_setopt(curl, CURLOPT_READDATA, input);
  1133. /* what call to read */
  1134. my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
  1135. /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
  1136. CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
  1137. my_setopt(curl, CURLOPT_SEEKDATA, input);
  1138. my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
  1139. if(config->recvpersecond &&
  1140. (config->recvpersecond < BUFFER_SIZE))
  1141. /* use a smaller sized buffer for better sleeps */
  1142. my_setopt(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond);
  1143. else
  1144. my_setopt(curl, CURLOPT_BUFFERSIZE, (long)BUFFER_SIZE);
  1145. my_setopt_str(curl, CURLOPT_URL, per->this_url);
  1146. my_setopt(curl, CURLOPT_NOPROGRESS, global->noprogress?1L:0L);
  1147. if(config->no_body)
  1148. my_setopt(curl, CURLOPT_NOBODY, 1L);
  1149. if(config->oauth_bearer)
  1150. my_setopt_str(curl, CURLOPT_XOAUTH2_BEARER, config->oauth_bearer);
  1151. my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
  1152. if(config->proxy && result) {
  1153. errorf(global, "proxy support is disabled in this libcurl\n");
  1154. config->synthetic_error = TRUE;
  1155. result = CURLE_NOT_BUILT_IN;
  1156. break;
  1157. }
  1158. /* new in libcurl 7.5 */
  1159. if(config->proxy)
  1160. my_setopt_enum(curl, CURLOPT_PROXYTYPE, config->proxyver);
  1161. my_setopt_str(curl, CURLOPT_PROXYUSERPWD, config->proxyuserpwd);
  1162. /* new in libcurl 7.3 */
  1163. my_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config->proxytunnel?1L:0L);
  1164. /* new in libcurl 7.52.0 */
  1165. if(config->preproxy)
  1166. my_setopt_str(curl, CURLOPT_PRE_PROXY, config->preproxy);
  1167. /* new in libcurl 7.10.6 */
  1168. if(config->proxyanyauth)
  1169. my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
  1170. (long)CURLAUTH_ANY);
  1171. else if(config->proxynegotiate)
  1172. my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
  1173. (long)CURLAUTH_GSSNEGOTIATE);
  1174. else if(config->proxyntlm)
  1175. my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
  1176. (long)CURLAUTH_NTLM);
  1177. else if(config->proxydigest)
  1178. my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
  1179. (long)CURLAUTH_DIGEST);
  1180. else if(config->proxybasic)
  1181. my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
  1182. (long)CURLAUTH_BASIC);
  1183. /* new in libcurl 7.19.4 */
  1184. my_setopt_str(curl, CURLOPT_NOPROXY, config->noproxy);
  1185. my_setopt(curl, CURLOPT_SUPPRESS_CONNECT_HEADERS,
  1186. config->suppress_connect_headers?1L:0L);
  1187. my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror?1L:0L);
  1188. my_setopt(curl, CURLOPT_REQUEST_TARGET, config->request_target);
  1189. my_setopt(curl, CURLOPT_UPLOAD, per->uploadfile?1L:0L);
  1190. my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly?1L:0L);
  1191. my_setopt(curl, CURLOPT_APPEND, config->ftp_append?1L:0L);
  1192. if(config->netrc_opt)
  1193. my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_OPTIONAL);
  1194. else if(config->netrc || config->netrc_file)
  1195. my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_REQUIRED);
  1196. else
  1197. my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_IGNORED);
  1198. if(config->netrc_file)
  1199. my_setopt_str(curl, CURLOPT_NETRC_FILE, config->netrc_file);
  1200. my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii?1L:0L);
  1201. if(config->login_options)
  1202. my_setopt_str(curl, CURLOPT_LOGIN_OPTIONS, config->login_options);
  1203. my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
  1204. my_setopt_str(curl, CURLOPT_RANGE, config->range);
  1205. if(!global->parallel) {
  1206. per->errorbuffer = global_errorbuffer;
  1207. my_setopt(curl, CURLOPT_ERRORBUFFER, global_errorbuffer);
  1208. }
  1209. my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000));
  1210. switch(config->httpreq) {
  1211. case HTTPREQ_SIMPLEPOST:
  1212. my_setopt_str(curl, CURLOPT_POSTFIELDS,
  1213. config->postfields);
  1214. my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
  1215. config->postfieldsize);
  1216. break;
  1217. case HTTPREQ_MIMEPOST:
  1218. /* free previous remainders */
  1219. curl_mime_free(config->mimepost);
  1220. config->mimepost = NULL;
  1221. result = tool2curlmime(curl, config->mimeroot, &config->mimepost);
  1222. if(result)
  1223. break;
  1224. my_setopt_mimepost(curl, CURLOPT_MIMEPOST, config->mimepost);
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. if(result)
  1230. break;
  1231. /* new in libcurl 7.81.0 */
  1232. if(config->mime_options)
  1233. my_setopt(curl, CURLOPT_MIME_OPTIONS, config->mime_options);
  1234. /* new in libcurl 7.10.6 (default is Basic) */
  1235. if(config->authtype)
  1236. my_setopt_bitmask(curl, CURLOPT_HTTPAUTH, (long)config->authtype);
  1237. my_setopt_slist(curl, CURLOPT_HTTPHEADER, config->headers);
  1238. if(proto_http || proto_rtsp) {
  1239. my_setopt_str(curl, CURLOPT_REFERER, config->referer);
  1240. my_setopt_str(curl, CURLOPT_USERAGENT, config->useragent);
  1241. }
  1242. if(proto_http) {
  1243. long postRedir = 0;
  1244. my_setopt(curl, CURLOPT_FOLLOWLOCATION,
  1245. config->followlocation?1L:0L);
  1246. my_setopt(curl, CURLOPT_UNRESTRICTED_AUTH,
  1247. config->unrestricted_auth?1L:0L);
  1248. my_setopt(curl, CURLOPT_AUTOREFERER, config->autoreferer?1L:0L);
  1249. /* new in libcurl 7.36.0 */
  1250. if(config->proxyheaders) {
  1251. my_setopt_slist(curl, CURLOPT_PROXYHEADER, config->proxyheaders);
  1252. my_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE);
  1253. }
  1254. /* new in libcurl 7.5 */
  1255. my_setopt(curl, CURLOPT_MAXREDIRS, config->maxredirs);
  1256. if(config->httpversion)
  1257. my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion);
  1258. else if(curlinfo->features & CURL_VERSION_HTTP2) {
  1259. my_setopt_enum(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
  1260. }
  1261. /* curl 7.19.1 (the 301 version existed in 7.18.2),
  1262. 303 was added in 7.26.0 */
  1263. if(config->post301)
  1264. postRedir |= CURL_REDIR_POST_301;
  1265. if(config->post302)
  1266. postRedir |= CURL_REDIR_POST_302;
  1267. if(config->post303)
  1268. postRedir |= CURL_REDIR_POST_303;
  1269. my_setopt(curl, CURLOPT_POSTREDIR, postRedir);
  1270. /* new in libcurl 7.21.6 */
  1271. if(config->encoding)
  1272. my_setopt_str(curl, CURLOPT_ACCEPT_ENCODING, "");
  1273. /* new in libcurl 7.21.6 */
  1274. if(config->tr_encoding)
  1275. my_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1L);
  1276. /* new in libcurl 7.64.0 */
  1277. my_setopt(curl, CURLOPT_HTTP09_ALLOWED,
  1278. config->http09_allowed ? 1L : 0L);
  1279. if(result) {
  1280. errorf(global, "HTTP/0.9 is not supported in this build!\n");
  1281. return result;
  1282. }
  1283. } /* (proto_http) */
  1284. if(proto_ftp)
  1285. my_setopt_str(curl, CURLOPT_FTPPORT, config->ftpport);
  1286. my_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,
  1287. config->low_speed_limit);
  1288. my_setopt(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
  1289. my_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE,
  1290. config->sendpersecond);
  1291. my_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE,
  1292. config->recvpersecond);
  1293. if(config->use_resume)
  1294. my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, config->resume_from);
  1295. else
  1296. my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, CURL_OFF_T_C(0));
  1297. my_setopt_str(curl, CURLOPT_KEYPASSWD, config->key_passwd);
  1298. my_setopt_str(curl, CURLOPT_PROXY_KEYPASSWD, config->proxy_key_passwd);
  1299. if(use_proto == proto_scp || use_proto == proto_sftp) {
  1300. /* SSH and SSL private key uses same command-line option */
  1301. /* new in libcurl 7.16.1 */
  1302. my_setopt_str(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key);
  1303. /* new in libcurl 7.16.1 */
  1304. my_setopt_str(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey);
  1305. /* new in libcurl 7.17.1: SSH host key md5 checking allows us
  1306. to fail if we are not talking to who we think we should */
  1307. my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,
  1308. config->hostpubmd5);
  1309. /* new in libcurl 7.80.0: SSH host key sha256 checking allows us
  1310. to fail if we are not talking to who we think we should */
  1311. my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256,
  1312. config->hostpubsha256);
  1313. /* new in libcurl 7.56.0 */
  1314. if(config->ssh_compression)
  1315. my_setopt(curl, CURLOPT_SSH_COMPRESSION, 1L);
  1316. }
  1317. if(config->cacert)
  1318. my_setopt_str(curl, CURLOPT_CAINFO, config->cacert);
  1319. if(config->proxy_cacert)
  1320. my_setopt_str(curl, CURLOPT_PROXY_CAINFO, config->proxy_cacert);
  1321. if(config->capath) {
  1322. result = res_setopt_str(curl, CURLOPT_CAPATH, config->capath);
  1323. if(result == CURLE_NOT_BUILT_IN) {
  1324. warnf(global, "ignoring %s, not supported by libcurl\n",
  1325. capath_from_env?
  1326. "SSL_CERT_DIR environment variable":"--capath");
  1327. }
  1328. else if(result)
  1329. break;
  1330. }
  1331. /* For the time being if --proxy-capath is not set then we use the
  1332. --capath value for it, if any. See #1257 */
  1333. if(config->proxy_capath || config->capath) {
  1334. result = res_setopt_str(curl, CURLOPT_PROXY_CAPATH,
  1335. (config->proxy_capath ?
  1336. config->proxy_capath :
  1337. config->capath));
  1338. if(result == CURLE_NOT_BUILT_IN) {
  1339. if(config->proxy_capath) {
  1340. warnf(global,
  1341. "ignoring --proxy-capath, not supported by libcurl\n");
  1342. }
  1343. }
  1344. else if(result)
  1345. break;
  1346. }
  1347. if(config->crlfile)
  1348. my_setopt_str(curl, CURLOPT_CRLFILE, config->crlfile);
  1349. if(config->proxy_crlfile)
  1350. my_setopt_str(curl, CURLOPT_PROXY_CRLFILE, config->proxy_crlfile);
  1351. else if(config->crlfile) /* CURLOPT_PROXY_CRLFILE default is crlfile */
  1352. my_setopt_str(curl, CURLOPT_PROXY_CRLFILE, config->crlfile);
  1353. if(config->pinnedpubkey)
  1354. my_setopt_str(curl, CURLOPT_PINNEDPUBLICKEY, config->pinnedpubkey);
  1355. if(config->ssl_ec_curves)
  1356. my_setopt_str(curl, CURLOPT_SSL_EC_CURVES, config->ssl_ec_curves);
  1357. if(curlinfo->features & CURL_VERSION_SSL) {
  1358. /* Check if config->cert is a PKCS#11 URI and set the
  1359. * config->cert_type if necessary */
  1360. if(config->cert) {
  1361. if(!config->cert_type) {
  1362. if(is_pkcs11_uri(config->cert)) {
  1363. config->cert_type = strdup("ENG");
  1364. }
  1365. }
  1366. }
  1367. /* Check if config->key is a PKCS#11 URI and set the
  1368. * config->key_type if necessary */
  1369. if(config->key) {
  1370. if(!config->key_type) {
  1371. if(is_pkcs11_uri(config->key)) {
  1372. config->key_type = strdup("ENG");
  1373. }
  1374. }
  1375. }
  1376. /* Check if config->proxy_cert is a PKCS#11 URI and set the
  1377. * config->proxy_type if necessary */
  1378. if(config->proxy_cert) {
  1379. if(!config->proxy_cert_type) {
  1380. if(is_pkcs11_uri(config->proxy_cert)) {
  1381. config->proxy_cert_type = strdup("ENG");
  1382. }
  1383. }
  1384. }
  1385. /* Check if config->proxy_key is a PKCS#11 URI and set the
  1386. * config->proxy_key_type if necessary */
  1387. if(config->proxy_key) {
  1388. if(!config->proxy_key_type) {
  1389. if(is_pkcs11_uri(config->proxy_key)) {
  1390. config->proxy_key_type = strdup("ENG");
  1391. }
  1392. }
  1393. }
  1394. /* In debug build of curl tool, using
  1395. * --cert loadmem=<filename>:<password> --cert-type p12
  1396. * must do the same thing as classic:
  1397. * --cert <filename>:<password> --cert-type p12
  1398. * but is designed to test blob */
  1399. #if defined(CURLDEBUG) || defined(DEBUGBUILD)
  1400. if(config->cert && (strlen(config->cert) > 8) &&
  1401. (memcmp(config->cert, "loadmem=",8) == 0)) {
  1402. FILE *fInCert = fopen(config->cert + 8, "rb");
  1403. void *certdata = NULL;
  1404. long filesize = 0;
  1405. bool continue_reading = fInCert != NULL;
  1406. if(continue_reading)
  1407. continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
  1408. if(continue_reading)
  1409. filesize = ftell(fInCert);
  1410. if(filesize < 0)
  1411. continue_reading = FALSE;
  1412. if(continue_reading)
  1413. continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
  1414. if(continue_reading)
  1415. certdata = malloc(((size_t)filesize) + 1);
  1416. if((!certdata) ||
  1417. ((int)fread(certdata, (size_t)filesize, 1, fInCert) != 1))
  1418. continue_reading = FALSE;
  1419. if(fInCert)
  1420. fclose(fInCert);
  1421. if((filesize > 0) && continue_reading) {
  1422. struct curl_blob structblob;
  1423. structblob.data = certdata;
  1424. structblob.len = (size_t)filesize;
  1425. structblob.flags = CURL_BLOB_COPY;
  1426. my_setopt_str(curl, CURLOPT_SSLCERT_BLOB, &structblob);
  1427. /* if test run well, we are sure we don't reuse
  1428. * original mem pointer */
  1429. memset(certdata, 0, (size_t)filesize);
  1430. }
  1431. free(certdata);
  1432. }
  1433. else
  1434. #endif
  1435. my_setopt_str(curl, CURLOPT_SSLCERT, config->cert);
  1436. my_setopt_str(curl, CURLOPT_PROXY_SSLCERT, config->proxy_cert);
  1437. my_setopt_str(curl, CURLOPT_SSLCERTTYPE, config->cert_type);
  1438. my_setopt_str(curl, CURLOPT_PROXY_SSLCERTTYPE,
  1439. config->proxy_cert_type);
  1440. #if defined(CURLDEBUG) || defined(DEBUGBUILD)
  1441. if(config->key && (strlen(config->key) > 8) &&
  1442. (memcmp(config->key, "loadmem=",8) == 0)) {
  1443. FILE *fInCert = fopen(config->key + 8, "rb");
  1444. void *certdata = NULL;
  1445. long filesize = 0;
  1446. bool continue_reading = fInCert != NULL;
  1447. if(continue_reading)
  1448. continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
  1449. if(continue_reading)
  1450. filesize = ftell(fInCert);
  1451. if(filesize < 0)
  1452. continue_reading = FALSE;
  1453. if(continue_reading)
  1454. continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
  1455. if(continue_reading)
  1456. certdata = malloc(((size_t)filesize) + 1);
  1457. if((!certdata) ||
  1458. ((int)fread(certdata, (size_t)filesize, 1, fInCert) != 1))
  1459. continue_reading = FALSE;
  1460. if(fInCert)
  1461. fclose(fInCert);
  1462. if((filesize > 0) && continue_reading) {
  1463. struct curl_blob structblob;
  1464. structblob.data = certdata;
  1465. structblob.len = (size_t)filesize;
  1466. structblob.flags = CURL_BLOB_COPY;
  1467. my_setopt_str(curl, CURLOPT_SSLKEY_BLOB, &structblob);
  1468. /* if test run well, we are sure we don't reuse
  1469. * original mem pointer */
  1470. memset(certdata, 0, (size_t)filesize);
  1471. }
  1472. free(certdata);
  1473. }
  1474. else
  1475. #endif
  1476. my_setopt_str(curl, CURLOPT_SSLKEY, config->key);
  1477. my_setopt_str(curl, CURLOPT_PROXY_SSLKEY, config->proxy_key);
  1478. my_setopt_str(curl, CURLOPT_SSLKEYTYPE, config->key_type);
  1479. my_setopt_str(curl, CURLOPT_PROXY_SSLKEYTYPE,
  1480. config->proxy_key_type);
  1481. my_setopt_str(curl, CURLOPT_AWS_SIGV4,
  1482. config->aws_sigv4);
  1483. if(config->insecure_ok) {
  1484. my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  1485. my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  1486. }
  1487. else {
  1488. my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  1489. /* libcurl default is strict verifyhost -> 2L */
  1490. /* my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); */
  1491. }
  1492. if(config->doh_insecure_ok) {
  1493. my_setopt(curl, CURLOPT_DOH_SSL_VERIFYPEER, 0L);
  1494. my_setopt(curl, CURLOPT_DOH_SSL_VERIFYHOST, 0L);
  1495. }
  1496. if(config->proxy_insecure_ok) {
  1497. my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);
  1498. my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);
  1499. }
  1500. else {
  1501. my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 1L);
  1502. }
  1503. if(config->verifystatus)
  1504. my_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1L);
  1505. if(config->doh_verifystatus)
  1506. my_setopt(curl, CURLOPT_DOH_SSL_VERIFYSTATUS, 1L);
  1507. if(config->falsestart)
  1508. my_setopt(curl, CURLOPT_SSL_FALSESTART, 1L);
  1509. my_setopt_enum(curl, CURLOPT_SSLVERSION,
  1510. config->ssl_version | config->ssl_version_max);
  1511. if(config->proxy)
  1512. my_setopt_enum(curl, CURLOPT_PROXY_SSLVERSION,
  1513. config->proxy_ssl_version);
  1514. {
  1515. long mask =
  1516. (config->ssl_allow_beast ?
  1517. CURLSSLOPT_ALLOW_BEAST : 0) |
  1518. (config->ssl_no_revoke ?
  1519. CURLSSLOPT_NO_REVOKE : 0) |
  1520. (config->ssl_revoke_best_effort ?
  1521. CURLSSLOPT_REVOKE_BEST_EFFORT : 0) |
  1522. (config->native_ca_store ?
  1523. CURLSSLOPT_NATIVE_CA : 0) |
  1524. (config->ssl_auto_client_cert ?
  1525. CURLSSLOPT_AUTO_CLIENT_CERT : 0);
  1526. if(mask)
  1527. my_setopt_bitmask(curl, CURLOPT_SSL_OPTIONS, mask);
  1528. }
  1529. {
  1530. long mask =
  1531. (config->proxy_ssl_allow_beast ?
  1532. CURLSSLOPT_ALLOW_BEAST : 0) |
  1533. (config->proxy_ssl_auto_client_cert ?
  1534. CURLSSLOPT_AUTO_CLIENT_CERT : 0);
  1535. if(mask)
  1536. my_setopt_bitmask(curl, CURLOPT_PROXY_SSL_OPTIONS, mask);
  1537. }
  1538. }
  1539. if(config->path_as_is)
  1540. my_setopt(curl, CURLOPT_PATH_AS_IS, 1L);
  1541. if((use_proto == proto_scp || use_proto == proto_sftp) &&
  1542. !config->insecure_ok) {
  1543. char *known = findfile(".ssh/known_hosts", FALSE);
  1544. if(known) {
  1545. /* new in curl 7.19.6 */
  1546. result = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, known);
  1547. curl_free(known);
  1548. if(result == CURLE_UNKNOWN_OPTION)
  1549. /* libssh2 version older than 1.1.1 */
  1550. result = CURLE_OK;
  1551. if(result)
  1552. break;
  1553. }
  1554. else
  1555. warnf(global, "Couldn't find a known_hosts file!");
  1556. }
  1557. if(config->no_body || config->remote_time) {
  1558. /* no body or use remote time */
  1559. my_setopt(curl, CURLOPT_FILETIME, 1L);
  1560. }
  1561. my_setopt(curl, CURLOPT_CRLF, config->crlf?1L:0L);
  1562. my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
  1563. my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
  1564. my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
  1565. if(config->cookies) {
  1566. struct curlx_dynbuf cookies;
  1567. struct curl_slist *cl;
  1568. /* The maximum size needs to match MAX_NAME in cookie.h */
  1569. #define MAX_COOKIE_LINE 4096
  1570. curlx_dyn_init(&cookies, MAX_COOKIE_LINE);
  1571. for(cl = config->cookies; cl; cl = cl->next) {
  1572. if(cl == config->cookies)
  1573. result = curlx_dyn_addf(&cookies, "%s", cl->data);
  1574. else
  1575. result = curlx_dyn_addf(&cookies, ";%s", cl->data);
  1576. if(result) {
  1577. warnf(global,
  1578. "skipped provided cookie, the cookie header "
  1579. "would go over %u bytes\n", MAX_COOKIE_LINE);
  1580. break;
  1581. }
  1582. }
  1583. my_setopt_str(curl, CURLOPT_COOKIE, curlx_dyn_ptr(&cookies));
  1584. curlx_dyn_free(&cookies);
  1585. }
  1586. if(config->cookiefiles) {
  1587. struct curl_slist *cfl;
  1588. for(cfl = config->cookiefiles; cfl; cfl = cfl->next)
  1589. my_setopt_str(curl, CURLOPT_COOKIEFILE, cfl->data);
  1590. }
  1591. /* new in libcurl 7.9 */
  1592. if(config->cookiejar)
  1593. my_setopt_str(curl, CURLOPT_COOKIEJAR, config->cookiejar);
  1594. /* new in libcurl 7.9.7 */
  1595. my_setopt(curl, CURLOPT_COOKIESESSION, config->cookiesession?1L:0L);
  1596. my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond);
  1597. my_setopt(curl, CURLOPT_TIMEVALUE_LARGE, config->condtime);
  1598. my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
  1599. customrequest_helper(config, config->httpreq, config->customrequest);
  1600. my_setopt(curl, CURLOPT_STDERR, global->errors);
  1601. /* three new ones in libcurl 7.3: */
  1602. my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
  1603. my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
  1604. progressbarinit(&per->progressbar, config);
  1605. if((global->progressmode == CURL_PROGRESS_BAR) &&
  1606. !global->noprogress && !global->mute) {
  1607. /* we want the alternative style, then we have to implement it
  1608. ourselves! */
  1609. my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb);
  1610. my_setopt(curl, CURLOPT_XFERINFODATA, per);
  1611. }
  1612. else if(per->uploadfile && !strcmp(per->uploadfile, ".")) {
  1613. /* when reading from stdin in non-blocking mode, we use the progress
  1614. function to unpause a busy read */
  1615. my_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  1616. my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_readbusy_cb);
  1617. my_setopt(curl, CURLOPT_XFERINFODATA, per);
  1618. }
  1619. /* new in libcurl 7.24.0: */
  1620. if(config->dns_servers)
  1621. my_setopt_str(curl, CURLOPT_DNS_SERVERS, config->dns_servers);
  1622. /* new in libcurl 7.33.0: */
  1623. if(config->dns_interface)
  1624. my_setopt_str(curl, CURLOPT_DNS_INTERFACE, config->dns_interface);
  1625. if(config->dns_ipv4_addr)
  1626. my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP4, config->dns_ipv4_addr);
  1627. if(config->dns_ipv6_addr)
  1628. my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP6, config->dns_ipv6_addr);
  1629. /* new in libcurl 7.6.2: */
  1630. my_setopt_slist(curl, CURLOPT_TELNETOPTIONS, config->telnet_options);
  1631. /* new in libcurl 7.7: */
  1632. my_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
  1633. (long)(config->connecttimeout * 1000));
  1634. if(config->doh_url)
  1635. my_setopt_str(curl, CURLOPT_DOH_URL, config->doh_url);
  1636. if(config->cipher_list)
  1637. my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
  1638. if(config->proxy_cipher_list)
  1639. my_setopt_str(curl, CURLOPT_PROXY_SSL_CIPHER_LIST,
  1640. config->proxy_cipher_list);
  1641. if(config->cipher13_list)
  1642. my_setopt_str(curl, CURLOPT_TLS13_CIPHERS, config->cipher13_list);
  1643. if(config->proxy_cipher13_list)
  1644. my_setopt_str(curl, CURLOPT_PROXY_TLS13_CIPHERS,
  1645. config->proxy_cipher13_list);
  1646. /* new in libcurl 7.9.2: */
  1647. if(config->disable_epsv)
  1648. /* disable it */
  1649. my_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);
  1650. /* new in libcurl 7.10.5 */
  1651. if(config->disable_eprt)
  1652. /* disable it */
  1653. my_setopt(curl, CURLOPT_FTP_USE_EPRT, 0L);
  1654. if(global->tracetype != TRACE_NONE) {
  1655. my_setopt(curl, CURLOPT_DEBUGFUNCTION, tool_debug_cb);
  1656. my_setopt(curl, CURLOPT_DEBUGDATA, config);
  1657. my_setopt(curl, CURLOPT_VERBOSE, 1L);
  1658. }
  1659. /* new in curl 7.9.3 */
  1660. if(config->engine) {
  1661. result = res_setopt_str(curl, CURLOPT_SSLENGINE, config->engine);
  1662. if(result)
  1663. break;
  1664. }
  1665. /* new in curl 7.10.7, extended in 7.19.4. Modified to use
  1666. CREATE_DIR_RETRY in 7.49.0 */
  1667. my_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
  1668. (long)(config->ftp_create_dirs?
  1669. CURLFTP_CREATE_DIR_RETRY:
  1670. CURLFTP_CREATE_DIR_NONE));
  1671. /* new in curl 7.10.8 */
  1672. if(config->max_filesize)
  1673. my_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
  1674. config->max_filesize);
  1675. my_setopt(curl, CURLOPT_IPRESOLVE, config->ip_version);
  1676. /* new in curl 7.15.5 */
  1677. if(config->ftp_ssl_reqd)
  1678. my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
  1679. /* new in curl 7.11.0 */
  1680. else if(config->ftp_ssl)
  1681. my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
  1682. /* new in curl 7.16.0 */
  1683. else if(config->ftp_ssl_control)
  1684. my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_CONTROL);
  1685. /* new in curl 7.16.1 */
  1686. if(config->ftp_ssl_ccc)
  1687. my_setopt_enum(curl, CURLOPT_FTP_SSL_CCC,
  1688. (long)config->ftp_ssl_ccc_mode);
  1689. /* new in curl 7.19.4 */
  1690. if(config->socks5_gssapi_nec)
  1691. my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_NEC,
  1692. config->socks5_gssapi_nec);
  1693. /* new in curl 7.55.0 */
  1694. if(config->socks5_auth)
  1695. my_setopt_bitmask(curl, CURLOPT_SOCKS5_AUTH,
  1696. (long)config->socks5_auth);
  1697. /* new in curl 7.43.0 */
  1698. if(config->proxy_service_name)
  1699. my_setopt_str(curl, CURLOPT_PROXY_SERVICE_NAME,
  1700. config->proxy_service_name);
  1701. /* new in curl 7.43.0 */
  1702. if(config->service_name)
  1703. my_setopt_str(curl, CURLOPT_SERVICE_NAME,
  1704. config->service_name);
  1705. /* curl 7.13.0 */
  1706. my_setopt_str(curl, CURLOPT_FTP_ACCOUNT, config->ftp_account);
  1707. my_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, config->ignorecl?1L:0L);
  1708. /* curl 7.14.2 */
  1709. my_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, config->ftp_skip_ip?1L:0L);
  1710. /* curl 7.15.1 */
  1711. if(proto_ftp)
  1712. my_setopt(curl, CURLOPT_FTP_FILEMETHOD,
  1713. (long)config->ftp_filemethod);
  1714. /* curl 7.15.2 */
  1715. if(config->localport) {
  1716. my_setopt(curl, CURLOPT_LOCALPORT, config->localport);
  1717. my_setopt_str(curl, CURLOPT_LOCALPORTRANGE, config->localportrange);
  1718. }
  1719. /* curl 7.15.5 */
  1720. my_setopt_str(curl, CURLOPT_FTP_ALTERNATIVE_TO_USER,
  1721. config->ftp_alternative_to_user);
  1722. /* curl 7.16.0 */
  1723. if(config->disable_sessionid)
  1724. /* disable it */
  1725. my_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0L);
  1726. /* curl 7.16.2 */
  1727. if(config->raw) {
  1728. my_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 0L);
  1729. my_setopt(curl, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
  1730. }
  1731. /* curl 7.17.1 */
  1732. if(!config->nokeepalive) {
  1733. my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
  1734. if(config->alivetime) {
  1735. my_setopt(curl, CURLOPT_TCP_KEEPIDLE, config->alivetime);
  1736. my_setopt(curl, CURLOPT_TCP_KEEPINTVL, config->alivetime);
  1737. }
  1738. }
  1739. else
  1740. my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
  1741. /* curl 7.20.0 */
  1742. if(config->tftp_blksize && proto_tftp)
  1743. my_setopt(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
  1744. if(config->mail_from)
  1745. my_setopt_str(curl, CURLOPT_MAIL_FROM, config->mail_from);
  1746. if(config->mail_rcpt)
  1747. my_setopt_slist(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
  1748. /* curl 7.69.x */
  1749. my_setopt(curl, CURLOPT_MAIL_RCPT_ALLLOWFAILS,
  1750. config->mail_rcpt_allowfails ? 1L : 0L);
  1751. /* curl 7.20.x */
  1752. if(config->ftp_pret)
  1753. my_setopt(curl, CURLOPT_FTP_USE_PRET, 1L);
  1754. if(config->create_file_mode)
  1755. my_setopt(curl, CURLOPT_NEW_FILE_PERMS, config->create_file_mode);
  1756. if(config->proto_present)
  1757. my_setopt_str(curl, CURLOPT_PROTOCOLS_STR, config->proto_str);
  1758. if(config->proto_redir_present)
  1759. my_setopt_str(curl, CURLOPT_REDIR_PROTOCOLS_STR,
  1760. config->proto_redir_str);
  1761. if(config->content_disposition
  1762. && (urlnode->flags & GETOUT_USEREMOTE))
  1763. hdrcbdata->honor_cd_filename = TRUE;
  1764. else
  1765. hdrcbdata->honor_cd_filename = FALSE;
  1766. hdrcbdata->outs = outs;
  1767. hdrcbdata->heads = heads;
  1768. hdrcbdata->etag_save = etag_save;
  1769. hdrcbdata->global = global;
  1770. hdrcbdata->config = config;
  1771. my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
  1772. my_setopt(curl, CURLOPT_HEADERDATA, per);
  1773. if(config->resolve)
  1774. /* new in 7.21.3 */
  1775. my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
  1776. if(config->connect_to)
  1777. /* new in 7.49.0 */
  1778. my_setopt_slist(curl, CURLOPT_CONNECT_TO, config->connect_to);
  1779. /* new in 7.21.4 */
  1780. if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
  1781. if(config->tls_username)
  1782. my_setopt_str(curl, CURLOPT_TLSAUTH_USERNAME,
  1783. config->tls_username);
  1784. if(config->tls_password)
  1785. my_setopt_str(curl, CURLOPT_TLSAUTH_PASSWORD,
  1786. config->tls_password);
  1787. if(config->tls_authtype)
  1788. my_setopt_str(curl, CURLOPT_TLSAUTH_TYPE,
  1789. config->tls_authtype);
  1790. if(config->proxy_tls_username)
  1791. my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_USERNAME,
  1792. config->proxy_tls_username);
  1793. if(config->proxy_tls_password)
  1794. my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_PASSWORD,
  1795. config->proxy_tls_password);
  1796. if(config->proxy_tls_authtype)
  1797. my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_TYPE,
  1798. config->proxy_tls_authtype);
  1799. }
  1800. /* new in 7.22.0 */
  1801. if(config->gssapi_delegation)
  1802. my_setopt_str(curl, CURLOPT_GSSAPI_DELEGATION,
  1803. config->gssapi_delegation);
  1804. if(config->mail_auth)
  1805. my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
  1806. /* new in 7.66.0 */
  1807. if(config->sasl_authzid)
  1808. my_setopt_str(curl, CURLOPT_SASL_AUTHZID, config->sasl_authzid);
  1809. /* new in 7.31.0 */
  1810. if(config->sasl_ir)
  1811. my_setopt(curl, CURLOPT_SASL_IR, 1L);
  1812. if(config->noalpn) {
  1813. my_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L);
  1814. }
  1815. /* new in 7.40.0, abstract support added in 7.53.0 */
  1816. if(config->unix_socket_path) {
  1817. if(config->abstract_unix_socket) {
  1818. my_setopt_str(curl, CURLOPT_ABSTRACT_UNIX_SOCKET,
  1819. config->unix_socket_path);
  1820. }
  1821. else {
  1822. my_setopt_str(curl, CURLOPT_UNIX_SOCKET_PATH,
  1823. config->unix_socket_path);
  1824. }
  1825. }
  1826. /* new in 7.45.0 */
  1827. if(config->proto_default)
  1828. my_setopt_str(curl, CURLOPT_DEFAULT_PROTOCOL, config->proto_default);
  1829. /* new in 7.47.0 */
  1830. if(config->expect100timeout > 0)
  1831. my_setopt_str(curl, CURLOPT_EXPECT_100_TIMEOUT_MS,
  1832. (long)(config->expect100timeout*1000));
  1833. /* new in 7.48.0 */
  1834. if(config->tftp_no_options && proto_tftp)
  1835. my_setopt(curl, CURLOPT_TFTP_NO_OPTIONS, 1L);
  1836. /* new in 7.59.0 */
  1837. if(config->happy_eyeballs_timeout_ms != CURL_HET_DEFAULT)
  1838. my_setopt(curl, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS,
  1839. config->happy_eyeballs_timeout_ms);
  1840. /* new in 7.60.0 */
  1841. if(config->haproxy_protocol)
  1842. my_setopt(curl, CURLOPT_HAPROXYPROTOCOL, 1L);
  1843. if(config->disallow_username_in_url)
  1844. my_setopt(curl, CURLOPT_DISALLOW_USERNAME_IN_URL, 1L);
  1845. if(config->altsvc)
  1846. my_setopt_str(curl, CURLOPT_ALTSVC, config->altsvc);
  1847. if(config->hsts)
  1848. my_setopt_str(curl, CURLOPT_HSTS, config->hsts);
  1849. /* initialize retry vars for loop below */
  1850. per->retry_sleep_default = (config->retry_delay) ?
  1851. config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
  1852. per->retry_numretries = config->req_retry;
  1853. per->retry_sleep = per->retry_sleep_default; /* ms */
  1854. per->retrystart = tvnow();
  1855. state->li++;
  1856. /* Here's looping around each globbed URL */
  1857. if(state->li >= urlnum) {
  1858. state->li = 0;
  1859. state->urlnum = 0; /* forced reglob of URLs */
  1860. glob_cleanup(state->urls);
  1861. state->urls = NULL;
  1862. state->up++;
  1863. Curl_safefree(state->uploadfile); /* clear it to get the next */
  1864. }
  1865. }
  1866. else {
  1867. /* Free this URL node data without destroying the
  1868. node itself nor modifying next pointer. */
  1869. Curl_safefree(urlnode->outfile);
  1870. Curl_safefree(urlnode->infile);
  1871. urlnode->flags = 0;
  1872. glob_cleanup(state->urls);
  1873. state->urls = NULL;
  1874. state->urlnum = 0;
  1875. Curl_safefree(state->outfiles);
  1876. Curl_safefree(state->uploadfile);
  1877. if(state->inglob) {
  1878. /* Free list of globbed upload files */
  1879. glob_cleanup(state->inglob);
  1880. state->inglob = NULL;
  1881. }
  1882. config->state.urlnode = urlnode->next;
  1883. state->up = 0;
  1884. continue;
  1885. }
  1886. }
  1887. break;
  1888. }
  1889. Curl_safefree(state->outfiles);
  1890. if(!*added || result) {
  1891. *added = FALSE;
  1892. single_transfer_cleanup(config);
  1893. }
  1894. return result;
  1895. }
  1896. static long all_added; /* number of easy handles currently added */
  1897. /*
  1898. * add_parallel_transfers() sets 'morep' to TRUE if there are more transfers
  1899. * to add even after this call returns. sets 'addedp' to TRUE if one or more
  1900. * transfers were added.
  1901. */
  1902. static CURLcode add_parallel_transfers(struct GlobalConfig *global,
  1903. CURLM *multi,
  1904. CURLSH *share,
  1905. bool *morep,
  1906. bool *addedp)
  1907. {
  1908. struct per_transfer *per;
  1909. CURLcode result = CURLE_OK;
  1910. CURLMcode mcode;
  1911. bool sleeping = FALSE;
  1912. char *errorbuf;
  1913. *addedp = FALSE;
  1914. *morep = FALSE;
  1915. if(all_pers < (global->parallel_max*2)) {
  1916. result = create_transfer(global, share, addedp);
  1917. if(result)
  1918. return result;
  1919. }
  1920. for(per = transfers; per && (all_added < global->parallel_max);
  1921. per = per->next) {
  1922. bool getadded = FALSE;
  1923. if(per->added)
  1924. /* already added */
  1925. continue;
  1926. if(per->startat && (time(NULL) < per->startat)) {
  1927. /* this is still delaying */
  1928. sleeping = TRUE;
  1929. continue;
  1930. }
  1931. per->added = TRUE;
  1932. result = pre_transfer(global, per);
  1933. if(result)
  1934. return result;
  1935. errorbuf = malloc(CURL_ERROR_SIZE);
  1936. if(!errorbuf)
  1937. return CURLE_OUT_OF_MEMORY;
  1938. /* parallel connect means that we don't set PIPEWAIT since pipewait
  1939. will make libcurl prefer multiplexing */
  1940. (void)curl_easy_setopt(per->curl, CURLOPT_PIPEWAIT,
  1941. global->parallel_connect ? 0L : 1L);
  1942. (void)curl_easy_setopt(per->curl, CURLOPT_PRIVATE, per);
  1943. (void)curl_easy_setopt(per->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
  1944. (void)curl_easy_setopt(per->curl, CURLOPT_XFERINFODATA, per);
  1945. (void)curl_easy_setopt(per->curl, CURLOPT_NOPROGRESS, 0L);
  1946. mcode = curl_multi_add_handle(multi, per->curl);
  1947. if(mcode) {
  1948. DEBUGASSERT(mcode == CURLM_OUT_OF_MEMORY);
  1949. result = CURLE_OUT_OF_MEMORY;
  1950. }
  1951. if(!result)
  1952. result = create_transfer(global, share, &getadded);
  1953. if(result) {
  1954. free(errorbuf);
  1955. return result;
  1956. }
  1957. errorbuf[0] = 0;
  1958. (void)curl_easy_setopt(per->curl, CURLOPT_ERRORBUFFER, errorbuf);
  1959. per->errorbuffer = errorbuf;
  1960. per->added = TRUE;
  1961. all_added++;
  1962. *addedp = TRUE;
  1963. }
  1964. *morep = (per || sleeping) ? TRUE : FALSE;
  1965. return CURLE_OK;
  1966. }
  1967. static CURLcode parallel_transfers(struct GlobalConfig *global,
  1968. CURLSH *share)
  1969. {
  1970. CURLM *multi;
  1971. CURLMcode mcode = CURLM_OK;
  1972. CURLcode result = CURLE_OK;
  1973. int still_running = 1;
  1974. struct timeval start = tvnow();
  1975. bool more_transfers;
  1976. bool added_transfers;
  1977. /* wrapitup is set TRUE after a critical error occurs to end all transfers */
  1978. bool wrapitup = FALSE;
  1979. /* wrapitup_processed is set TRUE after the per transfer abort flag is set */
  1980. bool wrapitup_processed = FALSE;
  1981. time_t tick = time(NULL);
  1982. multi = curl_multi_init();
  1983. if(!multi)
  1984. return CURLE_OUT_OF_MEMORY;
  1985. result = add_parallel_transfers(global, multi, share,
  1986. &more_transfers, &added_transfers);
  1987. if(result) {
  1988. curl_multi_cleanup(multi);
  1989. return result;
  1990. }
  1991. while(!mcode && (still_running || more_transfers)) {
  1992. /* If stopping prematurely (eg due to a --fail-early condition) then signal
  1993. that any transfers in the multi should abort (via progress callback). */
  1994. if(wrapitup) {
  1995. if(!still_running)
  1996. break;
  1997. if(!wrapitup_processed) {
  1998. struct per_transfer *per;
  1999. for(per = transfers; per; per = per->next) {
  2000. if(per->added)
  2001. per->abort = TRUE;
  2002. }
  2003. wrapitup_processed = TRUE;
  2004. }
  2005. }
  2006. mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
  2007. if(!mcode)
  2008. mcode = curl_multi_perform(multi, &still_running);
  2009. progress_meter(global, &start, FALSE);
  2010. if(!mcode) {
  2011. int rc;
  2012. CURLMsg *msg;
  2013. bool checkmore = FALSE;
  2014. do {
  2015. msg = curl_multi_info_read(multi, &rc);
  2016. if(msg) {
  2017. bool retry;
  2018. long delay;
  2019. struct per_transfer *ended;
  2020. CURL *easy = msg->easy_handle;
  2021. CURLcode tres = msg->data.result;
  2022. curl_easy_getinfo(easy, CURLINFO_PRIVATE, (void *)&ended);
  2023. curl_multi_remove_handle(multi, easy);
  2024. if(ended->abort && tres == CURLE_ABORTED_BY_CALLBACK) {
  2025. msnprintf(ended->errorbuffer, CURL_ERROR_SIZE,
  2026. "Transfer aborted due to critical error "
  2027. "in another transfer");
  2028. }
  2029. tres = post_per_transfer(global, ended, tres, &retry, &delay);
  2030. progress_finalize(ended); /* before it goes away */
  2031. all_added--; /* one fewer added */
  2032. checkmore = TRUE;
  2033. if(retry) {
  2034. ended->added = FALSE; /* add it again */
  2035. /* we delay retries in full integer seconds only */
  2036. ended->startat = delay ? time(NULL) + delay/1000 : 0;
  2037. }
  2038. else {
  2039. /* result receives this transfer's error unless the transfer was
  2040. marked for abort due to a critical error in another transfer */
  2041. if(tres && (!ended->abort || !result))
  2042. result = tres;
  2043. if(is_fatal_error(result) || (result && global->fail_early))
  2044. wrapitup = TRUE;
  2045. (void)del_per_transfer(ended);
  2046. }
  2047. }
  2048. } while(msg);
  2049. if(wrapitup) {
  2050. if(still_running)
  2051. continue;
  2052. else
  2053. break;
  2054. }
  2055. if(!checkmore) {
  2056. time_t tock = time(NULL);
  2057. if(tick != tock) {
  2058. checkmore = TRUE;
  2059. tick = tock;
  2060. }
  2061. }
  2062. if(checkmore) {
  2063. /* one or more transfers completed, add more! */
  2064. CURLcode tres = add_parallel_transfers(global, multi, share,
  2065. &more_transfers,
  2066. &added_transfers);
  2067. if(tres)
  2068. result = tres;
  2069. if(added_transfers)
  2070. /* we added new ones, make sure the loop doesn't exit yet */
  2071. still_running = 1;
  2072. }
  2073. if(is_fatal_error(result) || (result && global->fail_early))
  2074. wrapitup = TRUE;
  2075. }
  2076. }
  2077. (void)progress_meter(global, &start, TRUE);
  2078. /* Make sure to return some kind of error if there was a multi problem */
  2079. if(mcode) {
  2080. result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
  2081. /* The other multi errors should never happen, so return
  2082. something suitably generic */
  2083. CURLE_BAD_FUNCTION_ARGUMENT;
  2084. }
  2085. curl_multi_cleanup(multi);
  2086. return result;
  2087. }
  2088. static CURLcode serial_transfers(struct GlobalConfig *global,
  2089. CURLSH *share)
  2090. {
  2091. CURLcode returncode = CURLE_OK;
  2092. CURLcode result = CURLE_OK;
  2093. struct per_transfer *per;
  2094. bool added = FALSE;
  2095. result = create_transfer(global, share, &added);
  2096. if(result)
  2097. return result;
  2098. if(!added) {
  2099. errorf(global, "no transfer performed\n");
  2100. return CURLE_READ_ERROR;
  2101. }
  2102. for(per = transfers; per;) {
  2103. bool retry;
  2104. long delay_ms;
  2105. bool bailout = FALSE;
  2106. struct timeval start;
  2107. result = pre_transfer(global, per);
  2108. if(result)
  2109. break;
  2110. if(global->libcurl) {
  2111. result = easysrc_perform();
  2112. if(result)
  2113. break;
  2114. }
  2115. start = tvnow();
  2116. #ifdef CURLDEBUG
  2117. if(global->test_event_based)
  2118. result = curl_easy_perform_ev(per->curl);
  2119. else
  2120. #endif
  2121. result = curl_easy_perform(per->curl);
  2122. returncode = post_per_transfer(global, per, result, &retry, &delay_ms);
  2123. if(retry) {
  2124. tool_go_sleep(delay_ms);
  2125. continue;
  2126. }
  2127. /* Bail out upon critical errors or --fail-early */
  2128. if(is_fatal_error(returncode) || (returncode && global->fail_early))
  2129. bailout = TRUE;
  2130. else {
  2131. /* setup the next one just before we delete this */
  2132. result = create_transfer(global, share, &added);
  2133. if(result)
  2134. bailout = TRUE;
  2135. }
  2136. per = del_per_transfer(per);
  2137. if(bailout)
  2138. break;
  2139. if(per && global->ms_per_transfer) {
  2140. /* how long time did the most recent transfer take in number of
  2141. milliseconds */
  2142. long milli = tvdiff(tvnow(), start);
  2143. if(milli < global->ms_per_transfer) {
  2144. notef(global, "Transfer took %ld ms, waits %ldms as set by --rate\n",
  2145. milli, global->ms_per_transfer - milli);
  2146. /* The transfer took less time than wanted. Wait a little. */
  2147. tool_go_sleep(global->ms_per_transfer - milli);
  2148. }
  2149. }
  2150. }
  2151. if(returncode)
  2152. /* returncode errors have priority */
  2153. result = returncode;
  2154. if(result)
  2155. single_transfer_cleanup(global->current);
  2156. return result;
  2157. }
  2158. /* setup a transfer for the given config */
  2159. static CURLcode transfer_per_config(struct GlobalConfig *global,
  2160. struct OperationConfig *config,
  2161. CURLSH *share,
  2162. bool *added)
  2163. {
  2164. CURLcode result = CURLE_OK;
  2165. bool capath_from_env;
  2166. *added = FALSE;
  2167. /* Check we have a url */
  2168. if(!config->url_list || !config->url_list->url) {
  2169. helpf(global->errors, "no URL specified!\n");
  2170. return CURLE_FAILED_INIT;
  2171. }
  2172. /* On WIN32 we can't set the path to curl-ca-bundle.crt
  2173. * at compile time. So we look here for the file in two ways:
  2174. * 1: look at the environment variable CURL_CA_BUNDLE for a path
  2175. * 2: if #1 isn't found, use the windows API function SearchPath()
  2176. * to find it along the app's path (includes app's dir and CWD)
  2177. *
  2178. * We support the environment variable thing for non-Windows platforms
  2179. * too. Just for the sake of it.
  2180. */
  2181. capath_from_env = false;
  2182. if(!config->cacert &&
  2183. !config->capath &&
  2184. (!config->insecure_ok || (config->doh_url && !config->doh_insecure_ok))) {
  2185. CURL *curltls = curl_easy_init();
  2186. struct curl_tlssessioninfo *tls_backend_info = NULL;
  2187. /* With the addition of CAINFO support for Schannel, this search could find
  2188. * a certificate bundle that was previously ignored. To maintain backward
  2189. * compatibility, only perform this search if not using Schannel.
  2190. */
  2191. result = curl_easy_getinfo(curltls, CURLINFO_TLS_SSL_PTR,
  2192. &tls_backend_info);
  2193. if(result) {
  2194. curl_easy_cleanup(curltls);
  2195. return result;
  2196. }
  2197. /* Set the CA cert locations specified in the environment. For Windows if
  2198. * no environment-specified filename is found then check for CA bundle
  2199. * default filename curl-ca-bundle.crt in the user's PATH.
  2200. *
  2201. * If Schannel is the selected SSL backend then these locations are
  2202. * ignored. We allow setting CA location for schannel only when explicitly
  2203. * specified by the user via CURLOPT_CAINFO / --cacert.
  2204. */
  2205. if(tls_backend_info->backend != CURLSSLBACKEND_SCHANNEL) {
  2206. char *env;
  2207. env = curlx_getenv("CURL_CA_BUNDLE");
  2208. if(env) {
  2209. config->cacert = strdup(env);
  2210. if(!config->cacert) {
  2211. curl_free(env);
  2212. curl_easy_cleanup(curltls);
  2213. errorf(global, "out of memory\n");
  2214. return CURLE_OUT_OF_MEMORY;
  2215. }
  2216. }
  2217. else {
  2218. env = curlx_getenv("SSL_CERT_DIR");
  2219. if(env) {
  2220. config->capath = strdup(env);
  2221. if(!config->capath) {
  2222. curl_free(env);
  2223. curl_easy_cleanup(curltls);
  2224. helpf(global->errors, "out of memory\n");
  2225. return CURLE_OUT_OF_MEMORY;
  2226. }
  2227. capath_from_env = true;
  2228. }
  2229. else {
  2230. env = curlx_getenv("SSL_CERT_FILE");
  2231. if(env) {
  2232. config->cacert = strdup(env);
  2233. if(!config->cacert) {
  2234. curl_free(env);
  2235. curl_easy_cleanup(curltls);
  2236. errorf(global, "out of memory\n");
  2237. return CURLE_OUT_OF_MEMORY;
  2238. }
  2239. }
  2240. }
  2241. }
  2242. if(env)
  2243. curl_free(env);
  2244. #ifdef WIN32
  2245. else {
  2246. result = FindWin32CACert(config, tls_backend_info->backend,
  2247. TEXT("curl-ca-bundle.crt"));
  2248. }
  2249. #endif
  2250. }
  2251. curl_easy_cleanup(curltls);
  2252. }
  2253. if(!result)
  2254. result = single_transfer(global, config, share, capath_from_env, added);
  2255. return result;
  2256. }
  2257. /*
  2258. * 'create_transfer' gets the details and sets up a new transfer if 'added'
  2259. * returns TRUE.
  2260. */
  2261. static CURLcode create_transfer(struct GlobalConfig *global,
  2262. CURLSH *share,
  2263. bool *added)
  2264. {
  2265. CURLcode result = CURLE_OK;
  2266. *added = FALSE;
  2267. while(global->current) {
  2268. result = transfer_per_config(global, global->current, share, added);
  2269. if(!result && !*added) {
  2270. /* when one set is drained, continue to next */
  2271. global->current = global->current->next;
  2272. continue;
  2273. }
  2274. break;
  2275. }
  2276. return result;
  2277. }
  2278. static CURLcode run_all_transfers(struct GlobalConfig *global,
  2279. CURLSH *share,
  2280. CURLcode result)
  2281. {
  2282. /* Save the values of noprogress and isatty to restore them later on */
  2283. bool orig_noprogress = global->noprogress;
  2284. bool orig_isatty = global->isatty;
  2285. struct per_transfer *per;
  2286. /* Time to actually do the transfers */
  2287. if(!result) {
  2288. if(global->parallel)
  2289. result = parallel_transfers(global, share);
  2290. else
  2291. result = serial_transfers(global, share);
  2292. }
  2293. /* cleanup if there are any left */
  2294. for(per = transfers; per;) {
  2295. bool retry;
  2296. long delay;
  2297. CURLcode result2 = post_per_transfer(global, per, result, &retry, &delay);
  2298. if(!result)
  2299. /* don't overwrite the original error */
  2300. result = result2;
  2301. /* Free list of given URLs */
  2302. clean_getout(per->config);
  2303. per = del_per_transfer(per);
  2304. }
  2305. /* Reset the global config variables */
  2306. global->noprogress = orig_noprogress;
  2307. global->isatty = orig_isatty;
  2308. return result;
  2309. }
  2310. CURLcode operate(struct GlobalConfig *global, int argc, argv_item_t argv[])
  2311. {
  2312. CURLcode result = CURLE_OK;
  2313. char *first_arg = argc > 1 ? curlx_convert_tchar_to_UTF8(argv[1]) : NULL;
  2314. /* Setup proper locale from environment */
  2315. #ifdef HAVE_SETLOCALE
  2316. setlocale(LC_ALL, "");
  2317. #endif
  2318. /* Parse .curlrc if necessary */
  2319. if((argc == 1) ||
  2320. (first_arg && strncmp(first_arg, "-q", 2) &&
  2321. !curl_strequal(first_arg, "--disable"))) {
  2322. parseconfig(NULL, global); /* ignore possible failure */
  2323. /* If we had no arguments then make sure a url was specified in .curlrc */
  2324. if((argc < 2) && (!global->first->url_list)) {
  2325. helpf(global->errors, NULL);
  2326. result = CURLE_FAILED_INIT;
  2327. }
  2328. }
  2329. curlx_unicodefree(first_arg);
  2330. if(!result) {
  2331. /* Parse the command line arguments */
  2332. ParameterError res = parse_args(global, argc, argv);
  2333. if(res) {
  2334. result = CURLE_OK;
  2335. /* Check if we were asked for the help */
  2336. if(res == PARAM_HELP_REQUESTED)
  2337. tool_help(global->help_category);
  2338. /* Check if we were asked for the manual */
  2339. else if(res == PARAM_MANUAL_REQUESTED)
  2340. hugehelp();
  2341. /* Check if we were asked for the version information */
  2342. else if(res == PARAM_VERSION_INFO_REQUESTED)
  2343. tool_version_info();
  2344. /* Check if we were asked to list the SSL engines */
  2345. else if(res == PARAM_ENGINES_REQUESTED)
  2346. tool_list_engines();
  2347. else if(res == PARAM_LIBCURL_UNSUPPORTED_PROTOCOL)
  2348. result = CURLE_UNSUPPORTED_PROTOCOL;
  2349. else if(res == PARAM_READ_ERROR)
  2350. result = CURLE_READ_ERROR;
  2351. else
  2352. result = CURLE_FAILED_INIT;
  2353. }
  2354. else {
  2355. if(global->libcurl) {
  2356. /* Initialise the libcurl source output */
  2357. result = easysrc_init();
  2358. }
  2359. /* Perform the main operations */
  2360. if(!result) {
  2361. size_t count = 0;
  2362. struct OperationConfig *operation = global->first;
  2363. CURLSH *share = curl_share_init();
  2364. if(!share) {
  2365. if(global->libcurl) {
  2366. /* Cleanup the libcurl source output */
  2367. easysrc_cleanup();
  2368. }
  2369. return CURLE_OUT_OF_MEMORY;
  2370. }
  2371. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  2372. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  2373. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
  2374. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  2375. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
  2376. /* Get the required arguments for each operation */
  2377. do {
  2378. result = get_args(operation, count++);
  2379. operation = operation->next;
  2380. } while(!result && operation);
  2381. /* Set the current operation pointer */
  2382. global->current = global->first;
  2383. /* now run! */
  2384. result = run_all_transfers(global, share, result);
  2385. curl_share_cleanup(share);
  2386. if(global->libcurl) {
  2387. /* Cleanup the libcurl source output */
  2388. easysrc_cleanup();
  2389. /* Dump the libcurl code if previously enabled */
  2390. dumpeasysrc(global);
  2391. }
  2392. }
  2393. else
  2394. errorf(global, "out of memory\n");
  2395. }
  2396. }
  2397. return result;
  2398. }