roar
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Roar::Session Class Reference

#include <session.hpp>

Collaboration diagram for Roar::Session:

Classes

struct  Implementation
 
class  ReadIntermediate
 Utility class to build up read operations. More...
 
class  SendIntermediate
 Helper class to prepare and send a response. More...
 

Public Member Functions

 Session (boost::asio::basic_stream_socket< boost::asio::ip::tcp > &&socket, boost::beast::basic_flat_buffer< std::allocator< char > > &&buffer, std::optional< std::variant< SslServerContext, boost::asio::ssl::context > > &sslContext, bool isSecure, std::function< void(Error &&)> onError, std::weak_ptr< Router > router, std::shared_ptr< const StandardResponseProvider > standardResponseProvider)
 
 ROAR_PIMPL_SPECIAL_FUNCTIONS (Session)
 
void close ()
 Calls shutdown on the socket.
 
template<typename BodyT , typename OriginalBodyT , typename... Forwards>
std::shared_ptr< SendIntermediate< BodyT > > send (Request< OriginalBodyT > const &req, Forwards &&... forwards)
 
template<typename BodyT >
std::shared_ptr< SendIntermediate< BodyT > > send (boost::beast::http::response< BodyT > &&res)
 
template<typename BodyT >
std::shared_ptr< SendIntermediate< BodyT > > send (Response< BodyT > &&res)
 
template<typename BodyT >
std::shared_ptr< SendIntermediate< BodyT > > sendWithAllAcceptedCors ()
 
Detail::PromiseTypeBind< Detail::PromiseTypeBindThen< std::size_t >, Detail::PromiseTypeBindFail< Error > > send (std::string message, std::chrono::seconds timeout=std::chrono::seconds{10})
 Send raw data. Use this after sending a proper response for sending additional data.
 
void readLimit (std::size_t bytesPerSecond)
 Sets a rate limit in bytes/second for this session for reading.
 
void writeLimit (std::size_t bytesPerSecond)
 Sets a rate limit in bytes/second for this session for writing.
 
template<typename BodyT , typename OriginalBodyT , typename... Forwards>
std::shared_ptr< ReadIntermediate< BodyT > > read (Request< OriginalBodyT > req, Forwards &&... forwardArgs)
 Read data from the client.
 
template<typename BodyT = boost::beast::http::empty_body, typename RequestBodyT , typename... Forwards>
Response< BodyT > prepareResponse (Request< RequestBodyT > const &req, Forwards &&... forwardArgs)
 Prepares a response with some header values already set.
 
template<typename BodyT = boost::beast::http::empty_body>
Response< BodyT > prepareResponse ()
 
template<typename FunctionT >
void withStreamDo (FunctionT &&func)
 std::visit for the underlying beast stream. Either ssl_stream or tcp_stream.
 
Detail::PromiseTypeBind< Detail::PromiseTypeBindThen< std::shared_ptr< WebsocketSession > >, Detail::PromiseTypeBindFail< Error const & > > upgrade (Request< boost::beast::http::empty_body > const &req)
 Turn this session into a websocket session. Will return an invalid shared_ptr if this is not an upgrade request.
 
RouteOptions const & routeOptions ()
 Retrieve the options defined by the request listener class for this route.
 
bool isSecure () const
 Returns whether or not this is an encrypted session.
 
void sendStandardResponse (boost::beast::http::status status, std::string_view additionalInfo="")
 Sends a standard response like "404 not found".
 
void sendStrictTransportSecurityResponse ()
 Sends a 403 with Strict-Transport-Security. Used only for unencrypted request on enforced HTTPS.
 
template<typename OriginalBodyT >
Detail::PromiseTypeBind< Detail::PromiseTypeBindThen<>, Detail::PromiseTypeBindFail< Error const & > > awaitClientClose (Request< OriginalBodyT > req, std::chrono::milliseconds timeout=std::chrono::seconds{3})
 Some clients insist on closing the connection on their own even if they received a response. To avoid this, this function can be called and the connection will be kept alive until the client closes it or a timeout is reached.
 

Static Public Attributes

static constexpr uint64_t defaultHeaderLimit {8_MiB}
 
