3#include "util/Taggable.hpp"
4#include "web/dosguard/DOSGuardInterface.hpp"
5#include "web/impl/WsBase.hpp"
6#include "web/interface/ConnectionBase.hpp"
8#include <boost/asio/ip/tcp.hpp>
9#include <boost/beast/core/flat_buffer.hpp>
10#include <boost/beast/core/stream_traits.hpp>
11#include <boost/beast/core/tcp_stream.hpp>
12#include <boost/beast/http/message.hpp>
13#include <boost/beast/http/parser.hpp>
14#include <boost/beast/http/string_body.hpp>
15#include <boost/beast/websocket/stream.hpp>
16#include <boost/optional/optional.hpp>
31template <SomeServerHandler HandlerType>
33 using StreamType = boost::beast::websocket::stream<boost::beast::tcp_stream>;
50 boost::asio::ip::tcp::socket&& socket,
52 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
53 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
54 std::shared_ptr<HandlerType>
const& handler,
55 boost::beast::flat_buffer&& buffer,
57 std::uint32_t maxSendingQueueSize
67 , ws_(std::move(socket))
69 ConnectionBase::isAdmin_ =
isAdmin;
90template <SomeServerHandler HandlerType>
91class WsUpgrader :
public std::enable_shared_from_this<WsUpgrader<HandlerType>> {
94 boost::beast::tcp_stream http_;
95 boost::optional<http::request_parser<http::string_body>> parser_;
96 boost::beast::flat_buffer buffer_;
97 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
98 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
99 http::request<http::string_body> req_;
101 std::shared_ptr<HandlerType>
const handler_;
103 std::uint32_t maxWsSendingQueueSize_;
120 boost::beast::tcp_stream&& stream,
122 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
123 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
124 std::shared_ptr<HandlerType>
const& handler,
125 boost::beast::flat_buffer&& buffer,
126 http::request<http::string_body> request,
128 std::uint32_t maxWsSendingQueueSize
130 : http_(std::move(stream))
131 , buffer_(std::move(buffer))
132 , tagFactory_(tagFactory)
133 , dosGuard_(dosGuard)
134 , req_(std::move(request))
138 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
148 boost::asio::dispatch(
149 http_.get_executor(),
150 boost::beast::bind_front_handler(
151 &WsUpgrader<HandlerType>::doUpgrade, shared_from_this()
162 static constexpr auto kMaxBodySize = 10000;
163 parser_->body_limit(kMaxBodySize);
165 boost::beast::get_lowest_layer(http_).expires_after(std::chrono::seconds(30));
172 if (!boost::beast::websocket::is_upgrade(req_))
176 boost::beast::get_lowest_layer(http_).expires_never();
178 std::make_shared<PlainWsSession<HandlerType>>(
179 http_.release_socket(),
186 maxWsSendingQueueSize_
188 ->run(std::move(req_));
Represents a non-secure websocket session.
Definition PlainWsSession.hpp:32
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:49
StreamType & ws()
Definition PlainWsSession.hpp:78
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:119
void run()
Initiate the upgrade.
Definition PlainWsSession.hpp:146
Web socket implementation. This class is the base class of the web socket session,...
Definition WsBase.hpp:57
This namespace implements the web server and related components.
Definition Types.hpp:24
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:99