2 // Copyright (c) 2016-2019 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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_WEBSOCKET_RFC6455_HPP
11 #define BOOST_BEAST_WEBSOCKET_RFC6455_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/static_string.hpp>
15 #include <boost/beast/core/string.hpp>
16 #include <boost/beast/http/empty_body.hpp>
17 #include <boost/beast/http/message.hpp>
18 #include <boost/beast/http/string_body.hpp>
26 /// The type of object holding HTTP Upgrade requests
27 using request_type = http::request<http::empty_body>;
29 /// The type of object holding HTTP Upgrade responses
30 using response_type = http::response<http::string_body>;
32 /** Returns `true` if the specified HTTP request is a WebSocket Upgrade.
34 This function returns `true` when the passed HTTP Request
35 indicates a WebSocket Upgrade. It does not validate the
36 contents of the fields: it just trivially accepts requests
37 which could only possibly be a valid or invalid WebSocket
40 Callers who wish to manually read HTTP requests in their
41 server implementation can use this function to determine if
42 the request should be routed to an instance of
43 @ref websocket::stream.
47 void handle_connection(net::ip::tcp::socket& sock)
49 boost::beast::flat_buffer buffer;
50 boost::beast::http::request<boost::beast::http::string_body> req;
51 boost::beast::http::read(sock, buffer, req);
52 if(boost::beast::websocket::is_upgrade(req))
54 boost::beast::websocket::stream<decltype(sock)> ws{std::move(sock)};
60 @param req The HTTP Request object to check.
62 @return `true` if the request is a WebSocket Upgrade.
64 template<class Allocator>
66 is_upgrade(beast::http::header<true,
67 http::basic_fields<Allocator>> const& req);
69 /** Close status codes.
71 These codes accompany close frames.
73 @see <a href="https://tools.ietf.org/html/rfc6455#section-7.4.1">RFC 6455 7.4.1 Defined Status Codes</a>
75 enum close_code : std::uint16_t
77 /// Normal closure; the connection successfully completed whatever purpose for which it was created.
80 /// 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.
83 /// The endpoint is terminating the connection due to a protocol error.
84 protocol_error = 1002,
86 /// 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).
89 /// 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).
92 /// 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.
95 /// The endpoint is terminating the connection because a data frame was received that is too large.
98 /// The client is terminating the connection because it expected the server to negotiate one or more extension, but the server didn't.
99 needs_extension = 1010,
101 /// The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
102 internal_error = 1011,
104 /// The server is terminating the connection because it is restarting.
105 service_restart = 1012,
107 /// The server is terminating the connection due to a temporary condition, e.g. it is overloaded and is casting off some of its clients.
108 try_again_later = 1013,
112 // The following are illegal on the wire
115 /** Used internally to mean "no error"
117 This code is reserved and may not be sent.
121 /** Reserved for future use by the WebSocket standard.
123 This code is reserved and may not be sent.
127 /** No status code was provided even though one was expected.
129 This code is reserved and may not be sent.
133 /** Connection was closed without receiving a close frame
135 This code is reserved and may not be sent.
139 /** Reserved for future use by the WebSocket standard.
141 This code is reserved and may not be sent.
145 /** Reserved for future use by the WebSocket standard.
147 This code is reserved and may not be sent.
154 //last = 5000 // satisfy warnings
157 /// The type representing the reason string in a close frame.
158 using reason_string = static_string<123, char>;
160 /// The type representing the payload of ping and pong messages.
161 using ping_data = static_string<125, char>;
163 /** Description of the close reason.
165 This object stores the close code (if any) and the optional
166 utf-8 encoded implementation defined reason string.
171 std::uint16_t code = close_code::none;
173 /// The optional utf8-encoded reason string.
174 reason_string reason;
176 /** Default constructor.
178 The code will be none. Default constructed objects
179 will explicitly convert to bool as `false`.
181 close_reason() = default;
183 /// Construct from a code.
184 close_reason(std::uint16_t code_)
189 /// Construct from a reason string. code is @ref close_code::normal.
190 close_reason(string_view s)
191 : code(close_code::normal)
196 /// Construct from a reason string literal. code is @ref close_code::normal.
197 close_reason(char const* s)
198 : code(close_code::normal)
203 /// Construct from a close code and reason string.
204 close_reason(close_code code_, string_view s)
210 /// Returns `true` if a code was specified
211 operator bool() const
213 return code != close_code::none;
221 #include <boost/beast/websocket/impl/rfc6455.hpp>