Correct cast
[contrib/qtwebsockets.git] / src / websockets / qwebsocketprotocol_p.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qwebsocketprotocol_p.h"
43 #include <QString>
44 #include <QSet>
45 #include <QtEndian>
46
47 /*!
48     \enum QWebSocketProtocol::CloseCode
49
50     \inmodule QtWebSockets
51
52     The close codes supported by WebSockets V13
53
54     \value CC_NORMAL                    Normal closure
55     \value CC_GOING_AWAY                Going away
56     \value CC_PROTOCOL_ERROR            Protocol error
57     \value CC_DATATYPE_NOT_SUPPORTED    Unsupported data
58     \value CC_RESERVED_1004             Reserved
59     \value CC_MISSING_STATUS_CODE       No status received
60     \value CC_ABNORMAL_DISCONNECTION    Abnormal closure
61     \value CC_WRONG_DATATYPE            Invalid frame payload data
62     \value CC_POLICY_VIOLATED           Policy violation
63     \value CC_TOO_MUCH_DATA             Message too big
64     \value CC_MISSING_EXTENSION         Mandatory extension missing
65     \value CC_BAD_OPERATION             Internal server error
66     \value CC_TLS_HANDSHAKE_FAILED      TLS handshake failed
67
68     \sa \l{QWebSocket::} {close()}
69 */
70 /*!
71     \enum QWebSocketProtocol::Version
72
73     \inmodule QtWebSockets
74
75     \brief The different defined versions of the Websocket protocol.
76
77     For an overview of the differences between the different protocols, see
78     <http://code.google.com/p/pywebsocket/wiki/WebSocketProtocolSpec>
79
80     \value V_Unknow
81     \value V_0          hixie76: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 & hybi-00: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00.
82                         Works with key1, key2 and a key in the payload.
83                         Attribute: Sec-WebSocket-Draft value 0.
84     \value V_4          hybi-04: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-04.txt.
85                         Changed handshake: key1, key2, key3 ==> Sec-WebSocket-Key, Sec-WebSocket-Nonce, Sec-WebSocket-Accept
86                         Sec-WebSocket-Draft renamed to Sec-WebSocket-Version
87                         Sec-WebSocket-Version = 4
88     \value V_5          hybi-05: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-05.txt.
89                         Sec-WebSocket-Version = 5
90                         Removed Sec-WebSocket-Nonce
91                         Added Sec-WebSocket-Accept
92     \value V_6          Sec-WebSocket-Version = 6.
93     \value V_7          hybi-07: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07.
94                         Sec-WebSocket-Version = 7
95     \value V_8          hybi-8, hybi-9, hybi-10, hybi-11 and hybi-12.
96                         Status codes 1005 and 1006 are added and all codes are now unsigned
97                         Internal error results in 1006
98     \value V_13         hybi-13, hybi14, hybi-15, hybi-16, hybi-17 and RFC 6455.
99                         Sec-WebSocket-Version = 13
100                         Status code 1004 is now reserved
101                         Added 1008, 1009 and 1010
102                         Must support TLS
103                         Clarify multiple version support
104     \value V_LATEST     Refers to the latest know version to QWebSockets.
105 */
106
107 /*!
108   \fn QWebSocketProtocol::isOpCodeReserved(OpCode code)
109   Checks if \a code is a valid OpCode
110   \internal
111 */
112
113 /*!
114   \fn QWebSocketProtocol::isCloseCodeValid(int closeCode)
115   Checks if \a closeCode is a valid web socket close code
116   \internal
117 */
118
119 /*!
120   \fn QWebSocketProtocol::currentVersion()
121   Returns the latest version that WebSocket is supporting
122   \internal
123 */
124
125 QT_BEGIN_NAMESPACE
126
127 /**
128  * @brief Contains constants related to the WebSocket standard.
129  */
130 namespace QWebSocketProtocol
131 {
132 /*!
133         Parses the \a versionString and converts it to a Version value
134         \internal
135     */
136 Version versionFromString(const QString &versionString)
137 {
138     bool ok = false;
139     Version version = V_Unknow;
140     const int ver = versionString.toInt(&ok);
141     QSet<Version> supportedVersions;
142     supportedVersions << V_0 << V_4 << V_5 << V_6 << V_7 << V_8 << V_13;
143     if (ok)
144     {
145         if (supportedVersions.contains(static_cast<Version>(ver)))
146         {
147             version = static_cast<Version>(ver);
148         }
149     }
150     return version;
151 }
152
153 /*!
154       Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
155       \internal
156     */
157 void mask(QByteArray *payload, quint32 maskingKey)
158 {
159     mask(payload->data(), payload->size(), maskingKey);
160 }
161
162 /*!
163       Masks the \a payload of length \a size with the given \a maskingKey and stores the result back in \a payload.
164       \internal
165     */
166 void mask(char *payload, quint64 size, quint32 maskingKey)
167 {
168     quint32 *payloadData = reinterpret_cast<quint32 *>(payload);
169     const quint32 numIterations = static_cast<quint32>(size / sizeof(quint32));
170     const quint32 remainder = size % sizeof(quint32);
171     quint32 i;
172     for (i = 0; i < numIterations; ++i)
173     {
174         *(payloadData + i) ^= maskingKey;
175     }
176     if (remainder)
177     {
178         const quint32 offset = i * static_cast<quint32>(sizeof(quint32));
179         const char *mask = reinterpret_cast<const char *>(&maskingKey);
180         for (quint32 i = 0; i < remainder; ++i)
181         {
182             *(payload + offset + i) ^= (mask[(i + offset) % 4]);
183         }
184     }
185 }
186 }       //end namespace WebSocketProtocol
187
188 QT_END_NAMESPACE