static constexpr uint64_t defaultBodyLimit {8_MiB}
 
static constexpr std::chrono::seconds sessionTimeout {10}
 

Private Member Functions

void setupRouteOptions (RouteOptions options)
 
void readHeader ()
 
void performSslHandshake ()
 
bool onWriteComplete (bool expectsClose, boost::beast::error_code ec, std::size_t)
 
std::variant< Detail::StreamType, boost::beast::ssl_stream< Detail::StreamType > > & stream ()
 
std::shared_ptr< boost::beast::http::request_parser< boost::beast::http::empty_body > > & parser ()
 
boost::beast::flat_buffer & buffer ()
 
StandardResponseProvider const & standardResponseProvider ()
 
void startup (bool immediate=true)
 

Private Attributes

std::unique_ptr< Implementationimpl_
 

Friends

class Route
 
class Factory
 

Constructor & Destructor Documentation

◆ Session()

Roar::Session::Session ( boost::asio::basic_stream_socket< boost::asio::ip::tcp > &&  socket,
boost::beast::basic_flat_buffer< std::allocator< char > > &&  buffer,
std::optional< std::variant< SslServerContext, boost::asio::ssl::context > > &  sslContext,
bool  isSecure,
std::function< void(Error &&)>  onError,
std::weak_ptr< Router router,
std::shared_ptr< const StandardResponseProvider standardResponseProvider 
)

Member Function Documentation

◆ awaitClientClose()

template<typename OriginalBodyT >
Detail::PromiseTypeBind< Detail::PromiseTypeBindThen<>, Detail::PromiseTypeBindFail< Error const & > > Roar::Session::awaitClientClose ( Request< OriginalBodyT >  req,
std::chrono::milliseconds  timeout = std::chrono::seconds{3} 
)
inline

Some clients insist on closing the connection on their own even if they received a response. To avoid this, this function can be called and the connection will be kept alive until the client closes it or a timeout is reached.

Returns
A promise that resolves to true when the connection was closed by the client.

◆ buffer()

boost::beast::flat_buffer & Roar::Session::buffer ( )
private

◆ close()

void Roar::Session::close ( )

Calls shutdown on the socket.

◆ isSecure()

bool Roar::Session::isSecure ( ) const

Returns whether or not this is an encrypted session.

Returns
true Session is encrypted.
false Session is not encrypted.

◆ onWriteComplete()

bool Roar::Session::onWriteComplete ( bool  expectsClose,
boost::beast::error_code  ec,
std::size_t   
)
private

◆ parser()

std::shared_ptr< boost::beast::http::request_parser< boost::beast::http::empty_body > > & Roar::Session::parser ( )
private

◆ performSslHandshake()

void Roar::Session::performSslHandshake ( )
private

◆ prepareResponse() [1/2]

template<typename BodyT = boost::beast::http::empty_body>
Response< BodyT > Roar::Session::prepareResponse ( )
inline

◆ prepareResponse() [2/2]

template<typename BodyT = boost::beast::http::empty_body, typename RequestBodyT , typename... Forwards>
Response< BodyT > Roar::Session::prepareResponse ( Request< RequestBodyT > const &  req,
Forwards &&...  forwardArgs 
)
inline

Prepares a response with some header values already set.

Template Parameters
BodyT
Returns
Response<BodyT>

◆ read()

template<typename BodyT , typename OriginalBodyT , typename... Forwards>
std::shared_ptr< ReadIntermediate< BodyT > > Roar::Session::read ( Request< OriginalBodyT >  req,
Forwards &&...  forwardArgs 
)
inline

Read data from the client.

Template Parameters
BodyTWhat body type to read?
OriginalBodyTBody of the request that came before the this read.
Forwards
Parameters
reqA request that was received
forwardArgs
Returns
std::shared_ptr<ReadIntermediate<BodyT>> A class that can be used to start the reading process and set options.

◆ readHeader()

void Roar::Session::readHeader ( )
private

◆ readLimit()

void Roar::Session::readLimit ( std::size_t  bytesPerSecond)

Sets a rate limit in bytes/second for this session for reading.

Parameters
bytesPerSecond

◆ ROAR_PIMPL_SPECIAL_FUNCTIONS()

