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