1 ;;; url-cookie.el --- Netscape Cookie support
3 ;; Copyright (c) 1996 - 1999,2004 Free Software Foundation, Inc.
5 ;; Keywords: comm, data, processes, hypermedia
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (require 'cl
))
33 ;; See http://home.netscape.com/newsref/std/cookie_spec.html for the
34 ;; 'open standard' defining this crap.
36 ;; A cookie is stored internally as a vector of 7 slots
37 ;; [ 'cookie name value expires path domain secure ]
39 (defsubst url-cookie-name
(cookie) (aref cookie
1))
40 (defsubst url-cookie-value
(cookie) (aref cookie
2))
41 (defsubst url-cookie-expires
(cookie) (aref cookie
3))
42 (defsubst url-cookie-path
(cookie) (aref cookie
4))
43 (defsubst url-cookie-domain
(cookie) (aref cookie
5))
44 (defsubst url-cookie-secure
(cookie) (aref cookie
6))
46 (defsubst url-cookie-set-name
(cookie val
) (aset cookie
1 val
))
47 (defsubst url-cookie-set-value
(cookie val
) (aset cookie
2 val
))
48 (defsubst url-cookie-set-expires
(cookie val
) (aset cookie
3 val
))
49 (defsubst url-cookie-set-path
(cookie val
) (aset cookie
4 val
))
50 (defsubst url-cookie-set-domain
(cookie val
) (aset cookie
5 val
))
51 (defsubst url-cookie-set-secure
(cookie val
) (aset cookie
6 val
))
52 (defsubst url-cookie-retrieve-arg
(key args
) (nth 1 (memq key args
)))
54 (defsubst url-cookie-create
(&rest args
)
55 (let ((retval (make-vector 7 nil
)))
56 (aset retval
0 'cookie
)
57 (url-cookie-set-name retval
(url-cookie-retrieve-arg :name args
))
58 (url-cookie-set-value retval
(url-cookie-retrieve-arg :value args
))
59 (url-cookie-set-expires retval
(url-cookie-retrieve-arg :expires args
))
60 (url-cookie-set-path retval
(url-cookie-retrieve-arg :path args
))
61 (url-cookie-set-domain retval
(url-cookie-retrieve-arg :domain args
))
62 (url-cookie-set-secure retval
(url-cookie-retrieve-arg :secure args
))
65 (defun url-cookie-p (obj)
66 (and (vectorp obj
) (= (length obj
) 7) (eq (aref obj
0) 'cookie
)))
68 (defgroup url-cookie nil
74 (defvar url-cookie-storage nil
"Where cookies are stored.")
75 (defvar url-cookie-secure-storage nil
"Where secure cookies are stored.")
76 (defcustom url-cookie-file nil
"*Where cookies are stored on disk."
77 :type
'(choice (const :tag
"Default" :value nil
) file
)
81 (defcustom url-cookie-confirmation nil
82 "*If non-nil, confirmation by the user is required to accept HTTP cookies."
86 (defcustom url-cookie-multiple-line nil
87 "*If nil, HTTP requests put all cookies for the server on one line.
88 Some web servers, such as http://www.hotmail.com/, only accept cookies
89 when they are on one line. This is broken behaviour, but just try
90 telling Microsoft that."
94 (defvar url-cookies-changed-since-last-save nil
95 "Whether the cookies list has changed since the last save operation.")
98 (defun url-cookie-parse-file (&optional fname
)
99 (setq fname
(or fname url-cookie-file
))
102 (error (message "Could not load cookie file %s" fname
))))
104 (defun url-cookie-clean-up (&optional secure
)
106 (var (if secure
'url-cookie-secure-storage
'url-cookie-storage
))
107 (val (symbol-value var
))
120 (setq cur-cookie
(car cookies
)
121 cookies
(cdr cookies
))
122 (if (or (not (url-cookie-p cur-cookie
))
123 (url-cookie-expired-p cur-cookie
)
124 (null (url-cookie-expires cur-cookie
)))
126 (setq new-cookies
(cons cur-cookie new-cookies
))))
127 (if (not new-cookies
)
129 (setcdr cur new-cookies
)
130 (setq new
(cons cur new
))))
134 (defun url-cookie-write-file (&optional fname
)
135 (setq fname
(or fname url-cookie-file
))
137 ((not url-cookies-changed-since-last-save
) nil
)
138 ((not (file-writable-p fname
))
139 (message "Cookies file %s (see variable `url-cookie-file') is unwritable." fname
))
141 (url-cookie-clean-up)
142 (url-cookie-clean-up t
)
144 (set-buffer (get-buffer-create " *cookies*"))
147 (insert ";; Emacs-W3 HTTP cookies file\n"
148 ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
149 "(setq url-cookie-storage\n '")
150 (pp url-cookie-storage
(current-buffer))
151 (insert ")\n(setq url-cookie-secure-storage\n '")
152 (pp url-cookie-secure-storage
(current-buffer))
155 (kill-buffer (current-buffer))))))
157 (defun url-cookie-store (name value
&optional expires domain path secure
)
158 "Store a netscape-style cookie."
159 (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage
))
164 ;; First, look for a matching domain
165 (setq found-domain
(assoc domain storage
))
168 ;; Need to either stick the new cookie in existing domain storage
169 ;; or possibly replace an existing cookie if the names match.
171 (setq storage
(cdr found-domain
)
174 (setq cur
(car storage
)
175 storage
(cdr storage
))
176 (if (and (equal path
(url-cookie-path cur
))
177 (equal name
(url-cookie-name cur
)))
179 (url-cookie-set-expires cur expires
)
180 (url-cookie-set-value cur value
)
184 (setcdr found-domain
(cons
185 (url-cookie-create :name name
191 (cdr found-domain
)))))
192 ;; Need to add a new top-level domain
193 (setq tmp
(url-cookie-create :name name
201 (setcdr storage
(cons (list domain tmp
) (cdr storage
))))
203 (setq url-cookie-secure-storage
(list (list domain tmp
))))
205 (setq url-cookie-storage
(list (list domain tmp
))))))))
207 (defun url-cookie-expired-p (cookie)
209 (exp (url-cookie-expires cookie
))
210 (cur-date (and exp
(timezone-parse-date (current-time-string))))
211 (exp-date (and exp
(timezone-parse-date exp
)))
212 (cur-greg (and cur-date
(timezone-absolute-from-gregorian
213 (string-to-number (aref cur-date
1))
214 (string-to-number (aref cur-date
2))
215 (string-to-number (aref cur-date
0)))))
216 (exp-greg (and exp
(timezone-absolute-from-gregorian
217 (string-to-number (aref exp-date
1))
218 (string-to-number (aref exp-date
2))
219 (string-to-number (aref exp-date
0)))))
220 (diff-in-days (and exp
(- cur-greg exp-greg
)))
223 ((not exp
) nil
) ; No expiry == expires at browser quit
224 ((< diff-in-days
0) nil
) ; Expires sometime after today
225 ((> diff-in-days
0) t
) ; Expired before today
226 (t ; Expires sometime today, check times
227 (let* ((cur-time (timezone-parse-time (aref cur-date
3)))
228 (exp-time (timezone-parse-time (aref exp-date
3)))
229 (cur-norm (+ (* 360 (string-to-number (aref cur-time
2)))
230 (* 60 (string-to-number (aref cur-time
1)))
231 (* 1 (string-to-number (aref cur-time
0)))))
232 (exp-norm (+ (* 360 (string-to-number (aref exp-time
2)))
233 (* 60 (string-to-number (aref exp-time
1)))
234 (* 1 (string-to-number (aref exp-time
0))))))
235 (> (- cur-norm exp-norm
) 1))))))
238 (defun url-cookie-retrieve (host path
&optional secure
)
239 "Retrieve all the netscape-style cookies for a specified HOST and PATH."
240 (let ((storage (if secure
241 (append url-cookie-secure-storage url-cookie-storage
)
249 (setq cur
(car storage
)
250 storage
(cdr storage
)
253 (string-match (concat "^.*" (regexp-quote (car cur
)) "$") host
))
254 ;; The domains match - a possible hit!
256 (setq cur
(car cookies
)
257 cookies
(cdr cookies
)
258 path-regexp
(concat "^" (regexp-quote
259 (url-cookie-path cur
))))
260 (if (and (string-match path-regexp path
)
261 (not (url-cookie-expired-p cur
)))
262 (setq retval
(cons cur retval
))))))
266 (defun url-cookie-generate-header-lines (host path secure
)
267 (let* ((cookies (url-cookie-retrieve host path secure
))
271 ;; Have to sort this for sending most specific cookies first
272 (setq cookies
(and cookies
276 (> (length (url-cookie-path x
))
277 (length (url-cookie-path y
))))))))
279 (setq cur
(car cookies
)
280 cookies
(cdr cookies
)
281 chunk
(format "%s=%s" (url-cookie-name cur
) (url-cookie-value cur
))
282 retval
(if (and url-cookie-multiple-line
283 (< 80 (+ (length retval
) (length chunk
) 4)))
284 (concat retval
"\r\nCookie: " chunk
)
286 (concat retval
"; " chunk
)
287 (concat "Cookie: " chunk
)))))
289 (concat retval
"\r\n")
292 (defvar url-cookie-two-dot-domains
294 (mapconcat 'identity
(list "com" "edu" "net" "org" "gov" "mil" "int")
297 "A regexp of top level domains that only require two matching
298 '.'s in the domain name in order to set a cookie.")
300 (defcustom url-cookie-trusted-urls nil
301 "*A list of regular expressions matching URLs to always accept cookies from."
302 :type
'(repeat regexp
)
305 (defcustom url-cookie-untrusted-urls nil
306 "*A list of regular expressions matching URLs to never accept cookies from."
307 :type
'(repeat regexp
)
310 (defun url-cookie-host-can-set-p (host domain
)
316 (while (setq last
(string-match "\\." domain last
))
317 (setq numdots
(1+ numdots
)
319 (if (string-match url-cookie-two-dot-domains domain
)
322 ((string= host domain
) ; Apparently netscape lets you do this
324 ((>= numdots mindots
) ; We have enough dots in domain name
325 ;; Need to check and make sure the host is actually _in_ the
326 ;; domain it wants to set a cookie for though.
327 (string-match (concat (regexp-quote domain
) "$") host
))
332 (defun url-cookie-handle-set-cookie (str)
333 (setq url-cookies-changed-since-last-save t
)
334 (let* ((args (url-parse-args str t
))
336 (secure (and (assoc-string "secure" args t
) t
))
337 (domain (or (cdr-safe (assoc-string "domain" args t
))
338 (url-host url-current-object
)))
339 (current-url (url-view-url t
))
340 (trusted url-cookie-trusted-urls
)
341 (untrusted url-cookie-untrusted-urls
)
342 (expires (cdr-safe (assoc-string "expires" args t
)))
343 (path (or (cdr-safe (assoc-string "path" args t
))
345 (url-filename url-current-object
))))
348 (if (not (member (downcase (car (car args
)))
349 '("secure" "domain" "expires" "path")))
350 (setq rest
(cons (car args
) rest
)))
351 (setq args
(cdr args
)))
353 ;; Sometimes we get dates that the timezone package cannot handle very
354 ;; gracefully - take care of this here, instead of in url-cookie-expired-p
355 ;; to speed things up.
358 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
359 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
361 (setq expires
(concat (match-string 1 expires
) " "
362 (match-string 2 expires
) " "
363 (match-string 3 expires
) " "
364 (match-string 4 expires
) " ["
365 (match-string 5 expires
) "]")))
367 ;; This one is for older Emacs/XEmacs variants that don't
368 ;; understand this format without tenths of a second in it.
369 ;; Wednesday, 30-Dec-2037 16:00:00 GMT
371 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
374 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
376 (setq expires
(concat (match-string 1 expires
) "-" ; day
377 (match-string 2 expires
) "-" ; month
378 (match-string 3 expires
) " " ; year
379 (match-string 4 expires
) ".00 " ; hour:minutes:seconds
380 (match-string 6 expires
)))) ":" ; timezone
382 (while (consp trusted
)
383 (if (string-match (car trusted
) current-url
)
384 (setq trusted
(- (match-end 0) (match-beginning 0)))
386 (while (consp untrusted
)
387 (if (string-match (car untrusted
) current-url
)
388 (setq untrusted
(- (match-end 0) (match-beginning 0)))
390 (if (and trusted untrusted
)
391 ;; Choose the more specific match
392 (if (> trusted untrusted
)
397 ;; The site was explicity marked as untrusted by the user
399 ((or (eq url-privacy-level
'paranoid
)
400 (and (listp url-privacy-level
) (memq 'cookies url-privacy-level
)))
401 ;; user never wants cookies
403 ((and url-cookie-confirmation
405 (save-window-excursion
406 (with-output-to-temp-buffer "*Cookie Warning*"
410 (princ (format "%s - %s" (car x
) (cdr x
))))) rest
))
412 (not (funcall url-confirmation-func
413 (format "Allow %s to set these cookies? "
414 (url-host url-current-object
))))
415 (if (get-buffer "*Cookie Warning*")
416 (kill-buffer "*Cookie Warning*")))))
417 ;; user wants to be asked, and declined.
419 ((url-cookie-host-can-set-p (url-host url-current-object
) domain
)
420 ;; Cookie is accepted by the user, and passes our security checks
423 (setq cur
(pop rest
))
424 (url-cookie-store (car cur
) (cdr cur
)
425 expires domain path secure
))))
427 (message "%s tried to set a cookie for domain %s - rejected."
428 (url-host url-current-object
) domain
)))))
430 (defvar url-cookie-timer nil
)
432 (defcustom url-cookie-save-interval
3600
433 "*The number of seconds between automatic saves of cookies.
434 Default is 1 hour. Note that if you change this variable outside of
435 the `customize' interface after `url-do-setup' has been run, you need
436 to run the `url-cookie-setup-save-timer' function manually."
437 :set
(function (lambda (var val
)
438 (set-default var val
)
440 (fboundp 'url-cookie-setup-save-timer
)
441 (url-cookie-setup-save-timer))))
446 (defun url-cookie-setup-save-timer ()
447 "Reset the cookie saver timer."
450 (cond ((fboundp 'cancel-timer
) (cancel-timer url-cookie-timer
))
451 ((fboundp 'delete-itimer
) (delete-itimer url-cookie-timer
))))
452 (setq url-cookie-timer nil
)
453 (if url-cookie-save-interval
454 (setq url-cookie-timer
456 ((fboundp 'run-at-time
)
457 (run-at-time url-cookie-save-interval
458 url-cookie-save-interval
459 'url-cookie-write-file
))
460 ((fboundp 'start-itimer
)
461 (start-itimer "url-cookie-saver" 'url-cookie-write-file
462 url-cookie-save-interval
463 url-cookie-save-interval
))))))
465 (provide 'url-cookie
)
467 ;; arch-tag: 2568751b-6452-4398-aa2d-303edadb54d7
468 ;;; url-cookie.el ends here