3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/net/IPAddressConversion.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/server/Session.h>
7#include <xrpl/server/detail/Spawn.h>
8#include <xrpl/server/detail/io_list.h>
10#include <boost/asio/ip/tcp.hpp>
11#include <boost/asio/spawn.hpp>
12#include <boost/asio/ssl/stream.hpp>
13#include <boost/asio/strand.hpp>
14#include <boost/asio/streambuf.hpp>
15#include <boost/beast/core/stream_traits.hpp>
16#include <boost/beast/http/dynamic_body.hpp>
17#include <boost/beast/http/message.hpp>
18#include <boost/beast/http/parser.hpp>
19#include <boost/beast/http/read.hpp>
31template <
class Handler,
class Impl>
63 boost::asio::executor_work_guard<boost::asio::executor>
work_;
64 boost::asio::strand<boost::asio::executor>
strand_;
78 boost::system::error_code
ec_;
87 template <
class ConstBufferSequence>
91 boost::asio::executor
const& executor,
94 ConstBufferSequence
const& buffers);
111 return *
static_cast<Impl*
>(
this);
185template <
class Handler,
class Impl>
186template <
class ConstBufferSequence>
190 boost::asio::executor
const& executor,
193 ConstBufferSequence
const& buffers)
196 , work_(
boost::asio::make_work_guard(executor))
197 , strand_(
boost::asio::make_strand(executor))
198 , remote_address_(remote_address)
202 boost::asio::buffer_copy(
read_buf_.prepare(boost::asio::buffer_size(buffers)), buffers));
209template <
class Handler,
class Impl>
212 handler_.onClose(session(), ec_);
213 JLOG(journal_.trace()) << id_ <<
"destroyed: " << request_count_
214 << ((request_count_ == 1) ?
" request" :
" requests");
217template <
class Handler,
class Impl>
221 if (!strand_.running_in_this_thread())
226 boost::beast::get_lowest_layer(impl().stream_).close();
231template <
class Handler,
class Impl>
235 if (!ec_ && ec != boost::asio::error::operation_aborted)
238 JLOG(journal_.trace()) << id_ <<
std::string(what) <<
": " << ec.message();
239 boost::beast::get_lowest_layer(impl().stream_).close();
243template <
class Handler,
class Impl>
247 boost::beast::get_lowest_layer(impl().stream_)
250 remote_address_.address().is_loopback() ? timeoutSecondsLocal : timeoutSeconds));
254template <
class Handler,
class Impl>
258 boost::beast::get_lowest_layer(impl().stream_).expires_never();
262template <
class Handler,
class Impl>
266 auto ec = boost::system::errc::make_error_code(boost::system::errc::timed_out);
272template <
class Handler,
class Impl>
279 boost::beast::http::async_read(impl().stream_, read_buf_, message_, do_yield[ec]);
281 if (ec == boost::beast::http::error::end_of_stream)
283 if (ec == boost::beast::error::timeout)
286 return fail(ec,
"http::read");
292template <
class Handler,
class Impl>
297 if (ec == boost::beast::error::timeout)
300 return fail(ec,
"write");
301 bytes_out_ += bytes_transferred;
305 wq2_.reserve(wq_.size());
312 for (
auto const& b : wq2_)
315 return boost::asio::async_write(
322 impl().shared_from_this(),
323 std::placeholders::_1,
324 std::placeholders::_2)));
334 impl().shared_from_this(),
335 std::placeholders::_1));
338template <
class Handler,
class Impl>
347 auto const p = impl().shared_from_this();
356 std::placeholders::_1));
362 if (!writer->prepare(bufferSize, resume))
365 auto const bytes_transferred = boost::asio::async_write(
366 impl().stream_, writer->data(), boost::asio::transfer_at_least(1), do_yield[ec]);
368 return fail(ec,
"writer");
369 writer->consume(bytes_transferred);
370 if (writer->complete())
381 impl().shared_from_this(),
382 std::placeholders::_1));
388template <
class Handler,
class Impl>
396 wq_.emplace_back(buf, bytes);
397 return wq_.size() == 1 && wq2_.size() == 0;
400 if (!strand_.running_in_this_thread())
409template <
class Handler,
class Impl>
417 impl().shared_from_this(),
420 std::placeholders::_1));
425template <
class Handler,
class Impl>
429 return impl().shared_from_this();
434template <
class Handler,
class Impl>
438 if (!strand_.running_in_this_thread())
447 if (!wq_.empty() && !wq2_.empty())
456 impl().shared_from_this(),
457 std::placeholders::_1));
462template <
class Handler,
class Impl>
466 if (!strand_.running_in_this_thread())
471 impl().shared_from_this(),
480 if (!wq_.empty() || !wq2_.empty())
486 boost::beast::get_lowest_layer(impl().stream_).close();
A version-independent IP address and port combination.
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Represents an active connection.
boost::asio::streambuf read_buf_
http_request_type message_
void write(void const *buffer, std::size_t bytes) override
beast::IP::Endpoint remoteAddress() override
Returns the remote address of the connection.
std::vector< buffer > wq_
boost::asio::ip::tcp::endpoint endpoint_type
void do_read(yield_context do_yield)
boost::system::error_code ec_
boost::system::error_code error_code
beast::Journal const journal_
void fail(error_code ec, char const *what)
endpoint_type remote_address_
virtual void do_request()=0
std::vector< buffer > wq2_
std::shared_ptr< Session > detach() override
Detach the session.
void on_write(error_code const &ec, std::size_t bytes_transferred)
beast::Journal journal() override
Returns the Journal to use for logging.
boost::asio::strand< boost::asio::executor > strand_
BaseHTTPPeer(Port const &port, Handler &handler, boost::asio::executor const &executor, beast::Journal journal, endpoint_type remote_address, ConstBufferSequence const &buffers)
boost::asio::executor_work_guard< boost::asio::executor > work_
void complete() override
Indicate that the response is complete.
void write(std::shared_ptr< Writer > const &writer, bool keep_alive) override
void close(bool graceful) override
Close the session.
Port const & port() override
Returns the Port settings for this connection.
void do_writer(std::shared_ptr< Writer > const &writer, bool keep_alive, yield_context do_yield)
http_request_type & request() override
Returns the current HTTP request.
boost::asio::yield_context yield_context
virtual void do_close()=0
Persistent state information for a connection session.
T emplace_back(T... args)
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
buffer(void const *ptr, std::size_t len)
std::unique_ptr< char[]> data
Configuration information for a Server listening port.