aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/net
diff options
context:
space:
mode:
authorLars Ingebrigtsen2019-09-24 17:48:35 +0200
committerLars Ingebrigtsen2019-09-24 17:48:41 +0200
commitea5c79f657a9e2826073896ea00e6000ccc04a8d (patch)
treea0b3b5d4f427f4d221c8d3bfe97fac38512b8338 /lisp/net
parent9dcdb1384df51a568af5ec35c9f0a762d3cf205b (diff)
downloademacs-ea5c79f657a9e2826073896ea00e6000ccc04a8d.tar.gz
emacs-ea5c79f657a9e2826073896ea00e6000ccc04a8d.zip
Allow controlling when to send cookies when retrieving images in shr
* lisp/net/shr.el (shr--use-cookies-p): New function. (shr-tag-img): Use it. (shr-cookie-policy): New variable. (shr-save-contents): Use cookies. * doc/misc/eww.texi (Advanced): Document it.
Diffstat (limited to 'lisp/net')
-rw-r--r--lisp/net/shr.el45
1 files changed, 40 insertions, 5 deletions
diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index 2e4f7fa5c61..63988d01c88 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -39,6 +39,7 @@
39(require 'svg) 39(require 'svg)
40(require 'image) 40(require 'image)
41(require 'puny) 41(require 'puny)
42(require 'url-cookie)
42(require 'text-property-search) 43(require 'text-property-search)
43 44
44(defgroup shr nil 45(defgroup shr nil
@@ -111,6 +112,16 @@ Alternative suggestions are:
111 :version "24.4" 112 :version "24.4"
112 :type 'string) 113 :type 'string)
113 114
115(defcustom shr-cookie-policy 'same-origin
116 "When to use cookies when fetching dependent data like images.
117If t, always use cookies. If nil, never use cookies. If
118`same-origin', use cookies if the dependent data comes from the
119same domain as the main data."
120 :type '(choice (const :tag "Always use cookies" t)
121 (const :tag "Never use cookies" nil)
122 (const :tag "Use cookies for same domain" same-origin))
123 :version "27.1")
124
114(define-obsolete-variable-alias 'shr-external-browser 125(define-obsolete-variable-alias 'shr-external-browser
115 'browse-url-secondary-browser-function "27.1") 126 'browse-url-secondary-browser-function "27.1")
116 127
@@ -333,7 +344,7 @@ called."
333 ;; Remove common tracking junk from the URL. 344 ;; Remove common tracking junk from the URL.
334 (funcall cont (replace-regexp-in-string 345 (funcall cont (replace-regexp-in-string
335 ".utm_.*" "" destination))))) 346 ".utm_.*" "" destination)))))
336 nil t)) 347 nil t t))
337 348
338(defun shr-probe-and-copy-url (url) 349(defun shr-probe-and-copy-url (url)
339 "Copy the URL under point to the kill ring. 350 "Copy the URL under point to the kill ring.
@@ -427,7 +438,7 @@ the URL of the image to the kill buffer instead."
427 (message "Inserting %s..." url) 438 (message "Inserting %s..." url)
428 (url-retrieve url 'shr-image-fetched 439 (url-retrieve url 'shr-image-fetched
429 (list (current-buffer) (1- (point)) (point-marker)) 440 (list (current-buffer) (1- (point)) (point-marker))
430 t t)))) 441 t))))
431 442
432(defun shr-zoom-image () 443(defun shr-zoom-image ()
433 "Toggle the image size. 444 "Toggle the image size.
@@ -985,8 +996,7 @@ the mouse click event."
985 (if (not url) 996 (if (not url)
986 (message "No link under point") 997 (message "No link under point")
987 (url-retrieve (shr-encode-url url) 998 (url-retrieve (shr-encode-url url)
988 'shr-store-contents (list url directory) 999 'shr-store-contents (list url directory)))))
989 nil t))))
990 1000
991(defun shr-store-contents (status url directory) 1001(defun shr-store-contents (status url directory)
992 (unless (plist-get status :error) 1002 (unless (plist-get status :error)
@@ -1658,7 +1668,8 @@ The preference is a float determined from `shr-prefer-media-type'."
1658 (shr-encode-url url) 'shr-image-fetched 1668 (shr-encode-url url) 'shr-image-fetched
1659 (list (current-buffer) start (set-marker (make-marker) (point)) 1669 (list (current-buffer) start (set-marker (make-marker) (point))
1660 (list :width width :height height)) 1670 (list :width width :height height))
1661 t t))) 1671 t
1672 (not (shr--use-cookies-p url shr-base)))))
1662 (when (zerop shr-table-depth) ;; We are not in a table. 1673 (when (zerop shr-table-depth) ;; We are not in a table.
1663 (put-text-property start (point) 'keymap shr-image-map) 1674 (put-text-property start (point) 'keymap shr-image-map)
1664 (put-text-property start (point) 'shr-alt alt) 1675 (put-text-property start (point) 'shr-alt alt)
@@ -1669,6 +1680,30 @@ The preference is a float determined from `shr-prefer-media-type'."
1669 (shr-fill-text 1680 (shr-fill-text
1670 (or (dom-attr dom 'title) alt)))))))) 1681 (or (dom-attr dom 'title) alt))))))))
1671 1682
1683(defun shr--use-cookies-p (url base)
1684 "Say whether to use cookies when fetching URL (typically an image).
1685BASE is the URL of the HTML being rendered."
1686 (cond
1687 ((null base)
1688 ;; Disallow cookies if we don't know what the base is.
1689 nil)
1690 ((eq shr-cookie-policy 'same-origin)
1691 (let ((url-host (url-host (url-generic-parse-url url)))
1692 (base-host (split-string
1693 (url-host (url-generic-parse-url (car base)))
1694 "\\.")))
1695 ;; We allow cookies if it's for any of the sibling domains (that
1696 ;; we're allowed to set cookies for). Determine that by going
1697 ;; "upwards" in the base domain name.
1698 (cl-loop while base-host
1699 when (url-cookie-host-can-set-p
1700 url-host (mapconcat #'identity base-host "."))
1701 return t
1702 do (pop base-host)
1703 finally (return nil))))
1704 (t
1705 shr-cookie-policy)))
1706
1672(defun shr--preferred-image (dom) 1707(defun shr--preferred-image (dom)
1673 (let ((srcset (dom-attr dom 'srcset)) 1708 (let ((srcset (dom-attr dom 'srcset))
1674 (frame-width (frame-pixel-width)) 1709 (frame-width (frame-pixel-width))