diff --git a/lisp/url/url-http.el b/lisp/url/url-http.el index 5832e92..f775e72 100644 --- a/lisp/url/url-http.el +++ b/lisp/url/url-http.el @@ -211,15 +211,33 @@ url-http-find-free-connection (if connection (url-http-mark-connection-as-busy host port connection)))) +(defun url-http--user-agent-default-string () + "Compute a default User-Agent string based on `url-privacy-level'." + (let ((package-info (when url-package-name + (format "%s/%s" url-package-name url-package-version))) + (emacs-info (unless (and (listp url-privacy-level) + (memq 'emacs url-privacy-level)) + (format "GNUEmacs/%s" emacs-version))) + (os-info (unless (and (listp url-privacy-level) + (memq 'os url-privacy-level)) + (format "(%s; %s)" url-system-type url-os-type))) + (url-info (format "URL/%s" url-version))) + (string-join (delq nil (list package-info url-info + emacs-info os-info)) + " "))) + ;; Building an HTTP request (defun url-http-user-agent-string () (if (or (eq url-privacy-level 'paranoid) (and (listp url-privacy-level) (memq 'agent url-privacy-level))) "" - (if (functionp url-user-agent) - (funcall url-user-agent) - url-user-agent))) + (format "User-Agent: %s\r\n" + (string-trim + (cond + ((functionp url-user-agent) (funcall url-user-agent)) + ((stringp url-user-agent) url-user-agent) + (t (url-http--user-agent-default-string))))))) (defun url-http-create-request (&optional ref-url) "Create an HTTP request for `url-http-target-url', referred to by REF-URL." diff --git a/lisp/url/url-vars.el b/lisp/url/url-vars.el index 960a04a..48a4cbe 100644 --- a/lisp/url/url-vars.el +++ b/lisp/url/url-vars.el @@ -116,6 +116,7 @@ url-privacy-level Valid symbols are: email -- the email address os -- the operating system info +emacs -- the version of Emacs lastloc -- the last location agent -- do not send the User-Agent string cookies -- never accept HTTP cookies @@ -143,6 +144,7 @@ url-privacy-level (checklist :tag "Custom" (const :tag "Email address" :value email) (const :tag "Operating system" :value os) + (const :tag "Emacs version" :value emacs) (const :tag "Last location" :value lastloc) (const :tag "Browser identification" :value agent) (const :tag "No cookies" :value cookie))) @@ -357,13 +359,11 @@ url-gateway-method (const :tag "Direct connection" :value native)) :group 'url-hairy) -(defcustom url-user-agent (format "User-Agent: %sURL/%s\r\n" - (if url-package-name - (concat url-package-name "/" - url-package-version " ") - "") url-version) - "User Agent used by the URL package for HTTP/HTTPS requests -Should be a string or a function of no arguments returning a string." +(defcustom url-user-agent nil + "User Agent used by the URL package for HTTP/HTTPS requests. +Should be a string not including the \"User-Agent:\", or a +function of no arguments returning a such string. +If nil, the value is calculated based on `url-privacy-level'." :type '(choice (string :tag "A static User-Agent string") (function :tag "Call a function to get the User-Agent string")) :version "25.1"