From: hniksic Date: Fri, 8 Jul 2005 06:31:26 +0000 (-0700) Subject: [svn] Simplify rewrite_shorthand_url. X-Git-Tag: v1.13~758 X-Git-Url: http://sjero.net/git/?p=wget;a=commitdiff_plain;h=1fa77ff5b11647106725dd556595c063cc9779f2 [svn] Simplify rewrite_shorthand_url. --- diff --git a/src/ChangeLog b/src/ChangeLog index 99bfc20b..dbc98322 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2005-07-08 Hrvoje Niksic + + * url.c (rewrite_shorthand_url): Simplify code using aprintf and + strspn. + 2005-07-07 Hrvoje Niksic * gnutls.c (ssl_check_certificate): Check for the validity of the diff --git a/src/url.c b/src/url.c index 0a23df32..a4d7bdb1 100644 --- a/src/url.c +++ b/src/url.c @@ -508,7 +508,8 @@ parse_credentials (const char *beg, const char *end, char **user, char **passwd) } /* Used by main.c: detect URLs written using the "shorthand" URL forms - popularized by Netscape and NcFTP. HTTP shorthands look like this: + originally popularized by Netscape and NcFTP. HTTP shorthands look + like this: www.foo.com[:port]/dir/file -> http://www.foo.com[:port]/dir/file www.foo.com[:port] -> http://www.foo.com[:port] @@ -524,52 +525,42 @@ char * rewrite_shorthand_url (const char *url) { const char *p; + char *ret; if (url_scheme (url) != SCHEME_INVALID) return NULL; /* Look for a ':' or '/'. The former signifies NcFTP syntax, the latter Netscape. */ - for (p = url; *p && *p != ':' && *p != '/'; p++) - ; - + p = strpbrk (url, ":/"); if (p == url) return NULL; /* If we're looking at "://", it means the URL uses a scheme we don't support, which may include "https" when compiled without SSL support. Don't bogusly rewrite such URLs. */ - if (p[0] == ':' && p[1] == '/' && p[2] == '/') + if (p && p[0] == ':' && p[1] == '/' && p[2] == '/') return NULL; - if (*p == ':') + if (p && *p == ':') { - const char *pp; - char *res; - /* If the characters after the colon and before the next slash - or end of string are all digits, it's HTTP. */ - int digits = 0; - for (pp = p + 1; ISDIGIT (*pp); pp++) - ++digits; - if (digits > 0 && (*pp == '/' || *pp == '\0')) + /* Colon indicates ftp, as in foo.bar.com:path. Check for + special case of http port number ("localhost:10000"). */ + int digits = strspn (p + 1, "0123456789"); + if (digits && (p[1 + digits] == '/' || p[1 + digits] == '\0')) goto http; - /* Prepend "ftp://" to the entire URL... */ - res = xmalloc (6 + strlen (url) + 1); - sprintf (res, "ftp://%s", url); - /* ...and replace ':' with '/'. */ - res[6 + (p - url)] = '/'; - return res; + /* Turn "foo.bar.com:path" to "ftp://foo.bar.com/path". */ + ret = aprintf ("ftp://%s", url); + ret[6 + (p - url)] = '/'; } else { - char *res; http: - /* Just prepend "http://" to what we have. */ - res = xmalloc (7 + strlen (url) + 1); - sprintf (res, "http://%s", url); - return res; + /* Just prepend "http://" to URL. */ + ret = aprintf ("http://%s", url); } + return ret; } static void split_path (const char *, char **, char **);