tool_operate.c 85 KB

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