From d885e441310c1f69b306a0eadbafde1058aa389a 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. Pick-to: 6.4 Fixes: QTBUG-105851 Change-Id: I6c515e0dcdf83fa452979b06ab5f890c8b14b184 Reviewed-by: Timur Pocheptsov Reviewed-by: Ievgenii Meshcheriakov --- src/websockets/qwebsocketserver_p.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/websockets/qwebsocketserver_p.cpp') 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); } } -- cgit v1.2.1