diff options
author | Arno Rehn <a.rehn@menlosystems.com> | 2022-01-02 22:31:03 +0100 |
---|---|---|
committer | Arno Rehn <a.rehn@menlosystems.com> | 2022-03-30 17:45:18 +0200 |
commit | cc4c24b99a87629aeb60df5af96d9bb991b56635 (patch) | |
tree | 62459a42457b0680022bb1fcb759f9c4e64d2c4b /src/websockets/qwebsockethandshakeresponse.cpp | |
parent | 8545bb57efbfabf0dc7bc4b76efd6a99b4022669 (diff) | |
download | qtwebsockets-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/qwebsockethandshakeresponse.cpp')
-rw-r--r-- | src/websockets/qwebsockethandshakeresponse.cpp | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/websockets/qwebsockethandshakeresponse.cpp b/src/websockets/qwebsockethandshakeresponse.cpp index d3ef609..5e4a061 100644 --- a/src/websockets/qwebsockethandshakeresponse.cpp +++ b/src/websockets/qwebsockethandshakeresponse.cpp @@ -161,9 +161,21 @@ QString QWebSocketHandshakeResponse::getHandshakeResponse( } else { if (request.isValid()) { const QString acceptKey = calculateAcceptKey(request.key()); - const QList<QString> matchingProtocols = - listIntersection(supportedProtocols, request.protocols(), - std::less<QString>()); + + // Find first client protocol that is supported. Order is important! + const QString protocol = [&] { + const auto clientProtocols = request.protocols(); + + const auto isSupportedProtocol = [&](const QString &protocol) { + return supportedProtocols.contains(protocol); + }; + const auto it = std::find_if( + clientProtocols.constBegin(), clientProtocols.constEnd(), + isSupportedProtocol); + + return it == clientProtocols.constEnd() ? QString() : *it; + }(); + //TODO: extensions must be kept in the order in which they arrive //cannot use set.intersect() to get the supported extensions const QList<QString> matchingExtensions = @@ -182,8 +194,8 @@ QString QWebSocketHandshakeResponse::getHandshakeResponse( QStringLiteral("Upgrade: websocket") << QStringLiteral("Connection: Upgrade") << QStringLiteral("Sec-WebSocket-Accept: ") % acceptKey; - if (!matchingProtocols.isEmpty()) { - m_acceptedProtocol = matchingProtocols.first(); + if (!protocol.isEmpty()) { + m_acceptedProtocol = protocol; response << QStringLiteral("Sec-WebSocket-Protocol: ") % m_acceptedProtocol; } if (!matchingExtensions.isEmpty()) { |