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;
107template <SomeServerHandler HandlerType>
108class WsUpgrader :
public std::enable_shared_from_this<WsUpgrader<HandlerType>> {
111 boost::beast::tcp_stream http_;
112 boost::optional<http::request_parser<http::string_body>> parser_;
113 boost::beast::flat_buffer buffer_;
114 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
115 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
116 http::request<http::string_body> req_;
118 std::shared_ptr<HandlerType>
const handler_;
120 std::uint32_t maxWsSendingQueueSize_;
137 boost::beast::tcp_stream&& stream,
139 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
140 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
141 std::shared_ptr<HandlerType>
const& handler,
142 boost::beast::flat_buffer&& buffer,
143 http::request<http::string_body> request,
145 std::uint32_t maxWsSendingQueueSize
147 : http_(std::move(stream))
148 , buffer_(std::move(buffer))
149 , tagFactory_(tagFactory)
150 , dosGuard_(dosGuard)
151 , req_(std::move(request))
155 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
163 boost::asio::dispatch(
164 http_.get_executor(),
165 boost::beast::bind_front_handler(
166 &WsUpgrader<HandlerType>::doUpgrade, shared_from_this()
177 static constexpr auto kMAX_BODY_SIZE = 10000;
178 parser_->body_limit(kMAX_BODY_SIZE);
180 boost::beast::get_lowest_layer(http_).expires_after(std::chrono::seconds(30));
187 if (!boost::beast::websocket::is_upgrade(req_))
191 boost::beast::get_lowest_layer(http_).expires_never();
193 std::make_shared<PlainWsSession<HandlerType>>(
194 http_.release_socket(),
201 maxWsSendingQueueSize_
203 ->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
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:136
void run()
Initiate the upgrade.
Definition PlainWsSession.hpp:161
Web socket implementation. This class is the base class of the web socket session,...
Definition WsBase.hpp:76
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:118