Re: [PATCH 2/2] wget: add support for retries in http requests

2018-12-12 Thread Martin Lewis
Hello,

These patches add the -t (--tries) option to wget.
There was an issue that wget died on errors, so I had to change some
functions to return error so we are able to retry.
Also I had to replace the blocking functions (and the set_alarm) with time
outing functions (in first patch).
In order to imitate fgets(), and not reading char by char checking for
newline I had to buffer the read.
I've seen that many features can be turned off in config, but as I see it,
adding these #ifs would rather bloat the patch, while the default behaviour
without the flag should not change.
Also as for ftp retries don't work yet.
If you would like me to change anything in the patches I would gladly do
when I have some more time.

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


[PATCH 2/2] wget: add support for retries in http requests

2018-12-12 Thread Martin Lewis
Replace die handlers with error returning so download_one_url can retry from 
the beginning.
When retries is 1 (default) the behaviour should be the same as before.

Signed-off-by: Martin Lewis 
---
 networking/wget.c | 120 ++
 1 file changed, 95 insertions(+), 25 deletions(-)

diff --git a/networking/wget.c b/networking/wget.c
index e9fdd9f..4b1961c 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -123,14 +123,14 @@
 //usage:#define wget_trivial_usage
 //usage:   IF_FEATURE_WGET_LONG_OPTIONS(
 //usage:   "[-c|--continue] [--spider] [-q|--quiet] [-O|--output-document 
FILE]\n"
-//usage:   "   [--header 'header: value'] [-Y|--proxy on/off] [-P 
DIR]\n"
+//usage:   "   [--header 'header: value'] [-Y|--proxy on/off] 
[-t|--tries TRIES] [-P DIR]\n"
 /* Since we ignore these opts, we don't show them in --help */
 /* //usage:"   [--no-check-certificate] [--no-cache] [--passive-ftp] 
[-t TRIES]" */
 /* //usage:"   [-nv] [-nc] [-nH] [-np]" */
 //usage:   "   [-S|--server-response] [-U|--user-agent AGENT]" 
IF_FEATURE_WGET_TIMEOUT(" [-T SEC]") " URL..."
 //usage:   )
 //usage:   IF_NOT_FEATURE_WGET_LONG_OPTIONS(
-//usage:   "[-cq] [-O FILE] [-Y on/off] [-P DIR] [-S] [-U AGENT]"
+//usage:   "[-cq] [-O FILE] [-Y on/off] [-t TRIES] [-P DIR] [-S] [-U 
AGENT]"
 //usage:   IF_FEATURE_WGET_TIMEOUT(" [-T SEC]") " URL..."
 //usage:   )
 //usage:#define wget_full_usage "\n\n"
@@ -149,6 +149,7 @@
 //usage: "\n   -O FILE Save to FILE ('-' for stdout)"
 //usage: "\n   -U STR  Use STR for User-Agent header"
 //usage: "\n   -Y on/off   Use proxy"
+//usage: "\n   -t TRIESSet number of retries to TRIES (0 
unlimits)"
 
 #include "libbb.h"
 
@@ -233,6 +234,7 @@ struct globals {
char *fname_out;/* where to direct output (-O) */
const char *proxy_flag; /* Use proxies if env vars are set */
const char *user_agent; /* "User-Agent" header field */
+   unsigned tries; /* For -t option */
int output_fd;
int o_flags;
 #if ENABLE_FEATURE_WGET_TIMEOUT
@@ -402,6 +404,7 @@ static int is_ip_address(const char *string)
 }
 #endif
 
+/* Return NULL if connect() fails */
 static FILE *open_socket(len_and_sockaddr *lsa)
 {
int fd;
@@ -409,13 +412,19 @@ static FILE *open_socket(len_and_sockaddr *lsa)
 #if ENABLE_FEATURE_WGET_TIMEOUT
struct timeval timeout = {G.timeout_seconds, 0};
 #endif
+
fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
 #if ENABLE_FEATURE_WGET_TIMEOUT
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, , sizeof (timeout)) 
< 0) {
bb_perror_msg_and_die("setsockopt failed\n");
}
 #endif
-   xconnect(fd, >u.sa, lsa->len);
+   if (connect(fd, >u.sa, lsa->len) < 0) {
+   /* Failure */
+   bb_perror_msg("connect failed");
+   close(fd);
+   return NULL;
+   }
 
/* glibc 2.4 seems to try seeking on it - ??! */
/* hopefully it understands what ESPIPE means... */
@@ -584,14 +593,16 @@ static int fread_buffered(char *buffer, size_t len, FILE 
*fp)
return fread(buffer, 1, len, fp);
 }
 
-/* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */
-static char fgets_trim_sanitize(FILE *fp, const char *fmt)
+/* Returns '\n' if it was seen, -1 if timeout occured, else '\0'. Trims at 
first '\r' or '\n' */
+static signed char fgets_trim_sanitize(FILE *fp, const char *fmt)
 {
char c;
char *buf_ptr;
 
-   if (fgets_read_to_newline(G.wget_buf, sizeof(G.wget_buf), fp) == NULL)
-   bb_perror_msg_and_die("error getting response");
+   if (fgets_read_to_newline(G.wget_buf, sizeof(G.wget_buf), fp) == NULL) {
+   bb_perror_msg("error getting response");
+   return -1;
+   }
 
buf_ptr = strchrnul(G.wget_buf, '\n');
c = *buf_ptr;
@@ -629,7 +640,9 @@ static int ftpcmd(const char *s1, const char *s2, FILE *fp)
/* Read until "Nxx something" is received */
G.wget_buf[3] = 0;
do {
-   fgets_trim_sanitize(fp, "%s\n");
+   if (fgets_trim_sanitize(fp, "%s\n") == -1) {
+   xfunc_die();
+   }
} while (!isdigit(G.wget_buf[0]) || G.wget_buf[3] != ' ');
 
G.wget_buf[3] = '\0';
@@ -734,6 +747,11 @@ static char *get_sanitized_hdr(FILE *fp)
/* retrieve header line */
c = fgets_trim_sanitize(fp, "  %s\n");
 
+   if (c == -1) {
+   /* Timed out */
+   return NULL;
+   }
+
/* end of the headers? */
if (G.wget_buf[0] == '\0')
return NULL;
@@ -916,7 +934,11 @@ static FILE* prepare_ftp_session(FILE **dfpp, struct 
host_info *target, len_and_
char *pass;
int port;
 
+   /* TODO: Add retry support for ftp