Mercurial > emacs
annotate lisp/url/url-cookie.el @ 86713:b8932c30467d
Move here from lisp/nxml/char-name/unicode
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Wed, 28 Nov 2007 05:07:18 +0000 |
parents | 133e736aa5c6 |
children | 9bdd5616eaa8 |
rev | line source |
---|---|
54695 | 1 ;;; url-cookie.el --- Netscape Cookie support |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
2 |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, |
75347 | 4 ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
5 |
54695 | 6 ;; Keywords: comm, data, processes, hypermedia |
7 | |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
9 ;; |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by |
78222
8932997d0b62
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
76859
diff
changeset
|
12 ;; the Free Software Foundation; either version 3, or (at your option) |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
13 ;; any later version. |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
14 ;; |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
18 ;; GNU General Public License for more details. |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
19 ;; |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64084 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
24 |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
25 ;;; Commentary: |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
26 |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
27 ;;; Code: |
54695 | 28 |
29 (require 'timezone) | |
30 (require 'url-util) | |
31 (require 'url-parse) | |
32 (eval-when-compile (require 'cl)) | |
33 | |
34 ;; See http://home.netscape.com/newsref/std/cookie_spec.html for the | |
35 ;; 'open standard' defining this crap. | |
36 | |
37 (defgroup url-cookie nil | |
63986
8d2417e75fe5
(url-cookie): Finish `defgroup' description with period.
Juanma Barranquero <lekktu@gmail.com>
parents:
63801
diff
changeset
|
38 "URL cookies." |
54695 | 39 :prefix "url-" |
40 :prefix "url-cookie-" | |
41 :group 'url) | |
42 | |
81328
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
43 ;; A cookie is stored internally as a vector of 7 slots |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
44 ;; [ cookie NAME VALUE EXPIRES LOCALPART DOMAIN SECURE ] |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
45 |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
46 (defstruct (url-cookie |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
47 (:constructor url-cookie-create) |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
48 (:copier nil) |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
49 ;; For compatibility with a previous version which did not use |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
50 ;; defstruct, and also in order to make sure that the printed |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
51 ;; representation does not depend on CL internals, we use an |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
52 ;; explicitly managed tag. |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
53 (:type vector)) |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
54 (tag 'cookie :read-only t) |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
55 name value expires localpart domain secure) |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
56 |
54695 | 57 (defvar url-cookie-storage nil "Where cookies are stored.") |
58 (defvar url-cookie-secure-storage nil "Where secure cookies are stored.") | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
59 (defcustom url-cookie-file nil |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
60 "File where cookies are stored on disk." |
54695 | 61 :type '(choice (const :tag "Default" :value nil) file) |
62 :group 'url-file | |
63 :group 'url-cookie) | |
64 | |
65 (defcustom url-cookie-confirmation nil | |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
66 "If non-nil, confirmation by the user is required to accept HTTP cookies." |
54695 | 67 :type 'boolean |
68 :group 'url-cookie) | |
69 | |
70 (defcustom url-cookie-multiple-line nil | |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
71 "If nil, HTTP requests put all cookies for the server on one line. |
54695 | 72 Some web servers, such as http://www.hotmail.com/, only accept cookies |
63248
457f83187ef5
(url-cookie-multiple-line): Fix spelling in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
62529
diff
changeset
|
73 when they are on one line. This is broken behavior, but just try |
62529
b2ba3b366582
(url-cookie-multiple-line): Specify group and type in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents:
62400
diff
changeset
|
74 telling Microsoft that." |
b2ba3b366582
(url-cookie-multiple-line): Specify group and type in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents:
62400
diff
changeset
|
75 :type 'boolean |
b2ba3b366582
(url-cookie-multiple-line): Specify group and type in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents:
62400
diff
changeset
|
76 :group 'url-cookie) |
54695 | 77 |
78 (defvar url-cookies-changed-since-last-save nil | |
79 "Whether the cookies list has changed since the last save operation.") | |
80 | |
81 (defun url-cookie-parse-file (&optional fname) | |
82 (setq fname (or fname url-cookie-file)) | |
83 (condition-case () | |
84 (load fname nil t) | |
68055
63366306c117
(url-cookie-parse-file): Don't complain of missing file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
67723
diff
changeset
|
85 (error |
63366306c117
(url-cookie-parse-file): Don't complain of missing file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
67723
diff
changeset
|
86 ;; It's completely normal for the cookies file not to exist yet. |
63366306c117
(url-cookie-parse-file): Don't complain of missing file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
67723
diff
changeset
|
87 ;; (message "Could not load cookie file %s" fname) |
63366306c117
(url-cookie-parse-file): Don't complain of missing file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
67723
diff
changeset
|
88 ))) |
54695 | 89 |
86486 | 90 ;; check-declare does not handle defstruct. |
91 ;;;(declare-function url-cookie-p "url-cookie") | |
92 | |
54695 | 93 (defun url-cookie-clean-up (&optional secure) |
94 (let* ( | |
95 (var (if secure 'url-cookie-secure-storage 'url-cookie-storage)) | |
96 (val (symbol-value var)) | |
97 (cur nil) | |
98 (new nil) | |
99 (cookies nil) | |
100 (cur-cookie nil) | |
101 (new-cookies nil) | |
102 ) | |
103 (while val | |
104 (setq cur (car val) | |
105 val (cdr val) | |
106 new-cookies nil | |
107 cookies (cdr cur)) | |
108 (while cookies | |
109 (setq cur-cookie (car cookies) | |
110 cookies (cdr cookies)) | |
111 (if (or (not (url-cookie-p cur-cookie)) | |
112 (url-cookie-expired-p cur-cookie) | |
113 (null (url-cookie-expires cur-cookie))) | |
114 nil | |
115 (setq new-cookies (cons cur-cookie new-cookies)))) | |
116 (if (not new-cookies) | |
117 nil | |
118 (setcdr cur new-cookies) | |
119 (setq new (cons cur new)))) | |
120 (set var new))) | |
121 | |
122 (defun url-cookie-write-file (&optional fname) | |
123 (setq fname (or fname url-cookie-file)) | |
67723
f0ddbd3730d3
(url-cookie-write-file): Create parent dir.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
67368
diff
changeset
|
124 (unless (file-directory-p (file-name-directory fname)) |
f0ddbd3730d3
(url-cookie-write-file): Create parent dir.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
67368
diff
changeset
|
125 (ignore-errors (make-directory (file-name-directory fname)))) |
54695 | 126 (cond |
127 ((not url-cookies-changed-since-last-save) nil) | |
128 ((not (file-writable-p fname)) | |
129 (message "Cookies file %s (see variable `url-cookie-file') is unwritable." fname)) | |
130 (t | |
131 (url-cookie-clean-up) | |
132 (url-cookie-clean-up t) | |
67723
f0ddbd3730d3
(url-cookie-write-file): Create parent dir.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
67368
diff
changeset
|
133 (with-current-buffer (get-buffer-create " *cookies*") |
54695 | 134 (erase-buffer) |
135 (fundamental-mode) | |
136 (insert ";; Emacs-W3 HTTP cookies file\n" | |
137 ";; Automatically generated file!!! DO NOT EDIT!!!\n\n" | |
138 "(setq url-cookie-storage\n '") | |
139 (pp url-cookie-storage (current-buffer)) | |
140 (insert ")\n(setq url-cookie-secure-storage\n '") | |
141 (pp url-cookie-secure-storage (current-buffer)) | |
142 (insert ")\n") | |
72573
fd926d483a42
(url-cookie-write-file): Really don't use versioned backups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
71788
diff
changeset
|
143 (insert "\n;; Local Variables:\n" |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
144 ";; version-control: never\n" |
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
145 ";; no-byte-compile: t\n" |
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
146 ";; End:\n") |
72573
fd926d483a42
(url-cookie-write-file): Really don't use versioned backups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
71788
diff
changeset
|
147 (set (make-local-variable 'version-control) 'never) |
54695 | 148 (write-file fname) |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
149 (setq url-cookies-changed-since-last-save nil) |
54695 | 150 (kill-buffer (current-buffer)))))) |
151 | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
152 (defun url-cookie-store (name value &optional expires domain localpart secure) |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
153 "Store a netscape-style cookie." |
54695 | 154 (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage)) |
155 (tmp storage) | |
156 (cur nil) | |
157 (found-domain nil)) | |
158 | |
159 ;; First, look for a matching domain | |
160 (setq found-domain (assoc domain storage)) | |
161 | |
162 (if found-domain | |
163 ;; Need to either stick the new cookie in existing domain storage | |
164 ;; or possibly replace an existing cookie if the names match. | |
165 (progn | |
166 (setq storage (cdr found-domain) | |
167 tmp nil) | |
168 (while storage | |
169 (setq cur (car storage) | |
170 storage (cdr storage)) | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
171 (if (and (equal localpart (url-cookie-localpart cur)) |
54695 | 172 (equal name (url-cookie-name cur))) |
173 (progn | |
81328
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
174 (setf (url-cookie-expires cur) expires) |
99942c448a6f
(url-cookie-name, url-cookie-value, url-cookie-expires, url-cookie-localpart)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
76859
diff
changeset
|
175 (setf (url-cookie-value cur) value) |
54695 | 176 (setq tmp t)))) |
177 (if (not tmp) | |
178 ;; New cookie | |
179 (setcdr found-domain (cons | |
180 (url-cookie-create :name name | |
181 :value value | |
182 :expires expires | |
183 :domain domain | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
184 :localpart localpart |
54695 | 185 :secure secure) |
186 (cdr found-domain))))) | |
187 ;; Need to add a new top-level domain | |
188 (setq tmp (url-cookie-create :name name | |
189 :value value | |
190 :expires expires | |
191 :domain domain | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
192 :localpart localpart |
54695 | 193 :secure secure)) |
194 (cond | |
195 (storage | |
196 (setcdr storage (cons (list domain tmp) (cdr storage)))) | |
197 (secure | |
198 (setq url-cookie-secure-storage (list (list domain tmp)))) | |
199 (t | |
200 (setq url-cookie-storage (list (list domain tmp)))))))) | |
201 | |
202 (defun url-cookie-expired-p (cookie) | |
203 (let* ( | |
204 (exp (url-cookie-expires cookie)) | |
205 (cur-date (and exp (timezone-parse-date (current-time-string)))) | |
206 (exp-date (and exp (timezone-parse-date exp))) | |
207 (cur-greg (and cur-date (timezone-absolute-from-gregorian | |
62400
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
208 (string-to-number (aref cur-date 1)) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
209 (string-to-number (aref cur-date 2)) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
210 (string-to-number (aref cur-date 0))))) |
54695 | 211 (exp-greg (and exp (timezone-absolute-from-gregorian |
62400
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
212 (string-to-number (aref exp-date 1)) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
213 (string-to-number (aref exp-date 2)) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
214 (string-to-number (aref exp-date 0))))) |
54695 | 215 (diff-in-days (and exp (- cur-greg exp-greg))) |
216 ) | |
217 (cond | |
218 ((not exp) nil) ; No expiry == expires at browser quit | |
219 ((< diff-in-days 0) nil) ; Expires sometime after today | |
220 ((> diff-in-days 0) t) ; Expired before today | |
221 (t ; Expires sometime today, check times | |
222 (let* ((cur-time (timezone-parse-time (aref cur-date 3))) | |
223 (exp-time (timezone-parse-time (aref exp-date 3))) | |
62400
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
224 (cur-norm (+ (* 360 (string-to-number (aref cur-time 2))) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
225 (* 60 (string-to-number (aref cur-time 1))) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
226 (* 1 (string-to-number (aref cur-time 0))))) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
227 (exp-norm (+ (* 360 (string-to-number (aref exp-time 2))) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
228 (* 60 (string-to-number (aref exp-time 1))) |
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
229 (* 1 (string-to-number (aref exp-time 0)))))) |
54695 | 230 (> (- cur-norm exp-norm) 1)))))) |
231 | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
232 (defun url-cookie-retrieve (host localpart &optional secure) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
233 "Retrieve all the netscape-style cookies for a specified HOST and LOCALPART." |
54695 | 234 (let ((storage (if secure |
235 (append url-cookie-secure-storage url-cookie-storage) | |
236 url-cookie-storage)) | |
237 (case-fold-search t) | |
238 (cookies nil) | |
239 (cur nil) | |
240 (retval nil) | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
241 (localpart-regexp nil)) |
54695 | 242 (while storage |
243 (setq cur (car storage) | |
244 storage (cdr storage) | |
245 cookies (cdr cur)) | |
246 (if (and (car cur) | |
76859
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
247 (string-match |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
248 (concat "^.*" |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
249 (regexp-quote |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
250 ;; Remove the dot from wildcard domains |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
251 ;; before matching. |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
252 (if (eq ?. (aref (car cur) 0)) |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
253 (substring (car cur) 1) |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
254 (car cur))) |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
255 "$") host)) |
54695 | 256 ;; The domains match - a possible hit! |
257 (while cookies | |
258 (setq cur (car cookies) | |
259 cookies (cdr cookies) | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
260 localpart-regexp (concat "^" (regexp-quote |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
261 (url-cookie-localpart cur)))) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
262 (if (and (string-match localpart-regexp localpart) |
54695 | 263 (not (url-cookie-expired-p cur))) |
264 (setq retval (cons cur retval)))))) | |
265 retval)) | |
266 | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
267 (defun url-cookie-generate-header-lines (host localpart secure) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
268 (let* ((cookies (url-cookie-retrieve host localpart secure)) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
269 (retval nil) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
270 (cur nil) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
271 (chunk nil)) |
54695 | 272 ;; Have to sort this for sending most specific cookies first |
273 (setq cookies (and cookies | |
274 (sort cookies | |
275 (function | |
276 (lambda (x y) | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
277 (> (length (url-cookie-localpart x)) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
278 (length (url-cookie-localpart y)))))))) |
54695 | 279 (while cookies |
280 (setq cur (car cookies) | |
281 cookies (cdr cookies) | |
282 chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur)) | |
283 retval (if (and url-cookie-multiple-line | |
284 (< 80 (+ (length retval) (length chunk) 4))) | |
285 (concat retval "\r\nCookie: " chunk) | |
286 (if retval | |
287 (concat retval "; " chunk) | |
288 (concat "Cookie: " chunk))))) | |
289 (if retval | |
290 (concat retval "\r\n") | |
291 ""))) | |
292 | |
293 (defvar url-cookie-two-dot-domains | |
294 (concat "\\.\\(" | |
295 (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int") | |
296 "\\|") | |
297 "\\)$") | |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
298 "A regexp of top level domains that only require two matching |
54695 | 299 '.'s in the domain name in order to set a cookie.") |
300 | |
301 (defcustom url-cookie-trusted-urls nil | |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
302 "A list of regular expressions matching URLs to always accept cookies from." |
54695 | 303 :type '(repeat regexp) |
304 :group 'url-cookie) | |
305 | |
306 (defcustom url-cookie-untrusted-urls nil | |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
307 "A list of regular expressions matching URLs to never accept cookies from." |
54695 | 308 :type '(repeat regexp) |
309 :group 'url-cookie) | |
310 | |
311 (defun url-cookie-host-can-set-p (host domain) | |
312 (let ((numdots 0) | |
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 | |
323 t) | |
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. | |
76859
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
327 (string-match (concat (regexp-quote |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
328 ;; Remove the dot from wildcard domains |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
329 ;; before matching. |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
330 (if (eq ?. (aref domain 0)) |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
331 (substring domain 1) |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
332 domain)) |
8fca90acf555
(url-cookie-retrieve, url-cookie-host-can-set-p): Deal with wildcard
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
333 "$") host)) |
54695 | 334 (t |
335 nil)))) | |
336 | |
337 (defun url-cookie-handle-set-cookie (str) | |
338 (setq url-cookies-changed-since-last-save t) | |
339 (let* ((args (url-parse-args str t)) | |
340 (case-fold-search t) | |
54817
daf716c39a7f
(url-cookie-handle-set-cookie): Replace calls to obsolete
John Paul Wallington <jpw@pobox.com>
parents:
54794
diff
changeset
|
341 (secure (and (assoc-string "secure" args t) t)) |
daf716c39a7f
(url-cookie-handle-set-cookie): Replace calls to obsolete
John Paul Wallington <jpw@pobox.com>
parents:
54794
diff
changeset
|
342 (domain (or (cdr-safe (assoc-string "domain" args t)) |
54695 | 343 (url-host url-current-object))) |
344 (current-url (url-view-url t)) | |
345 (trusted url-cookie-trusted-urls) | |
346 (untrusted url-cookie-untrusted-urls) | |
54817
daf716c39a7f
(url-cookie-handle-set-cookie): Replace calls to obsolete
John Paul Wallington <jpw@pobox.com>
parents:
54794
diff
changeset
|
347 (expires (cdr-safe (assoc-string "expires" args t))) |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
348 (localpart (or (cdr-safe (assoc-string "path" args t)) |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
349 (file-name-directory |
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
350 (url-filename url-current-object)))) |
54695 | 351 (rest nil)) |
352 (while args | |
353 (if (not (member (downcase (car (car args))) | |
354 '("secure" "domain" "expires" "path"))) | |
355 (setq rest (cons (car args) rest))) | |
356 (setq args (cdr args))) | |
357 | |
358 ;; Sometimes we get dates that the timezone package cannot handle very | |
359 ;; gracefully - take care of this here, instead of in url-cookie-expired-p | |
360 ;; to speed things up. | |
361 (if (and expires | |
362 (string-match | |
363 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +" | |
364 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$") | |
365 expires)) | |
366 (setq expires (concat (match-string 1 expires) " " | |
367 (match-string 2 expires) " " | |
368 (match-string 3 expires) " " | |
369 (match-string 4 expires) " [" | |
370 (match-string 5 expires) "]"))) | |
371 | |
372 ;; This one is for older Emacs/XEmacs variants that don't | |
373 ;; understand this format without tenths of a second in it. | |
374 ;; Wednesday, 30-Dec-2037 16:00:00 GMT | |
375 ;; - vs - | |
376 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT | |
377 (if (and expires | |
378 (string-match | |
379 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)" | |
380 expires)) | |
381 (setq expires (concat (match-string 1 expires) "-" ; day | |
382 (match-string 2 expires) "-" ; month | |
383 (match-string 3 expires) " " ; year | |
384 (match-string 4 expires) ".00 " ; hour:minutes:seconds | |
385 (match-string 6 expires)))) ":" ; timezone | |
54817
daf716c39a7f
(url-cookie-handle-set-cookie): Replace calls to obsolete
John Paul Wallington <jpw@pobox.com>
parents:
54794
diff
changeset
|
386 |
54695 | 387 (while (consp trusted) |
388 (if (string-match (car trusted) current-url) | |
389 (setq trusted (- (match-end 0) (match-beginning 0))) | |
390 (pop trusted))) | |
391 (while (consp untrusted) | |
392 (if (string-match (car untrusted) current-url) | |
393 (setq untrusted (- (match-end 0) (match-beginning 0))) | |
394 (pop untrusted))) | |
395 (if (and trusted untrusted) | |
396 ;; Choose the more specific match | |
397 (if (> trusted untrusted) | |
398 (setq untrusted nil) | |
399 (setq trusted nil))) | |
400 (cond | |
401 (untrusted | |
402 ;; The site was explicity marked as untrusted by the user | |
403 nil) | |
404 ((or (eq url-privacy-level 'paranoid) | |
405 (and (listp url-privacy-level) (memq 'cookies url-privacy-level))) | |
406 ;; user never wants cookies | |
407 nil) | |
408 ((and url-cookie-confirmation | |
409 (not trusted) | |
410 (save-window-excursion | |
411 (with-output-to-temp-buffer "*Cookie Warning*" | |
412 (mapcar | |
413 (function | |
414 (lambda (x) | |
415 (princ (format "%s - %s" (car x) (cdr x))))) rest)) | |
416 (prog1 | |
417 (not (funcall url-confirmation-func | |
418 (format "Allow %s to set these cookies? " | |
419 (url-host url-current-object)))) | |
420 (if (get-buffer "*Cookie Warning*") | |
421 (kill-buffer "*Cookie Warning*"))))) | |
422 ;; user wants to be asked, and declined. | |
423 nil) | |
424 ((url-cookie-host-can-set-p (url-host url-current-object) domain) | |
425 ;; Cookie is accepted by the user, and passes our security checks | |
426 (let ((cur nil)) | |
427 (while rest | |
428 (setq cur (pop rest)) | |
429 (url-cookie-store (car cur) (cdr cur) | |
63801
c234ee23e519
(url-cookie-store): Rename arg PATH to LOCALPART.
Richard M. Stallman <rms@gnu.org>
parents:
63718
diff
changeset
|
430 expires domain localpart secure)))) |
54695 | 431 (t |
432 (message "%s tried to set a cookie for domain %s - rejected." | |
433 (url-host url-current-object) domain))))) | |
434 | |
435 (defvar url-cookie-timer nil) | |
436 | |
437 (defcustom url-cookie-save-interval 3600 | |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
438 "The number of seconds between automatic saves of cookies. |
54695 | 439 Default is 1 hour. Note that if you change this variable outside of |
440 the `customize' interface after `url-do-setup' has been run, you need | |
441 to run the `url-cookie-setup-save-timer' function manually." | |
67368
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
442 :set #'(lambda (var val) |
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
443 (set-default var val) |
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
444 (if (bound-and-true-p url-setup-done) |
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
445 (url-cookie-setup-save-timer))) |
54695 | 446 :type 'integer |
71788
c4527316e616
Remove spurious * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68055
diff
changeset
|
447 :group 'url-cookie) |
54695 | 448 |
449 (defun url-cookie-setup-save-timer () | |
450 "Reset the cookie saver timer." | |
451 (interactive) | |
67368
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
452 (ignore-errors (cancel-timer url-cookie-timer)) |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
453 (setq url-cookie-timer nil) |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
454 (if url-cookie-save-interval |
67368
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
455 (setq url-cookie-timer (run-at-time url-cookie-save-interval |
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
456 url-cookie-save-interval |
5c87802be570
(url-cookie-save-interval): Simplify.
Richard M. Stallman <rms@gnu.org>
parents:
64748
diff
changeset
|
457 #'url-cookie-write-file)))) |
54695 | 458 |
459 (provide 'url-cookie) | |
460 | |
54794
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
461 ;; arch-tag: 2568751b-6452-4398-aa2d-303edadb54d7 |
9978b1e48c24
(url-cookie-setup-save-timer): Avoid warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
462 ;;; url-cookie.el ends here |