diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-03-17 15:58:16 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-03-17 21:52:42 +0100 |
commit | 6701e91d84fe499b5d87dc21790cd9d7bcb13220 (patch) | |
tree | 6999b06877693ffb5bb86e4dfef659772fc7f2ac | |
parent | 0d1b71e8e61491cf50fc3a21ec0a412f6dca4d56 (diff) | |
download | busybox-w32-6701e91d84fe499b5d87dc21790cd9d7bcb13220.tar.gz busybox-w32-6701e91d84fe499b5d87dc21790cd9d7bcb13220.tar.bz2 busybox-w32-6701e91d84fe499b5d87dc21790cd9d7bcb13220.zip |
wget: make -T timeout work on header reads too. Closes 8636
function old new delta
set_alarm - 27 +27
fgets_and_trim 76 92 +16
wget_main 2610 2616 +6
open_socket 64 54 -10
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/wget.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/networking/wget.c b/networking/wget.c index 7f27e4e7b..5c12423c7 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -203,7 +203,7 @@ struct globals { | |||
203 | const char *user_agent; /* "User-Agent" header field */ | 203 | const char *user_agent; /* "User-Agent" header field */ |
204 | #if ENABLE_FEATURE_WGET_TIMEOUT | 204 | #if ENABLE_FEATURE_WGET_TIMEOUT |
205 | unsigned timeout_seconds; | 205 | unsigned timeout_seconds; |
206 | bool connecting; | 206 | bool die_if_timed_out; |
207 | #endif | 207 | #endif |
208 | int output_fd; | 208 | int output_fd; |
209 | int o_flags; | 209 | int o_flags; |
@@ -333,9 +333,20 @@ static char* sanitize_string(char *s) | |||
333 | static void alarm_handler(int sig UNUSED_PARAM) | 333 | static void alarm_handler(int sig UNUSED_PARAM) |
334 | { | 334 | { |
335 | /* This is theoretically unsafe (uses stdio and malloc in signal handler) */ | 335 | /* This is theoretically unsafe (uses stdio and malloc in signal handler) */ |
336 | if (G.connecting) | 336 | if (G.die_if_timed_out) |
337 | bb_error_msg_and_die("download timed out"); | 337 | bb_error_msg_and_die("download timed out"); |
338 | } | 338 | } |
339 | static void set_alarm(void) | ||
340 | { | ||
341 | if (G.timeout_seconds) { | ||
342 | alarm(G.timeout_seconds); | ||
343 | G.die_if_timed_out = 1; | ||
344 | } | ||
345 | } | ||
346 | # define clear_alarm() ((void)(G.die_if_timed_out = 0)) | ||
347 | #else | ||
348 | # define set_alarm() ((void)0) | ||
349 | # define clear_alarm() ((void)0) | ||
339 | #endif | 350 | #endif |
340 | 351 | ||
341 | static FILE *open_socket(len_and_sockaddr *lsa) | 352 | static FILE *open_socket(len_and_sockaddr *lsa) |
@@ -343,9 +354,9 @@ static FILE *open_socket(len_and_sockaddr *lsa) | |||
343 | int fd; | 354 | int fd; |
344 | FILE *fp; | 355 | FILE *fp; |
345 | 356 | ||
346 | IF_FEATURE_WGET_TIMEOUT(alarm(G.timeout_seconds); G.connecting = 1;) | 357 | set_alarm(); |
347 | fd = xconnect_stream(lsa); | 358 | fd = xconnect_stream(lsa); |
348 | IF_FEATURE_WGET_TIMEOUT(G.connecting = 0;) | 359 | clear_alarm(); |
349 | 360 | ||
350 | /* glibc 2.4 seems to try seeking on it - ??! */ | 361 | /* glibc 2.4 seems to try seeking on it - ??! */ |
351 | /* hopefully it understands what ESPIPE means... */ | 362 | /* hopefully it understands what ESPIPE means... */ |
@@ -357,14 +368,15 @@ static FILE *open_socket(len_and_sockaddr *lsa) | |||
357 | } | 368 | } |
358 | 369 | ||
359 | /* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */ | 370 | /* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */ |
360 | /* FIXME: does not respect FEATURE_WGET_TIMEOUT and -T N: */ | ||
361 | static char fgets_and_trim(FILE *fp) | 371 | static char fgets_and_trim(FILE *fp) |
362 | { | 372 | { |
363 | char c; | 373 | char c; |
364 | char *buf_ptr; | 374 | char *buf_ptr; |
365 | 375 | ||
376 | set_alarm(); | ||
366 | if (fgets(G.wget_buf, sizeof(G.wget_buf) - 1, fp) == NULL) | 377 | if (fgets(G.wget_buf, sizeof(G.wget_buf) - 1, fp) == NULL) |
367 | bb_perror_msg_and_die("error getting response"); | 378 | bb_perror_msg_and_die("error getting response"); |
379 | clear_alarm(); | ||
368 | 380 | ||
369 | buf_ptr = strchrnul(G.wget_buf, '\n'); | 381 | buf_ptr = strchrnul(G.wget_buf, '\n'); |
370 | c = *buf_ptr; | 382 | c = *buf_ptr; |