20#ifndef RIPPLE_SERVER_BASEHTTPPEER_H_INCLUDED
21#define RIPPLE_SERVER_BASEHTTPPEER_H_INCLUDED
23#include <xrpl/basics/Log.h>
24#include <xrpl/beast/net/IPAddressConversion.h>
25#include <xrpl/beast/utility/instrumentation.h>
26#include <xrpl/server/Session.h>
27#include <xrpl/server/detail/Spawn.h>
28#include <xrpl/server/detail/io_list.h>
30#include <boost/asio/ip/tcp.hpp>
31#include <boost/asio/spawn.hpp>
32#include <boost/asio/ssl/stream.hpp>
33#include <boost/asio/strand.hpp>
34#include <boost/asio/streambuf.hpp>
35#include <boost/beast/core/stream_traits.hpp>
36#include <boost/beast/http/dynamic_body.hpp>
37#include <boost/beast/http/message.hpp>
38#include <boost/beast/http/parser.hpp>
39#include <boost/beast/http/read.hpp>
51template <
class Handler,
class Impl>
84 boost::asio::executor_work_guard<boost::asio::executor>
work_;
85 boost::asio::strand<boost::asio::executor>
strand_;
99 boost::system::error_code
ec_;
108 template <
class ConstBufferSequence>
112 boost::asio::executor
const& executor,
115 ConstBufferSequence
const& buffers);
132 return *
static_cast<Impl*
>(
this);
209template <
class Handler,
class Impl>
210template <
class ConstBufferSequence>
214 boost::asio::executor
const& executor,
217 ConstBufferSequence
const& buffers)
220 , work_(
boost::asio::make_work_guard(executor))
221 , strand_(
boost::asio::make_strand(executor))
222 , remote_address_(remote_address)
225 read_buf_.commit(boost::asio::buffer_copy(
226 read_buf_.prepare(boost::asio::buffer_size(buffers)), buffers));
233template <
class Handler,
class Impl>
236 handler_.onClose(session(), ec_);
237 JLOG(journal_.trace()) << id_ <<
"destroyed: " << request_count_
238 << ((request_count_ == 1) ?
" request"
242template <
class Handler,
class Impl>
246 if (!strand_.running_in_this_thread())
251 impl().shared_from_this()));
252 boost::beast::get_lowest_layer(impl().stream_).close();
257template <
class Handler,
class Impl>
261 if (!ec_ && ec != boost::asio::error::operation_aborted)
264 JLOG(journal_.trace())
265 << id_ <<
std::string(what) <<
": " << ec.message();
266 boost::beast::get_lowest_layer(impl().stream_).close();
270template <
class Handler,
class Impl>
274 boost::beast::get_lowest_layer(impl().stream_)
276 remote_address_.address().is_loopback() ? timeoutSecondsLocal
281template <
class Handler,
class Impl>
285 boost::beast::get_lowest_layer(impl().stream_).expires_never();
289template <
class Handler,
class Impl>
294 boost::system::errc::make_error_code(boost::system::errc::timed_out);
300template <
class Handler,
class Impl>
307 boost::beast::http::async_read(
308 impl().stream_, read_buf_, message_, do_yield[ec]);
310 if (ec == boost::beast::http::error::end_of_stream)
312 if (ec == boost::beast::error::timeout)
315 return fail(ec,
"http::read");
321template <
class Handler,
class Impl>
328 if (ec == boost::beast::error::timeout)
331 return fail(ec,
"write");
332 bytes_out_ += bytes_transferred;
336 wq2_.reserve(wq_.size());
343 for (
auto const& b : wq2_)
346 return boost::asio::async_write(
353 impl().shared_from_this(),
354 std::placeholders::_1,
355 std::placeholders::_2)));
365 impl().shared_from_this(),
366 std::placeholders::_1));
369template <
class Handler,
class Impl>
378 auto const p = impl().shared_from_this();
387 std::placeholders::_1));
393 if (!writer->prepare(bufferSize, resume))
396 auto const bytes_transferred = boost::asio::async_write(
399 boost::asio::transfer_at_least(1),
402 return fail(ec,
"writer");
403 writer->consume(bytes_transferred);
404 if (writer->complete())
415 impl().shared_from_this(),
416 std::placeholders::_1));
422template <
class Handler,
class Impl>
430 wq_.emplace_back(buf, bytes);
431 return wq_.size() == 1 && wq2_.size() == 0;
434 if (!strand_.running_in_this_thread())
439 impl().shared_from_this(),
447template <
class Handler,
class Impl>
457 impl().shared_from_this(),
460 std::placeholders::_1));
465template <
class Handler,
class Impl>
469 return impl().shared_from_this();
474template <
class Handler,
class Impl>
478 if (!strand_.running_in_this_thread())
483 impl().shared_from_this()));
490 if (!wq_.empty() && !wq2_.empty())
499 impl().shared_from_this(),
500 std::placeholders::_1));
505template <
class Handler,
class Impl>
509 if (!strand_.running_in_this_thread())
515 impl().shared_from_this(),
524 if (!wq_.empty() || !wq2_.empty())
530 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.
BaseHTTPPeer(Port const &port, Handler &handler, boost::asio::executor const &executor, beast::Journal journal, endpoint_type remote_address, ConstBufferSequence const &buffers)
boost::asio::yield_context yield_context
void write(void const *buffer, std::size_t bytes) override
boost::system::error_code error_code
Port const & port() override
Returns the Port settings for this connection.
http_request_type message_
std::vector< buffer > wq2_
boost::asio::strand< boost::asio::executor > strand_
void on_write(error_code const &ec, std::size_t bytes_transferred)
void write(std::shared_ptr< Writer > const &writer, bool keep_alive) override
virtual void do_close()=0
beast::Journal const journal_
endpoint_type remote_address_
boost::asio::ip::tcp::endpoint endpoint_type
virtual void do_request()=0
boost::asio::streambuf read_buf_
std::vector< buffer > wq_
boost::system::error_code ec_
beast::IP::Endpoint remoteAddress() override
Returns the remote address of the connection.
void close(bool graceful) override
Close the session.
std::shared_ptr< Session > detach() override
Detach the session.
http_request_type & request() override
Returns the current HTTP request.
void complete() override
Indicate that the response is complete.
void do_writer(std::shared_ptr< Writer > const &writer, bool keep_alive, yield_context do_yield)
void fail(error_code ec, char const *what)
void do_read(yield_context do_yield)
boost::asio::executor_work_guard< boost::asio::executor > work_
beast::Journal journal() override
Returns the Journal to use for logging.
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.