diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2022-08-19 14:37:27 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2022-08-31 13:04:19 +0000 |
commit | d885e441310c1f69b306a0eadbafde1058aa389a (patch) | |
tree | abb1d7f8e0fa2cbdeafcc41de1ec3df867477c42 /src | |
parent | 430c34ae0dc158206c5310608871463de0d3e45d (diff) | |
download | qtwebsockets-d885e441310c1f69b306a0eadbafde1058aa389a.tar.gz |
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 <timur.pocheptsov@qt.io>
Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/websockets/qwebsocketserver_p.cpp | 27 | ||||
-rw-r--r-- | 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 <limits> #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); @@ -279,6 +280,24 @@ void QWebSocketServerPrivate::setMaxPendingConnections(int numConnections) /*! \internal */ +void QWebSocketServerPrivate::setHandshakeTimeout(int msec) +{ +#if QT_CONFIG(ssl) + if (auto *server = qobject_cast<QSslServer *>(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<int>::max(); + server->setHandshakeTimeout(timeout); + } +#endif + m_handshakeTimeout = msec; +} + +/*! + \internal + */ bool QWebSocketServerPrivate::setSocketDescriptor(qintptr socketDescriptor) { return m_pTcpServer->setSocketDescriptor(socketDescriptor); @@ -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; |