Hi,

Connect timeout support for wget.

Bloatcheck:

function                                             old     new   delta
open_socket                                           38      71     +33
alarm_handler                                          -      31     +31
wget_main                                           2432    2447     +15
ftpcmd                                               127     130      +3
fgets_and_trim                                        89      92      +3
base64enc                                             63      66      +3
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 5/0 up/down: 88/0)               Total: 88
bytes
   text    data     bss     dec     hex filename
  21739     970     104   22813    591d busybox_old
  21980     986     104   23070    5a1e busybox_unstripped


- Lauri

-- 
http://www.fastmail.fm - Or how I learned to stop worrying and
                          love email again

From 2a33129f02ce0609ba88e3d4ae98b1bed60654f0 Mon Sep 17 00:00:00 2001
From: Lauri Kasanen <cur...@operamail.com>
Date: Tue, 8 Oct 2013 17:11:22 +0300
Subject: [PATCH 2/2] wget: add support for connect timeout

Signed-off-by: Lauri Kasanen <cur...@operamail.com>
---
 networking/Config.src | 16 +++++++++-------
 networking/wget.c     | 18 +++++++++++++++++-
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/networking/Config.src b/networking/Config.src
index 0a483ae..987ae26 100644
--- a/networking/Config.src
+++ b/networking/Config.src
@@ -970,16 +970,18 @@ config FEATURE_WGET_LONG_OPTIONS
 	  Support long options for the wget applet.
 
 config FEATURE_WGET_TIMEOUT
-	bool "Enable read timeout option -T SEC"
+	bool "Enable timeout option -T SEC"
 	default y
 	depends on WGET
 	help
-	  Supports network read timeout for wget, so that wget will give
-	  up and timeout when reading network data, through the -T command
-	  line option.  Currently only network data read timeout is
-	  supported (i.e., timeout is not applied to the DNS nor TCP
-	  connection initialization).  When FEATURE_WGET_LONG_OPTIONS is
-	  also enabled, the --timeout option will work in addition to -T.
+	  Supports network read and connect timeouts for wget,
+	  so that wget will give up and timeout, through the -T
+	  command line option.
+
+	  Currently only connect and network data read timeout are
+	  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 WGET_DEFAULT_TIMEOUT
 	int "Default wget timeout"
diff --git a/networking/wget.c b/networking/wget.c
index f604828..5e3533a 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -72,6 +72,7 @@ struct globals {
 	const char *user_agent; /* "User-Agent" header field */
 #if ENABLE_FEATURE_WGET_TIMEOUT
 	unsigned timeout_seconds;
+	bool connecting;
 #endif
 	int output_fd;
 	int o_flags;
@@ -198,12 +199,16 @@ static FILE *open_socket(len_and_sockaddr *lsa)
 {
 	FILE *fp;
 
+	IF_FEATURE_WGET_TIMEOUT(G.connecting = 1; alarm(G.timeout_seconds);)
+
 	/* glibc 2.4 seems to try seeking on it - ??! */
 	/* hopefully it understands what ESPIPE means... */
 	fp = fdopen(xconnect_stream(lsa), "r+");
 	if (fp == NULL)
 		bb_perror_msg_and_die(bb_msg_memory_exhausted);
 
+	IF_FEATURE_WGET_TIMEOUT(G.connecting = 0;)
+
 	return fp;
 }
 
@@ -907,6 +912,14 @@ However, in real world it was observed that some web servers
 	free(redirected_path);
 }
 
+#if ENABLE_FEATURE_WGET_TIMEOUT
+static void alarm_handler(int sig UNUSED_PARAM)
+{
+	if (G.connecting)
+		bb_error_msg_and_die("download timed out");
+}
+#endif
+
 int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int wget_main(int argc UNUSED_PARAM, char **argv)
 {
@@ -943,7 +956,10 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
 
 	INIT_G();
 
-	IF_FEATURE_WGET_TIMEOUT(G.timeout_seconds = CONFIG_WGET_DEFAULT_TIMEOUT;)
+#if ENABLE_FEATURE_WGET_TIMEOUT
+	G.timeout_seconds = CONFIG_WGET_DEFAULT_TIMEOUT;
+	signal(SIGALRM, alarm_handler);
+#endif
 	G.proxy_flag = "on";   /* use proxies if env vars are set */
 	G.user_agent = "Wget"; /* "User-Agent" header field */
 
-- 
1.8.3.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to