]> sjero.net Git - wget/blobdiff - src/url.c
[svn] Minor -Wall-induced fixes. Also, skip_url is removed.
[wget] / src / url.c
index 747fb7be068bdf32f263459af1b7d3de7bdb4d71..9b110e6eec86bffff3fb29b5218984f46cd3654c 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -26,7 +26,6 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #else
 # include <strings.h>
 #endif
-#include <ctype.h>
 #include <sys/types.h>
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
@@ -43,11 +42,6 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 extern int errno;
 #endif
 
-/* Default port definitions */
-#define DEFAULT_HTTP_PORT 80
-#define DEFAULT_FTP_PORT 21
-#define DEFAULT_HTTPS_PORT 443
-
 /* Table of Unsafe chars.  This is intialized in
    init_unsafe_char_table.  */
 
@@ -77,9 +71,9 @@ static void path_simplify_with_kludge PARAMS ((char *));
 #endif
 static int urlpath_length PARAMS ((const char *));
 
-/* NULL-terminated list of strings to be recognized as prototypes (URL
-   schemes).  Note that recognized doesn't mean supported -- only HTTP,
-   HTTPS and FTP are currently supported .
+/* A NULL-terminated list of strings to be recognized as prototypes
+   (URL schemes).  Note that recognized doesn't mean supported -- only
+   HTTP, HTTPS and FTP are currently supported .
 
    However, a string that does not match anything in the list will be
    considered a relative URL.  Thus it's important that this list has
@@ -137,8 +131,7 @@ static struct proto sup_protos[] =
 #ifdef HAVE_SSL
   { "https://",URLHTTPS, DEFAULT_HTTPS_PORT},
 #endif
-  { "ftp://", URLFTP, DEFAULT_FTP_PORT },
-  /*{ "file://", URLFILE, DEFAULT_FTP_PORT },*/
+  { "ftp://", URLFTP, DEFAULT_FTP_PORT }
 };
 
 static void parse_dir PARAMS ((const char *, char **, char **));
@@ -148,27 +141,6 @@ static char *construct_relative PARAMS ((const char *, const char *));
 static char process_ftp_type PARAMS ((char *));
 
 \f
-/* Returns the number of characters to be skipped if the first thing
-   in a URL is URL: (which is 0 or 4+).  The optional spaces after
-   URL: are also skipped.  */
-int
-skip_url (const char *url)
-{
-  int i;
-
-  if (TOUPPER (url[0]) == 'U'
-      && TOUPPER (url[1]) == 'R'
-      && TOUPPER (url[2]) == 'L'
-      && url[3] == ':')
-    {
-      /* Skip blanks.  */
-      for (i = 4; url[i] && ISSPACE (url[i]); i++);
-      return i;
-    }
-  else
-    return 0;
-}
-
 /* Unsafe chars:
    - anything <= 32;
    - stuff from rfc1738 ("<>\"#%{}|\\^~[]`");
@@ -276,7 +248,6 @@ urlproto (const char *url)
 {
   int i;
 
-  url += skip_url (url);
   for (i = 0; i < ARRAY_SIZE (sup_protos); i++)
     if (!strncasecmp (url, sup_protos[i].name, strlen (sup_protos[i].name)))
       return sup_protos[i].ind;
@@ -323,7 +294,6 @@ has_proto (const char *url)
 {
   char **s;
 
-  url += skip_url (url);
   for (s = protostrings; *s; s++)
     if (strncasecmp (url, *s, strlen (*s)) == 0)
       return 1;
@@ -339,13 +309,13 @@ int
 skip_uname (const char *url)
 {
   const char *p;
-  for (p = url; *p && *p != '/'; p++)
-    if (*p == '@')
-      break;
+  const char *q = NULL;
+  for (p = url ; *p && *p != '/'; p++)
+    if (*p == '@') q = p;
   /* If a `@' was found before the first occurrence of `/', skip
      it.  */
-  if (*p == '@')
-    return p - url + 1;
+  if (q != NULL)
+    return q - url + 1;
   else
     return 0;
 }
