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>
32template <
class Handler,
class Impl>
49 memcpy(
data.get(), ptr, len);
59 boost::asio::executor_work_guard<boost::asio::executor>
work_;
60 boost::asio::strand<boost::asio::executor>
strand_;
74 boost::system::error_code
ec_;
83 template <
class ConstBufferSequence>
87 boost::asio::executor
const& executor,
90 ConstBufferSequence
const& buffers);
107 return *
static_cast<Impl*
>(
this);
176 close(
bool graceful)
override;
181template <
class Handler,
class Impl>
182template <
class ConstBufferSequence>
186 boost::asio::executor
const& executor,
189 ConstBufferSequence
const& buffers)
192 ,
work_(
boost::asio::make_work_guard(executor))
198 boost::asio::buffer_copy(
readBuf_.prepare(boost::asio::buffer_size(buffers)), buffers));
205template <
class Handler,
class Impl>
213template <
class Handler,
class Impl>
217 if (!
strand_.running_in_this_thread())
224 boost::beast::get_lowest_layer(
impl().stream_).close();
229template <
class Handler,
class Impl>
233 if (!
ec_ && ec != boost::asio::error::operation_aborted)
237 boost::beast::get_lowest_layer(
impl().stream_).close();
241template <
class Handler,
class Impl>
245 boost::beast::get_lowest_layer(
impl().stream_)
252template <
class Handler,
class Impl>
256 boost::beast::get_lowest_layer(
impl().stream_).expires_never();
260template <
class Handler,
class Impl>
264 auto ec = boost::system::errc::make_error_code(boost::system::errc::timed_out);
270template <
class Handler,
class Impl>
279 if (ec == boost::beast::http::error::end_of_stream)
281 if (ec == boost::beast::error::timeout)
284 return fail(ec,
"http::read");
290template <
class Handler,
class Impl>
295 if (ec == boost::beast::error::timeout)
298 return fail(ec,
"write");
310 for (
auto const& b :
wq2_)
313 return boost::asio::async_write(
320 impl().shared_from_this(),
321 std::placeholders::_1,
322 std::placeholders::_2)));
332 impl().shared_from_this(),
333 std::placeholders::_1));
336template <
class Handler,
class Impl>
345 auto const p =
impl().shared_from_this();
354 std::placeholders::_1));
363 auto const bytesTransferred = boost::asio::async_write(
364 impl().stream_, writer->data(), boost::asio::transfer_at_least(1), doYield[ec]);
366 return fail(ec,
"writer");
367 writer->consume(bytesTransferred);
368 if (writer->complete())
379 impl().shared_from_this(),
380 std::placeholders::_1));
386template <
class Handler,
class Impl>
394 wq_.emplace_back(buf, bytes);
395 return wq_.size() == 1 &&
wq2_.size() == 0;
398 if (!
strand_.running_in_this_thread())
408template <
class Handler,
class Impl>
416 impl().shared_from_this(),
419 std::placeholders::_1));
424template <
class Handler,
class Impl>
428 return impl().shared_from_this();
433template <
class Handler,
class Impl>
437 if (!
strand_.running_in_this_thread())
448 if (!
wq_.empty() && !
wq2_.empty())
457 impl().shared_from_this(),
458 std::placeholders::_1));
463template <
class Handler,
class Impl>
467 if (!
strand_.running_in_this_thread())
473 impl().shared_from_this(),
483 if (!
wq_.empty() || !
wq2_.empty())
489 boost::beast::get_lowest_layer(
impl().stream_).close();
A version-independent IP address and port combination.
A generic endpoint for log messages.
BaseHTTPPeer(Port const &port, Handler &handler, boost::asio::executor const &executor, beast::Journal journal, endpoint_type remoteAddress, ConstBufferSequence const &buffers)
endpoint_type remoteAddress_
http_request_type message_
void write(void const *buffer, std::size_t bytes) override
beast::IP::Endpoint remoteAddress() override
boost::asio::ip::tcp::endpoint endpoint_type
void onWrite(error_code const &ec, std::size_t bytesTransferred)
boost::system::error_code ec_
boost::system::error_code error_code
beast::Journal const journal_
void fail(error_code ec, char const *what)
void doWriter(std::shared_ptr< Writer > const &writer, bool keepAlive, yield_context doYield)
boost::asio::streambuf readBuf_
std::shared_ptr< Session > detach() override
Detach the session.
beast::Journal journal() override
boost::asio::strand< boost::asio::executor > strand_
static constexpr auto kTimeoutSecondsLocal
boost::asio::executor_work_guard< boost::asio::executor > work_
virtual void doRequest()=0
void complete() override
Indicate that the response is complete.
std::vector< Buffer > wq2_
std::chrono::system_clock clock_type
Port const & port() override
static constexpr auto kBufferSize
std::vector< Buffer > wq_
void doRead(yield_context doYield)
static constexpr auto kTimeoutSeconds
http_request_type & request() override
Returns the current HTTP request.
boost::asio::yield_context yield_context
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 fromAsio(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.