xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
server
detail
PlainHTTPPeer.h
1
#pragma once
2
3
#include <xrpl/beast/rfc2616.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/PlainWSPeer.h>
9
10
#include <boost/beast/core/tcp_stream.hpp>
11
12
#include <
memory
>
13
#include <
utility
>
14
15
namespace
xrpl
{
16
17
template
<
class
Handler>
18
class
PlainHTTPPeer
:
public
BaseHTTPPeer
<Handler, PlainHTTPPeer<Handler>>,
19
public
std::enable_shared_from_this
<PlainHTTPPeer<Handler>>
20
{
21
private
:
22
friend
class
BaseHTTPPeer
<Handler,
PlainHTTPPeer
>;
23
using
SocketType
= boost::asio::ip::tcp::socket;
24
using
StreamType
= boost::beast::tcp_stream;
25
using
EndpointType
= boost::asio::ip::tcp::endpoint;
26
27
StreamType
stream_
;
28
SocketType
&
socket_
;
29
30
public
:
31
template
<
class
ConstBufferSequence>
32
PlainHTTPPeer
(
33
Port
const
&
port
,
34
Handler& handler,
35
boost::asio::io_context& ioc,
36
beast::Journal
journal
,
37
EndpointType
remoteAddress
,
38
ConstBufferSequence
const
& buffers,
39
StreamType
&& stream);
40
41
void
42
run
();
43
44
std::shared_ptr<WSSession>
45
websocketUpgrade
()
override
;
46
47
private
:
48
void
49
doRequest
()
override
;
50
51
void
52
doClose
()
override
;
53
};
54
55
//------------------------------------------------------------------------------
56
57
template
<
class
Handler>
58
template
<
class
ConstBufferSequence>
59
PlainHTTPPeer<Handler>::PlainHTTPPeer
(
60
Port
const
&
port
,
61
Handler& handler,
62
boost::asio::io_context& ioc,
63
beast::Journal
journal
,
64
EndpointType
remoteEndpoint,
65
ConstBufferSequence
const
& buffers,
66
StreamType
&& stream)
67
:
BaseHTTPPeer
<Handler,
PlainHTTPPeer
>(
68
port
,
69
handler,
70
ioc.get_executor(),
71
journal
,
72
remoteEndpoint,
73
buffers)
74
,
stream_
(
std
::move(stream))
75
,
socket_
(
stream_
.socket())
76
{
77
// Set TCP_NODELAY on loopback interfaces,
78
// otherwise Nagle's algorithm makes Env
79
// tests run slower on Linux systems.
80
//
81
if
(remoteEndpoint.address().is_loopback())
82
socket_
.set_option(boost::asio::ip::tcp::no_delay{true});
83
}
84
85
template
<
class
Handler>
86
void
87
PlainHTTPPeer<Handler>::run
()
88
{
89
if
(!this->
handler_
.onAccept(this->session(), this->remoteAddress_))
90
{
91
util::spawn
(this->
strand_
, [self = this->
shared_from_this
()](boost::asio::yield_context) {
92
self->doClose();
93
});
94
return
;
95
}
96
97
if
(!
socket_
.is_open())
98
return
;
99
100
util::spawn
(
101
this->
strand_
, [self = this->
shared_from_this
()](boost::asio::yield_context doYield) {
102
self->doRead(doYield);
103
});
104
}
105
106
template
<
class
Handler>
107
std::shared_ptr<WSSession>
108
PlainHTTPPeer<Handler>::websocketUpgrade
()
109
{
110
auto
ws = this->
ios
().template emplace<PlainWSPeer<Handler>>(
111
this->
port_
,
112
this->
handler_
,
113
this->
remoteAddress_
,
114
std::move(this->
message_
),
115
std::move(
stream_
),
116
this->
journal_
);
117
return
ws;
118
}
119
120
template
<
class
Handler>
121
void
122
PlainHTTPPeer<Handler>::doRequest
()
123
{
124
++this->
requestCount_
;
125
auto
const
what =
126
this->
handler_
.onHandoff(this->
session
(), std::move(this->
message_
), this->
remoteAddress_
);
127
if
(what.moved)
128
return
;
129
boost::system::error_code ec;
130
if
(what.response)
131
{
132
// half-close on Connection: close
133
if
(!what.keepAlive)
134
socket_
.shutdown(SocketType::shutdown_receive, ec);
135
if
(ec)
136
return
this->
fail
(ec,
"request"
);
137
return
this->
write
(what.response, what.keepAlive);
138
}
139
140
// Perform half-close when Connection: close and not SSL
141
if
(!
beast::rfc2616::isKeepAlive
(this->
message_
))
142
socket_
.shutdown(SocketType::shutdown_receive, ec);
143
if
(ec)
144
return
this->
fail
(ec,
"request"
);
145
// legacy
146
this->
handler_
.onRequest(this->
session
());
147
}
148
149
template
<
class
Handler>
150
void
151
PlainHTTPPeer<Handler>::doClose
()
152
{
153
boost::system::error_code ec;
154
socket_
.shutdown(SocketType::shutdown_send, ec);
155
}
156
157
}
// namespace xrpl
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::session
Session & session()
Definition
BaseHTTPPeer.h:104
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::write
void write(void const *buffer, std::size_t bytes) override
Definition
BaseHTTPPeer.h:382
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< 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, PlainHTTPPeer< Handler > >::handler_
Handler & handler_
Definition
BaseHTTPPeer.h:67
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::remoteAddress
beast::ip::Endpoint remoteAddress() override
Definition
BaseHTTPPeer.h:161
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::journal_
beast::Journal const journal_
Definition
BaseHTTPPeer.h:71
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::port_
Port const & port_
Definition
BaseHTTPPeer.h:66
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::journal
beast::Journal journal() override
Definition
BaseHTTPPeer.h:149
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::strand_
boost::asio::strand< boost::asio::executor > strand_
Definition
BaseHTTPPeer.h:69
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::remoteAddress_
EndpointType remoteAddress_
Definition
BaseHTTPPeer.h:70
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::message_
HttpRequestType message_
Definition
BaseHTTPPeer.h:77
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::requestCount_
int requestCount_
Definition
BaseHTTPPeer.h:85
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::port
Port const & port() override
Definition
BaseHTTPPeer.h:155
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::fail
void fail(ErrorCode ec, char const *what)
Definition
BaseHTTPPeer.h:237
xrpl::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::ios
IOList & ios()
Definition
io_list.h:44
xrpl::PlainHTTPPeer::stream_
StreamType stream_
Definition
PlainHTTPPeer.h:27
xrpl::PlainHTTPPeer::socket_
SocketType & socket_
Definition
PlainHTTPPeer.h:28
xrpl::PlainHTTPPeer::EndpointType
boost::asio::ip::tcp::endpoint EndpointType
Definition
PlainHTTPPeer.h:25
xrpl::PlainHTTPPeer::StreamType
boost::beast::tcp_stream StreamType
Definition
PlainHTTPPeer.h:24
xrpl::PlainHTTPPeer::doRequest
void doRequest() override
Definition
PlainHTTPPeer.h:122
xrpl::PlainHTTPPeer::websocketUpgrade
std::shared_ptr< WSSession > websocketUpgrade() override
Convert the connection to WebSocket.
Definition
PlainHTTPPeer.h:108
xrpl::PlainHTTPPeer::doClose
void doClose() override
Definition
PlainHTTPPeer.h:151
xrpl::PlainHTTPPeer::PlainHTTPPeer
PlainHTTPPeer(Port const &port, Handler &handler, boost::asio::io_context &ioc, beast::Journal journal, EndpointType remoteAddress, ConstBufferSequence const &buffers, StreamType &&stream)
Definition
PlainHTTPPeer.h:59
xrpl::PlainHTTPPeer::run
void run()
Definition
PlainHTTPPeer.h:87
xrpl::PlainHTTPPeer::SocketType
boost::asio::ip::tcp::socket SocketType
Definition
PlainHTTPPeer.h:23
std::enable_shared_from_this
memory
beast::rfc2616::isKeepAlive
bool isKeepAlive(boost::beast::http::message< IsRequest, Body, Fields > const &m)
Definition
rfc2616.h:367
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< PlainHTTPPeer< 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
utility
Generated by
1.17.0