tool_operate.c 87 KB

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