Support URL patterns for content.headers settings

See #3636
This commit is contained in:
Florian Bruhin 2018-06-24 19:54:24 +02:00
parent a02c25dfb1
commit e6e844b039
7 changed files with 39 additions and 12 deletions

View File

@ -55,6 +55,11 @@ Added
Changed
~~~~~~~
- The following settings now support URL patterns:
- content.headers.do_not_track
- content.headers.custom
- content.headers.accept_language
- content.headers.user_agent
- The Windows/macOS releases now bundle Qt 5.11.1 which is based on
Chromium 65.0.3325.151 with security fixes up to Chromium 67.0.3396.87.
- New short flags for commandline arguments: `-B` and `-T` for `--basedir` and

View File

@ -1622,6 +1622,9 @@ Default: +pass:[ask]+
[[content.headers.accept_language]]
=== content.headers.accept_language
Value to send in the `Accept-Language` header.
Note that the value read from JavaScript is always the global value.
This setting supports URL patterns.
Type: <<types,String>>
@ -1631,6 +1634,8 @@ Default: +pass:[en-US,en]+
=== content.headers.custom
Custom headers for qutebrowser HTTP requests.
This setting supports URL patterns.
Type: <<types,Dict>>
Default: empty
@ -1640,6 +1645,8 @@ Default: empty
Value to send in the `DNT` header.
When this is set to true, qutebrowser asks websites to not track your identity. If set to null, the DNT header is not sent at all.
This setting supports URL patterns.
Type: <<types,Bool>>
Default: +pass:[true]+
@ -1664,6 +1671,9 @@ This setting is only available with the QtWebKit backend.
[[content.headers.user_agent]]
=== content.headers.user_agent
User agent to send. Unset to send the default.
Note that the value read from JavaScript is always the global value.
This setting supports URL patterns.
Type: <<types,String>>

View File

@ -34,21 +34,21 @@ class CallSuper(Exception):
"""Raised when the caller should call the superclass instead."""
def custom_headers():
def custom_headers(url):
"""Get the combined custom headers."""
headers = {}
dnt_config = config.val.content.headers.do_not_track
dnt_config = config.instance.get('content.headers.do_not_track', url=url)
if dnt_config is not None:
dnt = b'1' if dnt_config else b'0'
headers[b'DNT'] = dnt
headers[b'X-Do-Not-Track'] = dnt
conf_headers = config.val.content.headers.custom
conf_headers = config.instance.get('content.headers.custom', url=url)
for header, value in conf_headers.items():
headers[header.encode('ascii')] = value.encode('ascii')
accept_language = config.val.content.headers.accept_language
accept_language = config.instance.get('content.headers.accept_language', url=url)
if accept_language is not None:
headers[b'Accept-Language'] = accept_language.encode('ascii')

View File

@ -68,15 +68,17 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor):
info.firstPartyUrl().toDisplayString(),
resource_type, navigation_type))
url = info.requestUrl()
# FIXME:qtwebengine only block ads for NavigationTypeOther?
if self._host_blocker.is_blocked(info.requestUrl()):
if self._host_blocker.is_blocked(url):
log.webview.info("Request to {} blocked by host blocker.".format(
info.requestUrl().host()))
url.host()))
info.block(True)
for header, value in shared.custom_headers():
for header, value in shared.custom_headers(url=url):
info.setHttpHeader(header, value)
user_agent = config.val.content.headers.user_agent
user_agent = config.instance.get('content.headers.user_agent', url=url)
if user_agent is not None:
info.setHttpHeader(b'User-Agent', user_agent.encode('ascii'))

View File

@ -380,7 +380,7 @@ class NetworkManager(QNetworkAccessManager):
result.setParent(self)
return result
for header, value in shared.custom_headers():
for header, value in shared.custom_headers(url=req.url()):
req.setRawHeader(header, value)
host_blocker = objreg.get('host-blocker')

View File

@ -415,7 +415,7 @@ class BrowserPage(QWebPage):
def userAgentForUrl(self, url):
"""Override QWebPage::userAgentForUrl to customize the user agent."""
ua = config.val.content.headers.user_agent
ua = config.instance.get('content.headers.user_agent', url=url)
if ua is None:
return super().userAgentForUrl(url)
else:

View File

@ -356,8 +356,12 @@ content.headers.accept_language:
type:
name: String
none_ok: true
supports_pattern: true
default: en-US,en
desc: Value to send in the `Accept-Language` header.
desc: >-
Value to send in the `Accept-Language` header.
Note that the value read from JavaScript is always the global value.
content.headers.custom:
default: {}
@ -370,6 +374,7 @@ content.headers.custom:
name: String
encoding: ascii
none_ok: true
supports_pattern: true
desc: Custom headers for qutebrowser HTTP requests.
content.headers.do_not_track:
@ -377,6 +382,7 @@ content.headers.do_not_track:
name: Bool
none_ok: true
default: true
supports_pattern: true
desc: >-
Value to send in the `DNT` header.
@ -451,7 +457,11 @@ content.headers.user_agent:
Gecko"
- IE 11.0 for Desktop Win7 64-bit
desc: User agent to send. Unset to send the default.
supports_pattern: true
desc: >-
User agent to send. Unset to send the default.
Note that the value read from JavaScript is always the global value.
content.host_blocking.enabled:
default: true