2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:10:03 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
2019-08-20 23:31:23 +02:00
|
|
|
#include <QCoreApplication>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QIODevice>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QByteArray>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QNetworkAccessManager>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QNetworkRequest>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "networkaccessmanager.h"
|
2020-12-09 18:41:07 +01:00
|
|
|
#include "threadsafenetworkdiskcache.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
NetworkAccessManager::NetworkAccessManager(QObject *parent)
|
|
|
|
: QNetworkAccessManager(parent) {
|
2019-08-20 23:31:23 +02:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
|
|
|
|
setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
#endif
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
setCache(new ThreadSafeNetworkDiskCache(this));
|
2019-08-20 23:31:23 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData) {
|
|
|
|
|
2019-09-22 17:05:26 +02:00
|
|
|
QByteArray user_agent;
|
2018-02-27 18:06:05 +01:00
|
|
|
if (request.hasRawHeader("User-Agent")) {
|
2019-09-22 17:05:26 +02:00
|
|
|
user_agent = request.rawHeader("User-Agent");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
user_agent = QString("%1 %2").arg(QCoreApplication::applicationName(), QCoreApplication::applicationVersion()).toUtf8();
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkRequest new_request(request);
|
2020-08-14 20:20:41 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
|
|
|
|
new_request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
#else
|
2019-08-22 19:28:54 +02:00
|
|
|
new_request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
2020-08-14 20:20:41 +02:00
|
|
|
#endif
|
2018-02-27 18:06:05 +01:00
|
|
|
new_request.setRawHeader("User-Agent", user_agent);
|
|
|
|
|
2018-04-06 22:13:11 +02:00
|
|
|
if (op == QNetworkAccessManager::PostOperation && !new_request.header(QNetworkRequest::ContentTypeHeader).isValid()) {
|
|
|
|
new_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer the cache unless the caller has changed the setting already
|
|
|
|
if (request.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt() == QNetworkRequest::PreferNetwork) {
|
|
|
|
new_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QNetworkAccessManager::createRequest(op, new_request, outgoingData);
|
2020-12-09 18:41:07 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|