@@ -415,7 +385,6 @@ parseurl (const char *url, struct urlinfo *u, int strict)
   uerr_t type;
 
   DEBUGP (("parseurl (\"%s\") -> ", url));
-  url += skip_url (url);
   recognizable = has_proto (url);
   if (strict && !recognizable)
     return URLUNKNOWN;
@@ -497,6 +466,7 @@ parseurl (const char *url, struct urlinfo *u, int strict)
       /* #### We don't handle type `d' correctly yet.  */
       if (!u->ftp_type || TOUPPER (u->ftp_type) == 'D')
        u->ftp_type = 'I';
+      DEBUGP (("ftp_type %c -> ", u->ftp_type));
     }
   DEBUGP (("opath %s -> ", u->path));
   /* Parse the username and password (if existing).  */
@@ -519,7 +489,7 @@ parseurl (const char *url, struct urlinfo *u, int strict)
   DEBUGP (("ndir %s\n", u->dir));
   /* Strip trailing `/'.  */
   l = strlen (u->dir);
-  if (l && u->dir[l - 1] == '/')
+  if (l > 1 && u->dir[l - 1] == '/')
     u->dir[l - 1] = '\0';
   /* Re-create the path: */
   abs_ftp = (u->proto == URLFTP && *u->dir == '/');
@@ -607,13 +577,13 @@ static uerr_t
 parse_uname (const char *url, char **user, char **passwd)
 {
   int l;
-  const char *p, *col;
+  const char *p, *q, *col;
   char **where;
 
   *user = NULL;
   *passwd = NULL;
-  url += skip_url (url);
-  /* Look for end of protocol string.  */
+
+  /* Look for the end of the protocol string.  */
   l = skip_proto (url);
   if (!l)
     return URLUNKNOWN;
@@ -627,7 +597,7 @@ parse_uname (const char *url, char **user, char **passwd)
   if (*p != '@')
     return URLOK;
   /* Else find the username and password.  */
-  for (p = col = url; *p != '@'; p++)
+  for (p = q = col = url; *p && *p != '/'; p++)
     {
       if (*p == ':' && !*user)
        {
@@ -636,12 +606,13 @@ parse_uname (const char *url, char **user, char **passwd)
          (*user)[p - url] = '\0';
          col = p + 1;
        }
+      if (*p == '@') q = p;
     }
   /* Decide whether you have only the username or both.  */
   where = *user ? passwd : user;
-  *where = (char *)xmalloc (p - col + 1);
-  memcpy (*where, col, p - col);
-  (*where)[p - col] = '\0';
+  *where = (char *)xmalloc (q - col + 1);
+  memcpy (*where, col, q - col);
+  (*where)[q - col] = '\0';
   return URLOK;
 }
 
@@ -661,10 +632,11 @@ process_ftp_type (char *path)
     return '\0';
 }
 \f
-/* Return the URL as fine-formed string, with a proper protocol,
-   optional port number, directory and optional user/password.  If
-   HIDE is non-zero, password will be hidden.  The forbidden
-   characters in the URL will be cleansed.  */
+/* Return the URL as fine-formed string, with a proper protocol, optional port
+   number, directory and optional user/password.  If `hide' is non-zero (as it
+   is when we're calling this on a URL we plan to print, but not when calling it
+   to canonicalize a URL for use within the program), password will be hidden.
+   The forbidden characters in the URL will be cleansed.  */
 char *
 str_url (const struct urlinfo *u, int hide)
 {
@@ -691,9 +663,9 @@ str_url (const struct urlinfo *u, int hide)
       if (hide)
        /* Don't output the password, or someone might see it over the user's
           shoulder (or in saved wget output).  Don't give away the number of
-          characters in the password, either, as we did when we replaced the
-          password characters with 'x's. */
-       passwd = "<password>";
+          characters in the password, either, as we did in past versions of
+          this code, when we replaced the password characters with 'x's. */
+       passwd = xstrdup("<password>");
       else
        passwd = CLEANDUP (u->passwd);
     }