annotate lisp/url/url-cookie.el @ 74:54cc21c15cbb r20-0b32

Import from CVS: tag r20-0b32
author cvs
date Mon, 13 Aug 2007 09:04:33 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
1 ;;; url-cookie.el,v --- Netscape Cookie support
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
2 ;; Author: wmperry
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
3 ;; Created: 1996/06/05 14:31:40
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
4 ;; Version: 1.9
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
5 ;; Keywords: comm, data, processes, hypermedia
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
6
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
8 ;;; Copyright (c) 1996 by William M. Perry (wmperry@spry.com)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
9 ;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
10 ;;; This file is not part of GNU Emacs, but the same permissions apply.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
11 ;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
13 ;;; it under the terms of the GNU General Public License as published by
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
14 ;;; the Free Software Foundation; either version 2, or (at your option)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
15 ;;; any later version.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
16 ;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
20 ;;; GNU General Public License for more details.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
21 ;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
22 ;;; You should have received a copy of the GNU General Public License
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
26
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
28 ;;; Copyright (c) 1996 by William M. Perry (wmperry@spry.com) ;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
30
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
31 (require 'timezone)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
32 (require 'cl)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
33
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
34 (let ((keywords
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
35 '(:name :value :expires :path :domain :test :secure)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
36 (while keywords
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
37 (or (boundp (car keywords))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
38 (set (car keywords) (car keywords)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
39 (setq keywords (cdr keywords))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
40
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
41 ;; See http://home.netscape.com/newsref/std/cookie_spec.html for the
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
42 ;; 'open standard' defining this crap.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
43 ;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
44 ;; A cookie is stored internally as a vector of 7 slots
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
45 ;; [ 'cookie name value expires path domain secure ]
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
46
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
47 (defsubst url-cookie-name (cookie) (aref cookie 1))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
48 (defsubst url-cookie-value (cookie) (aref cookie 2))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
49 (defsubst url-cookie-expires (cookie) (aref cookie 3))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
50 (defsubst url-cookie-path (cookie) (aref cookie 4))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
51 (defsubst url-cookie-domain (cookie) (aref cookie 5))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
52 (defsubst url-cookie-secure (cookie) (aref cookie 6))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
53
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
54 (defsubst url-cookie-set-name (cookie val) (aset cookie 1 val))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
55 (defsubst url-cookie-set-value (cookie val) (aset cookie 2 val))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
56 (defsubst url-cookie-set-expires (cookie val) (aset cookie 3 val))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
57 (defsubst url-cookie-set-path (cookie val) (aset cookie 4 val))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
58 (defsubst url-cookie-set-domain (cookie val) (aset cookie 5 val))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
59 (defsubst url-cookie-set-secure (cookie val) (aset cookie 6 val))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
60 (defsubst url-cookie-retrieve-arg (key args) (nth 1 (memq key args)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
61
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
62 (defsubst url-cookie-create (&rest args)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
63 (let ((retval (make-vector 7 nil)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
64 (aset retval 0 'cookie)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
65 (url-cookie-set-name retval (url-cookie-retrieve-arg :name args))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
66 (url-cookie-set-value retval (url-cookie-retrieve-arg :value args))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
67 (url-cookie-set-expires retval (url-cookie-retrieve-arg :expires args))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
68 (url-cookie-set-path retval (url-cookie-retrieve-arg :path args))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
69 (url-cookie-set-domain retval (url-cookie-retrieve-arg :domain args))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
70 (url-cookie-set-secure retval (url-cookie-retrieve-arg :secure args))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
71 retval))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
72
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
73 (defvar url-cookie-storage nil "Where cookies are stored.")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
74 (defvar url-cookie-secure-storage nil "Where secure cookies are stored.")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
75 (defvar url-cookie-file nil "*Where cookies are stored on disk.")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
76
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
77 (defun url-cookie-p (obj)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
78 (and (vectorp obj) (= (length obj) 7) (eq (aref obj 0) 'cookie)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
79
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
80 (defun url-cookie-parse-file (&optional fname)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
81 (setq fname (or fname url-cookie-file))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
82 (condition-case ()
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
83 (load fname nil t)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
84 (error (message "Could not load cookie file %s" fname))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
85
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
86 (defun url-cookie-clean-up (&optional secure)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
87 (let* (
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
88 (var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
89 (val (symbol-value var))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
90 (cur nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
91 (new nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
92 (cookies nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
93 (cur-cookie nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
94 (new-cookies nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
95 )
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
96 (while val
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
97 (setq cur (car val)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
98 val (cdr val)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
99 new-cookies nil
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
100 cookies (cdr cur))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
101 (while cookies
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
102 (setq cur-cookie (car cookies)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
103 cookies (cdr cookies))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
104 (if (or (not (url-cookie-p cur-cookie))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
105 (url-cookie-expired-p cur-cookie)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
106 (null (url-cookie-expires cur-cookie)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
107 nil
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
108 (setq new-cookies (cons cur-cookie new-cookies))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
109 (if (not new-cookies)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
110 nil
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
111 (setcdr cur new-cookies)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
112 (setq new (cons cur new))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
113 (set var new)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
114
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
115 (defun url-cookie-write-file (&optional fname)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
116 (setq fname (or fname url-cookie-file))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
117 (url-cookie-clean-up)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
118 (url-cookie-clean-up t)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
119 (save-excursion
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
120 (set-buffer (get-buffer-create " *cookies*"))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
121 (erase-buffer)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
122 (fundamental-mode)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
123 (insert ";; Emacs-W3 HTTP cookies file\n"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
124 ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
125 "(setq url-cookie-storage\n '")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
126 (pp url-cookie-storage (current-buffer))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
127 (insert ")\n(setq url-cookie-secure-storage\n '")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
128 (pp url-cookie-secure-storage (current-buffer))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
129 (insert ")\n")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
130 (write-file fname)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
131 (kill-buffer (current-buffer))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
132
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
133 (defun url-cookie-store (name value &optional expires domain path secure)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
134 "Stores a netscape-style cookie"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
135 (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
136 (tmp storage)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
137 (cur nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
138 (found-domain nil))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
139
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
140 ;; First, look for a matching domain
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
141 (setq found-domain (assoc domain storage))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
142
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
143 (if found-domain
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
144 ;; Need to either stick the new cookie in existing domain storage
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
145 ;; or possibly replace an existing cookie if the names match.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
146 (progn
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
147 (setq storage (cdr found-domain)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
148 tmp nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
149 (while storage
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
150 (setq cur (car storage)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
151 storage (cdr storage))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
152 (if (and (equal path (url-cookie-path cur))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
153 (equal name (url-cookie-name cur)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
154 (progn
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
155 (url-cookie-set-expires cur expires)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
156 (url-cookie-set-value cur value)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
157 (setq tmp t))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
158 (if (not tmp)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
159 ;; New cookie
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
160 (setcdr found-domain (cons
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
161 (url-cookie-create :name name
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
162 :value value
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
163 :expires expires
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
164 :domain domain
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
165 :path path
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
166 :secure secure)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
167 (cdr found-domain)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
168 ;; Need to add a new top-level domain
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
169 (setq tmp (url-cookie-create :name name
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
170 :value value
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
171 :expires expires
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
172 :domain domain
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
173 :path path
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
174 :secure secure))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
175 (cond
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
176 (storage
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
177 (setcdr storage (cons (list domain tmp) (cdr storage))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
178 (secure
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
179 (setq url-cookie-secure-storage (list (list domain tmp))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
180 (t
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
181 (setq url-cookie-storage (list (list domain tmp))))))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
182
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
183 (defun url-cookie-expired-p (cookie)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
184 (let* (
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
185 (exp (url-cookie-expires cookie))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
186 (cur-date (and exp (timezone-parse-date (current-time-string))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
187 (exp-date (and exp (timezone-parse-date exp)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
188 (cur-greg (and cur-date (timezone-absolute-from-gregorian
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
189 (string-to-int (aref cur-date 1))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
190 (string-to-int (aref cur-date 2))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
191 (string-to-int (aref cur-date 0)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
192 (exp-greg (and exp (timezone-absolute-from-gregorian
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
193 (string-to-int (aref exp-date 1))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
194 (string-to-int (aref exp-date 2))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
195 (string-to-int (aref exp-date 0)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
196 (diff-in-days (and exp (- cur-greg exp-greg)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
197 )
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
198 (cond
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
199 ((not exp) nil) ; No expiry == expires at browser quit
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
200 ((< diff-in-days 0) nil) ; Expires sometime after today
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
201 ((> diff-in-days 0) t) ; Expired before today
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
202 (t ; Expires sometime today, check times
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
203 (let* ((cur-time (timezone-parse-time (aref cur-date 3)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
204 (exp-time (timezone-parse-time (aref exp-date 3)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
205 (cur-norm (+ (* 360 (string-to-int (aref cur-time 2)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
206 (* 60 (string-to-int (aref cur-time 1)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
207 (* 1 (string-to-int (aref cur-time 0)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
208 (exp-norm (+ (* 360 (string-to-int (aref exp-time 2)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
209 (* 60 (string-to-int (aref exp-time 1)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
210 (* 1 (string-to-int (aref exp-time 0))))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
211 (> (- cur-norm exp-norm) 1))))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
212
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
213 (defun url-cookie-retrieve (host path &optional secure)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
214 "Retrieves all the netscape-style cookies for a specified HOST and PATH"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
215 (let ((storage (if secure
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
216 (append url-cookie-secure-storage url-cookie-storage)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
217 url-cookie-storage))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
218 (case-fold-search t)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
219 (cookies nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
220 (cur nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
221 (retval nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
222 (path-regexp nil))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
223 (while storage
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
224 (setq cur (car storage)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
225 storage (cdr storage)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
226 cookies (cdr cur))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
227 (if (and (car cur)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
228 (string-match (concat "^.*" (regexp-quote (car cur)) "$") host))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
229 ;; The domains match - a possible hit!
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
230 (while cookies
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
231 (setq cur (car cookies)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
232 cookies (cdr cookies)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
233 path-regexp (concat "^" (regexp-quote
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
234 (url-cookie-path cur))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
235 (if (and (string-match path-regexp path)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
236 (not (url-cookie-expired-p cur)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
237 (setq retval (cons cur retval))))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
238 retval))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
239
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
240 (defun url-cookie-generate-header-lines (host path secure)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
241 (let* ((cookies (url-cookie-retrieve host path secure))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
242 (retval nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
243 (cur nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
244 (chunk nil))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
245 ;; Have to sort this for sending most specific cookies first
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
246 (setq cookies (and cookies
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
247 (sort cookies
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
248 (function
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
249 (lambda (x y)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
250 (> (length (url-cookie-path x))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
251 (length (url-cookie-path y))))))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
252 (while cookies
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
253 (setq cur (car cookies)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
254 cookies (cdr cookies)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
255 chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
256 retval (if (< 80 (+ (length retval) (length chunk) 4))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
257 (concat retval "\r\nCookie: " chunk)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
258 (if retval
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
259 (concat retval "; " chunk)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
260 (concat "Cookie: " chunk)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
261 (if retval
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
262 (concat retval "\r\n")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
263 "")))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
264
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
265 (defvar url-cookie-two-dot-domains
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
266 (concat "\\.\\("
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
267 (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
268 "\\|")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
269 "\\)$")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
270 "A regular expression of top-level domains that only require two matching
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
271 '.'s in the domain name in order to set a cookie.")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
272
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
273 (defun url-cookie-host-can-set-p (host domain)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
274 (let ((numdots 0)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
275 (tmp domain)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
276 (last nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
277 (case-fold-search t)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
278 (mindots 3))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
279 (while (setq last (string-match "\\." host last))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
280 (setq numdots (1+ numdots)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
281 last (1+ last)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
282 (if (string-match url-cookie-two-dot-domains domain)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
283 (setq mindots 2))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
284 (cond
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
285 ((string= host domain) ; Apparently netscape lets you do this
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
286 t)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
287 ((< numdots mindots) ; Not enough dots in domain name!
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
288 nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
289 (t
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
290 (string-match (concat (regexp-quote domain) "$") host)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
291
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
292 (defun url-header-comparison (x y)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
293 (string= (downcase x) (downcase y)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
294
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
295 (defun url-cookie-handle-set-cookie (str)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
296 (let* ((args (mm-parse-args str nil t)) ; Don't downcase names
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
297 (case-fold-search t)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
298 (secure (and (assoc* "secure" args :test 'url-header-comparison) t))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
299 (domain (or (cdr-safe (assoc* "domain" args :test
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
300 'url-header-comparison))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
301 url-current-server))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
302 (expires (cdr-safe (assoc* "expires" args :test
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
303 'url-header-comparison)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
304 (path (or (cdr-safe (assoc* "path" args :test
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
305 'url-header-comparison))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
306 (file-name-directory url-current-file)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
307 (rest nil))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
308 (while args
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
309 (if (not (member (downcase (car (car args)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
310 '("secure" "domain" "expires" "path")))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
311 (setq rest (cons (car args) rest)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
312 (setq args (cdr args)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
313
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
314 ;; Sometimes we get dates that the timezone package cannot handle very
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
315 ;; gracefully - take care of this here, instead of in url-cookie-expired-p
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
316 ;; to speed things up.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
317 (if (and expires
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
318 (string-match
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
319 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
320 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
321 expires))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
322 (setq expires (concat (url-match expires 1) " "
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
323 (url-match expires 2) " "
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
324 (url-match expires 3) " "
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
325 (url-match expires 4) " ["
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
326 (url-match expires 5) "]")))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
327 (cond
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
328 ((and (listp url-privacy-level) (memq 'cookies url-privacy-level))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
329 ;; user never wants cookies
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
330 nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
331 ((and url-cookie-confirmation
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
332 (not (funcall url-confirmation-func
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
333 (format "Allow %s to set a cookie? "
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
334 url-current-server))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
335 ;; user wants to be asked, and declined.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
336 nil)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
337 ((url-cookie-host-can-set-p url-current-server domain)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
338 ;; Cookie is accepted by the user, and passes our security checks
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
339 (while rest
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
340 (url-cookie-store (car (car rest)) (cdr (car rest))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
341 expires domain path secure)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
342 (setq rest (cdr rest))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
343 (t
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
344 (url-warn 'url (format
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
345 (concat "%s tried to set a cookie for domain %s\n"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
346 "Permission denied - cookie rejected.\n"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
347 "Set-Cookie: %s")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
348 url-current-server domain str))))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
349
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
350 (provide 'url-cookie)