2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 #ifndef BEAST_WEBSOCKET_RFC6455_HPP
9 #define BEAST_WEBSOCKET_RFC6455_HPP
11 #include <beast/config.hpp>
12 #include <beast/core/static_string.hpp>
13 #include <boost/optional.hpp>
20 /** WebSocket frame header opcodes. */
21 enum class opcode : std::uint8_t
41 /** Close status codes.
43 These codes accompany close frames.
45 @see <a href="https://tools.ietf.org/html/rfc6455#section-7.4.1">RFC 6455 7.4.1 Defined Status Codes</a>
50 /// Normal closure; the connection successfully completed whatever purpose for which it was created.
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.
56 /// The endpoint is terminating the connection due to a protocol error.
57 protocol_error = 1002,
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).
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).
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.
68 /// The endpoint is terminating the connection because a data frame was received that is too large.
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,
74 /// The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
75 internal_error = 1011,
77 /// The server is terminating the connection because it is restarting.
78 service_restart = 1012,
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,
85 // The following are illegal on the wire
88 /** Used internally to mean "no error"
90 This code is reserved and may not be sent.
94 /** Reserved for future use by the WebSocket standard.
96 This code is reserved and may not be sent.
100 /** No status code was provided even though one was expected.
102 This code is reserved and may not be sent.
106 /** Connection was closed without receiving a close frame
108 This code is reserved and may not be sent.
112 /** Reserved for future use by the WebSocket standard.
114 This code is reserved and may not be sent.
118 /** Reserved for future use by the WebSocket standard.
120 This code is reserved and may not be sent.
127 //last = 5000 // satisfy warnings
130 /// The type representing the reason string in a close frame.
131 using reason_string = static_string<123, char>;
133 /// The type representing the payload of ping and pong messages.
134 using ping_data = static_string<125, char>;
136 /** Description of the close reason.
138 This object stores the close code (if any) and the optional
139 utf-8 encoded implementation defined reason string.
144 std::uint16_t code = close_code::none;
146 /// The optional utf8-encoded reason string.
147 reason_string reason;
149 /** Default constructor.
151 The code will be none. Default constructed objects
152 will explicitly convert to bool as `false`.
154 close_reason() = default;
156 /// Construct from a code.
157 close_reason(std::uint16_t code_)
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)
170 /// Construct from a code and reason.
171 template<std::size_t N>
172 close_reason(close_code code_,
173 char const (&reason_)[N])
179 /// Returns `true` if a code was specified
180 operator bool() const
182 return code != close_code::none;