properly accept redirections with new API.

This commit is contained in:
Martin Rotter 2021-03-31 20:40:25 +02:00
parent fecfe22e7d
commit a6f3206081
4 changed files with 20 additions and 25 deletions

@ -1 +1 @@
Subproject commit 47f4125753452eff8800dbd6600c5a05540b15d9
Subproject commit 9c10723bfbaf6cb85107d6ee16e0324e9e487749

View File

@ -32,15 +32,21 @@ void BaseNetworkAccessManager::loadSettings() {
qDebugNN << LOGSEC_NETWORK << "Settings of BaseNetworkAccessManager loaded.";
}
void BaseNetworkAccessManager::acceptRedirection(const QUrl& url) {
auto* reply = qobject_cast<QNetworkReply*>(sender());
emit reply->redirectAllowed();
qDebugNN << LOGSEC_NETWORK << "Accepting redirect to" << QUOTE_W_SPACE_DOT(url.toString());
}
void BaseNetworkAccessManager::onSslErrors(QNetworkReply* reply, const QList<QSslError>& error) {
qWarningNN << LOGSEC_NETWORK
<< "Ignoring SSL errors for '"
<< reply->url().toString()
<< "':"
<< "Ignoring SSL errors for"
<< QUOTE_W_SPACE(reply->url().toString())
<< ":"
<< QUOTE_W_SPACE(reply->errorString())
<< "(code "
<< reply->error()
<< ").";
<< "- code"
<< QUOTE_W_SPACE_DOT(reply->error());
reply->ignoreSslErrors(error);
}
@ -50,19 +56,19 @@ QNetworkReply* BaseNetworkAccessManager::createRequest(QNetworkAccessManager::Op
QNetworkRequest new_request = request;
new_request.setAttribute(QNetworkRequest::Attribute::HttpPipeliningAllowedAttribute, true);
new_request.setAttribute(QNetworkRequest::Attribute::Http2AllowedAttribute, true);
#if QT_VERSION >= 0x050900
new_request.setAttribute(QNetworkRequest::Attribute::RedirectPolicyAttribute,
QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy);
QNetworkRequest::RedirectPolicy::UserVerifiedRedirectPolicy);
#elif QT_VERSION >= 0x050600
new_request.setAttribute(QNetworkRequest::Attribute::FollowRedirectsAttribute, true);
#endif
new_request.setRawHeader(QSL("Cookie").toLocal8Bit(), QSL("JSESSIONID= ").toLocal8Bit());
// Setup custom user-agent.
new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, QString(APP_USERAGENT).toLocal8Bit());
new_request.setHeader(QNetworkRequest::KnownHeaders::CookieHeader, QSL("JSESSIONID= ").toLocal8Bit());
new_request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, QString(APP_USERAGENT).toLocal8Bit());
auto reply = QNetworkAccessManager::createRequest(op, new_request, outgoingData);
connect(reply, &QNetworkReply::redirected, this, &BaseNetworkAccessManager::acceptRedirection);
return reply;
}

View File

@ -13,19 +13,13 @@ class BaseNetworkAccessManager : public QNetworkAccessManager {
explicit BaseNetworkAccessManager(QObject* parent = nullptr);
public slots:
// Loads network settings for this instance.
// NOTE: This sets up proxy settings.
virtual void loadSettings();
void loadSettings();
protected slots:
// Called when some SSL-related errors are detected.
void acceptRedirection(const QUrl& url);
void onSslErrors(QNetworkReply* reply, const QList<QSslError>& error);
protected:
// Creates custom request.
QNetworkReply* createRequest(Operation op, const QNetworkRequest& request, QIODevice* outgoingData);
};

View File

@ -106,11 +106,6 @@ void Downloader::finished() {
m_timer->stop();
// In this phase, some part of downloading process is completed.
const QUrl redirection_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
// No redirection is indicated. Final file is obtained in our "reply" object.
// Read the data into output buffer.
if (m_inputMultipartData == nullptr) {
m_lastOutputData = reply->readAll();
}