From e3b93f838ccade335753559d823f0b087fc81170 Mon Sep 17 00:00:00 2001 From: Kurt Pattyn Date: Mon, 4 Nov 2013 21:16:58 +0100 Subject: Add QWebSocketConfiguration to pre-cache socket settings Change-Id: Ibff22b277b89379dc31b0b7a8a02547e12bff58e Reviewed-by: Kurt Pattyn --- src/websockets/qwebsocket_p.cpp | 139 +++++++++++++++++++++--------------- src/websockets/qwebsocket_p.h | 37 +++++++--- src/websockets/qwebsocketprotocol.h | 4 +- 3 files changed, 108 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/websockets/qwebsocket_p.cpp b/src/websockets/qwebsocket_p.cpp index d6ff2c4..baae1e4 100644 --- a/src/websockets/qwebsocket_p.cpp +++ b/src/websockets/qwebsocket_p.cpp @@ -44,20 +44,24 @@ #include "qwebsocketprotocol_p.h" #include "qwebsockethandshakerequest_p.h" #include "qwebsockethandshakeresponse_p.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include //for more efficient string concatenation +#include +#include +#include +#include +#include +#include +#include +#include +#include //for more efficient string concatenation #ifndef QT_NONETWORKPROXY -#include +#include +#endif +#ifndef QT_NO_SSL +#include +#include #endif -#include +#include #include @@ -65,17 +69,24 @@ QT_BEGIN_NAMESPACE const quint64 FRAME_SIZE_IN_BYTES = 512 * 512 * 2; //maximum size of a frame when sending a message +QWebSocketConfiguration::QWebSocketConfiguration() : +#ifndef QT_NO_SSL + m_sslConfiguration(QSslConfiguration::defaultConfiguration()), + m_ignoredSslErrors(), + m_ignoreSslErrors(false), +#endif +#ifndef QT_NONETWORKPROXY + m_proxy(QNetworkProxy::DefaultProxy) +#endif +{ +} + /*! \internal */ QWebSocketPrivate::QWebSocketPrivate(const QString &origin, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent) : QObject(parent), q_ptr(pWebSocket), -#ifndef QT_NO_SSL - m_sslConfiguration(), - m_ignoredSslErrors(), - m_ignoreSslErrors(false), -#endif m_pSocket(Q_NULLPTR), m_errorString(), m_version(version), @@ -90,10 +101,10 @@ QWebSocketPrivate::QWebSocketPrivate(const QString &origin, QWebSocketProtocol:: m_isClosingHandshakeSent(false), m_isClosingHandshakeReceived(false), m_pingTimer(), - m_dataProcessor() + m_dataProcessor(), + m_configuration() { Q_ASSERT(pWebSocket); - //makeConnections(m_pSocket); qsrand(static_cast(QDateTime::currentMSecsSinceEpoch())); } @@ -103,11 +114,6 @@ QWebSocketPrivate::QWebSocketPrivate(const QString &origin, QWebSocketProtocol:: QWebSocketPrivate::QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent) : QObject(parent), q_ptr(pWebSocket), -#ifndef QT_NO_SSL - m_sslConfiguration(), //socket is already open, so we don't need to set the ssl configuration anymore - m_ignoredSslErrors(), //socket is already open, so we don't need to set the ignored errors anymore - m_ignoreSslErrors(false), -#endif m_pSocket(pTcpSocket), m_errorString(pTcpSocket->errorString()), m_version(version), @@ -122,7 +128,8 @@ QWebSocketPrivate::QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol: m_isClosingHandshakeSent(false), m_isClosingHandshakeReceived(false), m_pingTimer(), - m_dataProcessor() + m_dataProcessor(), + m_configuration() { Q_ASSERT(pWebSocket); makeConnections(m_pSocket); @@ -241,7 +248,7 @@ qint64 QWebSocketPrivate::write(const QByteArray &data) */ void QWebSocketPrivate::setSslConfiguration(const QSslConfiguration &sslConfiguration) { - m_sslConfiguration = sslConfiguration; + m_configuration.m_sslConfiguration = sslConfiguration; } /*! @@ -249,7 +256,7 @@ void QWebSocketPrivate::setSslConfiguration(const QSslConfiguration &sslConfigur */ QSslConfiguration QWebSocketPrivate::sslConfiguration() const { - return m_sslConfiguration; + return m_configuration.m_sslConfiguration; } /*! @@ -257,7 +264,7 @@ QSslConfiguration QWebSocketPrivate::sslConfiguration() const */ void QWebSocketPrivate::ignoreSslErrors(const QList &errors) { - m_ignoredSslErrors = errors; + m_configuration.m_ignoredSslErrors = errors; } /*! @@ -265,7 +272,15 @@ void QWebSocketPrivate::ignoreSslErrors(const QList &errors) */ void QWebSocketPrivate::ignoreSslErrors() { - m_ignoreSslErrors = true; + m_configuration.m_ignoreSslErrors = true; + if (m_pSocket) + { + QSslSocket *pSslSocket = qobject_cast(m_pSocket); + if (pSslSocket) + { + pSslSocket->ignoreSslErrors(); + } + } } #endif @@ -379,15 +394,18 @@ void QWebSocketPrivate::open(const QUrl &url, bool mask) connect(sslSocket, SIGNAL(encryptedBytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64))); setSocketState(QAbstractSocket::ConnectingState); - sslSocket->setSslConfiguration(m_sslConfiguration); - if (m_ignoreSslErrors) + sslSocket->setSslConfiguration(m_configuration.m_sslConfiguration); + if (m_configuration.m_ignoreSslErrors) { sslSocket->ignoreSslErrors(); } else { - sslSocket->ignoreSslErrors(m_ignoredSslErrors); + sslSocket->ignoreSslErrors(m_configuration.m_ignoredSslErrors); } +#ifndef QT_NO_NETWORKPROXY + sslSocket->setProxy(m_configuration.m_proxy); +#endif sslSocket->connectToHostEncrypted(url.host(), url.port(443)); } } @@ -400,6 +418,9 @@ void QWebSocketPrivate::open(const QUrl &url, bool mask) makeConnections(m_pSocket); connect(m_pSocket, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64))); setSocketState(QAbstractSocket::ConnectingState); +#ifndef QT_NO_NETWORKPROXY + m_pSocket->setProxy(m_configuration.m_proxy); +#endif m_pSocket->connectToHost(url.host(), url.port(80)); } else @@ -786,9 +807,12 @@ QString QWebSocketPrivate::calculateAcceptKey(const QString &key) const qint64 QWebSocketPrivate::writeFrames(const QList &frames) { qint64 written = 0; - for (int i = 0; i < frames.size(); ++i) + if (m_pSocket) { - written += writeFrame(frames[i]); + for (int i = 0; i < frames.size(); ++i) + { + written += writeFrame(frames[i]); + } } return written; } @@ -813,17 +837,20 @@ QString readLine(QTcpSocket *pSocket) { Q_ASSERT(pSocket); QString line; - char c; - while (pSocket->getChar(&c)) + if (pSocket) { - if (c == '\r') + char c; + while (pSocket->getChar(&c)) { - pSocket->getChar(&c); - break; - } - else - { - line.append(QChar::fromLatin1(c)); + if (c == '\r') + { + pSocket->getChar(&c); + break; + } + else + { + line.append(QChar::fromLatin1(c)); + } } } return line; @@ -1239,18 +1266,23 @@ quint16 QWebSocketPrivate::peerPort() const return port; } +#ifndef QT_NO_NETWORKPROXY /*! \internal */ QNetworkProxy QWebSocketPrivate::proxy() const { - QNetworkProxy proxy; - if (m_pSocket) - { - proxy = m_pSocket->proxy(); - } - return proxy; + return m_configuration.m_proxy; +} + +/*! + \internal + */ +void QWebSocketPrivate::setProxy(const QNetworkProxy &networkProxy) +{ + m_configuration.m_proxy = networkProxy; } +#endif //QT_NO_NETWORKPROXY /*! \internal @@ -1287,17 +1319,6 @@ void QWebSocketPrivate::setPauseMode(QAbstractSocket::PauseModes pauseMode) } } -/*! - \internal - */ -void QWebSocketPrivate::setProxy(const QNetworkProxy &networkProxy) -{ - if (m_pSocket) - { - m_pSocket->setProxy(networkProxy); - } -} - /*! \internal */ diff --git a/src/websockets/qwebsocket_p.h b/src/websockets/qwebsocket_p.h index f059c99..80c5e4f 100644 --- a/src/websockets/qwebsocket_p.h +++ b/src/websockets/qwebsocket_p.h @@ -52,16 +52,18 @@ // We mean it. // -#include -#include +#include +#include +#include #ifndef QT_NO_NETWORKPROXY -#include +#include #endif #ifndef QT_NO_SSL -#include -#include +#include +#include +#include #endif -#include +#include #include "qwebsocketprotocol.h" #include "qwebsocketdataprocessor_p.h" @@ -73,6 +75,23 @@ class QWebSocketHandshakeResponse; class QTcpSocket; class QWebSocket; +struct QWebSocketConfiguration +{ +public: + QWebSocketConfiguration(); + +public: +#ifndef QT_NO_SSL + QSslConfiguration m_sslConfiguration; + QList m_ignoredSslErrors; + bool m_ignoreSslErrors; +#endif +#ifndef QT_NONETWORKPROXY + QNetworkProxy m_proxy; +#endif + QTcpSocket *m_pSocket; +}; + class QWebSocketPrivate : public QObject { Q_OBJECT @@ -149,11 +168,6 @@ private Q_SLOTS: private: QWebSocket * const q_ptr; -#ifndef QT_NO_SSL - QSslConfiguration m_sslConfiguration; - QList m_ignoredSslErrors; - bool m_ignoreSslErrors; -#endif QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = Q_NULLPTR); void setVersion(QWebSocketProtocol::Version version); @@ -213,6 +227,7 @@ private: QTime m_pingTimer; QWebSocketDataProcessor m_dataProcessor; + QWebSocketConfiguration m_configuration; friend class QWebSocketServerPrivate; }; diff --git a/src/websockets/qwebsocketprotocol.h b/src/websockets/qwebsocketprotocol.h index 0fd342b..7ee77f5 100644 --- a/src/websockets/qwebsocketprotocol.h +++ b/src/websockets/qwebsocketprotocol.h @@ -42,8 +42,8 @@ #ifndef QWEBSOCKETPROTOCOL_H #define QWEBSOCKETPROTOCOL_H -#include -#include "QtWebSockets/qwebsockets_global.h" +#include +#include "qwebsockets_global.h" QT_BEGIN_NAMESPACE -- cgit v1.2.1