aboutsummaryrefslogtreecommitdiff
path: root/networking/wget.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2011-02-10 23:02:28 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2011-02-10 23:02:28 +0100
commitf836f01cc3eb39e5e4c7186f50b456b57fae2010 (patch)
tree146e03c1f78155a9a5b4c3d8c7d1091653a7bdee /networking/wget.c
parent805aa9fec923109e90c87eda2f116ee2fa5fe962 (diff)
downloadbusybox-w32-f836f01cc3eb39e5e4c7186f50b456b57fae2010.tar.gz
busybox-w32-f836f01cc3eb39e5e4c7186f50b456b57fae2010.tar.bz2
busybox-w32-f836f01cc3eb39e5e4c7186f50b456b57fae2010.zip
wget: shrink progress meter code; strink wget and add debug logging
function old new delta fgets_and_trim - 73 +73 retrieve_file_data 367 349 -18 bb_progress_update 723 699 -24 wget_main 2220 2190 -30 ftpcmd 133 87 -46 gethdr 200 153 -47 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/5 up/down: 73/-165) Total: -92 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/wget.c')
-rw-r--r--networking/wget.c74
1 files changed, 40 insertions, 34 deletions
diff --git a/networking/wget.c b/networking/wget.c
index 931882fde..d81426e8d 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -10,6 +10,10 @@
10 */ 10 */
11#include "libbb.h" 11#include "libbb.h"
12 12
13//#define log_io(...) bb_error_msg(__VA_ARGS__)
14#define log_io(...) ((void)0)
15
16
13struct host_info { 17struct host_info {
14 // May be used if we ever will want to free() all xstrdup()s... 18 // May be used if we ever will want to free() all xstrdup()s...
15 /* char *allocated; */ 19 /* char *allocated; */
@@ -197,25 +201,39 @@ static FILE *open_socket(len_and_sockaddr *lsa)
197 return fp; 201 return fp;
198} 202}
199 203
204/* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */
205static char fgets_and_trim(FILE *fp)
206{
207 char c;
208 char *buf_ptr;
209
210 if (fgets(G.wget_buf, sizeof(G.wget_buf) - 1, fp) == NULL)
211 bb_perror_msg_and_die("error getting response");
212
213 buf_ptr = strchrnul(G.wget_buf, '\n');
214 c = *buf_ptr;
215 *buf_ptr = '\0';
216 buf_ptr = strchrnul(G.wget_buf, '\r');
217 *buf_ptr = '\0';
218
219 log_io("< %s", G.wget_buf);
220
221 return c;
222}
223
200static int ftpcmd(const char *s1, const char *s2, FILE *fp) 224static int ftpcmd(const char *s1, const char *s2, FILE *fp)
201{ 225{
202 int result; 226 int result;
203 if (s1) { 227 if (s1) {
204 if (!s2) s2 = ""; 228 if (!s2)
229 s2 = "";
205 fprintf(fp, "%s%s\r\n", s1, s2); 230 fprintf(fp, "%s%s\r\n", s1, s2);
206 fflush(fp); 231 fflush(fp);
232 log_io("> %s%s", s1, s2);
207 } 233 }
208 234
209 do { 235 do {
210 char *buf_ptr; 236 fgets_and_trim(fp);
211
212 if (fgets(G.wget_buf, sizeof(G.wget_buf)-2, fp) == NULL) {
213 bb_perror_msg_and_die("error getting response");
214 }
215 buf_ptr = strstr(G.wget_buf, "\r\n");
216 if (buf_ptr) {
217 *buf_ptr = '\0';
218 }
219 } while (!isdigit(G.wget_buf[0]) || G.wget_buf[3] != ' '); 237 } while (!isdigit(G.wget_buf[0]) || G.wget_buf[3] != ' ');
220 238
221 G.wget_buf[3] = '\0'; 239 G.wget_buf[3] = '\0';
@@ -284,7 +302,7 @@ static void parse_url(char *src_url, struct host_info *h)
284 sp = h->host; 302 sp = h->host;
285} 303}
286 304
287static char *gethdr(FILE *fp /*, int *istrunc*/) 305static char *gethdr(FILE *fp)
288{ 306{
289 char *s, *hdrval; 307 char *s, *hdrval;
290 int c; 308 int c;
@@ -292,19 +310,16 @@ static char *gethdr(FILE *fp /*, int *istrunc*/)
292 /* *istrunc = 0; */ 310 /* *istrunc = 0; */
293 311
294 /* retrieve header line */ 312 /* retrieve header line */
295 if (fgets(G.wget_buf, sizeof(G.wget_buf), fp) == NULL) 313 c = fgets_and_trim(fp);
296 return NULL;
297 314
298 /* see if we are at the end of the headers */ 315 /* end of the headers? */
299 for (s = G.wget_buf; *s == '\r'; ++s) 316 if (G.wget_buf[0] == '\0')
300 continue;
301 if (*s == '\n')
302 return NULL; 317 return NULL;
303 318
304 /* convert the header name to lower case */ 319 /* convert the header name to lower case */
305 for (s = G.wget_buf; isalnum(*s) || *s == '-' || *s == '.'; ++s) { 320 for (s = G.wget_buf; isalnum(*s) || *s == '-' || *s == '.'; ++s) {
306 /* tolower for "A-Z", no-op for "0-9a-z-." */ 321 /* tolower for "A-Z", no-op for "0-9a-z-." */
307 *s = (*s | 0x20); 322 *s |= 0x20;
308 } 323 }
309 324
310 /* verify we are at the end of the header name */ 325 /* verify we are at the end of the header name */
@@ -315,20 +330,12 @@ static char *gethdr(FILE *fp /*, int *istrunc*/)
315 *s++ = '\0'; 330 *s++ = '\0';
316 hdrval = skip_whitespace(s); 331 hdrval = skip_whitespace(s);
317 332
318 /* locate the end of header */ 333 if (c != '\n') {
319 while (*s && *s != '\r' && *s != '\n') 334 /* Rats! The buffer isn't big enough to hold the entire header value */
320 ++s; 335 while (c = getc(fp), c != EOF && c != '\n')
321 336 continue;
322 /* end of header found */
323 if (*s) {
324 *s = '\0';
325 return hdrval;
326 } 337 }
327 338
328 /* Rats! The buffer isn't big enough to hold the entire header value */
329 while (c = getc(fp), c != EOF && c != '\n')
330 continue;
331 /* *istrunc = 1; */
332 return hdrval; 339 return hdrval;
333} 340}
334 341
@@ -520,9 +527,9 @@ static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
520 if (!G.chunked) 527 if (!G.chunked)
521 break; 528 break;
522 529
523 fgets(G.wget_buf, sizeof(G.wget_buf), dfp); /* This is a newline */ 530 fgets_and_trim(dfp); /* This is a newline */
524 get_clen: 531 get_clen:
525 fgets(G.wget_buf, sizeof(G.wget_buf), dfp); 532 fgets_and_trim(dfp);
526 G.content_len = STRTOOFF(G.wget_buf, NULL, 16); 533 G.content_len = STRTOOFF(G.wget_buf, NULL, 16);
527 /* FIXME: error check? */ 534 /* FIXME: error check? */
528 if (G.content_len == 0) 535 if (G.content_len == 0)
@@ -757,8 +764,7 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
757 * Retrieve HTTP response line and check for "200" status code. 764 * Retrieve HTTP response line and check for "200" status code.
758 */ 765 */
759 read_response: 766 read_response:
760 if (fgets(G.wget_buf, sizeof(G.wget_buf), sfp) == NULL) 767 fgets_and_trim(sfp);
761 bb_error_msg_and_die("no response from server");
762 768
763 str = G.wget_buf; 769 str = G.wget_buf;
764 str = skip_non_whitespace(str); 770 str = skip_non_whitespace(str);