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 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 - 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. 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 - 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]]
=== content.headers.accept_language === content.headers.accept_language
Value to send in the `Accept-Language` header. 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>> Type: <<types,String>>
@ -1631,6 +1634,8 @@ Default: +pass:[en-US,en]+
=== content.headers.custom === content.headers.custom
Custom headers for qutebrowser HTTP requests. Custom headers for qutebrowser HTTP requests.
This setting supports URL patterns.
Type: <<types,Dict>> Type: <<types,Dict>>
Default: empty Default: empty
@ -1640,6 +1645,8 @@ Default: empty
Value to send in the `DNT` header. 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. 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>> Type: <<types,Bool>>
Default: +pass:[true]+ Default: +pass:[true]+
@ -1664,6 +1671,9 @@ This setting is only available with the QtWebKit backend.
[[content.headers.user_agent]] [[content.headers.user_agent]]
=== content.headers.user_agent === content.headers.user_agent
User agent to send. Unset to send the default. 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>> Type: <<types,String>>

View File

@ -34,21 +34,21 @@ class CallSuper(Exception):
"""Raised when the caller should call the superclass instead.""" """Raised when the caller should call the superclass instead."""
def custom_headers(): def custom_headers(url):
"""Get the combined custom headers.""" """Get the combined custom headers."""
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: if dnt_config is not None:
dnt = b'1' if dnt_config else b'0' dnt = b'1' if dnt_config else b'0'
headers[b'DNT'] = dnt headers[b'DNT'] = dnt
headers[b'X-Do-Not-Track'] = 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(): for header, value in conf_headers.items():
headers[header.encode('ascii')] = value.encode('ascii') 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: if accept_language is not None:
headers[b'Accept-Language'] = accept_language.encode('ascii') headers[b'Accept-Language'] = accept_language.encode('ascii')

View File

@ -68,15 +68,17 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor):
info.firstPartyUrl().toDisplayString(), info.firstPartyUrl().toDisplayString(),
resource_type, navigation_type)) resource_type, navigation_type))
url = info.requestUrl()
# FIXME:qtwebengine only block ads for NavigationTypeOther? # 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( log.webview.info("Request to {} blocked by host blocker.".format(
info.requestUrl().host())) url.host()))
info.block(True) info.block(True)
for header, value in shared.custom_headers(): for header, value in shared.custom_headers(url=url):
info.setHttpHeader(header, value) 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: if user_agent is not None:
info.setHttpHeader(b'User-Agent', user_agent.encode('ascii')) info.setHttpHeader(b'User-Agent', user_agent.encode('ascii'))

View File

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

View File

@ -415,7 +415,7 @@ class BrowserPage(QWebPage):
def userAgentForUrl(self, url): def userAgentForUrl(self, url):
"""Override QWebPage::userAgentForUrl to customize the user agent.""" """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: if ua is None:
return super().userAgentForUrl(url) return super().userAgentForUrl(url)
else: else:

View File

@ -356,8 +356,12 @@ content.headers.accept_language:
type: type:
name: String name: String
none_ok: true none_ok: true
supports_pattern: true
default: en-US,en 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: content.headers.custom:
default: {} default: {}
@ -370,6 +374,7 @@ content.headers.custom:
name: String name: String
encoding: ascii encoding: ascii
none_ok: true none_ok: true
supports_pattern: true
desc: Custom headers for qutebrowser HTTP requests. desc: Custom headers for qutebrowser HTTP requests.
content.headers.do_not_track: content.headers.do_not_track:
@ -377,6 +382,7 @@ content.headers.do_not_track:
name: Bool name: Bool
none_ok: true none_ok: true
default: true default: true
supports_pattern: true
desc: >- desc: >-
Value to send in the `DNT` header. Value to send in the `DNT` header.
@ -451,7 +457,11 @@ content.headers.user_agent:
Gecko" Gecko"
- IE 11.0 for Desktop Win7 64-bit - 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: content.host_blocking.enabled:
default: true default: true