2017-07-20 08:01:46 -07:00
//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-07-20 08:01:46 -07:00
2017-07-20 13:40:34 -07:00
# ifndef BOOST_BEAST_WEBSOCKET_RFC6455_HPP
# define BOOST_BEAST_WEBSOCKET_RFC6455_HPP
2017-07-20 08:01:46 -07:00
2017-07-20 13:40:34 -07:00
# include <boost/beast/config.hpp>
# include <boost/beast/core/static_string.hpp>
# include <boost/beast/core/string.hpp>
# include <boost/beast/http/message.hpp>
2017-07-20 08:01:46 -07:00
# include <array>
# include <cstdint>
2017-07-20 13:40:34 -07:00
namespace boost {
2017-07-20 08:01:46 -07:00
namespace beast {
namespace websocket {
2017-04-26 18:20:59 -07:00
/** Returns `true` if the specified HTTP request is a WebSocket Upgrade.
This function returns ` true ` when the passed HTTP Request
indicates a WebSocket Upgrade . It does not validate the
contents of the fields : it just trivially accepts requests
which could only possibly be a valid or invalid WebSocket
Upgrade message .
Callers who wish to manually read HTTP requests in their
server implementation can use this function to determine if
the request should be routed to an instance of
@ ref websocket : : stream .
2017-05-10 12:03:00 -07:00
@ par Example
@ code
void handle_connection ( boost : : asio : : ip : : tcp : : socket & sock )
{
2017-07-27 12:00:50 -07:00
boost : : beast : : flat_buffer buffer ;
boost : : beast : : http : : request < boost : : beast : : http : : string_body > req ;
boost : : beast : : http : : read ( sock , buffer , req ) ;
if ( boost : : beast : : websocket : : is_upgrade ( req ) )
2017-05-10 12:03:00 -07:00
{
2017-07-27 12:00:50 -07:00
boost : : beast : : websocket : : stream < decltype ( sock ) > ws { std : : move ( sock ) } ;
2017-05-10 12:03:00 -07:00
ws . accept ( req ) ;
}
}
@ endcode
2017-04-26 18:20:59 -07:00
@ param req The HTTP Request object to check .
@ return ` true ` if the request is a WebSocket Upgrade .
*/
2017-06-19 13:01:59 -07:00
template < class Allocator >
2017-04-26 18:20:59 -07:00
bool
2017-06-19 13:01:59 -07:00
is_upgrade ( beast : : http : : header < true ,
http : : basic_fields < Allocator > > const & req ) ;
2017-04-26 18:20:59 -07:00
2017-07-20 08:01:46 -07:00
/** Close status codes.
These codes accompany close frames .
2016-05-01 12:33:35 -04:00
@ see < a href = " https://tools.ietf.org/html/rfc6455#section-7.4.1 " > RFC 6455 7.4 .1 Defined Status Codes < / a >
2017-07-20 08:01:46 -07:00
*/
2017-05-13 09:44:16 -07:00
enum close_code : std : : uint16_t
2017-07-20 08:01:46 -07:00
{
2017-04-21 11:46:51 -07:00
/// Normal closure; the connection successfully completed whatever purpose for which it was created.
2017-07-20 08:01:46 -07:00
normal = 1000 ,
2017-04-21 11:46:51 -07:00
/// 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.
2017-07-20 08:01:46 -07:00
going_away = 1001 ,
2017-04-21 11:46:51 -07:00
/// The endpoint is terminating the connection due to a protocol error.
2017-07-20 08:01:46 -07:00
protocol_error = 1002 ,
2016-05-01 12:33:35 -04:00
2017-04-21 11:46:51 -07:00
/// 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).
2017-07-20 08:01:46 -07:00
unknown_data = 1003 ,
2016-05-15 16:22:25 -04:00
2017-04-21 11:46:51 -07:00
/// 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).
2017-07-20 08:01:46 -07:00
bad_payload = 1007 ,
2017-04-21 11:46:51 -07:00
/// 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.
2017-07-20 08:01:46 -07:00
policy_error = 1008 ,
2017-04-21 11:46:51 -07:00
/// The endpoint is terminating the connection because a data frame was received that is too large.
2017-07-20 08:01:46 -07:00
too_big = 1009 ,
2017-04-21 11:46:51 -07:00
/// The client is terminating the connection because it expected the server to negotiate one or more extension, but the server didn't.
2017-07-20 08:01:46 -07:00
needs_extension = 1010 ,
2017-04-21 11:46:51 -07:00
/// The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
2017-07-20 08:01:46 -07:00
internal_error = 1011 ,
2017-04-21 11:46:51 -07:00
/// The server is terminating the connection because it is restarting.
2017-07-20 08:01:46 -07:00
service_restart = 1012 ,
2017-04-21 11:46:51 -07:00
/// The server is terminating the connection due to a temporary condition, e.g. it is overloaded and is casting off some of its clients.
2017-07-20 08:01:46 -07:00
try_again_later = 1013 ,
2017-04-21 11:46:51 -07:00
//----
//
// The following are illegal on the wire
//
/** Used internally to mean "no error"
This code is reserved and may not be sent .
*/
none = 0 ,
/** Reserved for future use by the WebSocket standard.
This code is reserved and may not be sent .
*/
2017-07-20 08:01:46 -07:00
reserved1 = 1004 ,
2017-04-21 11:46:51 -07:00
/** No status code was provided even though one was expected.
This code is reserved and may not be sent .
*/
no_status = 1005 ,
/** Connection was closed without receiving a close frame
This code is reserved and may not be sent .
*/
abnormal = 1006 ,
/** Reserved for future use by the WebSocket standard.
This code is reserved and may not be sent .
*/
reserved2 = 1014 ,
/** Reserved for future use by the WebSocket standard.
This code is reserved and may not be sent .
*/
reserved3 = 1015
//
//----
//last = 5000 // satisfy warnings
2017-07-20 08:01:46 -07:00
} ;
2016-05-15 16:22:25 -04:00
/// The type representing the reason string in a close frame.
using reason_string = static_string < 123 , char > ;
/// The type representing the payload of ping and pong messages.
using ping_data = static_string < 125 , char > ;
2017-07-20 08:01:46 -07:00
/** Description of the close reason.
This object stores the close code ( if any ) and the optional
utf - 8 encoded implementation defined reason string .
*/
struct close_reason
{
/// The close code.
2017-04-21 11:46:51 -07:00
std : : uint16_t code = close_code : : none ;
2017-07-20 08:01:46 -07:00
/// The optional utf8-encoded reason string.
2016-05-15 16:22:25 -04:00
reason_string reason ;
2017-07-20 08:01:46 -07:00
/** Default constructor.
The code will be none . Default constructed objects
will explicitly convert to bool as ` false ` .
*/
close_reason ( ) = default ;
/// Construct from a code.
2017-04-21 11:46:51 -07:00
close_reason ( std : : uint16_t code_ )
2017-07-20 08:01:46 -07:00
: code ( code_ )
{
}
2017-05-13 09:44:16 -07:00
/// Construct from a reason string. code is @ref close_code::normal.
close_reason ( string_view s )
: code ( close_code : : normal )
, reason ( s )
{
}
/// Construct from a reason string literal. code is @ref close_code::normal.
close_reason ( char const * s )
2017-07-20 08:01:46 -07:00
: code ( close_code : : normal )
2017-05-13 09:44:16 -07:00
, reason ( s )
2017-07-20 08:01:46 -07:00
{
}
2017-05-13 09:44:16 -07:00
/// Construct from a close code and reason string.
close_reason ( close_code code_ , string_view s )
2017-07-20 08:01:46 -07:00
: code ( code_ )
2017-05-13 09:44:16 -07:00
, reason ( s )
2017-07-20 08:01:46 -07:00
{
}
/// Returns `true` if a code was specified
operator bool ( ) const
{
return code ! = close_code : : none ;
}
} ;
} // websocket
} // beast
2017-07-20 13:40:34 -07:00
} // boost
2017-07-20 08:01:46 -07:00
2017-07-20 13:40:34 -07:00
# include <boost/beast/websocket/impl/rfc6455.ipp>
2017-04-26 18:20:59 -07:00
2017-07-20 08:01:46 -07:00
# endif