From 2353347f740032b27b376e954200782d8c26a17b Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 30 May 2018 16:29:29 +0200 Subject: [PATCH] PropagateDownload: Don't discard the body of error message We want to keep the body so we can get the message from it (Issue #6459) TestDownload::testErrorMessage did not fail because the FakeErrorReply did not emit readyRead and did not implement bytesAvailable. --- src/libsync/propagatedownload.cpp | 9 ++++++--- test/syncenginetestutils.h | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/libsync/propagatedownload.cpp b/src/libsync/propagatedownload.cpp index 56f635c1c6d..ae9ddf7a844 100644 --- a/src/libsync/propagatedownload.cpp +++ b/src/libsync/propagatedownload.cpp @@ -165,6 +165,9 @@ void GETFileJob::slotMetaDataChanged() // If the status code isn't 2xx, don't write the reply body to the file. // For any error: handle it when the job is finished, not here. if (httpStatus / 100 != 2) { + // Disable the buffer limit, as we don't limit the bandwidth for error messages. + // (We are only going to do a readAll() at the end.) + reply()->setReadBufferSize(0); _device->close(); return; } @@ -268,7 +271,7 @@ void GETFileJob::slotReadyRead() int bufferSize = qMin(1024 * 8ll, reply()->bytesAvailable()); QByteArray buffer(bufferSize, Qt::Uninitialized); - while (reply()->bytesAvailable() > 0) { + while (reply()->bytesAvailable() > 0 && _saveBodyToFile) { if (_bandwidthChoked) { qCWarning(lcGetJob) << "Download choked"; break; @@ -292,7 +295,7 @@ void GETFileJob::slotReadyRead() return; } - if (_device->isOpen() && _saveBodyToFile) { + if (_device->isOpen()) { qint64 w = _device->write(buffer.constData(), r); if (w != r) { _errorString = _device->errorString(); @@ -304,7 +307,7 @@ void GETFileJob::slotReadyRead() } } - if (reply()->isFinished() && reply()->bytesAvailable() == 0) { + if (reply()->isFinished() && (reply()->bytesAvailable() == 0 || !_saveBodyToFile)) { qCDebug(lcGetJob) << "Actually finished!"; if (_bandwidthManager) { _bandwidthManager->unregisterDownloadJob(this); diff --git a/test/syncenginetestutils.h b/test/syncenginetestutils.h index 1a78add5b29..3957479d609 100644 --- a/test/syncenginetestutils.h +++ b/test/syncenginetestutils.h @@ -728,6 +728,8 @@ class FakeErrorReply : public QNetworkReply setAttribute(QNetworkRequest::HttpStatusCodeAttribute, _httpErrorCode); setError(InternalServerError, "Internal Server Fake Error"); emit metaDataChanged(); + emit readyRead(); + setFinished(true); emit finished(); } @@ -738,6 +740,9 @@ class FakeErrorReply : public QNetworkReply _body = _body.mid(max); return max; } + qint64 bytesAvailable() const override { + return _body.size(); + } int _httpErrorCode; QByteArray _body;