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