Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * wget - retrieve a file using HTTP |
| 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 | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 7 | * Note: According to RFC2616 section 3.6.1, "All HTTP/1.1 applications MUST be |
| 8 | * able to receive and decode the "chunked" transfer-coding, and MUST ignore |
| 9 | * chunk-extension extensions they do not understand." |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 10 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 11 | * This prevents this particular wget app from completely RFC compliant, and as |
| 12 | * such, prevents it from being used as a general purpose web browser... This |
| 13 | * is a design decision, since it makes the code smaller. |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 14 | * |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
Eric Andersen | dff9d54 | 2001-01-26 02:04:49 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
| 22 | #include <ctype.h> |
| 23 | #include <string.h> |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <signal.h> |
| 26 | #include <sys/ioctl.h> |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 27 | |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 28 | #include <sys/time.h> |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <netinet/in.h> |
| 33 | #include <arpa/inet.h> |
| 34 | #include <netdb.h> |
| 35 | |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 36 | /* Stupid libc5 doesn't define this... */ |
| 37 | #ifndef timersub |
| 38 | #define timersub(a, b, result) \ |
| 39 | do { \ |
| 40 | (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ |
| 41 | (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ |
| 42 | if ((result)->tv_usec < 0) { \ |
| 43 | --(result)->tv_sec; \ |
| 44 | (result)->tv_usec += 1000000; \ |
| 45 | } \ |
| 46 | } while (0) |
| 47 | #endif |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 48 | |
| 49 | void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path); |
| 50 | FILE *open_socket(char *host, int port); |
| 51 | char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 52 | void progressmeter(int flag); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 53 | |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 54 | /* Globals (can be accessed from signal handlers */ |
| 55 | static off_t filesize = 0; /* content-length of the file */ |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 56 | #ifdef BB_FEATURE_WGET_STATUSBAR |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 57 | static char *curfile; /* Name of current file being transferred. */ |
| 58 | static struct timeval start; /* Time a transfer started. */ |
| 59 | volatile unsigned long statbytes; /* Number of bytes transferred so far. */ |
| 60 | /* For progressmeter() -- number of seconds before xfer considered "stalled" */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 61 | static const int STALLTIME = 5; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 62 | #endif |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 63 | |
| 64 | void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue) |
| 65 | { |
| 66 | if (output != stdout && do_continue==0) { |
| 67 | fclose(output); |
| 68 | unlink(fname_out); |
| 69 | } |
| 70 | } |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 71 | |
| 72 | int wget_main(int argc, char **argv) |
| 73 | { |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 74 | int n; |
| 75 | char *proxy, *proxy_host; |
| 76 | int uri_port, proxy_port; |
| 77 | char *s, buf[512]; |
| 78 | struct stat sbuf; |
| 79 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 80 | FILE *sfp; /* socket to web server */ |
| 81 | char *uri_host, *uri_path; /* parsed from command line url */ |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 82 | char *fname_out = NULL; /* where to direct output (-O) */ |
| 83 | int do_continue = 0; /* continue a prev transfer (-c) */ |
| 84 | long beg_range = 0L; /* range at which continue begins */ |
| 85 | int got_clen = 0; /* got content-length: from server */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 86 | FILE *output; /* socket to web server */ |
| 87 | int quiet_flag = FALSE; /* Be verry, verry quiet... */ |
| 88 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 89 | /* |
| 90 | * Crack command line. |
| 91 | */ |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 92 | while ((n = getopt(argc, argv, "cqO:")) != EOF) { |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 93 | switch (n) { |
| 94 | case 'c': |
| 95 | ++do_continue; |
| 96 | break; |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 97 | case 'q': |
| 98 | quiet_flag = TRUE; |
| 99 | break; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 100 | case 'O': |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 101 | /* can't set fname_out to NULL if outputting to stdout, because |
| 102 | * this gets interpreted as the auto-gen output filename |
| 103 | * case below - tausq@debian.org |
| 104 | */ |
| 105 | fname_out = (strcmp(optarg, "-") == 0 ? (char *)1 : optarg); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | usage(wget_usage); |
| 109 | } |
| 110 | } |
Eric Andersen | 25b669c | 2000-10-02 23:19:38 +0000 | [diff] [blame] | 111 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 112 | if (argc - optind != 1) |
| 113 | usage(wget_usage); |
Eric Andersen | 25b669c | 2000-10-02 23:19:38 +0000 | [diff] [blame] | 114 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 115 | /* |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 116 | * Use the proxy if necessary. |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 117 | */ |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 118 | if ((proxy = getenv("http_proxy")) != NULL) { |
| 119 | proxy = xstrdup(proxy); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 120 | parse_url(proxy, &proxy_host, &proxy_port, &uri_path); |
| 121 | parse_url(argv[optind], &uri_host, &uri_port, &uri_path); |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 122 | } else { |
| 123 | /* |
| 124 | * Parse url into components. |
| 125 | */ |
| 126 | parse_url(argv[optind], &uri_host, &uri_port, &uri_path); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 127 | proxy_host=uri_host; |
| 128 | proxy_port=uri_port; |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 129 | } |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 130 | |
| 131 | /* Guess an output filename */ |
| 132 | if (!fname_out) { |
| 133 | fname_out = |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 134 | #ifdef BB_FEATURE_WGET_STATUSBAR |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 135 | curfile = |
| 136 | #endif |
| 137 | get_last_path_component(uri_path); |
| 138 | if (fname_out==NULL || strlen(fname_out)<1) { |
| 139 | fname_out = |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 140 | #ifdef BB_FEATURE_WGET_STATUSBAR |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 141 | curfile = |
| 142 | #endif |
| 143 | "index.html"; |
| 144 | } |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 145 | #ifdef BB_FEATURE_WGET_STATUSBAR |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 146 | } else { |
| 147 | curfile=argv[optind]; |
| 148 | #endif |
| 149 | } |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 150 | if (do_continue && !fname_out) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 151 | error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 152 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Open socket to server. |
| 156 | */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 157 | sfp = open_socket(proxy_host, proxy_port); |
| 158 | |
| 159 | /* Make the assumption that if the file already exists |
| 160 | * on disk that the intention is to continue downloading |
| 161 | * a previously aborted download -Erik */ |
| 162 | if (stat(fname_out, &sbuf) == 0) { |
| 163 | ++do_continue; |
| 164 | } |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 167 | * Open the output file stream. |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 168 | */ |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 169 | if (fname_out != (char *)1) { |
Eric Andersen | 79e898a | 2001-01-31 17:49:47 +0000 | [diff] [blame] | 170 | output = xfopen( fname_out, (do_continue ? "a" : "w") ); |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 171 | } else { |
| 172 | output = stdout; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Determine where to start transfer. |
| 177 | */ |
| 178 | if (do_continue) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 179 | if (fstat(fileno(output), &sbuf) < 0) |
Matt Kraai | 0dab829 | 2000-12-18 03:08:29 +0000 | [diff] [blame] | 180 | perror_msg_and_die("fstat()"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 181 | if (sbuf.st_size > 0) |
| 182 | beg_range = sbuf.st_size; |
| 183 | else |
| 184 | do_continue = 0; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Send HTTP request. |
| 189 | */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 190 | fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n", |
| 191 | uri_host, uri_port, uri_path); |
| 192 | fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host); |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 193 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 194 | if (do_continue) |
| 195 | fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 196 | fprintf(sfp,"Connection: close\r\n\r\n"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Retrieve HTTP response line and check for "200" status code. |
| 200 | */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 201 | if (fgets(buf, sizeof(buf), sfp) == NULL) { |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 202 | close_and_delete_outfile(output, fname_out, do_continue); |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 203 | error_msg_and_die("no response from server"); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 204 | } |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 205 | for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) |
| 206 | ; |
| 207 | for ( ; isspace(*s) ; ++s) |
| 208 | ; |
| 209 | switch (atoi(s)) { |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 210 | case 0: |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 211 | case 200: |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 212 | break; |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 213 | case 206: |
| 214 | if (do_continue) |
| 215 | break; |
| 216 | /*FALLTHRU*/ |
| 217 | default: |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 218 | close_and_delete_outfile(output, fname_out, do_continue); |
| 219 | error_msg_and_die("server returned error %d: %s", atoi(s), buf); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Retrieve HTTP headers. |
| 224 | */ |
| 225 | while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 226 | if (strcasecmp(buf, "content-length") == 0) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 227 | filesize = atol(s); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 228 | got_clen = 1; |
| 229 | continue; |
| 230 | } |
Eric Andersen | 7d69701 | 2001-01-24 20:28:35 +0000 | [diff] [blame] | 231 | if (strcasecmp(buf, "transfer-encoding") == 0) { |
| 232 | close_and_delete_outfile(output, fname_out, do_continue); |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 233 | error_msg_and_die("server wants to do %s transfer encoding", s); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 234 | continue; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Retrieve HTTP body. |
| 240 | */ |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 241 | #ifdef BB_FEATURE_WGET_STATUSBAR |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 242 | statbytes=0; |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 243 | if (quiet_flag==FALSE) |
| 244 | progressmeter(-1); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 245 | #endif |
| 246 | while (filesize > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) { |
| 247 | fwrite(buf, 1, n, output); |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 248 | #ifdef BB_FEATURE_WGET_STATUSBAR |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 249 | statbytes+=n; |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 250 | if (quiet_flag==FALSE) |
| 251 | progressmeter(1); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 252 | #endif |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 253 | if (got_clen) |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 254 | filesize -= n; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 255 | } |
| 256 | if (n == 0 && ferror(sfp)) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 257 | perror_msg_and_die("network read error"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 258 | |
| 259 | exit(0); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path) |
| 264 | { |
Matt Kraai | a9711a5 | 2001-01-03 16:15:15 +0000 | [diff] [blame] | 265 | char *cp, *sp; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 266 | |
| 267 | *uri_port = 80; |
| 268 | |
| 269 | if (strncmp(url, "http://", 7) != 0) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 270 | error_msg_and_die("not an http url: %s", url); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 271 | |
Matt Kraai | a9711a5 | 2001-01-03 16:15:15 +0000 | [diff] [blame] | 272 | *uri_host = url + 7; |
| 273 | |
| 274 | cp = strchr(*uri_host, ':'); |
| 275 | sp = strchr(*uri_host, '/'); |
| 276 | |
| 277 | if (cp != NULL && (sp == NULL || cp < sp)) { |
| 278 | *cp++ = '\0'; |
| 279 | *uri_port = atoi(cp); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 280 | } |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 281 | |
Matt Kraai | a9711a5 | 2001-01-03 16:15:15 +0000 | [diff] [blame] | 282 | if (sp != NULL) { |
| 283 | *sp++ = '\0'; |
| 284 | *uri_path = sp; |
| 285 | } else |
| 286 | *uri_path = ""; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
| 290 | FILE *open_socket(char *host, int port) |
| 291 | { |
| 292 | struct sockaddr_in sin; |
| 293 | struct hostent *hp; |
| 294 | int fd; |
| 295 | FILE *fp; |
| 296 | |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 297 | memset(&sin, 0, sizeof(sin)); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 298 | sin.sin_family = AF_INET; |
| 299 | if ((hp = (struct hostent *) gethostbyname(host)) == NULL) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 300 | error_msg_and_die("cannot resolve %s", host); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 301 | memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length); |
| 302 | sin.sin_port = htons(port); |
| 303 | |
| 304 | /* |
| 305 | * Get the server onto a stdio stream. |
| 306 | */ |
| 307 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 308 | perror_msg_and_die("socket()"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 309 | if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 310 | perror_msg_and_die("connect(%s)", host); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 311 | if ((fp = fdopen(fd, "r+")) == NULL) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 312 | perror_msg_and_die("fdopen()"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 313 | |
| 314 | return fp; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc) |
| 319 | { |
| 320 | char *s, *hdrval; |
| 321 | int c; |
| 322 | |
| 323 | *istrunc = 0; |
| 324 | |
| 325 | /* retrieve header line */ |
| 326 | if (fgets(buf, bufsiz, fp) == NULL) |
| 327 | return NULL; |
| 328 | |
| 329 | /* see if we are at the end of the headers */ |
| 330 | for (s = buf ; *s == '\r' ; ++s) |
| 331 | ; |
| 332 | if (s[0] == '\n') |
| 333 | return NULL; |
| 334 | |
| 335 | /* convert the header name to lower case */ |
| 336 | for (s = buf ; isalnum(*s) || *s == '-' ; ++s) |
| 337 | *s = tolower(*s); |
| 338 | |
| 339 | /* verify we are at the end of the header name */ |
| 340 | if (*s != ':') |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 341 | error_msg_and_die("bad header line: %s", buf); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 342 | |
| 343 | /* locate the start of the header value */ |
| 344 | for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s) |
| 345 | ; |
| 346 | hdrval = s; |
| 347 | |
| 348 | /* locate the end of header */ |
| 349 | while (*s != '\0' && *s != '\r' && *s != '\n') |
| 350 | ++s; |
| 351 | |
| 352 | /* end of header found */ |
| 353 | if (*s != '\0') { |
| 354 | *s = '\0'; |
| 355 | return hdrval; |
| 356 | } |
| 357 | |
Eric Andersen | 5d63884 | 2000-09-14 21:46:30 +0000 | [diff] [blame] | 358 | /* 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] | 359 | while (c = getc(fp), c != EOF && c != '\n') |
| 360 | ; |
| 361 | *istrunc = 1; |
| 362 | return hdrval; |
| 363 | } |
| 364 | |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 365 | #ifdef BB_FEATURE_WGET_STATUSBAR |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 366 | /* Stuff below is from BSD rcp util.c, as added to openshh. |
| 367 | * Original copyright notice is retained at the end of this file. |
| 368 | * |
| 369 | */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 370 | |
| 371 | |
| 372 | int |
| 373 | getttywidth(void) |
| 374 | { |
| 375 | struct winsize winsize; |
| 376 | |
| 377 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) |
| 378 | return (winsize.ws_col ? winsize.ws_col : 80); |
| 379 | else |
| 380 | return (80); |
| 381 | } |
| 382 | |
| 383 | void |
| 384 | updateprogressmeter(int ignore) |
| 385 | { |
| 386 | int save_errno = errno; |
| 387 | |
| 388 | progressmeter(0); |
| 389 | errno = save_errno; |
| 390 | } |
| 391 | |
| 392 | void |
| 393 | alarmtimer(int wait) |
| 394 | { |
| 395 | struct itimerval itv; |
| 396 | |
| 397 | itv.it_value.tv_sec = wait; |
| 398 | itv.it_value.tv_usec = 0; |
| 399 | itv.it_interval = itv.it_value; |
| 400 | setitimer(ITIMER_REAL, &itv, NULL); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | void |
| 405 | progressmeter(int flag) |
| 406 | { |
| 407 | static const char prefixes[] = " KMGTP"; |
| 408 | static struct timeval lastupdate; |
| 409 | static off_t lastsize; |
| 410 | struct timeval now, td, wait; |
| 411 | off_t cursize, abbrevsize; |
| 412 | double elapsed; |
| 413 | int ratio, barlength, i, remaining; |
| 414 | char buf[256]; |
| 415 | |
| 416 | if (flag == -1) { |
| 417 | (void) gettimeofday(&start, (struct timezone *) 0); |
| 418 | lastupdate = start; |
| 419 | lastsize = 0; |
| 420 | } |
| 421 | |
| 422 | (void) gettimeofday(&now, (struct timezone *) 0); |
| 423 | cursize = statbytes; |
| 424 | if (filesize != 0) { |
| 425 | ratio = 100.0 * cursize / filesize; |
| 426 | ratio = MAX(ratio, 0); |
| 427 | ratio = MIN(ratio, 100); |
| 428 | } else |
| 429 | ratio = 100; |
| 430 | |
| 431 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); |
| 432 | |
| 433 | barlength = getttywidth() - 51; |
| 434 | if (barlength > 0) { |
| 435 | i = barlength * ratio / 100; |
| 436 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 437 | "|%.*s%*s|", i, |
| 438 | "*****************************************************************************" |
| 439 | "*****************************************************************************", |
| 440 | barlength - i, ""); |
| 441 | } |
| 442 | i = 0; |
| 443 | abbrevsize = cursize; |
| 444 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { |
| 445 | i++; |
| 446 | abbrevsize >>= 10; |
| 447 | } |
| 448 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ", |
| 449 | (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : |
| 450 | 'B'); |
| 451 | |
| 452 | timersub(&now, &lastupdate, &wait); |
| 453 | if (cursize > lastsize) { |
| 454 | lastupdate = now; |
| 455 | lastsize = cursize; |
| 456 | if (wait.tv_sec >= STALLTIME) { |
| 457 | start.tv_sec += wait.tv_sec; |
| 458 | start.tv_usec += wait.tv_usec; |
| 459 | } |
| 460 | wait.tv_sec = 0; |
| 461 | } |
| 462 | timersub(&now, &start, &td); |
| 463 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); |
| 464 | |
| 465 | if (statbytes <= 0 || elapsed <= 0.0 || cursize > filesize) { |
| 466 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 467 | " --:-- ETA"); |
| 468 | } else if (wait.tv_sec >= STALLTIME) { |
| 469 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 470 | " - stalled -"); |
| 471 | } else { |
| 472 | remaining = (int) (filesize / (statbytes / elapsed) - elapsed); |
| 473 | i = remaining / 3600; |
| 474 | if (i) |
| 475 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 476 | "%2d:", i); |
| 477 | else |
| 478 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 479 | " "); |
| 480 | i = remaining % 3600; |
| 481 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 482 | "%02d:%02d ETA", i / 60, i % 60); |
| 483 | } |
Randolph Chung | da7b829 | 2000-12-07 03:55:35 +0000 | [diff] [blame] | 484 | write(fileno(stderr), buf, strlen(buf)); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 485 | |
| 486 | if (flag == -1) { |
| 487 | struct sigaction sa; |
| 488 | sa.sa_handler = updateprogressmeter; |
| 489 | sigemptyset(&sa.sa_mask); |
| 490 | sa.sa_flags = SA_RESTART; |
| 491 | sigaction(SIGALRM, &sa, NULL); |
| 492 | alarmtimer(1); |
| 493 | } else if (flag == 1) { |
| 494 | alarmtimer(0); |
| 495 | statbytes = 0; |
| 496 | } |
| 497 | } |
| 498 | #endif |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 499 | |
Eric Andersen | 370fb08 | 2001-01-20 20:07:00 +0000 | [diff] [blame] | 500 | /* Original copyright notice which applies to the BB_FEATURE_WGET_STATUSBAR stuff, |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 501 | * much of which was blatently stolen from openssh. */ |
| 502 | |
| 503 | /*- |
| 504 | * Copyright (c) 1992, 1993 |
| 505 | * The Regents of the University of California. All rights reserved. |
| 506 | * |
| 507 | * Redistribution and use in source and binary forms, with or without |
| 508 | * modification, are permitted provided that the following conditions |
| 509 | * are met: |
| 510 | * 1. Redistributions of source code must retain the above copyright |
| 511 | * notice, this list of conditions and the following disclaimer. |
| 512 | * 2. Redistributions in binary form must reproduce the above copyright |
| 513 | * notice, this list of conditions and the following disclaimer in the |
| 514 | * documentation and/or other materials provided with the distribution. |
| 515 | * |
| 516 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 517 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
| 518 | * |
| 519 | * 4. Neither the name of the University nor the names of its contributors |
| 520 | * may be used to endorse or promote products derived from this software |
| 521 | * without specific prior written permission. |
| 522 | * |
| 523 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 524 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 525 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 526 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 527 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 528 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 529 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 530 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 531 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 532 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 533 | * SUCH DAMAGE. |
| 534 | * |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 535 | * $Id: wget.c,v 1.25 2001/01/31 19:00:21 kraai Exp $ |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 536 | */ |
| 537 | |
| 538 | |
| 539 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 540 | /* |
| 541 | Local Variables: |
| 542 | c-file-style: "linux" |
| 543 | c-basic-offset: 4 |
| 544 | tab-width: 4 |
| 545 | End: |
| 546 | */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 547 | |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 548 | |
| 549 | |