"When the filesize is known from content-length header, safe_fread is
authorGlenn L McGrath <bug1@ihug.co.nz>
Thu, 28 Aug 2003 21:55:22 +0000 (21:55 -0000)
committerGlenn L McGrath <bug1@ihug.co.nz>
Thu, 28 Aug 2003 21:55:22 +0000 (21:55 -0000)
always told to read sizeof(buf).  This waits until the underlying
fread() to time-out for the last part of the downloaded body.  Fix
this by sending the number of remaining bytes to read when known." -
junkio@
I reworked the logic in his patch

networking/wget.c

index a9ead7f..586a7e0 100644 (file)
@@ -481,25 +481,30 @@ read_response:            if (fgets(buf, sizeof(buf), sfp) == NULL)
                progressmeter(-1);
 #endif
        do {
-               while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, chunked ? (filesize > sizeof(buf) ? sizeof(buf) : filesize) : sizeof(buf), dfp)) > 0) {
-                       if (safe_fwrite(buf, 1, n, output) != n)
+               while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, (chunked || !got_clen || (filesize > sizeof(buf)) ? sizeof(buf) : filesize), dfp)) > 0) {
+                       if (safe_fwrite(buf, 1, n, output) != n) {
                                bb_perror_msg_and_die("write error");
+                       }
 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
-               statbytes+=n;
+                       statbytes+=n;
 #endif
-               if (got_clen)
-                       filesize -= n;
-       }
+                       if (got_clen) {
+                               filesize -= n;
+                       }
+               }
 
                if (chunked) {
                        safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
                        safe_fgets(buf, sizeof(buf), dfp);
                        filesize = strtol(buf, (char **) NULL, 16);
-                       if (filesize==0) chunked = 0; /* all done! */
+                       if (filesize==0) {
+                               chunked = 0; /* all done! */
+                       }
                }
 
-       if (n == 0 && ferror(dfp))
-               bb_perror_msg_and_die("network read error");
+               if (n == 0 && ferror(dfp)) {
+                       bb_perror_msg_and_die("network read error");
+               }
        } while (chunked);
 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
        if (quiet_flag==FALSE)
@@ -811,7 +816,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.54 2003/07/22 08:56:51 andersen Exp $
+ *     $Id: wget.c,v 1.55 2003/08/28 21:55:22 bug1 Exp $
  */
 
 
@@ -823,6 +828,3 @@ c-basic-offset: 4
 tab-width: 4
 End:
 */
-
-
-