Roar::Session::ROAR_PIMPL_SPECIAL_FUNCTIONS ( Session  )

◆ routeOptions()

RouteOptions const & Roar::Session::routeOptions ( )

Retrieve the options defined by the request listener class for this route.

Returns
RouteOptions const& The route options.

◆ send() [1/4]

template<typename BodyT >
std::shared_ptr< SendIntermediate< BodyT > > Roar::Session::send ( boost::beast::http::response< BodyT > &&  res)
inline

◆ send() [2/4]

template<typename BodyT , typename OriginalBodyT , typename... Forwards>
std::shared_ptr< SendIntermediate< BodyT > > Roar::Session::send ( Request< OriginalBodyT > const &  req,
Forwards &&...  forwards 
)
inline

◆ send() [3/4]

template<typename BodyT >
std::shared_ptr< SendIntermediate< BodyT > > Roar::Session::send ( Response< BodyT > &&  res)
inline

◆ send() [4/4]

Detail::PromiseTypeBind< Detail::PromiseTypeBindThen< std::size_t >, Detail::PromiseTypeBindFail< Error > > Roar::Session::send ( std::string  message,
std::chrono::seconds  timeout = std::chrono::seconds{10} 
)

Send raw data. Use this after sending a proper response for sending additional data.

Parameters
data

◆ sendStandardResponse()

void Roar::Session::sendStandardResponse ( boost::beast::http::status  status,
std::string_view  additionalInfo = "" 
)

Sends a standard response like "404 not found".

Parameters
statusThe status to send.
additionalInfoSome additional info added into the body (depends on standardResponseProvider).

◆ sendStrictTransportSecurityResponse()

void Roar::Session::sendStrictTransportSecurityResponse ( )

Sends a 403 with Strict-Transport-Security. Used only for unencrypted request on enforced HTTPS.

◆ sendWithAllAcceptedCors()

template<typename BodyT >
std::shared_ptr< SendIntermediate< BodyT > > Roar::Session::sendWithAllAcceptedCors ( )
inline

◆ setupRouteOptions()

void Roar::Session::setupRouteOptions ( RouteOptions  options)
private

◆ standardResponseProvider()

StandardResponseProvider const & Roar::Session::standardResponseProvider ( )
private

◆ startup()

void Roar::Session::startup ( bool  immediate = true)
private

◆ stream()

std::variant< Detail::StreamType, boost::beast::ssl_stream< Detail::StreamType > > & Roar::Session::stream ( )
private

◆ upgrade()

Detail::PromiseTypeBind< Detail::PromiseTypeBindThen< std::shared_ptr< WebsocketSession > >, Detail::PromiseTypeBindFail< Error const & > > Roar::Session::upgrade ( Request< boost::beast::http::empty_body > const &  req)

Turn this session into a websocket session. Will return an invalid shared_ptr if this is not an upgrade request.

Parameters
reqThe request to perform this upgrade from.
Returns
a promise resolving to std::shared_ptr<WebsocketSession> being the new websocket session or an

◆ withStreamDo()

template<typename FunctionT >
void Roar::Session::withStreamDo ( FunctionT &&  func)
inline

std::visit for the underlying beast stream. Either ssl_stream or tcp_stream.

Template Parameters
FunctionT
Parameters
func

◆ writeLimit()

void Roar::Session::writeLimit ( std::size_t  bytesPerSecond)

Sets a rate limit in bytes/second for this session for writing.

Parameters
bytesPerSecond

Friends And Related Symbol Documentation

◆ Factory

friend class Factory
friend

◆ Route

friend class Route
friend

Member Data Documentation

◆ defaultBodyLimit

constexpr uint64_t Roar::Session::defaultBodyLimit {8_MiB}
staticconstexpr

◆ defaultHeaderLimit

constexpr uint64_t Roar::Session::defaultHeaderLimit {8_MiB}
staticconstexpr

◆ impl_

std::unique_ptr<Implementation> Roar::Session::impl_
private

◆ sessionTimeout

constexpr std::chrono::seconds Roar::Session::sessionTimeout {10}
staticconstexpr

The documentation for this class was generated from the following files: