From d16d9e403a32e579d156a3f6c922db00b939e650 Mon Sep 17 00:00:00 2001 From: toofar Date: Wed, 2 May 2018 20:30:28 +1200 Subject: [PATCH] Make HTTPClient follow redirects by default. Closes #3875 The autoupdator, which uses `qutebrowser.misc.httpclient` has been failing recently because the URL that it hits to check version information is now serving a 301 moved permanently. By default QNetworkRequest doesn't follow redirects so it was getting back a (non-json, despite the request) body pointing to the new location, instead or version information. This changes fixes that by changing HTTPClient to use a QNetworkRequest subclass which follows redirects by default. It lookes like HTTPClient is currently only used in autoupdate.py, version.py, and crashdialog.py so I don't expect any breakage. 5.6-5.8 Only had a boolean setting available which allows redirects, but not from the https scheme to http, 5.9 introduces a more nuanced setting. I have tested locally on 5.7.1 and 5.10. --- qutebrowser/misc/httpclient.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/qutebrowser/misc/httpclient.py b/qutebrowser/misc/httpclient.py index b0b41af76..0b162befd 100644 --- a/qutebrowser/misc/httpclient.py +++ b/qutebrowser/misc/httpclient.py @@ -28,6 +28,21 @@ from PyQt5.QtNetwork import (QNetworkAccessManager, QNetworkRequest, QNetworkReply) +class HTTPRequest(QNetworkRequest): + """A QNetworkRquest that follows (secure) redirects by default.""" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + try: + self.setAttribute(QNetworkRequest.RedirectPolicyAttribute, + QNetworkRequest.NoLessSafeRedirectPolicy) + except AttributeError: + # RedirectPolicyAttribute was introduced in 5.9 to replace + # FollowRedirectsAttribute. + self.setAttribute(QNetworkRequest.FollowRedirectsAttribute, + True) + + class HTTPClient(QObject): """An HTTP client based on QNetworkAccessManager. @@ -63,7 +78,7 @@ class HTTPClient(QObject): if data is None: data = {} encoded_data = urllib.parse.urlencode(data).encode('utf-8') - request = QNetworkRequest(url) + request = HTTPRequest(url) request.setHeader(QNetworkRequest.ContentTypeHeader, 'application/x-www-form-urlencoded;charset=utf-8') reply = self._nam.post(request, encoded_data) @@ -77,7 +92,7 @@ class HTTPClient(QObject): Args: url: The URL to access, as QUrl. """ - request = QNetworkRequest(url) + request = HTTPRequest(url) reply = self._nam.get(request) self._handle_reply(reply)