mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-12-01 07:01:16 +01:00
Added download retries in case of connection timeout
This commit is contained in:
parent
9c125a0583
commit
247eef5261
@ -1,7 +1,6 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
#include <QNetworkReply>
|
|
||||||
#include <QBasicTimer>
|
#include <QBasicTimer>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "downloader.h"
|
#include "downloader.h"
|
||||||
@ -26,6 +25,7 @@
|
|||||||
#define ATTR_LEVEL (QNetworkRequest::Attribute)(QNetworkRequest::User + 2)
|
#define ATTR_LEVEL (QNetworkRequest::Attribute)(QNetworkRequest::User + 2)
|
||||||
|
|
||||||
#define MAX_REDIRECT_LEVEL 5
|
#define MAX_REDIRECT_LEVEL 5
|
||||||
|
#define RETRIES 3
|
||||||
|
|
||||||
|
|
||||||
Authorization::Authorization(const QString &username, const QString &password)
|
Authorization::Authorization(const QString &username, const QString &password)
|
||||||
@ -89,10 +89,12 @@ bool Downloader::doDownload(const Download &dl,
|
|||||||
|
|
||||||
if (!url.isValid() || !(url.scheme() == "http" || url.scheme() == "https")) {
|
if (!url.isValid() || !(url.scheme() == "http" || url.scheme() == "https")) {
|
||||||
qWarning("%s: Invalid URL\n", qPrintable(url.toString()));
|
qWarning("%s: Invalid URL\n", qPrintable(url.toString()));
|
||||||
|
if (redirect)
|
||||||
|
_errorDownloads.insert(redirect->origin(), RETRIES);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_errorDownloads.contains(url))
|
if (_errorDownloads.value(url) >= RETRIES)
|
||||||
return false;
|
return false;
|
||||||
if (_currentDownloads.contains(url) && !redirect)
|
if (_currentDownloads.contains(url) && !redirect)
|
||||||
return false;
|
return false;
|
||||||
@ -141,18 +143,27 @@ bool Downloader::saveToDisk(const QString &filename, QIODevice *data)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Downloader::insertError(const QUrl &url, QNetworkReply::NetworkError error)
|
||||||
|
{
|
||||||
|
if (error == QNetworkReply::OperationCanceledError)
|
||||||
|
_errorDownloads.insert(url, _errorDownloads.value(url) + 1);
|
||||||
|
else
|
||||||
|
_errorDownloads.insert(url, RETRIES);
|
||||||
|
}
|
||||||
|
|
||||||
void Downloader::downloadFinished(QNetworkReply *reply)
|
void Downloader::downloadFinished(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
QUrl url = reply->request().url();
|
QUrl url = reply->request().url();
|
||||||
|
QNetworkReply::NetworkError error = reply->error();
|
||||||
|
|
||||||
if (reply->error()) {
|
if (error) {
|
||||||
QUrl origin = reply->request().attribute(ATTR_ORIGIN).toUrl();
|
QUrl origin = reply->request().attribute(ATTR_ORIGIN).toUrl();
|
||||||
if (origin.isEmpty()) {
|
if (origin.isEmpty()) {
|
||||||
_errorDownloads.insert(url);
|
insertError(url, error);
|
||||||
qWarning("Error downloading file: %s: %s\n",
|
qWarning("Error downloading file: %s: %s\n",
|
||||||
url.toEncoded().constData(), qPrintable(reply->errorString()));
|
url.toEncoded().constData(), qPrintable(reply->errorString()));
|
||||||
} else {
|
} else {
|
||||||
_errorDownloads.insert(origin);
|
insertError(origin, error);
|
||||||
qWarning("Error downloading file: %s -> %s: %s\n",
|
qWarning("Error downloading file: %s -> %s: %s\n",
|
||||||
origin.toEncoded().constData(), url.toEncoded().constData(),
|
origin.toEncoded().constData(), url.toEncoded().constData(),
|
||||||
qPrintable(reply->errorString()));
|
qPrintable(reply->errorString()));
|
||||||
@ -167,7 +178,7 @@ void Downloader::downloadFinished(QNetworkReply *reply)
|
|||||||
int level = reply->request().attribute(ATTR_LEVEL).toInt();
|
int level = reply->request().attribute(ATTR_LEVEL).toInt();
|
||||||
|
|
||||||
if (level >= MAX_REDIRECT_LEVEL) {
|
if (level >= MAX_REDIRECT_LEVEL) {
|
||||||
_errorDownloads.insert(origin);
|
_errorDownloads.insert(origin, RETRIES);
|
||||||
qWarning("Error downloading file: %s: "
|
qWarning("Error downloading file: %s: "
|
||||||
"redirect level limit reached (redirect loop?)\n",
|
"redirect level limit reached (redirect loop?)\n",
|
||||||
origin.toEncoded().constData());
|
origin.toEncoded().constData());
|
||||||
@ -182,13 +193,12 @@ void Downloader::downloadFinished(QNetworkReply *reply)
|
|||||||
|
|
||||||
Redirect redirect(origin.isEmpty() ? url : origin, level + 1);
|
Redirect redirect(origin.isEmpty() ? url : origin, level + 1);
|
||||||
Download dl(redirectUrl, filename);
|
Download dl(redirectUrl, filename);
|
||||||
if (!doDownload(dl, reply->request().rawHeader("Authorization"),
|
doDownload(dl, reply->request().rawHeader("Authorization"),
|
||||||
&redirect))
|
&redirect);
|
||||||
_errorDownloads.insert(origin.isEmpty() ? url : origin);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!saveToDisk(filename, reply))
|
if (!saveToDisk(filename, reply))
|
||||||
_errorDownloads.insert(url);
|
_errorDownloads.insert(url, RETRIES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,12 @@
|
|||||||
#define DOWNLOADER_H
|
#define DOWNLOADER_H
|
||||||
|
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
|
#include <QNetworkReply>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
|
#include <QHash>
|
||||||
|
|
||||||
class QNetworkReply;
|
|
||||||
|
|
||||||
class Download
|
class Download
|
||||||
{
|
{
|
||||||
@ -59,12 +60,13 @@ private:
|
|||||||
class Redirect;
|
class Redirect;
|
||||||
class ReplyTimeout;
|
class ReplyTimeout;
|
||||||
|
|
||||||
|
void insertError(const QUrl &url, QNetworkReply::NetworkError error);
|
||||||
bool doDownload(const Download &dl, const QByteArray &authorization,
|
bool doDownload(const Download &dl, const QByteArray &authorization,
|
||||||
const Redirect *redirect = 0);
|
const Redirect *redirect = 0);
|
||||||
bool saveToDisk(const QString &filename, QIODevice *data);
|
bool saveToDisk(const QString &filename, QIODevice *data);
|
||||||
|
|
||||||
QSet<QUrl> _currentDownloads;
|
QSet<QUrl> _currentDownloads;
|
||||||
QSet<QUrl> _errorDownloads;
|
QHash<QUrl, int> _errorDownloads;
|
||||||
|
|
||||||
static int _timeout;
|
static int _timeout;
|
||||||
static QNetworkAccessManager *_manager;
|
static QNetworkAccessManager *_manager;
|
||||||
|
Loading…
Reference in New Issue
Block a user