libbb.h now includes ../busybox.h. This way, files in libbb that have
[oweals/busybox.git] / wget.c
diff --git a/wget.c b/wget.c
index 5bec0ddd5a918dd9d5a31a981e665ee0b56395bd..8261d9cb626c4a1f0718499cf631ecc7bf233e38 100644 (file)
--- a/wget.c
+++ b/wget.c
@@ -4,14 +4,6 @@
  *
  * Chip Rosenthal Covad Communications <chip@laserlink.net>
  *
- * Note: According to RFC2616 section 3.6.1, "All HTTP/1.1 applications MUST be
- * able to receive and decode the "chunked" transfer-coding, and MUST ignore
- * chunk-extension extensions they do not understand."  
- *
- * This prevents this particular wget app from completely RFC compliant, and as
- * such, prevents it from being used as a general purpose web browser...  This
- * is a design decision, since it makes the code smaller.
- *
  */
 
 #include <stdio.h>
@@ -63,10 +55,11 @@ static void progressmeter(int flag);
 
 /* Globals (can be accessed from signal handlers */
 static off_t filesize = 0;             /* content-length of the file */
+static int chunked = 0;                        /* chunked transfer encoding */
 #ifdef BB_FEATURE_WGET_STATUSBAR
 static char *curfile;                  /* Name of current file being transferred. */
 static struct timeval start;   /* Time a transfer started. */
-static volatile unsigned long statbytes; /* Number of bytes transferred so far. */
+static volatile unsigned long statbytes = 0; /* Number of bytes transferred so far. */
 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
 static const int STALLTIME = 5;
 #endif
@@ -148,7 +141,7 @@ int wget_main(int argc, char **argv)
                         * this gets interpreted as the auto-gen output filename
                         * case below  - tausq@debian.org
                         */
-                       fname_out = (strcmp(optarg, "-") == 0 ? (char *)1 : optarg);
+                       fname_out = optarg;
                        break;
                default:
                        show_usage();
@@ -195,10 +188,10 @@ int wget_main(int argc, char **argv)
        /*
         * Open the output file stream.
         */
