rippled
HTTPStream.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 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 #include <ripple/net/HTTPStream.h>
21 
22 namespace ripple {
23 
25  Config const& config,
26  boost::asio::io_service::strand& strand,
28  : ssl_ctx_(config, j, boost::asio::ssl::context::tlsv12_client)
29  , strand_(strand)
30 {
31 }
32 
33 boost::asio::ip::tcp::socket&
35 {
36  assert(stream_);
37  return stream_->next_layer();
38 }
39 
40 bool
42  std::string& errorOut,
43  std::string const host,
44  std::string const port,
45  boost::asio::yield_context& yield)
46 {
47  using namespace boost::asio;
48  using namespace boost::beast;
49 
50  boost::system::error_code ec;
51 
52  auto fail = [&errorOut](std::string const& errorIn) {
53  errorOut = errorIn;
54  return false;
55  };
56 
57  ip::tcp::resolver resolver{strand_.context()};
58  auto const endpoints = resolver.async_resolve(host, port, yield[ec]);
59  if (ec)
60  return fail("async_resolve");
61 
62  try
63  {
64  stream_.emplace(strand_.context(), ssl_ctx_.context());
65  }
66  catch (std::exception const& e)
67  {
68  return fail(std::string("exception: ") + e.what());
69  }
70 
71  ec = ssl_ctx_.preConnectVerify(*stream_, host);
72  if (ec)
73  return fail("preConnectVerify");
74 
75  boost::asio::async_connect(
76  stream_->next_layer(), endpoints.begin(), endpoints.end(), yield[ec]);
77  if (ec)
78  return fail("async_connect");
79 
80  ec = ssl_ctx_.postConnectVerify(*stream_, host);
81  if (ec)
82  return fail("postConnectVerify");
83 
84  stream_->async_handshake(ssl::stream_base::client, yield[ec]);
85  if (ec)
86  return fail("async_handshake");
87 
88  return true;
89 }
90 
91 void
93  request& req,
94  boost::asio::yield_context& yield,
95  boost::system::error_code& ec)
96 {
97  boost::beast::http::async_write(*stream_, req, yield[ec]);
98 }
99 
100 void
102  boost::beast::flat_buffer& buf,
103  parser& p,
104  bool readSome,
105  boost::asio::yield_context& yield,
106  boost::system::error_code& ec)
107 {
108  if (readSome)
109  boost::beast::http::async_read_some(*stream_, buf, p, yield[ec]);
110  else
111  boost::beast::http::async_read(*stream_, buf, p, yield[ec]);
112 }
113 
115  Config const& config,
116  boost::asio::io_service::strand& strand,
117  beast::Journal j)
118  : strand_(strand)
119 {
120 }
121 
122 boost::asio::ip::tcp::socket&
124 {
125  assert(stream_);
126  return *stream_;
127 }
128 
129 bool
131  std::string& errorOut,
132  std::string const host,
133  std::string const port,
134  boost::asio::yield_context& yield)
135 {
136  using namespace boost::asio;
137  using namespace boost::beast;
138 
139  boost::system::error_code ec;
140 
141  auto fail = [&errorOut](std::string const& errorIn) {
142  errorOut = errorIn;
143  return false;
144  };
145 
146  ip::tcp::resolver resolver{strand_.context()};
147  auto const endpoints = resolver.async_resolve(host, port, yield[ec]);
148  if (ec)
149  return fail("async_resolve");
150 
151  try
152  {
153  stream_.emplace(strand_.context());
154  }
155  catch (std::exception const& e)
156  {
157  return fail(std::string("exception: ") + e.what());
158  }
159 
160  boost::asio::async_connect(
161  *stream_, endpoints.begin(), endpoints.end(), yield[ec]);
162  if (ec)
163  return fail("async_connect");
164 
165  return true;
166 }
167 
168 void
170  request& req,
171  boost::asio::yield_context& yield,
172  boost::system::error_code& ec)
173 {
174  boost::beast::http::async_write(*stream_, req, yield[ec]);
175 }
176 
177 void
179  boost::beast::flat_buffer& buf,
180  parser& p,
181  bool readSome,
182  boost::asio::yield_context& yield,
183  boost::system::error_code& ec)
184 {
185  if (readSome)
186  boost::beast::http::async_read_some(*stream_, buf, p, yield[ec]);
187  else
188  boost::beast::http::async_read(*stream_, buf, p, yield[ec]);
189 }
190 
191 } // namespace ripple
std::string
STL class.
std::exception
STL class.
ripple::RawStream::asyncWrite
void asyncWrite(request &req, boost::asio::yield_context &yield, boost::system::error_code &ec) override
Definition: HTTPStream.cpp:169
ripple::SSLStream::getStream
boost::asio::ip::tcp::socket & getStream() override
Definition: HTTPStream.cpp:34
ripple::HTTPClientSSLContext::context
boost::asio::ssl::context & context()
Definition: HTTPClientSSLContext.h:72
ripple::RawStream::RawStream
RawStream(Config const &config, boost::asio::io_service::strand &strand, beast::Journal j)
Definition: HTTPStream.cpp:114
ripple::SSLStream::stream_
boost::optional< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > stream_
Definition: HTTPStream.h:113
ripple::HTTPStream::parser
boost::beast::http::basic_parser< false > parser
Definition: HTTPStream.h:37
ripple::HTTPClientSSLContext::preConnectVerify
boost::system::error_code preConnectVerify(T &strm, std::string const &host)
invoked before connect/async_connect on an ssl stream to setup name verification.
Definition: HTTPClientSSLContext.h:107
boost
Definition: IPAddress.h:117
ripple::RawStream::getStream
boost::asio::ip::tcp::socket & getStream() override
Definition: HTTPStream.cpp:123
ripple::RawStream::asyncRead
void asyncRead(boost::beast::flat_buffer &buf, parser &p, bool readSome, boost::asio::yield_context &yield, boost::system::error_code &ec) override
Definition: HTTPStream.cpp:178
ripple::SSLStream::asyncWrite
void asyncWrite(request &req, boost::asio::yield_context &yield, boost::system::error_code &ec) override
Definition: HTTPStream.cpp:92
ripple::Config
Definition: Config.h:67
ripple::RawStream::connect
bool connect(std::string &errorOut, std::string const host, std::string const port, boost::asio::yield_context &yield) override
Definition: HTTPStream.cpp:130
ripple::SSLStream::asyncRead
void asyncRead(boost::beast::flat_buffer &buf, parser &p, bool readSome, boost::asio::yield_context &yield, boost::system::error_code &ec) override
Definition: HTTPStream.cpp:101
boost::asio
Definition: Overlay.h:42
ripple::HTTPClientSSLContext::postConnectVerify
boost::system::error_code postConnectVerify(T &strm, std::string const &host)
invoked after connect/async_connect but before sending data on an ssl stream - to setup name verifica...
Definition: HTTPClientSSLContext.h:142
ripple::RawStream::strand_
boost::asio::io_service::strand & strand_
Definition: HTTPStream.h:153
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::RawStream::stream_
boost::optional< boost::asio::ip::tcp::socket > stream_
Definition: HTTPStream.h:152
ripple::SSLStream::strand_
boost::asio::io_service::strand & strand_
Definition: HTTPStream.h:114
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SSLStream::SSLStream
SSLStream(Config const &config, boost::asio::io_service::strand &strand, beast::Journal j)
Definition: HTTPStream.cpp:24
ripple::SSLStream::ssl_ctx_
HTTPClientSSLContext ssl_ctx_
Definition: HTTPStream.h:111
ripple::SSLStream::connect
bool connect(std::string &errorOut, std::string const host, std::string const port, boost::asio::yield_context &yield) override
Definition: HTTPStream.cpp:41
ripple::HTTPStream::request
boost::beast::http::request< boost::beast::http::empty_body > request
Definition: HTTPStream.h:36
std::exception::what
T what(T... args)