blob: 1b132bea8d023fee686e76c1fb8b1e35093c8026 [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 Vlasenkocba9ef52006-10-10 21:00:47 +000034#ifdef CONFIG_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 */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000038#ifdef CONFIG_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
Eric Andersenbdfd0d72001-10-24 05:00:29 +000078#ifdef CONFIG_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
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000089#define WGET_OPT_CONTINUE 1
90#define WGET_OPT_QUIET 2
91#define WGET_OPT_PASSIVE 4
92#define WGET_OPT_OUTNAME 8
93#define WGET_OPT_HEADER 16
94#define WGET_OPT_PREFIX 32
95#define WGET_OPT_PROXY 64
96#define WGET_OPT_USER_AGENT 128
Glenn L McGrath514aeab2003-12-19 12:08:56 +000097
Bernhard Reutner-Fischer289e86a2006-08-20 20:01:24 +000098#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Glenn L McGrath514aeab2003-12-19 12:08:56 +000099static const struct option wget_long_options[] = {
100 { "continue", 0, NULL, 'c' },
101 { "quiet", 0, NULL, 'q' },
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000102 { "passive-ftp", 0, NULL, 139 }, /* FIXME: what is this - 139?? */
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000103 { "output-document", 1, NULL, 'O' },
Rob Landley76ef08c2006-06-13 16:44:26 +0000104 { "header", 1, NULL, 131 },
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000105 { "directory-prefix",1, NULL, 'P' },
106 { "proxy", 1, NULL, 'Y' },
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000107 { "user-agent", 1, NULL, 'U' },
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000108 { 0, 0, 0, 0 }
109};
Bernhard Reutner-Fischer8d3a6f72006-05-31 14:11:38 +0000110#endif
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000111
Eric Andersen96700832000-09-04 15:15:55 +0000112int wget_main(int argc, char **argv)
113{
Eric Andersen79757c92001-04-05 21:45:54 +0000114 int n, try=5, status;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000115 unsigned opt;
Eric Andersen79757c92001-04-05 21:45:54 +0000116 int port;
Robert Griebld7760112002-05-14 23:36:45 +0000117 char *proxy = 0;
Eric Andersen8071c022001-06-21 19:45:06 +0000118 char *dir_prefix=NULL;
Eric Andersen29edd002000-12-09 16:55:35 +0000119 char *s, buf[512];
Eric Andersen50ae3102001-05-15 17:51:37 +0000120 char extra_headers[1024];
121 char *extra_headers_ptr = extra_headers;
122 int extra_headers_left = sizeof(extra_headers);
Eric Andersen79757c92001-04-05 21:45:54 +0000123 struct host_info server, target;
Eric Andersene6dc4392003-10-31 09:31:46 +0000124 struct sockaddr_in s_in;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000125 llist_t *headers_llist = NULL;
Eric Andersen79757c92001-04-05 21:45:54 +0000126
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000127 /* server.allocated = target.allocated = NULL; */
128
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000129 FILE *sfp = NULL; /* socket to web/ftp server */
130 FILE *dfp = NULL; /* socket to ftp server (data) */
131 char *fname_out = NULL; /* where to direct output (-O) */
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000132 int got_clen = 0; /* got content-length: from server */
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000133 int output_fd = -1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000134 int use_proxy = 1; /* Use proxies if env vars are set */
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000135 const char *proxy_flag = "on"; /* Use proxies if env vars are set */
136 const char *user_agent = "Wget";/* Content of the "User-Agent" header field */
Eric Andersen29edd002000-12-09 16:55:35 +0000137
Eric Andersen96700832000-09-04 15:15:55 +0000138 /*
139 * Crack command line.
140 */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000141 opt_complementary = "-1:\203::";
Bernhard Reutner-Fischer289e86a2006-08-20 20:01:24 +0000142#if ENABLE_FEATURE_WGET_LONG_OPTIONS
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 Vlasenko67b23e62006-10-03 21:00:06 +0000145 opt = getopt32(argc, argv, "cq\213O:\203:P:Y:U:",
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000146 &fname_out, &headers_llist,
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000147 &dir_prefix, &proxy_flag, &user_agent);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000148 if (strcmp(proxy_flag, "off") == 0) {
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000149 /* Use the proxy if necessary. */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000150 use_proxy = 0;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000151 }
152 if (opt & WGET_OPT_HEADER) {
153 while (headers_llist) {
154 int arglen = strlen(headers_llist->data);
155 if (extra_headers_left - arglen - 2 <= 0)
Denis Vlasenko3526a132006-09-09 12:20:57 +0000156 bb_error_msg_and_die("extra_headers buffer too small "
157 "(need %i)", extra_headers_left - arglen);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000158 strcpy(extra_headers_ptr, headers_llist->data);
159 extra_headers_ptr += arglen;
160 extra_headers_left -= ( arglen + 2 );
161 *extra_headers_ptr++ = '\r';
162 *extra_headers_ptr++ = '\n';
163 *(extra_headers_ptr + 1) = 0;
164 headers_llist = headers_llist->link;
Eric Andersen96700832000-09-04 15:15:55 +0000165 }
166 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000167
Eric Andersen79757c92001-04-05 21:45:54 +0000168 parse_url(argv[optind], &target);
169 server.host = target.host;
170 server.port = target.port;
171
Eric Andersen96700832000-09-04 15:15:55 +0000172 /*
Eric Andersenf3b2b522000-12-07 22:42:11 +0000173 * Use the proxy if necessary.
Eric Andersen96700832000-09-04 15:15:55 +0000174 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000175 if (use_proxy) {
Robert Griebld7760112002-05-14 23:36:45 +0000176 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000177 if (proxy && *proxy) {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000178 parse_url(proxy, &server);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000179 } else {
180 use_proxy = 0;
181 }
Robert Griebld7760112002-05-14 23:36:45 +0000182 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000183
Eric Andersen29edd002000-12-09 16:55:35 +0000184 /* Guess an output filename */
185 if (!fname_out) {
Eric Andersen751750e2004-10-08 08:27:40 +0000186 // Dirty hack. Needed because bb_get_last_path_component
187 // will destroy trailing / by storing '\0' in last byte!
Denis Vlasenko3526a132006-09-09 12:20:57 +0000188 if (*target.path && target.path[strlen(target.path)-1] != '/') {
Eric Andersen751750e2004-10-08 08:27:40 +0000189 fname_out =
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000190#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersen751750e2004-10-08 08:27:40 +0000191 curfile =
Eric Andersen29edd002000-12-09 16:55:35 +0000192#endif
Eric Andersen751750e2004-10-08 08:27:40 +0000193 bb_get_last_path_component(target.path);
194 }
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000195 if (!fname_out || !fname_out[0]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000196 fname_out =
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000197#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000198 curfile =
Eric Andersen29edd002000-12-09 16:55:35 +0000199#endif
200 "index.html";
201 }
Matt Kraai0382eb82001-07-19 19:13:55 +0000202 if (dir_prefix != NULL)
203 fname_out = concat_path_file(dir_prefix, fname_out);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000204#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000205 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 curfile = bb_get_last_path_component(fname_out);
Eric Andersen29edd002000-12-09 16:55:35 +0000207#endif
208 }
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000209 if ((opt & WGET_OPT_CONTINUE) && !fname_out)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
Eric Andersen29edd002000-12-09 16:55:35 +0000211
Eric Andersen96700832000-09-04 15:15:55 +0000212 /*
213 * Determine where to start transfer.
214 */
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000215 if (!strcmp(fname_out, "-")) {
216 output_fd = 1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000217 opt |= WGET_OPT_QUIET;
218 opt &= ~WGET_OPT_CONTINUE;
219 } else if (opt & WGET_OPT_CONTINUE) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000220 output_fd = open(fname_out, O_WRONLY);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000221 if (output_fd >= 0) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000222 beg_range = xlseek(output_fd, 0, SEEK_END);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000223 }
224 /* File doesn't exist. We do not create file here yet.
225 We are not sure it exists on remove side */
Eric Andersen96700832000-09-04 15:15:55 +0000226 }
227
Eric Andersene6dc4392003-10-31 09:31:46 +0000228 /* We want to do exactly _one_ DNS lookup, since some
229 * sites (i.e. ftp.us.debian.org) use round-robin DNS
230 * and we want to connect to only one IP... */
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000231 bb_lookup_host(&s_in, server.host);
232 s_in.sin_port = server.port;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000233 if (!(opt & WGET_OPT_QUIET)) {
Denis Vlasenko3526a132006-09-09 12:20:57 +0000234 printf("Connecting to %s[%s]:%d\n",
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000235 server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port));
Eric Andersene6dc4392003-10-31 09:31:46 +0000236 }
237
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000238 if (use_proxy || !target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000239 /*
240 * HTTP session
241 */
242 do {
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000243 got_clen = chunked = 0;
244
Denis Vlasenko3526a132006-09-09 12:20:57 +0000245 if (!--try)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000246 bb_error_msg_and_die("too many redirections");
Eric Andersen7d697012001-01-24 20:28:35 +0000247
Eric Andersen79757c92001-04-05 21:45:54 +0000248 /*
249 * Open socket to http server
250 */
251 if (sfp) fclose(sfp);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000252 sfp = open_socket(&s_in);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000253
Eric Andersen79757c92001-04-05 21:45:54 +0000254 /*
255 * Send HTTP request.
256 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000257 if (use_proxy) {
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000258 const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
259#ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
Rob Landley19a39402006-06-13 17:10:26 +0000260 if (strchr(target.host, ':'))
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000261 format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
262#endif
263 fprintf(sfp, format,
Eric Andersen79757c92001-04-05 21:45:54 +0000264 target.is_ftp ? "f" : "ht", target.host,
Glenn L McGrath24cb17f2004-01-31 08:08:57 +0000265 ntohs(target.port), target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000266 } else {
Eric Andersen6d7fa432001-04-10 18:17:05 +0000267 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000268 }
Eric Andersen96700832000-09-04 15:15:55 +0000269
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000270 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n", target.host,
271 user_agent);
Eric Andersen79757c92001-04-05 21:45:54 +0000272
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000273#ifdef CONFIG_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +0000274 if (target.user) {
275 fprintf(sfp, "Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000276 base64enc((unsigned char*)target.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000277 }
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000278 if (use_proxy && server.user) {
Eric Andersen79757c92001-04-05 21:45:54 +0000279 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000280 base64enc((unsigned char*)server.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000281 }
282#endif
283
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000284 if (beg_range)
Denis Vlasenko7039a662006-10-08 17:54:47 +0000285 fprintf(sfp, "Range: bytes="OFF_FMT"-\r\n", beg_range);
Eric Andersen50ae3102001-05-15 17:51:37 +0000286 if(extra_headers_left < sizeof(extra_headers))
Eric Andersen9abfe852001-05-15 20:11:49 +0000287 fputs(extra_headers,sfp);
Eric Andersen79757c92001-04-05 21:45:54 +0000288 fprintf(sfp,"Connection: close\r\n\r\n");
289
290 /*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000291 * Retrieve HTTP response line and check for "200" status code.
292 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000293 read_response:
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000294 if (fgets(buf, sizeof(buf), sfp) == NULL)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000295 bb_error_msg_and_die("no response from server");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000296
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000297 s = buf;
298 while (*s != '\0' && !isspace(*s)) ++s;
299 while (isspace(*s)) ++s;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000300 // FIXME: no error check
301 // xatou wouldn't work: "200 OK"
302 status = atoi(s);
303 switch (status) {
304 case 0:
305 case 100:
306 while (gethdr(buf, sizeof(buf), sfp, &n) != NULL)
307 /* eat all remaining headers */;
308 goto read_response;
309 case 200:
310 break;
311 case 300: /* redirection */
312 case 301:
313 case 302:
314 case 303:
315 break;
316 case 206:
317 if (beg_range)
Eric Andersen79757c92001-04-05 21:45:54 +0000318 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000319 /*FALLTHRU*/
320 default:
321 chomp(buf);
322 bb_error_msg_and_die("server returned error %s: %s", s, buf);
Eric Andersen79757c92001-04-05 21:45:54 +0000323 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000324
Eric Andersen79757c92001-04-05 21:45:54 +0000325 /*
326 * Retrieve HTTP headers.
327 */
328 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
329 if (strcasecmp(buf, "content-length") == 0) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000330 if (SAFE_STRTOOFF(s, &content_len) || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000331 bb_error_msg_and_die("content-length %s is garbage", s);
Eric Andersen24794452004-03-06 22:11:45 +0000332 }
Eric Andersen79757c92001-04-05 21:45:54 +0000333 got_clen = 1;
334 continue;
335 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000336 if (strcasecmp(buf, "transfer-encoding") == 0) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000337 if (strcasecmp(s, "chunked") != 0)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000338 bb_error_msg_and_die("server wants to do %s transfer encoding", s);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000339 chunked = got_clen = 1;
Eric Andersen6d7fa432001-04-10 18:17:05 +0000340 }
Eric Andersen79757c92001-04-05 21:45:54 +0000341 if (strcasecmp(buf, "location") == 0) {
342 if (s[0] == '/')
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000343 /* free(target.allocated); */
344 target.path = /* target.allocated = */ xstrdup(s+1);
Eric Andersen79757c92001-04-05 21:45:54 +0000345 else {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000346 parse_url(s, &target);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000347 if (use_proxy == 0) {
Eric Andersen79757c92001-04-05 21:45:54 +0000348 server.host = target.host;
349 server.port = target.port;
350 }
Glenn L McGrath58a2e0e2004-01-17 23:07:14 +0000351 bb_lookup_host(&s_in, server.host);
352 s_in.sin_port = server.port;
353 break;
Eric Andersen79757c92001-04-05 21:45:54 +0000354 }
355 }
356 }
357 } while(status >= 300);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000358
Eric Andersen79757c92001-04-05 21:45:54 +0000359 dfp = sfp;
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000360
361 } else {
362
Eric Andersen79757c92001-04-05 21:45:54 +0000363 /*
364 * FTP session
365 */
Denis Vlasenko3526a132006-09-09 12:20:57 +0000366 if (!target.user)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000367 target.user = xstrdup("anonymous:busybox@");
Eric Andersen79757c92001-04-05 21:45:54 +0000368
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000369 sfp = open_socket(&s_in);
Eric Andersen79757c92001-04-05 21:45:54 +0000370 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000371 bb_error_msg_and_die("%s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000372
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000373 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000374 * Splitting username:password pair,
375 * trying to log in
376 */
377 s = strchr(target.user, ':');
378 if (s)
379 *(s++) = '\0';
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000380 switch (ftpcmd("USER ", target.user, sfp, buf)) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000381 case 230:
382 break;
383 case 331:
384 if (ftpcmd("PASS ", s, sfp, buf) == 230)
Eric Andersenb520e082000-10-03 00:21:45 +0000385 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000386 /* FALLTHRU (failed login) */
387 default:
388 bb_error_msg_and_die("ftp login: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000389 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000390
Eric Andersen79757c92001-04-05 21:45:54 +0000391 ftpcmd("TYPE I", NULL, sfp, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000392
Eric Andersen79757c92001-04-05 21:45:54 +0000393 /*
394 * Querying file size
395 */
Rob Landleyaf12cb32006-06-27 18:41:03 +0000396 if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000397 if (SAFE_STRTOOFF(buf+4, &content_len) || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000398 bb_error_msg_and_die("SIZE value is garbage");
Eric Andersen24794452004-03-06 22:11:45 +0000399 }
Eric Andersen96700832000-09-04 15:15:55 +0000400 got_clen = 1;
Eric Andersen96700832000-09-04 15:15:55 +0000401 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000402
Eric Andersen79757c92001-04-05 21:45:54 +0000403 /*
404 * Entering passive mode
405 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000406 if (ftpcmd("PASV", NULL, sfp, buf) != 227) {
407 pasv_error:
408 bb_error_msg_and_die("bad response to %s: %s", "PASV", buf);
409 }
410 // Response is "227 garbageN1,N2,N3,N4,P1,P2
411 // Server's IP is N1.N2.N3.N4 (we ignore it)
412 // Server's port for data connection is P1*256+P2
Eric Andersen79757c92001-04-05 21:45:54 +0000413 s = strrchr(buf, ',');
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000414 if (!s) goto pasv_error;
Denis Vlasenko13858992006-10-08 12:49:22 +0000415 port = xatol_range(s+1, 0, 255);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000416 *s = '\0';
Eric Andersen79757c92001-04-05 21:45:54 +0000417 s = strrchr(buf, ',');
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000418 if (!s) goto pasv_error;
Denis Vlasenko13858992006-10-08 12:49:22 +0000419 port += xatol_range(s+1, 0, 255) * 256;
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000420 s_in.sin_port = htons(port);
421 dfp = open_socket(&s_in);
Eric Andersen79757c92001-04-05 21:45:54 +0000422
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000423 if (beg_range) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000424 sprintf(buf, "REST "OFF_FMT, beg_range);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000425 if (ftpcmd(buf, NULL, sfp, buf) == 350)
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000426 content_len -= beg_range;
Eric Andersen96700832000-09-04 15:15:55 +0000427 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000428
Rob Landleyaf12cb32006-06-27 18:41:03 +0000429 if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000430 bb_error_msg_and_die("bad response to %s: %s", "RETR", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000431 }
432
Eric Andersen79757c92001-04-05 21:45:54 +0000433
Eric Andersen96700832000-09-04 15:15:55 +0000434 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000435 * Retrieve file
Eric Andersen96700832000-09-04 15:15:55 +0000436 */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000437 if (chunked) {
438 fgets(buf, sizeof(buf), dfp);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000439 content_len = STRTOOFF(buf, (char **) NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000440 /* FIXME: error check?? */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000441 }
Rob Landley19a39402006-06-13 17:10:26 +0000442
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000443 /* Do it before progressmeter (want to have nice error message) */
444 if (output_fd < 0)
445 output_fd = xopen3(fname_out,
Denis Vlasenko7039a662006-10-08 17:54:47 +0000446 O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0666);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000447
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000448 if (!(opt & WGET_OPT_QUIET))
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +0000449 progressmeter(-1);
Rob Landley19a39402006-06-13 17:10:26 +0000450
Mark Whitley30ac01c2001-04-17 18:13:16 +0000451 do {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000452 while (content_len > 0 || !got_clen) {
Denis Vlasenko3526a132006-09-09 12:20:57 +0000453 unsigned rdsz = sizeof(buf);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000454 if (content_len < sizeof(buf) && (chunked || got_clen))
455 rdsz = (unsigned)content_len;
Denis Vlasenko3526a132006-09-09 12:20:57 +0000456 n = safe_fread(buf, 1, rdsz, dfp);
457 if (n <= 0)
458 break;
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000459 if (full_write(output_fd, buf, n) != n) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000460 bb_perror_msg_and_die(bb_msg_write_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000461 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000462#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Rob Landley19a39402006-06-13 17:10:26 +0000463 transferred += n;
Eric Andersenb520e082000-10-03 00:21:45 +0000464#endif
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000465 if (got_clen) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000466 content_len -= n;
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000467 }
468 }
Eric Andersen79757c92001-04-05 21:45:54 +0000469
Eric Andersen6d7fa432001-04-10 18:17:05 +0000470 if (chunked) {
Matt Kraai854125f2001-05-09 19:15:46 +0000471 safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
472 safe_fgets(buf, sizeof(buf), dfp);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000473 content_len = STRTOOFF(buf, (char **) NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000474 /* FIXME: error check? */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000475 if (content_len == 0) {
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000476 chunked = 0; /* all done! */
477 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000478 }
479
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000480 if (n == 0 && ferror(dfp)) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000481 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000482 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000483 } while (chunked);
Rob Landley19a39402006-06-13 17:10:26 +0000484
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000485 if (!(opt & WGET_OPT_QUIET))
Mark Whitley30ac01c2001-04-17 18:13:16 +0000486 progressmeter(1);
Rob Landley19a39402006-06-13 17:10:26 +0000487
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000488 if ((use_proxy == 0) && target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000489 fclose(dfp);
490 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000491 bb_error_msg_and_die("ftp error: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000492 ftpcmd("QUIT", NULL, sfp, buf);
493 }
Eric Andersen79757c92001-04-05 21:45:54 +0000494 exit(EXIT_SUCCESS);
Eric Andersen96700832000-09-04 15:15:55 +0000495}
496
497
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000498static void parse_url(char *src_url, struct host_info *h)
Eric Andersen96700832000-09-04 15:15:55 +0000499{
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000500 char *url, *p, *cp, *sp, *up, *pp;
501
502 /* h->allocated = */ url = xstrdup(src_url);
Eric Andersen96700832000-09-04 15:15:55 +0000503
Eric Andersen79757c92001-04-05 21:45:54 +0000504 if (strncmp(url, "http://", 7) == 0) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000505 h->port = bb_lookup_port("http", "tcp", 80);
Eric Andersen79757c92001-04-05 21:45:54 +0000506 h->host = url + 7;
507 h->is_ftp = 0;
508 } else if (strncmp(url, "ftp://", 6) == 0) {
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000509 h->port = bb_lookup_port("ftp", "tcp", 21);
Eric Andersen79757c92001-04-05 21:45:54 +0000510 h->host = url + 6;
511 h->is_ftp = 1;
512 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000513 bb_error_msg_and_die("not an http or ftp url: %s", url);
Eric Andersen96700832000-09-04 15:15:55 +0000514
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000515 // FYI:
516 // "Real" wget 'http://busybox.net?var=a/b' sends this request:
517 // 'GET /?var=a/b HTTP 1.0'
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000518 // and saves 'index.html?var=a%2Fb' (we save 'b')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000519 // wget 'http://busybox.net?login=john@doe':
520 // request: 'GET /?login=john@doe HTTP/1.0'
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000521 // saves: 'index.html?login=john@doe' (we save '?login=john@doe')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000522 // wget 'http://busybox.net#test/test':
523 // request: 'GET / HTTP/1.0'
524 // saves: 'index.html' (we save 'test')
525 //
526 // We also don't add unique .N suffix if file exists...
Eric Andersen79757c92001-04-05 21:45:54 +0000527 sp = strchr(h->host, '/');
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000528 p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
529 p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
530 if (!sp) {
531 h->path = "";
532 } else if (*sp == '/') {
Matt Kraaia9711a52001-01-03 16:15:15 +0000533 *sp++ = '\0';
Eric Andersen79757c92001-04-05 21:45:54 +0000534 h->path = sp;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000535 } else { // '#' or '?'
536 // http://busybox.net?login=john@doe is a valid URL
537 // memmove converts to:
538 // http:/busybox.nett?login=john@doe...
539 memmove(h->host-1, h->host, sp - h->host);
540 h->host--;
541 sp[-1] = '\0';
542 h->path = sp;
543 }
Eric Andersen79757c92001-04-05 21:45:54 +0000544
545 up = strrchr(h->host, '@');
546 if (up != NULL) {
547 h->user = h->host;
548 *up++ = '\0';
549 h->host = up;
550 } else
551 h->user = NULL;
552
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000553 pp = h->host;
554
555#ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
556 if (h->host[0] == '[') {
557 char *ep;
558
559 ep = h->host + 1;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000560 while (*ep == ':' || isxdigit(*ep))
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000561 ep++;
562 if (*ep == ']') {
563 h->host++;
564 *ep = '\0';
565 pp = ep + 1;
566 }
567 }
568#endif
569
570 cp = strchr(pp, ':');
Eric Andersen79757c92001-04-05 21:45:54 +0000571 if (cp != NULL) {
572 *cp++ = '\0';
Denis Vlasenko13858992006-10-08 12:49:22 +0000573 h->port = htons(xatou16(cp));
Eric Andersen79757c92001-04-05 21:45:54 +0000574 }
Eric Andersen96700832000-09-04 15:15:55 +0000575}
576
577
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000578static FILE *open_socket(struct sockaddr_in *s_in)
Eric Andersen96700832000-09-04 15:15:55 +0000579{
Eric Andersen96700832000-09-04 15:15:55 +0000580 FILE *fp;
581
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000582 fp = fdopen(xconnect(s_in), "r+");
583 if (fp == NULL)
Denis Vlasenko3526a132006-09-09 12:20:57 +0000584 bb_perror_msg_and_die("fdopen");
Eric Andersen96700832000-09-04 15:15:55 +0000585
586 return fp;
587}
588
589
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000590static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
Eric Andersen96700832000-09-04 15:15:55 +0000591{
592 char *s, *hdrval;
593 int c;
594
595 *istrunc = 0;
596
597 /* retrieve header line */
598 if (fgets(buf, bufsiz, fp) == NULL)
599 return NULL;
600
601 /* see if we are at the end of the headers */
602 for (s = buf ; *s == '\r' ; ++s)
603 ;
604 if (s[0] == '\n')
605 return NULL;
606
607 /* convert the header name to lower case */
608 for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
609 *s = tolower(*s);
610
611 /* verify we are at the end of the header name */
612 if (*s != ':')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000613 bb_error_msg_and_die("bad header line: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000614
615 /* locate the start of the header value */
616 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
617 ;
618 hdrval = s;
619
620 /* locate the end of header */
621 while (*s != '\0' && *s != '\r' && *s != '\n')
622 ++s;
623
624 /* end of header found */
625 if (*s != '\0') {
626 *s = '\0';
627 return hdrval;
628 }
629
Eric Andersen5d638842000-09-14 21:46:30 +0000630 /* Rats! The buffer isn't big enough to hold the entire header value. */
Eric Andersen96700832000-09-04 15:15:55 +0000631 while (c = getc(fp), c != EOF && c != '\n')
632 ;
633 *istrunc = 1;
634 return hdrval;
635}
636
Eric Andersen79757c92001-04-05 21:45:54 +0000637static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
638{
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000639 int result;
Eric Andersen79757c92001-04-05 21:45:54 +0000640 if (s1) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000641 if (!s2) s2 = "";
Eric Andersen3f1cf452003-03-11 18:03:39 +0000642 fprintf(fp, "%s%s\r\n", s1, s2);
Eric Andersen79757c92001-04-05 21:45:54 +0000643 fflush(fp);
644 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000645
Eric Andersen79757c92001-04-05 21:45:54 +0000646 do {
Glenn L McGrath32da8852004-04-08 10:27:11 +0000647 char *buf_ptr;
648
649 if (fgets(buf, 510, fp) == NULL) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000650 bb_perror_msg_and_die("error getting response");
Glenn L McGrath32da8852004-04-08 10:27:11 +0000651 }
652 buf_ptr = strstr(buf, "\r\n");
653 if (buf_ptr) {
654 *buf_ptr = '\0';
655 }
Denis Vlasenko3526a132006-09-09 12:20:57 +0000656 } while (!isdigit(buf[0]) || buf[3] != ' ');
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000657
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000658 buf[3] = '\0';
659 result = xatoi_u(buf);
660 buf[3] = ' ';
661 return result;
Eric Andersen79757c92001-04-05 21:45:54 +0000662}
663
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000664#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000665/* Stuff below is from BSD rcp util.c, as added to openshh.
Eric Andersen4e573f42000-11-14 23:29:24 +0000666 * Original copyright notice is retained at the end of this file.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000667 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000668static int
Eric Andersenb520e082000-10-03 00:21:45 +0000669getttywidth(void)
670{
Eric Andersen8efe9672003-09-15 08:33:45 +0000671 int width=0;
672 get_terminal_width_height(0, &width, NULL);
673 return (width);
Eric Andersenb520e082000-10-03 00:21:45 +0000674}
675
Eric Andersen3e6ff902001-03-09 21:24:12 +0000676static void
Eric Andersenb520e082000-10-03 00:21:45 +0000677updateprogressmeter(int ignore)
678{
679 int save_errno = errno;
680
681 progressmeter(0);
682 errno = save_errno;
683}
684
Rob Landleyc9c1a412006-07-12 19:17:55 +0000685static void alarmtimer(int iwait)
Eric Andersenb520e082000-10-03 00:21:45 +0000686{
687 struct itimerval itv;
688
Rob Landleyc9c1a412006-07-12 19:17:55 +0000689 itv.it_value.tv_sec = iwait;
Eric Andersenb520e082000-10-03 00:21:45 +0000690 itv.it_value.tv_usec = 0;
691 itv.it_interval = itv.it_value;
692 setitimer(ITIMER_REAL, &itv, NULL);
693}
694
695
Eric Andersen3e6ff902001-03-09 21:24:12 +0000696static void
Eric Andersenb520e082000-10-03 00:21:45 +0000697progressmeter(int flag)
698{
Eric Andersenb520e082000-10-03 00:21:45 +0000699 static struct timeval lastupdate;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000700 static off_t lastsize, totalsize;
Rob Landley19a39402006-06-13 17:10:26 +0000701
Rob Landleyc9c1a412006-07-12 19:17:55 +0000702 struct timeval now, td, tvwait;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000703 off_t abbrevsize;
Rob Landley19a39402006-06-13 17:10:26 +0000704 int elapsed, ratio, barlength, i;
Eric Andersenb520e082000-10-03 00:21:45 +0000705 char buf[256];
706
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000707 if (flag == -1) { /* first call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000708 (void) gettimeofday(&start, (struct timezone *) 0);
709 lastupdate = start;
710 lastsize = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000711 totalsize = content_len + beg_range; /* as content_len changes.. */
Eric Andersenb520e082000-10-03 00:21:45 +0000712 }
713
714 (void) gettimeofday(&now, (struct timezone *) 0);
Rob Landley19a39402006-06-13 17:10:26 +0000715 ratio = 100;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000716 if (totalsize != 0 && !chunked) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000717 ratio = (int) (100 * (transferred+beg_range) / totalsize);
Eric Andersenb520e082000-10-03 00:21:45 +0000718 ratio = MIN(ratio, 100);
Rob Landley19a39402006-06-13 17:10:26 +0000719 }
Eric Andersenb520e082000-10-03 00:21:45 +0000720
Rob Landley19a39402006-06-13 17:10:26 +0000721 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
Eric Andersenb520e082000-10-03 00:21:45 +0000722
723 barlength = getttywidth() - 51;
Rob Landley19a39402006-06-13 17:10:26 +0000724 if (barlength > 0 && barlength < sizeof(buf)) {
Eric Andersenb520e082000-10-03 00:21:45 +0000725 i = barlength * ratio / 100;
Rob Landley19a39402006-06-13 17:10:26 +0000726 memset(buf, '*', i);
727 memset(buf + i, ' ', barlength - i);
728 buf[barlength] = '\0';
729 fprintf(stderr, "|%s|", buf);
Eric Andersenb520e082000-10-03 00:21:45 +0000730 }
731 i = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000732 abbrevsize = transferred + beg_range;
Rob Landley19a39402006-06-13 17:10:26 +0000733 while (abbrevsize >= 100000) {
Eric Andersenb520e082000-10-03 00:21:45 +0000734 i++;
735 abbrevsize >>= 10;
736 }
Rob Landley19a39402006-06-13 17:10:26 +0000737 /* See http://en.wikipedia.org/wiki/Tera */
738 fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
Eric Andersenb520e082000-10-03 00:21:45 +0000739
Rob Landleyc9c1a412006-07-12 19:17:55 +0000740 timersub(&now, &lastupdate, &tvwait);
Rob Landley19a39402006-06-13 17:10:26 +0000741 if (transferred > lastsize) {
Eric Andersenb520e082000-10-03 00:21:45 +0000742 lastupdate = now;
Rob Landley19a39402006-06-13 17:10:26 +0000743 lastsize = transferred;
Rob Landleyc9c1a412006-07-12 19:17:55 +0000744 if (tvwait.tv_sec >= STALLTIME)
745 timeradd(&start, &tvwait, &start);
746 tvwait.tv_sec = 0;
Eric Andersenb520e082000-10-03 00:21:45 +0000747 }
748 timersub(&now, &start, &td);
Rob Landley19a39402006-06-13 17:10:26 +0000749 elapsed = td.tv_sec;
Eric Andersenb520e082000-10-03 00:21:45 +0000750
Rob Landleyc9c1a412006-07-12 19:17:55 +0000751 if (tvwait.tv_sec >= STALLTIME) {
Rob Landley19a39402006-06-13 17:10:26 +0000752 fprintf(stderr, " - stalled -");
Eric Andersenb520e082000-10-03 00:21:45 +0000753 } else {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000754 off_t to_download = totalsize - beg_range;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000755 if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) {
756 fprintf(stderr, "--:--:-- ETA");
757 } else {
758 /* to_download / (transferred/elapsed) - elapsed: */
759 int eta = (int) (to_download*elapsed/transferred - elapsed);
760 i = eta % 3600;
761 fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
762 }
Eric Andersenb520e082000-10-03 00:21:45 +0000763 }
Eric Andersenb520e082000-10-03 00:21:45 +0000764
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000765 if (flag == -1) { /* first call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000766 struct sigaction sa;
767 sa.sa_handler = updateprogressmeter;
768 sigemptyset(&sa.sa_mask);
769 sa.sa_flags = SA_RESTART;
770 sigaction(SIGALRM, &sa, NULL);
771 alarmtimer(1);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000772 } else if (flag == 1) { /* last call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000773 alarmtimer(0);
Rob Landley19a39402006-06-13 17:10:26 +0000774 transferred = 0;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000775 putc('\n', stderr);
Eric Andersenb520e082000-10-03 00:21:45 +0000776 }
777}
778#endif
Eric Andersen4e573f42000-11-14 23:29:24 +0000779
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000780/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
Eric Andersenaff114c2004-04-14 17:51:38 +0000781 * much of which was blatantly stolen from openssh. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000782
Eric Andersen4e573f42000-11-14 23:29:24 +0000783/*-
784 * Copyright (c) 1992, 1993
785 * The Regents of the University of California. All rights reserved.
786 *
787 * Redistribution and use in source and binary forms, with or without
788 * modification, are permitted provided that the following conditions
789 * are met:
790 * 1. Redistributions of source code must retain the above copyright
791 * notice, this list of conditions and the following disclaimer.
792 * 2. Redistributions in binary form must reproduce the above copyright
793 * notice, this list of conditions and the following disclaimer in the
794 * documentation and/or other materials provided with the distribution.
795 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000796 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
797 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen4e573f42000-11-14 23:29:24 +0000798 *
799 * 4. Neither the name of the University nor the names of its contributors
800 * may be used to endorse or promote products derived from this software
801 * without specific prior written permission.
802 *
803 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
804 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
805 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
806 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
807 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
808 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
809 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
810 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
811 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
812 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
813 * SUCH DAMAGE.
814 *
Eric Andersen751750e2004-10-08 08:27:40 +0000815 * $Id: wget.c,v 1.75 2004/10/08 08:27:40 andersen Exp $
Eric Andersen4e573f42000-11-14 23:29:24 +0000816 */