Limit line length to 100 characters.
[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   \fn QWebSocketProtocol::isOpCodeReserved(OpCode code)
119   Checks if \a code is a valid OpCode
120
121   \internal
122 */
123
124 /*!
125   \fn QWebSocketProtocol::isCloseCodeValid(int closeCode)
126   Checks if \a closeCode is a valid web socket close code
127
128   \internal
129 */
130
131 /*!
132   \fn QWebSocketProtocol::currentVersion()
133   Returns the latest version that WebSocket is supporting
134
135   \internal
136 */
137
138 /*!
139     Parses the \a versionString and converts it to a Version value
140
141     \internal
142 */
143 QWebSocketProtocol::Version QWebSocketProtocol::versionFromString(const QString &versionString)
144 {
145     bool ok = false;
146     Version version = V_Unknow;
147     const int ver = versionString.toInt(&ok);
148     QSet<Version> supportedVersions;
149     supportedVersions << V_0 << V_4 << V_5 << V_6 << V_7 << V_8 << V_13;
150     if (Q_LIKELY(ok) && (supportedVersions.contains(static_cast<Version>(ver))))
151         version = static_cast<Version>(ver);
152     return version;
153 }
154
155 /*!
156     Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
157
158     \internal
159 */
160 void QWebSocketProtocol::mask(QByteArray *payload, quint32 maskingKey)
161 {
162     Q_ASSERT(payload);
163     mask(payload->data(), payload->size(), maskingKey);
164 }
165
166 /*!
167     Masks the \a payload of length \a size with the given \a maskingKey and
168     stores the result back in \a payload.
169
170     \internal
171 */
172 void QWebSocketProtocol::mask(char *payload, quint64 size, quint32 maskingKey)
173 {
174     Q_ASSERT(payload);
175     const quint8 mask[] = { quint8((maskingKey & 0xFF000000u) >> 24),
176                             quint8((maskingKey & 0x00FF0000u) >> 16),
177                             quint8((maskingKey & 0x0000FF00u) >> 8),
178                             quint8((maskingKey & 0x000000FFu))
179                           };
180     int i = 0;
181     while (size-- > 0)
182         *payload++ ^= mask[i++ % 4];
183 }
184
185 QT_END_NAMESPACE