wget.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * wget - retrieve a file using HTTP or FTP
  4. *
  5. * Chip Rosenthal Covad Communications <chip@laserlink.net>
  6. * Licensed under GPLv2, see file LICENSE in this source tree.
  7. *
  8. * Copyright (C) 2010 Bradley M. Kuhn <bkuhn@ebb.org>
  9. * Kuhn's copyrights are licensed GPLv2-or-later. File as a whole remains GPLv2.
  10. */
  11. //usage:#define wget_trivial_usage
  12. //usage: IF_FEATURE_WGET_LONG_OPTIONS(
  13. //usage: "[-c|--continue] [-s|--spider] [-q|--quiet] [-O|--output-document FILE]\n"
  14. //usage: " [--header 'header: value'] [-Y|--proxy on/off] [-P DIR]\n"
  15. /* Since we ignore these opts, we don't show them in --help */
  16. /* //usage: " [--no-check-certificate] [--no-cache]" */
  17. //usage: " [-U|--user-agent AGENT]" IF_FEATURE_WGET_TIMEOUT(" [-T SEC]") " URL..."
  18. //usage: )
  19. //usage: IF_NOT_FEATURE_WGET_LONG_OPTIONS(
  20. //usage: "[-csq] [-O FILE] [-Y on/off] [-P DIR] [-U AGENT]"
  21. //usage: IF_FEATURE_WGET_TIMEOUT(" [-T SEC]") " URL..."
  22. //usage: )
  23. //usage:#define wget_full_usage "\n\n"
  24. //usage: "Retrieve files via HTTP or FTP\n"
  25. //usage: "\n -s Spider mode - only check file existence"
  26. //usage: "\n -c Continue retrieval of aborted transfer"
  27. //usage: "\n -q Quiet"
  28. //usage: "\n -P DIR Save to DIR (default .)"
  29. //usage: IF_FEATURE_WGET_TIMEOUT(
  30. //usage: "\n -T SEC Network read timeout is SEC seconds"
  31. //usage: )
  32. //usage: "\n -O FILE Save to FILE ('-' for stdout)"
  33. //usage: "\n -U STR Use STR for User-Agent header"
  34. //usage: "\n -Y Use proxy ('on' or 'off')"
  35. #include "libbb.h"
  36. #if 0
  37. # define log_io(...) bb_error_msg(__VA_ARGS__)
  38. # define SENDFMT(fp, fmt, ...) \
  39. do { \
  40. log_io("> " fmt, ##__VA_ARGS__); \
  41. fprintf(fp, fmt, ##__VA_ARGS__); \
  42. } while (0);
  43. #else
  44. # define log_io(...) ((void)0)
  45. # define SENDFMT(fp, fmt, ...) fprintf(fp, fmt, ##__VA_ARGS__)
  46. #endif
  47. struct host_info {
  48. char *allocated;
  49. const char *path;
  50. char *user;
  51. const char *protocol;
  52. char *host;
  53. int port;
  54. };
  55. static const char P_FTP[] = "ftp";
  56. static const char P_HTTP[] = "http";
  57. static const char P_HTTPS[] = "https";
  58. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  59. /* User-specified headers prevent using our corresponding built-in headers. */
  60. enum {
  61. HDR_HOST = (1<<0),
  62. HDR_USER_AGENT = (1<<1),
  63. HDR_RANGE = (1<<2),
  64. HDR_AUTH = (1<<3) * ENABLE_FEATURE_WGET_AUTHENTICATION,
  65. HDR_PROXY_AUTH = (1<<4) * ENABLE_FEATURE_WGET_AUTHENTICATION,
  66. };
  67. static const char wget_user_headers[] ALIGN1 =
  68. "Host:\0"
  69. "User-Agent:\0"
  70. "Range:\0"
  71. # if ENABLE_FEATURE_WGET_AUTHENTICATION
  72. "Authorization:\0"
  73. "Proxy-Authorization:\0"
  74. # endif
  75. ;
  76. # define USR_HEADER_HOST (G.user_headers & HDR_HOST)
  77. # define USR_HEADER_USER_AGENT (G.user_headers & HDR_USER_AGENT)
  78. # define USR_HEADER_RANGE (G.user_headers & HDR_RANGE)
  79. # define USR_HEADER_AUTH (G.user_headers & HDR_AUTH)
  80. # define USR_HEADER_PROXY_AUTH (G.user_headers & HDR_PROXY_AUTH)
  81. #else /* No long options, no user-headers :( */
  82. # define USR_HEADER_HOST 0
  83. # define USR_HEADER_USER_AGENT 0
  84. # define USR_HEADER_RANGE 0
  85. # define USR_HEADER_AUTH 0
  86. # define USR_HEADER_PROXY_AUTH 0
  87. #endif
  88. /* Globals */
  89. struct globals {
  90. off_t content_len; /* Content-length of the file */
  91. off_t beg_range; /* Range at which continue begins */
  92. #if ENABLE_FEATURE_WGET_STATUSBAR
  93. off_t transferred; /* Number of bytes transferred so far */
  94. const char *curfile; /* Name of current file being transferred */
  95. bb_progress_t pmt;
  96. #endif
  97. char *dir_prefix;
  98. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  99. char *post_data;
  100. char *extra_headers;
  101. unsigned char user_headers; /* Headers mentioned by the user */
  102. #endif
  103. char *fname_out; /* where to direct output (-O) */
  104. const char *proxy_flag; /* Use proxies if env vars are set */
  105. const char *user_agent; /* "User-Agent" header field */
  106. #if ENABLE_FEATURE_WGET_TIMEOUT
  107. unsigned timeout_seconds;
  108. bool connecting;
  109. #endif
  110. int output_fd;
  111. int o_flags;
  112. smallint chunked; /* chunked transfer encoding */
  113. smallint got_clen; /* got content-length: from server */
  114. /* Local downloads do benefit from big buffer.
  115. * With 512 byte buffer, it was measured to be
  116. * an order of magnitude slower than with big one.
  117. */
  118. uint64_t just_to_align_next_member;
  119. char wget_buf[CONFIG_FEATURE_COPYBUF_KB*1024];
  120. } FIX_ALIASING;
  121. #define G (*ptr_to_globals)
  122. #define INIT_G() do { \
  123. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  124. } while (0)
  125. #define FINI_G() do { \
  126. FREE_PTR_TO_GLOBALS(); \
  127. } while (0)
  128. /* Must match option string! */
  129. enum {
  130. WGET_OPT_CONTINUE = (1 << 0),
  131. WGET_OPT_SPIDER = (1 << 1),
  132. WGET_OPT_QUIET = (1 << 2),
  133. WGET_OPT_OUTNAME = (1 << 3),
  134. WGET_OPT_PREFIX = (1 << 4),
  135. WGET_OPT_PROXY = (1 << 5),
  136. WGET_OPT_USER_AGENT = (1 << 6),
  137. WGET_OPT_NETWORK_READ_TIMEOUT = (1 << 7),
  138. WGET_OPT_RETRIES = (1 << 8),
  139. WGET_OPT_PASSIVE = (1 << 9),
  140. WGET_OPT_HEADER = (1 << 10) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
  141. WGET_OPT_POST_DATA = (1 << 11) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
  142. };
  143. enum {
  144. PROGRESS_START = -1,
  145. PROGRESS_END = 0,
  146. PROGRESS_BUMP = 1,
  147. };
  148. #if ENABLE_FEATURE_WGET_STATUSBAR
  149. static void progress_meter(int flag)
  150. {
  151. if (option_mask32 & WGET_OPT_QUIET)
  152. return;
  153. if (flag == PROGRESS_START)
  154. bb_progress_init(&G.pmt, G.curfile);
  155. bb_progress_update(&G.pmt,
  156. G.beg_range,
  157. G.transferred,
  158. (G.chunked || !G.got_clen) ? 0 : G.beg_range + G.transferred + G.content_len
  159. );
  160. if (flag == PROGRESS_END) {
  161. bb_progress_free(&G.pmt);
  162. bb_putchar_stderr('\n');
  163. G.transferred = 0;
  164. }
  165. }
  166. #else
  167. static ALWAYS_INLINE void progress_meter(int flag UNUSED_PARAM) { }
  168. #endif
  169. /* IPv6 knows scoped address types i.e. link and site local addresses. Link
  170. * local addresses can have a scope identifier to specify the
  171. * interface/link an address is valid on (e.g. fe80::1%eth0). This scope
  172. * identifier is only valid on a single node.
  173. *
  174. * RFC 4007 says that the scope identifier MUST NOT be sent across the wire,
  175. * unless all nodes agree on the semantic. Apache e.g. regards zone identifiers
  176. * in the Host header as invalid requests, see
  177. * https://issues.apache.org/bugzilla/show_bug.cgi?id=35122
  178. */
  179. static void strip_ipv6_scope_id(char *host)
  180. {
  181. char *scope, *cp;
  182. /* bbox wget actually handles IPv6 addresses without [], like
  183. * wget "http://::1/xxx", but this is not standard.
  184. * To save code, _here_ we do not support it. */
  185. if (host[0] != '[')
  186. return; /* not IPv6 */
  187. scope = strchr(host, '%');
  188. if (!scope)
  189. return;
  190. /* Remove the IPv6 zone identifier from the host address */
  191. cp = strchr(host, ']');
  192. if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
  193. /* malformed address (not "[xx]:nn" or "[xx]") */
  194. return;
  195. }
  196. /* cp points to "]...", scope points to "%eth0]..." */
  197. overlapping_strcpy(scope, cp);
  198. }
  199. #if ENABLE_FEATURE_WGET_AUTHENTICATION
  200. /* Base64-encode character string. */
  201. static char *base64enc(const char *str)
  202. {
  203. unsigned len = strlen(str);
  204. if (len > sizeof(G.wget_buf)/4*3 - 10) /* paranoia */
  205. len = sizeof(G.wget_buf)/4*3 - 10;
  206. bb_uuencode(G.wget_buf, str, len, bb_uuenc_tbl_base64);
  207. return G.wget_buf;
  208. }
  209. #endif
  210. static char* sanitize_string(char *s)
  211. {
  212. unsigned char *p = (void *) s;
  213. while (*p >= ' ')
  214. p++;
  215. *p = '\0';
  216. return s;
  217. }
  218. #if ENABLE_FEATURE_WGET_TIMEOUT
  219. static void alarm_handler(int sig UNUSED_PARAM)
  220. {
  221. /* This is theoretically unsafe (uses stdio and malloc in signal handler) */
  222. if (G.connecting)
  223. bb_error_msg_and_die("download timed out");
  224. }
  225. #endif
  226. static FILE *open_socket(len_and_sockaddr *lsa)
  227. {
  228. int fd;
  229. FILE *fp;
  230. IF_FEATURE_WGET_TIMEOUT(alarm(G.timeout_seconds); G.connecting = 1;)
  231. fd = xconnect_stream(lsa);
  232. IF_FEATURE_WGET_TIMEOUT(G.connecting = 0;)
  233. /* glibc 2.4 seems to try seeking on it - ??! */
  234. /* hopefully it understands what ESPIPE means... */
  235. fp = fdopen(fd, "r+");
  236. if (!fp)
  237. bb_perror_msg_and_die(bb_msg_memory_exhausted);
  238. return fp;
  239. }
  240. /* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */
  241. /* FIXME: does not respect FEATURE_WGET_TIMEOUT and -T N: */
  242. static char fgets_and_trim(FILE *fp)
  243. {
  244. char c;
  245. char *buf_ptr;
  246. if (fgets(G.wget_buf, sizeof(G.wget_buf) - 1, fp) == NULL)
  247. bb_perror_msg_and_die("error getting response");
  248. buf_ptr = strchrnul(G.wget_buf, '\n');
  249. c = *buf_ptr;
  250. *buf_ptr = '\0';
  251. buf_ptr = strchrnul(G.wget_buf, '\r');
  252. *buf_ptr = '\0';
  253. log_io("< %s", G.wget_buf);
  254. return c;
  255. }
  256. static int ftpcmd(const char *s1, const char *s2, FILE *fp)
  257. {
  258. int result;
  259. if (s1) {
  260. if (!s2)
  261. s2 = "";
  262. fprintf(fp, "%s%s\r\n", s1, s2);
  263. fflush(fp);
  264. log_io("> %s%s", s1, s2);
  265. }
  266. do {
  267. fgets_and_trim(fp);
  268. } while (!isdigit(G.wget_buf[0]) || G.wget_buf[3] != ' ');
  269. G.wget_buf[3] = '\0';
  270. result = xatoi_positive(G.wget_buf);
  271. G.wget_buf[3] = ' ';
  272. return result;
  273. }
  274. static void parse_url(const char *src_url, struct host_info *h)
  275. {
  276. char *url, *p, *sp;
  277. free(h->allocated);
  278. h->allocated = url = xstrdup(src_url);
  279. h->protocol = P_FTP;
  280. p = strstr(url, "://");
  281. if (p) {
  282. *p = '\0';
  283. h->host = p + 3;
  284. if (strcmp(url, P_FTP) == 0) {
  285. h->port = bb_lookup_port(P_FTP, "tcp", 21);
  286. } else
  287. if (strcmp(url, P_HTTPS) == 0) {
  288. h->port = bb_lookup_port(P_HTTPS, "tcp", 443);
  289. h->protocol = P_HTTPS;
  290. } else
  291. if (strcmp(url, P_HTTP) == 0) {
  292. http:
  293. h->port = bb_lookup_port(P_HTTP, "tcp", 80);
  294. h->protocol = P_HTTP;
  295. } else {
  296. *p = ':';
  297. bb_error_msg_and_die("not an http or ftp url: %s", sanitize_string(url));
  298. }
  299. } else {
  300. // GNU wget is user-friendly and falls back to http://
  301. h->host = url;
  302. goto http;
  303. }
  304. // FYI:
  305. // "Real" wget 'http://busybox.net?var=a/b' sends this request:
  306. // 'GET /?var=a/b HTTP 1.0'
  307. // and saves 'index.html?var=a%2Fb' (we save 'b')
  308. // wget 'http://busybox.net?login=john@doe':
  309. // request: 'GET /?login=john@doe HTTP/1.0'
  310. // saves: 'index.html?login=john@doe' (we save '?login=john@doe')
  311. // wget 'http://busybox.net#test/test':
  312. // request: 'GET / HTTP/1.0'
  313. // saves: 'index.html' (we save 'test')
  314. //
  315. // We also don't add unique .N suffix if file exists...
  316. sp = strchr(h->host, '/');
  317. p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
  318. p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
  319. if (!sp) {
  320. h->path = "";
  321. } else if (*sp == '/') {
  322. *sp = '\0';
  323. h->path = sp + 1;
  324. } else { // '#' or '?'
  325. // http://busybox.net?login=john@doe is a valid URL
  326. // memmove converts to:
  327. // http:/busybox.nett?login=john@doe...
  328. memmove(h->host - 1, h->host, sp - h->host);
  329. h->host--;
  330. sp[-1] = '\0';
  331. h->path = sp;
  332. }
  333. sp = strrchr(h->host, '@');
  334. if (sp != NULL) {
  335. // URL-decode "user:password" string before base64-encoding:
  336. // wget http://test:my%20pass@example.com should send
  337. // Authorization: Basic dGVzdDpteSBwYXNz
  338. // which decodes to "test:my pass".
  339. // Standard wget and curl do this too.
  340. *sp = '\0';
  341. free(h->user);
  342. h->user = xstrdup(percent_decode_in_place(h->host, /*strict:*/ 0));
  343. h->host = sp + 1;
  344. }
  345. /* else: h->user remains NULL, or as set by original request
  346. * before redirect (if we are here after a redirect).
  347. */
  348. }
  349. static char *gethdr(FILE *fp)
  350. {
  351. char *s, *hdrval;
  352. int c;
  353. /* retrieve header line */
  354. c = fgets_and_trim(fp);
  355. /* end of the headers? */
  356. if (G.wget_buf[0] == '\0')
  357. return NULL;
  358. /* convert the header name to lower case */
  359. for (s = G.wget_buf; isalnum(*s) || *s == '-' || *s == '.' || *s == '_'; ++s) {
  360. /*
  361. * No-op for 20-3f and 60-7f. "0-9a-z-." are in these ranges.
  362. * 40-5f range ("@A-Z[\]^_") maps to 60-7f.
  363. * "A-Z" maps to "a-z".
  364. * "@[\]" can't occur in header names.
  365. * "^_" maps to "~,DEL" (which is wrong).
  366. * "^" was never seen yet, "_" was seen from web.archive.org
  367. * (x-archive-orig-x_commoncrawl_Signature: HEXSTRING).
  368. */
  369. *s |= 0x20;
  370. }
  371. /* verify we are at the end of the header name */
  372. if (*s != ':')
  373. bb_error_msg_and_die("bad header line: %s", sanitize_string(G.wget_buf));
  374. /* locate the start of the header value */
  375. *s++ = '\0';
  376. hdrval = skip_whitespace(s);
  377. if (c != '\n') {
  378. /* Rats! The buffer isn't big enough to hold the entire header value */
  379. while (c = getc(fp), c != EOF && c != '\n')
  380. continue;
  381. }
  382. return hdrval;
  383. }
  384. static void reset_beg_range_to_zero(void)
  385. {
  386. bb_error_msg("restart failed");
  387. G.beg_range = 0;
  388. xlseek(G.output_fd, 0, SEEK_SET);
  389. /* Done at the end instead: */
  390. /* ftruncate(G.output_fd, 0); */
  391. }
  392. static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_sockaddr *lsa)
  393. {
  394. FILE *sfp;
  395. char *str;
  396. int port;
  397. if (!target->user)
  398. target->user = xstrdup("anonymous:busybox@");
  399. sfp = open_socket(lsa);
  400. if (ftpcmd(NULL, NULL, sfp) != 220)
  401. bb_error_msg_and_die("%s", sanitize_string(G.wget_buf + 4));
  402. /*
  403. * Splitting username:password pair,
  404. * trying to log in
  405. */
  406. str = strchr(target->user, ':');
  407. if (str)
  408. *str++ = '\0';
  409. switch (ftpcmd("USER ", target->user, sfp)) {
  410. case 230:
  411. break;
  412. case 331:
  413. if (ftpcmd("PASS ", str, sfp) == 230)
  414. break;
  415. /* fall through (failed login) */
  416. default:
  417. bb_error_msg_and_die("ftp login: %s", sanitize_string(G.wget_buf + 4));
  418. }
  419. ftpcmd("TYPE I", NULL, sfp);
  420. /*
  421. * Querying file size
  422. */
  423. if (ftpcmd("SIZE ", target->path, sfp) == 213) {
  424. G.content_len = BB_STRTOOFF(G.wget_buf + 4, NULL, 10);
  425. if (G.content_len < 0 || errno) {
  426. bb_error_msg_and_die("SIZE value is garbage");
  427. }
  428. G.got_clen = 1;
  429. }
  430. /*
  431. * Entering passive mode
  432. */
  433. if (ftpcmd("PASV", NULL, sfp) != 227) {
  434. pasv_error:
  435. bb_error_msg_and_die("bad response to %s: %s", "PASV", sanitize_string(G.wget_buf));
  436. }
  437. // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
  438. // Server's IP is N1.N2.N3.N4 (we ignore it)
  439. // Server's port for data connection is P1*256+P2
  440. str = strrchr(G.wget_buf, ')');
  441. if (str) str[0] = '\0';
  442. str = strrchr(G.wget_buf, ',');
  443. if (!str) goto pasv_error;
  444. port = xatou_range(str+1, 0, 255);
  445. *str = '\0';
  446. str = strrchr(G.wget_buf, ',');
  447. if (!str) goto pasv_error;
  448. port += xatou_range(str+1, 0, 255) * 256;
  449. set_nport(&lsa->u.sa, htons(port));
  450. *dfpp = open_socket(lsa);
  451. if (G.beg_range != 0) {
  452. sprintf(G.wget_buf, "REST %"OFF_FMT"u", G.beg_range);
  453. if (ftpcmd(G.wget_buf, NULL, sfp) == 350)
  454. G.content_len -= G.beg_range;
  455. else
  456. reset_beg_range_to_zero();
  457. }
  458. if (ftpcmd("RETR ", target->path, sfp) > 150)
  459. bb_error_msg_and_die("bad response to %s: %s", "RETR", sanitize_string(G.wget_buf));
  460. return sfp;
  461. }
  462. static int spawn_https_helper(const char *host, unsigned port)
  463. {
  464. char *allocated = NULL;
  465. int sp[2];
  466. int pid;
  467. if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0)
  468. /* Kernel can have AF_UNIX support disabled */
  469. bb_perror_msg_and_die("socketpair");
  470. if (!strchr(host, ':'))
  471. host = allocated = xasprintf("%s:%u", host, port);
  472. pid = BB_MMU ? xfork() : xvfork();
  473. if (pid == 0) {
  474. /* Child */
  475. char *argv[6];
  476. close(sp[0]);
  477. xmove_fd(sp[1], 0);
  478. xdup2(0, 1);
  479. /*
  480. * TODO: develop a tiny ssl/tls helper (using matrixssl?),
  481. * try to exec it here before falling back to big fat openssl.
  482. */
  483. /*
  484. * openssl s_client -quiet -connect www.kernel.org:443 2>/dev/null
  485. * It prints some debug stuff on stderr, don't know how to suppress it.
  486. * Work around by dev-nulling stderr. We lose all error messages :(
  487. */
  488. xmove_fd(2, 3);
  489. xopen("/dev/null", O_RDWR);
  490. argv[0] = (char*)"openssl";
  491. argv[1] = (char*)"s_client";
  492. argv[2] = (char*)"-quiet";
  493. argv[3] = (char*)"-connect";
  494. argv[4] = (char*)host;
  495. argv[5] = NULL;
  496. BB_EXECVP(argv[0], argv);
  497. xmove_fd(3, 2);
  498. bb_perror_msg_and_die("can't execute '%s'", argv[0]);
  499. /* notreached */
  500. }
  501. /* Parent */
  502. free(allocated);
  503. close(sp[1]);
  504. return sp[0];
  505. }
  506. /* See networking/ssl_helper/README */
  507. #define SSL_HELPER 0
  508. #if SSL_HELPER
  509. static void spawn_https_helper1(int network_fd)
  510. {
  511. int sp[2];
  512. int pid;
  513. if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0)
  514. /* Kernel can have AF_UNIX support disabled */
  515. bb_perror_msg_and_die("socketpair");
  516. pid = BB_MMU ? xfork() : xvfork();
  517. if (pid == 0) {
  518. /* Child */
  519. char *argv[3];
  520. close(sp[0]);
  521. xmove_fd(sp[1], 0);
  522. xdup2(0, 1);
  523. xmove_fd(network_fd, 3);
  524. /*
  525. * A simple ssl/tls helper
  526. */
  527. argv[0] = (char*)"ssl_helper";
  528. argv[1] = (char*)"-d3";
  529. argv[2] = NULL;
  530. BB_EXECVP(argv[0], argv);
  531. bb_perror_msg_and_die("can't execute '%s'", argv[0]);
  532. /* notreached */
  533. }
  534. /* Parent */
  535. close(sp[1]);
  536. xmove_fd(sp[0], network_fd);
  537. }
  538. #endif
  539. static void NOINLINE retrieve_file_data(FILE *dfp)
  540. {
  541. #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
  542. # if ENABLE_FEATURE_WGET_TIMEOUT
  543. unsigned second_cnt = G.timeout_seconds;
  544. # endif
  545. struct pollfd polldata;
  546. polldata.fd = fileno(dfp);
  547. polldata.events = POLLIN | POLLPRI;
  548. #endif
  549. progress_meter(PROGRESS_START);
  550. if (G.chunked)
  551. goto get_clen;
  552. /* Loops only if chunked */
  553. while (1) {
  554. #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
  555. /* Must use nonblocking I/O, otherwise fread will loop
  556. * and *block* until it reads full buffer,
  557. * which messes up progress bar and/or timeout logic.
  558. * Because of nonblocking I/O, we need to dance
  559. * very carefully around EAGAIN. See explanation at
  560. * clearerr() calls.
  561. */
  562. ndelay_on(polldata.fd);
  563. #endif
  564. while (1) {
  565. int n;
  566. unsigned rdsz;
  567. #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
  568. /* fread internally uses read loop, which in our case
  569. * is usually exited when we get EAGAIN.
  570. * In this case, libc sets error marker on the stream.
  571. * Need to clear it before next fread to avoid possible
  572. * rare false positive ferror below. Rare because usually
  573. * fread gets more than zero bytes, and we don't fall
  574. * into if (n <= 0) ...
  575. */
  576. clearerr(dfp);
  577. #endif
  578. errno = 0;
  579. rdsz = sizeof(G.wget_buf);
  580. if (G.got_clen) {
  581. if (G.content_len < (off_t)sizeof(G.wget_buf)) {
  582. if ((int)G.content_len <= 0)
  583. break;
  584. rdsz = (unsigned)G.content_len;
  585. }
  586. }
  587. n = fread(G.wget_buf, 1, rdsz, dfp);
  588. if (n > 0) {
  589. xwrite(G.output_fd, G.wget_buf, n);
  590. #if ENABLE_FEATURE_WGET_STATUSBAR
  591. G.transferred += n;
  592. #endif
  593. if (G.got_clen) {
  594. G.content_len -= n;
  595. if (G.content_len == 0)
  596. break;
  597. }
  598. #if ENABLE_FEATURE_WGET_TIMEOUT
  599. second_cnt = G.timeout_seconds;
  600. #endif
  601. goto bump;
  602. }
  603. /* n <= 0.
  604. * man fread:
  605. * If error occurs, or EOF is reached, the return value
  606. * is a short item count (or zero).
  607. * fread does not distinguish between EOF and error.
  608. */
  609. if (errno != EAGAIN) {
  610. if (ferror(dfp)) {
  611. progress_meter(PROGRESS_END);
  612. bb_perror_msg_and_die(bb_msg_read_error);
  613. }
  614. break; /* EOF, not error */
  615. }
  616. #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
  617. /* It was EAGAIN. There is no data. Wait up to one second
  618. * then abort if timed out, or update the bar and try reading again.
  619. */
  620. if (safe_poll(&polldata, 1, 1000) == 0) {
  621. # if ENABLE_FEATURE_WGET_TIMEOUT
  622. if (second_cnt != 0 && --second_cnt == 0) {
  623. progress_meter(PROGRESS_END);
  624. bb_error_msg_and_die("download timed out");
  625. }
  626. # endif
  627. /* We used to loop back to poll here,
  628. * but there is no great harm in letting fread
  629. * to try reading anyway.
  630. */
  631. }
  632. #endif
  633. bump:
  634. /* Need to do it _every_ second for "stalled" indicator
  635. * to be shown properly.
  636. */
  637. progress_meter(PROGRESS_BUMP);
  638. } /* while (reading data) */
  639. #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
  640. clearerr(dfp);
  641. ndelay_off(polldata.fd); /* else fgets can get very unhappy */
  642. #endif
  643. if (!G.chunked)
  644. break;
  645. fgets_and_trim(dfp); /* Eat empty line */
  646. get_clen:
  647. fgets_and_trim(dfp);
  648. G.content_len = STRTOOFF(G.wget_buf, NULL, 16);
  649. /* FIXME: error check? */
  650. if (G.content_len == 0)
  651. break; /* all done! */
  652. G.got_clen = 1;
  653. /*
  654. * Note that fgets may result in some data being buffered in dfp.
  655. * We loop back to fread, which will retrieve this data.
  656. * Also note that code has to be arranged so that fread
  657. * is done _before_ one-second poll wait - poll doesn't know
  658. * about stdio buffering and can result in spurious one second waits!
  659. */
  660. }
  661. /* If -c failed, we restart from the beginning,
  662. * but we do not truncate file then, we do it only now, at the end.
  663. * This lets user to ^C if his 99% complete 10 GB file download
  664. * failed to restart *without* losing the almost complete file.
  665. */
  666. {
  667. off_t pos = lseek(G.output_fd, 0, SEEK_CUR);
  668. if (pos != (off_t)-1)
  669. ftruncate(G.output_fd, pos);
  670. }
  671. /* Draw full bar and free its resources */
  672. G.chunked = 0; /* makes it show 100% even for chunked download */
  673. G.got_clen = 1; /* makes it show 100% even for download of (formerly) unknown size */
  674. progress_meter(PROGRESS_END);
  675. }
  676. static void download_one_url(const char *url)
  677. {
  678. bool use_proxy; /* Use proxies if env vars are set */
  679. int redir_limit;
  680. len_and_sockaddr *lsa;
  681. FILE *sfp; /* socket to web/ftp server */
  682. FILE *dfp; /* socket to ftp server (data) */
  683. char *proxy = NULL;
  684. char *fname_out_alloc;
  685. char *redirected_path = NULL;
  686. struct host_info server;
  687. struct host_info target;
  688. server.allocated = NULL;
  689. target.allocated = NULL;
  690. server.user = NULL;
  691. target.user = NULL;
  692. parse_url(url, &target);
  693. /* Use the proxy if necessary */
  694. use_proxy = (strcmp(G.proxy_flag, "off") != 0);
  695. if (use_proxy) {
  696. proxy = getenv(target.protocol == P_FTP ? "ftp_proxy" : "http_proxy");
  697. //FIXME: what if protocol is https? Ok to use http_proxy?
  698. use_proxy = (proxy && proxy[0]);
  699. if (use_proxy)
  700. parse_url(proxy, &server);
  701. }
  702. if (!use_proxy) {
  703. server.port = target.port;
  704. if (ENABLE_FEATURE_IPV6) {
  705. //free(server.allocated); - can't be non-NULL
  706. server.host = server.allocated = xstrdup(target.host);
  707. } else {
  708. server.host = target.host;
  709. }
  710. }
  711. if (ENABLE_FEATURE_IPV6)
  712. strip_ipv6_scope_id(target.host);
  713. /* If there was no -O FILE, guess output filename */
  714. fname_out_alloc = NULL;
  715. if (!(option_mask32 & WGET_OPT_OUTNAME)) {
  716. G.fname_out = bb_get_last_path_component_nostrip(target.path);
  717. /* handle "wget http://kernel.org//" */
  718. if (G.fname_out[0] == '/' || !G.fname_out[0])
  719. G.fname_out = (char*)"index.html";
  720. /* -P DIR is considered only if there was no -O FILE */
  721. if (G.dir_prefix)
  722. G.fname_out = fname_out_alloc = concat_path_file(G.dir_prefix, G.fname_out);
  723. else {
  724. /* redirects may free target.path later, need to make a copy */
  725. G.fname_out = fname_out_alloc = xstrdup(G.fname_out);
  726. }
  727. }
  728. #if ENABLE_FEATURE_WGET_STATUSBAR
  729. G.curfile = bb_get_last_path_component_nostrip(G.fname_out);
  730. #endif
  731. /* Determine where to start transfer */
  732. G.beg_range = 0;
  733. if (option_mask32 & WGET_OPT_CONTINUE) {
  734. G.output_fd = open(G.fname_out, O_WRONLY);
  735. if (G.output_fd >= 0) {
  736. G.beg_range = xlseek(G.output_fd, 0, SEEK_END);
  737. }
  738. /* File doesn't exist. We do not create file here yet.
  739. * We are not sure it exists on remote side */
  740. }
  741. redir_limit = 5;
  742. resolve_lsa:
  743. lsa = xhost2sockaddr(server.host, server.port);
  744. if (!(option_mask32 & WGET_OPT_QUIET)) {
  745. char *s = xmalloc_sockaddr2dotted(&lsa->u.sa);
  746. fprintf(stderr, "Connecting to %s (%s)\n", server.host, s);
  747. free(s);
  748. }
  749. establish_session:
  750. /*G.content_len = 0; - redundant, got_clen = 0 is enough */
  751. G.got_clen = 0;
  752. G.chunked = 0;
  753. if (use_proxy || target.protocol != P_FTP) {
  754. /*
  755. * HTTP session
  756. */
  757. char *str;
  758. int status;
  759. /* Open socket to http(s) server */
  760. if (target.protocol == P_HTTPS) {
  761. /* openssl-based helper
  762. * Inconvenient API since we can't give it an open fd
  763. */
  764. int fd = spawn_https_helper(server.host, server.port);
  765. sfp = fdopen(fd, "r+");
  766. if (!sfp)
  767. bb_perror_msg_and_die(bb_msg_memory_exhausted);
  768. } else
  769. sfp = open_socket(lsa);
  770. #if SSL_HELPER
  771. if (target.protocol == P_HTTPS)
  772. spawn_https_helper1(fileno(sfp));
  773. #endif
  774. /* Send HTTP request */
  775. if (use_proxy) {
  776. SENDFMT(sfp, "GET %s://%s/%s HTTP/1.1\r\n",
  777. target.protocol, target.host,
  778. target.path);
  779. } else {
  780. SENDFMT(sfp, "%s /%s HTTP/1.1\r\n",
  781. (option_mask32 & WGET_OPT_POST_DATA) ? "POST" : "GET",
  782. target.path);
  783. }
  784. if (!USR_HEADER_HOST)
  785. SENDFMT(sfp, "Host: %s\r\n", target.host);
  786. if (!USR_HEADER_USER_AGENT)
  787. SENDFMT(sfp, "User-Agent: %s\r\n", G.user_agent);
  788. /* Ask server to close the connection as soon as we are done
  789. * (IOW: we do not intend to send more requests)
  790. */
  791. SENDFMT(sfp, "Connection: close\r\n");
  792. #if ENABLE_FEATURE_WGET_AUTHENTICATION
  793. if (target.user && !USR_HEADER_AUTH) {
  794. SENDFMT(sfp, "Proxy-Authorization: Basic %s\r\n"+6,
  795. base64enc(target.user));
  796. }
  797. if (use_proxy && server.user && !USR_HEADER_PROXY_AUTH) {
  798. SENDFMT(sfp, "Proxy-Authorization: Basic %s\r\n",
  799. base64enc(server.user));
  800. }
  801. #endif
  802. if (G.beg_range != 0 && !USR_HEADER_RANGE)
  803. SENDFMT(sfp, "Range: bytes=%"OFF_FMT"u-\r\n", G.beg_range);
  804. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  805. if (G.extra_headers) {
  806. log_io(G.extra_headers);
  807. fputs(G.extra_headers, sfp);
  808. }
  809. if (option_mask32 & WGET_OPT_POST_DATA) {
  810. SENDFMT(sfp,
  811. "Content-Type: application/x-www-form-urlencoded\r\n"
  812. "Content-Length: %u\r\n"
  813. "\r\n"
  814. "%s",
  815. (int) strlen(G.post_data), G.post_data
  816. );
  817. } else
  818. #endif
  819. {
  820. SENDFMT(sfp, "\r\n");
  821. }
  822. fflush(sfp);
  823. /*
  824. * Retrieve HTTP response line and check for "200" status code.
  825. */
  826. read_response:
  827. fgets_and_trim(sfp);
  828. str = G.wget_buf;
  829. str = skip_non_whitespace(str);
  830. str = skip_whitespace(str);
  831. // FIXME: no error check
  832. // xatou wouldn't work: "200 OK"
  833. status = atoi(str);
  834. switch (status) {
  835. case 0:
  836. case 100:
  837. while (gethdr(sfp) != NULL)
  838. /* eat all remaining headers */;
  839. goto read_response;
  840. case 200:
  841. /*
  842. Response 204 doesn't say "null file", it says "metadata
  843. has changed but data didn't":
  844. "10.2.5 204 No Content
  845. The server has fulfilled the request but does not need to return
  846. an entity-body, and might want to return updated metainformation.
  847. The response MAY include new or updated metainformation in the form
  848. of entity-headers, which if present SHOULD be associated with
  849. the requested variant.
  850. If the client is a user agent, it SHOULD NOT change its document
  851. view from that which caused the request to be sent. This response
  852. is primarily intended to allow input for actions to take place
  853. without causing a change to the user agent's active document view,
  854. although any new or updated metainformation SHOULD be applied
  855. to the document currently in the user agent's active view.
  856. The 204 response MUST NOT include a message-body, and thus
  857. is always terminated by the first empty line after the header fields."
  858. However, in real world it was observed that some web servers
  859. (e.g. Boa/0.94.14rc21) simply use code 204 when file size is zero.
  860. */
  861. case 204:
  862. if (G.beg_range != 0) {
  863. /* "Range:..." was not honored by the server.
  864. * Restart download from the beginning.
  865. */
  866. reset_beg_range_to_zero();
  867. }
  868. break;
  869. case 300: /* redirection */
  870. case 301:
  871. case 302:
  872. case 303:
  873. break;
  874. case 206: /* Partial Content */
  875. if (G.beg_range != 0)
  876. /* "Range:..." worked. Good. */
  877. break;
  878. /* Partial Content even though we did not ask for it??? */
  879. /* fall through */
  880. default:
  881. bb_error_msg_and_die("server returned error: %s", sanitize_string(G.wget_buf));
  882. }
  883. /*
  884. * Retrieve HTTP headers.
  885. */
  886. while ((str = gethdr(sfp)) != NULL) {
  887. static const char keywords[] ALIGN1 =
  888. "content-length\0""transfer-encoding\0""location\0";
  889. enum {
  890. KEY_content_length = 1, KEY_transfer_encoding, KEY_location
  891. };
  892. smalluint key;
  893. /* gethdr converted "FOO:" string to lowercase */
  894. /* strip trailing whitespace */
  895. char *s = strchrnul(str, '\0') - 1;
  896. while (s >= str && (*s == ' ' || *s == '\t')) {
  897. *s = '\0';
  898. s--;
  899. }
  900. key = index_in_strings(keywords, G.wget_buf) + 1;
  901. if (key == KEY_content_length) {
  902. G.content_len = BB_STRTOOFF(str, NULL, 10);
  903. if (G.content_len < 0 || errno) {
  904. bb_error_msg_and_die("content-length %s is garbage", sanitize_string(str));
  905. }
  906. G.got_clen = 1;
  907. continue;
  908. }
  909. if (key == KEY_transfer_encoding) {
  910. if (strcmp(str_tolower(str), "chunked") != 0)
  911. bb_error_msg_and_die("transfer encoding '%s' is not supported", sanitize_string(str));
  912. G.chunked = 1;
  913. }
  914. if (key == KEY_location && status >= 300) {
  915. if (--redir_limit == 0)
  916. bb_error_msg_and_die("too many redirections");
  917. fclose(sfp);
  918. if (str[0] == '/') {
  919. free(redirected_path);
  920. target.path = redirected_path = xstrdup(str+1);
  921. /* lsa stays the same: it's on the same server */
  922. } else {
  923. parse_url(str, &target);
  924. if (!use_proxy) {
  925. /* server.user remains untouched */
  926. free(server.allocated);
  927. server.allocated = NULL;
  928. server.host = target.host;
  929. /* strip_ipv6_scope_id(target.host); - no! */
  930. /* we assume remote never gives us IPv6 addr with scope id */
  931. server.port = target.port;
  932. free(lsa);
  933. goto resolve_lsa;
  934. } /* else: lsa stays the same: we use proxy */
  935. }
  936. goto establish_session;
  937. }
  938. }
  939. // if (status >= 300)
  940. // bb_error_msg_and_die("bad redirection (no Location: header from server)");
  941. /* For HTTP, data is pumped over the same connection */
  942. dfp = sfp;
  943. } else {
  944. /*
  945. * FTP session
  946. */
  947. sfp = prepare_ftp_session(&dfp, &target, lsa);
  948. }
  949. free(lsa);
  950. if (!(option_mask32 & WGET_OPT_SPIDER)) {
  951. if (G.output_fd < 0)
  952. G.output_fd = xopen(G.fname_out, G.o_flags);
  953. retrieve_file_data(dfp);
  954. if (!(option_mask32 & WGET_OPT_OUTNAME)) {
  955. xclose(G.output_fd);
  956. G.output_fd = -1;
  957. }
  958. }
  959. if (dfp != sfp) {
  960. /* It's ftp. Close data connection properly */
  961. fclose(dfp);
  962. if (ftpcmd(NULL, NULL, sfp) != 226)
  963. bb_error_msg_and_die("ftp error: %s", sanitize_string(G.wget_buf + 4));
  964. /* ftpcmd("QUIT", NULL, sfp); - why bother? */
  965. }
  966. fclose(sfp);
  967. free(server.allocated);
  968. free(target.allocated);
  969. free(server.user);
  970. free(target.user);
  971. free(fname_out_alloc);
  972. free(redirected_path);
  973. }
  974. int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  975. int wget_main(int argc UNUSED_PARAM, char **argv)
  976. {
  977. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  978. static const char wget_longopts[] ALIGN1 =
  979. /* name, has_arg, val */
  980. "continue\0" No_argument "c"
  981. //FIXME: -s isn't --spider, it's --save-headers!
  982. "spider\0" No_argument "s"
  983. "quiet\0" No_argument "q"
  984. "output-document\0" Required_argument "O"
  985. "directory-prefix\0" Required_argument "P"
  986. "proxy\0" Required_argument "Y"
  987. "user-agent\0" Required_argument "U"
  988. #if ENABLE_FEATURE_WGET_TIMEOUT
  989. "timeout\0" Required_argument "T"
  990. #endif
  991. /* Ignored: */
  992. // "tries\0" Required_argument "t"
  993. /* Ignored (we always use PASV): */
  994. "passive-ftp\0" No_argument "\xff"
  995. "header\0" Required_argument "\xfe"
  996. "post-data\0" Required_argument "\xfd"
  997. /* Ignored (we don't do ssl) */
  998. "no-check-certificate\0" No_argument "\xfc"
  999. /* Ignored (we don't support caching) */
  1000. "no-cache\0" No_argument "\xfb"
  1001. ;
  1002. #endif
  1003. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  1004. llist_t *headers_llist = NULL;
  1005. #endif
  1006. INIT_G();
  1007. #if ENABLE_FEATURE_WGET_TIMEOUT
  1008. G.timeout_seconds = 900;
  1009. signal(SIGALRM, alarm_handler);
  1010. #endif
  1011. G.proxy_flag = "on"; /* use proxies if env vars are set */
  1012. G.user_agent = "Wget"; /* "User-Agent" header field */
  1013. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  1014. applet_long_options = wget_longopts;
  1015. #endif
  1016. opt_complementary = "-1"
  1017. IF_FEATURE_WGET_TIMEOUT(":T+")
  1018. IF_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
  1019. getopt32(argv, "csqO:P:Y:U:T:" /*ignored:*/ "t:",
  1020. &G.fname_out, &G.dir_prefix,
  1021. &G.proxy_flag, &G.user_agent,
  1022. IF_FEATURE_WGET_TIMEOUT(&G.timeout_seconds) IF_NOT_FEATURE_WGET_TIMEOUT(NULL),
  1023. NULL /* -t RETRIES */
  1024. IF_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
  1025. IF_FEATURE_WGET_LONG_OPTIONS(, &G.post_data)
  1026. );
  1027. argv += optind;
  1028. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  1029. if (headers_llist) {
  1030. int size = 0;
  1031. char *hdr;
  1032. llist_t *ll = headers_llist;
  1033. while (ll) {
  1034. size += strlen(ll->data) + 2;
  1035. ll = ll->link;
  1036. }
  1037. G.extra_headers = hdr = xmalloc(size + 1);
  1038. while (headers_llist) {
  1039. int bit;
  1040. const char *words;
  1041. size = sprintf(hdr, "%s\r\n",
  1042. (char*)llist_pop(&headers_llist));
  1043. /* a bit like index_in_substrings but don't match full key */
  1044. bit = 1;
  1045. words = wget_user_headers;
  1046. while (*words) {
  1047. if (strstr(hdr, words) == hdr) {
  1048. G.user_headers |= bit;
  1049. break;
  1050. }
  1051. bit <<= 1;
  1052. words += strlen(words) + 1;
  1053. }
  1054. hdr += size;
  1055. }
  1056. }
  1057. #endif
  1058. G.output_fd = -1;
  1059. G.o_flags = O_WRONLY | O_CREAT | O_TRUNC | O_EXCL;
  1060. if (G.fname_out) { /* -O FILE ? */
  1061. if (LONE_DASH(G.fname_out)) { /* -O - ? */
  1062. G.output_fd = 1;
  1063. option_mask32 &= ~WGET_OPT_CONTINUE;
  1064. }
  1065. /* compat with wget: -O FILE can overwrite */
  1066. G.o_flags = O_WRONLY | O_CREAT | O_TRUNC;
  1067. }
  1068. while (*argv)
  1069. download_one_url(*argv++);
  1070. if (G.output_fd >= 0)
  1071. xclose(G.output_fd);
  1072. #if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_WGET_LONG_OPTIONS
  1073. free(G.extra_headers);
  1074. #endif
  1075. FINI_G();
  1076. return EXIT_SUCCESS;
  1077. }