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
33template <SomeServerHandler HandlerType>
34class PlainWsSession : public impl::WsBase<PlainWsSession, HandlerType> {
35 using StreamType = boost::beast::websocket::stream<boost::beast::tcp_stream>;
36 StreamType ws_;
37
38public:
52 boost::asio::ip::tcp::socket&& socket,
53 std::string ip,
54 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
55 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
56 std::shared_ptr<HandlerType> const& handler,
57 boost::beast::flat_buffer&& buffer,
58 bool isAdmin,
59 std::uint32_t maxSendingQueueSize
60 )
61 : impl::WsBase<PlainWsSession, HandlerType>(
62 ip,
63 tagFactory,
64 dosGuard,
65 handler,
66 std::move(buffer),
67 maxSendingQueueSize
68 )
69 , ws_(std::move(socket))
70 {
71 ConnectionBase::isAdmin_ = isAdmin; // NOLINT(cppcoreguidelines-prefer-member-initializer)
72 }
73
74 ~PlainWsSession() override = default;
75
79 StreamType&
81 {
82 return ws_;
83 }
84};
85
94template <SomeServerHandler HandlerType>
95class WsUpgrader : public std::enable_shared_from_this<WsUpgrader<HandlerType>> {
96 using std::enable_shared_from_this<WsUpgrader<HandlerType>>::shared_from_this;
97
98 boost::beast::tcp_stream http_;
99 boost::optional<http::request_parser<http::string_body>> parser_;
100 boost::beast::flat_buffer buffer_;
101 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
102 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
103 http::request<http::string_body> req_;
104 std::string ip_;
105 std::shared_ptr<HandlerType> const handler_;
106 bool isAdmin_;
107 std::uint32_t maxWsSendingQueueSize_;
108
109public:
124 boost::beast::tcp_stream&& stream,
125 std::string ip,
126 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
127 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
128 std::shared_ptr<HandlerType> const& handler,
129 boost::beast::flat_buffer&& buffer,
130 http::request<http::string_body> request,
131 bool isAdmin,
132 std::uint32_t maxWsSendingQueueSize
133 )
134 : http_(std::move(stream))
135 , buffer_(std::move(buffer))
136 , tagFactory_(tagFactory)
137 , dosGuard_(dosGuard)
138 , req_(std::move(request))
139 , ip_(std::move(ip))
140 , handler_(handler)
141 , isAdmin_(isAdmin)
142 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
143 {
144 }
145
149 void
151 {
152 boost::asio::dispatch(
153 http_.get_executor(),
154 boost::beast::bind_front_handler(
155 &WsUpgrader<HandlerType>::doUpgrade, shared_from_this()
156 )
157 );
158 }
159
160private:
161 void
162 doUpgrade()
163 {
164 parser_.emplace();
165
166 static constexpr auto kMaxBodySize = 10000;
167 parser_->body_limit(kMaxBodySize);
168
169 boost::beast::get_lowest_layer(http_).expires_after(std::chrono::seconds(30));
170 onUpgrade();
171 }
172
173 void
174 onUpgrade()
175 {
176 if (!boost::beast::websocket::is_upgrade(req_))
177 return;
178
179 // Disable the timeout. The websocket::stream uses its own timeout settings.
180 boost::beast::get_lowest_layer(http_).expires_never();
181
182 std::make_shared<PlainWsSession<HandlerType>>(
183 http_.release_socket(),
184 ip_,
185 tagFactory_,
186 dosGuard_,
187 handler_,
188 std::move(buffer_),
189 isAdmin_,
190 maxWsSendingQueueSize_
191 )
192 ->run(std::move(req_));
193 }
194};
195
196} // namespace web
Represents a non-secure websocket session.
Definition PlainWsSession.hpp:34
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:51
StreamType & ws()
Definition PlainWsSession.hpp:80
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:123
void run()
Initiate the upgrade.
Definition PlainWsSession.hpp:150
Web socket implementation. This class is the base class of the web socket session,...
Definition WsBase.hpp:58
This namespace implements the web server and related components.
Definition Types.hpp:23
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:99