From f0eb1fb7583046e500ad5fa1ed5fb15ab1a3afa0 Mon Sep 17 00:00:00 2001
From: hniksic <devnull@localhost>
Date: Tue, 24 Apr 2001 19:29:54 -0700
Subject: [PATCH] [svn] Fix loading of cookies. Published in
 <sxslmopyao6.fsf@florida.arsdigita.de>.

---
 src/ChangeLog |  7 +++++++
 src/cookies.c | 42 +++++++++++++++++++-----------------------
 src/http.c    |  5 ++++-
 3 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 08970a69..a6bb7086 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2001-04-25  Hrvoje Niksic  <hniksic@arsdigita.com>
+
+	* http.c (http_loop): Would load cookies every time.
+
+	* cookies.c (load_cookies): Handle cookies whose values contain
+	embedded spaces.
+
 2001-04-25  Hrvoje Niksic  <hniksic@arsdigita.com>
 
 	* utils.c: Define each DIGITS_* in one line.
diff --git a/src/cookies.c b/src/cookies.c
index f915d820..4c8e3a7b 100644
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -1192,7 +1192,7 @@ domain_port (const char *domain_b, const char *domain_e,
     ++p;					\
 } while (0)
 
-#define MARK_WORD(p, b, e) do {			\
+#define SET_WORD_BOUNDARIES(p, b, e) do {			\
   SKIP_WS (p);					\
   b = p;					\
   /* skip non-ws */				\
@@ -1239,16 +1239,25 @@ load_cookies (const char *file)
 	/* empty line */
 	continue;
 
-      MARK_WORD (p, domain_b,  domain_e);
-      MARK_WORD (p, ignore_b,  ignore_e);
-      MARK_WORD (p, path_b,    path_e);
-      MARK_WORD (p, secure_b,  secure_e);
-      MARK_WORD (p, expires_b, expires_e);
-      MARK_WORD (p, name_b,    name_e);
+      SET_WORD_BOUNDARIES (p, domain_b,  domain_e);
+      SET_WORD_BOUNDARIES (p, ignore_b,  ignore_e);
+      SET_WORD_BOUNDARIES (p, path_b,    path_e);
+      SET_WORD_BOUNDARIES (p, secure_b,  secure_e);
+      SET_WORD_BOUNDARIES (p, expires_b, expires_e);
+      SET_WORD_BOUNDARIES (p, name_b,    name_e);
 
-      /* Don't use MARK_WORD for value because it may contain
-	 whitespace itself.  Instead, . */
-      MARK_WORD (p, value_b,   value_e);
+      /* Don't use SET_WORD_BOUNDARIES for value because it may
+	 contain whitespace.  Instead, set value_e to the end of line,
+	 modulo trailing space (this will skip the line separator.) */
+      SKIP_WS (p);
+      value_b = p;
+      value_e = p + strlen (p);
+      while (value_e > value_b && ISSPACE (*(value_e - 1)))
+	--value_e;
+      if (value_b == value_e)
+	/* Hmm, should we check for empty value?  I guess that's
+	   legal, so I leave it.  */
+	;
 
       cookie = cookie_new ();
 
@@ -1269,19 +1278,6 @@ load_cookies (const char *file)
 
       cookie->domain  = strdupdelim (domain_b, domain_e);
 
-      /* Don't use MARK_WORD for value because it may contain
-	 whitespace itself.  Instead, set name_e to the end of line,
-	 modulo trailing space (which includes the NL separator.) */
-      SKIP_WS (p);
-      name_b = p;
-      name_e = p + strlen (p);
-      while (name_e >= name_b && ISSPACE (*name_e))
-	--name_e;
-      if (name_b == name_e)
-	/* Hmm, should we check for empty value?  I guess that's
-	   legal, so I leave it.  */
-	;
-
       /* safe default in case EXPIRES field is garbled. */
       cookie->expiry_time = cookies_now - 1;
 
diff --git a/src/http.c b/src/http.c
index 42b2dbcb..64744288 100644
--- a/src/http.c
+++ b/src/http.c
@@ -1381,7 +1381,10 @@ http_loop (struct urlinfo *u, char **newloc, int *dt)
      here so that we don't go through the hoops if we're just using
      FTP or whatever. */
   if (opt.cookies && opt.cookies_input && !cookies_loaded_p)
-    load_cookies (opt.cookies_input);
+    {
+      load_cookies (opt.cookies_input);
+      cookies_loaded_p = 1;
+    }
 
   *newloc = NULL;