accidentally applied wrong (old) patch, fixing up...
[oweals/busybox.git] / networking / wget.c
index 69cabfc7fcb356a9805ccdd9be99b052e73993d5..5c3083c25d70791d239be525b4f2ddc58ca3108f 100644 (file)
@@ -24,7 +24,7 @@ struct host_info {
 };
 
 static void parse_url(char *url, struct host_info *h);
-static FILE *open_socket(struct sockaddr_in *s_in);
+static FILE *open_socket(len_and_sockaddr *lsa);
 static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
 static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
 
@@ -90,7 +90,7 @@ int wget_main(int argc, char **argv)
 {
        char buf[512];
        struct host_info server, target;
-       struct sockaddr_in s_in;
+       len_and_sockaddr *lsa;
        int n, status;
        int port;
        int try = 5;
@@ -139,7 +139,7 @@ int wget_main(int argc, char **argv)
                { "passive-ftp",      no_argument, NULL, 0xff },
                { "header",           required_argument, NULL, 0xfe },
                { 0, 0, 0, 0 }
-};
+       };
        applet_long_options = wget_long_options;
 #endif
        opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
@@ -189,7 +189,7 @@ int wget_main(int argc, char **argv)
        if (!fname_out) {
                // Dirty hack. Needed because bb_get_last_path_component
                // will destroy trailing / by storing '\0' in last byte!
-               if (*target.path && target.path[strlen(target.path)-1] != '/') {
+               if (!last_char_is(target.path, '/')) {
                        fname_out =
 #if ENABLE_FEATURE_WGET_STATUSBAR
                                curfile =
@@ -210,17 +210,18 @@ int wget_main(int argc, char **argv)
                curfile = bb_get_last_path_component(fname_out);
 #endif
        }
+       /* Impossible?
        if ((opt & WGET_OPT_CONTINUE) && !fname_out)
-               bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
+               bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
 
        /*
         * Determine where to start transfer.
         */
-       if (!strcmp(fname_out, "-")) {
+       if (LONE_DASH(fname_out)) {
                output_fd = 1;
-               opt |= WGET_OPT_QUIET;
                opt &= ~WGET_OPT_CONTINUE;
-       } else if (opt & WGET_OPT_CONTINUE) {
+       }
+       if (opt & WGET_OPT_CONTINUE) {
                output_fd = open(fname_out, O_WRONLY);
                if (output_fd >= 0) {
                        beg_range = xlseek(output_fd, 0, SEEK_END);
@@ -232,11 +233,11 @@ int wget_main(int argc, char **argv)
        /* We want to do exactly _one_ DNS lookup, since some
         * sites (i.e. ftp.us.debian.org) use round-robin DNS
         * and we want to connect to only one IP... */
-       bb_lookup_host(&s_in, server.host);
-       s_in.sin_port = server.port;
+       lsa = host2sockaddr(server.host, server.port);
        if (!(opt & WGET_OPT_QUIET)) {
-               printf("Connecting to %s[%s]:%d\n",
-                               server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port));
+               fprintf(stderr, "Connecting to %s (%s)\n", server.host,
+                               xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
+               /* We leak result of xmalloc_sockaddr2dotted */
        }
 
        if (use_proxy || !target.is_ftp) {
@@ -253,26 +254,21 @@ int wget_main(int argc, char **argv)
                         * Open socket to http server
                         */
                        if (sfp) fclose(sfp);
-                       sfp = open_socket(&s_in);
+                       sfp = open_socket(lsa);
 
                        /*
                         * Send HTTP request.
                         */
                        if (use_proxy) {
-                               const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
-#if ENABLE_FEATURE_WGET_IP6_LITERAL
-                               if (strchr(target.host, ':'))
-                                       format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
-#endif
-                               fprintf(sfp, format,
+                               fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
                                        target.is_ftp ? "f" : "ht", target.host,
-                                       ntohs(target.port), target.path);
+                                       target.path);
                        } else {
                                fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
                        }
 
-                       fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n", target.host,
-                               user_agent);
+                       fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n",
+                               target.host, user_agent);
 
 #if ENABLE_FEATURE_WGET_AUTHENTICATION
                        if (target.user) {
@@ -286,7 +282,7 @@ int wget_main(int argc, char **argv)
 #endif
 
                        if (beg_range)
-                               fprintf(sfp, "Range: bytes=%"OFF_FMT"-\r\n", beg_range);
+                               fprintf(sfp, "Range: bytes=%"OFF_FMT"d-\r\n", beg_range);
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
                        if (extra_headers)
                                fputs(extra_headers, sfp);
@@ -334,7 +330,8 @@ int wget_main(int argc, char **argv)
                         */
                        while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
                                if (strcasecmp(buf, "content-length") == 0) {
-                                       if (SAFE_STRTOOFF(s, &content_len) || content_len < 0) {
+                                       content_len = BB_STRTOOFF(s, NULL, 10);
+                                       if (errno || content_len < 0) {
                                                bb_error_msg_and_die("content-length %s is garbage", s);
                                        }
                                        got_clen = 1;
@@ -355,13 +352,13 @@ int wget_main(int argc, char **argv)
                                                        server.host = target.host;
                                                        server.port = target.port;
                                                }
-                                               bb_lookup_host(&s_in, server.host);
-                                               s_in.sin_port = server.port;
+                                               free(lsa);
+                                               lsa = host2sockaddr(server.host, server.port);
                                                break;
                                        }
                                }
                        }
-               } while(status >= 300);
+               } while (status >= 300);
 
                dfp = sfp;
 
