Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SslHttpSession.hpp
1#pragma once
2
3#include "data/LedgerCacheInterface.hpp"
4#include "util/Taggable.hpp"
5#include "web/AdminVerificationStrategy.hpp"
6#include "web/ProxyIpResolver.hpp"
7#include "web/SslWsSession.hpp"
8#include "web/dosguard/DOSGuardInterface.hpp"
9#include "web/impl/HttpBase.hpp"
10#include "web/interface/Concepts.hpp"
11#include "web/interface/ConnectionBase.hpp"
12
13#include <boost/asio/ip/tcp.hpp>
14#include <boost/asio/ssl/context.hpp>
15#include <boost/asio/ssl/stream_base.hpp>
16#include <boost/beast/core/error.hpp>
17#include <boost/beast/core/flat_buffer.hpp>
18#include <boost/beast/core/stream_traits.hpp>
19#include <boost/beast/core/tcp_stream.hpp>
20#include <boost/beast/ssl/ssl_stream.hpp>
21
22#include <chrono>
23#include <cstddef>
24#include <cstdint>
25#include <functional>
26#include <memory>
27#include <string>
28#include <utility>
29
30namespace web {
31
32using tcp = boost::asio::ip::tcp;
33
42template <SomeServerHandler HandlerType>
43class SslHttpSession : public impl::HttpBase<SslHttpSession, HandlerType>,
44 public std::enable_shared_from_this<SslHttpSession<HandlerType>> {
45 boost::asio::ssl::stream<boost::beast::tcp_stream> stream_;
46 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
47 std::uint32_t maxWsSendingQueueSize_;
48
49public:
66 tcp::socket&& socket,
67 std::string const& ip,
68 std::shared_ptr<AdminVerificationStrategy> const& adminVerification,
69 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
70 boost::asio::ssl::context& ctx,
71 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
72 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
73 std::shared_ptr<HandlerType> const& handler,
74 std::reference_wrapper<data::LedgerCacheInterface const> cache,
75 boost::beast::flat_buffer buffer,
76 std::uint32_t maxWsSendingQueueSize
77 )
78 : impl::HttpBase<SslHttpSession, HandlerType>(
79 ip,
80 tagFactory,
81 adminVerification,
82 std::move(proxyIpResolver),
83 dosGuard,
84 handler,
85 cache,
86 std::move(buffer)
87 )
88 , stream_(std::move(socket), ctx)
89 , tagFactory_(tagFactory)
90 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
91 {
92 }
93
94 ~SslHttpSession() override = default;
95
99 boost::asio::ssl::stream<boost::beast::tcp_stream>&
101 {
102 return stream_;
103 }
104
108 void
110 {
111 auto self = this->shared_from_this();
112 boost::asio::dispatch(stream_.get_executor(), [self]() {
113 // Set the timeout.
114 boost::beast::get_lowest_layer(self->stream()).expires_after(std::chrono::seconds(30));
115
116 // Perform the SSL handshake
117 // Note, this is the buffered version of the handshake.
118 self->stream_.async_handshake(
119 boost::asio::ssl::stream_base::server,
120 self->buffer_.data(),
121 boost::beast::bind_front_handler(&SslHttpSession<HandlerType>::onHandshake, self)
122 );
123 });
124 }
125
132 void
133 onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
134 {
135 if (ec)
136 return this->httpFail(ec, "handshake");
137
138 this->buffer_.consume(bytesUsed);
139 this->doRead();
140 }
141
145 void
147 {
148 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
149 stream_.async_shutdown(
150 boost::beast::bind_front_handler(&SslHttpSession::onShutdown, this->shared_from_this())
151 );
152 }
153
159 void
160 onShutdown(boost::beast::error_code ec)
161 {
162 if (ec)
163 return this->httpFail(ec, "shutdown");
164 // At this point the connection is closed gracefully
165 }
166
170 void
172 {
173 std::make_shared<SslWsUpgrader<HandlerType>>(
174 std::move(stream_),
175 this->clientIp_,
176 tagFactory_,
177 this->dosGuard_,
178 this->handler_,
179 std::move(this->buffer_),
180 std::move(this->req_),
182 maxWsSendingQueueSize_
183 )
184 ->run();
185 }
186};
187} // namespace web
Represents a HTTPS connection established by a client.
Definition SslHttpSession.hpp:44
void upgrade()
Upgrades connection to secure websocket.
Definition SslHttpSession.hpp:171
void run()
Initiates the handshake.
Definition SslHttpSession.hpp:109
SslHttpSession(tcp::socket &&socket, std::string const &ip, std::shared_ptr< AdminVerificationStrategy > const &adminVerification, std::shared_ptr< ProxyIpResolver > proxyIpResolver, boost::asio::ssl::context &ctx, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > const &handler, std::reference_wrapper< data::LedgerCacheInterface const > cache, boost::beast::flat_buffer buffer, std::uint32_t maxWsSendingQueueSize)
Create a new SSL session.
Definition SslHttpSession.hpp:65
void onShutdown(boost::beast::error_code ec)
Handles a connection shutdown.
Definition SslHttpSession.hpp:160
void doClose()
Closes the underlying connection.
Definition SslHttpSession.hpp:146
boost::asio::ssl::stream< boost::beast::tcp_stream > & stream()
Definition SslHttpSession.hpp:100
void onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
Handles the handshake.
Definition SslHttpSession.hpp:133
This is the implementation class for http sessions.
Definition HttpBase.hpp:81
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