emacs/lisp/url/url-cookie.el

355 lines
12 KiB
EmacsLisp
Raw Normal View History

;;; url-cookie.el --- URL cookie support
;; Copyright (C) 1996-1999, 2004-2012 Free Software Foundation, Inc.
2004-04-04 01:21:46 +00:00
;; Keywords: comm, data, processes, hypermedia
;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
2004-04-04 01:21:46 +00:00
(require 'url-util)
(require 'url-parse)
(require 'url-domsuf)
2004-04-04 01:21:46 +00:00
More CL cleanups and reduction of use of cl.el. * woman.el, winner.el, vc/vc-rcs.el, vc/vc-hooks.el, vc/vc-hg.el: * vc/vc-git.el, vc/vc-dir.el, vc/vc-bzr.el, vc/vc-annotate.el: * textmodes/tex-mode.el, textmodes/sgml-mode.el, tar-mode.el: * strokes.el, ses.el, server.el, progmodes/js.el, progmodes/gdb-mi.el: * progmodes/flymake.el, progmodes/ebrowse.el, progmodes/compile.el: * play/tetris.el, play/snake.el, play/pong.el, play/landmark.el: * play/hanoi.el, play/decipher.el, play/5x5.el, nxml/nxml-mode.el: * net/secrets.el, net/quickurl.el, midnight.el, mail/footnote.el: * image-dired.el, ibuffer.el, ibuf-macs.el, ibuf-ext.el, hexl.el: * eshell/eshell.el, eshell/esh-io.el, eshell/esh-ext.el: * eshell/esh-cmd.el, eshell/em-ls.el, eshell/em-hist.el: * eshell/em-cmpl.el, eshell/em-banner.el: * url/url.el, url/url-queue.el, url/url-parse.el, url/url-http.el: * url/url-future.el, url/url-dav.el, url/url-cookie.el: * calendar/parse-time.el, test/eshell.el: Use cl-lib. * wid-browse.el, wdired.el, vc/vc.el, vc/vc-mtn.el, vc/vc-cvs.el: * vc/vc-arch.el, tree-widget.el, textmodes/texinfo.el: * textmodes/refill.el, textmodes/css-mode.el, term/tvi970.el: * term/ns-win.el, term.el, shell.el, ps-samp.el: * progmodes/perl-mode.el, progmodes/pascal.el, progmodes/gud.el: * progmodes/glasses.el, progmodes/etags.el, progmodes/cwarn.el: * play/gamegrid.el, play/bubbles.el, novice.el, notifications.el: * net/zeroconf.el, net/xesam.el, net/snmp-mode.el, net/mairix.el: * net/ldap.el, net/eudc.el, net/browse-url.el, man.el: * mail/mailheader.el, mail/feedmail.el: * url/url-util.el, url/url-privacy.el, url/url-nfs.el, url/url-misc.el: * url/url-methods.el, url/url-gw.el, url/url-file.el, url/url-expand.el: Dont use CL. * ibuf-ext.el (ibuffer-mark-old-buffers): Use float-time. * eshell/esh-opt.el (eshell-eval-using-options): Quote code with `lambda' rather than with `quote'. (eshell-do-opt): Adjust accordingly. (eshell-process-option): Simplify. * eshell/esh-var.el: * eshell/em-script.el: Require `esh-opt' for eshell-eval-using-options. * emacs-pcase.el (pcase--dontcare-upats, pcase--let*) (pcase--expand, pcase--u1): Rename pcase's internal `dontcare' pattern to `pcase--dontcare'. * emacs-cl.el (labels): Mark obsolete. (cl--letf, letf): Move to cl-lib. (cl--letf*, letf*): Remove. * emacs-cl-lib.el (cl-nth-value): Use defalias. * emacs-cl-macs.el (cl-dolist, cl-dotimes): Add indent rule. (cl-progv): Rewrite. (cl--letf, cl-letf): Move from cl.el. (cl-letf*): New macro. * emacs-cl-extra.el (cl--progv-before, cl--progv-after): Remove.
2012-07-11 19:13:41 -04:00
(eval-when-compile (require 'cl-lib))
2004-04-04 01:21:46 +00:00
(defgroup url-cookie nil
"URL cookies."
2004-04-04 01:21:46 +00:00
:prefix "url-"
:prefix "url-cookie-"
:group 'url)
;; A cookie is stored internally as a vector of 7 slots
;; [ url-cookie NAME VALUE EXPIRES LOCALPART DOMAIN SECURE ]
More CL cleanups and reduction of use of cl.el. * woman.el, winner.el, vc/vc-rcs.el, vc/vc-hooks.el, vc/vc-hg.el: * vc/vc-git.el, vc/vc-dir.el, vc/vc-bzr.el, vc/vc-annotate.el: * textmodes/tex-mode.el, textmodes/sgml-mode.el, tar-mode.el: * strokes.el, ses.el, server.el, progmodes/js.el, progmodes/gdb-mi.el: * progmodes/flymake.el, progmodes/ebrowse.el, progmodes/compile.el: * play/tetris.el, play/snake.el, play/pong.el, play/landmark.el: * play/hanoi.el, play/decipher.el, play/5x5.el, nxml/nxml-mode.el: * net/secrets.el, net/quickurl.el, midnight.el, mail/footnote.el: * image-dired.el, ibuffer.el, ibuf-macs.el, ibuf-ext.el, hexl.el: * eshell/eshell.el, eshell/esh-io.el, eshell/esh-ext.el: * eshell/esh-cmd.el, eshell/em-ls.el, eshell/em-hist.el: * eshell/em-cmpl.el, eshell/em-banner.el: * url/url.el, url/url-queue.el, url/url-parse.el, url/url-http.el: * url/url-future.el, url/url-dav.el, url/url-cookie.el: * calendar/parse-time.el, test/eshell.el: Use cl-lib. * wid-browse.el, wdired.el, vc/vc.el, vc/vc-mtn.el, vc/vc-cvs.el: * vc/vc-arch.el, tree-widget.el, textmodes/texinfo.el: * textmodes/refill.el, textmodes/css-mode.el, term/tvi970.el: * term/ns-win.el, term.el, shell.el, ps-samp.el: * progmodes/perl-mode.el, progmodes/pascal.el, progmodes/gud.el: * progmodes/glasses.el, progmodes/etags.el, progmodes/cwarn.el: * play/gamegrid.el, play/bubbles.el, novice.el, notifications.el: * net/zeroconf.el, net/xesam.el, net/snmp-mode.el, net/mairix.el: * net/ldap.el, net/eudc.el, net/browse-url.el, man.el: * mail/mailheader.el, mail/feedmail.el: * url/url-util.el, url/url-privacy.el, url/url-nfs.el, url/url-misc.el: * url/url-methods.el, url/url-gw.el, url/url-file.el, url/url-expand.el: Dont use CL. * ibuf-ext.el (ibuffer-mark-old-buffers): Use float-time. * eshell/esh-opt.el (eshell-eval-using-options): Quote code with `lambda' rather than with `quote'. (eshell-do-opt): Adjust accordingly. (eshell-process-option): Simplify. * eshell/esh-var.el: * eshell/em-script.el: Require `esh-opt' for eshell-eval-using-options. * emacs-pcase.el (pcase--dontcare-upats, pcase--let*) (pcase--expand, pcase--u1): Rename pcase's internal `dontcare' pattern to `pcase--dontcare'. * emacs-cl.el (labels): Mark obsolete. (cl--letf, letf): Move to cl-lib. (cl--letf*, letf*): Remove. * emacs-cl-lib.el (cl-nth-value): Use defalias. * emacs-cl-macs.el (cl-dolist, cl-dotimes): Add indent rule. (cl-progv): Rewrite. (cl--letf, cl-letf): Move from cl.el. (cl-letf*): New macro. * emacs-cl-extra.el (cl--progv-before, cl--progv-after): Remove.
2012-07-11 19:13:41 -04:00
(cl-defstruct (url-cookie
(:constructor url-cookie-create)
(:copier nil)
(:type vector)
:named)
name value expires localpart domain secure)
2004-04-04 01:21:46 +00:00
(defvar url-cookie-storage nil "Where cookies are stored.")
(defvar url-cookie-secure-storage nil "Where secure cookies are stored.")
(defcustom url-cookie-file nil
"File where cookies are stored on disk."
2004-04-04 01:21:46 +00:00
:type '(choice (const :tag "Default" :value nil) file)
:group 'url-file
:group 'url-cookie)
(defcustom url-cookie-confirmation nil
"If non-nil, confirmation by the user is required to accept HTTP cookies."
2004-04-04 01:21:46 +00:00
:type 'boolean
:group 'url-cookie)
(defcustom url-cookie-multiple-line nil
"If nil, HTTP requests put all cookies for the server on one line.
2004-04-04 01:21:46 +00:00
Some web servers, such as http://www.hotmail.com/, only accept cookies
when they are on one line. This is broken behavior, but just try
telling Microsoft that."
:type 'boolean
:group 'url-cookie)
2004-04-04 01:21:46 +00:00
(defvar url-cookies-changed-since-last-save nil
"Whether the cookies list has changed since the last save operation.")
(defun url-cookie-parse-file (&optional fname)
"Load FNAME, default `url-cookie-file'."
;; It's completely normal for the cookies file not to exist yet.
(load (or fname url-cookie-file) t t))
2004-04-04 01:21:46 +00:00
(defun url-cookie-clean-up (&optional secure)
(let ((var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
new new-cookies)
(dolist (cur (symbol-value var))
(setq new-cookies nil)
(dolist (cur-cookie (cdr cur))
(or (not (url-cookie-p cur-cookie))
(url-cookie-expired-p cur-cookie)
(null (url-cookie-expires cur-cookie))
(setq new-cookies (cons cur-cookie new-cookies))))
(when new-cookies
2004-04-04 01:21:46 +00:00
(setcdr cur new-cookies)
(setq new (cons cur new))))
(set var new)))
(defun url-cookie-write-file (&optional fname)
(when url-cookies-changed-since-last-save
(or fname (setq fname (expand-file-name url-cookie-file)))
(if (condition-case nil
(progn
(url-make-private-file fname)
nil)
(error t))
(message "Error accessing cookie file `%s'" fname)
2004-04-04 01:21:46 +00:00
(url-cookie-clean-up)
(url-cookie-clean-up t)
(with-temp-buffer
2004-04-04 01:21:46 +00:00
(insert ";; Emacs-W3 HTTP cookies file\n"
";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
"(setq url-cookie-storage\n '")
(pp url-cookie-storage (current-buffer))
(insert ")\n(setq url-cookie-secure-storage\n '")
(pp url-cookie-secure-storage (current-buffer))
(insert ")\n")
(insert " \n;; Local Variables:\n"
";; version-control: never\n"
";; no-byte-compile: t\n"
";; End:\n")
(set (make-local-variable 'version-control) 'never)
(write-file fname))
(setq url-cookies-changed-since-last-save nil))))
2004-04-04 01:21:46 +00:00
(defun url-cookie-store (name value &optional expires domain localpart secure)
"Store a cookie."
(let ((storage (if secure url-cookie-secure-storage url-cookie-storage))
tmp found-domain)
;; First, look for a matching domain.
(if (setq found-domain (assoc domain storage))
2004-04-04 01:21:46 +00:00
;; Need to either stick the new cookie in existing domain storage
;; or possibly replace an existing cookie if the names match.
(unless (dolist (cur (setq storage (cdr found-domain)) tmp)
(and (equal localpart (url-cookie-localpart cur))
(equal name (url-cookie-name cur))
(progn
(setf (url-cookie-expires cur) expires)
(setf (url-cookie-value cur) value)
(setq tmp t))))
;; New cookie.
(setcdr found-domain (cons
(url-cookie-create :name name
:value value
:expires expires
:domain domain
:localpart localpart
:secure secure)
(cdr found-domain))))
;; Need to add a new top-level domain.
2004-04-04 01:21:46 +00:00
(setq tmp (url-cookie-create :name name
:value value
:expires expires
:domain domain
:localpart localpart
2004-04-04 01:21:46 +00:00
:secure secure))
(cond (storage
(setcdr storage (cons (list domain tmp) (cdr storage))))
(secure
(setq url-cookie-secure-storage (list (list domain tmp))))
(t
(setq url-cookie-storage (list (list domain tmp))))))))
2004-04-04 01:21:46 +00:00
(defun url-cookie-expired-p (cookie)
"Return non-nil if COOKIE is expired."
(let ((exp (url-cookie-expires cookie)))
(and (> (length exp) 0)
(> (float-time) (float-time (date-to-time exp))))))
2004-04-04 01:21:46 +00:00
(defun url-cookie-retrieve (host &optional localpart secure)
"Retrieve all cookies for a specified HOST and LOCALPART."
2004-04-04 01:21:46 +00:00
(let ((storage (if secure
(append url-cookie-secure-storage url-cookie-storage)
url-cookie-storage))
(case-fold-search t)
cookies retval localpart-match)
(dolist (cur storage)
(setq cookies (cdr cur))
2004-04-04 01:21:46 +00:00
(if (and (car cur)
(string-match
(concat "^.*"
(regexp-quote
;; Remove the dot from wildcard domains
;; before matching.
(if (eq ?. (aref (car cur) 0))
(substring (car cur) 1)
(car cur)))
"$") host))
2004-04-04 01:21:46 +00:00
;; The domains match - a possible hit!
(dolist (cur cookies)
(and (if (and (stringp
(setq localpart-match (url-cookie-localpart cur)))
(stringp localpart))
(string-match (concat "^" (regexp-quote localpart-match))
localpart)
(equal localpart localpart-match))
(not (url-cookie-expired-p cur))
(setq retval (cons cur retval))))))
2004-04-04 01:21:46 +00:00
retval))
(defun url-cookie-generate-header-lines (host localpart secure)
(let ((cookies (url-cookie-retrieve host localpart secure))
retval chunk)
;; Have to sort this for sending most specific cookies first.
2004-04-04 01:21:46 +00:00
(setq cookies (and cookies
(sort cookies
(lambda (x y)
(> (length (url-cookie-localpart x))
(length (url-cookie-localpart y)))))))
(dolist (cur cookies)
(setq chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
2004-04-04 01:21:46 +00:00
retval (if (and url-cookie-multiple-line
(< 80 (+ (length retval) (length chunk) 4)))
(concat retval "\r\nCookie: " chunk)
(if retval
(concat retval "; " chunk)
(concat "Cookie: " chunk)))))
(if retval
(concat retval "\r\n")
"")))
(defcustom url-cookie-trusted-urls nil
"A list of regular expressions matching URLs to always accept cookies from."
2004-04-04 01:21:46 +00:00
:type '(repeat regexp)
:group 'url-cookie)
(defcustom url-cookie-untrusted-urls nil
"A list of regular expressions matching URLs to never accept cookies from."
2004-04-04 01:21:46 +00:00
:type '(repeat regexp)
:group 'url-cookie)
(defun url-cookie-host-can-set-p (host domain)
(let ((last nil)
(case-fold-search t))
(if (string= host domain) ; Apparently netscape lets you do this
t
;; Remove the dot from wildcard domains before matching.
(when (eq ?. (aref domain 0))
(setq domain (substring domain 1)))
(and (url-domsuf-cookie-allowed-p domain)
;; Need to check and make sure the host is actually _in_ the
;; domain it wants to set a cookie for though.
(string-match (concat (regexp-quote domain)
"$") host)))))
2004-04-04 01:21:46 +00:00
(defun url-cookie-handle-set-cookie (str)
(setq url-cookies-changed-since-last-save t)
(let* ((args (url-parse-args str t))
(case-fold-search t)
(secure (and (assoc-string "secure" args t) t))
(domain (or (cdr-safe (assoc-string "domain" args t))
2004-04-04 01:21:46 +00:00
(url-host url-current-object)))
(current-url (url-view-url t))
(trusted url-cookie-trusted-urls)
(untrusted url-cookie-untrusted-urls)
(expires (cdr-safe (assoc-string "expires" args t)))
(localpart (or (cdr-safe (assoc-string "path" args t))
(file-name-directory
(url-filename url-current-object))))
2004-04-04 01:21:46 +00:00
(rest nil))
(dolist (this args)
(or (member (downcase (car this)) '("secure" "domain" "expires" "path"))
(setq rest (cons this rest))))
2004-04-04 01:21:46 +00:00
;; Sometimes we get dates that the timezone package cannot handle very
;; gracefully - take care of this here, instead of in url-cookie-expired-p
;; to speed things up.
(and expires
(string-match
(concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
"\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
expires)
(setq expires (concat (match-string 1 expires) " "
(match-string 2 expires) " "
(match-string 3 expires) " "
(match-string 4 expires) " ["
(match-string 5 expires) "]")))
2004-04-04 01:21:46 +00:00
;; This one is for older Emacs/XEmacs variants that don't
;; understand this format without tenths of a second in it.
;; Wednesday, 30-Dec-2037 16:00:00 GMT
;; - vs -
;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
(and expires
(string-match
"\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
expires)
(setq expires (concat (match-string 1 expires) "-" ; day
(match-string 2 expires) "-" ; month
(match-string 3 expires) " " ; year
(match-string 4 expires) ".00 " ; hour:minutes:seconds
(match-string 6 expires)))) ":" ; timezone
2004-04-04 01:21:46 +00:00
(while (consp trusted)
(if (string-match (car trusted) current-url)
(setq trusted (- (match-end 0) (match-beginning 0)))
(pop trusted)))
(while (consp untrusted)
(if (string-match (car untrusted) current-url)
(setq untrusted (- (match-end 0) (match-beginning 0)))
(pop untrusted)))
(and trusted untrusted
;; Choose the more specific match.
(set (if (> trusted untrusted) 'untrusted 'trusted) nil))
2004-04-04 01:21:46 +00:00
(cond
(untrusted
2011-12-11 21:32:49 -08:00
;; The site was explicitly marked as untrusted by the user.
2004-04-04 01:21:46 +00:00
nil)
((or (eq url-privacy-level 'paranoid)
(and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
;; User never wants cookies.
2004-04-04 01:21:46 +00:00
nil)
((and url-cookie-confirmation
(not trusted)
(save-window-excursion
(with-output-to-temp-buffer "*Cookie Warning*"
(dolist (x rest)
(princ (format "%s - %s" (car x) (cdr x)))))
2004-04-04 01:21:46 +00:00
(prog1
(not (funcall url-confirmation-func
(format "Allow %s to set these cookies? "
(url-host url-current-object))))
(if (get-buffer "*Cookie Warning*")
(kill-buffer "*Cookie Warning*")))))
;; User wants to be asked, and declined.
2004-04-04 01:21:46 +00:00
nil)
((url-cookie-host-can-set-p (url-host url-current-object) domain)
;; Cookie is accepted by the user, and passes our security checks.
(dolist (cur rest)
(url-cookie-store (car cur) (cdr cur) expires domain localpart secure)))
2004-04-04 01:21:46 +00:00
(t
(url-lazy-message "%s tried to set a cookie for domain %s - rejected."
(url-host url-current-object) domain)))))
2004-04-04 01:21:46 +00:00
(defvar url-cookie-timer nil)
(defcustom url-cookie-save-interval 3600
"The number of seconds between automatic saves of cookies.
2004-04-04 01:21:46 +00:00
Default is 1 hour. Note that if you change this variable outside of
the `customize' interface after `url-do-setup' has been run, you need
to run the `url-cookie-setup-save-timer' function manually."
:set #'(lambda (var val)
(set-default var val)
(if (bound-and-true-p url-setup-done)
(url-cookie-setup-save-timer)))
2004-04-04 01:21:46 +00:00
:type 'integer
:group 'url-cookie)
2004-04-04 01:21:46 +00:00
(defun url-cookie-setup-save-timer ()
"Reset the cookie saver timer."
(interactive)
(ignore-errors (cancel-timer url-cookie-timer))
(setq url-cookie-timer nil)
(if url-cookie-save-interval
(setq url-cookie-timer (run-at-time url-cookie-save-interval
url-cookie-save-interval
#'url-cookie-write-file))))
2004-04-04 01:21:46 +00:00
(provide 'url-cookie)
;;; url-cookie.el ends here