22#include "util/Taggable.hpp"
23#include "web/dosguard/DOSGuardInterface.hpp"
24#include "web/impl/WsBase.hpp"
25#include "web/interface/ConnectionBase.hpp"
27#include <boost/asio/ip/tcp.hpp>
28#include <boost/beast/core/flat_buffer.hpp>
29#include <boost/beast/core/stream_traits.hpp>
30#include <boost/beast/core/tcp_stream.hpp>
31#include <boost/beast/http/message.hpp>
32#include <boost/beast/http/parser.hpp>
33#include <boost/beast/http/string_body.hpp>
34#include <boost/beast/websocket/stream.hpp>
35#include <boost/optional/optional.hpp>
50template <SomeServerHandler HandlerType>
52 using StreamType = boost::beast::websocket::stream<boost::beast::tcp_stream>;
69 boost::asio::ip::tcp::socket&& socket,
71 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
72 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
73 std::shared_ptr<HandlerType>
const& handler,
74 boost::beast::flat_buffer&& buffer,
76 std::uint32_t maxSendingQueueSize
86 , ws_(std::move(socket))
88 ConnectionBase::isAdmin_ =
isAdmin;
106template <SomeServerHandler HandlerType>
107class WsUpgrader :
public std::enable_shared_from_this<WsUpgrader<HandlerType>> {
110 boost::beast::tcp_stream http_;
111 boost::optional<http::request_parser<http::string_body>> parser_;
112 boost::beast::flat_buffer buffer_;
113 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
114 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
115 http::request<http::string_body> req_;
117 std::shared_ptr<HandlerType>
const handler_;
119 std::uint32_t maxWsSendingQueueSize_;
136 boost::beast::tcp_stream&& stream,
138 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
139 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
140 std::shared_ptr<HandlerType>
const& handler,
141 boost::beast::flat_buffer&& buffer,
142 http::request<http::string_body> request,
144 std::uint32_t maxWsSendingQueueSize
146 : http_(std::move(stream))
147 , buffer_(std::move(buffer))
148 , tagFactory_(tagFactory)
149 , dosGuard_(dosGuard)
150 , req_(std::move(request))
154 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
162 boost::asio::dispatch(
163 http_.get_executor(),
174 static constexpr auto kMAX_BODY_SIZE = 10000;
175 parser_->body_limit(kMAX_BODY_SIZE);
177 boost::beast::get_lowest_layer(http_).expires_after(std::chrono::seconds(30));
184 if (!boost::beast::websocket::is_upgrade(req_))
188 boost::beast::get_lowest_layer(http_).expires_never();
190 std::make_shared<PlainWsSession<HandlerType>>(
191 http_.release_socket(),
198 maxWsSendingQueueSize_
200 ->run(std::move(req_));
Represents a non-secure websocket session.
Definition PlainWsSession.hpp:51
PlainWsSession(boost::asio::ip::tcp::socket &&socket, std::string ip, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > const &handler, boost::beast::flat_buffer &&buffer, bool isAdmin, std::uint32_t maxSendingQueueSize)
Create a new non-secure websocket session.
Definition PlainWsSession.hpp:68
StreamType & ws()
Definition PlainWsSession.hpp:95
The websocket upgrader class, upgrade from an HTTP session to a non-secure websocket session.
Definition PlainWsSession.hpp:107
WsUpgrader(boost::beast::tcp_stream &&stream, std::string ip, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > const &handler, boost::beast::flat_buffer &&buffer, http::request< http::string_body > request, bool isAdmin, std::uint32_t maxWsSendingQueueSize)
Create a new upgrader to non-secure websocket.
Definition PlainWsSession.hpp:135
void run()
Initiate the upgrade.
Definition PlainWsSession.hpp:160
Web socket implementation. This class is the base class of the web socket session,...
Definition WsBase.hpp:75
This namespace implements the web server and related components.
Definition Types.hpp:43
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:111