2010-10-16 19:20:54 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2010-2013, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2010-2012, 2014, John Maguire <john.maguire@gmail.com>
|
2014-11-02 19:40:52 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2010-10-16 19:20:54 +02:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkDiskCache>
|
2011-03-13 14:17:35 +01:00
|
|
|
#include <QNetworkReply>
|
2010-10-16 19:20:54 +02:00
|
|
|
|
2012-10-10 14:45:13 +02:00
|
|
|
#include "core/closure.h"
|
2010-12-09 13:34:08 +01:00
|
|
|
#include "utilities.h"
|
|
|
|
|
2010-10-16 19:20:54 +02:00
|
|
|
QMutex ThreadSafeNetworkDiskCache::sMutex;
|
2014-02-06 16:49:49 +01:00
|
|
|
QNetworkDiskCache* ThreadSafeNetworkDiskCache::sCache = nullptr;
|
2010-10-16 19:20:54 +02:00
|
|
|
|
|
|
|
ThreadSafeNetworkDiskCache::ThreadSafeNetworkDiskCache(QObject* parent) {
|
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
if (!sCache) {
|
|
|
|
sCache = new QNetworkDiskCache;
|
2014-02-07 16:34:20 +01:00
|
|
|
sCache->setCacheDirectory(
|
|
|
|
Utilities::GetConfigPath(Utilities::Path_NetworkCache));
|
2010-10-16 19:20:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 ThreadSafeNetworkDiskCache::cacheSize() const {
|
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
return sCache->cacheSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
QIODevice* ThreadSafeNetworkDiskCache::data(const QUrl& url) {
|
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
return sCache->data(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadSafeNetworkDiskCache::insert(QIODevice* device) {
|
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
sCache->insert(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkCacheMetaData ThreadSafeNetworkDiskCache::metaData(const QUrl& url) {
|
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
return sCache->metaData(url);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QIODevice* ThreadSafeNetworkDiskCache::prepare(
|
|
|
|
const QNetworkCacheMetaData& metaData) {
|
2010-10-16 19:20:54 +02:00
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
return sCache->prepare(metaData);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadSafeNetworkDiskCache::remove(const QUrl& url) {
|
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
return sCache->remove(url);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void ThreadSafeNetworkDiskCache::updateMetaData(
|
|
|
|
const QNetworkCacheMetaData& metaData) {
|
2010-10-16 19:20:54 +02:00
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
sCache->updateMetaData(metaData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadSafeNetworkDiskCache::clear() {
|
|
|
|
QMutexLocker l(&sMutex);
|
|
|
|
sCache->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkAccessManager::NetworkAccessManager(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QNetworkAccessManager(parent) {
|
2010-10-16 19:20:54 +02:00
|
|
|
setCache(new ThreadSafeNetworkDiskCache(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply* NetworkAccessManager::createRequest(
|
|
|
|
Operation op, const QNetworkRequest& request, QIODevice* outgoingData) {
|
2014-02-07 16:34:20 +01:00
|
|
|
QByteArray user_agent = QString("%1 %2")
|
|
|
|
.arg(QCoreApplication::applicationName(),
|
|
|
|
QCoreApplication::applicationVersion())
|
|
|
|
.toUtf8();
|
2013-04-15 13:22:15 +02:00
|
|
|
|
|
|
|
if (request.hasRawHeader("User-Agent")) {
|
|
|
|
// Append the existing user-agent set by a client library (such as
|
|
|
|
// libmygpo-qt).
|
|
|
|
user_agent += " " + request.rawHeader("User-Agent");
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkRequest new_request(request);
|
|
|
|
new_request.setRawHeader("User-Agent", user_agent);
|
2010-11-27 17:14:09 +01:00
|
|
|
|
2011-11-10 23:42:34 +01:00
|
|
|
if (op == QNetworkAccessManager::PostOperation &&
|
|
|
|
!new_request.header(QNetworkRequest::ContentTypeHeader).isValid()) {
|
|
|
|
new_request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
}
|
|
|
|
|
2010-11-27 17:14:09 +01:00
|
|
|
// Prefer the cache unless the caller has changed the setting already
|
2014-02-07 16:34:20 +01:00
|
|
|
if (request.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt() ==
|
|
|
|
QNetworkRequest::PreferNetwork) {
|
2010-11-27 17:14:09 +01:00
|
|
|
new_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
|
|
|
|
QNetworkRequest::PreferCache);
|
|
|
|
}
|
2010-10-16 19:20:54 +02:00
|
|
|
|
|
|
|
return QNetworkAccessManager::createRequest(op, new_request, outgoingData);
|
|
|
|
}
|
2011-03-13 14:17:35 +01:00
|
|
|
|
|
|
|
NetworkTimeouts::NetworkTimeouts(int timeout_msec, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: timeout_msec_(timeout_msec) {}
|
2011-03-13 14:17:35 +01:00
|
|
|
|
|
|
|
void NetworkTimeouts::AddReply(QNetworkReply* reply) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (timers_.contains(reply)) return;
|
2011-03-13 14:17:35 +01:00
|
|
|
|
|
|
|
connect(reply, SIGNAL(destroyed()), SLOT(ReplyFinished()));
|
|
|
|
connect(reply, SIGNAL(finished()), SLOT(ReplyFinished()));
|
|
|
|
timers_[reply] = startTimer(timeout_msec_);
|
|
|
|
}
|
|
|
|
|
2012-10-10 14:45:13 +02:00
|
|
|
void NetworkTimeouts::AddReply(RedirectFollower* reply) {
|
|
|
|
if (redirect_timers_.contains(reply)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NewClosure(reply, SIGNAL(destroyed()), this,
|
|
|
|
SLOT(RedirectFinished(RedirectFollower*)), reply);
|
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(RedirectFinished(RedirectFollower*)), reply);
|
|
|
|
redirect_timers_[reply] = startTimer(timeout_msec_);
|
|
|
|
}
|
|
|
|
|
2011-03-13 14:17:35 +01:00
|
|
|
void NetworkTimeouts::ReplyFinished() {
|
|
|
|
QNetworkReply* reply = reinterpret_cast<QNetworkReply*>(sender());
|
|
|
|
if (timers_.contains(reply)) {
|
|
|
|
killTimer(timers_.take(reply));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-10 14:45:13 +02:00
|
|
|
void NetworkTimeouts::RedirectFinished(RedirectFollower* reply) {
|
|
|
|
if (redirect_timers_.contains(reply)) {
|
|
|
|
killTimer(redirect_timers_.take(reply));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-13 14:17:35 +01:00
|
|
|
void NetworkTimeouts::timerEvent(QTimerEvent* e) {
|
|
|
|
QNetworkReply* reply = timers_.key(e->timerId());
|
|
|
|
if (reply) {
|
|
|
|
reply->abort();
|
|
|
|
}
|
2012-10-10 14:45:13 +02:00
|
|
|
|
|
|
|
RedirectFollower* redirect = redirect_timers_.key(e->timerId());
|
|
|
|
if (redirect) {
|
|
|
|
redirect->abort();
|
|
|
|
}
|
2011-03-13 14:17:35 +01:00
|
|
|
}
|
2012-03-10 16:32:36 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
RedirectFollower::RedirectFollower(QNetworkReply* first_reply,
|
|
|
|
int max_redirects)
|
|
|
|
: QObject(nullptr),
|
|
|
|
current_reply_(first_reply),
|
|
|
|
redirects_remaining_(max_redirects) {
|
2012-03-10 16:32:36 +01:00
|
|
|
ConnectReply(first_reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedirectFollower::ConnectReply(QNetworkReply* reply) {
|
2012-06-14 14:50:49 +02:00
|
|
|
connect(reply, SIGNAL(readyRead()), SLOT(ReadyRead()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
SIGNAL(error(QNetworkReply::NetworkError)));
|
|
|
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
|
|
|
|
SIGNAL(downloadProgress(qint64, qint64)));
|
|
|
|
connect(reply, SIGNAL(uploadProgress(qint64, qint64)),
|
|
|
|
SIGNAL(uploadProgress(qint64, qint64)));
|
2012-03-10 16:32:36 +01:00
|
|
|
connect(reply, SIGNAL(finished()), SLOT(ReplyFinished()));
|
|
|
|
}
|
|
|
|
|
2012-06-14 14:50:49 +02:00
|
|
|
void RedirectFollower::ReadyRead() {
|
|
|
|
// Don't re-emit this signal for redirect replies.
|
2014-02-07 16:34:20 +01:00
|
|
|
if (current_reply_->attribute(QNetworkRequest::RedirectionTargetAttribute)
|
|
|
|
.isValid()) {
|
2012-06-14 14:50:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-10-10 14:45:13 +02:00
|
|
|
|
2012-06-14 14:50:49 +02:00
|
|
|
emit readyRead();
|
|
|
|
}
|
|
|
|
|
2012-03-10 16:32:36 +01:00
|
|
|
void RedirectFollower::ReplyFinished() {
|
|
|
|
current_reply_->deleteLater();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (current_reply_->attribute(QNetworkRequest::RedirectionTargetAttribute)
|
|
|
|
.isValid()) {
|
2012-03-10 16:32:36 +01:00
|
|
|
if (redirects_remaining_-- == 0) {
|
|
|
|
emit finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QUrl next_url = current_reply_->url().resolved(
|
2014-02-07 16:34:20 +01:00
|
|
|
current_reply_->attribute(QNetworkRequest::RedirectionTargetAttribute)
|
|
|
|
.toUrl());
|
2012-03-10 16:32:36 +01:00
|
|
|
|
|
|
|
QNetworkRequest req(current_reply_->request());
|
|
|
|
req.setUrl(next_url);
|
|
|
|
|
|
|
|
current_reply_ = current_reply_->manager()->get(req);
|
|
|
|
ConnectReply(current_reply_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit finished();
|
|
|
|
}
|