summaryrefslogtreecommitdiffstats
path: root/examples/network/downloadmanager/downloadmanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/network/downloadmanager/downloadmanager.cpp')
-rw-r--r--examples/network/downloadmanager/downloadmanager.cpp58
1 files changed, 44 insertions, 14 deletions
diff --git a/examples/network/downloadmanager/downloadmanager.cpp b/examples/network/downloadmanager/downloadmanager.cpp
index 69d8fd1ebc..e820b4ff70 100644
--- a/examples/network/downloadmanager/downloadmanager.cpp
+++ b/examples/network/downloadmanager/downloadmanager.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -50,23 +50,21 @@
#include "downloadmanager.h"
-#include <QFileInfo>
-#include <QNetworkRequest>
-#include <QNetworkReply>
-#include <QString>
-#include <QStringList>
-#include <QTimer>
-#include <stdio.h>
+#include <QTextStream>
+
+#include <cstdio>
+
+using namespace std;
DownloadManager::DownloadManager(QObject *parent)
- : QObject(parent), downloadedCount(0), totalCount(0)
+ : QObject(parent)
{
}
-void DownloadManager::append(const QStringList &urlList)
+void DownloadManager::append(const QStringList &urls)
{
- foreach (QString url, urlList)
- append(QUrl::fromEncoded(url.toLocal8Bit()));
+ for (const QString &urlAsString : urls)
+ append(QUrl::fromEncoded(urlAsString.toLocal8Bit()));
if (downloadQueue.isEmpty())
QTimer::singleShot(0, this, SIGNAL(finished()));
@@ -167,9 +165,16 @@ void DownloadManager::downloadFinished()
if (currentDownload->error()) {
// download failed
fprintf(stderr, "Failed: %s\n", qPrintable(currentDownload->errorString()));
+ output.remove();
} else {
- printf("Succeeded.\n");
- ++downloadedCount;
+ // let's check if it was actually a redirect
+ if (isHttpRedirect()) {
+ reportRedirect();
+ output.remove();
+ } else {
+ printf("Succeeded.\n");
+ ++downloadedCount;
+ }
}
currentDownload->deleteLater();
@@ -180,3 +185,28 @@ void DownloadManager::downloadReadyRead()
{
output.write(currentDownload->readAll());
}
+
+bool DownloadManager::isHttpRedirect() const
+{
+ int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+ return statusCode == 301 || statusCode == 302 || statusCode == 303
+ || statusCode == 305 || statusCode == 307 || statusCode == 308;
+}
+
+void DownloadManager::reportRedirect()
+{
+ int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+ QUrl requestUrl = currentDownload->request().url();
+ QTextStream(stderr) << "Request: " << requestUrl.toDisplayString()
+ << " was redirected with code: " << statusCode
+ << '\n';
+
+ QVariant target = currentDownload->attribute(QNetworkRequest::RedirectionTargetAttribute);
+ if (!target.isValid())
+ return;
+ QUrl redirectUrl = target.toUrl();
+ if (redirectUrl.isRelative())
+ redirectUrl = requestUrl.resolved(redirectUrl);
+ QTextStream(stderr) << "Redirected to: " << redirectUrl.toDisplayString()
+ << '\n';
+}