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