Update documentation
[contrib/qtwebsockets.git] / src / websockets / qwebsocketprotocol.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 "qwebsocketprotocol_p.h"
43 #include <QtCore/QString>
44 #include <QtCore/QSet>
45 #include <QtCore/QtEndian>
46
47 QT_BEGIN_NAMESPACE
48
49 /*!
50   \namespace QWebSocketProtocol
51   \inmodule QtWebSockets
52   \brief Contains constants related to the WebSocket standard.
53 */
54
55 /*!
56     \enum QWebSocketProtocol::CloseCode
57
58     \inmodule QtWebSockets
59
60     The close codes supported by WebSockets V13
61
62     \value CC_NORMAL                    Normal closure
63     \value CC_GOING_AWAY                Going away
64     \value CC_PROTOCOL_ERROR            Protocol error
65     \value CC_DATATYPE_NOT_SUPPORTED    Unsupported data
66     \value CC_RESERVED_1004             Reserved
67     \value CC_MISSING_STATUS_CODE       No status received
68     \value CC_ABNORMAL_DISCONNECTION    Abnormal closure
69     \value CC_WRONG_DATATYPE            Invalid frame payload data
70     \value CC_POLICY_VIOLATED           Policy violation
71     \value CC_TOO_MUCH_DATA             Message too big
72     \value CC_MISSING_EXTENSION         Mandatory extension missing
73     \value CC_BAD_OPERATION             Internal server error
74     \value CC_TLS_HANDSHAKE_FAILED      TLS handshake failed
75
76     \sa QWebSocket::close()
77 */
78 /*!
79     \enum QWebSocketProtocol::Version
80
81     \inmodule QtWebSockets
82
83     \brief The different defined versions of the Websocket protocol.
84
85     For an overview of the differences between the different protocols, see
86     <http://code.google.com/p/pywebsocket/wiki/WebSocketProtocolSpec>
87
88     \value V_Unknow
89     \value V_0        hixie76: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 &
90                       hybi-00: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00.
91                       Works with key1, key2 and a key in the payload.
92                       Attribute: Sec-WebSocket-Draft value 0.
93     \value V_4        hybi-04: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-04.txt.
94                       Changed handshake: key1, key2, key3
95                       ==> Sec-WebSocket-Key, Sec-WebSocket-Nonce, Sec-WebSocket-Accept
96                       Sec-WebSocket-Draft renamed to Sec-WebSocket-Version
97                       Sec-WebSocket-Version = 4
98     \value V_5        hybi-05: http://tools.ietf.org/id/draft-ietf-hybi-thewebsocketprotocol-05.txt.
99                       Sec-WebSocket-Version = 5
100                       Removed Sec-WebSocket-Nonce
101                       Added Sec-WebSocket-Accept
102     \value V_6        Sec-WebSocket-Version = 6.
103     \value V_7        hybi-07: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07.
104                       Sec-WebSocket-Version = 7
105     \value V_8        hybi-8, hybi-9, hybi-10, hybi-11 and hybi-12.
106                       Status codes 1005 and 1006 are added and all codes are now unsigned
107                       Internal error results in 1006
108     \value V_13       hybi-13, hybi14, hybi-15, hybi-16, hybi-17 and RFC 6455.
109                       Sec-WebSocket-Version = 13
110                       Status code 1004 is now reserved
111                       Added 1008, 1009 and 1010
112                       Must support TLS
113                       Clarify multiple version support
114     \value V_LATEST   Refers to the latest know version to QWebSockets.
115 */
116
117 /*!
118     \enum QWebSocketProtocol::OpCode
119
120     \inmodule QtWebSockets
121
122     The frame opcodes as defined by the WebSockets standard
123
124     \value OC_CONTINUE      Continuation frame
125     \value OC_TEXT          Text frame
126     \value OC_BINARY        Binary frame
127     \value OC_RESERVED_3    Reserved
128     \value OC_RESERVED_4    Reserved
129     \value OC_RESERVED_5    Reserved
130     \value OC_RESERVED_6    Reserved
131     \value OC_RESERVED_7    Reserved
132     \value OC_CLOSE         Close frame
133     \value OC_PING          Ping frame
134     \value OC_PONG          Pong frame
135     \value OC_RESERVED_B    Reserved
136     \value OC_RESERVED_C    Reserved
137     \value OC_RESERVED_D    Reserved
138     \value OC_RESERVED_E    Reserved
139     \value OC_RESERVED_F    Reserved
140
141     \internal
142 */
143
144 /*!
145   \fn QWebSocketProtocol::isOpCodeReserved(OpCode code)
146   Checks if \a code is a valid OpCode
147
148   \internal
149 */
150
151 /*!
152   \fn QWebSocketProtocol::isCloseCodeValid(int closeCode)
153   Checks if \a closeCode is a valid web socket close code
154
155   \internal
156 */
157
158 /*!
159   \fn QWebSocketProtocol::currentVersion()
160   Returns the latest version that WebSocket is supporting
161
162   \internal
163 */
164
165 /*!
166     Parses the \a versionString and converts it to a Version value
167
168     \internal
169 */
170 QWebSocketProtocol::Version QWebSocketProtocol::versionFromString(const QString &versionString)
171 {
172     bool ok = false;
173     Version version = V_Unknow;
174     const int ver = versionString.toInt(&ok);
175     QSet<Version> supportedVersions;
176     supportedVersions << V_0 << V_4 << V_5 << V_6 << V_7 << V_8 << V_13;
177     if (Q_LIKELY(ok) && (supportedVersions.contains(static_cast<Version>(ver))))
178         version = static_cast<Version>(ver);
179     return version;
180 }
181
182 /*!
183     Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
184
185     \internal
186 */
187 void QWebSocketProtocol::mask(QByteArray *payload, quint32 maskingKey)
188 {
189     Q_ASSERT(payload);
190     mask(payload->data(), payload->size(), maskingKey);
191 }
192
193 /*!
194     Masks the \a payload of length \a size with the given \a maskingKey and
195     stores the result back in \a payload.
196
197     \internal
198 */
199 void QWebSocketProtocol::mask(char *payload, quint64 size, quint32 maskingKey)
200 {
201     Q_ASSERT(payload);
202     const quint8 mask[] = { quint8((maskingKey & 0xFF000000u) >> 24),
203                             quint8((maskingKey & 0x00FF0000u) >> 16),
204                             quint8((maskingKey & 0x0000FF00u) >> 8),
205                             quint8((maskingKey & 0x000000FFu))
206                           };
207     int i = 0;
208     while (size-- > 0)
209         *payload++ ^= mask[i++ % 4];
210 }
211
212 QT_END_NAMESPACE