aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlenn Morris2010-12-13 20:18:22 -0800
committerGlenn Morris2010-12-13 20:18:22 -0800
commit105a786f510c78ec0310202aa7dd6755a3d7d062 (patch)
tree90611e3da2b5c442267f9422379a063eebfb96f2
parent8a1cdce58fbb2a2a1893c8f299456b0cdc1ca331 (diff)
downloademacs-105a786f510c78ec0310202aa7dd6755a3d7d062.tar.gz
emacs-105a786f510c78ec0310202aa7dd6755a3d7d062.zip
Misc url-cookie tidy-up.
* lisp/url/url-cookie.el: Don't require cl when compiling. (url-cookie-clean-up, url-cookie-generate-header-lines): Use dolist. (url-cookie-parse-file, url-cookie-store, url-cookie-retrieve) (url-cookie-handle-set-cookie): Simplify.
-rw-r--r--lisp/url/ChangeLog13
-rw-r--r--lisp/url/url-cookie.el246
2 files changed, 104 insertions, 155 deletions
diff --git a/lisp/url/ChangeLog b/lisp/url/ChangeLog
index 5fd96e43b6b..619375b0249 100644
--- a/lisp/url/ChangeLog
+++ b/lisp/url/ChangeLog
@@ -1,3 +1,10 @@
12010-12-14 Glenn Morris <rgm@gnu.org>
2
3 * url-cookie.el: Don't require cl when compiling.
4 (url-cookie-clean-up, url-cookie-generate-header-lines): Use dolist.
5 (url-cookie-parse-file, url-cookie-store, url-cookie-retrieve)
6 (url-cookie-handle-set-cookie): Simplify.
7
12010-12-13 Chong Yidong <cyd@stupidchicken.com> 82010-12-13 Chong Yidong <cyd@stupidchicken.com>
2 9
3 * url-cookie.el (url-cookie-retrieve): Handle null LOCALPART. 10 * url-cookie.el (url-cookie-retrieve): Handle null LOCALPART.
@@ -2363,11 +2370,10 @@
2363 2370
2364;; Local variables: 2371;; Local variables:
2365;; coding: utf-8 2372;; coding: utf-8
2366;; add-log-time-zone-rule: t
2367;; End: 2373;; End:
2368 2374
2369 Copyright (C) 1999, 2001, 2002, 2004, 2005, 2375 Copyright (C) 1999, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009,
2370 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2376 2010 Free Software Foundation, Inc.
2371 2377
2372 This file is part of GNU Emacs. 2378 This file is part of GNU Emacs.
2373 2379
@@ -2384,4 +2390,3 @@
2384 You should have received a copy of the GNU General Public License 2390 You should have received a copy of the GNU General Public License
2385 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. 2391 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
2386 2392
2387;; arch-tag: ac117078-3091-4533-be93-098162ac2926
diff --git a/lisp/url/url-cookie.el b/lisp/url/url-cookie.el
index ec0974e9d76..78dccac19c2 100644
--- a/lisp/url/url-cookie.el
+++ b/lisp/url/url-cookie.el
@@ -1,4 +1,4 @@
1;;; url-cookie.el --- Netscape Cookie support 1;;; url-cookie.el --- URL cookie support
2 2
3;; Copyright (C) 1996, 1997, 1998, 1999, 2004, 2005, 2006, 2007, 2008, 3;; Copyright (C) 1996, 1997, 1998, 1999, 2004, 2005, 2006, 2007, 2008,
4;; 2009, 2010 Free Software Foundation, Inc. 4;; 2009, 2010 Free Software Foundation, Inc.
@@ -26,10 +26,6 @@
26 26
27(require 'url-util) 27(require 'url-util)
28(require 'url-parse) 28(require 'url-parse)
29(eval-when-compile (require 'cl))
30
31;; See http://home.netscape.com/newsref/std/cookie_spec.html for the
32;; 'open standard' defining this crap.
33 29
34(defgroup url-cookie nil 30(defgroup url-cookie nil
35 "URL cookies." 31 "URL cookies."
@@ -76,41 +72,23 @@ telling Microsoft that."
76 "Whether the cookies list has changed since the last save operation.") 72 "Whether the cookies list has changed since the last save operation.")
77 73
78(defun url-cookie-parse-file (&optional fname) 74(defun url-cookie-parse-file (&optional fname)
79 (setq fname (or fname url-cookie-file)) 75 "Load FNAME, default `url-cookie-file'."
80 (condition-case () 76 ;; It's completely normal for the cookies file not to exist yet.
81 (load fname nil t) 77 (load (or fname url-cookie-file) t t))
82 (error
83 ;; It's completely normal for the cookies file not to exist yet.
84 ;; (message "Could not load cookie file %s" fname)
85 )))
86 78
87(declare-function url-cookie-p "url-cookie" t t) ; defstruct 79(declare-function url-cookie-p "url-cookie" t t) ; defstruct
88 80
89(defun url-cookie-clean-up (&optional secure) 81(defun url-cookie-clean-up (&optional secure)
90 (let* ( 82 (let ((var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
91 (var (if secure 'url-cookie-secure-storage 'url-cookie-storage)) 83 new new-cookies)
92 (val (symbol-value var)) 84 (dolist (cur (symbol-value var))
93 (cur nil) 85 (setq new-cookies nil)
94 (new nil) 86 (dolist (cur-cookie (cdr cur))
95 (cookies nil) 87 (or (not (url-cookie-p cur-cookie))
96 (cur-cookie nil) 88 (url-cookie-expired-p cur-cookie)
97 (new-cookies nil) 89 (null (url-cookie-expires cur-cookie))
98 ) 90 (setq new-cookies (cons cur-cookie new-cookies))))
99 (while val 91 (when new-cookies
100 (setq cur (car val)
101 val (cdr val)
102 new-cookies nil
103 cookies (cdr cur))
104 (while cookies
105 (setq cur-cookie (car cookies)
106 cookies (cdr cookies))
107 (if (or (not (url-cookie-p cur-cookie))
108 (url-cookie-expired-p cur-cookie)
109 (null (url-cookie-expires cur-cookie)))
110 nil
111 (setq new-cookies (cons cur-cookie new-cookies))))
112 (if (not new-cookies)
113 nil
114 (setcdr cur new-cookies) 92 (setcdr cur new-cookies)
115 (setq new (cons cur new)))) 93 (setq new (cons cur new))))
116 (set var new))) 94 (set var new)))
@@ -143,54 +121,42 @@ telling Microsoft that."
143 (setq url-cookies-changed-since-last-save nil)))) 121 (setq url-cookies-changed-since-last-save nil))))
144 122
145(defun url-cookie-store (name value &optional expires domain localpart secure) 123(defun url-cookie-store (name value &optional expires domain localpart secure)
146 "Store a netscape-style cookie." 124 "Store a cookie."
147 (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage)) 125 (let ((storage (if secure url-cookie-secure-storage url-cookie-storage))
148 (tmp storage) 126 tmp found-domain)
149 (cur nil) 127 ;; First, look for a matching domain.
150 (found-domain nil)) 128 (if (setq found-domain (assoc domain storage))
151
152 ;; First, look for a matching domain
153 (setq found-domain (assoc domain storage))
154
155 (if found-domain
156 ;; Need to either stick the new cookie in existing domain storage 129 ;; Need to either stick the new cookie in existing domain storage
157 ;; or possibly replace an existing cookie if the names match. 130 ;; or possibly replace an existing cookie if the names match.
158 (progn 131 (unless (dolist (cur (setq storage (cdr found-domain)) tmp)
159 (setq storage (cdr found-domain) 132 (and (equal localpart (url-cookie-localpart cur))
160 tmp nil) 133 (equal name (url-cookie-name cur))
161 (while storage 134 (progn
162 (setq cur (car storage) 135 (setf (url-cookie-expires cur) expires)
163 storage (cdr storage)) 136 (setf (url-cookie-value cur) value)
164 (if (and (equal localpart (url-cookie-localpart cur)) 137 (setq tmp t))))
165 (equal name (url-cookie-name cur))) 138 ;; New cookie.
166 (progn 139 (setcdr found-domain (cons
167 (setf (url-cookie-expires cur) expires) 140 (url-cookie-create :name name
168 (setf (url-cookie-value cur) value) 141 :value value
169 (setq tmp t)))) 142 :expires expires
170 (if (not tmp) 143 :domain domain
171 ;; New cookie 144 :localpart localpart
172 (setcdr found-domain (cons 145 :secure secure)
173 (url-cookie-create :name name 146 (cdr found-domain))))
174 :value value 147 ;; Need to add a new top-level domain.
175 :expires expires
176 :domain domain
177 :localpart localpart
178 :secure secure)
179 (cdr found-domain)))))
180 ;; Need to add a new top-level domain
181 (setq tmp (url-cookie-create :name name 148 (setq tmp (url-cookie-create :name name
182 :value value 149 :value value
183 :expires expires 150 :expires expires
184 :domain domain 151 :domain domain
185 :localpart localpart 152 :localpart localpart
186 :secure secure)) 153 :secure secure))
187 (cond 154 (cond (storage
188 (storage 155 (setcdr storage (cons (list domain tmp) (cdr storage))))
189 (setcdr storage (cons (list domain tmp) (cdr storage)))) 156 (secure
190 (secure 157 (setq url-cookie-secure-storage (list (list domain tmp))))
191 (setq url-cookie-secure-storage (list (list domain tmp)))) 158 (t
192 (t 159 (setq url-cookie-storage (list (list domain tmp))))))))
193 (setq url-cookie-storage (list (list domain tmp))))))))
194 160
195(defun url-cookie-expired-p (cookie) 161(defun url-cookie-expired-p (cookie)
196 "Return non-nil if COOKIE is expired." 162 "Return non-nil if COOKIE is expired."
@@ -203,14 +169,9 @@ telling Microsoft that."
203 (append url-cookie-secure-storage url-cookie-storage) 169 (append url-cookie-secure-storage url-cookie-storage)
204 url-cookie-storage)) 170 url-cookie-storage))
205 (case-fold-search t) 171 (case-fold-search t)
206 (cookies nil) 172 cookies retval localpart-match)
207 (cur nil) 173 (dolist (cur storage)
208 (retval nil) 174 (setq cookies (cdr cur))
209 (localpart-match nil))
210 (while storage
211 (setq cur (car storage)
212 storage (cdr storage)
213 cookies (cdr cur))
214 (if (and (car cur) 175 (if (and (car cur)
215 (string-match 176 (string-match
216 (concat "^.*" 177 (concat "^.*"
@@ -222,36 +183,28 @@ telling Microsoft that."
222 (car cur))) 183 (car cur)))
223 "$") host)) 184 "$") host))
224 ;; The domains match - a possible hit! 185 ;; The domains match - a possible hit!
225 (while cookies 186 (dolist (cur cookies)
226 (setq cur (car cookies) 187 (and (if (and (stringp
227 cookies (cdr cookies) 188 (setq localpart-match (url-cookie-localpart cur)))
228 localpart-match (url-cookie-localpart cur)) 189 (stringp localpart))
229 (if (and (if (and (stringp localpart-match) 190 (string-match (concat "^" (regexp-quote localpart-match))
230 (stringp localpart)) 191 localpart)
231 (string-match (concat "^" (regexp-quote 192 (equal localpart localpart-match))
232 localpart-match)) 193 (not (url-cookie-expired-p cur))
233 localpart) 194 (setq retval (cons cur retval))))))
234 (equal localpart localpart-match))
235 (not (url-cookie-expired-p cur)))
236 (setq retval (cons cur retval))))))
237 retval)) 195 retval))
238 196
239(defun url-cookie-generate-header-lines (host localpart secure) 197(defun url-cookie-generate-header-lines (host localpart secure)
240 (let* ((cookies (url-cookie-retrieve host localpart secure)) 198 (let ((cookies (url-cookie-retrieve host localpart secure))
241 (retval nil) 199 retval chunk)
242 (cur nil) 200 ;; Have to sort this for sending most specific cookies first.
243 (chunk nil))
244 ;; Have to sort this for sending most specific cookies first
245 (setq cookies (and cookies 201 (setq cookies (and cookies
246 (sort cookies 202 (sort cookies
247 (function 203 (lambda (x y)
248 (lambda (x y) 204 (> (length (url-cookie-localpart x))
249 (> (length (url-cookie-localpart x)) 205 (length (url-cookie-localpart y)))))))
250 (length (url-cookie-localpart y)))))))) 206 (dolist (cur cookies)
251 (while cookies 207 (setq chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
252 (setq cur (car cookies)
253 cookies (cdr cookies)
254 chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
255 retval (if (and url-cookie-multiple-line 208 retval (if (and url-cookie-multiple-line
256 (< 80 (+ (length retval) (length chunk) 4))) 209 (< 80 (+ (length retval) (length chunk) 4)))
257 (concat retval "\r\nCookie: " chunk) 210 (concat retval "\r\nCookie: " chunk)
@@ -321,40 +274,38 @@ telling Microsoft that."
321 (file-name-directory 274 (file-name-directory
322 (url-filename url-current-object)))) 275 (url-filename url-current-object))))
323 (rest nil)) 276 (rest nil))
324 (while args 277 (dolist (this args)
325 (if (not (member (downcase (car (car args))) 278 (or (member (downcase (car this)) '("secure" "domain" "expires" "path"))
326 '("secure" "domain" "expires" "path"))) 279 (setq rest (cons this rest))))
327 (setq rest (cons (car args) rest)))
328 (setq args (cdr args)))
329 280
330 ;; Sometimes we get dates that the timezone package cannot handle very 281 ;; Sometimes we get dates that the timezone package cannot handle very
331 ;; gracefully - take care of this here, instead of in url-cookie-expired-p 282 ;; gracefully - take care of this here, instead of in url-cookie-expired-p
332 ;; to speed things up. 283 ;; to speed things up.
333 (if (and expires 284 (and expires
334 (string-match 285 (string-match
335 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +" 286 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
336 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$") 287 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
337 expires)) 288 expires)
338 (setq expires (concat (match-string 1 expires) " " 289 (setq expires (concat (match-string 1 expires) " "
339 (match-string 2 expires) " " 290 (match-string 2 expires) " "
340 (match-string 3 expires) " " 291 (match-string 3 expires) " "
341 (match-string 4 expires) " [" 292 (match-string 4 expires) " ["
342 (match-string 5 expires) "]"))) 293 (match-string 5 expires) "]")))
343 294
344 ;; This one is for older Emacs/XEmacs variants that don't 295 ;; This one is for older Emacs/XEmacs variants that don't
345 ;; understand this format without tenths of a second in it. 296 ;; understand this format without tenths of a second in it.
346 ;; Wednesday, 30-Dec-2037 16:00:00 GMT 297 ;; Wednesday, 30-Dec-2037 16:00:00 GMT
347 ;; - vs - 298 ;; - vs -
348 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT 299 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
349 (if (and expires 300 (and expires
350 (string-match 301 (string-match
351 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)" 302 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
352 expires)) 303 expires)
353 (setq expires (concat (match-string 1 expires) "-" ; day 304 (setq expires (concat (match-string 1 expires) "-" ; day
354 (match-string 2 expires) "-" ; month 305 (match-string 2 expires) "-" ; month
355 (match-string 3 expires) " " ; year 306 (match-string 3 expires) " " ; year
356 (match-string 4 expires) ".00 " ; hour:minutes:seconds 307 (match-string 4 expires) ".00 " ; hour:minutes:seconds
357 (match-string 6 expires)))) ":" ; timezone 308 (match-string 6 expires)))) ":" ; timezone
358 309
359 (while (consp trusted) 310 (while (consp trusted)
360 (if (string-match (car trusted) current-url) 311 (if (string-match (car trusted) current-url)
@@ -364,42 +315,36 @@ telling Microsoft that."
364 (if (string-match (car untrusted) current-url) 315 (if (string-match (car untrusted) current-url)
365 (setq untrusted (- (match-end 0) (match-beginning 0))) 316 (setq untrusted (- (match-end 0) (match-beginning 0)))
366 (pop untrusted))) 317 (pop untrusted)))
367 (if (and trusted untrusted) 318 (and trusted untrusted
368 ;; Choose the more specific match 319 ;; Choose the more specific match.
369 (if (> trusted untrusted) 320 (set (if (> trusted untrusted) 'untrusted 'trusted) nil))
370 (setq untrusted nil)
371 (setq trusted nil)))
372 (cond 321 (cond
373 (untrusted 322 (untrusted
374 ;; The site was explicity marked as untrusted by the user 323 ;; The site was explicity marked as untrusted by the user.
375 nil) 324 nil)
376 ((or (eq url-privacy-level 'paranoid) 325 ((or (eq url-privacy-level 'paranoid)
377 (and (listp url-privacy-level) (memq 'cookies url-privacy-level))) 326 (and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
378 ;; user never wants cookies 327 ;; User never wants cookies.
379 nil) 328 nil)
380 ((and url-cookie-confirmation 329 ((and url-cookie-confirmation
381 (not trusted) 330 (not trusted)
382 (save-window-excursion 331 (save-window-excursion
383 (with-output-to-temp-buffer "*Cookie Warning*" 332 (with-output-to-temp-buffer "*Cookie Warning*"
384 (mapcar 333 (mapcar
385 (function 334 (lambda (x)
386 (lambda (x) 335 (princ (format "%s - %s" (car x) (cdr x)))) rest))
387 (princ (format "%s - %s" (car x) (cdr x))))) rest))
388 (prog1 336 (prog1
389 (not (funcall url-confirmation-func 337 (not (funcall url-confirmation-func
390 (format "Allow %s to set these cookies? " 338 (format "Allow %s to set these cookies? "
391 (url-host url-current-object)))) 339 (url-host url-current-object))))
392 (if (get-buffer "*Cookie Warning*") 340 (if (get-buffer "*Cookie Warning*")
393 (kill-buffer "*Cookie Warning*"))))) 341 (kill-buffer "*Cookie Warning*")))))
394 ;; user wants to be asked, and declined. 342 ;; User wants to be asked, and declined.
395 nil) 343 nil)
396 ((url-cookie-host-can-set-p (url-host url-current-object) domain) 344 ((url-cookie-host-can-set-p (url-host url-current-object) domain)
397 ;; Cookie is accepted by the user, and passes our security checks 345 ;; Cookie is accepted by the user, and passes our security checks.
398 (let ((cur nil)) 346 (dolist (cur rest)
399 (while rest 347 (url-cookie-store (car cur) (cdr cur) expires domain localpart secure)))
400 (setq cur (pop rest))
401 (url-cookie-store (car cur) (cdr cur)
402 expires domain localpart secure))))
403 (t 348 (t
404 (url-lazy-message "%s tried to set a cookie for domain %s - rejected." 349 (url-lazy-message "%s tried to set a cookie for domain %s - rejected."
405 (url-host url-current-object) domain))))) 350 (url-host url-current-object) domain)))))
@@ -430,5 +375,4 @@ to run the `url-cookie-setup-save-timer' function manually."
430 375
431(provide 'url-cookie) 376(provide 'url-cookie)
432 377
433;; arch-tag: 2568751b-6452-4398-aa2d-303edadb54d7
434;;; url-cookie.el ends here 378;;; url-cookie.el ends here