Skip to content

Commit 3da5ab6

Browse files
committedDec 11, 2017
[Plugin installer] Properly handle 301 Redirection
1 parent 678758c commit 3da5ab6

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed
 

‎python/pyplugin_installer/installer_data.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,11 @@ def load(self):
323323
settings.endGroup()
324324

325325
# ----------------------------------------- #
326-
def requestFetching(self, key):
326+
def requestFetching(self, key, url=None, redirectionCounter=0):
327327
""" start fetching the repository given by key """
328328
self.mRepositories[key]["state"] = 1
329-
url = QUrl(self.mRepositories[key]["url"] + self.urlParams())
329+
if not url:
330+
url = QUrl(self.mRepositories[key]["url"] + self.urlParams())
330331
# v=str(Qgis.QGIS_VERSION_INT)
331332
# url.addQueryItem('qgis', '.'.join([str(int(s)) for s in [v[0], v[1:3]]]) ) # don't include the bugfix version!
332333

@@ -345,6 +346,7 @@ def requestFetching(self, key):
345346
self.mRepositories[key]["QRequest"].setAttribute(QNetworkRequest.User, key)
346347
self.mRepositories[key]["xmlData"] = QgsNetworkAccessManager.instance().get(self.mRepositories[key]["QRequest"])
347348
self.mRepositories[key]["xmlData"].setProperty('reposName', key)
349+
self.mRepositories[key]["xmlData"].setProperty('redirectionCounter', redirectionCounter)
348350
self.mRepositories[key]["xmlData"].downloadProgress.connect(self.mRepositories[key]["Relay"].dataReadProgress)
349351
self.mRepositories[key]["xmlData"].finished.connect(self.xmlDownloaded)
350352

@@ -373,6 +375,19 @@ def xmlDownloaded(self):
373375
self.mRepositories[reposName]["error"] = reply.errorString()
374376
if reply.error() == QNetworkReply.OperationCanceledError:
375377
self.mRepositories[reposName]["error"] += "\n\n" + QCoreApplication.translate("QgsPluginInstaller", "If you haven't canceled the download manually, it was most likely caused by a timeout. In this case consider increasing the connection timeout value in QGIS options window.")
378+
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 301:
379+
redirectionUrl = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
380+
if redirectionUrl.isRelative():
381+
redirectionUrl = reply.url().resolved(redirectionUrl)
382+
redirectionCounter = reply.property('redirectionCounter') + 1
383+
if redirectionCounter > 4:
384+
self.mRepositories[reposName]["state"] = 3
385+
self.mRepositories[reposName]["error"] = QCoreApplication.translate("QgsPluginInstaller", "Too many redirections")
386+
else:
387+
# Fire a new request and exit immediately in order to quietly destroy the old one
388+
self.requestFetching(reposName, redirectionUrl, redirectionCounter)
389+
reply.deleteLater()
390+
return
376391
else:
377392
reposXML = QDomDocument()
378393
content = reply.readAll()

‎python/pyplugin_installer/qgsplugininstallerinstallingdialog.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ def __init__(self, parent, plugin):
5050
self.labelName.setText(plugin["name"])
5151
self.buttonBox.clicked.connect(self.abort)
5252

53-
url = QUrl(plugin["download_url"])
53+
self.url = QUrl(plugin["download_url"])
54+
self.redirectionCounter = 0
5455

5556
fileName = plugin["filename"]
5657
tmpDir = QDir.tempPath()
5758
tmpPath = QDir.cleanPath(tmpDir + "/" + fileName)
5859
self.file = QFile(tmpPath)
5960

60-
self.request = QNetworkRequest(url)
61-
authcfg = repositories.all()[plugin["zip_repository"]]["authcfg"]
61+
self.requestDownloading()
62+
63+
def requestDownloading(self):
64+
self.request = QNetworkRequest(self.url)
65+
authcfg = repositories.all()[self.plugin["zip_repository"]]["authcfg"]
6266
if authcfg and isinstance(authcfg, str):
6367
if not QgsApplication.authManager().updateNetworkRequest(
6468
self.request, authcfg.strip()):
@@ -106,6 +110,23 @@ def requestFinished(self):
106110
self.reject()
107111
reply.deleteLater()
108112
return
113+
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 301:
114+
redirectionUrl = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
115+
self.redirectionCounter += 1
116+
if self.redirectionCounter > 4:
117+
self.mResult = QCoreApplication.translate("QgsPluginInstaller", "Too many redirections")
118+
self.reject()
119+
reply.deleteLater()
120+
return
121+
else:
122+
if redirectionUrl.isRelative():
123+
redirectionUrl = reply.url().resolved(redirectionUrl)
124+
# Fire a new request and exit immediately in order to quietly destroy the old one
125+
self.url = redirectionUrl
126+
self.requestDownloading()
127+
reply.deleteLater()
128+
return
129+
109130
self.file.open(QFile.WriteOnly)
110131
self.file.write(reply.readAll())
111132
self.file.close()

0 commit comments

Comments
 (0)
Please sign in to comment.