eliminate redirection cycles

This commit is contained in:
Martin Rotter 2023-09-19 20:21:08 +02:00
parent 2a2af7ae1d
commit f1dd53eb8a
3 changed files with 25 additions and 0 deletions

View file

@ -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

View file

@ -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();

View file

@ -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<QNetworkReply*>(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 {