-       if (fname_out != (char *)1) {
-               output = xfopen( fname_out, (do_continue ? "a" : "w") );
-       } else {
+       if (strcmp(fname_out, "-") == 0) {
                output = stdout;
+       } else {
+               output = xfopen(fname_out, (do_continue ? "a" : "w"));
        }
 
        /*
@@ -231,11 +224,11 @@ int wget_main(int argc, char **argv)
                         * Send HTTP request.
                         */
                        if (proxy) {
-                               fprintf(sfp, "GET %stp://%s:%d/%s HTTP/1.0\r\n", 
+                               fprintf(sfp, "GET %stp://%s:%d/%s HTTP/1.1\r\n",
                                        target.is_ftp ? "f" : "ht", target.host,
                                        target.port, target.path);
                        } else {
-                               fprintf(sfp, "GET /%s HTTP/1.0\r\n", target.path);
+                               fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
                        }
 
                        fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", target.host);
@@ -258,7 +251,7 @@ int wget_main(int argc, char **argv)
                        /*
                        * Retrieve HTTP response line and check for "200" status code.
                        */
-                       if (fgets(buf, sizeof(buf), sfp) == NULL)
+read_response:         if (fgets(buf, sizeof(buf), sfp) == NULL)
                                close_delete_and_die("no response from server");
                                
                        for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
@@ -267,6 +260,9 @@ int wget_main(int argc, char **argv)
                        ;
                        switch (status = atoi(s)) {
                                case 0:
+                               case 100:
+                                       while (gethdr(buf, sizeof(buf), sfp, &n) != NULL);
+                                       goto read_response;
                                case 200:
                                        if (do_continue && output != stdout)
                                                output = freopen(fname_out, "w", output);
@@ -295,9 +291,13 @@ int wget_main(int argc, char **argv)
                                        got_clen = 1;
                                        continue;
                                }
-                               if (strcasecmp(buf, "transfer-encoding") == 0)
+                               if (strcasecmp(buf, "transfer-encoding") == 0) {
+                                       if (strcasecmp(s, "chunked") == 0) {
+                                               chunked = got_clen = 1;
+                                       } else {
                                        close_delete_and_die("server wants to do %s transfer encoding", s);
-
+                                       }
+                               }
                                if (strcasecmp(buf, "location") == 0) {
                                        if (s[0] == '/')
                                                target.path = xstrdup(s+1);
@@ -386,32 +386,44 @@ int wget_main(int argc, char **argv)
        /*
         * Retrieve file
         */
+       if (chunked) {
+               fgets(buf, sizeof(buf), dfp);
+               filesize = strtol(buf, (char **) NULL, 16);
+       }
 #ifdef BB_FEATURE_WGET_STATUSBAR
-       statbytes=0;
        if (quiet_flag==FALSE)
                progressmeter(-1);
 #endif
-       while ((filesize > 0 || !got_clen) && (n = fread(buf, 1, sizeof(buf), dfp)) > 0) {
+       do {
+               while ((filesize > 0 || !got_clen) && (n = fread(buf, 1, chunked ? (filesize > sizeof(buf) ? sizeof(buf) : filesize) : sizeof(buf), dfp)) > 0) {
                fwrite(buf, 1, n, output);
 #ifdef BB_FEATURE_WGET_STATUSBAR
                statbytes+=n;
-               if (quiet_flag==FALSE)
-                       progressmeter(1);
 #endif
                if (got_clen)
                        filesize -= n;
        }
 
+               if (chunked) {
+                       fgets(buf, sizeof(buf), dfp); /* This is a newline */
+                       fgets(buf, sizeof(buf), dfp);
+                       filesize = strtol(buf, (char **) NULL, 16);
+                       if (filesize==0) chunked = 0; /* all done! */
+               }
+
        if (n == 0 && ferror(dfp))
                perror_msg_and_die("network read error");
-
+       } while (chunked);
+#ifdef BB_FEATURE_WGET_STATUSBAR
+       if (quiet_flag==FALSE)
+               progressmeter(1);
+#endif
        if (!proxy && target.is_ftp) {
                fclose(dfp);
                if (ftpcmd(NULL, NULL, sfp, buf) != 226)
                        error_msg_and_die("ftp error: %s", buf+4);
                ftpcmd("QUIT", NULL, sfp, buf);
        }
-
        exit(EXIT_SUCCESS);
 }
 
@@ -593,7 +605,7 @@ progressmeter(int flag)
 {
        static const char prefixes[] = " KMGTP";
        static struct timeval lastupdate;
-       static off_t lastsize;
+       static off_t lastsize, totalsize;
        struct timeval now, td, wait;
        off_t cursize, abbrevsize;
        double elapsed;
@@ -604,12 +616,13 @@ progressmeter(int flag)
                (void) gettimeofday(&start, (struct timezone *) 0);
                lastupdate = start;
                lastsize = 0;
+               totalsize = filesize; /* as filesize changes.. */
        }
 
        (void) gettimeofday(&now, (struct timezone *) 0);
        cursize = statbytes;
-       if (filesize != 0) {
-               ratio = 100.0 * cursize / filesize;
+       if (totalsize != 0 && !chunked) {
+               ratio = 100.0 * cursize / totalsize;
                ratio = MAX(ratio, 0);
                ratio = MIN(ratio, 100);
        } else
@@ -649,14 +662,14 @@ progressmeter(int flag)
        timersub(&now, &start, &td);
        elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
 
-       if (statbytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
-               snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
-                        "   --:-- ETA");
-       } else if (wait.tv_sec >= STALLTIME) {
+       if (wait.tv_sec >= STALLTIME) {
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
                         " - stalled -");
+       } else if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalsize || chunked) {
+               snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                        "   --:-- ETA");
        } else {
-               remaining = (int) (filesize / (statbytes / elapsed) - elapsed);
+               remaining = (int) (totalsize / (statbytes / elapsed) - elapsed);
                i = remaining / 3600;
                if (i)
                        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
@@ -680,6 +693,7 @@ progressmeter(int flag)
        } else if (flag == 1) {
                alarmtimer(0);
                statbytes = 0;
+               putc('\n', stderr);
        }
 }
 #endif
@@ -719,7 +733,7 @@ progressmeter(int flag)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *     $Id: wget.c,v 1.31 2001/04/05 21:45:53 andersen Exp $
+ *     $Id: wget.c,v 1.36 2001/04/17 18:13:16 markw Exp $
  */