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
145 * @brief Contains constants related to the WebSocket standard.
147 namespace QWebSocketProtocol
150 Parses the \a versionString and converts it to a Version value
153 Version versionFromString(const QString &versionString)
156 Version version = V_Unknow;
157 int ver = versionString.toInt(&ok);
158 QSet<Version> supportedVersions;
159 supportedVersions << V_0 << V_4 << V_5 << V_6 << V_7 << V_8 << V_13;
162 if (supportedVersions.contains(static_cast<Version>(ver)))
164 version = static_cast<Version>(ver);
171 Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
174 void mask(QByteArray *payload, quint32 maskingKey)
176 quint32 *payloadData = reinterpret_cast<quint32 *>(payload->data());
177 quint32 numIterations = static_cast<quint32>(payload->size()) / sizeof(quint32);
178 quint32 remainder = static_cast<quint32>(payload->size()) % sizeof(quint32);
180 for (i = 0; i < numIterations; ++i)
182 *(payloadData + i) ^= maskingKey;
186 const quint32 offset = i * static_cast<quint32>(sizeof(quint32));
187 char *payloadBytes = payload->data();
188 uchar *mask = reinterpret_cast<uchar *>(&maskingKey);
189 for (quint32 i = 0; i < remainder; ++i)
191 *(payloadBytes + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
197 Masks the \a payload of length \a size with the given \a maskingKey and stores the result back in \a payload.
200 void mask(char *payload, quint64 size, quint32 maskingKey)
202 quint32 *payloadData = reinterpret_cast<quint32 *>(payload);
203 quint32 numIterations = static_cast<quint32>(size / sizeof(quint32));
204 quint32 remainder = size % sizeof(quint32);
206 for (i = 0; i < numIterations; ++i)
208 *(payloadData + i) ^= maskingKey;
212 const quint32 offset = i * static_cast<quint32>(sizeof(quint32));
213 uchar *mask = reinterpret_cast<uchar *>(&maskingKey);
214 for (quint32 i = 0; i < remainder; ++i)
216 *(payload + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
220 } //end namespace WebSocketProtocol