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