xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
server
detail
SSLHTTPPeer.h
1
#pragma once
2
3
#include <xrpl/basics/Log.h>
4
#include <xrpl/beast/utility/Journal.h>
5
#include <xrpl/server/Port.h>
6
#include <xrpl/server/WSSession.h>
7
#include <xrpl/server/detail/BaseHTTPPeer.h>
8
#include <xrpl/server/detail/SSLWSPeer.h>
9
10
#include <boost/asio/ip/tcp.hpp>
11
#include <boost/asio/ssl/context.hpp>
12
#include <boost/asio/ssl/stream.hpp>
13
#include <boost/beast/core/tcp_stream.hpp>
14
#include <boost/beast/ssl/ssl_stream.hpp>
15
16
#include <
memory
>
17
#include <
utility
>
18
19
namespace
xrpl
{
20
21
template
<
class
Handler>
22
class
SSLHTTPPeer
:
public
BaseHTTPPeer
<Handler, SSLHTTPPeer<Handler>>,
23
public
std::enable_shared_from_this
<SSLHTTPPeer<Handler>>
24
{
25
private
:
26
friend
class
BaseHTTPPeer
<Handler,
SSLHTTPPeer
>;
27
using
SocketType
= boost::asio::ip::tcp::socket;
28
using
MiddleType
= boost::beast::tcp_stream;
29
using
StreamType
= boost::beast::ssl_stream<MiddleType>;
30
using
EndpointType
= boost::asio::ip::tcp::endpoint;
31
using
YieldContext
= boost::asio::yield_context;
32
using
ErrorCode
= boost::system::error_code;
33
34
std::unique_ptr<StreamType>
streamPtr_
;
35
StreamType
&
stream_
;
36
SocketType
&
socket_
;
37
38
public
:
39
template
<
class
ConstBufferSequence>
40
SSLHTTPPeer
(
41
Port
const
&
port
,
42
Handler& handler,
43
boost::asio::io_context& ioc,
44
beast::Journal
journal
,
45
EndpointType
remoteAddress
,
46
ConstBufferSequence
const
& buffers,
47
MiddleType
&& stream);
48
49
void
50
run
();
51
52
std::shared_ptr<WSSession>
53
websocketUpgrade
()
override
;
54
55
private
:
56
void
57
doHandshake
(
YieldContext
doYield);
58
59
void
60
doRequest
()
override
;
61
62
void
63
doClose
()
override
;
64
65
void
66
onShutdown
(
ErrorCode
ec);
67
};
68
69
//------------------------------------------------------------------------------
70
71
template
<
class
Handler>
72
template
<
class
ConstBufferSequence>
73
SSLHTTPPeer<Handler>::SSLHTTPPeer
(
74
Port
const
&
port
,
75
Handler& handler,
76
boost::asio::io_context& ioc,
77
beast::Journal
journal
,
78
EndpointType
remoteAddress
,
79
ConstBufferSequence
const
& buffers,
80
MiddleType
&& stream)
81
:
BaseHTTPPeer
<Handler,
SSLHTTPPeer
>(
82
port
,
83
handler,
84
ioc.get_executor(),
85
journal
,
86
remoteAddress
,
87
buffers)
88
,
streamPtr_
(
std
::make_unique<
StreamType
>(
MiddleType
(
std
::move(stream)), *
port
.context))
89
,
stream_
(*
streamPtr_
)
90
,
socket_
(
stream_
.next_layer().socket())
91
{
92
}
93
94
// Called when the acceptor accepts our socket.
95
template
<
class
Handler>
96
void
97
SSLHTTPPeer<Handler>::run
()
98
{
99
if
(!this->
handler_
.onAccept(this->session(), this->remoteAddress_))
100
{
101
util::spawn
(
102
this->
strand_
, [self = this->
shared_from_this
()](
YieldContext
) { self->doClose(); });
103
return
;
104
}
105
if
(!
socket_
.is_open())
106
return
;
107
util::spawn
(this->
strand_
, [self = this->
shared_from_this
()](
YieldContext
doYield) {
108
self->doHandshake(doYield);
109
});
110
}
111
112
template
<
class
Handler>
113
std::shared_ptr<WSSession>
114
SSLHTTPPeer<Handler>::websocketUpgrade
()
115
{
116
auto
ws = this->
ios
().template emplace<SSLWSPeer<Handler>>(
117
this->
port_
,
118
this->
handler_
,
119
this->
remoteAddress_
,
120
std::move(this->
message_
),
121
std::move(this->
streamPtr_
),
122
this->
journal_
);
123
return
ws;
124
}
125
126
template
<
class
Handler>
127
void
128
SSLHTTPPeer<Handler>::doHandshake
(
YieldContext
doYield)
129
{
130
boost::system::error_code ec;
131
stream_
.set_verify_mode(boost::asio::ssl::verify_none);
132
this->
startTimer
();
133
this->
readBuf_
.consume(
134
stream_
.async_handshake(StreamType::server, this->readBuf_.data(), doYield[ec]));
135
this->
cancelTimer
();
136
if
(ec == boost::beast::error::timeout)
137
return
this->
onTimer
();
138
if
(ec)
139
return
this->
fail
(ec,
"handshake"
);
140
bool
const
http = this->
port
().protocol.count(
"peer"
) > 0 ||
141
this->
port
().protocol.count(
"wss"
) > 0 || this->
port
().protocol.count(
"wss2"
) > 0 ||
142
this->
port
().protocol.count(
"https"
) > 0;
143
if
(http)
144
{
145
util::spawn
(this->
strand_
, [self = this->
shared_from_this
()](
YieldContext
doYield) {
146
self->doRead(doYield);
147
});
148
return
;
149
}
150
// `this` will be destroyed
151
}
152
153
template
<
class
Handler>
154
void
155
SSLHTTPPeer<Handler>::doRequest
()
156
{
157
++this->
requestCount_
;
158
auto
const
what = this->
handler_
.onHandoff(
159
this->
session
(), std::move(
streamPtr_
), std::move(this->
message_
), this->
remoteAddress_
);
160
if
(what.moved)
161
return
;
162
if
(what.response)
163
return
this->
write
(what.response, what.keepAlive);
164
// legacy
165
this->
handler_
.onRequest(this->
session
());
166
}
167
168
template
<
class
Handler>
169
void
170
SSLHTTPPeer<Handler>::doClose
()
171
{
172
this->
startTimer
();
173
stream_
.async_shutdown(bind_executor(
174
this->
strand_
,
175
[self = this->
shared_from_this
()](
ErrorCode
const
& ec) { self->onShutdown(ec); }));
176
}
177
178
template
<
class
Handler>
179
void
180
SSLHTTPPeer<Handler>::onShutdown
(
ErrorCode
ec)
181
{
182
this->
cancelTimer
();
183
184
if
(ec == boost::asio::error::operation_aborted)
185
return
;
186
if
(ec)
187
{
188
JLOG(this->
journal_
.debug()) <<
"on_shutdown: "
<< ec.message();
189
}
190
191
// Close socket now in case this->destructor is delayed
192
stream_
.next_layer().close();
193
}
194
195
}
// namespace xrpl
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::session
Session & session()
Definition
BaseHTTPPeer.h:104
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::write
void write(void const *buffer, std::size_t bytes) override
Definition
BaseHTTPPeer.h:382
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::BaseHTTPPeer
BaseHTTPPeer(Port const &port, Handler &handler, boost::asio::executor const &executor, beast::Journal journal, EndpointType remoteAddress, ConstBufferSequence const &buffers)
Definition
BaseHTTPPeer.h:192
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::handler_
Handler & handler_
Definition
BaseHTTPPeer.h:67
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::remoteAddress
beast::ip::Endpoint remoteAddress() override
Definition
BaseHTTPPeer.h:161
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::journal_
beast::Journal const journal_
Definition
BaseHTTPPeer.h:71
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::readBuf_
boost::asio::streambuf readBuf_
Definition
BaseHTTPPeer.h:76
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::port_
Port const & port_
Definition
BaseHTTPPeer.h:66
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::journal
beast::Journal journal() override
Definition
BaseHTTPPeer.h:149
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::strand_
boost::asio::strand< boost::asio::executor > strand_
Definition
BaseHTTPPeer.h:69
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::remoteAddress_
EndpointType remoteAddress_
Definition
BaseHTTPPeer.h:70
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::message_
HttpRequestType message_
Definition
BaseHTTPPeer.h:77
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::onTimer
void onTimer()
Definition
BaseHTTPPeer.h:268
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::requestCount_
int requestCount_
Definition
BaseHTTPPeer.h:85
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::port
Port const & port() override
Definition
BaseHTTPPeer.h:155
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::fail
void fail(ErrorCode ec, char const *what)
Definition
BaseHTTPPeer.h:237
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::startTimer
void startTimer()
Definition
BaseHTTPPeer.h:249
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::cancelTimer
void cancelTimer()
Definition
BaseHTTPPeer.h:260
xrpl::BaseHTTPPeer< Handler, SSLHTTPPeer< Handler > >::ios
IOList & ios()
Definition
io_list.h:44
xrpl::SSLHTTPPeer::socket_
SocketType & socket_
Definition
SSLHTTPPeer.h:36
xrpl::SSLHTTPPeer::SSLHTTPPeer
SSLHTTPPeer(Port const &port, Handler &handler, boost::asio::io_context &ioc, beast::Journal journal, EndpointType remoteAddress, ConstBufferSequence const &buffers, MiddleType &&stream)
Definition
SSLHTTPPeer.h:73
xrpl::SSLHTTPPeer::StreamType
boost::beast::ssl_stream< MiddleType > StreamType
Definition
SSLHTTPPeer.h:29
xrpl::SSLHTTPPeer::ErrorCode
boost::system::error_code ErrorCode
Definition
SSLHTTPPeer.h:32
xrpl::SSLHTTPPeer::doClose
void doClose() override
Definition
SSLHTTPPeer.h:170
xrpl::SSLHTTPPeer::SocketType
boost::asio::ip::tcp::socket SocketType
Definition
SSLHTTPPeer.h:27
xrpl::SSLHTTPPeer::stream_
StreamType & stream_
Definition
SSLHTTPPeer.h:35
xrpl::SSLHTTPPeer::run
void run()
Definition
SSLHTTPPeer.h:97
xrpl::SSLHTTPPeer::EndpointType
boost::asio::ip::tcp::endpoint EndpointType
Definition
SSLHTTPPeer.h:30
xrpl::SSLHTTPPeer::MiddleType
boost::beast::tcp_stream MiddleType
Definition
SSLHTTPPeer.h:28
xrpl::SSLHTTPPeer::doRequest
void doRequest() override
Definition
SSLHTTPPeer.h:155
xrpl::SSLHTTPPeer::onShutdown
void onShutdown(ErrorCode ec)
Definition
SSLHTTPPeer.h:180
xrpl::SSLHTTPPeer::websocketUpgrade
std::shared_ptr< WSSession > websocketUpgrade() override
Convert the connection to WebSocket.
Definition
SSLHTTPPeer.h:114
xrpl::SSLHTTPPeer::streamPtr_
std::unique_ptr< StreamType > streamPtr_
Definition
SSLHTTPPeer.h:34
xrpl::SSLHTTPPeer::doHandshake
void doHandshake(YieldContext doYield)
Definition
SSLHTTPPeer.h:128
xrpl::SSLHTTPPeer::YieldContext
boost::asio::yield_context YieldContext
Definition
SSLHTTPPeer.h:31
std::enable_shared_from_this
memory
std
STL namespace.
xrpl::util::spawn
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn.
Definition
Spawn.h:67
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
std::enable_shared_from_this< SSLHTTPPeer< Handler > >::shared_from_this
T shared_from_this(T... args)
std::shared_ptr
xrpl::Port
Configuration information for a Server listening port.
Definition
Port.h:29
std::unique_ptr
utility
Generated by
1.17.0