1 /* vi: set sw=4 ts=4: */
3 * wget - retrieve a file using HTTP
9 * Note: According to RFC2616 section 3.6.1, "All HTTP/1.1 applications
10 * MUST be able to receive and decode the "chunked" transfer-coding,
11 * and MUST ignore chunk-extension extensions they do not understand."
12 * This prevents this particular wget app from completely RFC compliant,
13 * and as such, prevents it from being used as a general purpose web browser...
15 * This is a design decision, since it makes the code smaller.
27 #include <sys/ioctl.h>
30 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
38 void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path);
39 FILE *open_socket(char *host, int port);
40 char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
41 void progressmeter(int flag);
43 /* Globals (can be accessed from signal handlers */
44 static off_t filesize = 0; /* content-length of the file */
45 #ifdef BB_FEATURE_STATUSBAR
46 static char *curfile; /* Name of current file being transferred. */
47 static struct timeval start; /* Time a transfer started. */
48 volatile unsigned long statbytes; /* Number of bytes transferred so far. */
49 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
53 int wget_main(int argc, char **argv)
55 FILE *sfp; /* socket to web server */
56 char *uri_host, *uri_path; /* parsed from command line url */
61 char *fname_out = NULL; /* where to direct output (-O) */
62 int do_continue = 0; /* continue a prev transfer (-c) */
63 long beg_range = 0L; /* range at which continue begins */
64 int got_clen = 0; /* got content-length: from server */
65 FILE *output; /* socket to web server */
70 while ((n = getopt(argc, argv, "cO:")) != EOF) {
76 fname_out = (strcmp(optarg, "-") == 0 ? NULL : optarg);
83 if (argc - optind != 1)
86 /* Guess an output filename */
89 #ifdef BB_FEATURE_STATUSBAR
92 get_last_path_component(argv[optind]);
93 #ifdef BB_FEATURE_STATUSBAR
100 if (do_continue && !fname_out)
101 fatalError("wget: cannot specify continue (-c) without a filename (-O)\n");
103 * Parse url into components.
105 parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
108 * Open socket to server.
110 sfp = open_socket(uri_host, uri_port);
113 * Open the output stream.
115 if (fname_out != NULL) {
116 if ( (output=fopen(fname_out, (do_continue ? "a" : "w")))
118 fatalError("wget: freopen(%s): %s\n", fname_out, strerror(errno));
124 * Determine where to start transfer.
128 if (fstat(fileno(output), &sbuf) < 0)
129 fatalError("wget: fstat(): %s\n", strerror(errno));
130 if (sbuf.st_size > 0)
131 beg_range = sbuf.st_size;
139 fprintf(sfp, "GET %s HTTP/1.1\r\nHost: %s\r\n", uri_path, uri_host);
141 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
142 fputs("Connection: close\r\n\r\n", sfp);
145 * Retrieve HTTP response line and check for "200" status code.
147 if (fgets(buf, sizeof(buf), sfp) == NULL)
148 fatalError("wget: no response from server\n");
149 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
151 for ( ; isspace(*s) ; ++s)
157 fatalError("wget: server does not support ranges\n");
163 fatalError("wget: server returned error: %s", buf);
167 * Retrieve HTTP headers.
169 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
170 if (strcmp(buf, "content-length") == 0) {
175 if (strcmp(buf, "transfer-encoding") == 0) {
176 fatalError("wget: server wants to do %s transfer encoding\n", s);
182 * Retrieve HTTP body.
184 #ifdef BB_FEATURE_STATUSBAR
188 while (filesize > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) {
189 fwrite(buf, 1, n, output);
190 #ifdef BB_FEATURE_STATUSBAR
197 if (n == 0 && ferror(sfp))
198 fatalError("wget: network read error: %s", strerror(errno));
204 void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
210 if (strncmp(url, "http://", 7) != 0)
211 fatalError("wget: not an http url: %s\n", url);
213 /* pull the host portion to the front of the buffer */
214 for (s = url, h = url+7 ; *h != '/' ; ++h) {
216 fatalError("wget: cannot parse url: %s\n", url);
218 *uri_port = atoi(h+1);
229 FILE *open_socket(char *host, int port)
231 struct sockaddr_in sin;
236 memzero(&sin, sizeof(sin));
237 sin.sin_family = AF_INET;
238 if ((hp = (struct hostent *) gethostbyname(host)) == NULL)
239 fatalError("wget: cannot resolve %s\n", host);
240 memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length);
241 sin.sin_port = htons(port);
244 * Get the server onto a stdio stream.
246 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
247 fatalError("wget: socket(): %s\n", strerror(errno));
248 if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0)
249 fatalError("wget: connect(%s): %s\n", host, strerror(errno));
250 if ((fp = fdopen(fd, "r+")) == NULL)
251 fatalError("wget: fdopen(): %s\n", strerror(errno));
257 char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
264 /* retrieve header line */
265 if (fgets(buf, bufsiz, fp) == NULL)
268 /* see if we are at the end of the headers */
269 for (s = buf ; *s == '\r' ; ++s)
274 /* convert the header name to lower case */
275 for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
278 /* verify we are at the end of the header name */
280 fatalError("wget: bad header line: %s\n", buf);
282 /* locate the start of the header value */
283 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
287 /* locate the end of header */
288 while (*s != '\0' && *s != '\r' && *s != '\n')
291 /* end of header found */
297 /* Rats! The buffer isn't big enough to hold the entire header value. */
298 while (c = getc(fp), c != EOF && c != '\n')
304 #ifdef BB_FEATURE_STATUSBAR
305 /* Stuff below is from BSD rcp util.c, as added to openshh. */
308 * Copyright (c) 1992, 1993
309 * The Regents of the University of California. All rights reserved.
311 * Redistribution and use in source and binary forms, with or without
312 * modification, are permitted provided that the following conditions
314 * 1. Redistributions of source code must retain the above copyright
315 * notice, this list of conditions and the following disclaimer.
316 * 2. Redistributions in binary form must reproduce the above copyright
317 * notice, this list of conditions and the following disclaimer in the
318 * documentation and/or other materials provided with the distribution.
319 * 3. All advertising materials mentioning features or use of this software
320 * must display the following acknowledgement:
321 * This product includes software developed by the University of
322 * California, Berkeley and its contributors.
323 * 4. Neither the name of the University nor the names of its contributors
324 * may be used to endorse or promote products derived from this software
325 * without specific prior written permission.
327 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
328 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
329 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
330 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
331 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
332 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
333 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
334 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
335 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
336 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
339 * $Id: wget.c,v 1.5 2000/10/03 00:21:45 andersen Exp $
346 struct winsize winsize;
348 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
349 return (winsize.ws_col ? winsize.ws_col : 80);
355 updateprogressmeter(int ignore)
357 int save_errno = errno;
366 struct itimerval itv;
368 itv.it_value.tv_sec = wait;
369 itv.it_value.tv_usec = 0;
370 itv.it_interval = itv.it_value;
371 setitimer(ITIMER_REAL, &itv, NULL);
376 progressmeter(int flag)
378 static const char prefixes[] = " KMGTP";
379 static struct timeval lastupdate;
380 static off_t lastsize;
381 struct timeval now, td, wait;
382 off_t cursize, abbrevsize;
384 int ratio, barlength, i, remaining;
388 (void) gettimeofday(&start, (struct timezone *) 0);
393 (void) gettimeofday(&now, (struct timezone *) 0);
396 ratio = 100.0 * cursize / filesize;
397 ratio = MAX(ratio, 0);
398 ratio = MIN(ratio, 100);
402 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
404 barlength = getttywidth() - 51;
406 i = barlength * ratio / 100;
407 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
409 "*****************************************************************************"
410 "*****************************************************************************",
414 abbrevsize = cursize;
415 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
419 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
420 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
423 timersub(&now, &lastupdate, &wait);
424 if (cursize > lastsize) {
427 if (wait.tv_sec >= STALLTIME) {
428 start.tv_sec += wait.tv_sec;
429 start.tv_usec += wait.tv_usec;
433 timersub(&now, &start, &td);
434 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
436 if (statbytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
437 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
439 } else if (wait.tv_sec >= STALLTIME) {
440 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
443 remaining = (int) (filesize / (statbytes / elapsed) - elapsed);
444 i = remaining / 3600;
446 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
449 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
451 i = remaining % 3600;
452 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
453 "%02d:%02d ETA", i / 60, i % 60);
455 write(fileno(stdout), buf, strlen(buf));
459 sa.sa_handler = updateprogressmeter;
460 sigemptyset(&sa.sa_mask);
461 sa.sa_flags = SA_RESTART;
462 sigaction(SIGALRM, &sa, NULL);
464 } else if (flag == 1) {
472 c-file-style: "linux"