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/io_list.h>
29#include <boost/asio/ip/tcp.hpp>
30#include <boost/asio/spawn.hpp>
31#include <boost/asio/ssl/stream.hpp>
32#include <boost/asio/streambuf.hpp>
33#include <boost/beast/core/stream_traits.hpp>
34#include <boost/beast/http/dynamic_body.hpp>
35#include <boost/beast/http/message.hpp>
36#include <boost/beast/http/parser.hpp>
37#include <boost/beast/http/read.hpp>
50template <
class Handler,
class Impl>
83 boost::asio::executor_work_guard<boost::asio::executor>
work_;
84 boost::asio::strand<boost::asio::executor>
strand_;
98 boost::system::error_code
ec_;
107 template <
class ConstBufferSequence>
111 boost::asio::executor
const& executor,
114 ConstBufferSequence
const& buffers);
131 return *
static_cast<Impl*
>(
this);
208template <
class Handler,
class Impl>
209template <
class ConstBufferSequence>
213 boost::asio::executor
const& executor,
216 ConstBufferSequence
const& buffers)
221 , remote_address_(remote_address)
224 read_buf_.commit(boost::asio::buffer_copy(
225 read_buf_.prepare(boost::asio::buffer_size(buffers)), buffers));
232template <
class Handler,
class Impl>
235 handler_.onClose(session(), ec_);
236 JLOG(journal_.trace()) << id_ <<
"destroyed: " << request_count_
237 << ((request_count_ == 1) ?
" request"
241template <
class Handler,
class Impl>
245 if (!strand_.running_in_this_thread())
250 impl().shared_from_this()));
251 boost::beast::get_lowest_layer(impl().stream_).close();
256template <
class Handler,
class Impl>
260 if (!ec_ && ec != boost::asio::error::operation_aborted)
263 JLOG(journal_.trace())
264 << id_ <<
std::string(what) <<
": " << ec.message();
265 boost::beast::get_lowest_layer(impl().stream_).close();
269template <
class Handler,
class Impl>
273 boost::beast::get_lowest_layer(impl().stream_)
275 remote_address_.address().is_loopback() ? timeoutSecondsLocal
280template <
class Handler,
class Impl>
284 boost::beast::get_lowest_layer(impl().stream_).expires_never();
288template <
class Handler,
class Impl>
293 boost::system::errc::make_error_code(boost::system::errc::timed_out);
299template <
class Handler,
class Impl>
306 boost::beast::http::async_read(
307 impl().stream_, read_buf_, message_, do_yield[ec]);
309 if (ec == boost::beast::http::error::end_of_stream)
311 if (ec == boost::beast::error::timeout)
314 return fail(ec,
"http::read");
320template <
class Handler,
class Impl>
327 if (ec == boost::beast::error::timeout)
330 return fail(ec,
"write");
331 bytes_out_ += bytes_transferred;
335 wq2_.reserve(wq_.size());
342 for (
auto const& b : wq2_)
345 return boost::asio::async_write(
352 impl().shared_from_this(),
353 std::placeholders::_1,
354 std::placeholders::_2)));
364 impl().shared_from_this(),
365 std::placeholders::_1));
368template <
class Handler,
class Impl>
377 auto const p = impl().shared_from_this();
386 std::placeholders::_1));
392 if (!writer->prepare(bufferSize, resume))
395 auto const bytes_transferred = boost::asio::async_write(
398 boost::asio::transfer_at_least(1),
401 return fail(ec,
"writer");
402 writer->consume(bytes_transferred);
403 if (writer->complete())
414 impl().shared_from_this(),
415 std::placeholders::_1));
421template <
class Handler,
class Impl>
429 wq_.emplace_back(buf, bytes);
430 return wq_.size() == 1 && wq2_.size() == 0;
433 if (!strand_.running_in_this_thread())
438 impl().shared_from_this(),
446template <
class Handler,
class Impl>
452 boost::asio::spawn(bind_executor(
456 impl().shared_from_this(),
459 std::placeholders::_1)));
464template <
class Handler,
class Impl>
468 return impl().shared_from_this();
473template <
class Handler,
class Impl>
477 if (!strand_.running_in_this_thread())
482 impl().shared_from_this()));
489 if (!wq_.empty() && !wq2_.empty())
494 boost::asio::spawn(bind_executor(
498 impl().shared_from_this(),
499 std::placeholders::_1)));
504template <
class Handler,
class Impl>
508 if (!strand_.running_in_this_thread())
514 impl().shared_from_this(),
523 if (!wq_.empty() || !wq2_.empty())
529 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)
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.