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
97 boost::asio::ssl::stream<boost::beast::tcp_stream>&
99 {
100 return stream_;
101 }
102
104 void
106 {
107 auto self = this->shared_from_this();
108 boost::asio::dispatch(stream_.get_executor(), [self]() {
109 // Set the timeout.
110 boost::beast::get_lowest_layer(self->stream()).expires_after(std::chrono::seconds(30));
111
112 // Perform the SSL handshake
113 // Note, this is the buffered version of the handshake.
114 self->stream_.async_handshake(
115 boost::asio::ssl::stream_base::server,
116 self->buffer_.data(),
117 boost::beast::bind_front_handler(&SslHttpSession<HandlerType>::onHandshake, self)
118 );
119 });
120 }
121
128 void
129 onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
130 {
131 if (ec)
132 return this->httpFail(ec, "handshake");
133
134 this->buffer_.consume(bytesUsed);
135 this->doRead();
136 }
137
139 void
141 {
142 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
143 stream_.async_shutdown(
144 boost::beast::bind_front_handler(&SslHttpSession::onShutdown, this->shared_from_this())
145 );
146 }
147
153 void
154 onShutdown(boost::beast::error_code ec)
155 {
156 if (ec)
157 return this->httpFail(ec, "shutdown");
158 // At this point the connection is closed gracefully
159 }
160
162 void
164 {
165 std::make_shared<SslWsUpgrader<HandlerType>>(
166 std::move(stream_),
167 this->clientIp_,
168 tagFactory_,
169 this->dosGuard_,
170 this->handler_,
171 std::move(this->buffer_),
172 std::move(this->req_),
174 maxWsSendingQueueSize_
175 )
176 ->run();
177 }
178};
179} // namespace web
Represents a HTTPS connection established by a client.
Definition SslHttpSession.hpp:44
void upgrade()
Upgrades connection to secure websocket.
Definition SslHttpSession.hpp:163
void run()
Initiates the handshake.
Definition SslHttpSession.hpp:105
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:154
void doClose()
Closes the underlying connection.
Definition SslHttpSession.hpp:140
boost::asio::ssl::stream< boost::beast::tcp_stream > & stream()
Definition SslHttpSession.hpp:98
void onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
Handles the handshake.
Definition SslHttpSession.hpp:129
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