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>
49template <
class Handler,
class Impl>
82 boost::asio::executor_work_guard<boost::asio::executor>
work_;
83 boost::asio::strand<boost::asio::executor>
strand_;
97 boost::system::error_code
ec_;
106 template <
class ConstBufferSequence>
110 boost::asio::executor
const& executor,
113 ConstBufferSequence
const& buffers);
130 return *
static_cast<Impl*
>(
this);
207template <
class Handler,
class Impl>
208template <
class ConstBufferSequence>
212 boost::asio::executor
const& executor,
215 ConstBufferSequence
const& buffers)
220 , remote_address_(remote_address)
223 read_buf_.commit(boost::asio::buffer_copy(
224 read_buf_.prepare(boost::asio::buffer_size(buffers)), buffers));
231template <
class Handler,
class Impl>
234 handler_.onClose(session(), ec_);
235 JLOG(journal_.trace()) << id_ <<
"destroyed: " << request_count_
236 << ((request_count_ == 1) ?
" request"
240template <
class Handler,
class Impl>
244 if (!strand_.running_in_this_thread())
249 impl().shared_from_this()));
250 boost::beast::get_lowest_layer(impl().stream_).close();
255template <
class Handler,
class Impl>
259 if (!ec_ && ec != boost::asio::error::operation_aborted)
262 JLOG(journal_.trace())
263 << id_ <<
std::string(what) <<
": " << ec.message();
264 boost::beast::get_lowest_layer(impl().stream_).close();
268template <
class Handler,
class Impl>
272 boost::beast::get_lowest_layer(impl().stream_)
274 remote_address_.address().is_loopback() ? timeoutSecondsLocal
279template <
class Handler,
class Impl>
283 boost::beast::get_lowest_layer(impl().stream_).expires_never();
287template <
class Handler,
class Impl>
292 boost::system::errc::make_error_code(boost::system::errc::timed_out);
298template <
class Handler,
class Impl>
305 boost::beast::http::async_read(
306 impl().stream_, read_buf_, message_, do_yield[ec]);
308 if (ec == boost::beast::http::error::end_of_stream)
310 if (ec == boost::beast::error::timeout)
313 return fail(ec,
"http::read");
319template <
class Handler,
class Impl>
326 if (ec == boost::beast::error::timeout)
329 return fail(ec,
"write");
330 bytes_out_ += bytes_transferred;
334 wq2_.reserve(wq_.size());
341 for (
auto const& b : wq2_)
344 return boost::asio::async_write(
351 impl().shared_from_this(),
352 std::placeholders::_1,
353 std::placeholders::_2)));
363 impl().shared_from_this(),
364 std::placeholders::_1));
367template <
class Handler,
class Impl>
376 auto const p = impl().shared_from_this();
385 std::placeholders::_1));
391 if (!writer->prepare(bufferSize, resume))
394 auto const bytes_transferred = boost::asio::async_write(
397 boost::asio::transfer_at_least(1),
400 return fail(ec,
"writer");
401 writer->consume(bytes_transferred);
402 if (writer->complete())
413 impl().shared_from_this(),
414 std::placeholders::_1));
420template <
class Handler,
class Impl>
428 wq_.emplace_back(buf, bytes);
429 return wq_.size() == 1 && wq2_.size() == 0;
432 if (!strand_.running_in_this_thread())
437 impl().shared_from_this(),
445template <
class Handler,
class Impl>
451 boost::asio::spawn(bind_executor(
455 impl().shared_from_this(),
458 std::placeholders::_1)));
463template <
class Handler,
class Impl>
467 return impl().shared_from_this();
472template <
class Handler,
class Impl>
476 if (!strand_.running_in_this_thread())
481 impl().shared_from_this()));
488 if (!wq_.empty() && !wq2_.empty())
493 boost::asio::spawn(bind_executor(
497 impl().shared_from_this(),
498 std::placeholders::_1)));
503template <
class Handler,
class Impl>
507 if (!strand_.running_in_this_thread())
513 impl().shared_from_this(),
522 if (!wq_.empty() || !wq2_.empty())
528 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.