1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Added finite HTTP response timeout

This commit is contained in:
Martin Tůma 2018-03-14 07:23:07 +01:00
parent 07aca435d8
commit 3043128565
4 changed files with 40 additions and 5 deletions

View File

@ -2,6 +2,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <QBasicTimer>
#include "config.h" #include "config.h"
#include "downloader.h" #include "downloader.h"
@ -25,8 +26,37 @@
#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 TIMEOUT 30 /* s */
class ReplyTimeout : public QObject
{
public:
static void set(QNetworkReply *reply, int timeout)
{
Q_ASSERT(reply);
new ReplyTimeout(reply, timeout);
}
private:
ReplyTimeout(QNetworkReply *reply, int timeout) : QObject(reply)
{
_timer.start(timeout * 1000, this);
}
void timerEvent(QTimerEvent *ev)
{
if (!_timer.isActive() || ev->timerId() != _timer.timerId())
return;
QNetworkReply *reply = static_cast<QNetworkReply*>(parent());
if (reply->isRunning())
reply->close();
_timer.stop();
}
QBasicTimer _timer;
};
Downloader::Downloader(QObject *parent) : QObject(parent) Downloader::Downloader(QObject *parent) : QObject(parent)
{ {
connect(&_manager, SIGNAL(finished(QNetworkReply*)), connect(&_manager, SIGNAL(finished(QNetworkReply*)),
@ -49,9 +79,13 @@ bool Downloader::doDownload(const Download &dl, const Redirect &redirect)
request.setAttribute(ATTR_LEVEL, QVariant(redirect.level())); 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);
if (reply) {
_currentDownloads.insert(url, reply); _currentDownloads.insert(url, reply);
ReplyTimeout::set(reply, TIMEOUT);
} else
return false;
return true; return true;
} }

View File

@ -7,7 +7,6 @@
#include <QMap> #include <QMap>
#include <QSet> #include <QSet>
class QNetworkReply; class QNetworkReply;
class Download class Download

View File

@ -3,7 +3,8 @@
#include <QString> #include <QString>
#include "tile.h" #include "tile.h"
#include "downloader.h"
class Downloader;
class TileLoader class TileLoader
{ {

View File

@ -3,6 +3,7 @@
#include "common/wgs84.h" #include "common/wgs84.h"
#include "config.h" #include "config.h"
#include "transform.h" #include "transform.h"
#include "downloader.h"
#include "wmts.h" #include "wmts.h"
#include "wmtsmap.h" #include "wmtsmap.h"