Keh-Chen Lau <[EMAIL PROTECTED]> writes:

> I have installed the lastest version of wget (1.7) but found that
> a bug which is not existing in 1.5.3
> 
>    If I download a file VIA this URL
> 
>    http://www.foo.bar.com/server/download/abc.zip?fn=/public/abc.zip
> 
>    with the '-nd' option, it still create the following folders
> 
>    'abc.zip?fn=' and 'public' and then store abc.zip inside it.

Thanks for the report -- good to have it before the 1.7.1 release.
Does this patch fix the problem for you?

2001-06-18  Hrvoje Niksic  <[EMAIL PROTECTED]>

        * url.c (url_filename): Make sure that slashes that sneak in to
        u->file via query string get protected.
        (file_name_protect_query_string): New function.

Index: src/url.c
===================================================================
RCS file: /pack/anoncvs/wget/src/url.c,v
retrieving revision 1.45
diff -u -r1.45 url.c
--- src/url.c   2001/05/27 19:35:10     1.45
+++ src/url.c   2001/06/18 08:11:51
@@ -1030,6 +1030,38 @@
   return res;
 }
 
+/* Return a malloced copy of S, but protect any '/' characters. */
+
+static char *
+file_name_protect_query_string (const char *s)
+{
+  const char *from;
+  char *to, *dest;
+  int destlen = 0;
+  for (from = s; *from; from++)
+    {
+      ++destlen;
+      if (*from == '/')
+       destlen += 2;           /* each / gets replaced with %2F, so
+                                  it adds two more chars.  */
+    }
+  dest = (char *)xmalloc (destlen + 1);
+  for (from = s, to = dest; *from; from++)
+    {
+      if (*from != '/')
+       *to++ = *from;
+      else
+       {
+         *to++ = '%';
+         *to++ = '2';
+         *to++ = 'F';
+       }
+    }
+  assert (to - dest == destlen);
+  *to = '\0';
+  return dest;
+}
+
 /* Create a unique filename, corresponding to a given URL.  Calls
    mkstruct if necessary.  Does *not* actually create any directories.  */
 char *
@@ -1048,7 +1080,20 @@
       if (!*u->file)
        file = xstrdup ("index.html");
       else
-       file = xstrdup (u->file);
+       {
+         /* If the URL came with a query string, u->file will contain
+            a question mark followed by query string contents.  These
+            contents can contain '/' which would make us create
+            unwanted directories.  These slashes must be protected
+            explicitly.  */
+         if (!strchr (u->file, '/'))
+           file = xstrdup (u->file);
+         else
+           {
+             /*assert (strchr (u->file, '?') != NULL);*/
+             file = file_name_protect_query_string (u->file);
+           }
+       }
     }
 
   if (!have_prefix)

Reply via email to