This commit is contained in:
Martin Rotter 2021-06-28 08:00:33 +02:00
parent 6e51dd34b0
commit 868d75b182
4 changed files with 67 additions and 31 deletions

View File

@ -30,7 +30,7 @@
<url type="donation">https://martinrotter.github.io/donate/</url> <url type="donation">https://martinrotter.github.io/donate/</url>
<content_rating type="oars-1.1" /> <content_rating type="oars-1.1" />
<releases> <releases>
<release version="3.9.2" date="2021-06-25"/> <release version="3.9.2" date="2021-06-28"/>
</releases> </releases>
<content_rating type="oars-1.0"> <content_rating type="oars-1.0">
<content_attribute id="violence-cartoon">none</content_attribute> <content_attribute id="violence-cartoon">none</content_attribute>

View File

@ -32,13 +32,6 @@ void BaseNetworkAccessManager::loadSettings() {
qDebugNN << LOGSEC_NETWORK << "Settings of BaseNetworkAccessManager loaded."; 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) { void BaseNetworkAccessManager::onSslErrors(QNetworkReply* reply, const QList<QSslError>& error) {
qWarningNN << LOGSEC_NETWORK qWarningNN << LOGSEC_NETWORK
<< "Ignoring SSL errors for" << "Ignoring SSL errors for"
@ -60,17 +53,9 @@ QNetworkReply* BaseNetworkAccessManager::createRequest(QNetworkAccessManager::Op
new_request.setAttribute(QNetworkRequest::Attribute::Http2AllowedAttribute, true); new_request.setAttribute(QNetworkRequest::Attribute::Http2AllowedAttribute, true);
#endif #endif
#if QT_VERSION >= 0x050900 // Qt >= 5.9.0
new_request.setAttribute(QNetworkRequest::Attribute::RedirectPolicyAttribute,
QNetworkRequest::RedirectPolicy::UserVerifiedRedirectPolicy);
#elif QT_VERSION >= 0x050600 // Qt >= 5.6.0
new_request.setAttribute(QNetworkRequest::Attribute::FollowRedirectsAttribute, true);
#endif
new_request.setRawHeader(HTTP_HEADERS_COOKIE, QSL("JSESSIONID= ").toLocal8Bit()); new_request.setRawHeader(HTTP_HEADERS_COOKIE, QSL("JSESSIONID= ").toLocal8Bit());
new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, QString(APP_USERAGENT).toLocal8Bit()); new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, QString(APP_USERAGENT).toLocal8Bit());
auto reply = QNetworkAccessManager::createRequest(op, new_request, outgoingData); auto reply = QNetworkAccessManager::createRequest(op, new_request, outgoingData);
connect(reply, &QNetworkReply::redirected, this, &BaseNetworkAccessManager::acceptRedirection);
return reply; return reply;
} }

View File

@ -16,7 +16,6 @@ class BaseNetworkAccessManager : public QNetworkAccessManager {
void loadSettings(); void loadSettings();
protected slots: protected slots:
void acceptRedirection(const QUrl& url);
void onSslErrors(QNetworkReply* reply, const QList<QSslError>& error); void onSslErrors(QNetworkReply* reply, const QList<QSslError>& error);
protected: protected:

View File

@ -118,25 +118,77 @@ void Downloader::manipulateData(const QString& url,
void Downloader::finished() { void Downloader::finished() {
auto* reply = qobject_cast<QNetworkReply*>(sender()); auto* reply = qobject_cast<QNetworkReply*>(sender());
QNetworkAccessManager::Operation reply_operation = reply->operation();
m_timer->stop(); m_timer->stop();
if (m_inputMultipartData == nullptr) { // In this phase, some part of downloading process is completed.
m_lastOutputData = reply->readAll(); QUrl redirection_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (redirection_url.isValid()) {
// Communication indicates that HTTP redirection is needed.
// Setup redirection URL and download again.
QNetworkRequest request = reply->request();
qWarningNN << LOGSEC_NETWORK
<< "Network layer indicates HTTP redirection is needed.";
qWarningNN << LOGSEC_NETWORK
<< "Origin URL:"
<< QUOTE_W_SPACE_DOT(request.url().toString());
qWarningNN << LOGSEC_NETWORK
<< "Proposed redirection URL:"
<< QUOTE_W_SPACE_DOT(redirection_url.toString());
redirection_url = request.url().resolved(redirection_url);
qWarningNN << LOGSEC_NETWORK
<< "Resolved redirection URL:"
<< QUOTE_W_SPACE_DOT(redirection_url.toString());
request.setUrl(redirection_url);
m_activeReply->deleteLater();
m_activeReply = nullptr;
if (reply_operation == QNetworkAccessManager::GetOperation) {
runGetRequest(request);
}
else if (reply_operation == QNetworkAccessManager::PostOperation) {
if (m_inputMultipartData == nullptr) {
runPostRequest(request, m_inputData);
}
else {
runPostRequest(request, m_inputMultipartData);
}
}
else if (reply_operation == QNetworkAccessManager::PutOperation) {
runPutRequest(request, m_inputData);
}
else if (reply_operation == QNetworkAccessManager::DeleteOperation) {
runDeleteRequest(request);
}
} }
else { else {
m_lastOutputMultipartData = decodeMultipartAnswer(reply); // 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();
}
else {
m_lastOutputMultipartData = decodeMultipartAnswer(reply);
}
m_lastContentType = reply->header(QNetworkRequest::ContentTypeHeader);
m_lastOutputError = reply->error();
m_activeReply->deleteLater();
m_activeReply = nullptr;
if (m_inputMultipartData != nullptr) {
m_inputMultipartData->deleteLater();
}
emit completed(m_lastOutputError, m_lastOutputData);
} }
m_lastContentType = reply->header(QNetworkRequest::KnownHeaders::ContentTypeHeader);
m_lastOutputError = reply->error();
m_activeReply->deleteLater();
m_activeReply = nullptr;
if (m_inputMultipartData != nullptr) {
m_inputMultipartData->deleteLater();
}
emit completed(m_lastOutputError, m_lastOutputData);
} }
void Downloader::progressInternal(qint64 bytes_received, qint64 bytes_total) { void Downloader::progressInternal(qint64 bytes_received, qint64 bytes_total) {