1 /* vi: set sw=4 ts=4: */
3 * wget - retrieve a file using HTTP or FTP
5 * Chip Rosenthal Covad Communications <chip@laserlink.net>
6 * Licensed under GPLv2, see file LICENSE in this source tree.
8 * Copyright (C) 2010 Bradley M. Kuhn <bkuhn@ebb.org>
9 * Kuhn's copyrights are licensed GPLv2-or-later. File as a whole remains GPLv2.
13 //#define log_io(...) bb_error_msg(__VA_ARGS__)
14 #define log_io(...) ((void)0)
29 off_t content_len; /* Content-length of the file */
30 off_t beg_range; /* Range at which continue begins */
31 #if ENABLE_FEATURE_WGET_STATUSBAR
32 off_t transferred; /* Number of bytes transferred so far */
33 const char *curfile; /* Name of current file being transferred */
37 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
41 char *fname_out; /* where to direct output (-O) */
42 const char *proxy_flag; /* Use proxies if env vars are set */
43 const char *user_agent; /* "User-Agent" header field */
44 #if ENABLE_FEATURE_WGET_TIMEOUT
45 unsigned timeout_seconds;
49 smallint chunked; /* chunked transfer encoding */
50 smallint got_clen; /* got content-length: from server */
51 /* Local downloads do benefit from big buffer.
52 * With 512 byte buffer, it was measured to be
53 * an order of magnitude slower than with big one.
55 uint64_t just_to_align_next_member;
56 char wget_buf[CONFIG_FEATURE_COPYBUF_KB*1024];
58 #define G (*ptr_to_globals)
59 #define INIT_G() do { \
60 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
61 IF_FEATURE_WGET_TIMEOUT(G.timeout_seconds = 900;) \
65 /* Must match option string! */
67 WGET_OPT_CONTINUE = (1 << 0),
68 WGET_OPT_SPIDER = (1 << 1),
69 WGET_OPT_QUIET = (1 << 2),
70 WGET_OPT_OUTNAME = (1 << 3),
71 WGET_OPT_PREFIX = (1 << 4),
72 WGET_OPT_PROXY = (1 << 5),
73 WGET_OPT_USER_AGENT = (1 << 6),
74 WGET_OPT_NETWORK_READ_TIMEOUT = (1 << 7),
75 WGET_OPT_RETRIES = (1 << 8),
76 WGET_OPT_PASSIVE = (1 << 9),
77 WGET_OPT_HEADER = (1 << 10) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
78 WGET_OPT_POST_DATA = (1 << 11) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
86 #if ENABLE_FEATURE_WGET_STATUSBAR
87 static void progress_meter(int flag)
89 if (option_mask32 & WGET_OPT_QUIET)
92 if (flag == PROGRESS_START)
93 bb_progress_init(&G.pmt, G.curfile);
95 bb_progress_update(&G.pmt,
98 (G.chunked || !G.got_clen) ? 0 : G.beg_range + G.transferred + G.content_len
101 if (flag == PROGRESS_END) {
102 bb_progress_free(&G.pmt);
103 bb_putchar_stderr('\n');
108 static ALWAYS_INLINE void progress_meter(int flag UNUSED_PARAM) { }
112 /* IPv6 knows scoped address types i.e. link and site local addresses. Link
113 * local addresses can have a scope identifier to specify the
114 * interface/link an address is valid on (e.g. fe80::1%eth0). This scope
115 * identifier is only valid on a single node.
117 * RFC 4007 says that the scope identifier MUST NOT be sent across the wire,
118 * unless all nodes agree on the semantic. Apache e.g. regards zone identifiers
119 * in the Host header as invalid requests, see
120 * https://issues.apache.org/bugzilla/show_bug.cgi?id=35122
122 static void strip_ipv6_scope_id(char *host)
126 /* bbox wget actually handles IPv6 addresses without [], like
127 * wget "http://::1/xxx", but this is not standard.
128 * To save code, _here_ we do not support it. */
131 return; /* not IPv6 */
133 scope = strchr(host, '%');
137 /* Remove the IPv6 zone identifier from the host address */
138 cp = strchr(host, ']');
139 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
140 /* malformed address (not "[xx]:nn" or "[xx]") */
144 /* cp points to "]...", scope points to "%eth0]..." */
145 overlapping_strcpy(scope, cp);
148 #if ENABLE_FEATURE_WGET_AUTHENTICATION
149 /* Base64-encode character string. */
150 static char *base64enc(const char *str)
152 unsigned len = strlen(str);
153 if (len > sizeof(G.wget_buf)/4*3 - 10) /* paranoia */
154 len = sizeof(G.wget_buf)/4*3 - 10;
155 bb_uuencode(G.wget_buf, str, len, bb_uuenc_tbl_base64);
160 static char* sanitize_string(char *s)
162 unsigned char *p = (void *) s;
169 static FILE *open_socket(len_and_sockaddr *lsa)
173 /* glibc 2.4 seems to try seeking on it - ??! */
174 /* hopefully it understands what ESPIPE means... */
175 fp = fdopen(xconnect_stream(lsa), "r+");
177 bb_perror_msg_and_die(bb_msg_memory_exhausted);
182 /* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */
183 static char fgets_and_trim(FILE *fp)
188 if (fgets(G.wget_buf, sizeof(G.wget_buf) - 1, fp) == NULL)
189 bb_perror_msg_and_die("error getting response");
191 buf_ptr = strchrnul(G.wget_buf, '\n');
194 buf_ptr = strchrnul(G.wget_buf, '\r');
197 log_io("< %s", G.wget_buf);
202 static int ftpcmd(const char *s1, const char *s2, FILE *fp)
208 fprintf(fp, "%s%s\r\n", s1, s2);
210 log_io("> %s%s", s1, s2);
215 } while (!isdigit(G.wget_buf[0]) || G.wget_buf[3] != ' ');
217 G.wget_buf[3] = '\0';
218 result = xatoi_positive(G.wget_buf);
223 static void parse_url(const char *src_url, struct host_info *h)
228 h->allocated = url = xstrdup(src_url);
230 if (strncmp(url, "http://", 7) == 0) {
231 h->port = bb_lookup_port("http", "tcp", 80);
234 } else if (strncmp(url, "ftp://", 6) == 0) {
235 h->port = bb_lookup_port("ftp", "tcp", 21);
239 bb_error_msg_and_die("not an http or ftp url: %s", sanitize_string(url));
242 // "Real" wget 'http://busybox.net?var=a/b' sends this request:
243 // 'GET /?var=a/b HTTP 1.0'
244 // and saves 'index.html?var=a%2Fb' (we save 'b')
245 // wget 'http://busybox.net?login=john@doe':
246 // request: 'GET /?login=john@doe HTTP/1.0'
247 // saves: 'index.html?login=john@doe' (we save '?login=john@doe')
248 // wget 'http://busybox.net#test/test':
249 // request: 'GET / HTTP/1.0'
250 // saves: 'index.html' (we save 'test')
252 // We also don't add unique .N suffix if file exists...
253 sp = strchr(h->host, '/');
254 p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
255 p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
258 } else if (*sp == '/') {
261 } else { // '#' or '?'
262 // http://busybox.net?login=john@doe is a valid URL
263 // memmove converts to:
264 // http:/busybox.nett?login=john@doe...
265 memmove(h->host - 1, h->host, sp - h->host);
271 // We used to set h->user to NULL here, but this interferes
272 // with handling of code 302 ("object was moved")
274 sp = strrchr(h->host, '@');
284 static char *gethdr(FILE *fp)
291 /* retrieve header line */
292 c = fgets_and_trim(fp);
294 /* end of the headers? */
295 if (G.wget_buf[0] == '\0')
298 /* convert the header name to lower case */
299 for (s = G.wget_buf; isalnum(*s) || *s == '-' || *s == '.'; ++s) {
300 /* tolower for "A-Z", no-op for "0-9a-z-." */
304 /* verify we are at the end of the header name */
306 bb_error_msg_and_die("bad header line: %s", sanitize_string(G.wget_buf));
308 /* locate the start of the header value */
310 hdrval = skip_whitespace(s);
313 /* Rats! The buffer isn't big enough to hold the entire header value */
314 while (c = getc(fp), c != EOF && c != '\n')
321 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
322 static char *URL_escape(const char *str)
324 /* URL encode, see RFC 2396 */
326 char *res = dst = xmalloc(strlen(str) * 3 + 1);
332 /* || strchr("!&'()*-.=_~", c) - more code */
344 || (c >= '0' && c <= '9')
345 || ((c|0x20) >= 'a' && (c|0x20) <= 'z')
352 *dst++ = bb_hexdigits_upcase[c >> 4];
353 *dst++ = bb_hexdigits_upcase[c & 0xf];
359 static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_sockaddr *lsa)
366 target->user = xstrdup("anonymous:busybox@");
368 sfp = open_socket(lsa);
369 if (ftpcmd(NULL, NULL, sfp) != 220)
370 bb_error_msg_and_die("%s", sanitize_string(G.wget_buf + 4));
373 * Splitting username:password pair,
376 str = strchr(target->user, ':');
379 switch (ftpcmd("USER ", target->user, sfp)) {
383 if (ftpcmd("PASS ", str, sfp) == 230)
385 /* fall through (failed login) */
387 bb_error_msg_and_die("ftp login: %s", sanitize_string(G.wget_buf + 4));
390 ftpcmd("TYPE I", NULL, sfp);
395 if (ftpcmd("SIZE ", target->path, sfp) == 213) {
396 G.content_len = BB_STRTOOFF(G.wget_buf + 4, NULL, 10);
397 if (G.content_len < 0 || errno) {
398 bb_error_msg_and_die("SIZE value is garbage");
404 * Entering passive mode
406 if (ftpcmd("PASV", NULL, sfp) != 227) {
408 bb_error_msg_and_die("bad response to %s: %s", "PASV", sanitize_string(G.wget_buf));
410 // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
411 // Server's IP is N1.N2.N3.N4 (we ignore it)
412 // Server's port for data connection is P1*256+P2
413 str = strrchr(G.wget_buf, ')');
414 if (str) str[0] = '\0';
415 str = strrchr(G.wget_buf, ',');
416 if (!str) goto pasv_error;
417 port = xatou_range(str+1, 0, 255);
419 str = strrchr(G.wget_buf, ',');
420 if (!str) goto pasv_error;
421 port += xatou_range(str+1, 0, 255) * 256;
422 set_nport(lsa, htons(port));
424 *dfpp = open_socket(lsa);
427 sprintf(G.wget_buf, "REST %"OFF_FMT"u", G.beg_range);
428 if (ftpcmd(G.wget_buf, NULL, sfp) == 350)
429 G.content_len -= G.beg_range;
432 if (ftpcmd("RETR ", target->path, sfp) > 150)
433 bb_error_msg_and_die("bad response to %s: %s", "RETR", sanitize_string(G.wget_buf));
438 static void NOINLINE retrieve_file_data(FILE *dfp)
440 #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
441 # if ENABLE_FEATURE_WGET_TIMEOUT
444 struct pollfd polldata;
446 polldata.fd = fileno(dfp);
447 polldata.events = POLLIN | POLLPRI;
449 progress_meter(PROGRESS_START);
454 /* Loops only if chunked */
457 #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
458 /* Must use nonblocking I/O, otherwise fread will loop
459 * and *block* until it reads full buffer,
460 * which messes up progress bar and/or timeout logic.
461 * Because of nonblocking I/O, we need to dance
462 * very carefully around EAGAIN. See explanation at
465 ndelay_on(polldata.fd);
471 rdsz = sizeof(G.wget_buf);
473 if (G.content_len < (off_t)sizeof(G.wget_buf)) {
474 if ((int)G.content_len <= 0)
476 rdsz = (unsigned)G.content_len;
480 #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
481 # if ENABLE_FEATURE_WGET_TIMEOUT
482 second_cnt = G.timeout_seconds;
485 if (safe_poll(&polldata, 1, 1000) != 0)
486 break; /* error, EOF, or data is available */
487 # if ENABLE_FEATURE_WGET_TIMEOUT
488 if (second_cnt != 0 && --second_cnt == 0) {
489 progress_meter(PROGRESS_END);
490 bb_error_msg_and_die("download timed out");
493 /* Needed for "stalled" indicator */
494 progress_meter(PROGRESS_BUMP);
497 /* fread internally uses read loop, which in our case
498 * is usually exited when we get EAGAIN.
499 * In this case, libc sets error marker on the stream.
500 * Need to clear it before next fread to avoid possible
501 * rare false positive ferror below. Rare because usually
502 * fread gets more than zero bytes, and we don't fall
503 * into if (n <= 0) ...
508 n = fread(G.wget_buf, 1, rdsz, dfp);
510 * If error occurs, or EOF is reached, the return value
511 * is a short item count (or zero).
512 * fread does not distinguish between EOF and error.
515 #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
516 if (errno == EAGAIN) /* poll lied, there is no data? */
520 bb_perror_msg_and_die(bb_msg_read_error);
521 break; /* EOF, not error */
524 xwrite(G.output_fd, G.wget_buf, n);
526 #if ENABLE_FEATURE_WGET_STATUSBAR
528 progress_meter(PROGRESS_BUMP);
532 if (G.content_len == 0)
536 #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
538 ndelay_off(polldata.fd); /* else fgets can get very unhappy */
543 fgets_and_trim(dfp); /* Eat empty line */
546 G.content_len = STRTOOFF(G.wget_buf, NULL, 16);
547 /* FIXME: error check? */
548 if (G.content_len == 0)
549 break; /* all done! */
553 /* Draw full bar and free its resources */
554 G.chunked = 0; /* makes it show 100% even for chunked download */
555 G.got_clen = 1; /* makes it show 100% even for download of (formerly) unknown size */
556 progress_meter(PROGRESS_END);
559 static void download_one_url(const char *url)
561 bool use_proxy; /* Use proxies if env vars are set */
563 len_and_sockaddr *lsa;
564 FILE *sfp; /* socket to web/ftp server */
565 FILE *dfp; /* socket to ftp server (data) */
567 char *fname_out_alloc;
568 struct host_info server;
569 struct host_info target;
571 server.allocated = NULL;
572 target.allocated = NULL;
576 parse_url(url, &target);
578 /* Use the proxy if necessary */
579 use_proxy = (strcmp(G.proxy_flag, "off") != 0);
581 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
582 use_proxy = (proxy && proxy[0]);
584 parse_url(proxy, &server);
587 server.port = target.port;
588 if (ENABLE_FEATURE_IPV6) {
589 //free(server.allocated); - can't be non-NULL
590 server.host = server.allocated = xstrdup(target.host);
592 server.host = target.host;
596 if (ENABLE_FEATURE_IPV6)
597 strip_ipv6_scope_id(target.host);
599 /* If there was no -O FILE, guess output filename */
600 fname_out_alloc = NULL;
601 if (!(option_mask32 & WGET_OPT_OUTNAME)) {
602 G.fname_out = bb_get_last_path_component_nostrip(target.path);
603 /* handle "wget http://kernel.org//" */
604 if (G.fname_out[0] == '/' || !G.fname_out[0])
605 G.fname_out = (char*)"index.html";
606 /* -P DIR is considered only if there was no -O FILE */
608 G.fname_out = fname_out_alloc = concat_path_file(G.dir_prefix, G.fname_out);
610 #if ENABLE_FEATURE_WGET_STATUSBAR
611 G.curfile = bb_get_last_path_component_nostrip(G.fname_out);
614 /* Determine where to start transfer */
616 if (option_mask32 & WGET_OPT_CONTINUE) {
617 G.output_fd = open(G.fname_out, O_WRONLY);
618 if (G.output_fd >= 0) {
619 G.beg_range = xlseek(G.output_fd, 0, SEEK_END);
621 /* File doesn't exist. We do not create file here yet.
622 * We are not sure it exists on remote side */
627 lsa = xhost2sockaddr(server.host, server.port);
628 if (!(option_mask32 & WGET_OPT_QUIET)) {
629 char *s = xmalloc_sockaddr2dotted(&lsa->u.sa);
630 fprintf(stderr, "Connecting to %s (%s)\n", server.host, s);
634 /*G.content_len = 0; - redundant, got_clen = 0 is enough */
637 if (use_proxy || !target.is_ftp) {
645 /* Open socket to http server */
646 sfp = open_socket(lsa);
648 /* Send HTTP request */
650 fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
651 target.is_ftp ? "f" : "ht", target.host,
654 if (option_mask32 & WGET_OPT_POST_DATA)
655 fprintf(sfp, "POST /%s HTTP/1.1\r\n", target.path);
657 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
660 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n",
661 target.host, G.user_agent);
663 /* Ask server to close the connection as soon as we are done
664 * (IOW: we do not intend to send more requests)
666 fprintf(sfp, "Connection: close\r\n");
668 #if ENABLE_FEATURE_WGET_AUTHENTICATION
670 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n"+6,
671 base64enc(target.user));
673 if (use_proxy && server.user) {
674 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
675 base64enc(server.user));
680 fprintf(sfp, "Range: bytes=%"OFF_FMT"u-\r\n", G.beg_range);
682 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
684 fputs(G.extra_headers, sfp);
686 if (option_mask32 & WGET_OPT_POST_DATA) {
687 char *estr = URL_escape(G.post_data);
689 "Content-Type: application/x-www-form-urlencoded\r\n"
690 "Content-Length: %u\r\n"
693 (int) strlen(estr), estr
699 fprintf(sfp, "\r\n");
705 * Retrieve HTTP response line and check for "200" status code.
711 str = skip_non_whitespace(str);
712 str = skip_whitespace(str);
713 // FIXME: no error check
714 // xatou wouldn't work: "200 OK"
719 while (gethdr(sfp) != NULL)
720 /* eat all remaining headers */;
724 Response 204 doesn't say "null file", it says "metadata
725 has changed but data didn't":
727 "10.2.5 204 No Content
728 The server has fulfilled the request but does not need to return
729 an entity-body, and might want to return updated metainformation.
730 The response MAY include new or updated metainformation in the form
731 of entity-headers, which if present SHOULD be associated with
732 the requested variant.
734 If the client is a user agent, it SHOULD NOT change its document
735 view from that which caused the request to be sent. This response
736 is primarily intended to allow input for actions to take place
737 without causing a change to the user agent's active document view,
738 although any new or updated metainformation SHOULD be applied
739 to the document currently in the user agent's active view.
741 The 204 response MUST NOT include a message-body, and thus
742 is always terminated by the first empty line after the header fields."
744 However, in real world it was observed that some web servers
745 (e.g. Boa/0.94.14rc21) simply use code 204 when file size is zero.
749 case 300: /* redirection */
759 bb_error_msg_and_die("server returned error: %s", sanitize_string(G.wget_buf));
763 * Retrieve HTTP headers.
765 while ((str = gethdr(sfp)) != NULL) {
766 static const char keywords[] ALIGN1 =
767 "content-length\0""transfer-encoding\0""location\0";
769 KEY_content_length = 1, KEY_transfer_encoding, KEY_location
773 /* gethdr converted "FOO:" string to lowercase */
775 /* strip trailing whitespace */
776 char *s = strchrnul(str, '\0') - 1;
777 while (s >= str && (*s == ' ' || *s == '\t')) {
781 key = index_in_strings(keywords, G.wget_buf) + 1;
782 if (key == KEY_content_length) {
783 G.content_len = BB_STRTOOFF(str, NULL, 10);
784 if (G.content_len < 0 || errno) {
785 bb_error_msg_and_die("content-length %s is garbage", sanitize_string(str));
790 if (key == KEY_transfer_encoding) {
791 if (strcmp(str_tolower(str), "chunked") != 0)
792 bb_error_msg_and_die("transfer encoding '%s' is not supported", sanitize_string(str));
795 if (key == KEY_location && status >= 300) {
796 if (--redir_limit == 0)
797 bb_error_msg_and_die("too many redirections");
800 free(target.allocated);
801 target.path = target.allocated = xstrdup(str+1);
802 /* lsa stays the same: it's on the same server */
804 parse_url(str, &target);
806 free(server.allocated);
807 server.allocated = NULL;
808 server.host = target.host;
809 /* strip_ipv6_scope_id(target.host); - no! */
810 /* we assume remote never gives us IPv6 addr with scope id */
811 server.port = target.port;
814 } /* else: lsa stays the same: we use proxy */
816 goto establish_session;
819 // if (status >= 300)
820 // bb_error_msg_and_die("bad redirection (no Location: header from server)");
822 /* For HTTP, data is pumped over the same connection */
829 sfp = prepare_ftp_session(&dfp, &target, lsa);
834 if (!(option_mask32 & WGET_OPT_SPIDER)) {
836 G.output_fd = xopen(G.fname_out, G.o_flags);
837 retrieve_file_data(dfp);
838 if (!(option_mask32 & WGET_OPT_OUTNAME)) {
845 /* It's ftp. Close data connection properly */
847 if (ftpcmd(NULL, NULL, sfp) != 226)
848 bb_error_msg_and_die("ftp error: %s", sanitize_string(G.wget_buf + 4));
849 /* ftpcmd("QUIT", NULL, sfp); - why bother? */
853 free(server.allocated);
854 free(target.allocated);
855 free(fname_out_alloc);
858 int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
859 int wget_main(int argc UNUSED_PARAM, char **argv)
861 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
862 static const char wget_longopts[] ALIGN1 =
863 /* name, has_arg, val */
864 "continue\0" No_argument "c"
865 //FIXME: -s isn't --spider, it's --save-headers!
866 "spider\0" No_argument "s"
867 "quiet\0" No_argument "q"
868 "output-document\0" Required_argument "O"
869 "directory-prefix\0" Required_argument "P"
870 "proxy\0" Required_argument "Y"
871 "user-agent\0" Required_argument "U"
872 #if ENABLE_FEATURE_WGET_TIMEOUT
873 "timeout\0" Required_argument "T"
876 // "tries\0" Required_argument "t"
877 /* Ignored (we always use PASV): */
878 "passive-ftp\0" No_argument "\xff"
879 "header\0" Required_argument "\xfe"
880 "post-data\0" Required_argument "\xfd"
881 /* Ignored (we don't do ssl) */
882 "no-check-certificate\0" No_argument "\xfc"
886 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
887 llist_t *headers_llist = NULL;
892 IF_FEATURE_WGET_TIMEOUT(G.timeout_seconds = 900;)
893 G.proxy_flag = "on"; /* use proxies if env vars are set */
894 G.user_agent = "Wget"; /* "User-Agent" header field */
896 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
897 applet_long_options = wget_longopts;
899 opt_complementary = "-1" IF_FEATURE_WGET_TIMEOUT(":T+") IF_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
900 getopt32(argv, "csqO:P:Y:U:T:" /*ignored:*/ "t:",
901 &G.fname_out, &G.dir_prefix,
902 &G.proxy_flag, &G.user_agent,
903 IF_FEATURE_WGET_TIMEOUT(&G.timeout_seconds) IF_NOT_FEATURE_WGET_TIMEOUT(NULL),
904 NULL /* -t RETRIES */
905 IF_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
906 IF_FEATURE_WGET_LONG_OPTIONS(, &G.post_data)
910 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
914 llist_t *ll = headers_llist;
916 size += strlen(ll->data) + 2;
919 G.extra_headers = cp = xmalloc(size);
920 while (headers_llist) {
921 cp += sprintf(cp, "%s\r\n", (char*)llist_pop(&headers_llist));
927 G.o_flags = O_WRONLY | O_CREAT | O_TRUNC | O_EXCL;
928 if (G.fname_out) { /* -O FILE ? */
929 if (LONE_DASH(G.fname_out)) { /* -O - ? */
931 option_mask32 &= ~WGET_OPT_CONTINUE;
933 /* compat with wget: -O FILE can overwrite */
934 G.o_flags = O_WRONLY | O_CREAT | O_TRUNC;
938 download_one_url(*argv++);
940 if (G.output_fd >= 0)