+ issue #0001705: Download manager does not follow redirect (HTTP status 302)
This commit is contained in:
parent
0d1fc35b20
commit
c3102b69bc
|
@ -464,7 +464,8 @@ void DownloadItem::metaDataChanged()
|
||||||
header.chop(1);
|
header.chop(1);
|
||||||
m_fileName = QUrl::fromPercentEncoding(header);
|
m_fileName = QUrl::fromPercentEncoding(header);
|
||||||
}
|
}
|
||||||
else {
|
// Sometimes "filename=" and "filename*=UTF-8''" is set.
|
||||||
|
// So, search for this too.
|
||||||
index = header.indexOf("filename*=UTF-8''");
|
index = header.indexOf("filename*=UTF-8''");
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
header = header.mid(index+17);
|
header = header.mid(index+17);
|
||||||
|
@ -475,7 +476,6 @@ void DownloadItem::metaDataChanged()
|
||||||
m_fileName = QUrl::fromPercentEncoding(header);
|
m_fileName = QUrl::fromPercentEncoding(header);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
QVariant statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
QVariant statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
||||||
if (!statusCode.isValid())
|
if (!statusCode.isValid())
|
||||||
|
|
|
@ -71,6 +71,8 @@ DownloadManager::DownloadManager(QWidget *parent)
|
||||||
m_model = new DownloadModel(this);
|
m_model = new DownloadModel(this);
|
||||||
ui->downloadsView->setModel(m_model);
|
ui->downloadsView->setModel(m_model);
|
||||||
connect(ui->cleanupButton, SIGNAL(clicked()), this, SLOT(cleanup()));
|
connect(ui->cleanupButton, SIGNAL(clicked()), this, SLOT(cleanup()));
|
||||||
|
connect(m_manager, SIGNAL(finished(QNetworkReply*)),
|
||||||
|
this, SLOT(replyFinished(QNetworkReply*)));
|
||||||
load();
|
load();
|
||||||
|
|
||||||
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance();
|
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance();
|
||||||
|
@ -108,11 +110,57 @@ int DownloadManager::activeDownloads() const
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QUrl DownloadManager::redirectUrl(const QUrl& url) const
|
||||||
|
{
|
||||||
|
QUrl redirectUrl = url;
|
||||||
|
if (url.host() == QLatin1String("www.dropbox.com")) {
|
||||||
|
QList< QPair<QString, QString> > query = url.queryItems();
|
||||||
|
for (QList< QPair<QString, QString> >::iterator it = query.begin(); it != query.end(); ++it) {
|
||||||
|
if (it->first == QLatin1String("dl") && it->second == QLatin1String("0\r\n")) {
|
||||||
|
redirectUrl.removeQueryItem(QLatin1String("dl"));
|
||||||
|
redirectUrl.addQueryItem(QLatin1String("dl"), QLatin1String("1\r\n"));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirectUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DownloadManager::replyFinished(QNetworkReply* reply)
|
||||||
|
{
|
||||||
|
// The 'requestFileName' is used to store the argument passed by 'download()'
|
||||||
|
// and to also distinguish between replies created by 'download()' and
|
||||||
|
// this method.
|
||||||
|
// Replies from this method shouldn't be further examined because it's not
|
||||||
|
// assumed to get re-directed urls over several steps.
|
||||||
|
QVariant var = reply->property("requestFileName");
|
||||||
|
if (var.isValid()) {
|
||||||
|
bool requestFileName = reply->property("requestFileName").toBool();
|
||||||
|
|
||||||
|
QUrl url = reply->url();
|
||||||
|
|
||||||
|
// If this is a redirected url use this instead
|
||||||
|
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
||||||
|
if (!redirectUrl.isEmpty()) {
|
||||||
|
url = redirectUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start the actual download now
|
||||||
|
handleUnsupportedContent(m_manager->get(QNetworkRequest(url)), requestFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
reply->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
void DownloadManager::download(const QNetworkRequest &request, bool requestFileName)
|
void DownloadManager::download(const QNetworkRequest &request, bool requestFileName)
|
||||||
{
|
{
|
||||||
if (request.url().isEmpty())
|
if (request.url().isEmpty())
|
||||||
return;
|
return;
|
||||||
handleUnsupportedContent(m_manager->get(request), requestFileName);
|
|
||||||
|
// postpone this reply until we can examine it in replyFinished
|
||||||
|
QNetworkReply* reply = m_manager->get(request);
|
||||||
|
reply->setProperty("requestFileName", QVariant(requestFileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadManager::handleUnsupportedContent(QNetworkReply *reply, bool requestFileName)
|
void DownloadManager::handleUnsupportedContent(QNetworkReply *reply, bool requestFileName)
|
||||||
|
|
|
@ -65,6 +65,7 @@ public:
|
||||||
RemovePolicy removePolicy() const;
|
RemovePolicy removePolicy() const;
|
||||||
void setRemovePolicy(RemovePolicy policy);
|
void setRemovePolicy(RemovePolicy policy);
|
||||||
void closeEvent(QCloseEvent* e);
|
void closeEvent(QCloseEvent* e);
|
||||||
|
QUrl redirectUrl(const QUrl&) const;
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void download(const QNetworkRequest &request, bool requestFileName = false);
|
void download(const QNetworkRequest &request, bool requestFileName = false);
|
||||||
|
@ -76,6 +77,7 @@ public Q_SLOTS:
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void save() const;
|
void save() const;
|
||||||
void updateRow();
|
void updateRow();
|
||||||
|
void replyFinished(QNetworkReply* reply);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void addItem(DownloadItem *item);
|
void addItem(DownloadItem *item);
|
||||||
|
|
|
@ -1585,7 +1585,8 @@ void MainWindow::loadUrls(App::Document* doc, const QList<QUrl>& url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (it->scheme().toLower() == QLatin1String("http")) {
|
else if (it->scheme().toLower() == QLatin1String("http")) {
|
||||||
Gui::Dialog::DownloadManager::getInstance()->download(*it);
|
Gui::Dialog::DownloadManager* dm = Gui::Dialog::DownloadManager::getInstance();
|
||||||
|
dm->download(dm->redirectUrl(*it));
|
||||||
}
|
}
|
||||||
//#ifndef QT_NO_OPENSSL
|
//#ifndef QT_NO_OPENSSL
|
||||||
else if (it->scheme().toLower() == QLatin1String("https")) {
|
else if (it->scheme().toLower() == QLatin1String("https")) {
|
||||||
|
@ -1594,7 +1595,8 @@ void MainWindow::loadUrls(App::Document* doc, const QList<QUrl>& url)
|
||||||
url.removeEncodedQueryItem(QByteArray("sid"));
|
url.removeEncodedQueryItem(QByteArray("sid"));
|
||||||
url.setScheme(QLatin1String("http"));
|
url.setScheme(QLatin1String("http"));
|
||||||
}
|
}
|
||||||
Gui::Dialog::DownloadManager::getInstance()->download(url);
|
Gui::Dialog::DownloadManager* dm = Gui::Dialog::DownloadManager::getInstance();
|
||||||
|
dm->download(dm->redirectUrl(url));
|
||||||
}
|
}
|
||||||
//#endif
|
//#endif
|
||||||
else if (it->scheme().toLower() == QLatin1String("ftp")) {
|
else if (it->scheme().toLower() == QLatin1String("ftp")) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user