Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SslHttpSession.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Taggable.hpp"
23#include "web/AdminVerificationStrategy.hpp"
24#include "web/SslWsSession.hpp"
25#include "web/dosguard/DOSGuardInterface.hpp"
26#include "web/impl/HttpBase.hpp"
27#include "web/interface/Concepts.hpp"
28#include "web/interface/ConnectionBase.hpp"
29
30#include <boost/asio/ip/tcp.hpp>
31#include <boost/asio/ssl/context.hpp>
32#include <boost/asio/ssl/stream_base.hpp>
33#include <boost/beast/core/error.hpp>
34#include <boost/beast/core/flat_buffer.hpp>
35#include <boost/beast/core/stream_traits.hpp>
36#include <boost/beast/core/tcp_stream.hpp>
37#include <boost/beast/ssl/ssl_stream.hpp>
38
39#include <chrono>
40#include <cstddef>
41#include <cstdint>
42#include <functional>
43#include <memory>
44#include <string>
45#include <utility>
46
47namespace web {
48
49using tcp = boost::asio::ip::tcp;
50
59template <SomeServerHandler HandlerType>
60class SslHttpSession : public impl::HttpBase<SslHttpSession, HandlerType>,
61 public std::enable_shared_from_this<SslHttpSession<HandlerType>> {
62 boost::beast::ssl_stream<boost::beast::tcp_stream> stream_;
63 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
64 std::uint32_t maxWsSendingQueueSize_;
65
66public:
81 tcp::socket&& socket,
82 std::string const& ip,
83 std::shared_ptr<AdminVerificationStrategy> const& adminVerification,
84 boost::asio::ssl::context& ctx,
85 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
86 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
87 std::shared_ptr<HandlerType> const& handler,
88 boost::beast::flat_buffer buffer,
89 std::uint32_t maxWsSendingQueueSize
90 )
91 : impl::HttpBase<SslHttpSession, HandlerType>(
92 ip,
93 tagFactory,
94 adminVerification,
95 dosGuard,
96 handler,
97 std::move(buffer)
98 )
99 , stream_(std::move(socket), ctx)
100 , tagFactory_(tagFactory)
101 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
102 {
103 }
104
105 ~SslHttpSession() override = default;
106
108 boost::beast::ssl_stream<boost::beast::tcp_stream>&
110 {
111 return stream_;
112 }
113
115 void
117 {
118 auto self = this->shared_from_this();
119 boost::asio::dispatch(stream_.get_executor(), [self]() {
120 // Set the timeout.
121 boost::beast::get_lowest_layer(self->stream()).expires_after(std::chrono::seconds(30));
122
123 // Perform the SSL handshake
124 // Note, this is the buffered version of the handshake.
125 self->stream_.async_handshake(
126 boost::asio::ssl::stream_base::server,
127 self->buffer_.data(),
128 boost::beast::bind_front_handler(&SslHttpSession<HandlerType>::onHandshake, self)
129 );
130 });
131 }
132
139 void
140 onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
141 {
142 if (ec)
143 return this->httpFail(ec, "handshake");
144
145 this->buffer_.consume(bytesUsed);
146 this->doRead();
147 }
148
150 void
152 {
153 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
154 stream_.async_shutdown(boost::beast::bind_front_handler(&SslHttpSession::onShutdown, this->shared_from_this()));
155 }
156
162 void
163 onShutdown(boost::beast::error_code ec)
164 {
165 if (ec)
166 return this->httpFail(ec, "shutdown");
167 // At this point the connection is closed gracefully
168 }
169
171 void
173 {
174 std::make_shared<SslWsUpgrader<HandlerType>>(
175 std::move(stream_),
176 this->clientIp,
177 tagFactory_,
178 this->dosGuard_,
179 this->handler_,
180 std::move(this->buffer_),
181 std::move(this->req_),
183 maxWsSendingQueueSize_
184 )
185 ->run();
186 }
187};
188} // namespace web
Represents a HTTPS connection established by a client.
Definition SslHttpSession.hpp:61
void upgrade()
Upgrades connection to secure websocket.
Definition SslHttpSession.hpp:172
void run()
Initiates the handshake.
Definition SslHttpSession.hpp:116
void onShutdown(boost::beast::error_code ec)
Handles a connection shutdown.
Definition SslHttpSession.hpp:163
void doClose()
Closes the underlying connection.
Definition SslHttpSession.hpp:151
SslHttpSession(tcp::socket &&socket, std::string const &ip, std::shared_ptr< AdminVerificationStrategy > const &adminVerification, boost::asio::ssl::context &ctx, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > const &handler, boost::beast::flat_buffer buffer, std::uint32_t maxWsSendingQueueSize)
Create a new SSL session.
Definition SslHttpSession.hpp:80
boost::beast::ssl_stream< boost::beast::tcp_stream > & stream()
Definition SslHttpSession.hpp:109
void onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
Handles the handshake.
Definition SslHttpSession.hpp:140
This is the implementation class for http sessions.
Definition HttpBase.hpp:82
This namespace implements the web server and related components.
Definition Types.hpp:43
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:111