annotate lisp/url/url-cookie.el @ 9:6f2bbbbbe05a

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