wget: add support for connect timeout

function                                             old     new   delta
open_socket                                           33      64     +31
wget_main                                           2182    2194     +12

Signed-off-by: Lauri Kasanen <curaga@operamail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Lauri Kasanen 2013-10-12 21:47:07 +02:00 committed by Denys Vlasenko
parent 730e4d8b52
commit d074b416f8
2 changed files with 30 additions and 10 deletions

View File

@ -970,16 +970,18 @@ config FEATURE_WGET_LONG_OPTIONS
Support long options for the wget applet. Support long options for the wget applet.
config FEATURE_WGET_TIMEOUT config FEATURE_WGET_TIMEOUT
bool "Enable read timeout option -T SEC" bool "Enable timeout option -T SEC"
default y default y
depends on WGET depends on WGET
help help
Supports network read timeout for wget, so that wget will give Supports network read and connect timeouts for wget,
up and timeout when reading network data, through the -T command so that wget will give up and timeout, through the -T
line option. Currently only network data read timeout is command line option.
supported (i.e., timeout is not applied to the DNS nor TCP
connection initialization). When FEATURE_WGET_LONG_OPTIONS is Currently only connect and network data read timeout are
also enabled, the --timeout option will work in addition to -T. supported (i.e., timeout is not applied to the DNS query). When
FEATURE_WGET_LONG_OPTIONS is also enabled, the --timeout option
will work in addition to -T.
config ZCIP config ZCIP
bool "zcip" bool "zcip"

View File

@ -72,6 +72,7 @@ struct globals {
const char *user_agent; /* "User-Agent" header field */ const char *user_agent; /* "User-Agent" header field */
#if ENABLE_FEATURE_WGET_TIMEOUT #if ENABLE_FEATURE_WGET_TIMEOUT
unsigned timeout_seconds; unsigned timeout_seconds;
bool connecting;
#endif #endif
int output_fd; int output_fd;
int o_flags; int o_flags;
@ -87,7 +88,6 @@ struct globals {
#define G (*ptr_to_globals) #define G (*ptr_to_globals)
#define INIT_G() do { \ #define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
IF_FEATURE_WGET_TIMEOUT(G.timeout_seconds = 900;) \
} while (0) } while (0)
@ -195,13 +195,27 @@ static char* sanitize_string(char *s)
return s; return s;
} }
#if ENABLE_FEATURE_WGET_TIMEOUT
static void alarm_handler(int sig UNUSED_PARAM)
{
/* This is theoretically unsafe (uses stdio and malloc in signal handler) */
if (G.connecting)
bb_error_msg_and_die("download timed out");
}
#endif
static FILE *open_socket(len_and_sockaddr *lsa) static FILE *open_socket(len_and_sockaddr *lsa)
{ {
int fd;
FILE *fp; FILE *fp;
IF_FEATURE_WGET_TIMEOUT(alarm(G.timeout_seconds); G.connecting = 1;)
fd = xconnect_stream(lsa);
IF_FEATURE_WGET_TIMEOUT(G.connecting = 0;)
/* glibc 2.4 seems to try seeking on it - ??! */ /* glibc 2.4 seems to try seeking on it - ??! */
/* hopefully it understands what ESPIPE means... */ /* hopefully it understands what ESPIPE means... */
fp = fdopen(xconnect_stream(lsa), "r+"); fp = fdopen(fd, "r+");
if (fp == NULL) if (fp == NULL)
bb_perror_msg_and_die(bb_msg_memory_exhausted); bb_perror_msg_and_die(bb_msg_memory_exhausted);
@ -209,6 +223,7 @@ static FILE *open_socket(len_and_sockaddr *lsa)
} }
/* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */ /* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */
/* FIXME: does not respect FEATURE_WGET_TIMEOUT and -T N: */
static char fgets_and_trim(FILE *fp) static char fgets_and_trim(FILE *fp)
{ {
char c; char c;
@ -944,7 +959,10 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
INIT_G(); INIT_G();
IF_FEATURE_WGET_TIMEOUT(G.timeout_seconds = 900;) #if ENABLE_FEATURE_WGET_TIMEOUT
G.timeout_seconds = 900;
signal(SIGALRM, alarm_handler);
#endif
G.proxy_flag = "on"; /* use proxies if env vars are set */ G.proxy_flag = "on"; /* use proxies if env vars are set */
G.user_agent = "Wget"; /* "User-Agent" header field */ G.user_agent = "Wget"; /* "User-Agent" header field */