wok view busybox/stuff/busybox-1.18-wget.u @ rev 8913

busybox: wget speedup
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Feb 28 15:05:05 2011 +0100 (2011-02-28)
parents
children
line source
1 --- busybox-1.18.3/networking/wget.c
2 +++ busybox-1.18.3-wget/networking/wget.c
3 @@ -446,7 +446,7 @@ static FILE* prepare_ftp_session(FILE **
5 static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
6 {
7 - char buf[512];
8 + char buf[4*1024]; /* made bigger to speed up local xfers */
9 #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
10 # if ENABLE_FEATURE_WGET_TIMEOUT
11 unsigned second_cnt;
12 @@ -455,7 +455,6 @@ static void NOINLINE retrieve_file_data(
14 polldata.fd = fileno(dfp);
15 polldata.events = POLLIN | POLLPRI;
16 - ndelay_on(polldata.fd);
17 #endif
18 progress_meter(PROGRESS_START);
20 @@ -464,6 +463,10 @@ static void NOINLINE retrieve_file_data(
22 /* Loops only if chunked */
23 while (1) {
24 +
25 +#if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
26 + ndelay_on(polldata.fd);
27 +#endif
28 while (1) {
29 int n;
30 unsigned rdsz;
31 @@ -493,22 +496,46 @@ static void NOINLINE retrieve_file_data(
32 progress_meter(PROGRESS_BUMP);
33 }
34 #endif
35 + /* fread internally uses read loop, which in our case
36 + * is usually exited when we get EAGAIN.
37 + * In this case, libc sets error marker on the stream.
38 + * Need to clear it before next fread to avoid possible
39 + * rare false positive ferror below. Rare because usually
40 + * fread gets more than zero bytes, and we don't fall
41 + * into if (n <= 0) ...
42 + */
43 + clearerr(dfp);
44 + errno = 0;
45 n = safe_fread(buf, rdsz, dfp);
46 + /* man fread:
47 + * If error occurs, or EOF is reached, the return value
48 + * is a short item count (or zero).
49 + * fread does not distinguish between EOF and error.
50 + */
51 if (n <= 0) {
52 - if (ferror(dfp)) {
53 - /* perror will not work: ferror doesn't set errno */
54 - bb_error_msg_and_die(bb_msg_read_error);
55 - }
56 - break;
57 +#if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
58 + if (errno == EAGAIN) /* poll lied, there is no data? */
59 + continue; /* yes */
60 +#endif
61 + if (ferror(dfp))
62 + bb_perror_msg_and_die(bb_msg_read_error);
63 + break; /* EOF, not error */
64 }
65 +
66 xwrite(output_fd, buf, n);
67 #if ENABLE_FEATURE_WGET_STATUSBAR
68 G.transferred += n;
69 progress_meter(PROGRESS_BUMP);
70 #endif
71 - if (G.got_clen)
72 + if (G.got_clen) {
73 G.content_len -= n;
74 + if (G.content_len == 0)
75 + break;
76 + }
77 }
78 +#if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
79 + ndelay_off(polldata.fd);
80 +#endif
82 if (!G.chunked)
83 break;
84 @@ -706,6 +733,11 @@ int wget_main(int argc UNUSED_PARAM, cha
85 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n",
86 target.host, user_agent);
88 + /* Ask server to close the connection as soon as we are done
89 + * (IOW: we do not intend to send more requests)
90 + */
91 + fprintf(sfp, "Connection: close\r\n");
92 +
93 #if ENABLE_FEATURE_WGET_AUTHENTICATION
94 if (target.user) {
95 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n"+6,
96 @@ -719,22 +751,25 @@ int wget_main(int argc UNUSED_PARAM, cha
98 if (G.beg_range)
99 fprintf(sfp, "Range: bytes=%"OFF_FMT"u-\r\n", G.beg_range);
100 +
101 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
102 if (extra_headers)
103 fputs(extra_headers, sfp);
105 if (opt & WGET_OPT_POST_DATA) {
106 char *estr = URL_escape(post_data);
107 - fprintf(sfp, "Content-Type: application/x-www-form-urlencoded\r\n");
108 - fprintf(sfp, "Content-Length: %u\r\n" "\r\n" "%s",
109 - (int) strlen(estr), estr);
110 - /*fprintf(sfp, "Connection: Keep-Alive\r\n\r\n");*/
111 - /*fprintf(sfp, "%s\r\n", estr);*/
112 + fprintf(sfp,
113 + "Content-Type: application/x-www-form-urlencoded\r\n"
114 + "Content-Length: %u\r\n"
115 + "\r\n"
116 + "%s",
117 + (int) strlen(estr), estr
118 + );
119 free(estr);
120 } else
121 #endif
122 - { /* If "Connection:" is needed, document why */
123 - fprintf(sfp, /* "Connection: close\r\n" */ "\r\n");
124 + {
125 + fprintf(sfp, "\r\n");
126 }
128 fflush(sfp);