Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-196
[bpt/emacs.git] / lisp / url / url-cookie.el
CommitLineData
8c8b8430 1;;; url-cookie.el --- Netscape Cookie support
8c8b8430
SM
2;; Keywords: comm, data, processes, hypermedia
3
4;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5;;; Copyright (c) 1996 by William M. Perry <wmperry@cs.indiana.edu>
6;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
7;;;
8;;; This file is part of GNU Emacs.
9;;;
10;;; GNU Emacs is free software; you can redistribute it and/or modify
11;;; it under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 2, or (at your option)
13;;; any later version.
14;;;
15;;; GNU Emacs is distributed in the hope that it will be useful,
16;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Emacs; see the file COPYING. If not, write to the
22;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;;; Boston, MA 02111-1307, USA.
24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25
26(require 'timezone)
27(require 'url-util)
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;;
34;; A cookie is stored internally as a vector of 7 slots
35;; [ 'cookie name value expires path domain secure ]
36
37(defsubst url-cookie-name (cookie) (aref cookie 1))
38(defsubst url-cookie-value (cookie) (aref cookie 2))
39(defsubst url-cookie-expires (cookie) (aref cookie 3))
40(defsubst url-cookie-path (cookie) (aref cookie 4))
41(defsubst url-cookie-domain (cookie) (aref cookie 5))
42(defsubst url-cookie-secure (cookie) (aref cookie 6))
43
44(defsubst url-cookie-set-name (cookie val) (aset cookie 1 val))
45(defsubst url-cookie-set-value (cookie val) (aset cookie 2 val))
46(defsubst url-cookie-set-expires (cookie val) (aset cookie 3 val))
47(defsubst url-cookie-set-path (cookie val) (aset cookie 4 val))
48(defsubst url-cookie-set-domain (cookie val) (aset cookie 5 val))
49(defsubst url-cookie-set-secure (cookie val) (aset cookie 6 val))
50(defsubst url-cookie-retrieve-arg (key args) (nth 1 (memq key args)))
51
52(defsubst url-cookie-create (&rest args)
53 (let ((retval (make-vector 7 nil)))
54 (aset retval 0 'cookie)
55 (url-cookie-set-name retval (url-cookie-retrieve-arg :name args))
56 (url-cookie-set-value retval (url-cookie-retrieve-arg :value args))
57 (url-cookie-set-expires retval (url-cookie-retrieve-arg :expires args))
58 (url-cookie-set-path retval (url-cookie-retrieve-arg :path args))
59 (url-cookie-set-domain retval (url-cookie-retrieve-arg :domain args))
60 (url-cookie-set-secure retval (url-cookie-retrieve-arg :secure args))
61 retval))
62
63(defun url-cookie-p (obj)
64 (and (vectorp obj) (= (length obj) 7) (eq (aref obj 0) 'cookie)))
65
66(defgroup url-cookie nil
67 "URL cookies"
68 :prefix "url-"
69 :prefix "url-cookie-"
70 :group 'url)
71
72(defvar url-cookie-storage nil "Where cookies are stored.")
73(defvar url-cookie-secure-storage nil "Where secure cookies are stored.")
74(defcustom url-cookie-file nil "*Where cookies are stored on disk."
75 :type '(choice (const :tag "Default" :value nil) file)
76 :group 'url-file
77 :group 'url-cookie)
78
79(defcustom url-cookie-confirmation nil
80 "*If non-nil, confirmation by the user is required to accept HTTP cookies."
81 :type 'boolean
82 :group 'url-cookie)
83
84(defcustom url-cookie-multiple-line nil
85 "*If nil, HTTP requests put all cookies for the server on one line.
86Some web servers, such as http://www.hotmail.com/, only accept cookies
87when they are on one line. This is broken behaviour, but just try
88telling Microsoft that.")
89
90(defvar url-cookies-changed-since-last-save nil
91 "Whether the cookies list has changed since the last save operation.")
92
93;;;###autoload
94(defun url-cookie-parse-file (&optional fname)
95 (setq fname (or fname url-cookie-file))
96 (condition-case ()
97 (load fname nil t)
98 (error (message "Could not load cookie file %s" fname))))
99
100(defun url-cookie-clean-up (&optional secure)
101 (let* (
102 (var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
103 (val (symbol-value var))
104 (cur nil)
105 (new nil)
106 (cookies nil)
107 (cur-cookie nil)
108 (new-cookies nil)
109 )
110 (while val
111 (setq cur (car val)
112 val (cdr val)
113 new-cookies nil
114 cookies (cdr cur))
115 (while cookies
116 (setq cur-cookie (car cookies)
117 cookies (cdr cookies))
118 (if (or (not (url-cookie-p cur-cookie))
119 (url-cookie-expired-p cur-cookie)
120 (null (url-cookie-expires cur-cookie)))
121 nil
122 (setq new-cookies (cons cur-cookie new-cookies))))
123 (if (not new-cookies)
124 nil
125 (setcdr cur new-cookies)
126 (setq new (cons cur new))))
127 (set var new)))
128
129;;;###autoload
130(defun url-cookie-write-file (&optional fname)
131 (setq fname (or fname url-cookie-file))
132 (cond
133 ((not url-cookies-changed-since-last-save) nil)
134 ((not (file-writable-p fname))
135 (message "Cookies file %s (see variable `url-cookie-file') is unwritable." fname))
136 (t
137 (url-cookie-clean-up)
138 (url-cookie-clean-up t)
139 (save-excursion
140 (set-buffer (get-buffer-create " *cookies*"))
141 (erase-buffer)
142 (fundamental-mode)
143 (insert ";; Emacs-W3 HTTP cookies file\n"
144 ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
145 "(setq url-cookie-storage\n '")
146 (pp url-cookie-storage (current-buffer))
147 (insert ")\n(setq url-cookie-secure-storage\n '")
148 (pp url-cookie-secure-storage (current-buffer))
149 (insert ")\n")
150 (write-file fname)
151 (kill-buffer (current-buffer))))))
152
153(defun url-cookie-store (name value &optional expires domain path secure)
154 "Stores a netscape-style cookie"
155 (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage))
156 (tmp storage)
157 (cur nil)
158 (found-domain nil))
159
160 ;; First, look for a matching domain
161 (setq found-domain (assoc domain storage))
162
163 (if found-domain
164 ;; Need to either stick the new cookie in existing domain storage
165 ;; or possibly replace an existing cookie if the names match.
166 (progn
167 (setq storage (cdr found-domain)
168 tmp nil)
169 (while storage
170 (setq cur (car storage)
171 storage (cdr storage))
172 (if (and (equal path (url-cookie-path cur))
173 (equal name (url-cookie-name cur)))
174 (progn
175 (url-cookie-set-expires cur expires)
176 (url-cookie-set-value cur value)
177 (setq tmp t))))
178 (if (not tmp)
179 ;; New cookie
180 (setcdr found-domain (cons
181 (url-cookie-create :name name
182 :value value
183 :expires expires
184 :domain domain
185 :path path
186 :secure secure)
187 (cdr found-domain)))))
188 ;; Need to add a new top-level domain
189 (setq tmp (url-cookie-create :name name
190 :value value
191 :expires expires
192 :domain domain
193 :path path
194 :secure secure))
195 (cond
196 (storage
197 (setcdr storage (cons (list domain tmp) (cdr storage))))
198 (secure
199 (setq url-cookie-secure-storage (list (list domain tmp))))
200 (t
201 (setq url-cookie-storage (list (list domain tmp))))))))
202
203(defun url-cookie-expired-p (cookie)
204 (let* (
205 (exp (url-cookie-expires cookie))
206 (cur-date (and exp (timezone-parse-date (current-time-string))))
207 (exp-date (and exp (timezone-parse-date exp)))
208 (cur-greg (and cur-date (timezone-absolute-from-gregorian
209 (string-to-int (aref cur-date 1))
210 (string-to-int (aref cur-date 2))
211 (string-to-int (aref cur-date 0)))))
212 (exp-greg (and exp (timezone-absolute-from-gregorian
213 (string-to-int (aref exp-date 1))
214 (string-to-int (aref exp-date 2))
215 (string-to-int (aref exp-date 0)))))
216 (diff-in-days (and exp (- cur-greg exp-greg)))
217 )
218 (cond
219 ((not exp) nil) ; No expiry == expires at browser quit
220 ((< diff-in-days 0) nil) ; Expires sometime after today
221 ((> diff-in-days 0) t) ; Expired before today
222 (t ; Expires sometime today, check times
223 (let* ((cur-time (timezone-parse-time (aref cur-date 3)))
224 (exp-time (timezone-parse-time (aref exp-date 3)))
225 (cur-norm (+ (* 360 (string-to-int (aref cur-time 2)))
226 (* 60 (string-to-int (aref cur-time 1)))
227 (* 1 (string-to-int (aref cur-time 0)))))
228 (exp-norm (+ (* 360 (string-to-int (aref exp-time 2)))
229 (* 60 (string-to-int (aref exp-time 1)))
230 (* 1 (string-to-int (aref exp-time 0))))))
231 (> (- cur-norm exp-norm) 1))))))
232
233;;;###autoload
234(defun url-cookie-retrieve (host path &optional secure)
235 "Retrieves all the netscape-style cookies for a specified HOST and PATH"
236 (let ((storage (if secure
237 (append url-cookie-secure-storage url-cookie-storage)
238 url-cookie-storage))
239 (case-fold-search t)
240 (cookies nil)
241 (cur nil)
242 (retval nil)
243 (path-regexp nil))
244 (while storage
245 (setq cur (car storage)
246 storage (cdr storage)
247 cookies (cdr cur))
248 (if (and (car cur)
249 (string-match (concat "^.*" (regexp-quote (car cur)) "$") host))
250 ;; The domains match - a possible hit!
251 (while cookies
252 (setq cur (car cookies)
253 cookies (cdr cookies)
254 path-regexp (concat "^" (regexp-quote
255 (url-cookie-path cur))))
256 (if (and (string-match path-regexp path)
257 (not (url-cookie-expired-p cur)))
258 (setq retval (cons cur retval))))))
259 retval))
260
261;;;###autolaod
262(defun url-cookie-generate-header-lines (host path secure)
263 (let* ((cookies (url-cookie-retrieve host path secure))
264 (retval nil)
265 (cur nil)
266 (chunk nil))
267 ;; Have to sort this for sending most specific cookies first
268 (setq cookies (and cookies
269 (sort cookies
270 (function
271 (lambda (x y)
272 (> (length (url-cookie-path x))
273 (length (url-cookie-path y))))))))
274 (while cookies
275 (setq cur (car cookies)
276 cookies (cdr cookies)
277 chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
278 retval (if (and url-cookie-multiple-line
279 (< 80 (+ (length retval) (length chunk) 4)))
280 (concat retval "\r\nCookie: " chunk)
281 (if retval
282 (concat retval "; " chunk)
283 (concat "Cookie: " chunk)))))
284 (if retval
285 (concat retval "\r\n")
286 "")))
287
288(defvar url-cookie-two-dot-domains
289 (concat "\\.\\("
290 (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int")
291 "\\|")
292 "\\)$")
293 "A regular expression of top-level domains that only require two matching
294'.'s in the domain name in order to set a cookie.")
295
296(defcustom url-cookie-trusted-urls nil
297 "*A list of regular expressions matching URLs to always accept cookies from."
298 :type '(repeat regexp)
299 :group 'url-cookie)
300
301(defcustom url-cookie-untrusted-urls nil
302 "*A list of regular expressions matching URLs to never accept cookies from."
303 :type '(repeat regexp)
304 :group 'url-cookie)
305
306(defun url-cookie-host-can-set-p (host domain)
307 (let ((numdots 0)
308 (tmp domain)
309 (last nil)
310 (case-fold-search t)
311 (mindots 3))
312 (while (setq last (string-match "\\." domain last))
313 (setq numdots (1+ numdots)
314 last (1+ last)))
315 (if (string-match url-cookie-two-dot-domains domain)
316 (setq mindots 2))
317 (cond
318 ((string= host domain) ; Apparently netscape lets you do this
319 t)
320 ((>= numdots mindots) ; We have enough dots in domain name
321 ;; Need to check and make sure the host is actually _in_ the
322 ;; domain it wants to set a cookie for though.
323 (string-match (concat (regexp-quote domain) "$") host))
324 (t
325 nil))))
326
327;;;###autoload
328(defun url-cookie-handle-set-cookie (str)
329 (setq url-cookies-changed-since-last-save t)
330 (let* ((args (url-parse-args str t))
331 (case-fold-search t)
332 (secure (and (assoc-ignore-case "secure" args) t))
333 (domain (or (cdr-safe (assoc-ignore-case "domain" args))
334 (url-host url-current-object)))
335 (current-url (url-view-url t))
336 (trusted url-cookie-trusted-urls)
337 (untrusted url-cookie-untrusted-urls)
338 (expires (cdr-safe (assoc-ignore-case "expires" args)))
339 (path (or (cdr-safe (assoc-ignore-case "path" args))
340 (file-name-directory
341 (url-filename url-current-object))))
342 (rest nil))
343 (while args
344 (if (not (member (downcase (car (car args)))
345 '("secure" "domain" "expires" "path")))
346 (setq rest (cons (car args) rest)))
347 (setq args (cdr args)))
348
349 ;; Sometimes we get dates that the timezone package cannot handle very
350 ;; gracefully - take care of this here, instead of in url-cookie-expired-p
351 ;; to speed things up.
352 (if (and expires
353 (string-match
354 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
355 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
356 expires))
357 (setq expires (concat (match-string 1 expires) " "
358 (match-string 2 expires) " "
359 (match-string 3 expires) " "
360 (match-string 4 expires) " ["
361 (match-string 5 expires) "]")))
362
363 ;; This one is for older Emacs/XEmacs variants that don't
364 ;; understand this format without tenths of a second in it.
365 ;; Wednesday, 30-Dec-2037 16:00:00 GMT
366 ;; - vs -
367 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
368 (if (and expires
369 (string-match
370 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
371 expires))
372 (setq expires (concat (match-string 1 expires) "-" ; day
373 (match-string 2 expires) "-" ; month
374 (match-string 3 expires) " " ; year
375 (match-string 4 expires) ".00 " ; hour:minutes:seconds
376 (match-string 6 expires)))) ":" ; timezone
377
378 (while (consp trusted)
379 (if (string-match (car trusted) current-url)
380 (setq trusted (- (match-end 0) (match-beginning 0)))
381 (pop trusted)))
382 (while (consp untrusted)
383 (if (string-match (car untrusted) current-url)
384 (setq untrusted (- (match-end 0) (match-beginning 0)))
385 (pop untrusted)))
386 (if (and trusted untrusted)
387 ;; Choose the more specific match
388 (if (> trusted untrusted)
389 (setq untrusted nil)
390 (setq trusted nil)))
391 (cond
392 (untrusted
393 ;; The site was explicity marked as untrusted by the user
394 nil)
395 ((or (eq url-privacy-level 'paranoid)
396 (and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
397 ;; user never wants cookies
398 nil)
399 ((and url-cookie-confirmation
400 (not trusted)
401 (save-window-excursion
402 (with-output-to-temp-buffer "*Cookie Warning*"
403 (mapcar
404 (function
405 (lambda (x)
406 (princ (format "%s - %s" (car x) (cdr x))))) rest))
407 (prog1
408 (not (funcall url-confirmation-func
409 (format "Allow %s to set these cookies? "
410 (url-host url-current-object))))
411 (if (get-buffer "*Cookie Warning*")
412 (kill-buffer "*Cookie Warning*")))))
413 ;; user wants to be asked, and declined.
414 nil)
415 ((url-cookie-host-can-set-p (url-host url-current-object) domain)
416 ;; Cookie is accepted by the user, and passes our security checks
417 (let ((cur nil))
418 (while rest
419 (setq cur (pop rest))
420 (url-cookie-store (car cur) (cdr cur)
421 expires domain path secure))))
422 (t
423 (message "%s tried to set a cookie for domain %s - rejected."
424 (url-host url-current-object) domain)))))
425
426(defvar url-cookie-timer nil)
427
428(defcustom url-cookie-save-interval 3600
429 "*The number of seconds between automatic saves of cookies.
430Default is 1 hour. Note that if you change this variable outside of
431the `customize' interface after `url-do-setup' has been run, you need
432to run the `url-cookie-setup-save-timer' function manually."
433 :set (function (lambda (var val)
434 (set-default var val)
435 (and (featurep 'url)
436 (fboundp 'url-cookie-setup-save-timer)
437 (url-cookie-setup-save-timer))))
438 :type 'integer
439 :group 'url)
440
441;;;###autoload
442(defun url-cookie-setup-save-timer ()
443 "Reset the cookie saver timer."
444 (interactive)
445 (cond
446 ((featurep 'itimer)
447 (ignore-errors (delete-itimer url-cookie-timer))
448 (setq url-cookie-timer nil)
449 (if url-cookie-save-interval
450 (setq url-cookie-timer
451 (start-itimer "url-cookie-saver" 'url-cookie-write-file
452 url-cookie-save-interval
453 url-cookie-save-interval))))
454 ((fboundp 'run-at-time)
455 (ignore-errors (cancel-timer url-cookie-timer))
456 (setq url-cookie-timer nil)
457 (if url-cookie-save-interval
458 (setq url-cookie-timer
459 (run-at-time url-cookie-save-interval
460 url-cookie-save-interval
461 'url-cookie-write-file))))
462 (t nil)))
463
464(provide 'url-cookie)
465
e5566bd5 466;;; arch-tag: 2568751b-6452-4398-aa2d-303edadb54d7