// 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);
return;
}
if (reply()->error() != QNetworkReply::NoError) {
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;
return;
}
- qint64 w = _device->write(buffer.constData(), r);
- if (w != r) {
- _errorString = _device->errorString();
- _errorStatus = SyncFileItem::NormalError;
- qCWarning(lcGetJob) << "Error while writing to file" << w << r << _errorString;
- reply()->abort();
- return;
+ if (_device->isOpen()) {
+ qint64 w = _device->write(buffer.constData(), r);
+ if (w != r) {
+ _errorString = _device->errorString();
+ _errorStatus = SyncFileItem::NormalError;
+ qCWarning(lcGetJob) << "Error while writing to file" << w << r << _errorString;
+ reply()->abort();
+ return;
+ }
}
}
- if (reply()->isFinished() && reply()->bytesAvailable() == 0) {
+ if (reply()->isFinished() && (reply()->bytesAvailable() == 0 || !_saveBodyToFile)) {
qCDebug(lcGetJob) << "Actually finished!";
if (_bandwidthManager) {
_bandwidthManager->unregisterDownloadJob(this);
// finishing can come strictly after readyRead was called
QTimer::singleShot(5, this, &FakeErrorReply::slotSetFinished);
}
-
+
// make public to give tests easy interface
using QNetworkReply::setError;
using QNetworkReply::setAttribute;
_body = _body.mid(max);
return max;
}
+ qint64 bytesAvailable() const override {
+ return _body.size();
+ }
int _httpErrorCode;
QByteArray _body;