+ buf[3] = '\0';
+ result = xatoi_u(buf);
+ buf[3] = ' ';
+ return result;
+}
+
+
+static void parse_url(char *src_url, struct host_info *h)
+{
+ char *url, *p, *sp;
+
+ /* h->allocated = */ url = xstrdup(src_url);
+
+ if (strncmp(url, "http://", 7) == 0) {
+ h->port = bb_lookup_port("http", "tcp", 80);
+ h->host = url + 7;
+ h->is_ftp = 0;
+ } else if (strncmp(url, "ftp://", 6) == 0) {
+ h->port = bb_lookup_port("ftp", "tcp", 21);
+ h->host = url + 6;
+ h->is_ftp = 1;
+ } else
+ bb_error_msg_and_die("not an http or ftp url: %s", url);
+
+ // FYI:
+ // "Real" wget 'http://busybox.net?var=a/b' sends this request:
+ // 'GET /?var=a/b HTTP 1.0'
+ // 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')
+ // wget 'http://busybox.net#test/test':
+ // request: 'GET / HTTP/1.0'
+ // saves: 'index.html' (we save 'test')
+ //
+ // We also don't add unique .N suffix if file exists...
+ sp = strchr(h->host, '/');
+ p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
+ p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
+ if (!sp) {
+ h->path = "";
+ } else if (*sp == '/') {
+ *sp = '\0';
+ h->path = sp + 1;
+ } else { // '#' or '?'
+ // http://busybox.net?login=john@doe is a valid URL
+ // memmove converts to:
+ // http:/busybox.nett?login=john@doe...
+ memmove(h->host - 1, h->host, sp - h->host);
+ h->host--;
+ sp[-1] = '\0';
+ h->path = sp;
+ }
+
+ sp = strrchr(h->host, '@');
+ h->user = NULL;
+ if (sp != NULL) {
+ h->user = h->host;
+ *sp = '\0';
+ h->host = sp + 1;
+ }
+
+ sp = h->host;
+}
+
+
+static char *gethdr(char *buf, size_t bufsiz, FILE *fp /*, int *istrunc*/)
+{
+ char *s, *hdrval;
+ int c;
+
+ /* *istrunc = 0; */
+
+ /* retrieve header line */
+ if (fgets(buf, bufsiz, fp) == NULL)
+ return NULL;
+
+ /* see if we are at the end of the headers */
+ for (s = buf; *s == '\r'; ++s)
+ continue;
+ if (*s == '\n')
+ return NULL;
+
+ /* convert the header name to lower case */
+ for (s = buf; isalnum(*s) || *s == '-' || *s == '.'; ++s)
+ *s = tolower(*s);
+
+ /* verify we are at the end of the header name */
+ if (*s != ':')
+ bb_error_msg_and_die("bad header line: %s", buf);
+
+ /* locate the start of the header value */
+ *s++ = '\0';
+ hdrval = skip_whitespace(s);
+
+ /* locate the end of header */
+ while (*s && *s != '\r' && *s != '\n')
+ ++s;
+
+ /* end of header found */
+ if (*s) {
+ *s = '\0';
+ return hdrval;
+ }
+
+ /* Rats! The buffer isn't big enough to hold the entire header value. */
+ while (c = getc(fp), c != EOF && c != '\n')
+ continue;
+ /* *istrunc = 1; */
+ return hdrval;
+}
+
+
+int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int wget_main(int argc ATTRIBUTE_UNUSED, char **argv)
+{
+ char buf[512];
+ struct host_info server, target;
+ len_and_sockaddr *lsa;
+ int status;
+ int port;
+ int try = 5;
+ unsigned opt;
+ char *str;
+ char *proxy = 0;
+ char *dir_prefix = NULL;
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
+ char *extra_headers = NULL;
+ llist_t *headers_llist = NULL;
+#endif
+ FILE *sfp = NULL; /* socket to web/ftp server */
+ FILE *dfp; /* socket to ftp server (data) */
+ char *fname_out; /* where to direct output (-O) */
+ bool got_clen = 0; /* got content-length: from server */
+ int output_fd = -1;
+ bool use_proxy = 1; /* Use proxies if env vars are set */
+ const char *proxy_flag = "on"; /* Use proxies if env vars are set */
+ const char *user_agent = "Wget";/* "User-Agent" header field */
+
+ static const char keywords[] ALIGN1 =
+ "content-length\0""transfer-encoding\0""chunked\0""location\0";
+ enum {
+ KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location
+ };
+ enum {
+ WGET_OPT_CONTINUE = 0x1,
+ WGET_OPT_SPIDER = 0x2,
+ WGET_OPT_QUIET = 0x4,
+ WGET_OPT_OUTNAME = 0x8,
+ WGET_OPT_PREFIX = 0x10,
+ WGET_OPT_PROXY = 0x20,
+ WGET_OPT_USER_AGENT = 0x40,
+ WGET_OPT_PASSIVE = 0x80,
+ WGET_OPT_HEADER = 0x100,
+ };
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
+ static const char wget_longopts[] ALIGN1 =
+ /* name, has_arg, val */
+ "continue\0" No_argument "c"
+ "spider\0" No_argument "s"
+ "quiet\0" No_argument "q"
+ "output-document\0" Required_argument "O"
+ "directory-prefix\0" Required_argument "P"
+ "proxy\0" Required_argument "Y"
+ "user-agent\0" Required_argument "U"
+ "passive-ftp\0" No_argument "\xff"
+ "header\0" Required_argument "\xfe"
+ ;
+#endif
+
+ INIT_G();
+
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
+ applet_long_options = wget_longopts;
+#endif
+ /* server.allocated = target.allocated = NULL; */
+ opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
+ opt = getopt32(argv, "csqO:P:Y:U:" /*ignored:*/ "t:T:",
+ &fname_out, &dir_prefix,
+ &proxy_flag, &user_agent,
+ NULL, /* -t RETRIES */
+ NULL /* -T NETWORK_READ_TIMEOUT */
+ USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
+ );
+ if (strcmp(proxy_flag, "off") == 0) {
+ /* Use the proxy if necessary */
+ use_proxy = 0;
+ }
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
+ if (headers_llist) {
+ int size = 1;
+ char *cp;
+ llist_t *ll = headers_llist;
+ while (ll) {
+ size += strlen(ll->data) + 2;
+ ll = ll->link;
+ }
+ extra_headers = cp = xmalloc(size);
+ while (headers_llist) {
+ cp += sprintf(cp, "%s\r\n", (char*)llist_pop(&headers_llist));
+ }
+ }
+#endif
+
+ parse_url(argv[optind], &target);
+ server.host = target.host;
+ server.port = target.port;
+
+ /* Use the proxy if necessary */
+ if (use_proxy) {
+ proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
+ if (proxy && *proxy) {
+ parse_url(proxy, &server);
+ } else {
+ use_proxy = 0;
+ }
+ }
+
+ /* Guess an output filename, if there was no -O FILE */
+ if (!(opt & WGET_OPT_OUTNAME)) {
+ fname_out = bb_get_last_path_component_nostrip(target.path);
+ /* handle "wget http://kernel.org//" */
+ if (fname_out[0] == '/' || !fname_out[0])
+ fname_out = (char*)"index.html";
+ /* -P DIR is considered only if there was no -O FILE */
+ if (dir_prefix)
+ fname_out = concat_path_file(dir_prefix, fname_out);
+ } else {
+ if (LONE_DASH(fname_out)) {
+ /* -O - */
+ output_fd = 1;
+ opt &= ~WGET_OPT_CONTINUE;
+ }
+ }
+#if ENABLE_FEATURE_WGET_STATUSBAR
+ curfile = bb_get_last_path_component_nostrip(fname_out);
+#endif
+
+ /* Impossible?
+ if ((opt & WGET_OPT_CONTINUE) && !fname_out)
+ bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
+
+ /* Determine where to start transfer */
+ if (opt & WGET_OPT_CONTINUE) {
+ output_fd = open(fname_out, O_WRONLY);
+ if (output_fd >= 0) {
+ 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 */
+ }