From c33b4e252f8bf37bc716adc7789926185c3e384c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 21 Jan 2018 03:49:54 +0100 Subject: [PATCH] NOISSUE fix bad redirect URLs provided by the curse CDN MultiMC now parses the HTTP Location header in a (more) tolerant mode. --- api/logic/InstanceImportTask.cpp | 1 + .../minecraft/flame/FileResolvingTask.cpp | 8 +++- api/logic/minecraft/flame/PackManifest.h | 3 +- api/logic/net/Download.cpp | 42 ++++++++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/api/logic/InstanceImportTask.cpp b/api/logic/InstanceImportTask.cpp index 046f885d..2b481300 100644 --- a/api/logic/InstanceImportTask.cpp +++ b/api/logic/InstanceImportTask.cpp @@ -327,6 +327,7 @@ void InstanceImportTask::processFlame() case Flame::File::Type::SingleFile: case Flame::File::Type::Mod: { + qDebug() << "Will download" << result.url << "to" << path; auto dl = Net::Download::makeFile(result.url, path); m_filesNetJob->addNetAction(dl); break; diff --git a/api/logic/minecraft/flame/FileResolvingTask.cpp b/api/logic/minecraft/flame/FileResolvingTask.cpp index af8ef7aa..980b8c3a 100644 --- a/api/logic/minecraft/flame/FileResolvingTask.cpp +++ b/api/logic/minecraft/flame/FileResolvingTask.cpp @@ -48,7 +48,13 @@ void Flame::FileResolvingTask::netJobFinished() continue; } out.fileName = Json::requireString(obj, "FileNameOnDisk"); - out.url = Json::requireString(obj, "DownloadURL"); + auto urlString = Json::requireString(obj, "DownloadURL"); + urlString.replace(' ', "%20"); + out.url = QUrl(urlString, QUrl::StrictMode); + if(!out.url.isValid()) + { + throw "Perkele!"; + } // This is a piece of a Flame project JSON pulled out into the file metadata (here) for convenience // It is also optional QJsonObject projObj = Json::ensureObject(obj, "_Project", {}); diff --git a/api/logic/minecraft/flame/PackManifest.h b/api/logic/minecraft/flame/PackManifest.h index 07bf92b6..1a5254a8 100644 --- a/api/logic/minecraft/flame/PackManifest.h +++ b/api/logic/minecraft/flame/PackManifest.h @@ -2,6 +2,7 @@ #include #include +#include namespace Flame { @@ -15,7 +16,7 @@ struct File // our bool resolved = false; QString fileName; - QString url; + QUrl url; QString targetFolder = QLatin1Literal("mods"); enum class Type { diff --git a/api/logic/net/Download.cpp b/api/logic/net/Download.cpp index 816d2002..e45c41cb 100644 --- a/api/logic/net/Download.cpp +++ b/api/logic/net/Download.cpp @@ -150,13 +150,50 @@ void Download::sslErrors(const QList & errors) bool Download::handleRedirect() { - QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader); + QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl(); + if(!redirect.isValid()) + { + if(!m_reply->hasRawHeader("Location")) + { + // no redirect -> it's fine to continue + return false; + } + // there is a Location header, but it's not correct. we need to apply some workarounds... + QByteArray redirectBA = m_reply->rawHeader("Location"); + if(redirectBA.size() == 0) + { + // empty, yet present redirect header? WTF? + return false; + } + QString redirectStr = QString::fromUtf8(redirectBA); + + /* + * IF the URL begins with //, we need to insert the URL scheme. + * See: https://bugreports.qt-project.org/browse/QTBUG-41061 + */ + if(redirectStr.startsWith("//")) + { + redirectStr = m_reply->url().scheme() + ":" + redirectStr; + } + + /* + * Next, make sure the URL is parsed in tolerant mode. Qt doesn't parse the location header in tolerant mode, which causes issues. + * FIXME: report Qt bug for this + */ + redirect = QUrl(redirectStr, QUrl::TolerantMode); + qDebug() << "Fixed location header:" << redirect; + } + else + { + qDebug() << "Location header:" << redirect; + } + QString redirectURL; if(redirect.isValid()) { redirectURL = redirect.toString(); } - // FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061 + // FIXME: This is a hack for else if(m_reply->hasRawHeader("Location")) { auto data = m_reply->rawHeader("Location"); @@ -165,6 +202,7 @@ bool Download::handleRedirect() redirectURL = m_reply->url().scheme() + ":" + data; } } + if (!redirectURL.isEmpty()) { m_url = QUrl(redirect.toString());