Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
PlainWsSession.hpp
1#pragma once
2
3#include "util/Taggable.hpp"
4#include "web/dosguard/DOSGuardInterface.hpp"
5#include "web/impl/WsBase.hpp"
6#include "web/interface/ConnectionBase.hpp"
7
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>
17
18#include <chrono>
19#include <functional>
20#include <memory>
21#include <string>
22#include <utility>
23
24namespace web {
25
31template <SomeServerHandler HandlerType>
32class PlainWsSession : public impl::WsBase<PlainWsSession, HandlerType> {
33 using StreamType = boost::beast::websocket::stream<boost::beast::tcp_stream>;
34 StreamType ws_;
35
36public:
50 boost::asio::ip::tcp::socket&& socket,
51 std::string ip,
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,
56 bool isAdmin,
57 std::uint32_t maxSendingQueueSize
58 )
59 : impl::WsBase<PlainWsSession, HandlerType>(
60 ip,
61 tagFactory,
62 dosGuard,
63 handler,
64 std::move(buffer),
65 maxSendingQueueSize
66 )
67 , ws_(std::move(socket))
68 {
69 ConnectionBase::isAdmin_ = isAdmin; // NOLINT(cppcoreguidelines-prefer-member-initializer)
70 }
71
72 ~PlainWsSession() override = default;
73
75 StreamType&
77 {
78 return ws_;
79 }
80};
81
88template <SomeServerHandler HandlerType>
89class WsUpgrader : public std::enable_shared_from_this<WsUpgrader<HandlerType>> {
90 using std::enable_shared_from_this<WsUpgrader<HandlerType>>::shared_from_this;
91
92 boost::beast::tcp_stream http_;
93 boost::optional<http::request_parser<http::string_body>> parser_;
94 boost::beast::flat_buffer buffer_;
95 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
96 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
97 http::request<http::string_body> req_;
98 std::string ip_;
99 std::shared_ptr<HandlerType> const handler_;
100 bool isAdmin_;
101 std::uint32_t maxWsSendingQueueSize_;
102
103public:
118 boost::beast::tcp_stream&& stream,
119 std::string ip,
120 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
121 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
122 std::shared_ptr<HandlerType> const& handler,
123 boost::beast::flat_buffer&& buffer,
124 http::request<http::string_body> request,
125 bool isAdmin,
126 std::uint32_t maxWsSendingQueueSize
127 )
128 : http_(std::move(stream))
129 , buffer_(std::move(buffer))
130 , tagFactory_(tagFactory)
131 , dosGuard_(dosGuard)
132 , req_(std::move(request))
133 , ip_(std::move(ip))
134 , handler_(handler)
135 , isAdmin_(isAdmin)
136 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
137 {
138 }
139
141 void
143 {
144 boost::asio::dispatch(
145 http_.get_executor(),
146 boost::beast::bind_front_handler(
147 &WsUpgrader<HandlerType>::doUpgrade, shared_from_this()
148 )
149 );
150 }
151
152private:
153 void
154 doUpgrade()
155 {
156 parser_.emplace();
157
158 static constexpr auto kMAX_BODY_SIZE = 10000;
159 parser_->body_limit(kMAX_BODY_SIZE);
160
161 boost::beast::get_lowest_layer(http_).expires_after(std::chrono::seconds(30));
162 onUpgrade();
163 }
164
165 void
166 onUpgrade()
167 {
168 if (!boost::beast::websocket::is_upgrade(req_))
169 return;
170
171 // Disable the timeout. The websocket::stream uses its own timeout settings.
172 boost::beast::get_lowest_layer(http_).expires_never();
173
174 std::make_shared<PlainWsSession<HandlerType>>(
175 http_.release_socket(),
176 ip_,
177 tagFactory_,
178 dosGuard_,
179 handler_,
180 std::move(buffer_),
181 isAdmin_,
182 maxWsSendingQueueSize_
183 )
184 ->run(std::move(req_));
185 }
186};
187
188} // namespace web
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:76
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:117
void run()
Initiate the upgrade.
Definition PlainWsSession.hpp:142
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