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.")
92 (defvar url-cookies-changed-since-last-save nil
93 "Whether the cookies list has changed since the last save operation.")
96 (defun url-cookie-parse-file (&optional fname
)
97 (setq fname
(or fname url-cookie-file
))
100 (error (message "Could not load cookie file %s" fname
))))
102 (defun url-cookie-clean-up (&optional secure
)
104 (var (if secure
'url-cookie-secure-storage
'url-cookie-storage
))
105 (val (symbol-value var
))
118 (setq cur-cookie
(car cookies
)
119 cookies
(cdr cookies
))
120 (if (or (not (url-cookie-p cur-cookie
))
121 (url-cookie-expired-p cur-cookie
)
122 (null (url-cookie-expires cur-cookie
)))
124 (setq new-cookies
(cons cur-cookie new-cookies
))))
125 (if (not new-cookies
)
127 (setcdr cur new-cookies
)
128 (setq new
(cons cur new
))))
132 (defun url-cookie-write-file (&optional fname
)
133 (setq fname
(or fname url-cookie-file
))
135 ((not url-cookies-changed-since-last-save
) nil
)
136 ((not (file-writable-p fname
))
137 (message "Cookies file %s (see variable `url-cookie-file') is unwritable." fname
))
139 (url-cookie-clean-up)
140 (url-cookie-clean-up t
)
142 (set-buffer (get-buffer-create " *cookies*"))
145 (insert ";; Emacs-W3 HTTP cookies file\n"
146 ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
147 "(setq url-cookie-storage\n '")
148 (pp url-cookie-storage
(current-buffer))
149 (insert ")\n(setq url-cookie-secure-storage\n '")
150 (pp url-cookie-secure-storage
(current-buffer))
153 (kill-buffer (current-buffer))))))
155 (defun url-cookie-store (name value
&optional expires domain path secure
)
156 "Store a netscape-style cookie."
157 (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage
))
162 ;; First, look for a matching domain
163 (setq found-domain
(assoc domain storage
))
166 ;; Need to either stick the new cookie in existing domain storage
167 ;; or possibly replace an existing cookie if the names match.
169 (setq storage
(cdr found-domain
)
172 (setq cur
(car storage
)
173 storage
(cdr storage
))
174 (if (and (equal path
(url-cookie-path cur
))
175 (equal name
(url-cookie-name cur
)))
177 (url-cookie-set-expires cur expires
)
178 (url-cookie-set-value cur value
)
182 (setcdr found-domain
(cons
183 (url-cookie-create :name name
189 (cdr found-domain
)))))
190 ;; Need to add a new top-level domain
191 (setq tmp
(url-cookie-create :name name
199 (setcdr storage
(cons (list domain tmp
) (cdr storage
))))
201 (setq url-cookie-secure-storage
(list (list domain tmp
))))
203 (setq url-cookie-storage
(list (list domain tmp
))))))))
205 (defun url-cookie-expired-p (cookie)
207 (exp (url-cookie-expires cookie
))
208 (cur-date (and exp
(timezone-parse-date (current-time-string))))
209 (exp-date (and exp
(timezone-parse-date exp
)))
210 (cur-greg (and cur-date
(timezone-absolute-from-gregorian
211 (string-to-int (aref cur-date
1))
212 (string-to-int (aref cur-date
2))
213 (string-to-int (aref cur-date
0)))))
214 (exp-greg (and exp
(timezone-absolute-from-gregorian
215 (string-to-int (aref exp-date
1))
216 (string-to-int (aref exp-date
2))
217 (string-to-int (aref exp-date
0)))))
218 (diff-in-days (and exp
(- cur-greg exp-greg
)))
221 ((not exp
) nil
) ; No expiry == expires at browser quit
222 ((< diff-in-days
0) nil
) ; Expires sometime after today
223 ((> diff-in-days
0) t
) ; Expired before today
224 (t ; Expires sometime today, check times
225 (let* ((cur-time (timezone-parse-time (aref cur-date
3)))
226 (exp-time (timezone-parse-time (aref exp-date
3)))
227 (cur-norm (+ (* 360 (string-to-int (aref cur-time
2)))
228 (* 60 (string-to-int (aref cur-time
1)))
229 (* 1 (string-to-int (aref cur-time
0)))))
230 (exp-norm (+ (* 360 (string-to-int (aref exp-time
2)))
231 (* 60 (string-to-int (aref exp-time
1)))
232 (* 1 (string-to-int (aref exp-time
0))))))
233 (> (- cur-norm exp-norm
) 1))))))
236 (defun url-cookie-retrieve (host path
&optional secure
)
237 "Retrieve all the netscape-style cookies for a specified HOST and PATH."
238 (let ((storage (if secure
239 (append url-cookie-secure-storage url-cookie-storage
)
247 (setq cur
(car storage
)
248 storage
(cdr storage
)
251 (string-match (concat "^.*" (regexp-quote (car cur
)) "$") host
))
252 ;; The domains match - a possible hit!
254 (setq cur
(car cookies
)
255 cookies
(cdr cookies
)
256 path-regexp
(concat "^" (regexp-quote
257 (url-cookie-path cur
))))
258 (if (and (string-match path-regexp path
)
259 (not (url-cookie-expired-p cur
)))
260 (setq retval
(cons cur retval
))))))
264 (defun url-cookie-generate-header-lines (host path secure
)
265 (let* ((cookies (url-cookie-retrieve host path secure
))
269 ;; Have to sort this for sending most specific cookies first
270 (setq cookies
(and cookies
274 (> (length (url-cookie-path x
))
275 (length (url-cookie-path y
))))))))
277 (setq cur
(car cookies
)
278 cookies
(cdr cookies
)
279 chunk
(format "%s=%s" (url-cookie-name cur
) (url-cookie-value cur
))
280 retval
(if (and url-cookie-multiple-line
281 (< 80 (+ (length retval
) (length chunk
) 4)))
282 (concat retval
"\r\nCookie: " chunk
)
284 (concat retval
"; " chunk
)
285 (concat "Cookie: " chunk
)))))
287 (concat retval
"\r\n")
290 (defvar url-cookie-two-dot-domains
292 (mapconcat 'identity
(list "com" "edu" "net" "org" "gov" "mil" "int")
295 "A regexp of top level domains that only require two matching
296 '.'s in the domain name in order to set a cookie.")
298 (defcustom url-cookie-trusted-urls nil
299 "*A list of regular expressions matching URLs to always accept cookies from."
300 :type
'(repeat regexp
)
303 (defcustom url-cookie-untrusted-urls nil
304 "*A list of regular expressions matching URLs to never accept cookies from."
305 :type
'(repeat regexp
)
308 (defun url-cookie-host-can-set-p (host domain
)
314 (while (setq last
(string-match "\\." domain last
))
315 (setq numdots
(1+ numdots
)
317 (if (string-match url-cookie-two-dot-domains domain
)
320 ((string= host domain
) ; Apparently netscape lets you do this
322 ((>= numdots mindots
) ; We have enough dots in domain name
323 ;; Need to check and make sure the host is actually _in_ the
324 ;; domain it wants to set a cookie for though.
325 (string-match (concat (regexp-quote domain
) "$") host
))
330 (defun url-cookie-handle-set-cookie (str)
331 (setq url-cookies-changed-since-last-save t
)
332 (let* ((args (url-parse-args str t
))
334 (secure (and (assoc-string "secure" args t
) t
))
335 (domain (or (cdr-safe (assoc-string "domain" args t
))
336 (url-host url-current-object
)))
337 (current-url (url-view-url t
))
338 (trusted url-cookie-trusted-urls
)
339 (untrusted url-cookie-untrusted-urls
)
340 (expires (cdr-safe (assoc-string "expires" args t
)))
341 (path (or (cdr-safe (assoc-string "path" args t
))
343 (url-filename url-current-object
))))
346 (if (not (member (downcase (car (car args
)))
347 '("secure" "domain" "expires" "path")))
348 (setq rest
(cons (car args
) rest
)))
349 (setq args
(cdr args
)))
351 ;; Sometimes we get dates that the timezone package cannot handle very
352 ;; gracefully - take care of this here, instead of in url-cookie-expired-p
353 ;; to speed things up.
356 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
357 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
359 (setq expires
(concat (match-string 1 expires
) " "
360 (match-string 2 expires
) " "
361 (match-string 3 expires
) " "
362 (match-string 4 expires
) " ["
363 (match-string 5 expires
) "]")))
365 ;; This one is for older Emacs/XEmacs variants that don't
366 ;; understand this format without tenths of a second in it.
367 ;; Wednesday, 30-Dec-2037 16:00:00 GMT
369 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
372 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
374 (setq expires
(concat (match-string 1 expires
) "-" ; day
375 (match-string 2 expires
) "-" ; month
376 (match-string 3 expires
) " " ; year
377 (match-string 4 expires
) ".00 " ; hour:minutes:seconds
378 (match-string 6 expires
)))) ":" ; timezone
380 (while (consp trusted
)
381 (if (string-match (car trusted
) current-url
)
382 (setq trusted
(- (match-end 0) (match-beginning 0)))
384 (while (consp untrusted
)
385 (if (string-match (car untrusted
) current-url
)
386 (setq untrusted
(- (match-end 0) (match-beginning 0)))
388 (if (and trusted untrusted
)
389 ;; Choose the more specific match
390 (if (> trusted untrusted
)
395 ;; The site was explicity marked as untrusted by the user
397 ((or (eq url-privacy-level
'paranoid
)
398 (and (listp url-privacy-level
) (memq 'cookies url-privacy-level
)))
399 ;; user never wants cookies
401 ((and url-cookie-confirmation
403 (save-window-excursion
404 (with-output-to-temp-buffer "*Cookie Warning*"
408 (princ (format "%s - %s" (car x
) (cdr x
))))) rest
))
410 (not (funcall url-confirmation-func
411 (format "Allow %s to set these cookies? "
412 (url-host url-current-object
))))
413 (if (get-buffer "*Cookie Warning*")
414 (kill-buffer "*Cookie Warning*")))))
415 ;; user wants to be asked, and declined.
417 ((url-cookie-host-can-set-p (url-host url-current-object
) domain
)
418 ;; Cookie is accepted by the user, and passes our security checks
421 (setq cur
(pop rest
))
422 (url-cookie-store (car cur
) (cdr cur
)
423 expires domain path secure
))))
425 (message "%s tried to set a cookie for domain %s - rejected."
426 (url-host url-current-object
) domain
)))))
428 (defvar url-cookie-timer nil
)
430 (defcustom url-cookie-save-interval
3600
431 "*The number of seconds between automatic saves of cookies.
432 Default is 1 hour. Note that if you change this variable outside of
433 the `customize' interface after `url-do-setup' has been run, you need
434 to run the `url-cookie-setup-save-timer' function manually."
435 :set
(function (lambda (var val
)
436 (set-default var val
)
438 (fboundp 'url-cookie-setup-save-timer
)
439 (url-cookie-setup-save-timer))))
444 (defun url-cookie-setup-save-timer ()
445 "Reset the cookie saver timer."
448 (cond ((fboundp 'cancel-timer
) (cancel-timer url-cookie-timer
))
449 ((fboundp 'delete-itimer
) (delete-itimer url-cookie-timer
))))
450 (setq url-cookie-timer nil
)
451 (if url-cookie-save-interval
452 (setq url-cookie-timer
454 ((fboundp 'run-at-time
)
455 (run-at-time url-cookie-save-interval
456 url-cookie-save-interval
457 'url-cookie-write-file
))
458 ((fboundp 'start-itimer
)
459 (start-itimer "url-cookie-saver" 'url-cookie-write-file
460 url-cookie-save-interval
461 url-cookie-save-interval
))))))
463 (provide 'url-cookie
)
465 ;; arch-tag: 2568751b-6452-4398-aa2d-303edadb54d7
466 ;;; url-cookie.el ends here