From 3c778980df27429794ba8acbd7a429caf296bb4c Mon Sep 17 00:00:00 2001 From: Camila San Date: Sat, 3 Mar 2018 22:24:01 +0100 Subject: [PATCH] Makes sure JsonApiJob::finished won't throw a JSON error when status code is 304. Signed-off-by: Camila San --- src/gui/servernotificationhandler.cpp | 1 - src/libsync/networkjobs.cpp | 19 ++++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/gui/servernotificationhandler.cpp b/src/gui/servernotificationhandler.cpp index 385ac54d3..4368db612 100644 --- a/src/gui/servernotificationhandler.cpp +++ b/src/gui/servernotificationhandler.cpp @@ -63,7 +63,6 @@ void ServerNotificationHandler::slotFetchNotifications(AccountState *ptr) } void ServerNotificationHandler::slotEtagResponseHeaderReceived(const QByteArray &value, int statusCode){ - qCWarning(lcServerNotification) << "New Notification ETag Response Header received " << value; if(statusCode == successStatusCode){ qCWarning(lcServerNotification) << "New Notification ETag Response Header received " << value; AccountState *account = qvariant_cast(sender()->property(propertyAccountStateC)); diff --git a/src/libsync/networkjobs.cpp b/src/libsync/networkjobs.cpp index ff4f6126b..00e10bc0e 100644 --- a/src/libsync/networkjobs.cpp +++ b/src/libsync/networkjobs.cpp @@ -49,6 +49,7 @@ Q_LOGGING_CATEGORY(lcMkColJob, "sync.networkjob.mkcol", QtInfoMsg) Q_LOGGING_CATEGORY(lcProppatchJob, "sync.networkjob.proppatch", QtInfoMsg) Q_LOGGING_CATEGORY(lcJsonApiJob, "sync.networkjob.jsonapi", QtInfoMsg) Q_LOGGING_CATEGORY(lcDetermineAuthTypeJob, "sync.networkjob.determineauthtype", QtInfoMsg) +const int notModifiedStatusCode = 304; RequestEtagJob::RequestEtagJob(AccountPtr account, const QString &path, QObject *parent) : AbstractNetworkJob(account, path, parent) @@ -811,8 +812,9 @@ bool JsonApiJob::finished() << (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString()); int statusCode = 0; + int httpStatusCode = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if (reply()->error() != QNetworkReply::NoError) { - qCWarning(lcJsonApiJob) << "Network error: " << path() << errorString() << reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute); + qCWarning(lcJsonApiJob) << "Network error: " << path() << errorString() << httpStatusCode; emit jsonReceived(QJsonDocument(), statusCode); return true; } @@ -824,7 +826,9 @@ bool JsonApiJob::finished() // this is a error message coming back from ocs. statusCode = rex.cap(1).toInt(); } - + } else if(jsonStr.isEmpty() && httpStatusCode == notModifiedStatusCode){ + qCWarning(lcJsonApiJob) << "Nothing changed so nothing to retrieve - status code: " << httpStatusCode; + statusCode = httpStatusCode; } else { QRegExp rex("\"statuscode\":(\\d+),"); // example: "{"ocs":{"meta":{"status":"ok","statuscode":100,"message":null},"data":{"version":{"major":8,"minor":"... (504) @@ -833,18 +837,19 @@ bool JsonApiJob::finished() } } + // save new ETag value + if(reply()->rawHeaderList().contains("ETag")) + emit etagResponseHeaderReceived(reply()->rawHeader("ETag"), statusCode); + QJsonParseError error; auto json = QJsonDocument::fromJson(jsonStr.toUtf8(), &error); - // empty or invalid response - if (error.error != QJsonParseError::NoError || json.isNull()) { + // empty or invalid response and status code is != 304 because jsonStr is expected to be empty + if ((error.error != QJsonParseError::NoError || json.isNull()) && httpStatusCode != notModifiedStatusCode) { qCWarning(lcJsonApiJob) << "invalid JSON!" << jsonStr << error.errorString(); emit jsonReceived(json, statusCode); return true; } - if(reply()->rawHeaderList().contains("ETag")) - emit etagResponseHeaderReceived(reply()->rawHeader("ETag"), statusCode); - emit jsonReceived(json, statusCode); return true; }