Use FollowRedirectsAttribute everywhere
This commit is contained in:
@@ -123,6 +123,10 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR
|
||||
}
|
||||
|
||||
QNetworkRequest new_request(request);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||
new_request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
||||
#endif
|
||||
|
||||
new_request.setRawHeader("User-Agent", user_agent);
|
||||
|
||||
if (op == QNetworkAccessManager::PostOperation && !new_request.header(QNetworkRequest::ContentTypeHeader).isValid()) {
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
|
||||
#include "core/closure.h"
|
||||
#include "networktimeouts.h"
|
||||
#include "redirectfollower.h"
|
||||
|
||||
NetworkTimeouts::NetworkTimeouts(int timeout_msec, QObject *parent)
|
||||
: QObject(parent), timeout_msec_(timeout_msec) {}
|
||||
@@ -44,18 +43,6 @@ void NetworkTimeouts::AddReply(QNetworkReply *reply) {
|
||||
|
||||
}
|
||||
|
||||
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_);
|
||||
|
||||
}
|
||||
|
||||
void NetworkTimeouts::ReplyFinished() {
|
||||
|
||||
QNetworkReply *reply = reinterpret_cast<QNetworkReply*>(sender());
|
||||
@@ -65,14 +52,6 @@ void NetworkTimeouts::ReplyFinished() {
|
||||
|
||||
}
|
||||
|
||||
void NetworkTimeouts::RedirectFinished(RedirectFollower *reply) {
|
||||
|
||||
if (redirect_timers_.contains(reply)) {
|
||||
killTimer(redirect_timers_.take(reply));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void NetworkTimeouts::timerEvent(QTimerEvent *e) {
|
||||
|
||||
QNetworkReply *reply = timers_.key(e->timerId());
|
||||
@@ -80,11 +59,5 @@ void NetworkTimeouts::timerEvent(QTimerEvent *e) {
|
||||
reply->abort();
|
||||
}
|
||||
|
||||
RedirectFollower *redirect = redirect_timers_.key(e->timerId());
|
||||
if (redirect) {
|
||||
redirect->abort();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
|
||||
class QNetworkReply;
|
||||
class QTimerEvent;
|
||||
class RedirectFollower;
|
||||
|
||||
class NetworkTimeouts : public QObject {
|
||||
Q_OBJECT
|
||||
@@ -39,9 +38,7 @@ class NetworkTimeouts : public QObject {
|
||||
public:
|
||||
explicit NetworkTimeouts(int timeout_msec, QObject *parent = nullptr);
|
||||
|
||||
// TODO: Template this to avoid code duplication.
|
||||
void AddReply(QNetworkReply *reply);
|
||||
void AddReply(RedirectFollower *reply);
|
||||
void SetTimeout(int msec) { timeout_msec_ = msec; }
|
||||
|
||||
protected:
|
||||
@@ -49,12 +46,11 @@ class NetworkTimeouts : public QObject {
|
||||
|
||||
private slots:
|
||||
void ReplyFinished();
|
||||
void RedirectFinished(RedirectFollower *redirect);
|
||||
|
||||
private:
|
||||
int timeout_msec_;
|
||||
QMap<QNetworkReply*, int> timers_;
|
||||
QMap<RedirectFollower*, int> redirect_timers_;
|
||||
|
||||
};
|
||||
|
||||
#endif // NETWORKTIMEOUTS_H
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#include "redirectfollower.h"
|
||||
|
||||
RedirectFollower::RedirectFollower(QNetworkReply *first_reply, int max_redirects) : QObject(nullptr), current_reply_(first_reply), redirects_remaining_(max_redirects) {
|
||||
ConnectReply(first_reply);
|
||||
}
|
||||
|
||||
void RedirectFollower::ConnectReply(QNetworkReply *reply) {
|
||||
|
||||
connect(reply, SIGNAL(readyRead()), SLOT(ReadyRead()));
|
||||
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)));
|
||||
connect(reply, SIGNAL(finished()), SLOT(ReplyFinished()));
|
||||
|
||||
}
|
||||
|
||||
void RedirectFollower::ReadyRead() {
|
||||
|
||||
// Don't re-emit this signal for redirect replies.
|
||||
if (current_reply_->attribute(QNetworkRequest::RedirectionTargetAttribute).isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
emit readyRead();
|
||||
|
||||
}
|
||||
|
||||
void RedirectFollower::ReplyFinished() {
|
||||
|
||||
current_reply_->deleteLater();
|
||||
|
||||
if (current_reply_->attribute(QNetworkRequest::RedirectionTargetAttribute).isValid()) {
|
||||
if (redirects_remaining_-- == 0) {
|
||||
emit finished();
|
||||
return;
|
||||
}
|
||||
|
||||
const QUrl next_url = current_reply_->url().resolved(current_reply_->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
|
||||
|
||||
QNetworkRequest req(current_reply_->request());
|
||||
req.setUrl(next_url);
|
||||
|
||||
current_reply_ = current_reply_->manager()->get(req);
|
||||
ConnectReply(current_reply_);
|
||||
return;
|
||||
}
|
||||
|
||||
emit finished();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef REDIRECTFOLLOWER_H
|
||||
#define REDIRECTFOLLOWER_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include <QVariant>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
|
||||
class RedirectFollower : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit RedirectFollower(QNetworkReply *first_reply, int max_redirects = 5);
|
||||
|
||||
bool hit_redirect_limit() const { return redirects_remaining_ < 0; }
|
||||
QNetworkReply *reply() const { return current_reply_; }
|
||||
|
||||
// These are all forwarded to the current reply.
|
||||
QNetworkReply::NetworkError error() const { return current_reply_->error(); }
|
||||
QString errorString() const { return current_reply_->errorString(); }
|
||||
QVariant attribute(QNetworkRequest::Attribute code) const { return current_reply_->attribute(code); }
|
||||
QVariant header(QNetworkRequest::KnownHeaders header) const { return current_reply_->header(header); }
|
||||
qint64 bytesAvailable() const { return current_reply_->bytesAvailable(); }
|
||||
QUrl url() const { return current_reply_->url(); }
|
||||
QByteArray readAll() { return current_reply_->readAll(); }
|
||||
void abort() { current_reply_->abort(); }
|
||||
|
||||
signals:
|
||||
// These are all forwarded from the current reply.
|
||||
void readyRead();
|
||||
void error(QNetworkReply::NetworkError);
|
||||
void uploadProgress(qint64 bytesSent, qint64 bytesTotal);
|
||||
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
||||
|
||||
// This is NOT emitted when a request that has a redirect finishes.
|
||||
void finished();
|
||||
|
||||
private slots:
|
||||
void ReadyRead();
|
||||
void ReplyFinished();
|
||||
|
||||
private:
|
||||
void ConnectReply(QNetworkReply *reply);
|
||||
|
||||
private:
|
||||
QNetworkReply *current_reply_;
|
||||
int redirects_remaining_;
|
||||
};
|
||||
|
||||
#endif // REDIRECTFOLLOWER_H
|
||||
Reference in New Issue
Block a user