QNetworkReply: honor legacy behavior of setting Accept-Encoding

And don't decompress the data

Fixes: QTBUG-106689
Change-Id: I93a96be8178e24ff0b0bb8647276828161445cf5
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
(cherry picked from commit 1fa0e86995bfdf9c0507fcd097fce712554a769e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mårten Nordheim 2022-09-19 12:39:42 +02:00 committed by Qt Cherry-pick Bot
parent 7b8771d618
commit 8f1fc6c2c4
2 changed files with 57 additions and 13 deletions

View File

@ -1329,6 +1329,11 @@ void QNetworkReplyHttpImplPrivate::replyDownloadMetaData(const QList<QPair<QByte
q->setAttribute(QNetworkRequest::HttpPipeliningWasUsedAttribute, pu);
q->setAttribute(QNetworkRequest::Http2WasUsedAttribute, h2Used);
// A user having manually defined which encodings they accept is, for
// somwehat unknown (presumed legacy compatibility) reasons treated as
// disabling our decompression:
const bool autoDecompress = request.rawHeader("accept-encoding").isEmpty();
const bool shouldDecompress = isCompressed && autoDecompress;
// reconstruct the HTTP header
QList<QPair<QByteArray, QByteArray> > headerMap = hm;
QList<QPair<QByteArray, QByteArray> >::ConstIterator it = headerMap.constBegin(),
@ -1342,7 +1347,7 @@ void QNetworkReplyHttpImplPrivate::replyDownloadMetaData(const QList<QPair<QByte
if (it->first.toLower() == "location")
value.clear();
if (isCompressed && !decompressHelper.isValid()
if (shouldDecompress && !decompressHelper.isValid()
&& it->first.compare("content-encoding", Qt::CaseInsensitive) == 0) {
if (!synchronous) // with synchronous all the data is expected to be handled at once

View File

@ -9669,25 +9669,53 @@ void tst_QNetworkReply::contentEncoding_data()
QTest::addColumn<QByteArray>("encoding");
QTest::addColumn<QByteArray>("body");
QTest::addColumn<QByteArray>("expected");
QTest::addColumn<bool>("decompress");
const QByteArray helloWorld = "hello world";
const QByteArray gzipBody = QByteArray::fromBase64("H4sIAAAAAAAAA8tIzcnJVyjPL8pJAQCFEUoNCwAAAA==");
QTest::newRow("gzip-hello-world")
<< QByteArray("gzip")
<< QByteArray::fromBase64("H4sIAAAAAAAAA8tIzcnJVyjPL8pJAQCFEUoNCwAAAA==")
<< QByteArray("hello world");
<< gzipBody
<< helloWorld
<< true;
QTest::newRow("gzip-hello-world-no-decompress")
<< QByteArray("gzip")
<< gzipBody
<< helloWorld
<< false;
const QByteArray deflateBody = QByteArray::fromBase64("eJzLSM3JyVcozy/KSQEAGgsEXQ==");
QTest::newRow("deflate-hello-world")
<< QByteArray("deflate") << QByteArray::fromBase64("eJzLSM3JyVcozy/KSQEAGgsEXQ==")
<< QByteArray("hello world");
<< QByteArray("deflate") << deflateBody
<< helloWorld
<< true;
QTest::newRow("deflate-hello-world-no-decompress")
<< QByteArray("deflate") << deflateBody
<< helloWorld
<< false;
#if QT_CONFIG(brotli)
const QByteArray brotliBody = QByteArray::fromBase64("DwWAaGVsbG8gd29ybGQD");
QTest::newRow("brotli-hello-world")
<< QByteArray("br") << QByteArray::fromBase64("DwWAaGVsbG8gd29ybGQD")
<< QByteArray("hello world");
<< QByteArray("br") << brotliBody
<< helloWorld
<< true;
QTest::newRow("brotli-hello-world-no-decompress")
<< QByteArray("br") << brotliBody
<< helloWorld
<< false;
#endif
#if defined(QT_BUILD_INTERNAL) && QT_CONFIG(zstd)
const QByteArray zstdBody = QByteArray::fromBase64("KLUv/QRYWQAAaGVsbG8gd29ybGRoaR6y");
QTest::newRow("zstandard-hello-world")
<< QByteArray("zstd") << QByteArray::fromBase64("KLUv/QRYWQAAaGVsbG8gd29ybGRoaR6y")
<< QByteArray("hello world");
<< QByteArray("zstd") << zstdBody
<< helloWorld
<< true;
QTest::newRow("zstandard-hello-world-no-decompress")
<< QByteArray("zstd") << zstdBody
<< helloWorld
<< false;
#else
qDebug("Note: ZStandard testdata is only available for developer builds.");
#endif
@ -9697,12 +9725,19 @@ void tst_QNetworkReply::contentEncoding()
{
QFETCH(QByteArray, encoding);
QFETCH(QByteArray, body);
QFETCH(bool, decompress);
QString header("HTTP/1.0 200 OK\r\nContent-Encoding: %1\r\nContent-Length: %2\r\n\r\n");
header = header.arg(encoding, QString::number(body.size()));
MiniHttpServer server(header.toLatin1() + body);
QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
if (!decompress) {
// This disables decompression of the received content:
request.setRawHeader("Accept-Encoding", QLatin1String("%1").arg(encoding).toLatin1());
// This disables the zerocopy optimization
request.setAttribute(QNetworkRequest::MaximumDownloadBufferSizeAttribute, 0);
}
QNetworkReplyPtr reply(manager.get(request));
QVERIFY2(waitForFinish(reply) == Success, msgWaitForFinished(reply));
@ -9723,10 +9758,14 @@ void tst_QNetworkReply::contentEncoding()
QVERIFY2(list.contains(encoding), acceptedEncoding.data());
}
QFETCH(QByteArray, expected);
QCOMPARE(reply->bytesAvailable(), expected.size());
QCOMPARE(reply->readAll(), expected);
if (decompress) {
QFETCH(QByteArray, expected);
QCOMPARE(reply->bytesAvailable(), expected.size());
QCOMPARE(reply->readAll(), expected);
} else {
QCOMPARE(reply->bytesAvailable(), body.size());
QCOMPARE(reply->readAll(), body);
}
}
void tst_QNetworkReply::contentEncodingBigPayload_data()