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)
201 read_buf_.commit(boost::asio::buffer_copy(
read_buf_.prepare(boost::asio::buffer_size(buffers)), buffers));
208template <
class Handler,
class Impl>
211 handler_.onClose(session(), ec_);
212 JLOG(journal_.trace()) << id_ <<
"destroyed: " << request_count_
213 << ((request_count_ == 1) ?
" request" :
" requests");
216template <
class Handler,
class Impl>
220 if (!strand_.running_in_this_thread())
222 boost::beast::get_lowest_layer(impl().stream_).close();
227template <
class Handler,
class Impl>
231 if (!ec_ && ec != boost::asio::error::operation_aborted)
234 JLOG(journal_.trace()) << id_ <<
std::string(what) <<
": " << ec.message();
235 boost::beast::get_lowest_layer(impl().stream_).close();
239template <
class Handler,
class Impl>
243 boost::beast::get_lowest_layer(impl().stream_)
245 std::chrono::seconds(remote_address_.address().is_loopback() ? timeoutSecondsLocal : timeoutSeconds));
249template <
class Handler,
class Impl>
253 boost::beast::get_lowest_layer(impl().stream_).expires_never();
257template <
class Handler,
class Impl>
261 auto ec = boost::system::errc::make_error_code(boost::system::errc::timed_out);
267template <
class Handler,
class Impl>
274 boost::beast::http::async_read(impl().stream_, read_buf_, message_, do_yield[ec]);
276 if (ec == boost::beast::http::error::end_of_stream)
278 if (ec == boost::beast::error::timeout)
281 return fail(ec,
"http::read");
287template <
class Handler,
class Impl>
292 if (ec == boost::beast::error::timeout)
295 return fail(ec,
"write");
296 bytes_out_ += bytes_transferred;
300 wq2_.reserve(wq_.size());
307 for (
auto const& b : wq2_)
310 return boost::asio::async_write(
326template <
class Handler,
class Impl>
332 auto const p = impl().shared_from_this();
342 if (!writer->prepare(bufferSize, resume))
345 auto const bytes_transferred =
346 boost::asio::async_write(impl().stream_, writer->data(), boost::asio::transfer_at_least(1), do_yield[ec]);
348 return fail(ec,
"writer");
349 writer->consume(bytes_transferred);
350 if (writer->complete())
364template <
class Handler,
class Impl>
372 wq_.emplace_back(buf, bytes);
373 return wq_.size() == 1 && wq2_.size() == 0;
376 if (!strand_.running_in_this_thread())
383template <
class Handler,
class Impl>
391 impl().shared_from_this(),
394 std::placeholders::_1));
399template <
class Handler,
class Impl>
403 return impl().shared_from_this();
408template <
class Handler,
class Impl>
412 if (!strand_.running_in_this_thread())
420 if (!wq_.empty() && !wq2_.empty())
431template <
class Handler,
class Impl>
435 if (!strand_.running_in_this_thread())
440 impl().shared_from_this(),
449 if (!wq_.empty() || !wq2_.empty())
455 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.