From 42d7bc4c00bac55bd1aa9da5695be93e3f51a6a1 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Wed, 27 Sep 2017 14:11:58 +0200 Subject: [PATCH] QtNetwork (examples) - update network download manager Mainly 'modernizing' - use c-library includes ( -> ), add appropriate using directive; minor fixes in formatting + removal of a hated double negation (aka ifndef QT_NO_NOTHING). Also, as our rules ('how to write examples') suggest - replace too many inclusion directives with module-level headers. Basic redirects handling - do not create empty files for redirected requests (or even files with some useless html). Task-number: QTBUG-60628 Change-Id: Ia4398d39126313e6213bc7244d11a55958e64dec Reviewed-by: Edward Welbourne --- examples/network/download/main.cpp | 58 ++++++++++++++++-------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/examples/network/download/main.cpp b/examples/network/download/main.cpp index e5ad050de34..96111983eac 100644 --- a/examples/network/download/main.cpp +++ b/examples/network/download/main.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. @@ -48,37 +48,29 @@ ** ****************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include -#include +#include QT_BEGIN_NAMESPACE class QSslError; QT_END_NAMESPACE -QT_USE_NAMESPACE +using namespace std; class DownloadManager: public QObject { Q_OBJECT QNetworkAccessManager manager; - QList currentDownloads; + QVector currentDownloads; public: DownloadManager(); void doDownload(const QUrl &url); - QString saveFileName(const QUrl &url); + static QString saveFileName(const QUrl &url); bool saveToDisk(const QString &filename, QIODevice *data); + static bool isHttpRedirect(QNetworkReply *reply); public slots: void execute(); @@ -97,8 +89,9 @@ void DownloadManager::doDownload(const QUrl &url) QNetworkRequest request(url); QNetworkReply *reply = manager.get(request); -#ifndef QT_NO_SSL - connect(reply, SIGNAL(sslErrors(QList)), SLOT(sslErrors(QList))); +#if QT_CONFIG(ssl) + connect(reply, SIGNAL(sslErrors(QList)), + SLOT(sslErrors(QList))); #endif currentDownloads.append(reply); @@ -141,6 +134,13 @@ bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data) return true; } +bool DownloadManager::isHttpRedirect(QNetworkReply *reply) +{ + int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + return statusCode == 301 || statusCode == 302 || statusCode == 303 + || statusCode == 305 || statusCode == 307 || statusCode == 308; +} + void DownloadManager::execute() { QStringList args = QCoreApplication::instance()->arguments(); @@ -156,7 +156,7 @@ void DownloadManager::execute() return; } - foreach (QString arg, args) { + for (const QString &arg : qAsConst(args)) { QUrl url = QUrl::fromEncoded(arg.toLocal8Bit()); doDownload(url); } @@ -164,8 +164,8 @@ void DownloadManager::execute() void DownloadManager::sslErrors(const QList &sslErrors) { -#ifndef QT_NO_SSL - foreach (const QSslError &error, sslErrors) +#if QT_CONFIG(ssl) + for (const QSslError &error : sslErrors) fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString())); #else Q_UNUSED(sslErrors); @@ -180,18 +180,24 @@ void DownloadManager::downloadFinished(QNetworkReply *reply) url.toEncoded().constData(), qPrintable(reply->errorString())); } else { - QString filename = saveFileName(url); - if (saveToDisk(filename, reply)) - printf("Download of %s succeeded (saved to %s)\n", - url.toEncoded().constData(), qPrintable(filename)); + if (isHttpRedirect(reply)) { + fputs("Request was redirected.\n", stderr); + } else { + QString filename = saveFileName(url); + if (saveToDisk(filename, reply)) { + printf("Download of %s succeeded (saved to %s)\n", + url.toEncoded().constData(), qPrintable(filename)); + } + } } currentDownloads.removeAll(reply); reply->deleteLater(); - if (currentDownloads.isEmpty()) + if (currentDownloads.isEmpty()) { // all downloads finished QCoreApplication::instance()->quit(); + } } int main(int argc, char **argv)