From 78e159cb27afd6e3e04c25b8b83c5a364f655272 Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Thu, 4 Jun 2015 01:26:00 +0200 Subject: [PATCH] Add referer-header settng, #712 --- qutebrowser/browser/network/networkmanager.py | 19 ++++++++++++++++++- qutebrowser/config/configdata.py | 4 ++++ qutebrowser/config/configtypes.py | 13 +++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/network/networkmanager.py b/qutebrowser/browser/network/networkmanager.py index a3fc76baf..97e88008c 100644 --- a/qutebrowser/browser/network/networkmanager.py +++ b/qutebrowser/browser/network/networkmanager.py @@ -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'), diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index a3bbe2d48..0e1cf9b9c 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -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."), diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 55d782202..8e9c27697 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -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."""