02adc55207b8cd8802ce2dd78f647f4fab7b51d8
[oweals/busybox.git] / networking / wget.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * wget - retrieve a file using HTTP
4  *
5  * Chip Rosenthal
6  * Covad Communications
7  * <chip@laserlink.net>
8  *
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...
14  *
15  * This is a design decision, since it makes the code smaller.
16  *
17  */
18
19 #include "busybox.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <sys/ioctl.h>
28
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36
37
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);
42
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" */
50 #define STALLTIME       5
51 #endif
52
53 int wget_main(int argc, char **argv)
54 {
55         FILE *sfp;                                      /* socket to web server                         */
56         char *uri_host, *uri_path;      /* parsed from command line url         */
57         int uri_port;
58         char *s, buf[512];
59         int n;
60
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                         */
66
67         /*
68          * Crack command line.
69          */
70         while ((n = getopt(argc, argv, "cO:")) != EOF) {
71                 switch (n) {
72                 case 'c':
73                         ++do_continue;
74                         break;
75                 case 'O':
76                         fname_out = (strcmp(optarg, "-") == 0 ? NULL : optarg);
77                         break;
78                 default:
79                         usage(wget_usage);
80                 }
81         }
82
83         if (argc - optind != 1)
84                         usage(wget_usage);
85
86         /* Guess an output filename */
87         if (!fname_out) {
88                 fname_out = 
89 #ifdef BB_FEATURE_STATUSBAR
90                         curfile = 
91 #endif
92                         get_last_path_component(argv[optind]);
93 #ifdef BB_FEATURE_STATUSBAR
94         } else {
95                 curfile=argv[optind];
96 #endif
97         }
98
99
100         if (do_continue && !fname_out)
101                 fatalError("wget: cannot specify continue (-c) without a filename (-O)\n");
102         /*
103          * Parse url into components.
104          */
105         parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
106
107         /*
108          * Open socket to server.
109          */
110         sfp = open_socket(uri_host, uri_port);
111
112         /*
113          * Open the output stream.
114          */
115         if (fname_out != NULL) {
116                 if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) 
117                                 == NULL)
118                         fatalError("wget: freopen(%s): %s\n", fname_out, strerror(errno));
119         } else { 
120                 output=stdout;
121         }
122
123         /*
124          * Determine where to start transfer.
125          */
126         if (do_continue) {
127                 struct stat sbuf;
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;
132                 else
133                         do_continue = 0;
134         }
135
136         /*
137          * Send HTTP request.
138          */
139         fprintf(sfp, "GET %s HTTP/1.1\r\nHost: %s\r\n", uri_path, uri_host);
140         if (do_continue)
141                 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
142         fputs("Connection: close\r\n\r\n", sfp);
143
144         /*
145          * Retrieve HTTP response line and check for "200" status code.
146          */
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)
150                 ;
151         for ( ; isspace(*s) ; ++s)
152                 ;
153         switch (atoi(s)) {
154                 case 200:
155                         if (!do_continue)
156                                 break;
157                         fatalError("wget: server does not support ranges\n");
158                 case 206:
159                         if (do_continue)
160                                 break;
161                         /*FALLTHRU*/
162                 default:
163                         fatalError("wget: server returned error: %s", buf);
164         }
165
166         /*
167          * Retrieve HTTP headers.
168          */
169         while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
170                 if (strcmp(buf, "content-length") == 0) {
171                         filesize = atol(s);
172                         got_clen = 1;
173                         continue;
174                 }
175                 if (strcmp(buf, "transfer-encoding") == 0) {
176                         fatalError("wget: server wants to do %s transfer encoding\n", s);
177                         continue;
178                 }
179         }
180
181         /*
182          * Retrieve HTTP body.
183          */
184 #ifdef BB_FEATURE_STATUSBAR
185         statbytes=0;
186         progressmeter(-1);
187 #endif
188         while (filesize > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) {
189                 fwrite(buf, 1, n, output);
190 #ifdef BB_FEATURE_STATUSBAR
191                 statbytes+=n;
192                 progressmeter(1);
193 #endif
194                 if (got_clen)
195                         filesize -= n;
196         }
197         if (n == 0 && ferror(sfp))
198                 fatalError("wget: network read error: %s", strerror(errno));
199
200         exit(0);
201 }
202
203
204 void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
205 {
206         char *s, *h;
207
208         *uri_port = 80;
209
210         if (strncmp(url, "http://", 7) != 0)
211                 fatalError("wget: not an http url: %s\n", url);
212
213         /* pull the host portion to the front of the buffer */
214         for (s = url, h = url+7 ; *h != '/' ; ++h) {
215                 if (*h == '\0')
216                         fatalError("wget: cannot parse url: %s\n", url);
217                 if (*h == ':') {
218                         *uri_port = atoi(h+1);
219                         *h = '\0';
220                 }
221                 *s++ = *h;
222         }
223         *s = '\0';
224         *uri_host = url;
225         *uri_path = h;
226 }
227
228
229 FILE *open_socket(char *host, int port)
230 {
231         struct sockaddr_in sin;
232         struct hostent *hp;
233         int fd;
234         FILE *fp;
235
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);
242
243         /*
244          * Get the server onto a stdio stream.
245          */
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));
252
253         return fp;
254 }
255
256
257 char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
258 {
259         char *s, *hdrval;
260         int c;
261
262         *istrunc = 0;
263
264         /* retrieve header line */
265         if (fgets(buf, bufsiz, fp) == NULL)
266                 return NULL;
267
268         /* see if we are at the end of the headers */
269         for (s = buf ; *s == '\r' ; ++s)
270                 ;
271         if (s[0] == '\n')
272                 return NULL;
273
274         /* convert the header name to lower case */
275         for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
276                 *s = tolower(*s);
277
278         /* verify we are at the end of the header name */
279         if (*s != ':')
280                 fatalError("wget: bad header line: %s\n", buf);
281
282         /* locate the start of the header value */
283         for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
284                 ;
285         hdrval = s;
286
287         /* locate the end of header */
288         while (*s != '\0' && *s != '\r' && *s != '\n')
289                 ++s;
290
291         /* end of header found */
292         if (*s != '\0') {
293                 *s = '\0';
294                 return hdrval;
295         }
296
297         /* Rats!  The buffer isn't big enough to hold the entire header value. */
298         while (c = getc(fp), c != EOF && c != '\n')
299                 ;
300         *istrunc = 1;
301         return hdrval;
302 }
303
304 #ifdef BB_FEATURE_STATUSBAR
305 /* Stuff below is from BSD rcp util.c, as added to openshh. */
306
307 /*-
308  * Copyright (c) 1992, 1993
309  *      The Regents of the University of California.  All rights reserved.
310  *
311  * Redistribution and use in source and binary forms, with or without
312  * modification, are permitted provided that the following conditions
313  * are met:
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.
326  *
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
337  * SUCH DAMAGE.
338  *
339  *      $Id: wget.c,v 1.5 2000/10/03 00:21:45 andersen Exp $
340  */
341
342
343 int
344 getttywidth(void)
345 {
346         struct winsize winsize;
347
348         if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
349                 return (winsize.ws_col ? winsize.ws_col : 80);
350         else
351                 return (80);
352 }
353
354 void
355 updateprogressmeter(int ignore)
356 {
357         int save_errno = errno;
358
359         progressmeter(0);
360         errno = save_errno;
361 }
362
363 void
364 alarmtimer(int wait)
365 {
366         struct itimerval itv;
367
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);
372 }
373
374
375 void
376 progressmeter(int flag)
377 {
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;
383         double elapsed;
384         int ratio, barlength, i, remaining;
385         char buf[256];
386
387         if (flag == -1) {
388                 (void) gettimeofday(&start, (struct timezone *) 0);
389                 lastupdate = start;
390                 lastsize = 0;
391         }
392
393         (void) gettimeofday(&now, (struct timezone *) 0);
394         cursize = statbytes;
395         if (filesize != 0) {
396                 ratio = 100.0 * cursize / filesize;
397                 ratio = MAX(ratio, 0);
398                 ratio = MIN(ratio, 100);
399         } else
400                 ratio = 100;
401
402         snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
403
404         barlength = getttywidth() - 51;
405         if (barlength > 0) {
406                 i = barlength * ratio / 100;
407                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
408                          "|%.*s%*s|", i,
409                          "*****************************************************************************"
410                          "*****************************************************************************",
411                          barlength - i, "");
412         }
413         i = 0;
414         abbrevsize = cursize;
415         while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
416                 i++;
417                 abbrevsize >>= 10;
418         }
419         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
420              (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
421                  'B');
422
423         timersub(&now, &lastupdate, &wait);
424         if (cursize > lastsize) {
425                 lastupdate = now;
426                 lastsize = cursize;
427                 if (wait.tv_sec >= STALLTIME) {
428                         start.tv_sec += wait.tv_sec;
429                         start.tv_usec += wait.tv_usec;
430                 }
431                 wait.tv_sec = 0;
432         }
433         timersub(&now, &start, &td);
434         elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
435
436         if (statbytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
437                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
438                          "   --:-- ETA");
439         } else if (wait.tv_sec >= STALLTIME) {
440                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
441                          " - stalled -");
442         } else {
443                 remaining = (int) (filesize / (statbytes / elapsed) - elapsed);
444                 i = remaining / 3600;
445                 if (i)
446                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
447                                  "%2d:", i);
448                 else
449                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
450                                  "   ");
451                 i = remaining % 3600;
452                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
453                          "%02d:%02d ETA", i / 60, i % 60);
454         }
455         write(fileno(stdout), buf, strlen(buf));
456
457         if (flag == -1) {
458                 struct sigaction sa;
459                 sa.sa_handler = updateprogressmeter;
460                 sigemptyset(&sa.sa_mask);
461                 sa.sa_flags = SA_RESTART;
462                 sigaction(SIGALRM, &sa, NULL);
463                 alarmtimer(1);
464         } else if (flag == 1) {
465                 alarmtimer(0);
466                 statbytes = 0;
467         }
468 }
469 #endif
470 /*
471 Local Variables:
472 c-file-style: "linux"
473 c-basic-offset: 4
474 tab-width: 4
475 End:
476 */
477