rippled
Loading...
Searching...
No Matches
PlainHTTPPeer.h
1#pragma once
2
3#include <xrpl/beast/rfc2616.h>
4#include <xrpl/server/detail/BaseHTTPPeer.h>
5#include <xrpl/server/detail/PlainWSPeer.h>
6
7#include <boost/beast/core/tcp_stream.hpp>
8
9#include <memory>
10
11namespace xrpl {
12
13template <class Handler>
14class PlainHTTPPeer : public BaseHTTPPeer<Handler, PlainHTTPPeer<Handler>>,
15 public std::enable_shared_from_this<PlainHTTPPeer<Handler>>
16{
17private:
18 friend class BaseHTTPPeer<Handler, PlainHTTPPeer>;
19 using socket_type = boost::asio::ip::tcp::socket;
20 using stream_type = boost::beast::tcp_stream;
21 using endpoint_type = boost::asio::ip::tcp::endpoint;
22
25
26public:
27 template <class ConstBufferSequence>
29 Port const& port,
30 Handler& handler,
31 boost::asio::io_context& ioc,
33 endpoint_type remote_address,
34 ConstBufferSequence const& buffers,
35 stream_type&& stream);
36
37 void
38 run();
39
41 websocketUpgrade() override;
42
43private:
44 void
45 do_request() override;
46
47 void
48 do_close() override;
49};
50
51//------------------------------------------------------------------------------
52
53template <class Handler>
54template <class ConstBufferSequence>
56 Port const& port,
57 Handler& handler,
58 boost::asio::io_context& ioc,
59 beast::Journal journal,
60 endpoint_type remote_endpoint,
61 ConstBufferSequence const& buffers,
62 stream_type&& stream)
63 : BaseHTTPPeer<Handler, PlainHTTPPeer>(
64 port,
65 handler,
66 ioc.get_executor(),
67 journal,
68 remote_endpoint,
69 buffers)
70 , stream_(std::move(stream))
71 , socket_(stream_.socket())
72{
73 // Set TCP_NODELAY on loopback interfaces,
74 // otherwise Nagle's algorithm makes Env
75 // tests run slower on Linux systems.
76 //
77 if (remote_endpoint.address().is_loopback())
78 socket_.set_option(boost::asio::ip::tcp::no_delay{true});
79}
80
81template <class Handler>
82void
84{
85 if (!this->handler_.onAccept(this->session(), this->remote_address_))
86 {
87 util::spawn(this->strand_, std::bind(&PlainHTTPPeer::do_close, this->shared_from_this()));
88 return;
89 }
90
91 if (!socket_.is_open())
92 return;
93
95 this->strand_,
96 std::bind(&PlainHTTPPeer::do_read, this->shared_from_this(), std::placeholders::_1));
97}
98
99template <class Handler>
102{
103 auto ws = this->ios().template emplace<PlainWSPeer<Handler>>(
104 this->port_,
105 this->handler_,
106 this->remote_address_,
107 std::move(this->message_),
108 std::move(stream_),
109 this->journal_);
110 return ws;
111}
112
113template <class Handler>
114void
116{
117 ++this->request_count_;
118 auto const what =
119 this->handler_.onHandoff(this->session(), std::move(this->message_), this->remote_address_);
120 if (what.moved)
121 return;
122 boost::system::error_code ec;
123 if (what.response)
124 {
125 // half-close on Connection: close
126 if (!what.keep_alive)
127 socket_.shutdown(socket_type::shutdown_receive, ec);
128 if (ec)
129 return this->fail(ec, "request");
130 return this->write(what.response, what.keep_alive);
131 }
132
133 // Perform half-close when Connection: close and not SSL
134 if (!beast::rfc2616::is_keep_alive(this->message_))
135 socket_.shutdown(socket_type::shutdown_receive, ec);
136 if (ec)
137 return this->fail(ec, "request");
138 // legacy
139 this->handler_.onRequest(this->session());
140}
141
142template <class Handler>
143void
145{
146 boost::system::error_code ec;
147 socket_.shutdown(socket_type::shutdown_send, ec);
148}
149
150} // namespace xrpl
T bind(T... args)
A generic endpoint for log messages.
Definition Journal.h:40
Represents an active connection.
void do_close() override
std::shared_ptr< WSSession > websocketUpgrade() override
Convert the connection to WebSocket.
boost::asio::ip::tcp::socket socket_type
boost::asio::ip::tcp::endpoint endpoint_type
boost::beast::tcp_stream stream_type
socket_type & socket_
PlainHTTPPeer(Port const &port, Handler &handler, boost::asio::io_context &ioc, beast::Journal journal, endpoint_type remote_address, ConstBufferSequence const &buffers, stream_type &&stream)
void do_request() override
bool is_keep_alive(boost::beast::http::message< isRequest, Body, Fields > const &m)
Definition rfc2616.h:359
STL namespace.
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn
Definition Spawn.h:66
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Configuration information for a Server listening port.
Definition Port.h:30