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