*** empty log message ***
[emacs.git] / lisp / url / url-cookie.el
blob9f7db86759777a0e58281bc0ca4a31ba0a75baad
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.
8 ;;
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)
12 ;; any later version.
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.
24 ;;; Commentary:
26 ;;; Code:
28 (require 'timezone)
29 (require 'url-util)
30 (require 'url-parse)
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))
63 retval))
65 (defun url-cookie-p (obj)
66 (and (vectorp obj) (= (length obj) 7) (eq (aref obj 0) 'cookie)))
68 (defgroup url-cookie nil
69 "URL cookies"
70 :prefix "url-"
71 :prefix "url-cookie-"
72 :group 'url)
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)
78 :group 'url-file
79 :group 'url-cookie)
81 (defcustom url-cookie-confirmation nil
82 "*If non-nil, confirmation by the user is required to accept HTTP cookies."
83 :type 'boolean
84 :group 'url-cookie)
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.")
95 ;;;###autoload
96 (defun url-cookie-parse-file (&optional fname)
97 (setq fname (or fname url-cookie-file))
98 (condition-case ()
99 (load fname nil t)
100 (error (message "Could not load cookie file %s" fname))))
102 (defun url-cookie-clean-up (&optional secure)
103 (let* (
104 (var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
105 (val (symbol-value var))
106 (cur nil)
107 (new nil)
108 (cookies nil)
109 (cur-cookie nil)
110 (new-cookies nil)
112 (while val
113 (setq cur (car val)
114 val (cdr val)
115 new-cookies nil
116 cookies (cdr cur))
117 (while cookies
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))))
129 (set var new)))
131 ;;;###autoload
132 (defun url-cookie-write-file (&optional fname)
133 (setq fname (or fname url-cookie-file))
134 (cond
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)
141 (save-excursion
142 (set-buffer (get-buffer-create " *cookies*"))
143 (erase-buffer)
144 (fundamental-mode)
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))
151 (insert ")\n")
152 (write-file fname)
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))
158 (tmp storage)
159 (cur nil)
160 (found-domain nil))
162 ;; First, look for a matching domain
163 (setq found-domain (assoc domain storage))
165 (if found-domain
166 ;; Need to either stick the new cookie in existing domain storage
167 ;; or possibly replace an existing cookie if the names match.
168 (progn
169 (setq storage (cdr found-domain)
170 tmp nil)
171 (while storage
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)))
176 (progn
177 (url-cookie-set-expires cur expires)
178 (url-cookie-set-value cur value)
179 (setq tmp t))))
180 (if (not tmp)
181 ;; New cookie
182 (setcdr found-domain (cons
183 (url-cookie-create :name name
184 :value value
185 :expires expires
186 :domain domain
187 :path path
188 :secure secure)
189 (cdr found-domain)))))
190 ;; Need to add a new top-level domain
191 (setq tmp (url-cookie-create :name name
192 :value value
193 :expires expires
194 :domain domain
195 :path path
196 :secure secure))
197 (cond
198 (storage
199 (setcdr storage (cons (list domain tmp) (cdr storage))))
200 (secure
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)
206 (let* (
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)))
220 (cond
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))))))
235 ;;;###autoload
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)
240 url-cookie-storage))
241 (case-fold-search t)
242 (cookies nil)
243 (cur nil)
244 (retval nil)
245 (path-regexp nil))
246 (while storage
247 (setq cur (car storage)
248 storage (cdr storage)
249 cookies (cdr cur))
250 (if (and (car cur)
251 (string-match (concat "^.*" (regexp-quote (car cur)) "$") host))
252 ;; The domains match - a possible hit!
253 (while cookies
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))))))
261 retval))
263 ;;;###autolaod
264 (defun url-cookie-generate-header-lines (host path secure)
265 (let* ((cookies (url-cookie-retrieve host path secure))
266 (retval nil)
267 (cur nil)
268 (chunk nil))
269 ;; Have to sort this for sending most specific cookies first
270 (setq cookies (and cookies
271 (sort cookies
272 (function
273 (lambda (x y)
274 (> (length (url-cookie-path x))
275 (length (url-cookie-path y))))))))
276 (while cookies
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)
283 (if retval
284 (concat retval "; " chunk)
285 (concat "Cookie: " chunk)))))
286 (if retval
287 (concat retval "\r\n")
288 "")))
290 (defvar url-cookie-two-dot-domains
291 (concat "\\.\\("
292 (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int")
293 "\\|")
294 "\\)$")
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)
301 :group 'url-cookie)
303 (defcustom url-cookie-untrusted-urls nil
304 "*A list of regular expressions matching URLs to never accept cookies from."
305 :type '(repeat regexp)
306 :group 'url-cookie)
308 (defun url-cookie-host-can-set-p (host domain)
309 (let ((numdots 0)
310 (tmp domain)
311 (last nil)
312 (case-fold-search t)
313 (mindots 3))
314 (while (setq last (string-match "\\." domain last))
315 (setq numdots (1+ numdots)
316 last (1+ last)))
317 (if (string-match url-cookie-two-dot-domains domain)
318 (setq mindots 2))
319 (cond
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))
327 nil))))
329 ;;;###autoload
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))
333 (case-fold-search 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))
342 (file-name-directory
343 (url-filename url-current-object))))
344 (rest nil))
345 (while args
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.
354 (if (and expires
355 (string-match
356 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
357 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
358 expires))
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
368 ;; - vs -
369 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
370 (if (and expires
371 (string-match
372 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
373 expires))
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)))
383 (pop trusted)))
384 (while (consp untrusted)
385 (if (string-match (car untrusted) current-url)
386 (setq untrusted (- (match-end 0) (match-beginning 0)))
387 (pop untrusted)))
388 (if (and trusted untrusted)
389 ;; Choose the more specific match
390 (if (> trusted untrusted)
391 (setq untrusted nil)
392 (setq trusted nil)))
393 (cond
394 (untrusted
395 ;; The site was explicity marked as untrusted by the user
396 nil)
397 ((or (eq url-privacy-level 'paranoid)
398 (and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
399 ;; user never wants cookies
400 nil)
401 ((and url-cookie-confirmation
402 (not trusted)
403 (save-window-excursion
404 (with-output-to-temp-buffer "*Cookie Warning*"
405 (mapcar
406 (function
407 (lambda (x)
408 (princ (format "%s - %s" (car x) (cdr x))))) rest))
409 (prog1
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.
416 nil)
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
419 (let ((cur nil))
420 (while rest
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)
437 (and (featurep 'url)
438 (fboundp 'url-cookie-setup-save-timer)
439 (url-cookie-setup-save-timer))))
440 :type 'integer
441 :group 'url)
443 ;;;###autoload
444 (defun url-cookie-setup-save-timer ()
445 "Reset the cookie saver timer."
446 (interactive)
447 (ignore-errors
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
453 (cond
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