]> sjero.net Git - wget/commitdiff
[svn] Simplify rewrite_shorthand_url.
authorhniksic <devnull@localhost>
Fri, 8 Jul 2005 06:31:26 +0000 (23:31 -0700)
committerhniksic <devnull@localhost>
Fri, 8 Jul 2005 06:31:26 +0000 (23:31 -0700)
src/ChangeLog
src/url.c

index 99bfc20b3f21a43f3815c08d98e547b6d31a06cd..dbc9832244689f40cf89b274f6da9e8656309caa 100644 (file)
@@ -1,3 +1,8 @@
+2005-07-08  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * url.c (rewrite_shorthand_url): Simplify code using aprintf and
+       strspn.
+
 2005-07-07  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * gnutls.c (ssl_check_certificate): Check for the validity of the
index 0a23df32d38ba2806cb8ae0f20a0587f802d6016..a4d7bdb10b4de4b6c31e565031eaf19283c51aef 100644 (file)
--- 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;
 }
 \f
 static void split_path (const char *, char **, char **);