Skip to content

Commit

Permalink
PropagateDownload: Don't discard the body of error message
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ogoffart committed May 31, 2018
1 parent 40d432e commit 2353347
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/libsync/propagatedownload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions test/syncenginetestutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
Expand Down

0 comments on commit 2353347

Please sign in to comment.