From f1dd53eb8a27615ec6d581d07d8cbac2e6f3dd06 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Tue, 19 Sep 2023 20:21:08 +0200 Subject: [PATCH] eliminate redirection cycles --- src/librssguard/definitions/definitions.h | 1 + .../network-web/basenetworkaccessmanager.cpp | 2 ++ src/librssguard/network-web/downloader.cpp | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/librssguard/definitions/definitions.h b/src/librssguard/definitions/definitions.h index d4e347dbd..826b45765 100644 --- a/src/librssguard/definitions/definitions.h +++ b/src/librssguard/definitions/definitions.h @@ -99,6 +99,7 @@ #define DEFAULT_NOTIFICATION_VOLUME 50 #define MAX_THREADPOOL_THREADS 32 #define WEB_BROWSER_SCROLL_STEP 50.0 +#define MAX_NUMBER_OF_REDIRECTIONS 4 #define NOTIFICATIONS_MARGIN 16 #define NOTIFICATIONS_WIDTH 256 diff --git a/src/librssguard/network-web/basenetworkaccessmanager.cpp b/src/librssguard/network-web/basenetworkaccessmanager.cpp index 294e7bf63..e1d18cd3a 100644 --- a/src/librssguard/network-web/basenetworkaccessmanager.cpp +++ b/src/librssguard/network-web/basenetworkaccessmanager.cpp @@ -63,6 +63,8 @@ QNetworkReply* BaseNetworkAccessManager::createRequest(QNetworkAccessManager::Op new_request.setAttribute(QNetworkRequest::Attribute::Http2AllowedAttribute, m_enableHttp2); #endif + // new_request.setMaximumRedirectsAllowed(0); + new_request.setRawHeader(HTTP_HEADERS_COOKIE, QSL("JSESSIONID= ").toLocal8Bit()); auto custom_ua = qApp->web()->customUserAgent(); diff --git a/src/librssguard/network-web/downloader.cpp b/src/librssguard/network-web/downloader.cpp index 4693595b2..472beb3f5 100644 --- a/src/librssguard/network-web/downloader.cpp +++ b/src/librssguard/network-web/downloader.cpp @@ -134,6 +134,14 @@ void Downloader::manipulateData(const QString& url, } } +static int numberOfRedirections(QNetworkReply* reply) { + return reply->property("redirections_count").toInt(); +} + +static int setNumberOfRedirections(QNetworkReply* reply, int number) { + return reply->setProperty("redirections_count", number); +} + void Downloader::finished() { auto* reply = qobject_cast(sender()); @@ -151,6 +159,19 @@ void Downloader::finished() { QUrl redirection_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); if (redirection_url.isValid()) { + auto redir_number = numberOfRedirections(reply); + + qDebugNN << LOGSEC_NETWORK << "This network request was redirected" << QUOTE_W_SPACE(redir_number) << "times."; + + redir_number++; + + if (redir_number > MAX_NUMBER_OF_REDIRECTIONS) { + qDebugNN << LOGSEC_NETWORK << "Aborting request due too many redirections."; + + emit completed(redirection_url, QNetworkReply::NetworkError::TooManyRedirectsError, 404, {}); + return; + } + // Communication indicates that HTTP redirection is needed. // Setup redirection URL and download again. QNetworkRequest request = reply->request(); @@ -188,6 +209,7 @@ void Downloader::finished() { if (m_activeReply != nullptr) { m_activeReply->setProperty("original_url", original_url); + setNumberOfRedirections(m_activeReply, redir_number); } } else {