Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 3 | * wget - retrieve a file using HTTP or FTP |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 5 | * Chip Rosenthal Covad Communications <chip@laserlink.net> |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 6 | * |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 9 | /* 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 Landley | af12cb3 | 2006-06-27 18:41:03 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 14 | #include <getopt.h> /* for struct option */ |
| 15 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 16 | struct host_info { |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 17 | // May be used if we ever will want to free() all xstrdup()s... |
| 18 | /* char *allocated; */ |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 19 | char *host; |
| 20 | int port; |
| 21 | char *path; |
| 22 | int is_ftp; |
| 23 | char *user; |
| 24 | }; |
| 25 | |
| 26 | static void parse_url(char *url, struct host_info *h); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 27 | static FILE *open_socket(len_and_sockaddr *lsa); |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 28 | static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc); |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 29 | static int ftpcmd(const char *s1, const char *s2, FILE *fp, char *buf); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 30 | |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 31 | /* Globals (can be accessed from signal handlers */ |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 32 | static off_t content_len; /* Content-length of the file */ |
| 33 | static off_t beg_range; /* Range at which continue begins */ |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 34 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 35 | static off_t transferred; /* Number of bytes transferred so far */ |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 36 | #endif |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 37 | static int chunked; /* chunked transfer encoding */ |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 38 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 39 | static void progressmeter(int flag); |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 40 | static const char *curfile; /* Name of current file being transferred */ |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 41 | static struct timeval start; /* Time a transfer started */ |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 42 | enum { |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 43 | STALLTIME = 5 /* Seconds when xfer considered "stalled" */ |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 44 | }; |
| 45 | #else |
Rob Landley | f5fc138 | 2006-09-15 04:08:25 +0000 | [diff] [blame] | 46 | static void progressmeter(int flag) {} |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 47 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 48 | |
Matt Kraai | 854125f | 2001-05-09 19:15:46 +0000 | [diff] [blame] | 49 | /* 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. */ |
| 52 | static 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 Kraai | 854125f | 2001-05-09 19:15:46 +0000 | [diff] [blame] | 64 | /* 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. */ |
| 66 | static 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 Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 78 | #if ENABLE_FEATURE_WGET_AUTHENTICATION |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 79 | /* |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 80 | * Base64-encode character string and return the string. |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 81 | */ |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 82 | static char *base64enc(unsigned char *p, char *buf, int len) |
| 83 | { |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 84 | bb_uuencode(p, buf, len, bb_uuenc_tbl_base64); |
Rob Landley | 76ef08c | 2006-06-13 16:44:26 +0000 | [diff] [blame] | 85 | return buf; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 86 | } |
| 87 | #endif |
| 88 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 89 | int wget_main(int argc, char **argv); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 90 | int wget_main(int argc, char **argv) |
| 91 | { |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 92 | char buf[512]; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 93 | struct host_info server, target; |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 94 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 95 | int n, status; |
| 96 | int port; |
| 97 | int try = 5; |
| 98 | unsigned opt; |
| 99 | char *s; |
| 100 | char *proxy = 0; |
| 101 | char *dir_prefix = NULL; |
| 102 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
| 103 | char *extra_headers = NULL; |
Glenn L McGrath | 514aeab | 2003-12-19 12:08:56 +0000 | [diff] [blame] | 104 | llist_t *headers_llist = NULL; |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 105 | #endif |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 107 | /* server.allocated = target.allocated = NULL; */ |
| 108 | |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 109 | 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 Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 112 | int got_clen = 0; /* got content-length: from server */ |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 113 | int output_fd = -1; |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 114 | int use_proxy = 1; /* Use proxies if env vars are set */ |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 115 | const char *proxy_flag = "on"; /* Use proxies if env vars are set */ |
| 116 | const char *user_agent = "Wget";/* Content of the "User-Agent" header field */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 117 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 118 | /* |
| 119 | * Crack command line. |
| 120 | */ |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 121 | enum { |
| 122 | WGET_OPT_CONTINUE = 0x1, |
| 123 | WGET_OPT_QUIET = 0x2, |
| 124 | WGET_OPT_OUTNAME = 0x4, |
| 125 | WGET_OPT_PREFIX = 0x8, |
| 126 | WGET_OPT_PROXY = 0x10, |
| 127 | WGET_OPT_USER_AGENT = 0x20, |
| 128 | WGET_OPT_PASSIVE = 0x40, |
| 129 | WGET_OPT_HEADER = 0x80, |
| 130 | }; |
Bernhard Reutner-Fischer | 289e86a | 2006-08-20 20:01:24 +0000 | [diff] [blame] | 131 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 132 | static const struct option wget_long_options[] = { |
| 133 | // name, has_arg, flag, val |
| 134 | { "continue", no_argument, NULL, 'c' }, |
| 135 | { "quiet", no_argument, NULL, 'q' }, |
| 136 | { "output-document", required_argument, NULL, 'O' }, |
| 137 | { "directory-prefix", required_argument, NULL, 'P' }, |
| 138 | { "proxy", required_argument, NULL, 'Y' }, |
| 139 | { "user-agent", required_argument, NULL, 'U' }, |
| 140 | { "passive-ftp", no_argument, NULL, 0xff }, |
| 141 | { "header", required_argument, NULL, 0xfe }, |
| 142 | { 0, 0, 0, 0 } |
Denis Vlasenko | 601ae13 | 2006-11-28 23:37:46 +0000 | [diff] [blame] | 143 | }; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 144 | applet_long_options = wget_long_options; |
Bernhard Reutner-Fischer | 8d3a6f7 | 2006-05-31 14:11:38 +0000 | [diff] [blame] | 145 | #endif |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 146 | opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::"); |
| 147 | opt = getopt32(argc, argv, "cqO:P:Y:U:", |
| 148 | &fname_out, &dir_prefix, |
| 149 | &proxy_flag, &user_agent |
| 150 | USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist) |
| 151 | ); |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 152 | if (strcmp(proxy_flag, "off") == 0) { |
Glenn L McGrath | 514aeab | 2003-12-19 12:08:56 +0000 | [diff] [blame] | 153 | /* Use the proxy if necessary. */ |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 154 | use_proxy = 0; |
Glenn L McGrath | 514aeab | 2003-12-19 12:08:56 +0000 | [diff] [blame] | 155 | } |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 156 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
Denis Vlasenko | 7534e08 | 2006-10-23 23:21:58 +0000 | [diff] [blame] | 157 | if (headers_llist) { |
| 158 | int size = 1; |
| 159 | char *cp; |
Denis Vlasenko | 54cf511 | 2007-02-17 18:11:45 +0000 | [diff] [blame] | 160 | llist_t *ll = headers_llist = llist_rev(headers_llist); |
Denis Vlasenko | 7534e08 | 2006-10-23 23:21:58 +0000 | [diff] [blame] | 161 | while (ll) { |
| 162 | size += strlen(ll->data) + 2; |
| 163 | ll = ll->link; |
| 164 | } |
| 165 | extra_headers = cp = xmalloc(size); |
Glenn L McGrath | 514aeab | 2003-12-19 12:08:56 +0000 | [diff] [blame] | 166 | while (headers_llist) { |
Denis Vlasenko | 7534e08 | 2006-10-23 23:21:58 +0000 | [diff] [blame] | 167 | cp += sprintf(cp, "%s\r\n", headers_llist->data); |
Glenn L McGrath | 514aeab | 2003-12-19 12:08:56 +0000 | [diff] [blame] | 168 | headers_llist = headers_llist->link; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 171 | #endif |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 172 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 173 | parse_url(argv[optind], &target); |
| 174 | server.host = target.host; |
| 175 | server.port = target.port; |
| 176 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 177 | /* |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 178 | * Use the proxy if necessary. |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 179 | */ |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 180 | if (use_proxy) { |
Robert Griebl | d776011 | 2002-05-14 23:36:45 +0000 | [diff] [blame] | 181 | proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy"); |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 182 | if (proxy && *proxy) { |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 183 | parse_url(proxy, &server); |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 184 | } else { |
| 185 | use_proxy = 0; |
| 186 | } |
Robert Griebl | d776011 | 2002-05-14 23:36:45 +0000 | [diff] [blame] | 187 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 188 | |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 189 | /* Guess an output filename */ |
| 190 | if (!fname_out) { |
Eric Andersen | 751750e | 2004-10-08 08:27:40 +0000 | [diff] [blame] | 191 | // Dirty hack. Needed because bb_get_last_path_component |
| 192 | // will destroy trailing / by storing '\0' in last byte! |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 193 | if (!last_char_is(target.path, '/')) { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 194 | fname_out = bb_get_last_path_component(target.path); |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 195 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 196 | curfile = fname_out; |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 197 | #endif |
Eric Andersen | 751750e | 2004-10-08 08:27:40 +0000 | [diff] [blame] | 198 | } |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 199 | if (!fname_out || !fname_out[0]) { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 200 | /* bb_get_last_path_component writes |
| 201 | * to last '/' only. We don't have one here... */ |
| 202 | fname_out = (char*)"index.html"; |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 203 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 204 | curfile = fname_out; |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 205 | #endif |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 206 | } |
Matt Kraai | 0382eb8 | 2001-07-19 19:13:55 +0000 | [diff] [blame] | 207 | if (dir_prefix != NULL) |
| 208 | fname_out = concat_path_file(dir_prefix, fname_out); |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 209 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 210 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 211 | curfile = bb_get_last_path_component(fname_out); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 212 | #endif |
| 213 | } |
Denis Vlasenko | 4e4662c | 2006-11-23 13:10:23 +0000 | [diff] [blame] | 214 | /* Impossible? |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 215 | if ((opt & WGET_OPT_CONTINUE) && !fname_out) |
Denis Vlasenko | 4e4662c | 2006-11-23 13:10:23 +0000 | [diff] [blame] | 216 | bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 217 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 218 | /* |
| 219 | * Determine where to start transfer. |
| 220 | */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 221 | if (LONE_DASH(fname_out)) { |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 222 | output_fd = 1; |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 223 | opt &= ~WGET_OPT_CONTINUE; |
Denis Vlasenko | 4e4662c | 2006-11-23 13:10:23 +0000 | [diff] [blame] | 224 | } |
| 225 | if (opt & WGET_OPT_CONTINUE) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 226 | output_fd = open(fname_out, O_WRONLY); |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 227 | if (output_fd >= 0) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 228 | beg_range = xlseek(output_fd, 0, SEEK_END); |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 229 | } |
| 230 | /* File doesn't exist. We do not create file here yet. |
| 231 | We are not sure it exists on remove side */ |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 234 | /* We want to do exactly _one_ DNS lookup, since some |
| 235 | * sites (i.e. ftp.us.debian.org) use round-robin DNS |
| 236 | * and we want to connect to only one IP... */ |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 237 | lsa = xhost2sockaddr(server.host, server.port); |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 238 | if (!(opt & WGET_OPT_QUIET)) { |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 239 | fprintf(stderr, "Connecting to %s (%s)\n", server.host, |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 240 | xmalloc_sockaddr2dotted(&lsa->sa, lsa->len)); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 241 | /* We leak result of xmalloc_sockaddr2dotted */ |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 244 | if (use_proxy || !target.is_ftp) { |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 245 | /* |
| 246 | * HTTP session |
| 247 | */ |
| 248 | do { |
Glenn L McGrath | e7bdfcc | 2003-08-28 22:03:19 +0000 | [diff] [blame] | 249 | got_clen = chunked = 0; |
| 250 | |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 251 | if (!--try) |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 252 | bb_error_msg_and_die("too many redirections"); |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 253 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 254 | /* |
| 255 | * Open socket to http server |
| 256 | */ |
| 257 | if (sfp) fclose(sfp); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 258 | sfp = open_socket(lsa); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 259 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 260 | /* |
| 261 | * Send HTTP request. |
| 262 | */ |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 263 | if (use_proxy) { |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 264 | fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n", |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 265 | target.is_ftp ? "f" : "ht", target.host, |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 266 | target.path); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 267 | } else { |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 268 | fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 269 | } |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 270 | |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 271 | fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n", |
| 272 | target.host, user_agent); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 274 | #if ENABLE_FEATURE_WGET_AUTHENTICATION |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 275 | if (target.user) { |
| 276 | fprintf(sfp, "Authorization: Basic %s\r\n", |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 277 | base64enc((unsigned char*)target.user, buf, sizeof(buf))); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 278 | } |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 279 | if (use_proxy && server.user) { |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 280 | fprintf(sfp, "Proxy-Authorization: Basic %s\r\n", |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 281 | base64enc((unsigned char*)server.user, buf, sizeof(buf))); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 282 | } |
| 283 | #endif |
| 284 | |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 285 | if (beg_range) |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 286 | fprintf(sfp, "Range: bytes=%"OFF_FMT"d-\r\n", beg_range); |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 287 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
Denis Vlasenko | 7534e08 | 2006-10-23 23:21:58 +0000 | [diff] [blame] | 288 | if (extra_headers) |
| 289 | fputs(extra_headers, sfp); |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 290 | #endif |
Denis Vlasenko | 7534e08 | 2006-10-23 23:21:58 +0000 | [diff] [blame] | 291 | fprintf(sfp, "Connection: close\r\n\r\n"); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 292 | |
| 293 | /* |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 294 | * Retrieve HTTP response line and check for "200" status code. |
| 295 | */ |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 296 | read_response: |
Glenn L McGrath | e7bdfcc | 2003-08-28 22:03:19 +0000 | [diff] [blame] | 297 | if (fgets(buf, sizeof(buf), sfp) == NULL) |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 298 | bb_error_msg_and_die("no response from server"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 299 | |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 300 | s = buf; |
| 301 | while (*s != '\0' && !isspace(*s)) ++s; |
Denis Vlasenko | d18a3a2 | 2006-10-25 12:46:03 +0000 | [diff] [blame] | 302 | s = skip_whitespace(s); |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 303 | // FIXME: no error check |
| 304 | // xatou wouldn't work: "200 OK" |
| 305 | status = atoi(s); |
| 306 | switch (status) { |
| 307 | case 0: |
| 308 | case 100: |
| 309 | while (gethdr(buf, sizeof(buf), sfp, &n) != NULL) |
| 310 | /* eat all remaining headers */; |
| 311 | goto read_response; |
| 312 | case 200: |
| 313 | break; |
| 314 | case 300: /* redirection */ |
| 315 | case 301: |
| 316 | case 302: |
| 317 | case 303: |
| 318 | break; |
| 319 | case 206: |
| 320 | if (beg_range) |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 321 | break; |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 322 | /*FALLTHRU*/ |
| 323 | default: |
Denis Vlasenko | 067e3f0 | 2006-11-10 23:25:53 +0000 | [diff] [blame] | 324 | /* Show first line only and kill any ESC tricks */ |
| 325 | buf[strcspn(buf, "\n\r\x1b")] = '\0'; |
| 326 | bb_error_msg_and_die("server returned error: %s", buf); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 327 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 328 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 329 | /* |
| 330 | * Retrieve HTTP headers. |
| 331 | */ |
| 332 | while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { |
| 333 | if (strcasecmp(buf, "content-length") == 0) { |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 334 | content_len = BB_STRTOOFF(s, NULL, 10); |
| 335 | if (errno || content_len < 0) { |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 336 | bb_error_msg_and_die("content-length %s is garbage", s); |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 337 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 338 | got_clen = 1; |
| 339 | continue; |
| 340 | } |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 341 | if (strcasecmp(buf, "transfer-encoding") == 0) { |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 342 | if (strcasecmp(s, "chunked") != 0) |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 343 | bb_error_msg_and_die("server wants to do %s transfer encoding", s); |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 344 | chunked = got_clen = 1; |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 345 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 346 | if (strcasecmp(buf, "location") == 0) { |
| 347 | if (s[0] == '/') |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 348 | /* free(target.allocated); */ |
| 349 | target.path = /* target.allocated = */ xstrdup(s+1); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 350 | else { |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 351 | parse_url(s, &target); |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 352 | if (use_proxy == 0) { |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 353 | server.host = target.host; |
| 354 | server.port = target.port; |
| 355 | } |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 356 | free(lsa); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 357 | lsa = xhost2sockaddr(server.host, server.port); |
Glenn L McGrath | 58a2e0e | 2004-01-17 23:07:14 +0000 | [diff] [blame] | 358 | break; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
Denis Vlasenko | 3469c18 | 2006-12-16 22:19:47 +0000 | [diff] [blame] | 362 | } while (status >= 300); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 363 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 364 | dfp = sfp; |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 365 | |
| 366 | } else { |
| 367 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 368 | /* |
| 369 | * FTP session |
| 370 | */ |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 371 | if (!target.user) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 372 | target.user = xstrdup("anonymous:busybox@"); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 373 | |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 374 | sfp = open_socket(lsa); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 375 | if (ftpcmd(NULL, NULL, sfp, buf) != 220) |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 376 | bb_error_msg_and_die("%s", buf+4); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 377 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 378 | /* |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 379 | * Splitting username:password pair, |
| 380 | * trying to log in |
| 381 | */ |
| 382 | s = strchr(target.user, ':'); |
| 383 | if (s) |
| 384 | *(s++) = '\0'; |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 385 | switch (ftpcmd("USER ", target.user, sfp, buf)) { |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 386 | case 230: |
| 387 | break; |
| 388 | case 331: |
| 389 | if (ftpcmd("PASS ", s, sfp, buf) == 230) |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 390 | break; |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 391 | /* FALLTHRU (failed login) */ |
| 392 | default: |
| 393 | bb_error_msg_and_die("ftp login: %s", buf+4); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 394 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 395 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 396 | ftpcmd("TYPE I", NULL, sfp, buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 397 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 398 | /* |
| 399 | * Querying file size |
| 400 | */ |
Rob Landley | af12cb3 | 2006-06-27 18:41:03 +0000 | [diff] [blame] | 401 | if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) { |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 402 | content_len = BB_STRTOOFF(buf+4, NULL, 10); |
| 403 | if (errno || content_len < 0) { |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 404 | bb_error_msg_and_die("SIZE value is garbage"); |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 405 | } |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 406 | got_clen = 1; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 407 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 408 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 409 | /* |
| 410 | * Entering passive mode |
| 411 | */ |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 412 | if (ftpcmd("PASV", NULL, sfp, buf) != 227) { |
| 413 | pasv_error: |
| 414 | bb_error_msg_and_die("bad response to %s: %s", "PASV", buf); |
| 415 | } |
Denis Vlasenko | f8c8bb1 | 2006-11-21 19:10:26 +0000 | [diff] [blame] | 416 | // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage] |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 417 | // Server's IP is N1.N2.N3.N4 (we ignore it) |
| 418 | // Server's port for data connection is P1*256+P2 |
Denis Vlasenko | 40f62a8 | 2006-11-21 11:04:31 +0000 | [diff] [blame] | 419 | s = strrchr(buf, ')'); |
Denis Vlasenko | f8c8bb1 | 2006-11-21 19:10:26 +0000 | [diff] [blame] | 420 | if (s) s[0] = '\0'; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 421 | s = strrchr(buf, ','); |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 422 | if (!s) goto pasv_error; |
Denis Vlasenko | f8c8bb1 | 2006-11-21 19:10:26 +0000 | [diff] [blame] | 423 | port = xatou_range(s+1, 0, 255); |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 424 | *s = '\0'; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 425 | s = strrchr(buf, ','); |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 426 | if (!s) goto pasv_error; |
Denis Vlasenko | f8c8bb1 | 2006-11-21 19:10:26 +0000 | [diff] [blame] | 427 | port += xatou_range(s+1, 0, 255) * 256; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 428 | set_nport(lsa, htons(port)); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 429 | dfp = open_socket(lsa); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 430 | |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 431 | if (beg_range) { |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 432 | sprintf(buf, "REST %"OFF_FMT"d", beg_range); |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 433 | if (ftpcmd(buf, NULL, sfp, buf) == 350) |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 434 | content_len -= beg_range; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 435 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 436 | |
Rob Landley | af12cb3 | 2006-06-27 18:41:03 +0000 | [diff] [blame] | 437 | if (ftpcmd("RETR ", target.path, sfp, buf) > 150) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 438 | bb_error_msg_and_die("bad response to RETR: %s", buf); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 441 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 442 | /* |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 443 | * Retrieve file |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 444 | */ |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 445 | if (chunked) { |
| 446 | fgets(buf, sizeof(buf), dfp); |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 447 | content_len = STRTOOFF(buf, NULL, 16); |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 448 | /* FIXME: error check?? */ |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 449 | } |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 450 | |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 451 | /* Do it before progressmeter (want to have nice error message) */ |
| 452 | if (output_fd < 0) |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame] | 453 | output_fd = xopen(fname_out, |
| 454 | O_WRONLY|O_CREAT|O_EXCL|O_TRUNC); |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 455 | |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 456 | if (!(opt & WGET_OPT_QUIET)) |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 457 | progressmeter(-1); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 458 | |
Mark Whitley | 30ac01c | 2001-04-17 18:13:16 +0000 | [diff] [blame] | 459 | do { |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 460 | while (content_len > 0 || !got_clen) { |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 461 | unsigned rdsz = sizeof(buf); |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 462 | if (content_len < sizeof(buf) && (chunked || got_clen)) |
| 463 | rdsz = (unsigned)content_len; |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 464 | n = safe_fread(buf, 1, rdsz, dfp); |
| 465 | if (n <= 0) |
| 466 | break; |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 467 | if (full_write(output_fd, buf, n) != n) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 468 | bb_perror_msg_and_die(bb_msg_write_error); |
Glenn L McGrath | 83e4a5b | 2003-08-28 21:55:22 +0000 | [diff] [blame] | 469 | } |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 470 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 471 | transferred += n; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 472 | #endif |
Glenn L McGrath | 83e4a5b | 2003-08-28 21:55:22 +0000 | [diff] [blame] | 473 | if (got_clen) { |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 474 | content_len -= n; |
Glenn L McGrath | 83e4a5b | 2003-08-28 21:55:22 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 477 | |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 478 | if (chunked) { |
Matt Kraai | 854125f | 2001-05-09 19:15:46 +0000 | [diff] [blame] | 479 | safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */ |
| 480 | safe_fgets(buf, sizeof(buf), dfp); |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 481 | content_len = STRTOOFF(buf, NULL, 16); |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 482 | /* FIXME: error check? */ |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 483 | if (content_len == 0) { |
Glenn L McGrath | 83e4a5b | 2003-08-28 21:55:22 +0000 | [diff] [blame] | 484 | chunked = 0; /* all done! */ |
| 485 | } |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Glenn L McGrath | 83e4a5b | 2003-08-28 21:55:22 +0000 | [diff] [blame] | 488 | if (n == 0 && ferror(dfp)) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 489 | bb_perror_msg_and_die(bb_msg_read_error); |
Glenn L McGrath | 83e4a5b | 2003-08-28 21:55:22 +0000 | [diff] [blame] | 490 | } |
Eric Andersen | 6d7fa43 | 2001-04-10 18:17:05 +0000 | [diff] [blame] | 491 | } while (chunked); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 492 | |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 493 | if (!(opt & WGET_OPT_QUIET)) |
Mark Whitley | 30ac01c | 2001-04-17 18:13:16 +0000 | [diff] [blame] | 494 | progressmeter(1); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 495 | |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 496 | if ((use_proxy == 0) && target.is_ftp) { |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 497 | fclose(dfp); |
| 498 | if (ftpcmd(NULL, NULL, sfp, buf) != 226) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 499 | bb_error_msg_and_die("ftp error: %s", buf+4); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 500 | ftpcmd("QUIT", NULL, sfp, buf); |
| 501 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 502 | exit(EXIT_SUCCESS); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 506 | static void parse_url(char *src_url, struct host_info *h) |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 507 | { |
Denis Vlasenko | 3469c18 | 2006-12-16 22:19:47 +0000 | [diff] [blame] | 508 | char *url, *p, *sp; |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 509 | |
| 510 | /* h->allocated = */ url = xstrdup(src_url); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 511 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 512 | if (strncmp(url, "http://", 7) == 0) { |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 513 | h->port = bb_lookup_port("http", "tcp", 80); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 514 | h->host = url + 7; |
| 515 | h->is_ftp = 0; |
| 516 | } else if (strncmp(url, "ftp://", 6) == 0) { |
Denis Vlasenko | a655152 | 2006-10-07 14:28:28 +0000 | [diff] [blame] | 517 | h->port = bb_lookup_port("ftp", "tcp", 21); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 518 | h->host = url + 6; |
| 519 | h->is_ftp = 1; |
| 520 | } else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 521 | bb_error_msg_and_die("not an http or ftp url: %s", url); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 522 | |
Denis Vlasenko | a655152 | 2006-10-07 14:28:28 +0000 | [diff] [blame] | 523 | // FYI: |
| 524 | // "Real" wget 'http://busybox.net?var=a/b' sends this request: |
| 525 | // 'GET /?var=a/b HTTP 1.0' |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 526 | // and saves 'index.html?var=a%2Fb' (we save 'b') |
Denis Vlasenko | a655152 | 2006-10-07 14:28:28 +0000 | [diff] [blame] | 527 | // wget 'http://busybox.net?login=john@doe': |
| 528 | // request: 'GET /?login=john@doe HTTP/1.0' |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 529 | // saves: 'index.html?login=john@doe' (we save '?login=john@doe') |
Denis Vlasenko | a655152 | 2006-10-07 14:28:28 +0000 | [diff] [blame] | 530 | // wget 'http://busybox.net#test/test': |
| 531 | // request: 'GET / HTTP/1.0' |
| 532 | // saves: 'index.html' (we save 'test') |
| 533 | // |
| 534 | // We also don't add unique .N suffix if file exists... |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 535 | sp = strchr(h->host, '/'); |
Denis Vlasenko | a655152 | 2006-10-07 14:28:28 +0000 | [diff] [blame] | 536 | p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p; |
| 537 | p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p; |
| 538 | if (!sp) { |
Denis Vlasenko | de55b5d | 2007-02-01 01:53:25 +0000 | [diff] [blame] | 539 | /* must be writable because of bb_get_last_path_component() */ |
Denis Vlasenko | afe488d | 2007-01-28 16:07:45 +0000 | [diff] [blame] | 540 | static char nullstr[] = ""; |
| 541 | h->path = nullstr; |
Denis Vlasenko | a655152 | 2006-10-07 14:28:28 +0000 | [diff] [blame] | 542 | } else if (*sp == '/') { |
Denis Vlasenko | 3469c18 | 2006-12-16 22:19:47 +0000 | [diff] [blame] | 543 | *sp = '\0'; |
| 544 | h->path = sp + 1; |
Denis Vlasenko | a655152 | 2006-10-07 14:28:28 +0000 | [diff] [blame] | 545 | } else { // '#' or '?' |
| 546 | // http://busybox.net?login=john@doe is a valid URL |
| 547 | // memmove converts to: |
| 548 | // http:/busybox.nett?login=john@doe... |
| 549 | memmove(h->host-1, h->host, sp - h->host); |
| 550 | h->host--; |
| 551 | sp[-1] = '\0'; |
| 552 | h->path = sp; |
| 553 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 554 | |
Denis Vlasenko | 3469c18 | 2006-12-16 22:19:47 +0000 | [diff] [blame] | 555 | sp = strrchr(h->host, '@'); |
| 556 | h->user = NULL; |
| 557 | if (sp != NULL) { |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 558 | h->user = h->host; |
Denis Vlasenko | 3469c18 | 2006-12-16 22:19:47 +0000 | [diff] [blame] | 559 | *sp = '\0'; |
| 560 | h->host = sp + 1; |
| 561 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 562 | |
Denis Vlasenko | 3469c18 | 2006-12-16 22:19:47 +0000 | [diff] [blame] | 563 | sp = h->host; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 567 | static FILE *open_socket(len_and_sockaddr *lsa) |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 568 | { |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 569 | FILE *fp; |
| 570 | |
Denis Vlasenko | 067e3f0 | 2006-11-10 23:25:53 +0000 | [diff] [blame] | 571 | /* glibc 2.4 seems to try seeking on it - ??! */ |
| 572 | /* hopefully it understands what ESPIPE means... */ |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 573 | fp = fdopen(xconnect_stream(lsa), "r+"); |
Glenn L McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 574 | if (fp == NULL) |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 575 | bb_perror_msg_and_die("fdopen"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 576 | |
| 577 | return fp; |
| 578 | } |
| 579 | |
| 580 | |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 581 | static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc) |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 582 | { |
| 583 | char *s, *hdrval; |
| 584 | int c; |
| 585 | |
| 586 | *istrunc = 0; |
| 587 | |
| 588 | /* retrieve header line */ |
| 589 | if (fgets(buf, bufsiz, fp) == NULL) |
| 590 | return NULL; |
| 591 | |
| 592 | /* see if we are at the end of the headers */ |
Denis Vlasenko | 40f62a8 | 2006-11-21 11:04:31 +0000 | [diff] [blame] | 593 | for (s = buf; *s == '\r'; ++s) |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 594 | ; |
| 595 | if (s[0] == '\n') |
| 596 | return NULL; |
| 597 | |
| 598 | /* convert the header name to lower case */ |
Denis Vlasenko | 40f62a8 | 2006-11-21 11:04:31 +0000 | [diff] [blame] | 599 | for (s = buf; isalnum(*s) || *s == '-'; ++s) |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 600 | *s = tolower(*s); |
| 601 | |
| 602 | /* verify we are at the end of the header name */ |
| 603 | if (*s != ':') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 604 | bb_error_msg_and_die("bad header line: %s", buf); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 605 | |
| 606 | /* locate the start of the header value */ |
Denis Vlasenko | 40f62a8 | 2006-11-21 11:04:31 +0000 | [diff] [blame] | 607 | for (*s++ = '\0'; *s == ' ' || *s == '\t'; ++s) |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 608 | ; |
| 609 | hdrval = s; |
| 610 | |
| 611 | /* locate the end of header */ |
| 612 | while (*s != '\0' && *s != '\r' && *s != '\n') |
| 613 | ++s; |
| 614 | |
| 615 | /* end of header found */ |
| 616 | if (*s != '\0') { |
| 617 | *s = '\0'; |
| 618 | return hdrval; |
| 619 | } |
| 620 | |
Eric Andersen | 5d63884 | 2000-09-14 21:46:30 +0000 | [diff] [blame] | 621 | /* Rats! The buffer isn't big enough to hold the entire header value. */ |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 622 | while (c = getc(fp), c != EOF && c != '\n') |
| 623 | ; |
| 624 | *istrunc = 1; |
| 625 | return hdrval; |
| 626 | } |
| 627 | |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 628 | static int ftpcmd(const char *s1, const char *s2, FILE *fp, char *buf) |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 629 | { |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 630 | int result; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 631 | if (s1) { |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 632 | if (!s2) s2 = ""; |
Eric Andersen | 3f1cf45 | 2003-03-11 18:03:39 +0000 | [diff] [blame] | 633 | fprintf(fp, "%s%s\r\n", s1, s2); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 634 | fflush(fp); |
| 635 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 636 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 637 | do { |
Glenn L McGrath | 32da885 | 2004-04-08 10:27:11 +0000 | [diff] [blame] | 638 | char *buf_ptr; |
| 639 | |
| 640 | if (fgets(buf, 510, fp) == NULL) { |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 641 | bb_perror_msg_and_die("error getting response"); |
Glenn L McGrath | 32da885 | 2004-04-08 10:27:11 +0000 | [diff] [blame] | 642 | } |
| 643 | buf_ptr = strstr(buf, "\r\n"); |
| 644 | if (buf_ptr) { |
| 645 | *buf_ptr = '\0'; |
| 646 | } |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 647 | } while (!isdigit(buf[0]) || buf[3] != ' '); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 648 | |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 649 | buf[3] = '\0'; |
| 650 | result = xatoi_u(buf); |
| 651 | buf[3] = ' '; |
| 652 | return result; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 655 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 656 | /* Stuff below is from BSD rcp util.c, as added to openshh. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 657 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 658 | */ |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 659 | static int |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 660 | getttywidth(void) |
| 661 | { |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 662 | int width; |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 663 | get_terminal_width_height(0, &width, NULL); |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 664 | return width; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 667 | static void |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 668 | updateprogressmeter(int ignore) |
| 669 | { |
| 670 | int save_errno = errno; |
| 671 | |
| 672 | progressmeter(0); |
| 673 | errno = save_errno; |
| 674 | } |
| 675 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 676 | static void alarmtimer(int iwait) |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 677 | { |
| 678 | struct itimerval itv; |
| 679 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 680 | itv.it_value.tv_sec = iwait; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 681 | itv.it_value.tv_usec = 0; |
| 682 | itv.it_interval = itv.it_value; |
| 683 | setitimer(ITIMER_REAL, &itv, NULL); |
| 684 | } |
| 685 | |
| 686 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 687 | static void |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 688 | progressmeter(int flag) |
| 689 | { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 690 | static struct timeval lastupdate; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 691 | static off_t lastsize, totalsize; |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 692 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 693 | struct timeval now, td, tvwait; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 694 | off_t abbrevsize; |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 695 | int elapsed, ratio, barlength, i; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 696 | char buf[256]; |
| 697 | |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 698 | if (flag == -1) { /* first call to progressmeter */ |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 699 | gettimeofday(&start, (struct timezone *) 0); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 700 | lastupdate = start; |
| 701 | lastsize = 0; |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 702 | totalsize = content_len + beg_range; /* as content_len changes.. */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 705 | gettimeofday(&now, (struct timezone *) 0); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 706 | ratio = 100; |
Mark Whitley | 30ac01c | 2001-04-17 18:13:16 +0000 | [diff] [blame] | 707 | if (totalsize != 0 && !chunked) { |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 708 | /* long long helps to have working ETA even if !LFS */ |
| 709 | ratio = (int) (100 * (unsigned long long)(transferred+beg_range) / totalsize); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 710 | ratio = MIN(ratio, 100); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 711 | } |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 712 | |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 713 | fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 714 | |
| 715 | barlength = getttywidth() - 51; |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 716 | if (barlength > 0 && barlength < sizeof(buf)) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 717 | i = barlength * ratio / 100; |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 718 | memset(buf, '*', i); |
| 719 | memset(buf + i, ' ', barlength - i); |
| 720 | buf[barlength] = '\0'; |
| 721 | fprintf(stderr, "|%s|", buf); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 722 | } |
| 723 | i = 0; |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 724 | abbrevsize = transferred + beg_range; |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 725 | while (abbrevsize >= 100000) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 726 | i++; |
| 727 | abbrevsize >>= 10; |
| 728 | } |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 729 | /* see http://en.wikipedia.org/wiki/Tera */ |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 730 | fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' '); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 731 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 732 | timersub(&now, &lastupdate, &tvwait); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 733 | if (transferred > lastsize) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 734 | lastupdate = now; |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 735 | lastsize = transferred; |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 736 | if (tvwait.tv_sec >= STALLTIME) |
| 737 | timeradd(&start, &tvwait, &start); |
| 738 | tvwait.tv_sec = 0; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 739 | } |
| 740 | timersub(&now, &start, &td); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 741 | elapsed = td.tv_sec; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 742 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 743 | if (tvwait.tv_sec >= STALLTIME) { |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 744 | fprintf(stderr, " - stalled -"); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 745 | } else { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 746 | off_t to_download = totalsize - beg_range; |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 747 | if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) { |
| 748 | fprintf(stderr, "--:--:-- ETA"); |
| 749 | } else { |
| 750 | /* to_download / (transferred/elapsed) - elapsed: */ |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 751 | int eta = (int) ((unsigned long long)to_download*elapsed/transferred - elapsed); |
| 752 | /* (long long helps to have working ETA even if !LFS) */ |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 753 | i = eta % 3600; |
| 754 | fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60); |
| 755 | } |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 756 | } |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 757 | |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 758 | if (flag == -1) { /* first call to progressmeter */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 759 | struct sigaction sa; |
| 760 | sa.sa_handler = updateprogressmeter; |
| 761 | sigemptyset(&sa.sa_mask); |
| 762 | sa.sa_flags = SA_RESTART; |
| 763 | sigaction(SIGALRM, &sa, NULL); |
| 764 | alarmtimer(1); |
Denis Vlasenko | f8aa109 | 2006-10-01 10:58:54 +0000 | [diff] [blame] | 765 | } else if (flag == 1) { /* last call to progressmeter */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 766 | alarmtimer(0); |
Rob Landley | 19a3940 | 2006-06-13 17:10:26 +0000 | [diff] [blame] | 767 | transferred = 0; |
Mark Whitley | 30ac01c | 2001-04-17 18:13:16 +0000 | [diff] [blame] | 768 | putc('\n', stderr); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | #endif |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 772 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 773 | /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff, |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 774 | * much of which was blatantly stolen from openssh. */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 775 | |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 776 | /*- |
| 777 | * Copyright (c) 1992, 1993 |
| 778 | * The Regents of the University of California. All rights reserved. |
| 779 | * |
| 780 | * Redistribution and use in source and binary forms, with or without |
| 781 | * modification, are permitted provided that the following conditions |
| 782 | * are met: |
| 783 | * 1. Redistributions of source code must retain the above copyright |
| 784 | * notice, this list of conditions and the following disclaimer. |
| 785 | * 2. Redistributions in binary form must reproduce the above copyright |
| 786 | * notice, this list of conditions and the following disclaimer in the |
| 787 | * documentation and/or other materials provided with the distribution. |
| 788 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 789 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 790 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 791 | * |
| 792 | * 4. Neither the name of the University nor the names of its contributors |
| 793 | * may be used to endorse or promote products derived from this software |
| 794 | * without specific prior written permission. |
| 795 | * |
| 796 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 797 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 798 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 799 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 800 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 801 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 802 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 803 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 804 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 805 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 806 | * SUCH DAMAGE. |
| 807 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 808 | */ |