Repository: qutebrowser/qutebrowser

Issue:
Checking for new version fails
After doing a crash report:

> There was an error while getting the newest version: Invalid JSON received in reply: Expecting value: line 1 column 1 (char 0)!. Please check for a new version on qutebrowser.org by yourself.

Probably something changed on PyPI?


Discussion:
Well https://pypi.python.org/pypi/qutebrowser/json is getting a 301 moved permanently to https://pypi.org/pypi/qutebrowser/json. I'm pretty sure QNetworkRequest defaults to not following redirects.
https://warehouse.readthedocs.io/api-reference/integration-guide/#migrating-to-the-new-pypi might help.
Yeah, adjusting the URL and ideally telling the QNetworkAccessManager to follow redirects (not sure if that was supported on 5.7.1 already though!) should be all that's needed.

Resolving patch:
diff --git a/qutebrowser/misc/autoupdate.py b/qutebrowser/misc/autoupdate.py
--- a/qutebrowser/misc/autoupdate.py
+++ b/qutebrowser/misc/autoupdate.py
@@ -45,7 +45,7 @@ class PyPIVersionClient(QObject):
                arg: The error message, as string.
     """
 
-    API_URL = 'https://pypi.python.org/pypi/{}/json'
+    API_URL = 'https://pypi.org/pypi/{}/json'
     success = pyqtSignal(str)
     error = pyqtSignal(str)
 
diff --git a/qutebrowser/misc/httpclient.py b/qutebrowser/misc/httpclient.py
--- a/qutebrowser/misc/httpclient.py
+++ b/qutebrowser/misc/httpclient.py
@@ -28,6 +28,21 @@
                              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 @@ def post(self, url, data=None):
         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 @@ def get(self, url):
         Args:
             url: The URL to access, as QUrl.
         """
-        request = QNetworkRequest(url)
+        request = HTTPRequest(url)
         reply = self._nam.get(request)
         self._handle_reply(reply)
 


Test patch:
diff --git a/tests/unit/misc/test_autoupdate.py b/tests/unit/misc/test_autoupdate.py
--- a/tests/unit/misc/test_autoupdate.py
+++ b/tests/unit/misc/test_autoupdate.py
@@ -67,7 +67,7 @@ def test_get_version_success(qtbot):
         with qtbot.waitSignal(client.success):
             client.get_version('test')
 
-    assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json')
+    assert http_stub.url == QUrl(client.API_URL.format('test'))
 
 
 def test_get_version_error(qtbot):