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>(port, handler, ioc.get_executor(), journal, remote_endpoint, buffers)
64 , stream_(std::move(stream))
65 , socket_(stream_.socket())
66{
67 // Set TCP_NODELAY on loopback interfaces,
68 // otherwise Nagle's algorithm makes Env
69 // tests run slower on Linux systems.
70 //
71 if (remote_endpoint.address().is_loopback())
72 socket_.set_option(boost::asio::ip::tcp::no_delay{true});
73}
74
75template <class Handler>
76void
78{
79 if (!this->handler_.onAccept(this->session(), this->remote_address_))
80 {
81 util::spawn(this->strand_, std::bind(&PlainHTTPPeer::do_close, this->shared_from_this()));
82 return;
83 }
84
85 if (!socket_.is_open())
86 return;
87
88 util::spawn(this->strand_, std::bind(&PlainHTTPPeer::do_read, this->shared_from_this(), std::placeholders::_1));
89}
90
91template <class Handler>
94{
95 auto ws = this->ios().template emplace<PlainWSPeer<Handler>>(
96 this->port_,
97 this->handler_,
98 this->remote_address_,
99 std::move(this->message_),
100 std::move(stream_),
101 this->journal_);
102 return ws;
103}
104
105template <class Handler>
106void
108{
109 ++this->request_count_;
110 auto const what = this->handler_.onHandoff(this->session(), std::move(this->message_), this->remote_address_);
111 if (what.moved)
112 return;
113 boost::system::error_code ec;
114 if (what.response)
115 {
116 // half-close on Connection: close
117 if (!what.keep_alive)
118 socket_.shutdown(socket_type::shutdown_receive, ec);
119 if (ec)
120 return this->fail(ec, "request");
121 return this->write(what.response, what.keep_alive);
122 }
123
124 // Perform half-close when Connection: close and not SSL
125 if (!beast::rfc2616::is_keep_alive(this->message_))
126 socket_.shutdown(socket_type::shutdown_receive, ec);
127 if (ec)
128 return this->fail(ec, "request");
129 // legacy
130 this->handler_.onRequest(this->session());
131}
132
133template <class Handler>
134void
136{
137 boost::system::error_code ec;
138 socket_.shutdown(socket_type::shutdown_send, ec);
139}
140
141} // 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:356
STL namespace.
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn
Definition Spawn.h:65
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