httpd: reduce ifdef forest. comment out redundant PATH setting
[oweals/busybox.git] / networking / wget.c
index 264ae4483331a433f84cee4b2fc7f4a02cf5be94..a4c6289cb493ed16abe0de65aad52236a3ab056f 100644 (file)
@@ -86,43 +86,22 @@ static char *base64enc(unsigned char *p, char *buf, int len)
 }
 #endif
 
-#define WGET_OPT_CONTINUE     1
-#define WGET_OPT_QUIET        2
-#define WGET_OPT_PASSIVE      4
-#define WGET_OPT_OUTNAME      8
-#define WGET_OPT_HEADER      16
-#define WGET_OPT_PREFIX      32
-#define WGET_OPT_PROXY       64
-#define WGET_OPT_USER_AGENT 128
-
-#if ENABLE_FEATURE_WGET_LONG_OPTIONS
-static const struct option wget_long_options[] = {
-       { "continue",        0, NULL, 'c' },
-       { "quiet",           0, NULL, 'q' },
-       { "passive-ftp",     0, NULL, 139 }, /* FIXME: what is this - 139?? */
-       { "output-document", 1, NULL, 'O' },
-       { "header",          1, NULL, 131 },
-       { "directory-prefix",1, NULL, 'P' },
-       { "proxy",           1, NULL, 'Y' },
-       { "user-agent",      1, NULL, 'U' },
-       { 0,                 0, 0, 0 }
-};
-#endif
-
 int wget_main(int argc, char **argv)
 {
-       int n, try=5, status;
-       unsigned opt;
-       int port;
-       char *proxy = 0;
-       char *dir_prefix=NULL;
-       char *s, buf[512];
-       char extra_headers[1024];
-       char *extra_headers_ptr = extra_headers;
-       int extra_headers_left = sizeof(extra_headers);
+       char buf[512];
        struct host_info server, target;
        struct sockaddr_in s_in;
+       int n, status;
+       int port;
+       int try = 5;
+       unsigned opt;
+       char *s;
+       char *proxy = 0;
+       char *dir_prefix = NULL;
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
+       char *extra_headers = NULL;
        llist_t *headers_llist = NULL;
+#endif
 
        /* server.allocated = target.allocated = NULL; */
 
@@ -138,32 +117,57 @@ int wget_main(int argc, char **argv)
        /*
         * Crack command line.
         */
-       opt_complementary = "-1:\203::";
+       enum {
+               WGET_OPT_CONTINUE   = 0x1,
+               WGET_OPT_QUIET      = 0x2,
+               WGET_OPT_OUTNAME    = 0x4,
+               WGET_OPT_PREFIX     = 0x8,
+               WGET_OPT_PROXY      = 0x10,
+               WGET_OPT_USER_AGENT = 0x20,
+               WGET_OPT_PASSIVE    = 0x40,
+               WGET_OPT_HEADER     = 0x80,
+       };
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
+       static const struct option wget_long_options[] = {
+               // name, has_arg, flag, val
+               { "continue",         no_argument, NULL, 'c' },
+               { "quiet",            no_argument, NULL, 'q' },
+               { "output-document",  required_argument, NULL, 'O' },
+               { "directory-prefix", required_argument, NULL, 'P' },
+               { "proxy",            required_argument, NULL, 'Y' },
+               { "user-agent",       required_argument, NULL, 'U' },
+               { "passive-ftp",      no_argument, NULL, 0xff },
+               { "header",           required_argument, NULL, 0xfe },
+               { 0, 0, 0, 0 }
+};
        applet_long_options = wget_long_options;
 #endif
-       opt = getopt32(argc, argv, "cq\213O:\203:P:Y:U:",
-                                       &fname_out, &headers_llist,
-                                       &dir_prefix, &proxy_flag, &user_agent);
+       opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
+       opt = getopt32(argc, argv, "cqO:P:Y:U:",
+                               &fname_out, &dir_prefix,
+                               &proxy_flag, &user_agent
+                               USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
+                               );
        if (strcmp(proxy_flag, "off") == 0) {
                /* Use the proxy if necessary. */
                use_proxy = 0;
        }
-       if (opt & WGET_OPT_HEADER) {
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
+       if (headers_llist) {
+               int size = 1;
+               char *cp;
+               llist_t *ll = headers_llist = rev_llist(headers_llist);
+               while (ll) {
+                       size += strlen(ll->data) + 2;
+                       ll = ll->link;
+               }
+               extra_headers = cp = xmalloc(size);
                while (headers_llist) {
-                       int arglen = strlen(headers_llist->data);
-                       if (extra_headers_left - arglen - 2 <= 0)
-                               bb_error_msg_and_die("extra_headers buffer too small "
-                                       "(need %i)", extra_headers_left - arglen);
-                       strcpy(extra_headers_ptr, headers_llist->data);
-                       extra_headers_ptr += arglen;
-                       extra_headers_left -= ( arglen + 2 );
-                       *extra_headers_ptr++ = '\r';
-                       *extra_headers_ptr++ = '\n';
-                       *(extra_headers_ptr + 1) = 0;
+                       cp += sprintf(cp, "%s\r\n", headers_llist->data);
                        headers_llist = headers_llist->link;
                }
        }
+#endif
 
        parse_url(argv[optind], &target);
        server.host = target.host;
@@ -219,9 +223,7 @@ int wget_main(int argc, char **argv)
        } else if (opt & WGET_OPT_CONTINUE) {
                output_fd = open(fname_out, O_WRONLY);
                if (output_fd >= 0) {
-                       beg_range = lseek(output_fd, 0, SEEK_END);
-                       if (beg_range == (off_t)-1)
-                               bb_perror_msg_and_die("lseek");
+                       beg_range = xlseek(output_fd, 0, SEEK_END);
                }
                /* File doesn't exist. We do not create file here yet.
                   We are not sure it exists on remove side */
@@ -284,41 +286,47 @@ int wget_main(int argc, char **argv)
 #endif
 
                        if (beg_range)
-                               fprintf(sfp, "Range: bytes="OFF_FMT"-\r\n", beg_range);
-                       if(extra_headers_left < sizeof(extra_headers))
-                               fputs(extra_headers,sfp);
-                       fprintf(sfp,"Connection: close\r\n\r\n");
+                               fprintf(sfp, "Range: bytes=%"OFF_FMT"-\r\n", beg_range);
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
+                       if (extra_headers)
+                               fputs(extra_headers, sfp);
+#endif
+                       fprintf(sfp, "Connection: close\r\n\r\n");
 
                        /*
                        * Retrieve HTTP response line and check for "200" status code.
                        */
-read_response:
+ read_response:
                        if (fgets(buf, sizeof(buf), sfp) == NULL)
                                bb_error_msg_and_die("no response from server");
 
                        s = buf;
                        while (*s != '\0' && !isspace(*s)) ++s;
-                       while (isspace(*s)) ++s;
-                       switch (status = xatoi(s)) {
-                               case 0:
-                               case 100:
-                                       while (gethdr(buf, sizeof(buf), sfp, &n) != NULL)
-                                               /* eat all remaining headers */;
-                                       goto read_response;
-                               case 200:
-                                       break;
-                               case 300:       /* redirection */
-                               case 301:
-                               case 302:
-                               case 303:
+                       s = skip_whitespace(s);
+                       // FIXME: no error check
+                       // xatou wouldn't work: "200 OK"
+                       status = atoi(s);
+                       switch (status) {
+                       case 0:
+                       case 100:
+                               while (gethdr(buf, sizeof(buf), sfp, &n) != NULL)
+                                       /* eat all remaining headers */;
+                               goto read_response;
+                       case 200:
+                               break;
+                       case 300:       /* redirection */
+                       case 301:
+                       case 302:
+                       case 303:
+                               break;
+                       case 206:
+                               if (beg_range)
                                        break;
-                               case 206:
-                                       if (beg_range)
-                                               break;
-                                       /*FALLTHRU*/
-                               default:
-                                       chomp(buf);
-                                       bb_error_msg_and_die("server returned error %s: %s", s, buf);
+                               /*FALLTHRU*/
+                       default:
+                               /* Show first line only and kill any ESC tricks */
+                               buf[strcspn(buf, "\n\r\x1b")] = '\0';
+                               bb_error_msg_and_die("server returned error: %s", buf);
                        }
 
                        /*
@@ -333,11 +341,9 @@ read_response:
                                        continue;
                                }
                                if (strcasecmp(buf, "transfer-encoding") == 0) {
-                                       if (strcasecmp(s, "chunked") == 0) {
-                                               chunked = got_clen = 1;
-                                       } else {
+                                       if (strcasecmp(s, "chunked") != 0)
                                                bb_error_msg_and_die("server wants to do %s transfer encoding", s);
-                                       }
+                                       chunked = got_clen = 1;
                                }
                                if (strcasecmp(buf, "location") == 0) {
                                        if (s[0] == '/')
@@ -379,14 +385,14 @@ read_response:
                if (s)
                        *(s++) = '\0';
                switch (ftpcmd("USER ", target.user, sfp, buf)) {
-                       case 230:
+               case 230:
+                       break;
+               case 331:
+                       if (ftpcmd("PASS ", s, sfp, buf) == 230)
                                break;
-                       case 331:
-                               if (ftpcmd("PASS ", s, sfp, buf) == 230)
-                                       break;
-                               /* FALLTHRU (failed login) */
-                       default:
-                               bb_error_msg_and_die("ftp login: %s", buf+4);
+                       /* FALLTHRU (failed login) */
+               default:
+                       bb_error_msg_and_die("ftp login: %s", buf+4);
                }
 
                ftpcmd("TYPE I", NULL, sfp, buf);
@@ -404,24 +410,31 @@ read_response:
                /*
                 * Entering passive mode
                 */
-               if (ftpcmd("PASV", NULL, sfp, buf) !=  227)
-                       bb_error_msg_and_die("PASV: %s", buf+4);
+               if (ftpcmd("PASV", NULL, sfp, buf) != 227) {
+ pasv_error:
+                       bb_error_msg_and_die("bad response to %s: %s", "PASV", buf);
+               }
+               // Response is "227 garbageN1,N2,N3,N4,P1,P2
+               // Server's IP is N1.N2.N3.N4 (we ignore it)
+               // Server's port for data connection is P1*256+P2
                s = strrchr(buf, ',');
-               *s = 0;
+               if (!s) goto pasv_error;
                port = xatol_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);
 
                if (beg_range) {
-                       sprintf(buf, "REST "OFF_FMT, beg_range);
+                       sprintf(buf, "REST %"OFF_FMT, 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("RETR: %s", buf+4);
+                       bb_error_msg_and_die("bad response to %s: %s", "RETR", buf);
        }
 
 
@@ -512,7 +525,7 @@ static void parse_url(char *src_url, struct host_info *h)
        //   and saves 'index.html?var=a%2Fb' (we save 'b')
        // wget 'http://busybox.net?login=john@doe':
        //   request: 'GET /?login=john@doe HTTP/1.0'
-       //   saves: 'index.html?login=john@doe' (we save ?login=john@doe)
+       //   saves: 'index.html?login=john@doe' (we save '?login=john@doe')
        // wget 'http://busybox.net#test/test':
        //   request: 'GET / HTTP/1.0'
        //   saves: 'index.html' (we save 'test')
@@ -573,7 +586,9 @@ static FILE *open_socket(struct sockaddr_in *s_in)
 {
        FILE *fp;
 
-       fp = fdopen(xconnect(s_in), "r+");
+       /* glibc 2.4 seems to try seeking on it - ??! */
+       /* hopefully it understands what ESPIPE means... */
+       fp = fdopen(xconnect_tcp_v4(s_in), "r+");
        if (fp == NULL)
                bb_perror_msg_and_die("fdopen");
 
@@ -630,8 +645,9 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
 
 static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
 {
+       int result;
        if (s1) {
-               if (!s2) s2="";
+               if (!s2) s2 = "";
                fprintf(fp, "%s%s\r\n", s1, s2);
                fflush(fp);
        }
@@ -640,7 +656,7 @@ static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
                char *buf_ptr;
 
                if (fgets(buf, 510, fp) == NULL) {
-                       bb_perror_msg_and_die("fgets");
+                       bb_perror_msg_and_die("error getting response");
                }
                buf_ptr = strstr(buf, "\r\n");
                if (buf_ptr) {
@@ -648,7 +664,10 @@ static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
                }
        } while (!isdigit(buf[0]) || buf[3] != ' ');
 
-       return xatoi(buf);
+       buf[3] = '\0';
+       result = xatoi_u(buf);
+       buf[3] = ' ';
+       return result;
 }
 
 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
@@ -660,7 +679,7 @@ getttywidth(void)
 {
        int width=0;
        get_terminal_width_height(0, &width, NULL);
-       return (width);
+       return width;
 }
 
 static void