From a584bfaaa663347270782bccf9309ff016eea530 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 2 Apr 2018 15:21:10 +0900 Subject: BaseJob: set the status upon headers arrival So that DownloadFileJob could figure if it should save the incoming payload to the file or it's the JSON details about the error. --- lib/jobs/basejob.cpp | 24 +++++++++++++++--------- lib/jobs/basejob.h | 3 ++- lib/jobs/downloadfilejob.cpp | 20 +++++++++++--------- 3 files changed, 28 insertions(+), 19 deletions(-) (limited to 'lib/jobs') diff --git a/lib/jobs/basejob.cpp b/lib/jobs/basejob.cpp index a23f43b3..4e35cb01 100644 --- a/lib/jobs/basejob.cpp +++ b/lib/jobs/basejob.cpp @@ -242,6 +242,8 @@ void BaseJob::sendRequest() connect( d->reply.data(), &QNetworkReply::finished, this, &BaseJob::gotReply ); if (d->reply->isRunning()) { + connect( d->reply.data(), &QNetworkReply::metaDataChanged, + this, &BaseJob::checkReply); connect( d->reply.data(), &QNetworkReply::uploadProgress, this, &BaseJob::uploadProgress); connect( d->reply.data(), &QNetworkReply::downloadProgress, @@ -254,13 +256,22 @@ void BaseJob::sendRequest() qCWarning(d->logCat) << this << "request could not start"; } +void BaseJob::checkReply() +{ + setStatus(doCheckReply(d->reply.data())); +} + void BaseJob::gotReply() { - setStatus(checkReply(d->reply.data())); + checkReply(); + qCDebug(d->logCat).nospace().noquote() << this << " returned HTTP code " + << d->reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() + << ": " << (d->reply->error() == QNetworkReply::NoError ? + "Success" : d->reply->errorString()) + << " (URL: " << d->reply->url().toDisplayString() << ")"; if (status().good()) setStatus(parseReply(d->reply.data())); - else - { + else { const auto body = d->reply->readAll(); if (!body.isEmpty()) { @@ -324,15 +335,10 @@ bool checkContentType(const QByteArray& type, const QByteArrayList& patterns) return false; } -BaseJob::Status BaseJob::checkReply(QNetworkReply* reply) const +BaseJob::Status BaseJob::doCheckReply(QNetworkReply* reply) const { const auto httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - qCDebug(d->logCat).nospace().noquote() << this << " returned HTTP code " - << httpCode << ": " - << (reply->error() == QNetworkReply::NoError ? - "Success" : reply->errorString()) - << " (URL: " << reply->url().toDisplayString() << ")"; if (httpCode == 429) // Qt doesn't know about it yet return { TooManyRequestsError, tr("Too many requests") }; diff --git a/lib/jobs/basejob.h b/lib/jobs/basejob.h index 252b8ea0..f243066f 100644 --- a/lib/jobs/basejob.h +++ b/lib/jobs/basejob.h @@ -248,7 +248,7 @@ namespace QMatrixClient * * @see gotReply */ - virtual Status checkReply(QNetworkReply* reply) const; + virtual Status doCheckReply(QNetworkReply* reply) const; /** * Processes the reply. By default, parses the reply into @@ -286,6 +286,7 @@ namespace QMatrixClient private slots: void sendRequest(); + void checkReply(); void gotReply(); private: diff --git a/lib/jobs/downloadfilejob.cpp b/lib/jobs/downloadfilejob.cpp index 6a3d8483..2bf9dd8f 100644 --- a/lib/jobs/downloadfilejob.cpp +++ b/lib/jobs/downloadfilejob.cpp @@ -62,6 +62,8 @@ void DownloadFileJob::beforeStart(const ConnectionData*) void DownloadFileJob::afterStart(const ConnectionData*, QNetworkReply* reply) { connect(reply, &QNetworkReply::metaDataChanged, this, [this,reply] { + if (!status().good()) + return; auto sizeHeader = reply->header(QNetworkRequest::ContentLengthHeader); if (sizeHeader.isValid()) { @@ -77,15 +79,15 @@ void DownloadFileJob::afterStart(const ConnectionData*, QNetworkReply* reply) } }); connect(reply, &QIODevice::readyRead, this, [this,reply] { - auto bytes = reply->read(reply->bytesAvailable()); - if (bytes.isEmpty()) - { - qCWarning(JOBS) - << "Unexpected empty chunk when downloading from" - << reply->url() << "to" << d->tempFile->fileName(); - } else { - d->tempFile->write(bytes); - } + if (!status().good()) + return; + auto bytes = reply->read(reply->bytesAvailable()); + if (!bytes.isEmpty()) + d->tempFile->write(bytes); + else + qCWarning(JOBS) + << "Unexpected empty chunk when downloading from" + << reply->url() << "to" << d->tempFile->fileName(); }); } -- cgit v1.2.3