Made accessor methods const; made serverName parameter const in QWebSocket
[contrib/qtwebsockets.git] / src / qwebsocketprotocol.cpp
1 /*
2 QWebSockets implements the WebSocket protocol as defined in RFC 6455.
3 Copyright (C) 2013 Kurt Pattyn (pattyn.kurt@gmail.com)
4
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.
9
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.
14
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
18 */
19
20 #include "qwebsocketprotocol.h"
21 #include <QString>
22 #include <QSet>
23 #include <QtEndian>
24
25 /*!
26     \enum WebSocketProtocol::CloseCode
27
28     The close codes supported by WebSockets V13
29     \sa WebSocket::close()
30 */
31 /*!
32     \var WebSocketProtocol::CloseCode::CC_NORMAL
33     Normal closure
34 */
35 /*!
36     \var WebSocketProtocol::CloseCode::CC_GOING_AWAY
37     Going away
38 */
39 /*!
40     \var WebSocketProtocol::CloseCode::CC_PROTOCOL_ERROR
41     Protocol error
42 */
43 /*!
44     \var WebSocketProtocol::CloseCode::CC_DATATYPE_NOT_SUPPORTED
45     Unsupported data
46 */
47 /*!
48     \var WebSocketProtocol::CloseCode::CC_RESERVED_1004
49     Reserved
50 */
51 /*!
52     \var WebSocketProtocol::CloseCode::CC_MISSING_STATUS_CODE
53     No status received
54 */
55 /*!
56     \var WebSocketProtocol::CloseCode::CC_ABNORMAL_DISCONNECTION
57     Abnormal closure
58 */
59 /*!
60     \var WebSocketProtocol::CloseCode::CC_WRONG_DATATYPE
61     Invalid frame payload data
62 */
63 /*!
64     \var WebSocketProtocol::CloseCode::CC_POLICY_VIOLATED
65     Policy violation
66 */
67 /*!
68     \var WebSocketProtocol::CloseCode::CC_TOO_MUCH_DATA
69     Message too big
70 */
71 /*!
72     \var WebSocketProtocol::CloseCode::CC_MISSING_EXTENSION
73     Mandatory extension missing
74 */
75 /*!
76     \var WebSocketProtocol::CloseCode::CC_BAD_OPERATION
77     Internal server error
78 */
79 /*!
80     \var WebSocketProtocol::CloseCode::CC_TLS_HANDSHAKE_FAILED
81     TLS handshake failed
82 */
83 /*!
84     \enum WebSocketProtocol::Version
85
86     \brief The different defined versions of the Websocket protocol.
87
88     For an overview of the differences between the different protocols, see
89     <http://code.google.com/p/pywebsocket/wiki/WebSocketProtocolSpec>
90 */
91 /*!
92     \var WebSocketProtocol::Version::V_Unknow
93 */
94 /*!
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.
99 */
100 /*!
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
106 */
107 /*!
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
113 */
114 /*!
115     \var WebSocketProtocol::Version::V_6
116     Sec-WebSocket-Version = 6.
117 */
118 /*!
119     \var WebSocketProtocol::Version::V_7
120     hybi-07: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07.
121     Sec-WebSocket-Version = 7
122 */
123 /*!
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
128 */
129 /*!
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
135     Must support TLS\n
136     Clarify multiple version support
137 */
138 /*!
139     \var WebSocketProtocol::Version::V_LATEST
140     Refers to the latest know version to QWebSockets.
141 */
142
143 /*!
144   \fn WebSocketProtocol::isOpCodeReserved(OpCode code)
145   Checks if \a code is a valid OpCode
146   \internal
147 */
148
149 /*!
150   \fn WebSocketProtocol::isCloseCodeValid(int closeCode)
151   Checks if \a closeCode is a valid web socket close code
152   \internal
153 */
154
155 /*!
156   \fn WebSocketProtocol::getCurrentVersion()
157   Returns the latest version that WebSocket is supporting
158   \internal
159 */
160
161 QT_BEGIN_NAMESPACE
162
163 /**
164  * @brief Contains constants related to the WebSocket standard.
165  */
166 namespace QWebSocketProtocol
167 {
168 /*!
169         Parses the \a versionString and converts it to a Version value
170         \internal
171     */
172 Version versionFromString(const QString &versionString)
173 {
174     bool ok = false;
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;
179     if (ok)
180     {
181         if (supportedVersions.contains(static_cast<Version>(ver)))
182         {
183             version = static_cast<Version>(ver);
184         }
185     }
186     return version;
187 }
188
189 /*!
190       Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
191       \internal
192     */
193 void mask(QByteArray *payload, quint32 maskingKey)
194 {
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);
198     quint32 i;
199     for (i = 0; i < numIterations; ++i)
200     {
201         *(payloadData + i) ^= maskingKey;
202     }
203     if (remainder)
204     {
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)
209         {
210             *(payloadBytes + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
211         }
212     }
213 }
214
215 /*!
216       Masks the \a payload of length \a size with the given \a maskingKey and stores the result back in \a payload.
217       \internal
218     */
219 void mask(char *payload, quint64 size, quint32 maskingKey)
220 {
221     quint32 *payloadData = reinterpret_cast<quint32 *>(payload);
222     quint32 numIterations = static_cast<quint32>(size / sizeof(quint32));
223     quint32 remainder = size % sizeof(quint32);
224     quint32 i;
225     for (i = 0; i < numIterations; ++i)
226     {
227         *(payloadData + i) ^= maskingKey;
228     }
229     if (remainder)
230     {
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)
234         {
235             *(payload + offset + i) ^= static_cast<char>(mask[(i + offset) % 4]);
236         }
237     }
238 }
239 }       //end namespace WebSocketProtocol
240
241 QT_END_NAMESPACE