From 5e949114ae38123757d17de7559287dde23cf141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 19 Aug 2022 14:37:27 +0200 Subject: QWebSocketServer: update handshake timeout to keep up with QSslServer A side-effect of this is that the timeout is now duplicated. The TLS handshake first has the 'msec' timeout, and then the websocket handshake has the same 'msec' timeout. [ChangeLog][QWebSocketServer] Due to the introduction of a standalone QSslServer, with its own timeout handling, the setHandshakeTimeout() function now applies the same timeout to both the TLS and WebSocket handshakes separately. Fixes: QTBUG-105851 Change-Id: I6c515e0dcdf83fa452979b06ab5f890c8b14b184 Reviewed-by: Timur Pocheptsov Reviewed-by: Ievgenii Meshcheriakov (cherry picked from commit d885e441310c1f69b306a0eadbafde1058aa389a) Reviewed-by: Qt Cherry-pick Bot --- src/websockets/qwebsocketserver_p.cpp | 27 +++++++++++++++++++++++---- src/websockets/qwebsocketserver_p.h | 4 +--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/websockets/qwebsocketserver_p.cpp b/src/websockets/qwebsocketserver_p.cpp index cbc2dce..5adfd1f 100644 --- a/src/websockets/qwebsocketserver_p.cpp +++ b/src/websockets/qwebsocketserver_p.cpp @@ -9,6 +9,7 @@ #include "qwebsocket.h" #include "qwebsocket_p.h" #include "qwebsocketcorsauthenticator.h" +#include #ifndef QT_NO_SSL #include "QtNetwork/QSslServer" @@ -59,12 +60,12 @@ void QWebSocketServerPrivate::init() #ifndef QT_NO_SSL QSslServer *pSslServer = new QSslServer(q); m_pTcpServer = pSslServer; + // Update the QSslServer with the timeout we have: + setHandshakeTimeout(m_handshakeTimeout); if (Q_LIKELY(m_pTcpServer)) { QObjectPrivate::connect(pSslServer, &QTcpServer::pendingConnectionAvailable, this, &QWebSocketServerPrivate::onNewConnection, Qt::QueuedConnection); - QObjectPrivate::connect(pSslServer, &QSslServer::startedEncryptionHandshake, - this, &QWebSocketServerPrivate::startHandshakeTimeout); QObject::connect(pSslServer, &QSslServer::peerVerifyError, [q](QSslSocket *socket, const QSslError &error) { Q_UNUSED(socket); @@ -276,6 +277,24 @@ void QWebSocketServerPrivate::setMaxPendingConnections(int numConnections) m_maxPendingConnections = numConnections; } +/*! + \internal + */ +void QWebSocketServerPrivate::setHandshakeTimeout(int msec) +{ +#if QT_CONFIG(ssl) + if (auto *server = qobject_cast(m_pTcpServer)) { + int timeout = msec; + // Since QSslServer doesn't deal with negative numbers we set a very + // large one instead to keep some level of compatibility: + if (timeout < 0) + timeout = std::numeric_limits::max(); + server->setHandshakeTimeout(timeout); + } +#endif + m_handshakeTimeout = msec; +} + /*! \internal */ @@ -385,8 +404,8 @@ void QWebSocketServerPrivate::onNewConnection() { while (m_pTcpServer->hasPendingConnections()) { QTcpSocket *pTcpSocket = m_pTcpServer->nextPendingConnection(); - if (Q_LIKELY(pTcpSocket) && m_secureMode == NonSecureMode) - startHandshakeTimeout(pTcpSocket); + Q_ASSERT(pTcpSocket); + startHandshakeTimeout(pTcpSocket); handleConnection(pTcpSocket); } } diff --git a/src/websockets/qwebsocketserver_p.h b/src/websockets/qwebsocketserver_p.h index 6d7ffcd..9941341 100644 --- a/src/websockets/qwebsocketserver_p.h +++ b/src/websockets/qwebsocketserver_p.h @@ -68,9 +68,7 @@ public: QWebSocketProtocol::CloseCode serverError() const; quint16 serverPort() const; void setMaxPendingConnections(int numConnections); - void setHandshakeTimeout(int msec) { - m_handshakeTimeout = msec; - } + void setHandshakeTimeout(int msec); bool setSocketDescriptor(qintptr socketDescriptor); qintptr socketDescriptor() const; -- cgit v1.2.1