1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01:00

Added redirect loop check

This commit is contained in:
Martin Tůma 2017-01-16 21:45:27 +01:00
parent 1a29ab6304
commit 0e356d0222
2 changed files with 43 additions and 11 deletions

View File

@ -17,8 +17,13 @@
#define USER_AGENT \ #define USER_AGENT \
APP_NAME "/" APP_VERSION " (" PLATFORM_STR "; Qt " QT_VERSION_STR ")" APP_NAME "/" APP_VERSION " (" PLATFORM_STR "; Qt " QT_VERSION_STR ")"
#define ATTR_FILE QNetworkRequest::User #define ATTR_REDIRECT QNetworkRequest::RedirectionTargetAttribute
#define ATTR_ORIGIN (QNetworkRequest::Attribute)(QNetworkRequest::User + 1) #define ATTR_FILE QNetworkRequest::User
#define ATTR_ORIGIN (QNetworkRequest::Attribute)(QNetworkRequest::User + 1)
#define ATTR_LEVEL (QNetworkRequest::Attribute)(QNetworkRequest::User + 2)
#define MAX_REDIRECT_LEVEL 5
Downloader::Downloader() Downloader::Downloader()
{ {
@ -26,7 +31,7 @@ Downloader::Downloader()
SLOT(downloadFinished(QNetworkReply*))); SLOT(downloadFinished(QNetworkReply*)));
} }
bool Downloader::doDownload(const Download &dl, const QUrl &origin) bool Downloader::doDownload(const Download &dl, const Redirect &redirect)
{ {
QUrl url(dl.url()); QUrl url(dl.url());
@ -37,8 +42,10 @@ bool Downloader::doDownload(const Download &dl, const QUrl &origin)
QNetworkRequest request(url); QNetworkRequest request(url);
request.setAttribute(ATTR_FILE, QVariant(dl.file())); request.setAttribute(ATTR_FILE, QVariant(dl.file()));
if (!origin.isEmpty()) if (!redirect.isNull()) {
request.setAttribute(ATTR_ORIGIN, QVariant(origin)); request.setAttribute(ATTR_ORIGIN, QVariant(redirect.origin()));
request.setAttribute(ATTR_LEVEL, QVariant(redirect.level()));
}
request.setRawHeader("User-Agent", USER_AGENT); request.setRawHeader("User-Agent", USER_AGENT);
QNetworkReply *reply = _manager.get(request); QNetworkReply *reply = _manager.get(request);
@ -80,15 +87,23 @@ void Downloader::downloadFinished(QNetworkReply *reply)
qPrintable(reply->errorString())); qPrintable(reply->errorString()));
} }
} else { } else {
QUrl redirect = reply->attribute( QUrl location = reply->attribute(ATTR_REDIRECT).toUrl();
QNetworkRequest::RedirectionTargetAttribute).toUrl();
QString filename = reply->request().attribute(ATTR_FILE) QString filename = reply->request().attribute(ATTR_FILE)
.toString(); .toString();
if (!redirect.isEmpty()) { if (!location.isEmpty()) {
QUrl origin = reply->request().attribute(ATTR_ORIGIN).toUrl(); QUrl origin = reply->request().attribute(ATTR_ORIGIN).toUrl();
Download dl(redirect, filename); int level = reply->request().attribute(ATTR_LEVEL).toInt();
doDownload(dl, origin.isEmpty() ? url : origin);
if (level >= MAX_REDIRECT_LEVEL)
fprintf(stderr, "Error downloading map tile: %s: "
"redirect level limit reached\n",
origin.toEncoded().constData());
else {
Redirect redirect(origin.isEmpty() ? url : origin, level++);
Download dl(location, filename);
doDownload(dl, redirect);
}
} else } else
if (!saveToDisk(filename, reply)) if (!saveToDisk(filename, reply))
_errorDownloads.insert(url); _errorDownloads.insert(url);

View File

@ -39,11 +39,28 @@ private slots:
void downloadFinished(QNetworkReply *reply); void downloadFinished(QNetworkReply *reply);
private: private:
class Redirect
{
public:
Redirect() : _level(0) {}
Redirect(const QUrl &origin, int level) :
_origin(origin), _level(level) {}
const QUrl &origin() const {return _origin;}
int level() const {return _level;}
bool isNull() const {return (_level == 0);}
private:
QUrl _origin;
int _level;
};
Downloader(); Downloader();
Downloader(Downloader const&); Downloader(Downloader const&);
void operator=(Downloader const&); void operator=(Downloader const&);
bool doDownload(const Download &dl, const QUrl &origin = QUrl()); bool doDownload(const Download &dl, const Redirect &redirect = Redirect());
bool saveToDisk(const QString &filename, QIODevice *data); bool saveToDisk(const QString &filename, QIODevice *data);
QNetworkAccessManager _manager; QNetworkAccessManager _manager;