1 #include "qwebsocketprotocol.h"
7 \enum WebSocketProtocol::CloseCode
9 The close codes supported by WebSockets V13
10 \sa WebSocket::close()
13 \var WebSocketProtocol::CloseCode::CC_NORMAL
17 \var WebSocketProtocol::CloseCode::CC_GOING_AWAY
21 \var WebSocketProtocol::CloseCode::CC_PROTOCOL_ERROR
25 \var WebSocketProtocol::CloseCode::CC_DATATYPE_NOT_SUPPORTED
29 \var WebSocketProtocol::CloseCode::CC_RESERVED_1004
33 \var WebSocketProtocol::CloseCode::CC_MISSING_STATUS_CODE
37 \var WebSocketProtocol::CloseCode::CC_ABNORMAL_DISCONNECTION
41 \var WebSocketProtocol::CloseCode::CC_WRONG_DATATYPE
42 Invalid frame payload data
45 \var WebSocketProtocol::CloseCode::CC_POLICY_VIOLATED
49 \var WebSocketProtocol::CloseCode::CC_TOO_MUCH_DATA
53 \var WebSocketProtocol::CloseCode::CC_MISSING_EXTENSION
54 Mandatory extension missing
57 \var WebSocketProtocol::CloseCode::CC_BAD_OPERATION
61 \var WebSocketProtocol::CloseCode::CC_TLS_HANDSHAKE_FAILED
65 \enum WebSocketProtocol::Version
67 \brief The different defined versions of the Websocket protocol.
69 For an overview of the differences between the different protocols, see
70 <http://code.google.com/p/pywebsocket/wiki/WebSocketProtocolSpec>
73 \var WebSocketProtocol::Version::V_Unknow
76 \var WebSocketProtocol::Version::V_0
77 hixie76: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 & hybi-00: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00.
78 Works with key1, key2 and a key in the payload.\n
79 Attribute: Sec-WebSocket-Draft value 0.
82 \var WebSocketProtocol::Version::V_4
83 hybi-04: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-04.txt.
84 Changed handshake: key1, key2, key3 ==> Sec-WebSocket-Key, Sec-WebSocket-Nonce, Sec-WebSocket-Accept\n
85 Sec-WebSocket-Draft renamed to Sec-WebSocket-Version\n
86 Sec-WebSocket-Version = 4
89 \var WebSocketProtocol::Version::V_5
90 hybi-05: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-05.txt.
91 Sec-WebSocket-Version = 5\n
92 Removed Sec-WebSocket-Nonce\n
93 Added Sec-WebSocket-Accept\n
96 \var WebSocketProtocol::Version::V_6
97 Sec-WebSocket-Version = 6.
100 \var WebSocketProtocol::Version::V_7
101 hybi-07: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07.
102 Sec-WebSocket-Version = 7
105 \var WebSocketProtocol::Version::V_8
106 hybi-8, hybi-9, hybi-10, hybi-11 and hybi-12.
107 Status codes 1005 and 1006 are added and all codes are now unsigned\n
108 Internal error results in 1006
111 \var WebSocketProtocol::Version::V_13
112 hybi-13, hybi14, hybi-15, hybi-16, hybi-17 and RFC 6455.
113 Sec-WebSocket-Version = 13\n
114 Status code 1004 is now reserved\n
115 Added 1008, 1009 and 1010\n
117 Clarify multiple version support
120 \var WebSocketProtocol::Version::V_LATEST
121 Refers to the latest know version to QWebSockets.
125 \fn WebSocketProtocol::isOpCodeReserved(OpCode code)
126 Checks if \a code is a valid OpCode
131 \fn WebSocketProtocol::isCloseCodeValid(int closeCode)
132 Checks if \a closeCode is a valid web socket close code
137 \fn WebSocketProtocol::getCurrentVersion()
138 Returns the latest version that WebSocket is supporting
143 * @brief Contains constants related to the WebSocket standard.
145 namespace QWebSocketProtocol
148 Parses the \a versionString and converts it to a Version value
151 Version versionFromString(const QString &versionString)
154 Version version = V_Unknow;
155 int ver = versionString.toInt(&ok);
156 QSet<Version> supportedVersions;
157 supportedVersions << V_0 << V_4 << V_5 << V_6 << V_7 << V_8 << V_13;
160 if (supportedVersions.contains(static_cast<Version>(ver)))
162 version = static_cast<Version>(ver);
169 Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
172 void mask(QByteArray *payload, quint32 maskingKey)
174 quint32 *payloadData = reinterpret_cast<quint32 *>(payload->data());
175 quint32 numIterations = static_cast<quint32>(payload->size()) / sizeof(quint32);
176 quint32 remainder = static_cast<quint32>(payload->size()) % sizeof(quint32);
178 for (i = 0; i < numIterations; ++i)
180 *(payloadData + i) ^= maskingKey;
184 const quint32 offset = i * static_cast<quint32>(sizeof(quint32));
185 char *payloadBytes = payload->data();
186 uchar *mask = reinterpret_cast<uchar *>(&maskingKey);
187 for (quint32 i = 0; i < remainder; ++i)
189 *(payloadBytes + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
195 Masks the \a payload of length \a size with the given \a maskingKey and stores the result back in \a payload.
198 void mask(char *payload, quint64 size, quint32 maskingKey)
200 quint32 *payloadData = reinterpret_cast<quint32 *>(payload);
201 quint32 numIterations = static_cast<quint32>(size / sizeof(quint32));
202 quint32 remainder = size % sizeof(quint32);
204 for (i = 0; i < numIterations; ++i)
206 *(payloadData + i) ^= maskingKey;
210 const quint32 offset = i * static_cast<quint32>(sizeof(quint32));
211 uchar *mask = reinterpret_cast<uchar *>(&maskingKey);
212 for (quint32 i = 0; i < remainder; ++i)
214 *(payload + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
218 } //end namespace WebSocketProtocol