Added check on QT_NO_NETWORKPROXY to include proxy functionality only when required
[contrib/qtwebsockets.git] / src / qwebsocketprotocol.cpp
1 #include "qwebsocketprotocol.h"
2 #include <QString>
3 #include <QSet>
4 #include <QtEndian>
5
6 /*!
7         \enum WebSocketProtocol::CloseCode
8
9         The close codes supported by WebSockets V13
10         \sa WebSocket::close()
11 */
12 /*!
13         \var WebSocketProtocol::CloseCode::CC_NORMAL
14         Normal closure
15 */
16 /*!
17         \var WebSocketProtocol::CloseCode::CC_GOING_AWAY
18         Going away
19 */
20 /*!
21         \var WebSocketProtocol::CloseCode::CC_PROTOCOL_ERROR
22         Protocol error
23 */
24 /*!
25         \var WebSocketProtocol::CloseCode::CC_DATATYPE_NOT_SUPPORTED
26         Unsupported data
27 */
28 /*!
29         \var WebSocketProtocol::CloseCode::CC_RESERVED_1004
30         Reserved
31 */
32 /*!
33         \var WebSocketProtocol::CloseCode::CC_MISSING_STATUS_CODE
34         No status received
35 */
36 /*!
37         \var WebSocketProtocol::CloseCode::CC_ABNORMAL_DISCONNECTION
38         Abnormal closure
39 */
40 /*!
41         \var WebSocketProtocol::CloseCode::CC_WRONG_DATATYPE
42         Invalid frame payload data
43 */
44 /*!
45         \var WebSocketProtocol::CloseCode::CC_POLICY_VIOLATED
46         Policy violation
47 */
48 /*!
49         \var WebSocketProtocol::CloseCode::CC_TOO_MUCH_DATA
50         Message too big
51 */
52 /*!
53         \var WebSocketProtocol::CloseCode::CC_MISSING_EXTENSION
54         Mandatory extension missing
55 */
56 /*!
57         \var WebSocketProtocol::CloseCode::CC_BAD_OPERATION
58         Internal server error
59 */
60 /*!
61         \var WebSocketProtocol::CloseCode::CC_TLS_HANDSHAKE_FAILED
62         TLS handshake failed
63 */
64 /*!
65         \enum WebSocketProtocol::Version
66
67         \brief The different defined versions of the Websocket protocol.
68
69         For an overview of the differences between the different protocols, see
70         <http://code.google.com/p/pywebsocket/wiki/WebSocketProtocolSpec>
71 */
72 /*!
73         \var WebSocketProtocol::Version::V_Unknow
74 */
75 /*!
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.
80 */
81 /*!
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
87 */
88 /*!
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
94 */
95 /*!
96         \var WebSocketProtocol::Version::V_6
97         Sec-WebSocket-Version = 6.
98 */
99 /*!
100         \var WebSocketProtocol::Version::V_7
101         hybi-07: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07.
102         Sec-WebSocket-Version = 7
103 */
104 /*!
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
109 */
110 /*!
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
116         Must support TLS\n
117         Clarify multiple version support
118 */
119 /*!
120         \var WebSocketProtocol::Version::V_LATEST
121         Refers to the latest know version to QWebSockets.
122 */
123
124 /*!
125   \fn WebSocketProtocol::isOpCodeReserved(OpCode code)
126   Checks if \a code is a valid OpCode
127   \internal
128 */
129
130 /*!
131   \fn WebSocketProtocol::isCloseCodeValid(int closeCode)
132   Checks if \a closeCode is a valid web socket close code
133   \internal
134 */
135
136 /*!
137   \fn WebSocketProtocol::getCurrentVersion()
138   Returns the latest version that WebSocket is supporting
139   \internal
140 */
141
142 QT_BEGIN_NAMESPACE
143
144 /**
145  * @brief Contains constants related to the WebSocket standard.
146  */
147 namespace QWebSocketProtocol
148 {
149         /*!
150                 Parses the \a versionString and converts it to a Version value
151                 \internal
152         */
153         Version versionFromString(const QString &versionString)
154         {
155                 bool ok = false;
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;
160                 if (ok)
161                 {
162                         if (supportedVersions.contains(static_cast<Version>(ver)))
163                         {
164                                 version = static_cast<Version>(ver);
165                         }
166                 }
167                 return version;
168         }
169
170         /*!
171           Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
172           \internal
173         */
174         void mask(QByteArray *payload, quint32 maskingKey)
175         {
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);
179                 quint32 i;
180                 for (i = 0; i < numIterations; ++i)
181                 {
182                         *(payloadData + i) ^= maskingKey;
183                 }
184                 if (remainder)
185                 {
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)
190                         {
191                                 *(payloadBytes + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
192                         }
193                 }
194         }
195
196         /*!
197           Masks the \a payload of length \a size with the given \a maskingKey and stores the result back in \a payload.
198           \internal
199         */
200         void mask(char *payload, quint64 size, quint32 maskingKey)
201         {
202                 quint32 *payloadData = reinterpret_cast<quint32 *>(payload);
203                 quint32 numIterations = static_cast<quint32>(size / sizeof(quint32));
204                 quint32 remainder = size % sizeof(quint32);
205                 quint32 i;
206                 for (i = 0; i < numIterations; ++i)
207                 {
208                         *(payloadData + i) ^= maskingKey;
209                 }
210                 if (remainder)
211                 {
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)
215                         {
216                                 *(payload + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
217                         }
218                 }
219         }
220 }       //end namespace WebSocketProtocol
221
222 QT_END_NAMESPACE