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