diff options
author | Kurt Pattyn <pattyn.kurt@gmail.com> | 2013-11-04 21:16:58 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-11-04 21:18:01 +0100 |
commit | e3b93f838ccade335753559d823f0b087fc81170 (patch) | |
tree | 056caa101efec80742f7705a03ecbf83cfc9bb4e /src/websockets | |
parent | 017405f210bd46d15cdfce2dace674cab6260402 (diff) | |
download | qtwebsockets-e3b93f838ccade335753559d823f0b087fc81170.tar.gz |
Add QWebSocketConfiguration to pre-cache socket settings
Change-Id: Ibff22b277b89379dc31b0b7a8a02547e12bff58e
Reviewed-by: Kurt Pattyn <pattyn.kurt@gmail.com>
Diffstat (limited to 'src/websockets')
-rw-r--r-- | src/websockets/qwebsocket_p.cpp | 139 | ||||
-rw-r--r-- | src/websockets/qwebsocket_p.h | 37 | ||||
-rw-r--r-- | src/websockets/qwebsocketprotocol.h | 4 |
3 files changed, 108 insertions, 72 deletions
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 <QUrl> -#include <QTcpSocket> -#include <QByteArray> -#include <QtEndian> -#include <QCryptographicHash> -#include <QRegularExpression> -#include <QStringList> -#include <QHostAddress> -#include <QStringBuilder> //for more efficient string concatenation +#include <QtCore/QUrl> +#include <QtNetwork/QTcpSocket> +#include <QtCore/QByteArray> +#include <QtCore/QtEndian> +#include <QtCore/QCryptographicHash> +#include <QtCore/QRegularExpression> +#include <QtCore/QStringList> +#include <QtNetwork/QHostAddress> +#include <QtCore/QStringBuilder> //for more efficient string concatenation #ifndef QT_NONETWORKPROXY -#include <QNetworkProxy> +#include <QtNetwork/QNetworkProxy> +#endif +#ifndef QT_NO_SSL +#include <QtNetwork/QSslConfiguration> +#include <QtNetwork/QSslError> #endif -#include <QDebug> +#include <QtCore/QDebug> #include <limits> @@ -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<uint>(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<QSslError> &errors) { - m_ignoredSslErrors = errors; + m_configuration.m_ignoredSslErrors = errors; } /*! @@ -265,7 +272,15 @@ void QWebSocketPrivate::ignoreSslErrors(const QList<QSslError> &errors) */ void QWebSocketPrivate::ignoreSslErrors() { - m_ignoreSslErrors = true; + m_configuration.m_ignoreSslErrors = true; + if (m_pSocket) + { + QSslSocket *pSslSocket = qobject_cast<QSslSocket *>(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<QByteArray> &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 @@ -1290,17 +1322,6 @@ void QWebSocketPrivate::setPauseMode(QAbstractSocket::PauseModes pauseMode) /*! \internal */ -void QWebSocketPrivate::setProxy(const QNetworkProxy &networkProxy) -{ - if (m_pSocket) - { - m_pSocket->setProxy(networkProxy); - } -} - -/*! - \internal - */ void QWebSocketPrivate::setReadBufferSize(qint64 size) { if (m_pSocket) 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 <QUrl> -#include <QHostAddress> +#include <QtCore/QUrl> +#include <QtNetwork/QTcpSocket> +#include <QtNetwork/QHostAddress> #ifndef QT_NO_NETWORKPROXY -#include <QNetworkProxy> +#include <QtNetwork/QNetworkProxy> #endif #ifndef QT_NO_SSL -#include <QSslConfiguration> -#include <QSslError> +#include <QtNetwork/QSslConfiguration> +#include <QtNetwork/QSslError> +#include <QtNetwork/QSslSocket> #endif -#include <QTime> +#include <QtCore/QTime> #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<QSslError> 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<QSslError> 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 <qglobal.h> -#include "QtWebSockets/qwebsockets_global.h" +#include <QtCore/qglobal.h> +#include "qwebsockets_global.h" QT_BEGIN_NAMESPACE |