CONFIG += qt_example_installs
-MODULE_VERSION = 5.1.1
+MODULE_VERSION = 5.2.0
#define ECHOCLIENT_H
#include <QObject>
-#include "qwebsocket.h"
+#include <QtWebSockets/QWebSocket>
class EchoClient : public QObject
{
The \a bytes argument is set to the number of bytes that were written in this payload.
\note This signal has the same meaning both for secure and non-secure websockets.
- As opposed to QSslSocket, bytesWritten() is only emitted when encrypted data is effectively written (\sa {{QSslSocket:}}{{encryptedBytesWritten}}).
+ As opposed to QSslSocket, bytesWritten() is only emitted when encrypted data is effectively written (\sa QSslSocket:encryptedBytesWritten()).
\sa close()
*/
d->ping(payload);
}
+#ifndef QT_NO_SSL
+/*!
+ This slot tells QWebSocket to ignore errors during QWebSocket's
+ handshake phase and continue connecting. If you want to continue
+ with the connection even if errors occur during the handshake
+ phase, then you must call this slot, either from a slot connected
+ to sslErrors(), or before the handshake phase. If you don't call
+ this slot, either in response to errors or before the handshake,
+ the connection will be dropped after the sslErrors() signal has
+ been emitted.
+
+ \warning Be sure to always let the user inspect the errors
+ reported by the sslErrors() signal, and only call this method
+ upon confirmation from the user that proceeding is ok.
+ If there are unexpected errors, the connection should be aborted.
+ Calling this method without inspecting the actual errors will
+ most likely pose a security risk for your application. Use it
+ with great care!
+
+ \sa sslErrors(), QSslSocket::ignoreSslErrors(), QNetworkReply::ignoreSslErrors()
+*/
+void QWebSocket::ignoreSslErrors()
+{
+ Q_D(QWebSocket);
+ d->ignoreSslErrors();
+}
+
+/*!
+ \overload
+
+ This method tells QWebSocket to ignore the errors given in \a errors.
+
+ Note that you can set the expected certificate in the SSL error:
+ If, for instance, you want to connect to a server that uses
+ a self-signed certificate, consider the following snippet:
+
+ \snippet src_websockets_ssl_qwebsocket 6
+
+ Multiple calls to this function will replace the list of errors that
+ were passed in previous calls.
+ You can clear the list of errors you want to ignore by calling this
+ function with an empty list.
+
+ \sa sslErrors()
+*/
+void QWebSocket::ignoreSslErrors(const QList<QSslError> &errors)
+{
+ Q_D(QWebSocket);
+ d->ignoreSslErrors(errors);
+}
+
+#endif //not QT_NO_SSL
+
/*!
\brief Returns the version the socket is currently using
*/
void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, const QString &reason = QString());
void open(const QUrl &url, bool mask = true);
void ping(const QByteArray &payload = QByteArray());
+#ifndef QT_NO_SSL
+ void ignoreSslErrors();
+#endif
Q_SIGNALS:
void aboutToClose();
void pong(quint64 elapsedTime, QByteArray payload);
void bytesWritten(qint64 bytes);
+#ifndef QT_NO_SSL
+ void sslErrors(const QList<QSslError> &errors);
+#endif
+
private:
QWebSocket(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QObject *parent = Q_NULLPTR);
QWebSocketPrivate * const d_ptr;
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),
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),
m_ignoredSslErrors = errors;
}
+/*!
+ * \internal
+ */
+void QWebSocketPrivate::ignoreSslErrors()
+{
+ m_ignoreSslErrors = true;
+}
+
#endif
/*!
{
if (!QSslSocket::supportsSsl())
{
- qWarning() << tr("SSL Sockets are not supported on this platform.");
- setErrorString(tr("SSL Sockets are not supported on this platform."));
+ const QString message = tr("SSL Sockets are not supported on this platform.");
+ qWarning() << message;
+ setErrorString(message);
emit q->error(QAbstractSocket::UnsupportedSocketOperationError);
- return;
}
else
{
setSocketState(QAbstractSocket::ConnectingState);
sslSocket->setSslConfiguration(m_sslConfiguration);
- sslSocket->ignoreSslErrors(m_ignoredSslErrors);
+ if (m_ignoreSslErrors)
+ {
+ sslSocket->ignoreSslErrors();
+ }
+ else
+ {
+ sslSocket->ignoreSslErrors(m_ignoredSslErrors);
+ }
sslSocket->connectToHostEncrypted(url.host(), url.port(443));
}
}
else
#endif
+ if (url.scheme() == QStringLiteral("ws"))
{
m_pSocket = new QTcpSocket(this);
setSocketState(QAbstractSocket::ConnectingState);
m_pSocket->connectToHost(url.host(), url.port(80));
}
+ else
+ {
+ const QString message = tr("Unsupported websockets scheme: %1").arg(url.scheme());
+ qWarning() << message;
+ setErrorString(message);
+ emit q->error(QAbstractSocket::UnsupportedSocketOperationError);
+ }
}
/*!
void open(const QUrl &url, bool mask);
void ping(const QByteArray &payload);
+#ifndef QT_NO_SSL
+ void ignoreSslErrors();
+#endif
+
private Q_SLOTS:
void processData();
void processPing(QByteArray data);
#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);
#define QWEBSOCKETPROTOCOL_H
#include <qglobal.h>
+#include "qwebsockets_global.h"
QT_BEGIN_NAMESPACE
#include <QtTest/qtestcase.h>
#include <QSignalSpy>
#include <QHostInfo>
+#include <QSslError>
#include <QDebug>
-#include "qwebsocket.h"
+#include <QWebSocket>
class tst_ComplianceTest : public QObject
{
};
tst_ComplianceTest::tst_ComplianceTest() :
- m_url("ws://localhost:9001")
+ m_url("wss://localhost:9001")
{
}
{
return;
}
+
QWebSocket *pWebSocket = new QWebSocket;
+ pWebSocket->ignoreSslErrors();
QSignalSpy spy(pWebSocket, SIGNAL(disconnected()));
//next for every case, connect to url
{
//connect to autobahn server at url ws://ipaddress:port/getCaseCount
QWebSocket *pWebSocket = new QWebSocket;
+ pWebSocket->ignoreSslErrors();
QUrl url = m_url;
int numberOfTestCases = 0;
QSignalSpy spy(pWebSocket, SIGNAL(disconnected()));