]> sjero.net Git - wget/blobdiff - src/cookies.c
[svn] Use bitfields for booleans in struct cookie to save space.
[wget] / src / cookies.c
index d0d9e6101e3c4930b210aee19aaa8a53acad0753..2addd18548c7821e8d9c5139cf0e16527f26f822 100644 (file)
@@ -27,32 +27,29 @@ modify this file, you may extend this exception to your version of the
 file, but you are not obligated to do so.  If you do not wish to do
 so, delete this exception statement from your version.  */
 
-/* Written by Hrvoje Niksic.  Parts are loosely inspired by cookie
-   code submitted by Tomasz Wegrzanowski.
+/* Written by Hrvoje Niksic.  Parts are loosely inspired by the
+   cookie patch submitted by Tomasz Wegrzanowski.
 
-   Ideas for future work:
+   This implements the client-side cookie support, as specified
+   (loosely) by Netscape's "preliminary specification", currently
+   available at:
 
-   * Implement limits on cookie-related sizes, such as max. cookie
-     size, max. number of cookies, etc.
+       http://wp.netscape.com/newsref/std/cookie_spec.html
 
-   * Add more "cookie jar" methods, such as methods to iterate over
-     stored cookies, to clear temporary cookies, to perform
-     intelligent auto-saving, etc.
-
-   * Support `Set-Cookie2' and `Cookie2' headers?  Does anyone really
-     use them?  */
+   rfc2109 is not supported because of its incompatibilities with the
+   above widely-used specification.  rfc2965 is entirely ignored,
+   since popular client software doesn't implement it, and even the
+   sites that do send Set-Cookie2 also emit Set-Cookie for
+   compatibility.  */
 
 #include <config.h>
 
 #include <stdio.h>
-#ifdef HAVE_STRING_H
-# include <string.h>
-#else
-# include <strings.h>
-#endif
+#include <string.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <errno.h>
+#include <time.h>
 
 #include "wget.h"
 #include "utils.h"
@@ -60,7 +57,7 @@ so, delete this exception statement from your version.  */
 #include "cookies.h"
 
 /* This should *really* be in a .h file!  */
-time_t http_atotm PARAMS ((const char *));
+time_t http_atotm (const char *);
 \f
 /* Declarations of `struct cookie' and the most basic functions. */
 
@@ -101,21 +98,21 @@ struct cookie {
   int port;                    /* port number */
   char *path;                  /* path prefix of the cookie */
 
-  int secure;                  /* whether cookie should be
+  unsigned discard_requested :1; /* whether cookie was created to
+                                  request discarding another
+                                  cookie. */
+
+  unsigned secure :1;          /* whether cookie should be
                                   transmitted over non-https
                                   connections. */
-  int domain_exact;            /* whether DOMAIN must match as a
+  unsigned domain_exact :1;    /* whether DOMAIN must match as a
                                   whole. */
 
-  int permanent;               /* whether the cookie should outlive
+  int permanent :1;            /* whether the cookie should outlive
                                   the session. */
   time_t expiry_time;          /* time when the cookie expires, 0
                                   means undetermined. */
 
-  int discard_requested;       /* whether cookie was created to
-                                  request discarding another
-                                  cookie. */
-
   char *attr;                  /* cookie attribute name */
   char *value;                 /* cookie attribute value */
 
@@ -261,10 +258,9 @@ store_cookie (struct cookie_jar *jar, struct cookie *cookie)
   hash_table_put (jar->chains, chain_key, cookie);
   ++jar->cookie_count;
 
-#ifdef ENABLE_DEBUG
-  if (opt.debug)
+  IF_DEBUG
     {
-      time_t exptime = (time_t) cookie->expiry_time;
+      time_t exptime = cookie->expiry_time;
       DEBUGP (("\nStored cookie %s %d%s %s <%s> <%s> [expiry %s] %s %s\n",
               cookie->domain, cookie->port,
               cookie->port == PORT_ANY ? " (ANY)" : "",
@@ -274,7 +270,6 @@ store_cookie (struct cookie_jar *jar, struct cookie *cookie)
               cookie->expiry_time ? datetime_str (&exptime) : "none",
               cookie->attr, cookie->value));
     }
-#endif
 }
 
 /* Discard a cookie matching COOKIE's domain, port, path, and
@@ -394,10 +389,10 @@ update_cookie_field (struct cookie *cookie,
       BOUNDED_TO_ALLOCA (value_b, value_e, value_copy);
 
       expires = http_atotm (value_copy);
-      if (expires != -1)
+      if (expires != (time_t) -1)
        {
          cookie->permanent = 1;
-         cookie->expiry_time = (time_t)expires;
+         cookie->expiry_time = expires;
        }
       else
        /* Error in expiration spec.  Assume default (cookie doesn't
@@ -818,7 +813,7 @@ check_domain_match (const char *cookie_domain, const char *host)
   return 1;
 }
 
-static int path_matches PARAMS ((const char *, const char *));
+static int path_matches (const char *, const char *);
 
 /* Check whether PATH begins with COOKIE_PATH. */
 
@@ -862,7 +857,7 @@ cookie_handle_set_cookie (struct cookie_jar *jar,
       if (!check_domain_match (cookie->domain, host))
        {
          logprintf (LOG_NOTQUIET,
-                    "Cookie coming from %s attempted to set domain to %s\n",
+                    _("Cookie coming from %s attempted to set domain to %s\n"),
                     escnonprint (host), escnonprint (cookie->domain));
          xfree (cookie->domain);
          goto copy_domain;
@@ -870,9 +865,19 @@ cookie_handle_set_cookie (struct cookie_jar *jar,
     }
 
   if (!cookie->path)
-    cookie->path = xstrdup (path);
+    {
+      /* The cookie doesn't set path: set it to the URL path, sans the
+        file part ("/dir/file" truncated to "/dir/").  */
+      char *trailing_slash = strrchr (path, '/');
+      if (trailing_slash)
+       cookie->path = strdupdelim (path, trailing_slash + 1);
+      else
+       /* no slash in the string -- can this even happen? */
+       cookie->path = xstrdup (path);
+    }
   else
     {
+      /* The cookie sets its own path; verify that it is legal. */
       if (!check_path_match (cookie->path, path))
        {
          DEBUGP (("Attempt to fake the path: %s, %s\n",
@@ -881,6 +886,9 @@ cookie_handle_set_cookie (struct cookie_jar *jar,
        }
     }
 
+  /* Now store the cookie, or discard an existing cookie, if
+     discarding was requested.  */
+
   if (cookie->discard_requested)
     {
       discard_matching_cookie (jar, cookie);
@@ -1304,7 +1312,7 @@ cookie_jar_load (struct cookie_jar *jar, const char *file)
   FILE *fp = fopen (file, "r");
   if (!fp)
     {
-      logprintf (LOG_NOTQUIET, "Cannot open cookies file `%s': %s\n",
+      logprintf (LOG_NOTQUIET, _("Cannot open cookies file `%s': %s\n"),
                 file, strerror (errno));
       return;
     }
@@ -1391,7 +1399,7 @@ cookie_jar_load (struct cookie_jar *jar, const char *file)
       else
        {
          if (expiry < cookies_now)
-           goto abort;         /* ignore stale cookie. */
+           goto abort_cookie;  /* ignore stale cookie. */
          cookie->expiry_time = expiry;
          cookie->permanent = 1;
        }
@@ -1401,7 +1409,7 @@ cookie_jar_load (struct cookie_jar *jar, const char *file)
     next:
       continue;
 
-    abort:
+    abort_cookie:
       delete_cookie (cookie);
     }
   fclose (fp);