Skip to content
Permalink
Browse files

[Plugin installer] Properly handle 301 Redirection

  • Loading branch information
borysiasty committed Dec 11, 2017
1 parent 678758c commit 3da5ab69965289b457b7db772f7033f705ce9b66
@@ -323,10 +323,11 @@ def load(self):
settings.endGroup()

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

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

@@ -373,6 +375,19 @@ def xmlDownloaded(self):
self.mRepositories[reposName]["error"] = reply.errorString()
if reply.error() == QNetworkReply.OperationCanceledError:
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.")
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 301:
redirectionUrl = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
if redirectionUrl.isRelative():
redirectionUrl = reply.url().resolved(redirectionUrl)
redirectionCounter = reply.property('redirectionCounter') + 1
if redirectionCounter > 4:
self.mRepositories[reposName]["state"] = 3
self.mRepositories[reposName]["error"] = QCoreApplication.translate("QgsPluginInstaller", "Too many redirections")
else:
# Fire a new request and exit immediately in order to quietly destroy the old one
self.requestFetching(reposName, redirectionUrl, redirectionCounter)
reply.deleteLater()
return
else:
reposXML = QDomDocument()
content = reply.readAll()
@@ -50,15 +50,19 @@ def __init__(self, parent, plugin):
self.labelName.setText(plugin["name"])
self.buttonBox.clicked.connect(self.abort)

url = QUrl(plugin["download_url"])
self.url = QUrl(plugin["download_url"])
self.redirectionCounter = 0

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

self.request = QNetworkRequest(url)
authcfg = repositories.all()[plugin["zip_repository"]]["authcfg"]
self.requestDownloading()

def requestDownloading(self):
self.request = QNetworkRequest(self.url)
authcfg = repositories.all()[self.plugin["zip_repository"]]["authcfg"]
if authcfg and isinstance(authcfg, str):
if not QgsApplication.authManager().updateNetworkRequest(
self.request, authcfg.strip()):
@@ -106,6 +110,23 @@ def requestFinished(self):
self.reject()
reply.deleteLater()
return
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 301:
redirectionUrl = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
self.redirectionCounter += 1
if self.redirectionCounter > 4:
self.mResult = QCoreApplication.translate("QgsPluginInstaller", "Too many redirections")
self.reject()
reply.deleteLater()
return
else:
if redirectionUrl.isRelative():
redirectionUrl = reply.url().resolved(redirectionUrl)
# Fire a new request and exit immediately in order to quietly destroy the old one
self.url = redirectionUrl
self.requestDownloading()
reply.deleteLater()
return

self.file.open(QFile.WriteOnly)
self.file.write(reply.readAll())
self.file.close()

0 comments on commit 3da5ab6

Please sign in to comment.
You can’t perform that action at this time.