Add referer-header settng, #712

This commit is contained in:
Martin Tournoij 2015-06-04 01:26:00 +02:00
parent e780efb3d9
commit 78e159cb27
3 changed files with 35 additions and 1 deletions

View File

@ -22,7 +22,7 @@
import collections
from PyQt5.QtCore import (pyqtSlot, pyqtSignal, PYQT_VERSION, QCoreApplication,
QUrl)
QUrl, QByteArray)
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkReply, QSslError
try:
@ -358,6 +358,23 @@ class NetworkManager(QNetworkAccessManager):
dnt = '0'.encode('ascii')
req.setRawHeader('DNT'.encode('ascii'), dnt)
req.setRawHeader('X-Do-Not-Track'.encode('ascii'), dnt)
def same_domain():
tabbed_browser = objreg.get('tabbed-browser', scope='window', window=self._win_id)
view = tabbed_browser.currentWidget()
print(req.url().host() == view.url().host(), repr(view.url().host()), repr(req.url().host()))
# TODO: We probably want to allow headers if we're on test.com and
# doing a request to www.test.com? Or maybe a new settings value for
# this?
return req.url().host() == view.url().host()
if (config.get('network', 'referer-header') == 'never' or
(config.get('network', 'referer-header') == 'same-domain' and
same_domain())):
# Note: using ''.encode('ascii') sends a header with no value,
# instead of no header at all
req.setRawHeader('Referer'.encode('ascii'), QByteArray())
accept_language = config.get('network', 'accept-language')
if accept_language is not None:
req.setRawHeader('Accept-Language'.encode('ascii'),

View File

@ -321,6 +321,10 @@ def data(readonly=False):
SettingValue(typ.String(none_ok=True), 'en-US,en'),
"Value to send in the `accept-language` header."),
('referer-header',
SettingValue(typ.Referer(), 'same-domain'),
"Send the Referer header"),
('user-agent',
SettingValue(typ.UserAgent(none_ok=True), ''),
"User agent to send. Empty to send the default."),

View File

@ -1459,6 +1459,19 @@ class DownloadPathSuggestion(BaseType):
('both', "Show download path and filename."))
class Referer(BaseType):
"""Send the Referer header."""
valid_values = ValidValues(('always', "Always send."),
('never', "Never send; this is not recommended,"
" as some sites may break."),
('same-domain', "Only send for the same domain,"
" this will still protect your privacy, but"
" shouldn't break any sites."))
class UserAgent(BaseType):
"""The user agent to use."""