summaryrefslogtreecommitdiff
path: root/src/websockets/qwebsocket.cpp
diff options
context:
space:
mode:
authorArno Rehn <a.rehn@menlosystems.com>2022-01-02 22:31:03 +0100
committerArno Rehn <a.rehn@menlosystems.com>2022-03-30 17:45:18 +0200
commitcc4c24b99a87629aeb60df5af96d9bb991b56635 (patch)
tree62459a42457b0680022bb1fcb759f9c4e64d2c4b /src/websockets/qwebsocket.cpp
parent8545bb57efbfabf0dc7bc4b76efd6a99b4022669 (diff)
downloadqtwebsockets-cc4c24b99a87629aeb60df5af96d9bb991b56635.tar.gz
Add support for WebSocket Sub-Protocols
Sub-Protocol support follows RFC 6455 Sections 4.1 and 4.2. See also https://datatracker.ietf.org/doc/html/rfc6455. This patch introduces a new class QWebSocketHandshakeOptions which collects options for the WebSocket handshake. At the moment, this contains only accessors for sub-protocols. In the future, it can be extended with things like WebSocket extensions. [ChangeLog] Add support for WebSocket Sub-Protocols Fixes: QTBUG-38742 Change-Id: Ibdcef17f717f0a76caab54f65c550865df1ec78d Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/websockets/qwebsocket.cpp')
-rw-r--r--src/websockets/qwebsocket.cpp63
1 files changed, 58 insertions, 5 deletions
diff --git a/src/websockets/qwebsocket.cpp b/src/websockets/qwebsocket.cpp
index 63122d1..956c9dc 100644
--- a/src/websockets/qwebsocket.cpp
+++ b/src/websockets/qwebsocket.cpp
@@ -52,9 +52,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}.
@@ -332,6 +330,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 +483,7 @@ void QWebSocket::open(const QUrl &url)
{
Q_D(QWebSocket);
QNetworkRequest request(url);
- d->open(request, true);
+ d->open(request, QWebSocketHandshakeOptions{}, true);
}
/*!
@@ -498,7 +497,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);
}
/*!
@@ -654,6 +687,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
@@ -663,6 +706,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()
*/