(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / url / url-cookie.el
blob328e60b63bcb238e7fa2c2caef86757fb89023cb
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."
91 :type 'boolean
92 :group 'url-cookie)
94 (defvar url-cookies-changed-since-last-save nil
95 "Whether the cookies list has changed since the last save operation.")
97 ;;;###autoload
98 (defun url-cookie-parse-file (&optional fname)
99 (setq fname (or fname url-cookie-file))
100 (condition-case ()
101 (load fname nil t)
102 (error (message "Could not load cookie file %s" fname))))
104 (defun url-cookie-clean-up (&optional secure)
105 (let* (
106 (var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
107 (val (symbol-value var))
108 (cur nil)
109 (new nil)
110 (cookies nil)
111 (cur-cookie nil)
112 (new-cookies nil)
114 (while val
115 (setq cur (car val)
116 val (cdr val)
117 new-cookies nil
118 cookies (cdr cur))
119 (while cookies
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))))
131 (set var new)))
133 ;;;###autoload
134 (defun url-cookie-write-file (&optional fname)
135 (setq fname (or fname url-cookie-file))
136 (cond
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)
143 (save-excursion
144 (set-buffer (get-buffer-create " *cookies*"))
145 (erase-buffer)
146 (fundamental-mode)
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))
153 (insert ")\n")
154 (write-file fname)
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))
160 (tmp storage)
161 (cur nil)
162 (found-domain nil))
164 ;; First, look for a matching domain
165 (setq found-domain (assoc domain storage))
167 (if found-domain
168 ;; Need to either stick the new cookie in existing domain storage
169 ;; or possibly replace an existing cookie if the names match.
170 (progn
171 (setq storage (cdr found-domain)
172 tmp nil)
173 (while storage
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)))
178 (progn
179 (url-cookie-set-expires cur expires)
180 (url-cookie-set-value cur value)
181 (setq tmp t))))
182 (if (not tmp)
183 ;; New cookie
184 (setcdr found-domain (cons
185 (url-cookie-create :name name
186 :value value
187 :expires expires
188 :domain domain
189 :path path
190 :secure secure)
191 (cdr found-domain)))))
192 ;; Need to add a new top-level domain
193 (setq tmp (url-cookie-create :name name
194 :value value
195 :expires expires
196 :domain domain
197 :path path
198 :secure secure))
199 (cond
200 (storage
201 (setcdr storage (cons (list domain tmp) (cdr storage))))
202 (secure
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)
208 (let* (
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)))
222 (cond
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))))))
237 ;;;###autoload
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)
242 url-cookie-storage))
243 (case-fold-search t)
244 (cookies nil)
245 (cur nil)
246 (retval nil)
247 (path-regexp nil))
248 (while storage
249 (setq cur (car storage)
250 storage (cdr storage)
251 cookies (cdr cur))
252 (if (and (car cur)
253 (string-match (concat "^.*" (regexp-quote (car cur)) "$") host))
254 ;; The domains match - a possible hit!
255 (while cookies
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))))))
263 retval))
265 ;;;###autolaod
266 (defun url-cookie-generate-header-lines (host path secure)
267 (let* ((cookies (url-cookie-retrieve host path secure))
268 (retval nil)
269 (cur nil)
270 (chunk nil))
271 ;; Have to sort this for sending most specific cookies first
272 (setq cookies (and cookies
273 (sort cookies
274 (function
275 (lambda (x y)
276 (> (length (url-cookie-path x))
277 (length (url-cookie-path y))))))))
278 (while cookies
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)
285 (if retval
286 (concat retval "; " chunk)
287 (concat "Cookie: " chunk)))))
288 (if retval
289 (concat retval "\r\n")
290 "")))
292 (defvar url-cookie-two-dot-domains
293 (concat "\\.\\("
294 (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int")
295 "\\|")
296 "\\)$")
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)
303 :group 'url-cookie)
305 (defcustom url-cookie-untrusted-urls nil
306 "*A list of regular expressions matching URLs to never accept cookies from."
307 :type '(repeat regexp)
308 :group 'url-cookie)
310 (defun url-cookie-host-can-set-p (host domain)
311 (let ((numdots 0)
312 (tmp domain)
313 (last nil)
314 (case-fold-search t)
315 (mindots 3))
316 (while (setq last (string-match "\\." domain last))
317 (setq numdots (1+ numdots)
318 last (1+ last)))
319 (if (string-match url-cookie-two-dot-domains domain)
320 (setq mindots 2))
321 (cond
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))
329 nil))))
331 ;;;###autoload
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))
335 (case-fold-search 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))
344 (file-name-directory
345 (url-filename url-current-object))))
346 (rest nil))
347 (while args
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.
356 (if (and expires
357 (string-match
358 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
359 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
360 expires))
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
370 ;; - vs -
371 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
372 (if (and expires
373 (string-match
374 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
375 expires))
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)))
385 (pop trusted)))
386 (while (consp untrusted)
387 (if (string-match (car untrusted) current-url)
388 (setq untrusted (- (match-end 0) (match-beginning 0)))
389 (pop untrusted)))
390 (if (and trusted untrusted)
391 ;; Choose the more specific match
392 (if (> trusted untrusted)
393 (setq untrusted nil)
394 (setq trusted nil)))
395 (cond
396 (untrusted
397 ;; The site was explicity marked as untrusted by the user
398 nil)
399 ((or (eq url-privacy-level 'paranoid)
400 (and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
401 ;; user never wants cookies
402 nil)
403 ((and url-cookie-confirmation
404 (not trusted)
405 (save-window-excursion
406 (with-output-to-temp-buffer "*Cookie Warning*"
407 (mapcar
408 (function
409 (lambda (x)
410 (princ (format "%s - %s" (car x) (cdr x))))) rest))
411 (prog1
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.
418 nil)
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
421 (let ((cur nil))
422 (while rest
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)
439 (and (featurep 'url)
440 (fboundp 'url-cookie-setup-save-timer)
441 (url-cookie-setup-save-timer))))
442 :type 'integer
443 :group 'url)
445 ;;;###autoload
446 (defun url-cookie-setup-save-timer ()
447 "Reset the cookie saver timer."
448 (interactive)
449 (ignore-errors
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
455 (cond
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