rippled
PlainHTTPPeer.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED
21 #define RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED
22 
23 #include <ripple/beast/rfc2616.h>
24 #include <ripple/server/impl/BaseHTTPPeer.h>
25 #include <ripple/server/impl/PlainWSPeer.h>
26 #include <boost/beast/core/tcp_stream.hpp>
27 #include <memory>
28 
29 namespace ripple {
30 
31 template<class Handler>
33  : public BaseHTTPPeer<Handler, PlainHTTPPeer<Handler>>
34  , public std::enable_shared_from_this<PlainHTTPPeer<Handler>>
35 {
36 private:
37  friend class BaseHTTPPeer<Handler, PlainHTTPPeer>;
38  using socket_type = boost::asio::ip::tcp::socket;
39  using stream_type = boost::beast::tcp_stream;
40  using endpoint_type = boost::asio::ip::tcp::endpoint;
41 
44 
45 public:
46  template <class ConstBufferSequence>
48  Port const& port,
49  Handler& handler,
50  boost::asio::io_context& ioc,
52  endpoint_type remote_address,
53  ConstBufferSequence const& buffers,
54  stream_type&& stream);
55 
56  void run();
57 
59  websocketUpgrade() override;
60 
61 private:
62  void
63  do_request() override;
64 
65  void
66  do_close() override;
67 };
68 
69 //------------------------------------------------------------------------------
70 
71 template <class Handler>
72 template <class ConstBufferSequence>
74  Port const& port,
75  Handler& handler,
76  boost::asio::io_context& ioc,
77  beast::Journal journal,
78  endpoint_type remote_endpoint,
79  ConstBufferSequence const& buffers,
80  stream_type&& stream)
81  : BaseHTTPPeer<Handler, PlainHTTPPeer>(
82  port,
83  handler,
84  ioc.get_executor(),
85  journal,
86  remote_endpoint,
87  buffers)
88  , stream_(std::move(stream))
89  , socket_(stream_.socket())
90 {
91  // Set TCP_NODELAY on loopback interfaces,
92  // otherwise Nagle's algorithm makes Env
93  // tests run slower on Linux systems.
94  //
95  if(remote_endpoint.address().is_loopback())
96  socket_.set_option(boost::asio::ip::tcp::no_delay{true});
97 }
98 
99 template<class Handler>
100 void
103 {
104  if (! this->handler_.onAccept(this->session(), this->remote_address_))
105  {
106  boost::asio::spawn(this->strand_,
108  this->shared_from_this()));
109  return;
110  }
111 
112  if (! socket_.is_open())
113  return;
114 
115  boost::asio::spawn(this->strand_, std::bind(&PlainHTTPPeer::do_read,
116  this->shared_from_this(), std::placeholders::_1));
117 }
118 
119 template<class Handler>
123 {
124  auto ws = this->ios().template emplace<PlainWSPeer<Handler>>(
125  this->port_, this->handler_, this->remote_address_,
126  std::move(this->message_), std::move(stream_),
127  this->journal_);
128  return ws;
129 }
130 
131 template<class Handler>
132 void
135 {
136  ++this->request_count_;
137  auto const what = this->handler_.onHandoff(this->session(),
138  std::move(this->message_), this->remote_address_);
139  if (what.moved)
140  return;
141  boost::system::error_code ec;
142  if (what.response)
143  {
144  // half-close on Connection: close
145  if (! what.keep_alive)
146  socket_.shutdown(socket_type::shutdown_receive, ec);
147  if (ec)
148  return this->fail(ec, "request");
149  return this->write(what.response, what.keep_alive);
150  }
151 
152  // Perform half-close when Connection: close and not SSL
153  if (! beast::rfc2616::is_keep_alive(this->message_))
154  socket_.shutdown(socket_type::shutdown_receive, ec);
155  if (ec)
156  return this->fail(ec, "request");
157  // legacy
158  this->handler_.onRequest(this->session());
159 }
160 
161 template<class Handler>
162 void
165 {
166  boost::system::error_code ec;
167  socket_.shutdown(socket_type::shutdown_send, ec);
168 }
169 
170 } // ripple
171 
172 #endif
ripple::PlainHTTPPeer::socket_
socket_type & socket_
Definition: PlainHTTPPeer.h:43
std::bind
T bind(T... args)
std::shared_ptr
STL class.
ripple::BaseHTTPPeer
Represents an active connection.
Definition: BaseHTTPPeer.h:49
ripple::PlainHTTPPeer::do_close
void do_close() override
Definition: PlainHTTPPeer.h:164
beast::rfc2616::is_keep_alive
bool is_keep_alive(boost::beast::http::message< isRequest, Body, Fields > const &m)
Definition: rfc2616.h:478
ripple::PlainHTTPPeer::endpoint_type
boost::asio::ip::tcp::endpoint endpoint_type
Definition: PlainHTTPPeer.h:40
ripple::PlainHTTPPeer::run
void run()
Definition: PlainHTTPPeer.h:102
std::enable_shared_from_this
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::PlainHTTPPeer::stream_
stream_type stream_
Definition: PlainHTTPPeer.h:42
ripple::PlainHTTPPeer::do_request
void do_request() override
Definition: PlainHTTPPeer.h:134
ripple::Port
Configuration information for a Server listening port.
Definition: Port.h:38
ripple::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::journal
beast::Journal journal() override
Definition: BaseHTTPPeer.h:171
memory
ripple::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::port
Port const & port() override
Definition: BaseHTTPPeer.h:177
ripple::PlainHTTPPeer::websocketUpgrade
std::shared_ptr< WSSession > websocketUpgrade() override
Convert the connection to WebSocket.
Definition: PlainHTTPPeer.h:122
ripple::BaseHTTPPeer< Handler, PlainHTTPPeer< Handler > >::do_read
void do_read(yield_context do_yield)
Definition: BaseHTTPPeer.h:316
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std
STL namespace.
ripple::PlainHTTPPeer
Definition: PlainHTTPPeer.h:32
ripple::PlainHTTPPeer::socket_type
boost::asio::ip::tcp::socket socket_type
Definition: PlainHTTPPeer.h:38
ripple::PlainHTTPPeer::PlainHTTPPeer
PlainHTTPPeer(Port const &port, Handler &handler, boost::asio::io_context &ioc, beast::Journal journal, endpoint_type remote_address, ConstBufferSequence const &buffers, stream_type &&stream)
Definition: PlainHTTPPeer.h:73
ripple::PlainHTTPPeer::stream_type
boost::beast::tcp_stream stream_type
Definition: PlainHTTPPeer.h:39