@@ -373,7 +370,7 @@ int wget_main(int argc, char **argv)
                if (!target.user)
                        target.user = xstrdup("anonymous:busybox@");
 
-               sfp = open_socket(&s_in);
+               sfp = open_socket(lsa);
                if (ftpcmd(NULL, NULL, sfp, buf) != 220)
                        bb_error_msg_and_die("%s", buf+4);
 
@@ -401,7 +398,8 @@ int wget_main(int argc, char **argv)
                 * Querying file size
                 */
                if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
-                       if (SAFE_STRTOOFF(buf+4, &content_len) || content_len < 0) {
+                       content_len = BB_STRTOOFF(buf+4, NULL, 10);
+                       if (errno || content_len < 0) {
                                bb_error_msg_and_die("SIZE value is garbage");
                        }
                        got_clen = 1;
@@ -414,27 +412,29 @@ int wget_main(int argc, char **argv)
  pasv_error:
                        bb_error_msg_and_die("bad response to %s: %s", "PASV", buf);
                }
-               // Response is "227 garbageN1,N2,N3,N4,P1,P2
+               // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
                // Server's IP is N1.N2.N3.N4 (we ignore it)
                // Server's port for data connection is P1*256+P2
+               s = strrchr(buf, ')');
+               if (s) s[0] = '\0';
                s = strrchr(buf, ',');
                if (!s) goto pasv_error;
-               port = xatol_range(s+1, 0, 255);
+               port = xatou_range(s+1, 0, 255);
                *s = '\0';
                s = strrchr(buf, ',');
                if (!s) goto pasv_error;
-               port += xatol_range(s+1, 0, 255) * 256;
-               s_in.sin_port = htons(port);
-               dfp = open_socket(&s_in);
+               port += xatou_range(s+1, 0, 255) * 256;
+               set_nport(lsa, htons(port));
+               dfp = open_socket(lsa);
 
                if (beg_range) {
-                       sprintf(buf, "REST %"OFF_FMT, beg_range);
+                       sprintf(buf, "REST %"OFF_FMT"d", beg_range);
                        if (ftpcmd(buf, NULL, sfp, buf) == 350)
                                content_len -= beg_range;
                }
 
                if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
-                       bb_error_msg_and_die("bad response to %s: %s", "RETR", buf);
+                       bb_error_msg_and_die("bad response to RETR: %s", buf);
        }
 
 
@@ -443,14 +443,14 @@ int wget_main(int argc, char **argv)
         */
        if (chunked) {
                fgets(buf, sizeof(buf), dfp);
-               content_len = STRTOOFF(buf, (char **) NULL, 16);
+               content_len = STRTOOFF(buf, NULL, 16);
                /* FIXME: error check?? */
        }
 
        /* Do it before progressmeter (want to have nice error message) */
        if (output_fd < 0)
