diff options
author | Petr Mrázek <peterix@gmail.com> | 2014-09-06 18:16:56 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2014-09-06 19:03:05 +0200 |
commit | 20cb97a35af5097e9d3b2062c0dfcb5f2e5fff5c (patch) | |
tree | 56bf51e681f2e73590a549499bd83d7b505c39f8 /logic/net | |
parent | 36efcf8d3c0cbd7823fc65569cfc2b011435db2c (diff) | |
download | MultiMC-20cb97a35af5097e9d3b2062c0dfcb5f2e5fff5c.tar MultiMC-20cb97a35af5097e9d3b2062c0dfcb5f2e5fff5c.tar.gz MultiMC-20cb97a35af5097e9d3b2062c0dfcb5f2e5fff5c.tar.lz MultiMC-20cb97a35af5097e9d3b2062c0dfcb5f2e5fff5c.tar.xz MultiMC-20cb97a35af5097e9d3b2062c0dfcb5f2e5fff5c.zip |
Sync from quickmods
Diffstat (limited to 'logic/net')
-rw-r--r-- | logic/net/ByteArrayDownload.cpp | 26 | ||||
-rw-r--r-- | logic/net/ByteArrayDownload.h | 4 | ||||
-rw-r--r-- | logic/net/CacheDownload.cpp | 24 | ||||
-rw-r--r-- | logic/net/CacheDownload.h | 2 | ||||
-rw-r--r-- | logic/net/NetAction.h | 3 | ||||
-rw-r--r-- | logic/net/NetJob.h | 20 |
6 files changed, 65 insertions, 14 deletions
diff --git a/logic/net/ByteArrayDownload.cpp b/logic/net/ByteArrayDownload.cpp index 27d2a250..a8d1d330 100644 --- a/logic/net/ByteArrayDownload.cpp +++ b/logic/net/ByteArrayDownload.cpp @@ -53,16 +53,42 @@ void ByteArrayDownload::downloadError(QNetworkReply::NetworkError error) QLOG_ERROR() << "Error getting URL:" << m_url.toString().toLocal8Bit() << "Network error: " << error; m_status = Job_Failed; + m_errorString = m_reply->errorString(); } void ByteArrayDownload::downloadFinished() { + if (m_followRedirects) + { + QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader); + QString redirectURL; + if(redirect.isValid()) + { + redirectURL = redirect.toString(); + } + // FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061 + else if(m_reply->hasRawHeader("Location")) + { + auto data = m_reply->rawHeader("Location"); + if(data.size() > 2 && data[0] == '/' && data[1] == '/') + redirectURL = m_reply->url().scheme() + ":" + data; + } + if (!redirectURL.isEmpty()) + { + m_url = QUrl(redirect.toString()); + QLOG_INFO() << "Following redirect to " << m_url.toString(); + start(); + return; + } + } + // if the download succeeded if (m_status != Job_Failed) { // nothing went wrong... m_status = Job_Finished; m_data = m_reply->readAll(); + m_content_type = m_reply->header(QNetworkRequest::ContentTypeHeader).toString(); m_reply.reset(); emit succeeded(m_index_within_job); return; diff --git a/logic/net/ByteArrayDownload.h b/logic/net/ByteArrayDownload.h index 76e2e279..85ed2dab 100644 --- a/logic/net/ByteArrayDownload.h +++ b/logic/net/ByteArrayDownload.h @@ -31,6 +31,10 @@ public: /// if not saving to file, downloaded data is placed here QByteArray m_data; + QString m_errorString; + + bool m_followRedirects = false; + public slots: virtual void start(); diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp index d2a9bdee..6d937e30 100644 --- a/logic/net/CacheDownload.cpp +++ b/logic/net/CacheDownload.cpp @@ -101,6 +101,30 @@ void CacheDownload::downloadError(QNetworkReply::NetworkError error) } void CacheDownload::downloadFinished() { + if (m_followRedirects) + { + QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader); + QString redirectURL; + if(redirect.isValid()) + { + redirectURL = redirect.toString(); + } + // FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061 + else if(m_reply->hasRawHeader("Location")) + { + auto data = m_reply->rawHeader("Location"); + if(data.size() > 2 && data[0] == '/' && data[1] == '/') + redirectURL = m_reply->url().scheme() + ":" + data; + } + if (!redirectURL.isEmpty()) + { + m_url = QUrl(redirect.toString()); + QLOG_INFO() << "Following redirect to " << m_url.toString(); + start(); + return; + } + } + // if the download succeeded if (m_status == Job_Failed) { diff --git a/logic/net/CacheDownload.h b/logic/net/CacheDownload.h index d446d23e..9eb69fcd 100644 --- a/logic/net/CacheDownload.h +++ b/logic/net/CacheDownload.h @@ -36,6 +36,8 @@ private: bool wroteAnyData = false; public: + bool m_followRedirects = false; + explicit CacheDownload(QUrl url, MetaEntryPtr entry); static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry) { diff --git a/logic/net/NetAction.h b/logic/net/NetAction.h index 97c96e5d..fdd69eec 100644 --- a/logic/net/NetAction.h +++ b/logic/net/NetAction.h @@ -55,6 +55,9 @@ public: /// the network reply std::shared_ptr<QNetworkReply> m_reply; + /// the content of the content-type header + QString m_content_type; + /// source URL QUrl m_url; diff --git a/logic/net/NetJob.h b/logic/net/NetJob.h index d05e7b6f..8fc04d06 100644 --- a/logic/net/NetJob.h +++ b/logic/net/NetJob.h @@ -30,8 +30,8 @@ class NetJob : public ProgressProvider { Q_OBJECT public: - explicit NetJob(QString job_name) : ProgressProvider(), m_job_name(job_name) {}; - virtual ~NetJob() {}; + explicit NetJob(QString job_name) : ProgressProvider(), m_job_name(job_name) {} + virtual ~NetJob() {} template <typename T> bool addNetAction(T action) { NetActionPtr base = std::static_pointer_cast<NetAction>(action); @@ -62,7 +62,10 @@ public: { return downloads[index]; } - ; + const NetActionPtr at(const int index) + { + return downloads.at(index); + } NetActionPtr first() { if (downloads.size()) @@ -73,21 +76,10 @@ public: { return downloads.size(); } - virtual void getProgress(qint64 ¤t, qint64 &total) - { - current = current_progress; - total = total_progress; - } - ; - virtual QString getStatus() const - { - return m_job_name; - } virtual bool isRunning() const { return m_running; } - ; QStringList getFailedFiles(); signals: void started(); |