[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

Re: Changing a user agent string [PATCH]



On 2018-01-18 13:31, Katsumi Yamaoka wrote:
> In [emacs-w3m:12878]
> On Wed, 17 Jan 2018 22:17:36 -0500, Boruch Baum wrote:
> > OTOH, setting the variable as `buffer-local' seems straightforward. The
> > function, when called directly interactively would prompt the user to
> > choose whether to apply the setting globally, or just for the current
> > buffer. If you're certain you want that, let me know.
>
> I think the `buffer-local' is good enough and easy to implement.

It turns out to not be straightforward at all because the code generates
temporary work buffers to build the GET headers, so making the variable
buffer local doesn't help.

> >> Another idea is to introduce an alist that holds a list of a url-
> >> regexp and a user-agent pairs.  It overrides the default user-agent
> >> if a regexp matches the url to visit.
>
> > This is a very nice idea. A complexity would be that there would need to
> > be a way to manually over-ride the alist ...
>
> It would not be wasteful even if such one exists together with
> the manually changing feature.

Patch attached.

> > For me, the current value of `w3m-add-referer` is:
> >   ("\\`http:" . "\\`http://\\(?:localhost\\|127\\.0\\.0\\.1\\)/")
> > The change would be?
> >   ("\\`https?:" . "\\`https?://\\(?:localhost\\|127\\.0\\.0\\.1\\)/")
> Exactly.  Thanks.

Attached in same patch.

-- 
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
Index: ChangeLog
===================================================================
RCS file: /storage/cvsroot/emacs-w3m/ChangeLog,v
retrieving revision 1.3640
diff -u -r1.3640 ChangeLog
--- ChangeLog	18 Jan 2018 04:00:22 -0000	1.3640
+++ ChangeLog	18 Jan 2018 16:35:17 -0000
@@ -1,3 +1,9 @@
+2018-01-18  Boruch Baum  <boruch_baum@xxxxxxx>
+
+	* w3m.el (w3m-add-referer): Update for https support.
+	(w3m-user-agent-site-specific-alist, w3m-user-agent-site-specific): New feature.
+	(w3m-header-arguments, w3m-request-arguments): Integrate new feature.
+
 2018-01-17  Boruch Baum  <boruch_baum@xxxxxxx>
 
 	* w3m.el (w3m-user-agent-change): New feature.
Index: w3m.el
===================================================================
RCS file: /storage/cvsroot/emacs-w3m/w3m.el,v
retrieving revision 1.1697
diff -u -r1.1697 w3m.el
--- w3m.el	18 Jan 2018 04:51:17 -0000	1.1697
+++ w3m.el	18 Jan 2018 16:35:30 -0000
@@ -354,6 +354,14 @@
   :type '(repeat (cons (string :tag "Short Description")
 		       (string :tag "User Agent string"))))
 
+(defcustom w3m-user-agent-site-specific-alist
+  "An alist of user-agent strings to be used for specific URLs.
+Each entry should be a cons of a regexp for the URLs to be
+covered by the rule, and a user-agent string to be used."
+  :group 'w3m
+  :type '(repeat (cons (string :tag "URL regexp")
+		       (string :tag "User Agent string"))))
+
 (defcustom w3m-language
   (if (and (boundp 'current-language-environment)
 	   ;; In XEmacs 21.5 it may be the one like "Japanese (UTF-8)".
@@ -1848,7 +1856,7 @@
 (defcustom w3m-add-referer
   (if (boundp 'w3m-add-referer-regexps)
       (symbol-value 'w3m-add-referer-regexps)
-    (cons "\\`http:" "\\`http://\\(?:localhost\\|127\\.0\\.0\\.1\\)/"))
+    (cons "\\`https?:" "\\`https?://\\(?:localhost\\|127\\.0\\.0\\.1\\)/"))
   "*Rule of sending referers.
 There are five choices as the valid values of this option.
 
@@ -1871,8 +1879,8 @@
 follows:
 
 \(setq w3m-add-referer
-      '(\"\\\\`http:\"
-	. \"\\\\`http://\\\\(?:[^./]+\\\\.\\\\)*example\\\\.net/\")\)
+      '(\"\\\\`https?:\"
+	. \"\\\\`https?://\\\\(?:[^./]+\\\\.\\\\)*example\\\\.net/\")\)
 "
   :group 'w3m
   :type '(choice
@@ -5563,7 +5571,7 @@
 	       (null content-type))
 	  (append
 	   (list "-header" (concat "User-Agent:"
-				   (if w3m-add-user-agent w3m-user-agent "?")))
+				   (if w3m-add-user-agent (or (w3m-user-agent-site-specific url) w3m-user-agent) "?")))
 	   (when (w3m-add-referer-p url referer)
 	     (list "-header" (concat "Referer: " referer)))
 	   (when w3m-accept-languages
@@ -5605,7 +5613,7 @@
 	args)
     (setq args (nconc args
       (list "-o" (concat "user_agent="
-			 (if w3m-add-user-agent w3m-user-agent "?")))))
+			 (if w3m-add-user-agent (or (w3m-user-agent-site-specific url) w3m-user-agent) "?")))))
     (when cookie
       (setq args (nconc args
 			(list "-header" (concat "Cookie: " cookie)))))
@@ -10031,6 +10039,20 @@
   (interactive)
   (w3m-goto-url-new-session "about:blank"))
 
+(defun w3m-user-agent-site-specific (url)
+  "Return a site-specific user-agent string.
+
+Compares URL against the regexps of
+`w3m-user-agent-site-specific-alist' and returns the
+corresponding user-agent string of the first match, or NIL if
+none match."
+  (let ((check-list w3m-user-agent-site-specific-alist) result entry)
+    (while (and (not result) (setq entry (pop check-list)))
+      (message "%s" entry)
+      (when (string-match (car entry) url)
+        (setq result (cdr entry))))
+    result))
+
 (defun w3m-user-agent-change (&optional ua-string)
   "Return a user-agent string.
 
@@ -10052,6 +10074,14 @@
   (when (string-equal ua-string "")
     (setq ua-string nil))
   (when (called-interactively-p 'interactive)
+;   Does not work because we use temporary work buffers when
+;   constructing the GET header (eg. `w3m-header-arguments')
+;   (if (y-or-n-p "For this buffer only? ")
+;     (progn
+;       (make-local-variable 'w3m-add-user-agent)
+;       (make-local-variable 'w3m-user-agent))
+;    (kill-local-variable 'w3m-add-user-agent)
+;    (kill-local-variable 'w3m-user-agent))
     (if (not ua-string)
 	(setq w3m-add-user-agent nil)
       (setq w3m-add-user-agent t)