From 1d8fed1d61ca36de9bd4ebd2b9ce1dcabafa815e Mon Sep 17 00:00:00 2001 From: Juha Vuolle Date: Mon, 3 Oct 2022 14:00:46 +0300 Subject: Fix uninitialized QWebSocket::errorString() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Amends: bbd9f2f4f5e0fda85029fa320f793973ea607c2b Fixes: QTBUG-106937 Change-Id: Ia805df3e3dd8ba61e53592ebfb0a8bfae9184042 Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit 17b96b31585e338afe5f5c71706a12cc3d2cd740) Reviewed-by: Qt Cherry-pick Bot --- src/websockets/qwebsocket_p.cpp | 2 +- src/websockets/qwebsocket_p.h | 5 ----- .../auto/websockets/qwebsocket/tst_qwebsocket.cpp | 24 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/websockets/qwebsocket_p.cpp b/src/websockets/qwebsocket_p.cpp index e43a701..15217ce 100644 --- a/src/websockets/qwebsocket_p.cpp +++ b/src/websockets/qwebsocket_p.cpp @@ -1060,7 +1060,7 @@ void QWebSocketPrivate::processHandshake(QTcpSocket *pSocket) } else { errorDescription = QWebSocket::tr("QWebSocketPrivate::processHandshake: Unhandled http status code: %1 (%2).") - .arg(m_httpStatusCode).arg(m_httpStatusMessage); + .arg(parser.getStatusCode()).arg(parser.getReasonPhrase()); } if (ok) { diff --git a/src/websockets/qwebsocket_p.h b/src/websockets/qwebsocket_p.h index 0579b3d..167535c 100644 --- a/src/websockets/qwebsocket_p.h +++ b/src/websockets/qwebsocket_p.h @@ -220,11 +220,6 @@ private: QDefaultMaskGenerator m_defaultMaskGenerator; QByteArray m_statusLine; - int m_httpStatusCode; - int m_httpMajorVersion, m_httpMinorVersion; - QString m_httpStatusMessage; - QMultiMap m_headers; - quint64 m_outgoingFrameSize; friend class QWebSocketServerPrivate; diff --git a/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp b/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp index 4b6e5ba..09310a3 100644 --- a/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp +++ b/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,7 @@ public: Q_SIGNALS: void newConnection(QUrl requestUrl); void newConnection(QNetworkRequest request); + void originAuthenticationRequired(QWebSocketCorsAuthenticator* pAuthenticator); private Q_SLOTS: void onNewConnection(); @@ -56,6 +58,8 @@ EchoServer::EchoServer(QObject *parent, quint64 maxAllowedIncomingMessageSize, q if (m_pWebSocketServer->listen(QHostAddress(QStringLiteral("127.0.0.1")))) { connect(m_pWebSocketServer, SIGNAL(newConnection()), this, SLOT(onNewConnection())); + connect(m_pWebSocketServer, &QWebSocketServer::originAuthenticationRequired, + this, &EchoServer::originAuthenticationRequired); } } @@ -613,6 +617,26 @@ void tst_QWebSocket::tst_errorString() qvariant_cast(arguments.at(0)); QCOMPARE(socketError, QAbstractSocket::HostNotFoundError); QCOMPARE(socket.errorString(), QStringLiteral("Host not found")); + + // Check that handshake status code is parsed. The error is triggered by + // refusing the origin authentication + EchoServer echoServer; + errorSpy.clear(); + QSignalSpy socketConnectedSpy(&socket, SIGNAL(connected())); + QSignalSpy serverConnectedSpy(&echoServer, SIGNAL(newConnection(QUrl))); + connect(&echoServer, &EchoServer::originAuthenticationRequired, + &socket, [](QWebSocketCorsAuthenticator* pAuthenticator){ + pAuthenticator->setAllowed(false); + }); + + socket.open(QUrl(QStringLiteral("ws://") + echoServer.hostAddress().toString() + + QStringLiteral(":") + QString::number(echoServer.port()))); + QTRY_VERIFY(errorSpy.size() > 0); + QCOMPARE(serverConnectedSpy.size(), 0); + QCOMPARE(socketConnectedSpy.size(), 0); + QCOMPARE(socket.errorString(), + QStringLiteral("QWebSocketPrivate::processHandshake: Unhandled http status code: 403" + " (Access Forbidden).")); } void tst_QWebSocket::tst_openRequest_data() -- cgit v1.2.1