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