blob: fbdbf62f61ba26252f3f622eff4602d659e4f00f [file] [log] [blame]
Eric Andersen96700832000-09-04 15:15:55 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersen79757c92001-04-05 21:45:54 +00003 * wget - retrieve a file using HTTP or FTP
Eric Andersen96700832000-09-04 15:15:55 +00004 *
Eric Andersen4e573f42000-11-14 23:29:24 +00005 * Chip Rosenthal Covad Communications <chip@laserlink.net>
Eric Andersenb520e082000-10-03 00:21:45 +00006 *
Eric Andersen96700832000-09-04 15:15:55 +00007 */
8
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +00009/* We want libc to give us xxx64 functions also */
10/* http://www.unix.org/version2/whatsnew/lfs20mar.html */
11#define _LARGEFILE64_SOURCE 1
12
Rob Landleyaf12cb32006-06-27 18:41:03 +000013#include "busybox.h"
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000014#include <getopt.h> /* for struct option */
15
Eric Andersen79757c92001-04-05 21:45:54 +000016struct host_info {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +000017 // May be used if we ever will want to free() all xstrdup()s...
18 /* char *allocated; */
Eric Andersen79757c92001-04-05 21:45:54 +000019 char *host;
20 int port;
21 char *path;
22 int is_ftp;
23 char *user;
24};
25
26static void parse_url(char *url, struct host_info *h);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000027static FILE *open_socket(struct sockaddr_in *s_in);
Eric Andersen3e6ff902001-03-09 21:24:12 +000028static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
Eric Andersen79757c92001-04-05 21:45:54 +000029static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
Eric Andersen96700832000-09-04 15:15:55 +000030
Eric Andersenb520e082000-10-03 00:21:45 +000031/* Globals (can be accessed from signal handlers */
Denis Vlasenko7039a662006-10-08 17:54:47 +000032static off_t content_len; /* Content-length of the file */
33static off_t beg_range; /* Range at which continue begins */
Denis Vlasenko9cade082006-11-21 10:43:02 +000034#if ENABLE_FEATURE_WGET_STATUSBAR
Denis Vlasenko7039a662006-10-08 17:54:47 +000035static off_t transferred; /* Number of bytes transferred so far */
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000036#endif
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000037static int chunked; /* chunked transfer encoding */
Denis Vlasenko9cade082006-11-21 10:43:02 +000038#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersen20aab262001-07-19 22:28:02 +000039static void progressmeter(int flag);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000040static char *curfile; /* Name of current file being transferred */
41static struct timeval start; /* Time a transfer started */
Rob Landley19a39402006-06-13 17:10:26 +000042enum {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000043 STALLTIME = 5 /* Seconds when xfer considered "stalled" */
Rob Landley19a39402006-06-13 17:10:26 +000044};
45#else
Rob Landleyf5fc1382006-09-15 04:08:25 +000046static void progressmeter(int flag) {}
Eric Andersenb520e082000-10-03 00:21:45 +000047#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048
Matt Kraai854125f2001-05-09 19:15:46 +000049/* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the
50 * number of elements read, and a short count if an eof or non-interrupt
51 * error is encountered. */
52static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
53{
54 size_t ret = 0;
55
56 do {
57 clearerr(stream);
58 ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream);
59 } while (ret < nmemb && ferror(stream) && errno == EINTR);
60
61 return ret;
62}
63
Matt Kraai854125f2001-05-09 19:15:46 +000064/* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM.
65 * Returns S, or NULL if an eof or non-interrupt error is encountered. */
66static char *safe_fgets(char *s, int size, FILE *stream)
67{
68 char *ret;
69
70 do {
71 clearerr(stream);
72 ret = fgets(s, size, stream);
73 } while (ret == NULL && ferror(stream) && errno == EINTR);
74
75 return ret;
76}
77
Denis Vlasenko9cade082006-11-21 10:43:02 +000078#if ENABLE_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +000079/*
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000080 * Base64-encode character string and return the string.
Eric Andersen79757c92001-04-05 21:45:54 +000081 */
Denis Vlasenko3526a132006-09-09 12:20:57 +000082static char *base64enc(unsigned char *p, char *buf, int len)
83{
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000084 bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
Rob Landley76ef08c2006-06-13 16:44:26 +000085 return buf;
Eric Andersen79757c92001-04-05 21:45:54 +000086}
87#endif
88
Eric Andersen96700832000-09-04 15:15:55 +000089int wget_main(int argc, char **argv)
90{
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000091 char buf[512];
Eric Andersen79757c92001-04-05 21:45:54 +000092 struct host_info server, target;
Eric Andersene6dc4392003-10-31 09:31:46 +000093 struct sockaddr_in s_in;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000094 int n, status;
95 int port;
96 int try = 5;
97 unsigned opt;
98 char *s;
99 char *proxy = 0;
100 char *dir_prefix = NULL;
101#if ENABLE_FEATURE_WGET_LONG_OPTIONS
102 char *extra_headers = NULL;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000103 llist_t *headers_llist = NULL;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000104#endif
Eric Andersen79757c92001-04-05 21:45:54 +0000105
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000106 /* server.allocated = target.allocated = NULL; */
107
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000108 FILE *sfp = NULL; /* socket to web/ftp server */
109 FILE *dfp = NULL; /* socket to ftp server (data) */
110 char *fname_out = NULL; /* where to direct output (-O) */
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000111 int got_clen = 0; /* got content-length: from server */
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000112 int output_fd = -1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000113 int use_proxy = 1; /* Use proxies if env vars are set */
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000114 const char *proxy_flag = "on"; /* Use proxies if env vars are set */
115 const char *user_agent = "Wget";/* Content of the "User-Agent" header field */
Eric Andersen29edd002000-12-09 16:55:35 +0000116
Eric Andersen96700832000-09-04 15:15:55 +0000117 /*
118 * Crack command line.
119 */
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000120 enum {
121 WGET_OPT_CONTINUE = 0x1,
122 WGET_OPT_QUIET = 0x2,
123 WGET_OPT_OUTNAME = 0x4,
124 WGET_OPT_PREFIX = 0x8,
125 WGET_OPT_PROXY = 0x10,
126 WGET_OPT_USER_AGENT = 0x20,
127 WGET_OPT_PASSIVE = 0x40,
128 WGET_OPT_HEADER = 0x80,
129 };
Bernhard Reutner-Fischer289e86a2006-08-20 20:01:24 +0000130#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000131 static const struct option wget_long_options[] = {
132 // name, has_arg, flag, val
133 { "continue", no_argument, NULL, 'c' },
134 { "quiet", no_argument, NULL, 'q' },
135 { "output-document", required_argument, NULL, 'O' },
136 { "directory-prefix", required_argument, NULL, 'P' },
137 { "proxy", required_argument, NULL, 'Y' },
138 { "user-agent", required_argument, NULL, 'U' },
139 { "passive-ftp", no_argument, NULL, 0xff },
140 { "header", required_argument, NULL, 0xfe },
141 { 0, 0, 0, 0 }
Denis Vlasenko601ae132006-11-28 23:37:46 +0000142 };
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000143 applet_long_options = wget_long_options;
Bernhard Reutner-Fischer8d3a6f72006-05-31 14:11:38 +0000144#endif
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000145 opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
146 opt = getopt32(argc, argv, "cqO:P:Y:U:",
147 &fname_out, &dir_prefix,
148 &proxy_flag, &user_agent
149 USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
150 );
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000151 if (strcmp(proxy_flag, "off") == 0) {
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000152 /* Use the proxy if necessary. */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000153 use_proxy = 0;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000154 }
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000155#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenko7534e082006-10-23 23:21:58 +0000156 if (headers_llist) {
157 int size = 1;
158 char *cp;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000159 llist_t *ll = headers_llist = rev_llist(headers_llist);
Denis Vlasenko7534e082006-10-23 23:21:58 +0000160 while (ll) {
161 size += strlen(ll->data) + 2;
162 ll = ll->link;
163 }
164 extra_headers = cp = xmalloc(size);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000165 while (headers_llist) {
Denis Vlasenko7534e082006-10-23 23:21:58 +0000166 cp += sprintf(cp, "%s\r\n", headers_llist->data);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000167 headers_llist = headers_llist->link;
Eric Andersen96700832000-09-04 15:15:55 +0000168 }
169 }
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000170#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000171
Eric Andersen79757c92001-04-05 21:45:54 +0000172 parse_url(argv[optind], &target);
173 server.host = target.host;
174 server.port = target.port;
175
Eric Andersen96700832000-09-04 15:15:55 +0000176 /*
Eric Andersenf3b2b522000-12-07 22:42:11 +0000177 * Use the proxy if necessary.
Eric Andersen96700832000-09-04 15:15:55 +0000178 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000179 if (use_proxy) {
Robert Griebld7760112002-05-14 23:36:45 +0000180 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000181 if (proxy && *proxy) {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000182 parse_url(proxy, &server);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000183 } else {
184 use_proxy = 0;
185 }
Robert Griebld7760112002-05-14 23:36:45 +0000186 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000187
Eric Andersen29edd002000-12-09 16:55:35 +0000188 /* Guess an output filename */
189 if (!fname_out) {
Eric Andersen751750e2004-10-08 08:27:40 +0000190 // Dirty hack. Needed because bb_get_last_path_component
191 // will destroy trailing / by storing '\0' in last byte!
Denis Vlasenko3526a132006-09-09 12:20:57 +0000192 if (*target.path && target.path[strlen(target.path)-1] != '/') {
Eric Andersen751750e2004-10-08 08:27:40 +0000193 fname_out =
Denis Vlasenko9cade082006-11-21 10:43:02 +0000194#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersen751750e2004-10-08 08:27:40 +0000195 curfile =
Eric Andersen29edd002000-12-09 16:55:35 +0000196#endif
Eric Andersen751750e2004-10-08 08:27:40 +0000197 bb_get_last_path_component(target.path);
198 }
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000199 if (!fname_out || !fname_out[0]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000200 fname_out =
Denis Vlasenko9cade082006-11-21 10:43:02 +0000201#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000202 curfile =
Eric Andersen29edd002000-12-09 16:55:35 +0000203#endif
204 "index.html";
205 }
Matt Kraai0382eb82001-07-19 19:13:55 +0000206 if (dir_prefix != NULL)
207 fname_out = concat_path_file(dir_prefix, fname_out);
Denis Vlasenko9cade082006-11-21 10:43:02 +0000208#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000209 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 curfile = bb_get_last_path_component(fname_out);
Eric Andersen29edd002000-12-09 16:55:35 +0000211#endif
212 }
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000213 /* Impossible?
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000214 if ((opt & WGET_OPT_CONTINUE) && !fname_out)
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000215 bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
Eric Andersen29edd002000-12-09 16:55:35 +0000216
Eric Andersen96700832000-09-04 15:15:55 +0000217 /*
218 * Determine where to start transfer.
219 */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000220 if (LONE_DASH(fname_out)) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000221 output_fd = 1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000222 opt &= ~WGET_OPT_CONTINUE;
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000223 }
224 if (opt & WGET_OPT_CONTINUE) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000225 output_fd = open(fname_out, O_WRONLY);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000226 if (output_fd >= 0) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000227 beg_range = xlseek(output_fd, 0, SEEK_END);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000228 }
229 /* File doesn't exist. We do not create file here yet.
230 We are not sure it exists on remove side */
Eric Andersen96700832000-09-04 15:15:55 +0000231 }
232
Eric Andersene6dc4392003-10-31 09:31:46 +0000233 /* We want to do exactly _one_ DNS lookup, since some
234 * sites (i.e. ftp.us.debian.org) use round-robin DNS
235 * and we want to connect to only one IP... */
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000236 bb_lookup_host(&s_in, server.host);
237 s_in.sin_port = server.port;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000238 if (!(opt & WGET_OPT_QUIET)) {
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000239 fprintf(stderr, "Connecting to %s[%s]:%d\n",
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000240 server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port));
Eric Andersene6dc4392003-10-31 09:31:46 +0000241 }
242
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000243 if (use_proxy || !target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000244 /*
245 * HTTP session
246 */
247 do {
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000248 got_clen = chunked = 0;
249
Denis Vlasenko3526a132006-09-09 12:20:57 +0000250 if (!--try)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000251 bb_error_msg_and_die("too many redirections");
Eric Andersen7d697012001-01-24 20:28:35 +0000252
Eric Andersen79757c92001-04-05 21:45:54 +0000253 /*
254 * Open socket to http server
255 */
256 if (sfp) fclose(sfp);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000257 sfp = open_socket(&s_in);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000258
Eric Andersen79757c92001-04-05 21:45:54 +0000259 /*
260 * Send HTTP request.
261 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000262 if (use_proxy) {
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000263 const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
Denis Vlasenko9cade082006-11-21 10:43:02 +0000264#if ENABLE_FEATURE_WGET_IP6_LITERAL
Rob Landley19a39402006-06-13 17:10:26 +0000265 if (strchr(target.host, ':'))
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000266 format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
267#endif
268 fprintf(sfp, format,
Eric Andersen79757c92001-04-05 21:45:54 +0000269 target.is_ftp ? "f" : "ht", target.host,
Glenn L McGrath24cb17f2004-01-31 08:08:57 +0000270 ntohs(target.port), target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000271 } else {
Eric Andersen6d7fa432001-04-10 18:17:05 +0000272 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000273 }
Eric Andersen96700832000-09-04 15:15:55 +0000274
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000275 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n", target.host,
276 user_agent);
Eric Andersen79757c92001-04-05 21:45:54 +0000277
Denis Vlasenko9cade082006-11-21 10:43:02 +0000278#if ENABLE_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +0000279 if (target.user) {
280 fprintf(sfp, "Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000281 base64enc((unsigned char*)target.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000282 }
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000283 if (use_proxy && server.user) {
Eric Andersen79757c92001-04-05 21:45:54 +0000284 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000285 base64enc((unsigned char*)server.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000286 }
287#endif
288
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000289 if (beg_range)
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000290 fprintf(sfp, "Range: bytes=%"OFF_FMT"d-\r\n", beg_range);
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000291#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenko7534e082006-10-23 23:21:58 +0000292 if (extra_headers)
293 fputs(extra_headers, sfp);
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000294#endif
Denis Vlasenko7534e082006-10-23 23:21:58 +0000295 fprintf(sfp, "Connection: close\r\n\r\n");
Eric Andersen79757c92001-04-05 21:45:54 +0000296
297 /*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000298 * Retrieve HTTP response line and check for "200" status code.
299 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000300 read_response:
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000301 if (fgets(buf, sizeof(buf), sfp) == NULL)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000302 bb_error_msg_and_die("no response from server");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000303
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000304 s = buf;
305 while (*s != '\0' && !isspace(*s)) ++s;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000306 s = skip_whitespace(s);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000307 // FIXME: no error check
308 // xatou wouldn't work: "200 OK"
309 status = atoi(s);
310 switch (status) {
311 case 0:
312 case 100:
313 while (gethdr(buf, sizeof(buf), sfp, &n) != NULL)
314 /* eat all remaining headers */;
315 goto read_response;
316 case 200:
317 break;
318 case 300: /* redirection */
319 case 301:
320 case 302:
321 case 303:
322 break;
323 case 206:
324 if (beg_range)
Eric Andersen79757c92001-04-05 21:45:54 +0000325 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000326 /*FALLTHRU*/
327 default:
Denis Vlasenko067e3f02006-11-10 23:25:53 +0000328 /* Show first line only and kill any ESC tricks */
329 buf[strcspn(buf, "\n\r\x1b")] = '\0';
330 bb_error_msg_and_die("server returned error: %s", buf);
Eric Andersen79757c92001-04-05 21:45:54 +0000331 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000332
Eric Andersen79757c92001-04-05 21:45:54 +0000333 /*
334 * Retrieve HTTP headers.
335 */
336 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
337 if (strcasecmp(buf, "content-length") == 0) {
Denis Vlasenkod686a042006-11-27 14:43:21 +0000338 content_len = BB_STRTOOFF(s, NULL, 10);
339 if (errno || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000340 bb_error_msg_and_die("content-length %s is garbage", s);
Eric Andersen24794452004-03-06 22:11:45 +0000341 }
Eric Andersen79757c92001-04-05 21:45:54 +0000342 got_clen = 1;
343 continue;
344 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000345 if (strcasecmp(buf, "transfer-encoding") == 0) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000346 if (strcasecmp(s, "chunked") != 0)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000347 bb_error_msg_and_die("server wants to do %s transfer encoding", s);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000348 chunked = got_clen = 1;
Eric Andersen6d7fa432001-04-10 18:17:05 +0000349 }
Eric Andersen79757c92001-04-05 21:45:54 +0000350 if (strcasecmp(buf, "location") == 0) {
351 if (s[0] == '/')
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000352 /* free(target.allocated); */
353 target.path = /* target.allocated = */ xstrdup(s+1);
Eric Andersen79757c92001-04-05 21:45:54 +0000354 else {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000355 parse_url(s, &target);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000356 if (use_proxy == 0) {
Eric Andersen79757c92001-04-05 21:45:54 +0000357 server.host = target.host;
358 server.port = target.port;
359 }
Glenn L McGrath58a2e0e2004-01-17 23:07:14 +0000360 bb_lookup_host(&s_in, server.host);
361 s_in.sin_port = server.port;
362 break;
Eric Andersen79757c92001-04-05 21:45:54 +0000363 }
364 }
365 }
Denis Vlasenko3469c182006-12-16 22:19:47 +0000366 } while (status >= 300);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000367
Eric Andersen79757c92001-04-05 21:45:54 +0000368 dfp = sfp;
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000369
370 } else {
371
Eric Andersen79757c92001-04-05 21:45:54 +0000372 /*
373 * FTP session
374 */
Denis Vlasenko3526a132006-09-09 12:20:57 +0000375 if (!target.user)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000376 target.user = xstrdup("anonymous:busybox@");
Eric Andersen79757c92001-04-05 21:45:54 +0000377
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000378 sfp = open_socket(&s_in);
Eric Andersen79757c92001-04-05 21:45:54 +0000379 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000380 bb_error_msg_and_die("%s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000381
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000382 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000383 * Splitting username:password pair,
384 * trying to log in
385 */
386 s = strchr(target.user, ':');
387 if (s)
388 *(s++) = '\0';
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000389 switch (ftpcmd("USER ", target.user, sfp, buf)) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000390 case 230:
391 break;
392 case 331:
393 if (ftpcmd("PASS ", s, sfp, buf) == 230)
Eric Andersenb520e082000-10-03 00:21:45 +0000394 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000395 /* FALLTHRU (failed login) */
396 default:
397 bb_error_msg_and_die("ftp login: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000398 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000399
Eric Andersen79757c92001-04-05 21:45:54 +0000400 ftpcmd("TYPE I", NULL, sfp, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000401
Eric Andersen79757c92001-04-05 21:45:54 +0000402 /*
403 * Querying file size
404 */
Rob Landleyaf12cb32006-06-27 18:41:03 +0000405 if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
Denis Vlasenkod686a042006-11-27 14:43:21 +0000406 content_len = BB_STRTOOFF(buf+4, NULL, 10);
407 if (errno || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000408 bb_error_msg_and_die("SIZE value is garbage");
Eric Andersen24794452004-03-06 22:11:45 +0000409 }
Eric Andersen96700832000-09-04 15:15:55 +0000410 got_clen = 1;
Eric Andersen96700832000-09-04 15:15:55 +0000411 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000412
Eric Andersen79757c92001-04-05 21:45:54 +0000413 /*
414 * Entering passive mode
415 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000416 if (ftpcmd("PASV", NULL, sfp, buf) != 227) {
417 pasv_error:
418 bb_error_msg_and_die("bad response to %s: %s", "PASV", buf);
419 }
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000420 // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000421 // Server's IP is N1.N2.N3.N4 (we ignore it)
422 // Server's port for data connection is P1*256+P2
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000423 s = strrchr(buf, ')');
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000424 if (s) s[0] = '\0';
Eric Andersen79757c92001-04-05 21:45:54 +0000425 s = strrchr(buf, ',');
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000426 if (!s) goto pasv_error;
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000427 port = xatou_range(s+1, 0, 255);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000428 *s = '\0';
Eric Andersen79757c92001-04-05 21:45:54 +0000429 s = strrchr(buf, ',');
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000430 if (!s) goto pasv_error;
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000431 port += xatou_range(s+1, 0, 255) * 256;
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000432 s_in.sin_port = htons(port);
433 dfp = open_socket(&s_in);
Eric Andersen79757c92001-04-05 21:45:54 +0000434
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000435 if (beg_range) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000436 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000437 if (ftpcmd(buf, NULL, sfp, buf) == 350)
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000438 content_len -= beg_range;
Eric Andersen96700832000-09-04 15:15:55 +0000439 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000440
Rob Landleyaf12cb32006-06-27 18:41:03 +0000441 if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000442 bb_error_msg_and_die("bad response to RETR: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000443 }
444
Eric Andersen79757c92001-04-05 21:45:54 +0000445
Eric Andersen96700832000-09-04 15:15:55 +0000446 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000447 * Retrieve file
Eric Andersen96700832000-09-04 15:15:55 +0000448 */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000449 if (chunked) {
450 fgets(buf, sizeof(buf), dfp);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000451 content_len = STRTOOFF(buf, NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000452 /* FIXME: error check?? */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000453 }
Rob Landley19a39402006-06-13 17:10:26 +0000454
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000455 /* Do it before progressmeter (want to have nice error message) */
456 if (output_fd < 0)
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000457 output_fd = xopen(fname_out,
458 O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000459
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000460 if (!(opt & WGET_OPT_QUIET))
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +0000461 progressmeter(-1);
Rob Landley19a39402006-06-13 17:10:26 +0000462
Mark Whitley30ac01c2001-04-17 18:13:16 +0000463 do {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000464 while (content_len > 0 || !got_clen) {
Denis Vlasenko3526a132006-09-09 12:20:57 +0000465 unsigned rdsz = sizeof(buf);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000466 if (content_len < sizeof(buf) && (chunked || got_clen))
467 rdsz = (unsigned)content_len;
Denis Vlasenko3526a132006-09-09 12:20:57 +0000468 n = safe_fread(buf, 1, rdsz, dfp);
469 if (n <= 0)
470 break;
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000471 if (full_write(output_fd, buf, n) != n) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000472 bb_perror_msg_and_die(bb_msg_write_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000473 }
Denis Vlasenko9cade082006-11-21 10:43:02 +0000474#if ENABLE_FEATURE_WGET_STATUSBAR
Rob Landley19a39402006-06-13 17:10:26 +0000475 transferred += n;
Eric Andersenb520e082000-10-03 00:21:45 +0000476#endif
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000477 if (got_clen) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000478 content_len -= n;
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000479 }
480 }
Eric Andersen79757c92001-04-05 21:45:54 +0000481
Eric Andersen6d7fa432001-04-10 18:17:05 +0000482 if (chunked) {
Matt Kraai854125f2001-05-09 19:15:46 +0000483 safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
484 safe_fgets(buf, sizeof(buf), dfp);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000485 content_len = STRTOOFF(buf, NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000486 /* FIXME: error check? */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000487 if (content_len == 0) {
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000488 chunked = 0; /* all done! */
489 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000490 }
491
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000492 if (n == 0 && ferror(dfp)) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000493 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000494 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000495 } while (chunked);
Rob Landley19a39402006-06-13 17:10:26 +0000496
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000497 if (!(opt & WGET_OPT_QUIET))
Mark Whitley30ac01c2001-04-17 18:13:16 +0000498 progressmeter(1);
Rob Landley19a39402006-06-13 17:10:26 +0000499
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000500 if ((use_proxy == 0) && target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000501 fclose(dfp);
502 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000503 bb_error_msg_and_die("ftp error: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000504 ftpcmd("QUIT", NULL, sfp, buf);
505 }
Eric Andersen79757c92001-04-05 21:45:54 +0000506 exit(EXIT_SUCCESS);
Eric Andersen96700832000-09-04 15:15:55 +0000507}
508
509
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000510static void parse_url(char *src_url, struct host_info *h)
Eric Andersen96700832000-09-04 15:15:55 +0000511{
Denis Vlasenko3469c182006-12-16 22:19:47 +0000512 char *url, *p, *sp;
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000513
514 /* h->allocated = */ url = xstrdup(src_url);
Eric Andersen96700832000-09-04 15:15:55 +0000515
Eric Andersen79757c92001-04-05 21:45:54 +0000516 if (strncmp(url, "http://", 7) == 0) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000517 h->port = bb_lookup_port("http", "tcp", 80);
Eric Andersen79757c92001-04-05 21:45:54 +0000518 h->host = url + 7;
519 h->is_ftp = 0;
520 } else if (strncmp(url, "ftp://", 6) == 0) {
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000521 h->port = bb_lookup_port("ftp", "tcp", 21);
Eric Andersen79757c92001-04-05 21:45:54 +0000522 h->host = url + 6;
523 h->is_ftp = 1;
524 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000525 bb_error_msg_and_die("not an http or ftp url: %s", url);
Eric Andersen96700832000-09-04 15:15:55 +0000526
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000527 // FYI:
528 // "Real" wget 'http://busybox.net?var=a/b' sends this request:
529 // 'GET /?var=a/b HTTP 1.0'
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000530 // and saves 'index.html?var=a%2Fb' (we save 'b')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000531 // wget 'http://busybox.net?login=john@doe':
532 // request: 'GET /?login=john@doe HTTP/1.0'
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000533 // saves: 'index.html?login=john@doe' (we save '?login=john@doe')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000534 // wget 'http://busybox.net#test/test':
535 // request: 'GET / HTTP/1.0'
536 // saves: 'index.html' (we save 'test')
537 //
538 // We also don't add unique .N suffix if file exists...
Eric Andersen79757c92001-04-05 21:45:54 +0000539 sp = strchr(h->host, '/');
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000540 p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
541 p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
542 if (!sp) {
543 h->path = "";
544 } else if (*sp == '/') {
Denis Vlasenko3469c182006-12-16 22:19:47 +0000545 *sp = '\0';
546 h->path = sp + 1;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000547 } else { // '#' or '?'
548 // http://busybox.net?login=john@doe is a valid URL
549 // memmove converts to:
550 // http:/busybox.nett?login=john@doe...
551 memmove(h->host-1, h->host, sp - h->host);
552 h->host--;
553 sp[-1] = '\0';
554 h->path = sp;
555 }
Eric Andersen79757c92001-04-05 21:45:54 +0000556
Denis Vlasenko3469c182006-12-16 22:19:47 +0000557 sp = strrchr(h->host, '@');
558 h->user = NULL;
559 if (sp != NULL) {
Eric Andersen79757c92001-04-05 21:45:54 +0000560 h->user = h->host;
Denis Vlasenko3469c182006-12-16 22:19:47 +0000561 *sp = '\0';
562 h->host = sp + 1;
563 }
Eric Andersen79757c92001-04-05 21:45:54 +0000564
Denis Vlasenko3469c182006-12-16 22:19:47 +0000565 sp = h->host;
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000566
Denis Vlasenko9cade082006-11-21 10:43:02 +0000567#if ENABLE_FEATURE_WGET_IP6_LITERAL
Denis Vlasenko3469c182006-12-16 22:19:47 +0000568 if (sp[0] == '[') {
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000569 char *ep;
570
Denis Vlasenko3469c182006-12-16 22:19:47 +0000571 ep = sp + 1;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000572 while (*ep == ':' || isxdigit(*ep))
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000573 ep++;
574 if (*ep == ']') {
575 h->host++;
576 *ep = '\0';
Denis Vlasenko3469c182006-12-16 22:19:47 +0000577 sp = ep + 1;
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000578 }
579 }
580#endif
581
Denis Vlasenko3469c182006-12-16 22:19:47 +0000582 p = strchr(sp, ':');
583 if (p != NULL) {
584 *p = '\0';
585 h->port = htons(xatou16(p + 1));
Eric Andersen79757c92001-04-05 21:45:54 +0000586 }
Eric Andersen96700832000-09-04 15:15:55 +0000587}
588
589
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000590static FILE *open_socket(struct sockaddr_in *s_in)
Eric Andersen96700832000-09-04 15:15:55 +0000591{
Eric Andersen96700832000-09-04 15:15:55 +0000592 FILE *fp;
593
Denis Vlasenko067e3f02006-11-10 23:25:53 +0000594 /* glibc 2.4 seems to try seeking on it - ??! */
595 /* hopefully it understands what ESPIPE means... */
Denis Vlasenko14579152006-10-26 01:09:46 +0000596 fp = fdopen(xconnect_tcp_v4(s_in), "r+");
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000597 if (fp == NULL)
Denis Vlasenko3526a132006-09-09 12:20:57 +0000598 bb_perror_msg_and_die("fdopen");
Eric Andersen96700832000-09-04 15:15:55 +0000599
600 return fp;
601}
602
603
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000604static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
Eric Andersen96700832000-09-04 15:15:55 +0000605{
606 char *s, *hdrval;
607 int c;
608
609 *istrunc = 0;
610
611 /* retrieve header line */
612 if (fgets(buf, bufsiz, fp) == NULL)
613 return NULL;
614
615 /* see if we are at the end of the headers */
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000616 for (s = buf; *s == '\r'; ++s)
Eric Andersen96700832000-09-04 15:15:55 +0000617 ;
618 if (s[0] == '\n')
619 return NULL;
620
621 /* convert the header name to lower case */
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000622 for (s = buf; isalnum(*s) || *s == '-'; ++s)
Eric Andersen96700832000-09-04 15:15:55 +0000623 *s = tolower(*s);
624
625 /* verify we are at the end of the header name */
626 if (*s != ':')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000627 bb_error_msg_and_die("bad header line: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000628
629 /* locate the start of the header value */
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000630 for (*s++ = '\0'; *s == ' ' || *s == '\t'; ++s)
Eric Andersen96700832000-09-04 15:15:55 +0000631 ;
632 hdrval = s;
633
634 /* locate the end of header */
635 while (*s != '\0' && *s != '\r' && *s != '\n')
636 ++s;
637
638 /* end of header found */
639 if (*s != '\0') {
640 *s = '\0';
641 return hdrval;
642 }
643
Eric Andersen5d638842000-09-14 21:46:30 +0000644 /* Rats! The buffer isn't big enough to hold the entire header value. */
Eric Andersen96700832000-09-04 15:15:55 +0000645 while (c = getc(fp), c != EOF && c != '\n')
646 ;
647 *istrunc = 1;
648 return hdrval;
649}
650
Eric Andersen79757c92001-04-05 21:45:54 +0000651static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
652{
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000653 int result;
Eric Andersen79757c92001-04-05 21:45:54 +0000654 if (s1) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000655 if (!s2) s2 = "";
Eric Andersen3f1cf452003-03-11 18:03:39 +0000656 fprintf(fp, "%s%s\r\n", s1, s2);
Eric Andersen79757c92001-04-05 21:45:54 +0000657 fflush(fp);
658 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000659
Eric Andersen79757c92001-04-05 21:45:54 +0000660 do {
Glenn L McGrath32da8852004-04-08 10:27:11 +0000661 char *buf_ptr;
662
663 if (fgets(buf, 510, fp) == NULL) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000664 bb_perror_msg_and_die("error getting response");
Glenn L McGrath32da8852004-04-08 10:27:11 +0000665 }
666 buf_ptr = strstr(buf, "\r\n");
667 if (buf_ptr) {
668 *buf_ptr = '\0';
669 }
Denis Vlasenko3526a132006-09-09 12:20:57 +0000670 } while (!isdigit(buf[0]) || buf[3] != ' ');
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000671
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000672 buf[3] = '\0';
673 result = xatoi_u(buf);
674 buf[3] = ' ';
675 return result;
Eric Andersen79757c92001-04-05 21:45:54 +0000676}
677
Denis Vlasenko9cade082006-11-21 10:43:02 +0000678#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000679/* Stuff below is from BSD rcp util.c, as added to openshh.
Eric Andersen4e573f42000-11-14 23:29:24 +0000680 * Original copyright notice is retained at the end of this file.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000681 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000682static int
Eric Andersenb520e082000-10-03 00:21:45 +0000683getttywidth(void)
684{
Denis Vlasenko9cade082006-11-21 10:43:02 +0000685 int width;
Eric Andersen8efe9672003-09-15 08:33:45 +0000686 get_terminal_width_height(0, &width, NULL);
Denis Vlasenko621204b2006-10-27 09:03:24 +0000687 return width;
Eric Andersenb520e082000-10-03 00:21:45 +0000688}
689
Eric Andersen3e6ff902001-03-09 21:24:12 +0000690static void
Eric Andersenb520e082000-10-03 00:21:45 +0000691updateprogressmeter(int ignore)
692{
693 int save_errno = errno;
694
695 progressmeter(0);
696 errno = save_errno;
697}
698
Rob Landleyc9c1a412006-07-12 19:17:55 +0000699static void alarmtimer(int iwait)
Eric Andersenb520e082000-10-03 00:21:45 +0000700{
701 struct itimerval itv;
702
Rob Landleyc9c1a412006-07-12 19:17:55 +0000703 itv.it_value.tv_sec = iwait;
Eric Andersenb520e082000-10-03 00:21:45 +0000704 itv.it_value.tv_usec = 0;
705 itv.it_interval = itv.it_value;
706 setitimer(ITIMER_REAL, &itv, NULL);
707}
708
709
Eric Andersen3e6ff902001-03-09 21:24:12 +0000710static void
Eric Andersenb520e082000-10-03 00:21:45 +0000711progressmeter(int flag)
712{
Eric Andersenb520e082000-10-03 00:21:45 +0000713 static struct timeval lastupdate;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000714 static off_t lastsize, totalsize;
Rob Landley19a39402006-06-13 17:10:26 +0000715
Rob Landleyc9c1a412006-07-12 19:17:55 +0000716 struct timeval now, td, tvwait;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000717 off_t abbrevsize;
Rob Landley19a39402006-06-13 17:10:26 +0000718 int elapsed, ratio, barlength, i;
Eric Andersenb520e082000-10-03 00:21:45 +0000719 char buf[256];
720
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000721 if (flag == -1) { /* first call to progressmeter */
Denis Vlasenko9cade082006-11-21 10:43:02 +0000722 gettimeofday(&start, (struct timezone *) 0);
Eric Andersenb520e082000-10-03 00:21:45 +0000723 lastupdate = start;
724 lastsize = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000725 totalsize = content_len + beg_range; /* as content_len changes.. */
Eric Andersenb520e082000-10-03 00:21:45 +0000726 }
727
Denis Vlasenko9cade082006-11-21 10:43:02 +0000728 gettimeofday(&now, (struct timezone *) 0);
Rob Landley19a39402006-06-13 17:10:26 +0000729 ratio = 100;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000730 if (totalsize != 0 && !chunked) {
Denis Vlasenko9cade082006-11-21 10:43:02 +0000731 /* long long helps to have working ETA even if !LFS */
732 ratio = (int) (100 * (unsigned long long)(transferred+beg_range) / totalsize);
Eric Andersenb520e082000-10-03 00:21:45 +0000733 ratio = MIN(ratio, 100);
Rob Landley19a39402006-06-13 17:10:26 +0000734 }
Eric Andersenb520e082000-10-03 00:21:45 +0000735
Rob Landley19a39402006-06-13 17:10:26 +0000736 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
Eric Andersenb520e082000-10-03 00:21:45 +0000737
738 barlength = getttywidth() - 51;
Rob Landley19a39402006-06-13 17:10:26 +0000739 if (barlength > 0 && barlength < sizeof(buf)) {
Eric Andersenb520e082000-10-03 00:21:45 +0000740 i = barlength * ratio / 100;
Rob Landley19a39402006-06-13 17:10:26 +0000741 memset(buf, '*', i);
742 memset(buf + i, ' ', barlength - i);
743 buf[barlength] = '\0';
744 fprintf(stderr, "|%s|", buf);
Eric Andersenb520e082000-10-03 00:21:45 +0000745 }
746 i = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000747 abbrevsize = transferred + beg_range;
Rob Landley19a39402006-06-13 17:10:26 +0000748 while (abbrevsize >= 100000) {
Eric Andersenb520e082000-10-03 00:21:45 +0000749 i++;
750 abbrevsize >>= 10;
751 }
Denis Vlasenko9cade082006-11-21 10:43:02 +0000752 /* see http://en.wikipedia.org/wiki/Tera */
Rob Landley19a39402006-06-13 17:10:26 +0000753 fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
Eric Andersenb520e082000-10-03 00:21:45 +0000754
Rob Landleyc9c1a412006-07-12 19:17:55 +0000755 timersub(&now, &lastupdate, &tvwait);
Rob Landley19a39402006-06-13 17:10:26 +0000756 if (transferred > lastsize) {
Eric Andersenb520e082000-10-03 00:21:45 +0000757 lastupdate = now;
Rob Landley19a39402006-06-13 17:10:26 +0000758 lastsize = transferred;
Rob Landleyc9c1a412006-07-12 19:17:55 +0000759 if (tvwait.tv_sec >= STALLTIME)
760 timeradd(&start, &tvwait, &start);
761 tvwait.tv_sec = 0;
Eric Andersenb520e082000-10-03 00:21:45 +0000762 }
763 timersub(&now, &start, &td);
Rob Landley19a39402006-06-13 17:10:26 +0000764 elapsed = td.tv_sec;
Eric Andersenb520e082000-10-03 00:21:45 +0000765
Rob Landleyc9c1a412006-07-12 19:17:55 +0000766 if (tvwait.tv_sec >= STALLTIME) {
Rob Landley19a39402006-06-13 17:10:26 +0000767 fprintf(stderr, " - stalled -");
Eric Andersenb520e082000-10-03 00:21:45 +0000768 } else {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000769 off_t to_download = totalsize - beg_range;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000770 if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) {
771 fprintf(stderr, "--:--:-- ETA");
772 } else {
773 /* to_download / (transferred/elapsed) - elapsed: */
Denis Vlasenko9cade082006-11-21 10:43:02 +0000774 int eta = (int) ((unsigned long long)to_download*elapsed/transferred - elapsed);
775 /* (long long helps to have working ETA even if !LFS) */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000776 i = eta % 3600;
777 fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
778 }
Eric Andersenb520e082000-10-03 00:21:45 +0000779 }
Eric Andersenb520e082000-10-03 00:21:45 +0000780
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000781 if (flag == -1) { /* first call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000782 struct sigaction sa;
783 sa.sa_handler = updateprogressmeter;
784 sigemptyset(&sa.sa_mask);
785 sa.sa_flags = SA_RESTART;
786 sigaction(SIGALRM, &sa, NULL);
787 alarmtimer(1);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000788 } else if (flag == 1) { /* last call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000789 alarmtimer(0);
Rob Landley19a39402006-06-13 17:10:26 +0000790 transferred = 0;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000791 putc('\n', stderr);
Eric Andersenb520e082000-10-03 00:21:45 +0000792 }
793}
794#endif
Eric Andersen4e573f42000-11-14 23:29:24 +0000795
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000796/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
Eric Andersenaff114c2004-04-14 17:51:38 +0000797 * much of which was blatantly stolen from openssh. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000798
Eric Andersen4e573f42000-11-14 23:29:24 +0000799/*-
800 * Copyright (c) 1992, 1993
801 * The Regents of the University of California. All rights reserved.
802 *
803 * Redistribution and use in source and binary forms, with or without
804 * modification, are permitted provided that the following conditions
805 * are met:
806 * 1. Redistributions of source code must retain the above copyright
807 * notice, this list of conditions and the following disclaimer.
808 * 2. Redistributions in binary form must reproduce the above copyright
809 * notice, this list of conditions and the following disclaimer in the
810 * documentation and/or other materials provided with the distribution.
811 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000812 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
813 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen4e573f42000-11-14 23:29:24 +0000814 *
815 * 4. Neither the name of the University nor the names of its contributors
816 * may be used to endorse or promote products derived from this software
817 * without specific prior written permission.
818 *
819 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
820 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
821 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
822 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
823 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
824 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
825 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
826 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
827 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
828 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
829 * SUCH DAMAGE.
830 *
Eric Andersen751750e2004-10-08 08:27:40 +0000831 * $Id: wget.c,v 1.75 2004/10/08 08:27:40 andersen Exp $
Eric Andersen4e573f42000-11-14 23:29:24 +0000832 */