blob: 39d63f11692aa05673be7b0ff1d1bd5f5a0127bb [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 */
Denis Vlasenko76791452007-06-18 08:55:57 +000011//#define _LARGEFILE64_SOURCE 1
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000012
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000013#include <getopt.h> /* for struct option */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000015
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);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000027static FILE *open_socket(len_and_sockaddr *lsa);
Denis Vlasenko06783a52007-09-24 13:51:54 +000028static char *gethdr(char *buf, size_t bufsiz, FILE *fp /*,int *istrunc*/ );
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000029static int ftpcmd(const char *s1, const 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
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +000037static bool 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 Vlasenkob6aae0f2007-01-29 22:51:25 +000040static const char *curfile; /* Name of current file being transferred */
Rob Landley19a39402006-06-13 17:10:26 +000041enum {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000042 STALLTIME = 5 /* Seconds when xfer considered "stalled" */
Rob Landley19a39402006-06-13 17:10:26 +000043};
44#else
Denis Vlasenko12d21292007-06-27 21:40:07 +000045static ALWAYS_INLINE void progressmeter(int flag) {}
Eric Andersenb520e082000-10-03 00:21:45 +000046#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000047
Denis Vlasenko12d21292007-06-27 21:40:07 +000048/* Read NMEMB bytes into PTR from STREAM. Returns the number of bytes read,
49 * and a short count if an eof or non-interrupt error is encountered. */
50static size_t safe_fread(void *ptr, size_t nmemb, FILE *stream)
Matt Kraai854125f2001-05-09 19:15:46 +000051{
Denis Vlasenko12d21292007-06-27 21:40:07 +000052 size_t ret;
53 char *p = (char*)ptr;
Matt Kraai854125f2001-05-09 19:15:46 +000054
55 do {
56 clearerr(stream);
Denis Vlasenko12d21292007-06-27 21:40:07 +000057 ret = fread(p, 1, nmemb, stream);
58 p += ret;
59 nmemb -= ret;
60 } while (nmemb && ferror(stream) && errno == EINTR);
Matt Kraai854125f2001-05-09 19:15:46 +000061
Denis Vlasenko12d21292007-06-27 21:40:07 +000062 return p - (char*)ptr;
Matt Kraai854125f2001-05-09 19:15:46 +000063}
64
Denis Vlasenko12d21292007-06-27 21:40:07 +000065/* Read a line or SIZE-1 bytes into S, whichever is less, from STREAM.
Matt Kraai854125f2001-05-09 19:15:46 +000066 * Returns S, or NULL if an eof or non-interrupt error is encountered. */
67static char *safe_fgets(char *s, int size, FILE *stream)
68{
69 char *ret;
70
71 do {
72 clearerr(stream);
73 ret = fgets(s, size, stream);
74 } while (ret == NULL && ferror(stream) && errno == EINTR);
75
76 return ret;
77}
78
Denis Vlasenko9cade082006-11-21 10:43:02 +000079#if ENABLE_FEATURE_WGET_AUTHENTICATION
Denis Vlasenko12d21292007-06-27 21:40:07 +000080/* Base64-encode character string. buf is assumed to be char buf[512]. */
81static char *base64enc_512(char buf[512], const char *str)
Denis Vlasenko3526a132006-09-09 12:20:57 +000082{
Denis Vlasenko12d21292007-06-27 21:40:07 +000083 unsigned len = strlen(str);
84 if (len > 512/4*3 - 10) /* paranoia */
85 len = 512/4*3 - 10;
86 bb_uuencode(buf, str, len, bb_uuenc_tbl_base64);
Rob Landley76ef08c2006-06-13 16:44:26 +000087 return buf;
Eric Andersen79757c92001-04-05 21:45:54 +000088}
89#endif
90
Denis Vlasenko06af2162007-02-03 17:28:39 +000091int wget_main(int argc, char **argv);
Eric Andersen96700832000-09-04 15:15:55 +000092int wget_main(int argc, char **argv)
93{
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000094 char buf[512];
Eric Andersen79757c92001-04-05 21:45:54 +000095 struct host_info server, target;
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000096 len_and_sockaddr *lsa;
Denis Vlasenko06783a52007-09-24 13:51:54 +000097 int status;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000098 int port;
99 int try = 5;
100 unsigned opt;
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000101 char *str;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000102 char *proxy = 0;
103 char *dir_prefix = NULL;
104#if ENABLE_FEATURE_WGET_LONG_OPTIONS
105 char *extra_headers = NULL;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000106 llist_t *headers_llist = NULL;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000107#endif
Eric Andersen79757c92001-04-05 21:45:54 +0000108
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000109 FILE *sfp = NULL; /* socket to web/ftp server */
110 FILE *dfp = NULL; /* socket to ftp server (data) */
111 char *fname_out = NULL; /* where to direct output (-O) */
Denis Vlasenko46611172007-08-06 15:43:17 +0000112 bool got_clen = 0; /* got content-length: from server */
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000113 int output_fd = -1;
Denis Vlasenko46611172007-08-06 15:43:17 +0000114 bool use_proxy = 1; /* Use proxies if env vars are set */
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000115 const char *proxy_flag = "on"; /* Use proxies if env vars are set */
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000116 const char *user_agent = "Wget";/* "User-Agent" header field */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000117 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000118 "content-length\0""transfer-encoding\0""chunked\0""location\0";
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000119 enum {
120 KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location
121 };
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000122 enum {
123 WGET_OPT_CONTINUE = 0x1,
Bernhard Reutner-Fischer2e75dcc2007-04-05 10:31:47 +0000124 WGET_OPT_SPIDER = 0x2,
125 WGET_OPT_QUIET = 0x4,
126 WGET_OPT_OUTNAME = 0x8,
127 WGET_OPT_PREFIX = 0x10,
128 WGET_OPT_PROXY = 0x20,
129 WGET_OPT_USER_AGENT = 0x40,
130 WGET_OPT_PASSIVE = 0x80,
131 WGET_OPT_HEADER = 0x100,
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000132 };
Bernhard Reutner-Fischer289e86a2006-08-20 20:01:24 +0000133#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000134 static const char wget_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000135 /* name, has_arg, val */
136 "continue\0" No_argument "c"
137 "spider\0" No_argument "s"
138 "quiet\0" No_argument "q"
139 "output-document\0" Required_argument "O"
140 "directory-prefix\0" Required_argument "P"
141 "proxy\0" Required_argument "Y"
142 "user-agent\0" Required_argument "U"
143 "passive-ftp\0" No_argument "\xff"
144 "header\0" Required_argument "\xfe"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000145 ;
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000146 applet_long_options = wget_longopts;
Bernhard Reutner-Fischer8d3a6f72006-05-31 14:11:38 +0000147#endif
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000148 /* server.allocated = target.allocated = NULL; */
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000149 opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000150 opt = getopt32(argv, "csqO:P:Y:U:",
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000151 &fname_out, &dir_prefix,
152 &proxy_flag, &user_agent
153 USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
154 );
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000155 if (strcmp(proxy_flag, "off") == 0) {
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000156 /* Use the proxy if necessary */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000157 use_proxy = 0;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000158 }
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000159#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenko7534e082006-10-23 23:21:58 +0000160 if (headers_llist) {
161 int size = 1;
162 char *cp;
Denis Vlasenko8d9f4952007-04-08 15:08:42 +0000163 llist_t *ll = headers_llist;
Denis Vlasenko7534e082006-10-23 23:21:58 +0000164 while (ll) {
165 size += strlen(ll->data) + 2;
166 ll = ll->link;
167 }
168 extra_headers = cp = xmalloc(size);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000169 while (headers_llist) {
Denis Vlasenko7534e082006-10-23 23:21:58 +0000170 cp += sprintf(cp, "%s\r\n", headers_llist->data);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000171 headers_llist = headers_llist->link;
Eric Andersen96700832000-09-04 15:15:55 +0000172 }
173 }
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000174#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000175
Eric Andersen79757c92001-04-05 21:45:54 +0000176 parse_url(argv[optind], &target);
177 server.host = target.host;
178 server.port = target.port;
179
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000180 /* Use the proxy if necessary */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000181 if (use_proxy) {
Robert Griebld7760112002-05-14 23:36:45 +0000182 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000183 if (proxy && *proxy) {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000184 parse_url(proxy, &server);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000185 } else {
186 use_proxy = 0;
187 }
Robert Griebld7760112002-05-14 23:36:45 +0000188 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000189
Eric Andersen29edd002000-12-09 16:55:35 +0000190 /* Guess an output filename */
191 if (!fname_out) {
Eric Andersen751750e2004-10-08 08:27:40 +0000192 // Dirty hack. Needed because bb_get_last_path_component
193 // will destroy trailing / by storing '\0' in last byte!
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000194 if (!last_char_is(target.path, '/')) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000195 fname_out = bb_get_last_path_component(target.path);
Denis Vlasenko9cade082006-11-21 10:43:02 +0000196#if ENABLE_FEATURE_WGET_STATUSBAR
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000197 curfile = fname_out;
Eric Andersen29edd002000-12-09 16:55:35 +0000198#endif
Eric Andersen751750e2004-10-08 08:27:40 +0000199 }
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000200 if (!fname_out || !fname_out[0]) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000201 /* bb_get_last_path_component writes
202 * to last '/' only. We don't have one here... */
203 fname_out = (char*)"index.html";
Denis Vlasenko9cade082006-11-21 10:43:02 +0000204#if ENABLE_FEATURE_WGET_STATUSBAR
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000205 curfile = fname_out;
Eric Andersen29edd002000-12-09 16:55:35 +0000206#endif
Eric Andersen29edd002000-12-09 16:55:35 +0000207 }
Matt Kraai0382eb82001-07-19 19:13:55 +0000208 if (dir_prefix != NULL)
209 fname_out = concat_path_file(dir_prefix, fname_out);
Denis Vlasenko9cade082006-11-21 10:43:02 +0000210#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000211 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212 curfile = bb_get_last_path_component(fname_out);
Eric Andersen29edd002000-12-09 16:55:35 +0000213#endif
214 }
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000215 /* Impossible?
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000216 if ((opt & WGET_OPT_CONTINUE) && !fname_out)
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000217 bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
Eric Andersen29edd002000-12-09 16:55:35 +0000218
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000219 /* Determine where to start transfer */
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... */
Denis Vlasenko42823d52007-02-04 02:39:08 +0000236 lsa = xhost2sockaddr(server.host, server.port);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000237 if (!(opt & WGET_OPT_QUIET)) {
Denis Vlasenko85629f02007-01-22 09:36:41 +0000238 fprintf(stderr, "Connecting to %s (%s)\n", server.host,
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000239 xmalloc_sockaddr2dotted(&lsa->sa));
Denis Vlasenko85629f02007-01-22 09:36:41 +0000240 /* We leak result of xmalloc_sockaddr2dotted */
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
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000253 /* Open socket to http server */
Eric Andersen79757c92001-04-05 21:45:54 +0000254 if (sfp) fclose(sfp);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000255 sfp = open_socket(lsa);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000256
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000257 /* Send HTTP request. */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000258 if (use_proxy) {
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000259 fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
Eric Andersen79757c92001-04-05 21:45:54 +0000260 target.is_ftp ? "f" : "ht", target.host,
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000261 target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000262 } else {
Eric Andersen6d7fa432001-04-10 18:17:05 +0000263 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000264 }
Eric Andersen96700832000-09-04 15:15:55 +0000265
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000266 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n",
267 target.host, user_agent);
Eric Andersen79757c92001-04-05 21:45:54 +0000268
Denis Vlasenko9cade082006-11-21 10:43:02 +0000269#if ENABLE_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +0000270 if (target.user) {
Denis Vlasenko12d21292007-06-27 21:40:07 +0000271 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n"+6,
272 base64enc_512(buf, target.user));
Eric Andersen79757c92001-04-05 21:45:54 +0000273 }
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000274 if (use_proxy && server.user) {
Eric Andersen79757c92001-04-05 21:45:54 +0000275 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
Denis Vlasenko12d21292007-06-27 21:40:07 +0000276 base64enc_512(buf, server.user));
Eric Andersen79757c92001-04-05 21:45:54 +0000277 }
278#endif
279
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000280 if (beg_range)
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000281 fprintf(sfp, "Range: bytes=%"OFF_FMT"d-\r\n", beg_range);
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000282#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenko7534e082006-10-23 23:21:58 +0000283 if (extra_headers)
284 fputs(extra_headers, sfp);
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000285#endif
Denis Vlasenko7534e082006-10-23 23:21:58 +0000286 fprintf(sfp, "Connection: close\r\n\r\n");
Eric Andersen79757c92001-04-05 21:45:54 +0000287
288 /*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000289 * Retrieve HTTP response line and check for "200" status code.
290 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000291 read_response:
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000292 if (fgets(buf, sizeof(buf), sfp) == NULL)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000293 bb_error_msg_and_die("no response from server");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000294
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000295 str = buf;
296 str = skip_non_whitespace(str);
297 str = skip_whitespace(str);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000298 // FIXME: no error check
299 // xatou wouldn't work: "200 OK"
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000300 status = atoi(str);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000301 switch (status) {
302 case 0:
303 case 100:
Denis Vlasenko06783a52007-09-24 13:51:54 +0000304 while (gethdr(buf, sizeof(buf), sfp /*, &n*/) != NULL)
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000305 /* eat all remaining headers */;
306 goto read_response;
307 case 200:
308 break;
309 case 300: /* redirection */
310 case 301:
311 case 302:
312 case 303:
313 break;
314 case 206:
315 if (beg_range)
Eric Andersen79757c92001-04-05 21:45:54 +0000316 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000317 /*FALLTHRU*/
318 default:
Denis Vlasenko067e3f02006-11-10 23:25:53 +0000319 /* Show first line only and kill any ESC tricks */
320 buf[strcspn(buf, "\n\r\x1b")] = '\0';
321 bb_error_msg_and_die("server returned error: %s", buf);
Eric Andersen79757c92001-04-05 21:45:54 +0000322 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000323
Eric Andersen79757c92001-04-05 21:45:54 +0000324 /*
325 * Retrieve HTTP headers.
326 */
Denis Vlasenko06783a52007-09-24 13:51:54 +0000327 while ((str = gethdr(buf, sizeof(buf), sfp /*, &n*/)) != NULL) {
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000328 /* gethdr did already convert the "FOO:" string to lowercase */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000329 smalluint key = index_in_strings(keywords, *&buf) + 1;
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000330 if (key == KEY_content_length) {
331 content_len = BB_STRTOOFF(str, NULL, 10);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000332 if (errno || content_len < 0) {
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000333 bb_error_msg_and_die("content-length %s is garbage", str);
Eric Andersen24794452004-03-06 22:11:45 +0000334 }
Eric Andersen79757c92001-04-05 21:45:54 +0000335 got_clen = 1;
336 continue;
337 }
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000338 if (key == KEY_transfer_encoding) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000339 if (index_in_strings(keywords, str_tolower(str)) + 1 != KEY_chunked)
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000340 bb_error_msg_and_die("server wants to do %s transfer encoding", str);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000341 chunked = got_clen = 1;
Eric Andersen6d7fa432001-04-10 18:17:05 +0000342 }
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000343 if (key == KEY_location) {
344 if (str[0] == '/')
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000345 /* free(target.allocated); */
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000346 target.path = /* target.allocated = */ xstrdup(str+1);
Eric Andersen79757c92001-04-05 21:45:54 +0000347 else {
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000348 parse_url(str, &target);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000349 if (use_proxy == 0) {
Eric Andersen79757c92001-04-05 21:45:54 +0000350 server.host = target.host;
351 server.port = target.port;
352 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000353 free(lsa);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000354 lsa = xhost2sockaddr(server.host, server.port);
Glenn L McGrath58a2e0e2004-01-17 23:07:14 +0000355 break;
Eric Andersen79757c92001-04-05 21:45:54 +0000356 }
357 }
358 }
Denis Vlasenko3469c182006-12-16 22:19:47 +0000359 } while (status >= 300);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000360
Eric Andersen79757c92001-04-05 21:45:54 +0000361 dfp = sfp;
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000362
363 } else {
364
Eric Andersen79757c92001-04-05 21:45:54 +0000365 /*
366 * FTP session
367 */
Denis Vlasenko3526a132006-09-09 12:20:57 +0000368 if (!target.user)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000369 target.user = xstrdup("anonymous:busybox@");
Eric Andersen79757c92001-04-05 21:45:54 +0000370
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000371 sfp = open_socket(lsa);
Eric Andersen79757c92001-04-05 21:45:54 +0000372 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000373 bb_error_msg_and_die("%s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000374
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000375 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000376 * Splitting username:password pair,
377 * trying to log in
378 */
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000379 str = strchr(target.user, ':');
380 if (str)
381 *(str++) = '\0';
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000382 switch (ftpcmd("USER ", target.user, sfp, buf)) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000383 case 230:
384 break;
385 case 331:
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000386 if (ftpcmd("PASS ", str, sfp, buf) == 230)
Eric Andersenb520e082000-10-03 00:21:45 +0000387 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000388 /* FALLTHRU (failed login) */
389 default:
390 bb_error_msg_and_die("ftp login: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000391 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000392
Eric Andersen79757c92001-04-05 21:45:54 +0000393 ftpcmd("TYPE I", NULL, sfp, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000394
Eric Andersen79757c92001-04-05 21:45:54 +0000395 /*
396 * Querying file size
397 */
Rob Landleyaf12cb32006-06-27 18:41:03 +0000398 if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
Denis Vlasenkod686a042006-11-27 14:43:21 +0000399 content_len = BB_STRTOOFF(buf+4, NULL, 10);
400 if (errno || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000401 bb_error_msg_and_die("SIZE value is garbage");
Eric Andersen24794452004-03-06 22:11:45 +0000402 }
Eric Andersen96700832000-09-04 15:15:55 +0000403 got_clen = 1;
Eric Andersen96700832000-09-04 15:15:55 +0000404 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000405
Eric Andersen79757c92001-04-05 21:45:54 +0000406 /*
407 * Entering passive mode
408 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000409 if (ftpcmd("PASV", NULL, sfp, buf) != 227) {
410 pasv_error:
411 bb_error_msg_and_die("bad response to %s: %s", "PASV", buf);
412 }
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000413 // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000414 // Server's IP is N1.N2.N3.N4 (we ignore it)
415 // Server's port for data connection is P1*256+P2
Bernhard Reutner-Fischer7e8a53a2007-04-10 09:37:29 +0000416 str = strrchr(buf, ')');
417 if (str) str[0] = '\0';
418 str = strrchr(buf, ',');
419 if (!str) goto pasv_error;
420 port = xatou_range(str+1, 0, 255);
421 *str = '\0';
422 str = strrchr(buf, ',');
423 if (!str) goto pasv_error;
424 port += xatou_range(str+1, 0, 255) * 256;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000425 set_nport(lsa, htons(port));
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000426 dfp = open_socket(lsa);
Eric Andersen79757c92001-04-05 21:45:54 +0000427
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000428 if (beg_range) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000429 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000430 if (ftpcmd(buf, NULL, sfp, buf) == 350)
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000431 content_len -= beg_range;
Eric Andersen96700832000-09-04 15:15:55 +0000432 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000433
Rob Landleyaf12cb32006-06-27 18:41:03 +0000434 if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000435 bb_error_msg_and_die("bad response to RETR: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000436 }
Bernhard Reutner-Fischer2e75dcc2007-04-05 10:31:47 +0000437 if (opt & WGET_OPT_SPIDER) {
438 if (ENABLE_FEATURE_CLEAN_UP)
439 fclose(sfp);
440 goto done;
441 }
Eric Andersen79757c92001-04-05 21:45:54 +0000442
Eric Andersen96700832000-09-04 15:15:55 +0000443 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000444 * Retrieve file
Eric Andersen96700832000-09-04 15:15:55 +0000445 */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000446 if (chunked) {
447 fgets(buf, sizeof(buf), dfp);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000448 content_len = STRTOOFF(buf, NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000449 /* FIXME: error check?? */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000450 }
Rob Landley19a39402006-06-13 17:10:26 +0000451
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000452 /* Do it before progressmeter (want to have nice error message) */
453 if (output_fd < 0)
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000454 output_fd = xopen(fname_out,
455 O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000456
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000457 if (!(opt & WGET_OPT_QUIET))
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +0000458 progressmeter(-1);
Rob Landley19a39402006-06-13 17:10:26 +0000459
Denis Vlasenko06783a52007-09-24 13:51:54 +0000460 /* Loops only if chunked */
461 while (1) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000462 while (content_len > 0 || !got_clen) {
Denis Vlasenko06783a52007-09-24 13:51:54 +0000463 int n;
Denis Vlasenko3526a132006-09-09 12:20:57 +0000464 unsigned rdsz = sizeof(buf);
Denis Vlasenko06783a52007-09-24 13:51:54 +0000465
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000466 if (content_len < sizeof(buf) && (chunked || got_clen))
467 rdsz = (unsigned)content_len;
Denis Vlasenko12d21292007-06-27 21:40:07 +0000468 n = safe_fread(buf, rdsz, dfp);
Denis Vlasenko06783a52007-09-24 13:51:54 +0000469 if (n <= 0) {
470 if (ferror(dfp)) {
471 /* perror will not work: ferror doesn't set errno */
472 bb_error_msg_and_die(bb_msg_read_error);
473 }
Denis Vlasenko3526a132006-09-09 12:20:57 +0000474 break;
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000475 }
Denis Vlasenko06783a52007-09-24 13:51:54 +0000476 xwrite(output_fd, buf, n);
Denis Vlasenko9cade082006-11-21 10:43:02 +0000477#if ENABLE_FEATURE_WGET_STATUSBAR
Rob Landley19a39402006-06-13 17:10:26 +0000478 transferred += n;
Eric Andersenb520e082000-10-03 00:21:45 +0000479#endif
Denis Vlasenko06783a52007-09-24 13:51:54 +0000480 if (got_clen)
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000481 content_len -= n;
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000482 }
Eric Andersen79757c92001-04-05 21:45:54 +0000483
Denis Vlasenko06783a52007-09-24 13:51:54 +0000484 if (!chunked)
485 break;
Eric Andersen6d7fa432001-04-10 18:17:05 +0000486
Denis Vlasenko06783a52007-09-24 13:51:54 +0000487 safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
488 safe_fgets(buf, sizeof(buf), dfp);
489 content_len = STRTOOFF(buf, NULL, 16);
490 /* FIXME: error check? */
491 if (content_len == 0)
492 break; /* all done! */
493 }
Rob Landley19a39402006-06-13 17:10:26 +0000494
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000495 if (!(opt & WGET_OPT_QUIET))
Mark Whitley30ac01c2001-04-17 18:13:16 +0000496 progressmeter(1);
Rob Landley19a39402006-06-13 17:10:26 +0000497
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000498 if ((use_proxy == 0) && target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000499 fclose(dfp);
500 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000501 bb_error_msg_and_die("ftp error: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000502 ftpcmd("QUIT", NULL, sfp, buf);
503 }
Bernhard Reutner-Fischer2e75dcc2007-04-05 10:31:47 +0000504done:
Eric Andersen79757c92001-04-05 21:45:54 +0000505 exit(EXIT_SUCCESS);
Eric Andersen96700832000-09-04 15:15:55 +0000506}
507
508
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000509static void parse_url(char *src_url, struct host_info *h)
Eric Andersen96700832000-09-04 15:15:55 +0000510{
Denis Vlasenko3469c182006-12-16 22:19:47 +0000511 char *url, *p, *sp;
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000512
513 /* h->allocated = */ url = xstrdup(src_url);
Eric Andersen96700832000-09-04 15:15:55 +0000514
Eric Andersen79757c92001-04-05 21:45:54 +0000515 if (strncmp(url, "http://", 7) == 0) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000516 h->port = bb_lookup_port("http", "tcp", 80);
Eric Andersen79757c92001-04-05 21:45:54 +0000517 h->host = url + 7;
518 h->is_ftp = 0;
519 } else if (strncmp(url, "ftp://", 6) == 0) {
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000520 h->port = bb_lookup_port("ftp", "tcp", 21);
Eric Andersen79757c92001-04-05 21:45:54 +0000521 h->host = url + 6;
522 h->is_ftp = 1;
523 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000524 bb_error_msg_and_die("not an http or ftp url: %s", url);
Eric Andersen96700832000-09-04 15:15:55 +0000525
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000526 // FYI:
527 // "Real" wget 'http://busybox.net?var=a/b' sends this request:
528 // 'GET /?var=a/b HTTP 1.0'
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000529 // and saves 'index.html?var=a%2Fb' (we save 'b')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000530 // wget 'http://busybox.net?login=john@doe':
531 // request: 'GET /?login=john@doe HTTP/1.0'
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000532 // saves: 'index.html?login=john@doe' (we save '?login=john@doe')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000533 // wget 'http://busybox.net#test/test':
534 // request: 'GET / HTTP/1.0'
535 // saves: 'index.html' (we save 'test')
536 //
537 // We also don't add unique .N suffix if file exists...
Eric Andersen79757c92001-04-05 21:45:54 +0000538 sp = strchr(h->host, '/');
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000539 p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
540 p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
541 if (!sp) {
Denis Vlasenkode55b5d2007-02-01 01:53:25 +0000542 /* must be writable because of bb_get_last_path_component() */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000543 static char nullstr[] ALIGN1 = "";
Denis Vlasenkoafe488d2007-01-28 16:07:45 +0000544 h->path = nullstr;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000545 } else if (*sp == '/') {
Denis Vlasenko3469c182006-12-16 22:19:47 +0000546 *sp = '\0';
547 h->path = sp + 1;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000548 } else { // '#' or '?'
549 // http://busybox.net?login=john@doe is a valid URL
550 // memmove converts to:
551 // http:/busybox.nett?login=john@doe...
552 memmove(h->host-1, h->host, sp - h->host);
553 h->host--;
554 sp[-1] = '\0';
555 h->path = sp;
556 }
Eric Andersen79757c92001-04-05 21:45:54 +0000557
Denis Vlasenko3469c182006-12-16 22:19:47 +0000558 sp = strrchr(h->host, '@');
559 h->user = NULL;
560 if (sp != NULL) {
Eric Andersen79757c92001-04-05 21:45:54 +0000561 h->user = h->host;
Denis Vlasenko3469c182006-12-16 22:19:47 +0000562 *sp = '\0';
563 h->host = sp + 1;
564 }
Eric Andersen79757c92001-04-05 21:45:54 +0000565
Denis Vlasenko3469c182006-12-16 22:19:47 +0000566 sp = h->host;
Eric Andersen96700832000-09-04 15:15:55 +0000567}
568
569
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000570static FILE *open_socket(len_and_sockaddr *lsa)
Eric Andersen96700832000-09-04 15:15:55 +0000571{
Eric Andersen96700832000-09-04 15:15:55 +0000572 FILE *fp;
573
Denis Vlasenko067e3f02006-11-10 23:25:53 +0000574 /* glibc 2.4 seems to try seeking on it - ??! */
575 /* hopefully it understands what ESPIPE means... */
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000576 fp = fdopen(xconnect_stream(lsa), "r+");
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000577 if (fp == NULL)
Denis Vlasenko3526a132006-09-09 12:20:57 +0000578 bb_perror_msg_and_die("fdopen");
Eric Andersen96700832000-09-04 15:15:55 +0000579
580 return fp;
581}
582
583
Denis Vlasenko06783a52007-09-24 13:51:54 +0000584static char *gethdr(char *buf, size_t bufsiz, FILE *fp /*, int *istrunc*/)
Eric Andersen96700832000-09-04 15:15:55 +0000585{
586 char *s, *hdrval;
587 int c;
588
Denis Vlasenko06783a52007-09-24 13:51:54 +0000589 /* *istrunc = 0; */
Eric Andersen96700832000-09-04 15:15:55 +0000590
591 /* retrieve header line */
592 if (fgets(buf, bufsiz, fp) == NULL)
593 return NULL;
594
595 /* see if we are at the end of the headers */
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000596 for (s = buf; *s == '\r'; ++s)
Denis Vlasenko06783a52007-09-24 13:51:54 +0000597 continue;
598 if (*s == '\n')
Eric Andersen96700832000-09-04 15:15:55 +0000599 return NULL;
600
601 /* convert the header name to lower case */
Denis Vlasenko06783a52007-09-24 13:51:54 +0000602 for (s = buf; isalnum(*s) || *s == '-' || *s == '.'; ++s)
Eric Andersen96700832000-09-04 15:15:55 +0000603 *s = tolower(*s);
604
605 /* verify we are at the end of the header name */
606 if (*s != ':')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000607 bb_error_msg_and_die("bad header line: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000608
609 /* locate the start of the header value */
Denis Vlasenko06783a52007-09-24 13:51:54 +0000610 *s++ = '\0';
611 hdrval = skip_whitespace(s);
Eric Andersen96700832000-09-04 15:15:55 +0000612
613 /* locate the end of header */
Denis Vlasenko06783a52007-09-24 13:51:54 +0000614 while (*s && *s != '\r' && *s != '\n')
Eric Andersen96700832000-09-04 15:15:55 +0000615 ++s;
616
617 /* end of header found */
Denis Vlasenko06783a52007-09-24 13:51:54 +0000618 if (*s) {
Eric Andersen96700832000-09-04 15:15:55 +0000619 *s = '\0';
620 return hdrval;
621 }
622
Denis Vlasenko06783a52007-09-24 13:51:54 +0000623 /* Rats! The buffer isn't big enough to hold the entire header value. */
Eric Andersen96700832000-09-04 15:15:55 +0000624 while (c = getc(fp), c != EOF && c != '\n')
Denis Vlasenko06783a52007-09-24 13:51:54 +0000625 continue;
626 /* *istrunc = 1; */
Eric Andersen96700832000-09-04 15:15:55 +0000627 return hdrval;
628}
629
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000630static int ftpcmd(const char *s1, const char *s2, FILE *fp, char *buf)
Eric Andersen79757c92001-04-05 21:45:54 +0000631{
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000632 int result;
Eric Andersen79757c92001-04-05 21:45:54 +0000633 if (s1) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000634 if (!s2) s2 = "";
Eric Andersen3f1cf452003-03-11 18:03:39 +0000635 fprintf(fp, "%s%s\r\n", s1, s2);
Eric Andersen79757c92001-04-05 21:45:54 +0000636 fflush(fp);
637 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000638
Eric Andersen79757c92001-04-05 21:45:54 +0000639 do {
Glenn L McGrath32da8852004-04-08 10:27:11 +0000640 char *buf_ptr;
641
642 if (fgets(buf, 510, fp) == NULL) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000643 bb_perror_msg_and_die("error getting response");
Glenn L McGrath32da8852004-04-08 10:27:11 +0000644 }
645 buf_ptr = strstr(buf, "\r\n");
646 if (buf_ptr) {
647 *buf_ptr = '\0';
648 }
Denis Vlasenko3526a132006-09-09 12:20:57 +0000649 } while (!isdigit(buf[0]) || buf[3] != ' ');
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000650
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000651 buf[3] = '\0';
652 result = xatoi_u(buf);
653 buf[3] = ' ';
654 return result;
Eric Andersen79757c92001-04-05 21:45:54 +0000655}
656
Denis Vlasenko9cade082006-11-21 10:43:02 +0000657#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000658/* Stuff below is from BSD rcp util.c, as added to openshh.
Eric Andersen4e573f42000-11-14 23:29:24 +0000659 * Original copyright notice is retained at the end of this file.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000660 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000661static int
Eric Andersenb520e082000-10-03 00:21:45 +0000662getttywidth(void)
663{
Denis Vlasenko9cade082006-11-21 10:43:02 +0000664 int width;
Eric Andersen8efe9672003-09-15 08:33:45 +0000665 get_terminal_width_height(0, &width, NULL);
Denis Vlasenko621204b2006-10-27 09:03:24 +0000666 return width;
Eric Andersenb520e082000-10-03 00:21:45 +0000667}
668
Eric Andersen3e6ff902001-03-09 21:24:12 +0000669static void
Eric Andersenb520e082000-10-03 00:21:45 +0000670updateprogressmeter(int ignore)
671{
672 int save_errno = errno;
673
674 progressmeter(0);
675 errno = save_errno;
676}
677
Rob Landleyc9c1a412006-07-12 19:17:55 +0000678static void alarmtimer(int iwait)
Eric Andersenb520e082000-10-03 00:21:45 +0000679{
680 struct itimerval itv;
681
Rob Landleyc9c1a412006-07-12 19:17:55 +0000682 itv.it_value.tv_sec = iwait;
Eric Andersenb520e082000-10-03 00:21:45 +0000683 itv.it_value.tv_usec = 0;
684 itv.it_interval = itv.it_value;
685 setitimer(ITIMER_REAL, &itv, NULL);
686}
687
Eric Andersen3e6ff902001-03-09 21:24:12 +0000688static void
Eric Andersenb520e082000-10-03 00:21:45 +0000689progressmeter(int flag)
690{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000691 static unsigned lastupdate_sec;
692 static unsigned start_sec;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000693 static off_t lastsize, totalsize;
Rob Landley19a39402006-06-13 17:10:26 +0000694
Denis Vlasenko7039a662006-10-08 17:54:47 +0000695 off_t abbrevsize;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000696 unsigned since_last_update, elapsed;
697 unsigned ratio;
698 int barlength, i;
Eric Andersenb520e082000-10-03 00:21:45 +0000699
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000700 if (flag == -1) { /* first call to progressmeter */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000701 start_sec = monotonic_sec();
702 lastupdate_sec = start_sec;
Eric Andersenb520e082000-10-03 00:21:45 +0000703 lastsize = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000704 totalsize = content_len + beg_range; /* as content_len changes.. */
Eric Andersenb520e082000-10-03 00:21:45 +0000705 }
706
Rob Landley19a39402006-06-13 17:10:26 +0000707 ratio = 100;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000708 if (totalsize != 0 && !chunked) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000709 /* long long helps to have it working even if !LFS */
710 ratio = (unsigned) (100ULL * (transferred+beg_range) / totalsize);
711 if (ratio > 100) ratio = 100;
Rob Landley19a39402006-06-13 17:10:26 +0000712 }
Eric Andersenb520e082000-10-03 00:21:45 +0000713
Rob Landley19a39402006-06-13 17:10:26 +0000714 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
Eric Andersenb520e082000-10-03 00:21:45 +0000715
Denis Vlasenko76791452007-06-18 08:55:57 +0000716 barlength = getttywidth() - 49;
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000717 if (barlength > 0) {
718 /* god bless gcc for variable arrays :) */
Eric Andersenb520e082000-10-03 00:21:45 +0000719 i = barlength * ratio / 100;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000720 {
721 char buf[i+1];
722 memset(buf, '*', i);
723 buf[i] = '\0';
724 fprintf(stderr, "|%s%*s|", buf, barlength - i, "");
725 }
Eric Andersenb520e082000-10-03 00:21:45 +0000726 }
727 i = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000728 abbrevsize = transferred + beg_range;
Rob Landley19a39402006-06-13 17:10:26 +0000729 while (abbrevsize >= 100000) {
Eric Andersenb520e082000-10-03 00:21:45 +0000730 i++;
731 abbrevsize >>= 10;
732 }
Denis Vlasenko9cade082006-11-21 10:43:02 +0000733 /* see http://en.wikipedia.org/wiki/Tera */
Denis Vlasenko76791452007-06-18 08:55:57 +0000734 fprintf(stderr, "%6d%c ", (int)abbrevsize, " kMGTPEZY"[i]);
Eric Andersenb520e082000-10-03 00:21:45 +0000735
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000736// Nuts! Ain't it easier to update progress meter ONLY when we transferred++?
737// FIXME: get rid of alarmtimer + updateprogressmeter mess
Eric Andersenb520e082000-10-03 00:21:45 +0000738
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000739 elapsed = monotonic_sec();
740 since_last_update = elapsed - lastupdate_sec;
741 if (transferred > lastsize) {
742 lastupdate_sec = elapsed;
743 lastsize = transferred;
744 if (since_last_update >= STALLTIME) {
745 /* We "cut off" these seconds from elapsed time
746 * by adjusting start time */
747 start_sec += since_last_update;
748 }
749 since_last_update = 0; /* we are un-stalled now */
750 }
751 elapsed -= start_sec; /* now it's "elapsed since start" */
752
753 if (since_last_update >= STALLTIME) {
Rob Landley19a39402006-06-13 17:10:26 +0000754 fprintf(stderr, " - stalled -");
Eric Andersenb520e082000-10-03 00:21:45 +0000755 } else {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000756 off_t to_download = totalsize - beg_range;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000757 if (transferred <= 0 || (int)elapsed <= 0 || transferred > to_download || chunked) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000758 fprintf(stderr, "--:--:-- ETA");
759 } else {
760 /* to_download / (transferred/elapsed) - elapsed: */
Denis Vlasenko9cade082006-11-21 10:43:02 +0000761 int eta = (int) ((unsigned long long)to_download*elapsed/transferred - elapsed);
762 /* (long long helps to have working ETA even if !LFS) */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000763 i = eta % 3600;
764 fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
765 }
Eric Andersenb520e082000-10-03 00:21:45 +0000766 }
Eric Andersenb520e082000-10-03 00:21:45 +0000767
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000768 if (flag == -1) { /* first call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000769 struct sigaction sa;
770 sa.sa_handler = updateprogressmeter;
771 sigemptyset(&sa.sa_mask);
772 sa.sa_flags = SA_RESTART;
773 sigaction(SIGALRM, &sa, NULL);
774 alarmtimer(1);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000775 } else if (flag == 1) { /* last call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000776 alarmtimer(0);
Rob Landley19a39402006-06-13 17:10:26 +0000777 transferred = 0;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000778 putc('\n', stderr);
Eric Andersenb520e082000-10-03 00:21:45 +0000779 }
780}
Denis Vlasenko12d21292007-06-27 21:40:07 +0000781#endif /* FEATURE_WGET_STATUSBAR */
Eric Andersen4e573f42000-11-14 23:29:24 +0000782
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000783/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
Eric Andersenaff114c2004-04-14 17:51:38 +0000784 * much of which was blatantly stolen from openssh. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000785
Eric Andersen4e573f42000-11-14 23:29:24 +0000786/*-
787 * Copyright (c) 1992, 1993
788 * The Regents of the University of California. All rights reserved.
789 *
790 * Redistribution and use in source and binary forms, with or without
791 * modification, are permitted provided that the following conditions
792 * are met:
793 * 1. Redistributions of source code must retain the above copyright
794 * notice, this list of conditions and the following disclaimer.
795 * 2. Redistributions in binary form must reproduce the above copyright
796 * notice, this list of conditions and the following disclaimer in the
797 * documentation and/or other materials provided with the distribution.
798 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000799 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
800 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen4e573f42000-11-14 23:29:24 +0000801 *
802 * 4. Neither the name of the University nor the names of its contributors
803 * may be used to endorse or promote products derived from this software
804 * without specific prior written permission.
805 *
806 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
807 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
808 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
809 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
810 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
811 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
812 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
813 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
814 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
815 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
816 * SUCH DAMAGE.
817 *
Eric Andersen4e573f42000-11-14 23:29:24 +0000818 */