diff options
author | Kurt Pattyn <pattyn.kurt@gmail.com> | 2013-10-31 09:31:25 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-10-31 11:52:27 +0100 |
commit | 3501d4e9c315af97f0d7878d1af7f4300a815ea3 (patch) | |
tree | d8ada550d4ef45aad87bc44150b4a36218e06a50 /src/websockets/qwebsocket_p.cpp | |
parent | 7a156a994dd093de80289574d0b542d59a5dd955 (diff) | |
download | qtwebsockets-3501d4e9c315af97f0d7878d1af7f4300a815ea3.tar.gz |
Implement of secure web sockets
Change-Id: I1f96d5e4e327eae211fd6b458168e1f7607e2dcf
Reviewed-by: Kurt Pattyn <pattyn.kurt@gmail.com>
Diffstat (limited to 'src/websockets/qwebsocket_p.cpp')
-rw-r--r-- | src/websockets/qwebsocket_p.cpp | 69 |
1 files changed, 65 insertions, 4 deletions
diff --git a/src/websockets/qwebsocket_p.cpp b/src/websockets/qwebsocket_p.cpp index ca7e903..e47f4e6 100644 --- a/src/websockets/qwebsocket_p.cpp +++ b/src/websockets/qwebsocket_p.cpp @@ -48,7 +48,7 @@ const quint64 FRAME_SIZE_IN_BYTES = 512 * 512 * 2; //maximum size of a frame whe QWebSocketPrivate::QWebSocketPrivate(const QString &origin, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent) : QObject(parent), q_ptr(pWebSocket), - m_pSocket(new QTcpSocket(this)), + m_pSocket(Q_NULLPTR), m_errorString(), m_version(version), m_resourceName(), @@ -65,7 +65,7 @@ QWebSocketPrivate::QWebSocketPrivate(const QString &origin, QWebSocketProtocol:: m_dataProcessor() { Q_ASSERT(pWebSocket); - makeConnections(m_pSocket); + //makeConnections(m_pSocket); qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch())); } @@ -184,7 +184,35 @@ qint64 QWebSocketPrivate::write(const QByteArray &data) return doWriteData(data, true); } +#ifndef QT_NO_SSL +/*! + \internal + */ +void QWebSocketPrivate::setSslConfiguration(const QSslConfiguration &sslConfiguration) +{ + m_sslConfiguration = sslConfiguration; +} + +/*! + \internal + */ +QSslConfiguration QWebSocketPrivate::sslConfiguration() const +{ + return m_sslConfiguration; +} + +/*! + \internal + */ +void QWebSocketPrivate::ignoreSslErrors(const QList<QSslError> &errors) +{ + m_ignoredSslErrors = errors; +} + +#endif + /*! + Called from QWebSocketServer \internal */ QWebSocket *QWebSocketPrivate::upgradeFrom(QTcpSocket *pTcpSocket, @@ -245,6 +273,8 @@ void QWebSocketPrivate::close(QWebSocketProtocol::CloseCode closeCode, QString r */ void QWebSocketPrivate::open(const QUrl &url, bool mask) { + Q_Q(QWebSocket); + m_dataProcessor.clear(); m_isClosingHandshakeReceived = false; m_isClosingHandshakeSent = false; @@ -266,9 +296,40 @@ void QWebSocketPrivate::open(const QUrl &url, bool mask) setResourceName(resourceName); enableMasking(mask); - setSocketState(QAbstractSocket::ConnectingState); +#ifndef QT_NO_SSL + if (url.scheme() == QStringLiteral("wss")) + { + if (!QSslSocket::supportsSsl()) + { + qWarning() << tr("SSL Sockets are not supported on this platform."); + setErrorString(tr("SSL Sockets are not supported on this platform.")); + emit q->error(QAbstractSocket::UnsupportedSocketOperationError); + return; + } + else + { + QSslSocket *sslSocket = new QSslSocket(this); + m_pSocket = sslSocket; + + makeConnections(m_pSocket); + connect(sslSocket, SIGNAL(encryptedBytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64))); + setSocketState(QAbstractSocket::ConnectingState); - m_pSocket->connectToHost(url.host(), url.port(80)); + sslSocket->setSslConfiguration(m_sslConfiguration); + sslSocket->ignoreSslErrors(m_ignoredSslErrors); + sslSocket->connectToHostEncrypted(url.host(), url.port(443)); + } + } + else +#endif + { + m_pSocket = new QTcpSocket(this); + + makeConnections(m_pSocket); + connect(m_pSocket, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64))); + setSocketState(QAbstractSocket::ConnectingState); + m_pSocket->connectToHost(url.host(), url.port(80)); + } } /*! |