diff options
author | Øystein Heskestad <oystein.heskestad@qt.io> | 2022-05-03 16:07:25 +0200 |
---|---|---|
committer | Øystein Heskestad <oystein.heskestad@qt.io> | 2022-06-09 13:48:16 +0000 |
commit | 86932a1b793bca41a7ff61c6637b4e4402e48395 (patch) | |
tree | fbcb25e5ed026aaac08ddf0168f0a4e96eb4de1f | |
parent | 227c56aeb4cb7f8876d37f4d351cbd8e3cd0a5c0 (diff) |
Use QSslServer from QtNetwork and remove own private implementation
Task-number: QTBUG-100823
Pick-to: dev
Change-Id: I09d855a7763d218b9dad5667c29a28cb5351e98f
Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>
-rw-r--r-- | src/websockets/CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/websockets/qsslserver.cpp | 151 | ||||
-rw-r--r-- | src/websockets/qsslserver_p.h | 99 | ||||
-rw-r--r-- | src/websockets/qwebsocketserver_p.cpp | 47 |
4 files changed, 34 insertions, 270 deletions
diff --git a/src/websockets/CMakeLists.txt b/src/websockets/CMakeLists.txt index 5f535fc..5abad62 100644 --- a/src/websockets/CMakeLists.txt +++ b/src/websockets/CMakeLists.txt @@ -42,13 +42,6 @@ qt_internal_extend_target(WebSockets CONDITION WASM websocket.js ) -qt_internal_extend_target(WebSockets CONDITION QT_FEATURE_ssl - SOURCES - qsslserver.cpp -) - -#### Keys ignored in scope 3:.:.:websockets.pro:QT_FEATURE_ssl: -# PRIVATE_HEADERS = "qsslserver_p.h" qt_internal_add_docs(WebSockets doc/qtwebsockets.qdocconf ) diff --git a/src/websockets/qsslserver.cpp b/src/websockets/qsslserver.cpp deleted file mode 100644 index ed4e4c0..0000000 --- a/src/websockets/qsslserver.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebSockets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/*! - \class QSslServer - - \inmodule QtWebSockets - - \brief Implements a secure TCP server over SSL. - - \internal -*/ - -#include "qsslserver_p.h" - -#include <QtNetwork/QSslSocket> -#include <QtNetwork/QSslCipher> - -QT_BEGIN_NAMESPACE - -/*! - Constructs a new QSslServer with the given \a parent. - - \internal -*/ -QSslServer::QSslServer(QObject *parent) : - QTcpServer(parent), - m_sslConfiguration(QSslConfiguration::defaultConfiguration()) -{ -} - -/*! - Destroys the QSslServer. - - All open connections are closed. - - \internal -*/ -QSslServer::~QSslServer() -{ -} - -/*! - Sets the \a sslConfiguration to use. - - \sa QSslSocket::setSslConfiguration() - - \internal -*/ -void QSslServer::setSslConfiguration(const QSslConfiguration &sslConfiguration) -{ - m_sslConfiguration = sslConfiguration; -} - -/*! - Returns the current ssl configuration. - - \internal -*/ -QSslConfiguration QSslServer::sslConfiguration() const -{ - return m_sslConfiguration; -} - -/*! - Called when a new connection is established. - - Converts \a socket to a QSslSocket. - - \internal -*/ -void QSslServer::incomingConnection(qintptr socket) -{ - QSslSocket *pSslSocket = new QSslSocket(this); - - if (Q_LIKELY(pSslSocket)) { - pSslSocket->setSslConfiguration(m_sslConfiguration); - - if (Q_LIKELY(pSslSocket->setSocketDescriptor(socket))) { - connect(pSslSocket, &QSslSocket::peerVerifyError, this, &QSslServer::peerVerifyError); - - connect(pSslSocket, QOverload<const QList<QSslError>&>::of(&QSslSocket::sslErrors), - this, &QSslServer::sslErrors); - connect(pSslSocket, &QSslSocket::encrypted, - this, &QSslServer::socketEncrypted); - connect(pSslSocket, &QSslSocket::preSharedKeyAuthenticationRequired, - this, &QSslServer::preSharedKeyAuthenticationRequired); - connect(pSslSocket, &QSslSocket::alertSent, - this, &QSslServer::alertSent); - connect(pSslSocket, &QSslSocket::alertReceived, - this, &QSslServer::alertReceived); - connect(pSslSocket, &QSslSocket::handshakeInterruptedOnError, - this, &QSslServer::handshakeInterruptedOnError); - - Q_EMIT startedEncryptionHandshake(pSslSocket); - - pSslSocket->startServerEncryption(); - } else { - delete pSslSocket; - } - } -} - -void QSslServer::socketEncrypted() -{ - QSslSocket *pSslSocket = qobject_cast<QSslSocket *>(sender()); - - // We do not add the connection until the encryption handshake is complete. - // In case the handshake is aborted, we would be left with a stale - // connection in the queue otherwise. - addPendingConnection(pSslSocket); - Q_EMIT newEncryptedConnection(); -} - -QT_END_NAMESPACE diff --git a/src/websockets/qsslserver_p.h b/src/websockets/qsslserver_p.h deleted file mode 100644 index a019fa6..0000000 --- a/src/websockets/qsslserver_p.h +++ /dev/null @@ -1,99 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebSockets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSSLSERVER_P_H -#define QSSLSERVER_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include <QtNetwork/QTcpServer> -#include <QtNetwork/QSslError> -#include <QtNetwork/QSslConfiguration> -#include <QtNetwork/QSslPreSharedKeyAuthenticator> -#include <QtCore/QList> -#include <QtCore/private/qglobal_p.h> - -QT_BEGIN_NAMESPACE - -class QSslSocket; - -class QSslServer : public QTcpServer -{ - Q_OBJECT - Q_DISABLE_COPY(QSslServer) - -public: - explicit QSslServer(QObject *parent = nullptr); - ~QSslServer() override; - - void setSslConfiguration(const QSslConfiguration &sslConfiguration); - QSslConfiguration sslConfiguration() const; - -Q_SIGNALS: - void sslErrors(const QList<QSslError> &errors); - void peerVerifyError(const QSslError &error); - void newEncryptedConnection(); - void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator); - void alertSent(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description); - void alertReceived(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description); - void handshakeInterruptedOnError(const QSslError &error); - void startedEncryptionHandshake(QSslSocket *socket); - -protected: - void incomingConnection(qintptr socket) override; - -private slots: - void socketEncrypted(); - -private: - QSslConfiguration m_sslConfiguration; -}; - -QT_END_NAMESPACE - -#endif // QSSLSERVER_P_H diff --git a/src/websockets/qwebsocketserver_p.cpp b/src/websockets/qwebsocketserver_p.cpp index c91c436..c841949 100644 --- a/src/websockets/qwebsocketserver_p.cpp +++ b/src/websockets/qwebsocketserver_p.cpp @@ -39,9 +39,6 @@ #include "qwebsocketserver.h" #include "qwebsocketserver_p.h" -#ifndef QT_NO_SSL -#include "qsslserver_p.h" -#endif #include "qwebsocketprotocol.h" #include "qwebsockethandshakerequest_p.h" #include "qwebsockethandshakeresponse_p.h" @@ -49,6 +46,9 @@ #include "qwebsocket_p.h" #include "qwebsocketcorsauthenticator.h" +#ifndef QT_NO_SSL +#include "QtNetwork/QSslServer" +#endif #include <QtCore/QTimer> #include <QtNetwork/QTcpServer> #include <QtNetwork/QTcpSocket> @@ -87,8 +87,8 @@ void QWebSocketServerPrivate::init() if (m_secureMode == NonSecureMode) { m_pTcpServer = new QTcpServer(q); if (Q_LIKELY(m_pTcpServer)) - QObjectPrivate::connect(m_pTcpServer, &QTcpServer::newConnection, - this, &QWebSocketServerPrivate::onNewConnection); + QObjectPrivate::connect(m_pTcpServer, &QTcpServer::pendingConnectionAvailable, this, + &QWebSocketServerPrivate::onNewConnection); else qFatal("Could not allocate memory for tcp server."); } else { @@ -96,23 +96,44 @@ void QWebSocketServerPrivate::init() QSslServer *pSslServer = new QSslServer(q); m_pTcpServer = pSslServer; if (Q_LIKELY(m_pTcpServer)) { - QObjectPrivate::connect(pSslServer, &QSslServer::newEncryptedConnection, - this, &QWebSocketServerPrivate::onNewConnection, + QObjectPrivate::connect(pSslServer, &QTcpServer::pendingConnectionAvailable, this, + &QWebSocketServerPrivate::onNewConnection, Qt::QueuedConnection); QObjectPrivate::connect(pSslServer, &QSslServer::startedEncryptionHandshake, this, &QWebSocketServerPrivate::startHandshakeTimeout); QObject::connect(pSslServer, &QSslServer::peerVerifyError, - q, &QWebSocketServer::peerVerifyError); + [q](QSslSocket *socket, const QSslError &error) { + Q_UNUSED(socket); + Q_EMIT q->peerVerifyError(error); + }); QObject::connect(pSslServer, &QSslServer::sslErrors, - q, &QWebSocketServer::sslErrors); + [q](QSslSocket *socket, const QList<QSslError> &errors) { + Q_UNUSED(socket); + Q_EMIT q->sslErrors(errors); + }); QObject::connect(pSslServer, &QSslServer::preSharedKeyAuthenticationRequired, - q, &QWebSocketServer::preSharedKeyAuthenticationRequired); + [q](QSslSocket *socket, + QSslPreSharedKeyAuthenticator *authenticator) { + Q_UNUSED(socket); + Q_EMIT q->preSharedKeyAuthenticationRequired(authenticator); + }); QObject::connect(pSslServer, &QSslServer::alertSent, - q, &QWebSocketServer::alertSent); + [q](QSslSocket *socket, QSsl::AlertLevel level, + QSsl::AlertType type, const QString &description) { + Q_UNUSED(socket); + Q_EMIT q->alertSent(level, type, description); + }); QObject::connect(pSslServer, &QSslServer::alertReceived, - q, &QWebSocketServer::alertReceived); + [q](QSslSocket *socket, QSsl::AlertLevel level, + QSsl::AlertType type, const QString &description) { + Q_UNUSED(socket); + Q_EMIT q->alertReceived(level, type, description); + }); QObject::connect(pSslServer, &QSslServer::handshakeInterruptedOnError, - q, &QWebSocketServer::handshakeInterruptedOnError); + [q](QSslSocket *socket, const QSslError &error) { + Q_UNUSED(socket); + Q_EMIT q->handshakeInterruptedOnError(error); + }); } #else qFatal("SSL not supported on this platform."); |