2 QWebSockets implements the WebSocket protocol as defined in RFC 6455.
3 Copyright (C) 2013 Kurt Pattyn (pattyn.kurt@gmail.com)
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "qwebsocketprotocol.h"
26 \enum WebSocketProtocol::CloseCode
28 The close codes supported by WebSockets V13
29 \sa WebSocket::close()
32 \var WebSocketProtocol::CloseCode::CC_NORMAL
36 \var WebSocketProtocol::CloseCode::CC_GOING_AWAY
40 \var WebSocketProtocol::CloseCode::CC_PROTOCOL_ERROR
44 \var WebSocketProtocol::CloseCode::CC_DATATYPE_NOT_SUPPORTED
48 \var WebSocketProtocol::CloseCode::CC_RESERVED_1004
52 \var WebSocketProtocol::CloseCode::CC_MISSING_STATUS_CODE
56 \var WebSocketProtocol::CloseCode::CC_ABNORMAL_DISCONNECTION
60 \var WebSocketProtocol::CloseCode::CC_WRONG_DATATYPE
61 Invalid frame payload data
64 \var WebSocketProtocol::CloseCode::CC_POLICY_VIOLATED
68 \var WebSocketProtocol::CloseCode::CC_TOO_MUCH_DATA
72 \var WebSocketProtocol::CloseCode::CC_MISSING_EXTENSION
73 Mandatory extension missing
76 \var WebSocketProtocol::CloseCode::CC_BAD_OPERATION
80 \var WebSocketProtocol::CloseCode::CC_TLS_HANDSHAKE_FAILED
84 \enum WebSocketProtocol::Version
86 \brief The different defined versions of the Websocket protocol.
88 For an overview of the differences between the different protocols, see
89 <http://code.google.com/p/pywebsocket/wiki/WebSocketProtocolSpec>
92 \var WebSocketProtocol::Version::V_Unknow
95 \var WebSocketProtocol::Version::V_0
96 hixie76: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 & hybi-00: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00.
97 Works with key1, key2 and a key in the payload.\n
98 Attribute: Sec-WebSocket-Draft value 0.
101 \var WebSocketProtocol::Version::V_4
102 hybi-04: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-04.txt.
103 Changed handshake: key1, key2, key3 ==> Sec-WebSocket-Key, Sec-WebSocket-Nonce, Sec-WebSocket-Accept\n
104 Sec-WebSocket-Draft renamed to Sec-WebSocket-Version\n
105 Sec-WebSocket-Version = 4
108 \var WebSocketProtocol::Version::V_5
109 hybi-05: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-05.txt.
110 Sec-WebSocket-Version = 5\n
111 Removed Sec-WebSocket-Nonce\n
112 Added Sec-WebSocket-Accept\n
115 \var WebSocketProtocol::Version::V_6
116 Sec-WebSocket-Version = 6.
119 \var WebSocketProtocol::Version::V_7
120 hybi-07: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07.
121 Sec-WebSocket-Version = 7
124 \var WebSocketProtocol::Version::V_8
125 hybi-8, hybi-9, hybi-10, hybi-11 and hybi-12.
126 Status codes 1005 and 1006 are added and all codes are now unsigned\n
127 Internal error results in 1006
130 \var WebSocketProtocol::Version::V_13
131 hybi-13, hybi14, hybi-15, hybi-16, hybi-17 and RFC 6455.
132 Sec-WebSocket-Version = 13\n
133 Status code 1004 is now reserved\n
134 Added 1008, 1009 and 1010\n
136 Clarify multiple version support
139 \var WebSocketProtocol::Version::V_LATEST
140 Refers to the latest know version to QWebSockets.
144 \fn WebSocketProtocol::isOpCodeReserved(OpCode code)
145 Checks if \a code is a valid OpCode
150 \fn WebSocketProtocol::isCloseCodeValid(int closeCode)
151 Checks if \a closeCode is a valid web socket close code
156 \fn WebSocketProtocol::getCurrentVersion()
157 Returns the latest version that WebSocket is supporting
164 * @brief Contains constants related to the WebSocket standard.
166 namespace QWebSocketProtocol
169 Parses the \a versionString and converts it to a Version value
172 Version versionFromString(const QString &versionString)
175 Version version = V_Unknow;
176 int ver = versionString.toInt(&ok);
177 QSet<Version> supportedVersions;
178 supportedVersions << V_0 << V_4 << V_5 << V_6 << V_7 << V_8 << V_13;
181 if (supportedVersions.contains(static_cast<Version>(ver)))
183 version = static_cast<Version>(ver);
190 Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
193 void mask(QByteArray *payload, quint32 maskingKey)
195 quint32 *payloadData = reinterpret_cast<quint32 *>(payload->data());
196 quint32 numIterations = static_cast<quint32>(payload->size()) / sizeof(quint32);
197 quint32 remainder = static_cast<quint32>(payload->size()) % sizeof(quint32);
199 for (i = 0; i < numIterations; ++i)
201 *(payloadData + i) ^= maskingKey;
205 const quint32 offset = i * static_cast<quint32>(sizeof(quint32));
206 char *payloadBytes = payload->data();
207 uchar *mask = reinterpret_cast<uchar *>(&maskingKey);
208 for (quint32 i = 0; i < remainder; ++i)
210 *(payloadBytes + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
216 Masks the \a payload of length \a size with the given \a maskingKey and stores the result back in \a payload.
219 void mask(char *payload, quint64 size, quint32 maskingKey)
221 quint32 *payloadData = reinterpret_cast<quint32 *>(payload);
222 quint32 numIterations = static_cast<quint32>(size / sizeof(quint32));
223 quint32 remainder = size % sizeof(quint32);
225 for (i = 0; i < numIterations; ++i)
227 *(payloadData + i) ^= maskingKey;
231 const quint32 offset = i * static_cast<quint32>(sizeof(quint32));
232 uchar *mask = reinterpret_cast<uchar *>(&maskingKey);
233 for (quint32 i = 0; i < remainder; ++i)
235 *(payload + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
239 } //end namespace WebSocketProtocol