-               output_fd = xopen3(fname_out,
-                       O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0666);
+               output_fd = xopen(fname_out,
+                       O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
 
        if (!(opt & WGET_OPT_QUIET))
                progressmeter(-1);
@@ -477,7 +477,7 @@ int wget_main(int argc, char **argv)
                if (chunked) {
                        safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
                        safe_fgets(buf, sizeof(buf), dfp);
-                       content_len = STRTOOFF(buf, (char **) NULL, 16);
+                       content_len = STRTOOFF(buf, NULL, 16);
                        /* FIXME: error check? */
                        if (content_len == 0) {
                                chunked = 0; /* all done! */
@@ -504,7 +504,7 @@ int wget_main(int argc, char **argv)
 
 static void parse_url(char *src_url, struct host_info *h)
 {
-       char *url, *p, *cp, *sp, *up, *pp;
+       char *url, *p, *sp;
 
        /* h->allocated = */ url = xstrdup(src_url);
 
@@ -537,8 +537,8 @@ static void parse_url(char *src_url, struct host_info *h)
        if (!sp) {
                h->path = "";
        } else if (*sp == '/') {
-               *sp++ = '\0';
-               h->path = sp;
+               *sp = '\0';
+               h->path = sp + 1;
        } else { // '#' or '?'
                // http://busybox.net?login=john@doe is a valid URL
                // memmove converts to:
@@ -549,46 +549,25 @@ static void parse_url(char *src_url, struct host_info *h)
                h->path = sp;
        }
 
-       up = strrchr(h->host, '@');
-       if (up != NULL) {
+       sp = strrchr(h->host, '@');
+       h->user = NULL;
+       if (sp != NULL) {
                h->user = h->host;
-               *up++ = '\0';
-               h->host = up;
-       } else
-               h->user = NULL;
-
-       pp = h->host;
-
-#if ENABLE_FEATURE_WGET_IP6_LITERAL
-       if (h->host[0] == '[') {
-               char *ep;
-
-               ep = h->host + 1;
-               while (*ep == ':' || isxdigit(*ep))
-                       ep++;
-               if (*ep == ']') {
-                       h->host++;
-                       *ep = '\0';
-                       pp = ep + 1;
-               }
+               *sp = '\0';
+               h->host = sp + 1;
        }
-#endif
 
-       cp = strchr(pp, ':');
-       if (cp != NULL) {
-               *cp++ = '\0';
-               h->port = htons(xatou16(cp));
-       }
+       sp = h->host;
 }
 
 
-static FILE *open_socket(struct sockaddr_in *s_in)
+static FILE *open_socket(len_and_sockaddr *lsa)
 {
        FILE *fp;
 
        /* glibc 2.4 seems to try seeking on it - ??! */
        /* hopefully it understands what ESPIPE means... */
-       fp = fdopen(xconnect_tcp_v4(s_in), "r+");
+       fp = fdopen(xconnect_stream(lsa), "r+");
        if (fp == NULL)
                bb_perror_msg_and_die("fdopen");
 
@@ -608,13 +587,13 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
                return NULL;
 
        /* see if we are at the end of the headers */
-       for (s = buf ; *s == '\r' ; ++s)
+       for (s = buf; *s == '\r'; ++s)
                ;
        if (s[0] == '\n')
                return NULL;
 
        /* convert the header name to lower case */
-       for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
+       for (s = buf; isalnum(*s) || *s == '-'; ++s)
                *s = tolower(*s);
 
        /* verify we are at the end of the header name */
@@ -622,7 +601,7 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
                bb_error_msg_and_die("bad header line: %s", buf);
 
        /* locate the start of the header value */
-       for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
+       for (*s++ = '\0'; *s == ' ' || *s == '\t'; ++s)
                ;
        hdrval = s;
 
@@ -823,5 +802,4 @@ progressmeter(int flag)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *     $Id: wget.c,v 1.75 2004/10/08 08:27:40 andersen Exp $
  */