};
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);
{
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;
{ "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::");
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 =
/*
* Determine where to start transfer.
*/
- if (fname_out[0] == '-' && !fname_out[1]) {
+ if (LONE_DASH(fname_out)) {
output_fd = 1;
opt &= ~WGET_OPT_CONTINUE;
}
/* 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)) {
- fprintf(stderr, "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) {
* 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) {
*/
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;
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;
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);
* 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;
s = strrchr(buf, ',');
if (!s) goto pasv_error;
port += xatou_range(s+1, 0, 255) * 256;
- s_in.sin_port = htons(port);
- dfp = open_socket(&s_in);
+ set_nport(lsa, htons(port));
+ dfp = open_socket(lsa);
if (beg_range) {
sprintf(buf, "REST %"OFF_FMT"d", 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);
}
*/
if (chunked) {
fgets(buf, sizeof(buf), dfp);
- content_len = STRTOOFF(buf, (char **) NULL, 16);
+ content_len = STRTOOFF(buf, NULL, 16);
/* FIXME: error check?? */
}
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! */
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);
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:
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");
* 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 $
*/