From b98e7cb69fbe06e6e1178404cd851be39803b132 Mon Sep 17 00:00:00 2001 From: brvphoenix <30111323+brvphoenix@users.noreply.github.com> Date: Fri, 29 Jul 2022 11:03:21 +0800 Subject: [PATCH] Fix reply data can't be decompressed correctly If the "Accept-Encoding" is not manually specified, it will be automatically set to the supported encodings by QT and the reply data will also be automatically decompressed in this case. Setting "Accept-Encoding" manually will disable the "autodecompress" feature before QT 6.3.0. Although QT 6.3.x has different behaviors, let QT specify the "Accept-Encoding" and we will always obtain the decompressed data. The macro "QT_NO_COMPRESS" defined when QT is compiled will disable the zlib support. We can manually address this exceptions. PR #17438. --- src/base/net/downloadhandlerimpl.cpp | 8 ++++---- src/base/net/downloadmanager.cpp | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/base/net/downloadhandlerimpl.cpp b/src/base/net/downloadhandlerimpl.cpp index a578fa817..deac598c9 100644 --- a/src/base/net/downloadhandlerimpl.cpp +++ b/src/base/net/downloadhandlerimpl.cpp @@ -37,7 +37,7 @@ #include "base/utils/io.h" #include "base/utils/misc.h" -#if (QT_VERSION < QT_VERSION_CHECK(6, 3, 0)) +#ifdef QT_NO_COMPRESS #include "base/utils/gzip.h" #endif @@ -124,12 +124,12 @@ void DownloadHandlerImpl::processFinishedDownload() } // Success -#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0)) - m_result.data = m_reply->readAll(); -#else +#ifdef QT_NO_COMPRESS m_result.data = (m_reply->rawHeader("Content-Encoding") == "gzip") ? Utils::Gzip::decompress(m_reply->readAll()) : m_reply->readAll(); +#else + m_result.data = m_reply->readAll(); #endif if (m_downloadRequest.saveToFile()) diff --git a/src/base/net/downloadmanager.cpp b/src/base/net/downloadmanager.cpp index b11cee9cd..ac3022c6d 100644 --- a/src/base/net/downloadmanager.cpp +++ b/src/base/net/downloadmanager.cpp @@ -123,8 +123,12 @@ namespace // Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents request.setRawHeader("Referer", request.url().toEncoded().data()); - // Accept gzip +#ifdef QT_NO_COMPRESS + // The macro "QT_NO_COMPRESS" defined in QT will disable the zlib releated features + // and reply data auto-decompression in QT will also be disabled. But we can support + // gzip encoding and manually decompress the reply data. request.setRawHeader("Accept-Encoding", "gzip"); +#endif // Qt doesn't support Magnet protocol so we need to handle redirections manually request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);