summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorBe <be@mixxx.org>2020-12-11 14:46:10 -0600
committerBe <be@mixxx.org>2020-12-11 14:46:10 -0600
commitfaaf65377663a19389d803b9863596fd74f9c25b (patch)
tree8d876ebdda9ec09bfe3562a0009f8e79e3ba656a /src/network
parentbea2be9ae78ddb5e8474072dbe6746befb575817 (diff)
parent418e5f4a46baa33eaf931f14c6c6c39ef03d3f9b (diff)
Merge remote-tracking branch 'upstream/2.3' into main
Diffstat (limited to 'src/network')
-rw-r--r--src/network/jsonwebtask.cpp22
-rw-r--r--src/network/webtask.cpp102
-rw-r--r--src/network/webtask.h2
3 files changed, 85 insertions, 41 deletions
diff --git a/src/network/jsonwebtask.cpp b/src/network/jsonwebtask.cpp
index d959149aa3..ebefc20560 100644
--- a/src/network/jsonwebtask.cpp
+++ b/src/network/jsonwebtask.cpp
@@ -138,6 +138,7 @@ JsonWebTask::~JsonWebTask() {
void JsonWebTask::onFinished(
JsonWebResponse&& response) {
kLogger.info()
+ << this
<< "Response received"
<< response.replyUrl
<< response.statusCode
@@ -148,6 +149,7 @@ void JsonWebTask::onFinished(
void JsonWebTask::onFinishedCustom(
CustomWebResponse&& response) {
kLogger.info()
+ << this
<< "Custom response received"
<< response.replyUrl
<< response.statusCode
@@ -165,6 +167,7 @@ QNetworkReply* JsonWebTask::sendNetworkRequest(
DEBUG_ASSERT(m_request.content.isEmpty());
if (kLogger.debugEnabled()) {
kLogger.debug()
+ << this
<< "GET"
<< url;
}
@@ -175,6 +178,7 @@ QNetworkReply* JsonWebTask::sendNetworkRequest(
const auto body = content.toJson(QJsonDocument::Compact);
if (kLogger.debugEnabled()) {
kLogger.debug()
+ << this
<< "PUT"
<< url
<< body;
@@ -187,6 +191,7 @@ QNetworkReply* JsonWebTask::sendNetworkRequest(
const auto body = content.toJson(QJsonDocument::Compact);
if (kLogger.debugEnabled()) {
kLogger.debug()
+ << this
<< "POST"
<< url
<< body;
@@ -199,6 +204,7 @@ QNetworkReply* JsonWebTask::sendNetworkRequest(
const auto body = m_request.content.toJson(QJsonDocument::Compact);
if (kLogger.debugEnabled()) {
kLogger.debug()
+ << this
<< "PATCH"
<< url
<< body;
@@ -212,6 +218,7 @@ QNetworkReply* JsonWebTask::sendNetworkRequest(
DEBUG_ASSERT(content.isEmpty());
if (kLogger.debugEnabled()) {
kLogger.debug()
+ << this
<< "DELETE"
<< url;
}
@@ -231,6 +238,7 @@ bool JsonWebTask::doStart(
DEBUG_ASSERT(networkAccessManager);
VERIFY_OR_DEBUG_ASSERT(!m_pendingNetworkReply) {
kLogger.warning()
+ << this
<< "Task has already been started";
return false;
}
@@ -250,26 +258,19 @@ bool JsonWebTask::doStart(
m_request.content);
VERIFY_OR_DEBUG_ASSERT(m_pendingNetworkReply) {
kLogger.warning()
+ << this
<< "Request not sent";
return false;
}
+ // It is not necessary to connect the QNetworkReply::errorOccurred signal.
+ // Network errors are also received through the QNetworkReply::finished signal.
connect(m_pendingNetworkReply,
&QNetworkReply::finished,
this,
&JsonWebTask::slotNetworkReplyFinished,
Qt::UniqueConnection);
- connect(m_pendingNetworkReply,
-#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
- &QNetworkReply::errorOccurred,
-#else
- QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
-#endif
- this,
- &JsonWebTask::slotNetworkReplyFinished,
- Qt::UniqueConnection);
-
return true;
}
@@ -337,6 +338,7 @@ void JsonWebTask::emitFailed(
VERIFY_OR_DEBUG_ASSERT(
isSignalFuncConnected(&JsonWebTask::failed)) {
kLogger.warning()
+ << this
<< "Unhandled failed signal"
<< response;
deleteLater();
diff --git a/src/network/webtask.cpp b/src/network/webtask.cpp
index 534c582b22..b1481df2d6 100644
--- a/src/network/webtask.cpp
+++ b/src/network/webtask.cpp
@@ -92,10 +92,14 @@ WebTask::~WebTask() {
void WebTask::onAborted(
QUrl&& requestUrl) {
- DEBUG_ASSERT(m_status == Status::Aborted);
+ VERIFY_OR_DEBUG_ASSERT(m_status == Status::Aborting) {
+ return;
+ }
+ m_status = Status::Aborted;
VERIFY_OR_DEBUG_ASSERT(
isSignalFuncConnected(&WebTask::aborted)) {
kLogger.warning()
+ << this
<< "Unhandled abort signal"
<< requestUrl;
deleteLater();
@@ -107,7 +111,13 @@ void WebTask::onAborted(
void WebTask::onTimedOut(
QUrl&& requestUrl) {
- DEBUG_ASSERT(m_status == Status::TimedOut);
+ VERIFY_OR_DEBUG_ASSERT(m_status == Status::Pending) {
+ return;
+ }
+ if (m_timeoutTimerId != kInvalidTimerId) {
+ killTimer(m_timeoutTimerId);
+ m_timeoutTimerId = kInvalidTimerId;
+ }
onNetworkError(
std::move(requestUrl),
QNetworkReply::TimeoutError,
@@ -120,10 +130,21 @@ void WebTask::onNetworkError(
QNetworkReply::NetworkError errorCode,
QString&& errorString,
QByteArray&& errorContent) {
+ DEBUG_ASSERT(m_timeoutTimerId == kInvalidTimerId);
+ VERIFY_OR_DEBUG_ASSERT(m_status == Status::Pending) {
+ return;
+ }
+ DEBUG_ASSERT(errorCode != QNetworkReply::NoError);
+ if (errorCode == QNetworkReply::TimeoutError) {
+ m_status = Status::TimedOut;
+ } else {
+ m_status = Status::Failed;
+ }
VERIFY_OR_DEBUG_ASSERT(
isSignalFuncConnected(&WebTask::networkError)) {
kLogger.warning()
- << "Unhandled network error signal"
+ << this
+ << "Unhandled network error:"
<< requestUrl
<< errorCode
<< errorString
@@ -168,8 +189,13 @@ void WebTask::invokeAbort() {
void WebTask::slotStart(int timeoutMillis) {
DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this);
- DEBUG_ASSERT(m_status != Status::Pending);
+ VERIFY_OR_DEBUG_ASSERT(m_status != Status::Pending) {
+ return;
+ }
+ m_status = Status::Idle;
+
VERIFY_OR_DEBUG_ASSERT(m_networkAccessManager) {
+ m_status = Status::Pending;
onNetworkError(
QUrl(),
QNetworkReply::NetworkSessionFailedError,
@@ -179,13 +205,14 @@ void WebTask::slotStart(int timeoutMillis) {
}
kLogger.debug()
+ << this
<< "Starting...";
- m_status = Status::Idle;
if (!doStart(m_networkAccessManager, timeoutMillis)) {
// Still idle, because we are in the same thread.
// The callee is not supposed to abort a request
// before it has beeen started successfully.
DEBUG_ASSERT(m_status == Status::Idle);
+ m_status = Status::Pending;
onNetworkError(
QUrl(),
QNetworkReply::OperationCanceledError,
@@ -240,8 +267,9 @@ QUrl WebTask::abort() {
killTimer(m_timeoutTimerId);
m_timeoutTimerId = kInvalidTimerId;
}
- m_status = Status::Aborted;
+ m_status = Status::Aborting;
kLogger.debug()
+ << this
<< "Aborting...";
QUrl url = doAbort();
onAborted(QUrl(url));
@@ -256,16 +284,16 @@ void WebTask::timerEvent(QTimerEvent* event) {
DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this);
const auto timerId = event->timerId();
DEBUG_ASSERT(timerId != kInvalidTimerId);
- if (timerId != m_timeoutTimerId) {
- // ignore
+ VERIFY_OR_DEBUG_ASSERT(timerId == m_timeoutTimerId) {
return;
}
killTimer(m_timeoutTimerId);
m_timeoutTimerId = kInvalidTimerId;
- if (m_status != Status::Aborted) {
- m_status = Status::TimedOut;
+ VERIFY_OR_DEBUG_ASSERT(m_status == Status::Pending) {
+ return;
}
- kLogger.debug()
+ kLogger.info()
+ << this
<< "Timed out";
onTimedOut(doTimeOut());
}
@@ -280,34 +308,16 @@ QPair<QNetworkReply*, HttpStatusCode> WebTask::receiveNetworkReply() {
}
networkReply->deleteLater();
- if (m_timeoutTimerId != kInvalidTimerId) {
- killTimer(m_timeoutTimerId);
- m_timeoutTimerId = kInvalidTimerId;
- }
-
- if (m_status == Status::Aborted) {
- onAborted(networkReply->request().url());
- return qMakePair(nullptr, statusCode);
- }
- m_status = Status::Finished;
-
- if (networkReply->error() != QNetworkReply::NetworkError::NoError) {
- onNetworkError(
- networkReply->request().url(),
- networkReply->error(),
- networkReply->errorString(),
- networkReply->readAll());
- return qMakePair(nullptr, statusCode);
- }
-
if (kLogger.debugEnabled()) {
if (networkReply->url() == networkReply->request().url()) {
kLogger.debug()
+ << this
<< "Received reply for request"
<< networkReply->url();
} else {
// Redirected
kLogger.debug()
+ << this
<< "Received reply for redirected request"
<< networkReply->request().url()
<< "->"
@@ -315,9 +325,39 @@ QPair<QNetworkReply*, HttpStatusCode> WebTask::receiveNetworkReply() {
}
}
+ if (m_status == Status::Aborted ||
+ m_status == Status::TimedOut) {
+ // Already aborted or timed out by the client
+ DEBUG_ASSERT(m_timeoutTimerId == kInvalidTimerId);
+ kLogger.debug()
+ << this
+ << "Ignoring obsolete network reply";
+ return qMakePair(nullptr, statusCode);
+ }
+ VERIFY_OR_DEBUG_ASSERT(m_status == Status::Pending) {
+ DEBUG_ASSERT(m_timeoutTimerId == kInvalidTimerId);
+ return qMakePair(nullptr, statusCode);
+ }
+
+ if (m_timeoutTimerId != kInvalidTimerId) {
+ killTimer(m_timeoutTimerId);
+ m_timeoutTimerId = kInvalidTimerId;
+ }
+
+ if (networkReply->error() != QNetworkReply::NetworkError::NoError) {
+ onNetworkError(
+ networkReply->request().url(),
+ networkReply->error(),
+ networkReply->errorString(),
+ networkReply->readAll());
+ return qMakePair(nullptr, statusCode);
+ }
+ m_status = Status::Finished;
+
DEBUG_ASSERT(statusCode == kHttpStatusCodeInvalid);
VERIFY_OR_DEBUG_ASSERT(readStatusCode(networkReply, &statusCode)) {
kLogger.warning()
+ << this
<< "Failed to read HTTP status code";
}
diff --git a/src/network/webtask.h b/src/network/webtask.h
index 8735d3c824..98f60880ac 100644
--- a/src/network/webtask.h
+++ b/src/network/webtask.h
@@ -148,8 +148,10 @@ class WebTask : public QObject {
enum class Status {
Idle,
Pending,
+ Aborting,
Aborted,
TimedOut,
+ Failed,
Finished,
};