]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_WEBSOCKET_RFC6455_HPP | |
9 | #define BEAST_WEBSOCKET_RFC6455_HPP | |
10 | ||
11 | #include <beast/config.hpp> | |
12 | #include <beast/core/static_string.hpp> | |
13 | #include <boost/optional.hpp> | |
14 | #include <array> | |
15 | #include <cstdint> | |
16 | ||
17 | namespace beast { | |
18 | namespace websocket { | |
19 | ||
20 | /** WebSocket frame header opcodes. */ | |
21 | enum class opcode : std::uint8_t | |
22 | { | |
23 | cont = 0, | |
24 | text = 1, | |
25 | binary = 2, | |
26 | rsv3 = 3, | |
27 | rsv4 = 4, | |
28 | rsv5 = 5, | |
29 | rsv6 = 6, | |
30 | rsv7 = 7, | |
31 | close = 8, | |
32 | ping = 9, | |
33 | pong = 10, | |
34 | crsvb = 11, | |
35 | crsvc = 12, | |
36 | crsvd = 13, | |
37 | crsve = 14, | |
38 | crsvf = 15 | |
39 | }; | |
40 | ||
41 | /** Close status codes. | |
42 | ||
43 | These codes accompany close frames. | |
44 | ||
45 | @see <a href="https://tools.ietf.org/html/rfc6455#section-7.4.1">RFC 6455 7.4.1 Defined Status Codes</a> | |
46 | ||
47 | */ | |
48 | enum close_code | |
49 | { | |
50 | /// Normal closure; the connection successfully completed whatever purpose for which it was created. | |
51 | normal = 1000, | |
52 | ||
53 | /// The endpoint is going away, either because of a server failure or because the browser is navigating away from the page that opened the connection. | |
54 | going_away = 1001, | |
55 | ||
56 | /// The endpoint is terminating the connection due to a protocol error. | |
57 | protocol_error = 1002, | |
58 | ||
59 | /// The connection is being terminated because the endpoint received data of a type it cannot accept (for example, a text-only endpoint received binary data). | |
60 | unknown_data = 1003, | |
61 | ||
62 | /// The endpoint is terminating the connection because a message was received that contained inconsistent data (e.g., non-UTF-8 data within a text message). | |
63 | bad_payload = 1007, | |
64 | ||
65 | /// The endpoint is terminating the connection because it received a message that violates its policy. This is a generic status code, used when codes 1003 and 1009 are not suitable. | |
66 | policy_error = 1008, | |
67 | ||
68 | /// The endpoint is terminating the connection because a data frame was received that is too large. | |
69 | too_big = 1009, | |
70 | ||
71 | /// The client is terminating the connection because it expected the server to negotiate one or more extension, but the server didn't. | |
72 | needs_extension = 1010, | |
73 | ||
74 | /// The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request. | |
75 | internal_error = 1011, | |
76 | ||
77 | /// The server is terminating the connection because it is restarting. | |
78 | service_restart = 1012, | |
79 | ||
80 | /// The server is terminating the connection due to a temporary condition, e.g. it is overloaded and is casting off some of its clients. | |
81 | try_again_later = 1013, | |
82 | ||
83 | //---- | |
84 | // | |
85 | // The following are illegal on the wire | |
86 | // | |
87 | ||
88 | /** Used internally to mean "no error" | |
89 | ||
90 | This code is reserved and may not be sent. | |
91 | */ | |
92 | none = 0, | |
93 | ||
94 | /** Reserved for future use by the WebSocket standard. | |
95 | ||
96 | This code is reserved and may not be sent. | |
97 | */ | |
98 | reserved1 = 1004, | |
99 | ||
100 | /** No status code was provided even though one was expected. | |
101 | ||
102 | This code is reserved and may not be sent. | |
103 | */ | |
104 | no_status = 1005, | |
105 | ||
106 | /** Connection was closed without receiving a close frame | |
107 | ||
108 | This code is reserved and may not be sent. | |
109 | */ | |
110 | abnormal = 1006, | |
111 | ||
112 | /** Reserved for future use by the WebSocket standard. | |
113 | ||
114 | This code is reserved and may not be sent. | |
115 | */ | |
116 | reserved2 = 1014, | |
117 | ||
118 | /** Reserved for future use by the WebSocket standard. | |
119 | ||
120 | This code is reserved and may not be sent. | |
121 | */ | |
122 | reserved3 = 1015 | |
123 | ||
124 | // | |
125 | //---- | |
126 | ||
127 | //last = 5000 // satisfy warnings | |
128 | }; | |
129 | ||
130 | /// The type representing the reason string in a close frame. | |
131 | using reason_string = static_string<123, char>; | |
132 | ||
133 | /// The type representing the payload of ping and pong messages. | |
134 | using ping_data = static_string<125, char>; | |
135 | ||
136 | /** Description of the close reason. | |
137 | ||
138 | This object stores the close code (if any) and the optional | |
139 | utf-8 encoded implementation defined reason string. | |
140 | */ | |
141 | struct close_reason | |
142 | { | |
143 | /// The close code. | |
144 | std::uint16_t code = close_code::none; | |
145 | ||
146 | /// The optional utf8-encoded reason string. | |
147 | reason_string reason; | |
148 | ||
149 | /** Default constructor. | |
150 | ||
151 | The code will be none. Default constructed objects | |
152 | will explicitly convert to bool as `false`. | |
153 | */ | |
154 | close_reason() = default; | |
155 | ||
156 | /// Construct from a code. | |
157 | close_reason(std::uint16_t code_) | |
158 | : code(code_) | |
159 | { | |
160 | } | |
161 | ||
162 | /// Construct from a reason. code is close_code::normal. | |
163 | template<std::size_t N> | |
164 | close_reason(char const (&reason_)[N]) | |
165 | : code(close_code::normal) | |
166 | , reason(reason_) | |
167 | { | |
168 | } | |
169 | ||
170 | /// Construct from a code and reason. | |
171 | template<std::size_t N> | |
172 | close_reason(close_code code_, | |
173 | char const (&reason_)[N]) | |
174 | : code(code_) | |
175 | , reason(reason_) | |
176 | { | |
177 | } | |
178 | ||
179 | /// Returns `true` if a code was specified | |
180 | operator bool() const | |
181 | { | |
182 | return code != close_code::none; | |
183 | } | |
184 | }; | |
185 | ||
186 | } // websocket | |
187 | } // beast | |
188 | ||
189 | #endif |