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>
28#include <boost/asio/ip/tcp.hpp>
29#include <boost/asio/spawn.hpp>
30#include <boost/asio/ssl/stream.hpp>
31#include <boost/asio/streambuf.hpp>
32#include <boost/beast/core/stream_traits.hpp>
33#include <boost/beast/http/dynamic_body.hpp>
34#include <boost/beast/http/message.hpp>
35#include <boost/beast/http/parser.hpp>
36#include <boost/beast/http/read.hpp>
48template <
class Handler,
class Impl>
81 boost::asio::executor_work_guard<boost::asio::executor>
work_;
82 boost::asio::strand<boost::asio::executor>
strand_;
96 boost::system::error_code
ec_;
105 template <
class ConstBufferSequence>
109 boost::asio::executor
const& executor,
112 ConstBufferSequence
const& buffers);
129 return *
static_cast<Impl*
>(
this);
206template <
class Handler,
class Impl>
207template <
class ConstBufferSequence>
211 boost::asio::executor
const& executor,
214 ConstBufferSequence
const& buffers)
219 , remote_address_(remote_address)
222 read_buf_.commit(boost::asio::buffer_copy(
223 read_buf_.prepare(boost::asio::buffer_size(buffers)), buffers));
230template <
class Handler,
class Impl>
233 handler_.onClose(session(), ec_);
234 JLOG(journal_.trace()) << id_ <<
"destroyed: " << request_count_
235 << ((request_count_ == 1) ?
" request"
239template <
class Handler,
class Impl>
243 if (!strand_.running_in_this_thread())
248 impl().shared_from_this()));
249 boost::beast::get_lowest_layer(impl().stream_).close();
254template <
class Handler,
class Impl>
258 if (!ec_ && ec != boost::asio::error::operation_aborted)
261 JLOG(journal_.trace())
262 << id_ <<
std::string(what) <<
": " << ec.message();
263 boost::beast::get_lowest_layer(impl().stream_).close();
267template <
class Handler,
class Impl>
271 boost::beast::get_lowest_layer(impl().stream_)
273 remote_address_.address().is_loopback() ? timeoutSecondsLocal
278template <
class Handler,
class Impl>
282 boost::beast::get_lowest_layer(impl().stream_).expires_never();
286template <
class Handler,
class Impl>
291 boost::system::errc::make_error_code(boost::system::errc::timed_out);
297template <
class Handler,
class Impl>
304 boost::beast::http::async_read(
305 impl().stream_, read_buf_, message_, do_yield[ec]);
307 if (ec == boost::beast::http::error::end_of_stream)
309 if (ec == boost::beast::error::timeout)
312 return fail(ec,
"http::read");
318template <
class Handler,
class Impl>
325 if (ec == boost::beast::error::timeout)
328 return fail(ec,
"write");
329 bytes_out_ += bytes_transferred;
333 wq2_.reserve(wq_.size());
340 for (
auto const& b : wq2_)
343 return boost::asio::async_write(
350 impl().shared_from_this(),
351 std::placeholders::_1,
352 std::placeholders::_2)));
362 impl().shared_from_this(),
363 std::placeholders::_1));
366template <
class Handler,
class Impl>
375 auto const p = impl().shared_from_this();
384 std::placeholders::_1));
390 if (!writer->prepare(bufferSize, resume))
393 auto const bytes_transferred = boost::asio::async_write(
396 boost::asio::transfer_at_least(1),
399 return fail(ec,
"writer");
400 writer->consume(bytes_transferred);
401 if (writer->complete())
412 impl().shared_from_this(),
413 std::placeholders::_1));
419template <
class Handler,
class Impl>
427 wq_.emplace_back(buf, bytes);
428 return wq_.size() == 1 && wq2_.size() == 0;
431 if (!strand_.running_in_this_thread())
436 impl().shared_from_this(),
444template <
class Handler,
class Impl>
450 boost::asio::spawn(bind_executor(
454 impl().shared_from_this(),
457 std::placeholders::_1)));
462template <
class Handler,
class Impl>
466 return impl().shared_from_this();
471template <
class Handler,
class Impl>
475 if (!strand_.running_in_this_thread())
480 impl().shared_from_this()));
487 if (!wq_.empty() && !wq2_.empty())
492 boost::asio::spawn(bind_executor(
496 impl().shared_from_this(),
497 std::placeholders::_1)));
502template <
class Handler,
class Impl>
506 if (!strand_.running_in_this_thread())
512 impl().shared_from_this(),
521 if (!wq_.empty() || !wq2_.empty())
527 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.