Add optimizations
[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 "qwebsocketprotocol_p.h"
43 #include <QtCore/QString>
44 #include <QtCore/QSet>
45 #include <QtCore/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 (Q_LIKELY(ok)) {
144         if (supportedVersions.contains(static_cast<Version>(ver))) {
145             version = static_cast<Version>(ver);
146         }
147     }
148     return version;
149 }
150
151 /*!
152       Mask the \a payload with the given \a maskingKey and stores the result back in \a payload.
153       \internal
154     */
155 void mask(QByteArray *payload, quint32 maskingKey)
156 {
157     Q_ASSERT(payload);
158     mask(payload->data(), payload->size(), maskingKey);
159 }
160
161 /*!
162       Masks the \a payload of length \a size with the given \a maskingKey and stores the result back in \a payload.
163       \internal
164     */
165 void mask(char *payload, quint64 size, quint32 maskingKey)
166 {
167     Q_ASSERT(payload);
168     const quint8 mask[] = { quint8((maskingKey & 0xFF000000u) >> 24),
169                             quint8((maskingKey & 0x00FF0000u) >> 16),
170                             quint8((maskingKey & 0x0000FF00u) >> 8),
171                             quint8((maskingKey & 0x000000FFu))
172                           };
173     int i = 0;
174     while (size-- > 0) {
175         *payload++ ^= mask[i++ % 4];
176     }
177 }
178 }       //end namespace WebSocketProtocol
179
180 QT_END_NAMESPACE