summaryrefslogtreecommitdiff
path: root/src/websockets/qwebsocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/websockets/qwebsocket.cpp')
-rw-r--r--src/websockets/qwebsocket.cpp172
1 files changed, 111 insertions, 61 deletions
diff --git a/src/websockets/qwebsocket.cpp b/src/websockets/qwebsocket.cpp
index 8381047..b495ffd 100644
--- a/src/websockets/qwebsocket.cpp
+++ b/src/websockets/qwebsocket.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
/*!
\class QWebSocket
@@ -52,9 +16,7 @@
This class was modeled after QAbstractSocket.
- QWebSocket currently does not support
- \l {WebSocket Extensions} and
- \l {WebSocket Subprotocols}.
+ QWebSocket currently does not support \l {WebSocket Extensions}.
QWebSocket only supports version 13 of the WebSocket protocol, as outlined in
\l {RFC 6455}.
@@ -78,7 +40,7 @@
*/
/*!
- \page echoclient.html example
+ \page echoclient.html
\title QWebSocket client example
\brief A sample WebSocket client that sends a message and displays the message that
it receives back.
@@ -136,6 +98,27 @@ not been filled in with new information when the signal returns.
\sa QAuthenticator, QNetworkProxy
*/
+
+/*!
+ \fn void QWebSocket::authenticationRequired(QAuthenticator *authenticator)
+ \since 6.6
+
+ This signal is emitted when the server requires authentication.
+ The \a authenticator object must then be filled in with the required details
+ to allow authentication and continue the connection.
+
+ If you know that the server may require authentication, you can set the
+ username and password on the initial QUrl, using QUrl::setUserName and
+ QUrl::setPassword. QWebSocket will still try to connect \e{once} without
+ using the provided credentials.
+
+ \note It is not possible to use a QueuedConnection to connect to
+ this signal, as the connection will fail if the authenticator has
+ not been filled in with new information when the signal returns.
+
+ \sa QAuthenticator
+*/
+
/*!
\fn void QWebSocket::stateChanged(QAbstractSocket::SocketState state);
@@ -210,18 +193,6 @@ not been filled in with new information when the signal returns.
\sa textMessageReceived()
*/
/*!
- \fn void QWebSocket::error(QAbstractSocket::SocketError error);
-
- This signal is emitted after an error occurred. The \a error
- parameter describes the type of error that occurred.
-
- QAbstractSocket::SocketError is not a registered metatype, so for queued
- connections, you will have to register it with Q_DECLARE_METATYPE() and
- qRegisterMetaType().
-
- \sa error(), errorString()
-*/
-/*!
\fn void QWebSocket::sslErrors(const QList<QSslError> &errors)
QWebSocket emits this signal after the SSL handshake to indicate that one or more errors have
occurred while establishing the identity of the peer.
@@ -262,7 +233,7 @@ not been filled in with new information when the signal returns.
*/
/*!
\fn void QWebSocket::peerVerifyError(const QSslError &error)
- \since 6.0
+ \since 6.2
QWebSocket can emit this signal several times during the SSL handshake,
before encryption has been established, to indicate that an error has
@@ -280,7 +251,7 @@ not been filled in with new information when the signal returns.
*/
/*!
\fn void QWebSocket::alertSent(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description)
- \since 6.0
+ \since 6.2
QWebSocket emits this signal if an alert message was sent to a peer. \a level
describes if it was a warning or a fatal error. \a type gives the code
@@ -295,7 +266,7 @@ not been filled in with new information when the signal returns.
*/
/*!
\fn void QWebSocket::alertReceived(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description)
- \since 6.0
+ \since 6.2
QWebSocket emits this signal if an alert message was received from a peer.
\a level tells if the alert was fatal or it was a warning. \a type is the
@@ -311,7 +282,7 @@ not been filled in with new information when the signal returns.
*/
/*!
\fn void QWebSocket::handshakeInterruptedOnError(const QSslError &error)
- \since 6.0
+ \since 6.2
QWebSocket emits this signal if a certificate verification error was
found and if early error reporting was enabled in QSslConfiguration.
@@ -332,6 +303,7 @@ not been filled in with new information when the signal returns.
*/
#include "qwebsocket.h"
#include "qwebsocket_p.h"
+#include "qwebsockethandshakeoptions.h"
#include <QtCore/QUrl>
#include <QtNetwork/QTcpSocket>
@@ -484,7 +456,7 @@ void QWebSocket::open(const QUrl &url)
{
Q_D(QWebSocket);
QNetworkRequest request(url);
- d->open(request, true);
+ d->open(request, QWebSocketHandshakeOptions{}, true);
}
/*!
@@ -498,7 +470,41 @@ void QWebSocket::open(const QUrl &url)
void QWebSocket::open(const QNetworkRequest &request)
{
Q_D(QWebSocket);
- d->open(request, true);
+ d->open(request, QWebSocketHandshakeOptions{}, true);
+}
+
+/*!
+ \brief Opens a WebSocket connection using the given \a url and \a options.
+ \since 6.4
+
+ If the url contains newline characters (\\r\\n), then the error signal will be emitted
+ with QAbstractSocket::ConnectionRefusedError as error type.
+
+ Additional options for the WebSocket handshake such as subprotocols can be specified in
+ \a options.
+ */
+void QWebSocket::open(const QUrl &url, const QWebSocketHandshakeOptions &options)
+{
+ Q_D(QWebSocket);
+ QNetworkRequest request(url);
+ d->open(request, options, true);
+}
+
+/*!
+ \brief Opens a WebSocket connection using the given \a request and \a options.
+ \since 6.4
+
+ The \a request url will be used to open the WebSocket connection.
+ Headers present in the request will be sent to the server in the upgrade request,
+ together with the ones needed for the websocket handshake.
+
+ Additional options for the WebSocket handshake such as subprotocols can be specified in
+ \a options.
+ */
+void QWebSocket::open(const QNetworkRequest &request, const QWebSocketHandshakeOptions &options)
+{
+ Q_D(QWebSocket);
+ d->open(request, options, true);
}
/*!
@@ -508,6 +514,9 @@ void QWebSocket::open(const QNetworkRequest &request)
The size of the \a payload cannot be bigger than 125.
If it is larger, the \a payload is clipped to 125 bytes.
+ \note QWebSocket and QWebSocketServer handles ping requests internally,
+ which means they automatically send back a pong response to the peer.
+
\sa pong()
*/
void QWebSocket::ping(const QByteArray &payload)
@@ -544,7 +553,7 @@ void QWebSocket::ignoreSslErrors()
}
/*!
- \since 6.0
+ \since 6.2
If an application wants to conclude a handshake even after receiving
handshakeInterruptedOnError() signal, it must call this function.
@@ -651,6 +660,16 @@ QNetworkRequest QWebSocket::request() const
}
/*!
+ \brief Returns the handshake options that were used to open this socket.
+ \since 6.4
+ */
+QWebSocketHandshakeOptions QWebSocket::handshakeOptions() const
+{
+ Q_D(const QWebSocket);
+ return d->handshakeOptions();
+}
+
+/*!
\brief Returns the current origin.
*/
QString QWebSocket::origin() const
@@ -660,6 +679,16 @@ QString QWebSocket::origin() const
}
/*!
+ \brief Returns the used WebSocket protocol.
+ \since 6.4
+ */
+QString QWebSocket::subprotocol() const
+{
+ Q_D(const QWebSocket);
+ return d->protocol();
+}
+
+/*!
\brief Returns the code indicating why the socket was closed.
\sa QWebSocketProtocol::CloseCode, closeReason()
*/
@@ -976,4 +1005,25 @@ quint64 QWebSocket::maxOutgoingFrameSize()
return QWebSocketPrivate::maxOutgoingFrameSize();
}
+/*!
+ \fn void QWebSocket::errorOccurred(QAbstractSocket::SocketError error);
+
+ \since 6.5
+ \brief This signal is emitted after an error occurred.
+
+ The \a error parameter describes the type of error that occurred.
+
+ QAbstractSocket::SocketError is not a registered metatype, so for queued
+ connections, you will have to register it with Q_DECLARE_METATYPE() and
+ qRegisterMetaType().
+
+ \sa error(), errorString()
+*/
+#if QT_DEPRECATED_SINCE(6, 5)
+/*!
+ \fn void QWebSocket::error(QAbstractSocket::SocketError error);
+ \deprecated [6.5] Use errorOccurred(QAbstractSocket::SocketError error) instead.
+*/
+#endif
+
QT_END_NAMESPACE