Added copyright disclaimer to all files
[contrib/qtwebsockets.git] / src / qwebsocketprotocol.h
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 #ifndef QWEBSOCKETPROTOCOL_H
20 #define QWEBSOCKETPROTOCOL_H
21
22 #include <qglobal.h>
23
24 QT_BEGIN_NAMESPACE
25
26 class QString;
27 class QByteArray;
28
29 namespace QWebSocketProtocol
30 {
31         enum Version
32         {
33                 V_Unknow = -1,
34                 V_0 = 0,
35                 //hybi-01, hybi-02 and hybi-03 not supported
36                 V_4 = 4,
37                 V_5 = 5,
38                 V_6 = 6,
39                 V_7 = 7,
40                 V_8 = 8,
41                 V_13 = 13,
42                 V_LATEST = V_13
43         };
44
45         Version versionFromString(const QString &versionString);
46
47         enum CloseCode
48         {
49                 CC_NORMAL                                       = 1000,
50                 CC_GOING_AWAY                           = 1001,
51                 CC_PROTOCOL_ERROR                       = 1002,
52                 CC_DATATYPE_NOT_SUPPORTED       = 1003,
53                 CC_RESERVED_1004                        = 1004,
54                 CC_MISSING_STATUS_CODE          = 1005,
55                 CC_ABNORMAL_DISCONNECTION       = 1006,
56                 CC_WRONG_DATATYPE                       = 1007,
57                 CC_POLICY_VIOLATED                      = 1008,
58                 CC_TOO_MUCH_DATA                        = 1009,
59                 CC_MISSING_EXTENSION            = 1010,
60                 CC_BAD_OPERATION                        = 1011,
61                 CC_TLS_HANDSHAKE_FAILED         = 1015
62         };
63
64         enum OpCode
65         {
66                 OC_CONTINUE             = 0x0,
67                 OC_TEXT                 = 0x1,
68                 OC_BINARY               = 0x2,
69                 OC_RESERVED_3   = 0x3,
70                 OC_RESERVED_4   = 0x4,
71                 OC_RESERVED_5   = 0x5,
72                 OC_RESERVED_6   = 0x6,
73                 OC_RESERVED_7   = 0x7,
74                 OC_CLOSE                = 0x8,
75                 OC_PING                 = 0x9,
76                 OC_PONG                 = 0xA,
77                 OC_RESERVED_B   = 0xB,
78                 OC_RESERVED_V   = 0xC,
79                 OC_RESERVED_D   = 0xD,
80                 OC_RESERVED_E   = 0xE,
81                 OC_RESERVED_F   = 0xF
82         };
83
84
85         inline bool isOpCodeReserved(OpCode code)
86         {
87                 return ((code > OC_BINARY) && (code < OC_CLOSE)) || (code > OC_PONG);
88         }
89         inline bool isCloseCodeValid(int closeCode)
90         {
91                 return  (closeCode > 999) && (closeCode < 5000) &&
92                                 (closeCode != CC_RESERVED_1004) &&          //see RFC6455 7.4.1
93                                 (closeCode != CC_MISSING_STATUS_CODE) &&
94                                 (closeCode != CC_ABNORMAL_DISCONNECTION) &&
95                                 ((closeCode >= 3000) || (closeCode < 1012));
96         }
97
98         void mask(QByteArray *payload, quint32 maskingKey);
99         void mask(char *payload, quint64 size, quint32 maskingKey);
100
101         inline Version currentVersion() { return V_LATEST; }
102
103 }       //end namespace QWebSocketProtocol
104
105 QT_END_NAMESPACE
106
107 #endif // QWEBSOCKETPROTOCOL_H