Replaced: -f file with -O file to behave more like GNU wget Added: parsing of redirect responses, will now follow redirectation path until maximum of 10 jumps. This will not help much because most likely this will lead to server advertising new unsupported HTTPS url.
-Jarno --- toys/pending/wget.c | 81 +++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 33 deletions(-)
From 94f01ac9df401ba25be399d8bd4fcebd2a6826e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarno=20M=C3=A4kip=C3=A4=C3=A4?= <[email protected]> Date: Sat, 14 Sep 2019 13:15:04 +0300 Subject: [PATCH] wget: Added support for HTTP 301 and 302 redirects Replaced: -f file with -O file to behave more like GNU wget Added: parsing of redirect responses, will now follow redirectation path until maximum of 10 jumps. This will not help much because most likely this will lead to server advertising new unsupported HTTPS url. --- toys/pending/wget.c | 81 +++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/toys/pending/wget.c b/toys/pending/wget.c index ced51d9c..7feda8e8 100644 --- a/toys/pending/wget.c +++ b/toys/pending/wget.c @@ -3,19 +3,19 @@ * Copyright 2016 Lipi C.H. Lee <[email protected]> * -USE_WGET(NEWTOY(wget, "f:", TOYFLAG_USR|TOYFLAG_BIN)) +USE_WGET(NEWTOY(wget, "O:", TOYFLAG_USR|TOYFLAG_BIN)) config WGET bool "wget" default n help - usage: wget -f filename URL - -f filename: specify the filename to be saved + usage: wget -O filename URL + -O filename: specify the filename to be saved URL: HTTP uniform resource location and only HTTP, not HTTPS examples: - wget -f index.html http://www.example.com - wget -f sample.jpg http://www.example.com:8080/sample.jpg + wget -O index.html http://www.example.com + wget -O sample.jpg http://www.example.com:8080/sample.jpg */ #define FOR_wget @@ -96,7 +96,7 @@ static int conn_svr(const char *hostname, const char *port) { continue; } if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) - break; // succeed in connecting to any server IP + break; // succeed in connecting to any server IP else perror_msg("connect error"); close(sock); } @@ -132,41 +132,56 @@ void wget_main(void) ssize_t len, body_len; char *body, *result, *rc, *r_str; char ua[18] = "toybox wget/", ver[6], hostname[1024], port[6], path[1024]; - + int redirects = 10; + char* redir_loc = 0; // TODO extract filename to be saved from URL - if (!(toys.optflags & FLAG_f)) help_exit("no filename"); + if (!(toys.optflags & FLAG_O)) help_exit("no filename"); if (fopen(TT.filename, "r")) error_exit("'%s' already exists", TT.filename); if(!toys.optargs[0]) help_exit("no URL"); get_info(toys.optargs[0], hostname, port, path); - sock = conn_svr(hostname, port); - - // compose HTTP request - sprintf(toybuf, "GET %s HTTP/1.1\r\n", path); - mk_fld("Host", hostname); strncpy(ver, TOYBOX_VERSION, 5); strcat(ua, ver); - mk_fld("User-Agent", ua); - mk_fld("Connection", "close"); - strcat(toybuf, "\r\n"); - - // send the HTTP request - len = strlen(toybuf); - if (write(sock, toybuf, len) != len) perror_exit("write error"); - - // read HTTP response - if ((len = read(sock, toybuf, 4096)) == -1) perror_exit("read error"); - if (!strstr(toybuf, "\r\n\r\n")) error_exit("too long HTTP response"); - body = get_body(len, &body_len); - result = strtok(toybuf, "\r"); - strtok(result, " "); - rc = strtok(NULL, " "); - r_str = strtok(NULL, " "); - - // HTTP res code check - // TODO handle HTTP 302 Found(Redirection) - if (strcmp(rc, "200")) error_exit("res: %s(%s)", rc, r_str); + for(;;redirects--) { + sock = conn_svr(hostname, port); + + // compose HTTP request + sprintf(toybuf, "GET %s HTTP/1.1\r\n", path); + mk_fld("Host", hostname); + mk_fld("User-Agent", ua); + mk_fld("Connection", "close"); + strcat(toybuf, "\r\n"); + + // send the HTTP request + len = strlen(toybuf); + if (write(sock, toybuf, len) != len) perror_exit("write error"); + + // read HTTP response + if ((len = read(sock, toybuf, 4096)) == -1) perror_exit("read error"); + if (!strstr(toybuf, "\r\n\r\n")) error_exit("too long HTTP response"); + body = get_body(len, &body_len); + redir_loc = strstr(toybuf, "Location: "); + result = strtok(toybuf, "\r"); + strtok(result, " "); + rc = strtok(NULL, " "); + r_str = strtok(NULL, " "); + + // HTTP res code check + if (!strcmp(rc, "301") || !strcmp(rc, "302")) { + char* eol = 0; + if ((eol = strchr(redir_loc, '\r')) > 0) *eol = 0; + else if (redir_loc) error_exit("Could not parse redirect URL"); + if (redirects < 0) error_exit("Too many redirects"); + + printf("Redirection: %s %s \n", rc, r_str); + printf("%s \n", redir_loc); + redir_loc = redir_loc+strlen("Location: "); + close(sock); + get_info(redir_loc, hostname, port, path); + } else if (!strcmp(rc, "200")) break; + else error_exit("res: %s(%s)", rc, r_str); + } if (!(fp = fopen(TT.filename, "w"))) perror_exit("fopen error"); if (fwrite(body, 1, body_len, fp) != body_len) -- 2.19.1
_______________________________________________ Toybox mailing list [email protected] http://lists.landley.net/listinfo.cgi/toybox-landley.net
