mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-18 18:15:50 +00:00
This updates Boost to 1.88, which is needed because Clio wants to move to 1.88 as that fixes several ASAN false positives around coroutine usage. In order for Clio to move to newer boost, libXRPL needs to move too. Hence the changes in this PR. A lot has changed between 1.83 and 1.88 so there are lots of changes in the diff, especially in regards to Boost.Asio and coroutines in particular.
179 lines
5.0 KiB
C++
179 lines
5.0 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of rippled: https://github.com/ripple/rippled
|
|
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#ifndef RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED
|
|
#define RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED
|
|
|
|
#include <xrpl/beast/rfc2616.h>
|
|
#include <xrpl/server/detail/BaseHTTPPeer.h>
|
|
#include <xrpl/server/detail/PlainWSPeer.h>
|
|
|
|
#include <boost/beast/core/tcp_stream.hpp>
|
|
|
|
#include <memory>
|
|
|
|
namespace ripple {
|
|
|
|
template <class Handler>
|
|
class PlainHTTPPeer
|
|
: public BaseHTTPPeer<Handler, PlainHTTPPeer<Handler>>,
|
|
public std::enable_shared_from_this<PlainHTTPPeer<Handler>>
|
|
{
|
|
private:
|
|
friend class BaseHTTPPeer<Handler, PlainHTTPPeer>;
|
|
using socket_type = boost::asio::ip::tcp::socket;
|
|
using stream_type = boost::beast::tcp_stream;
|
|
using endpoint_type = boost::asio::ip::tcp::endpoint;
|
|
|
|
stream_type stream_;
|
|
socket_type& socket_;
|
|
|
|
public:
|
|
template <class ConstBufferSequence>
|
|
PlainHTTPPeer(
|
|
Port const& port,
|
|
Handler& handler,
|
|
boost::asio::io_context& ioc,
|
|
beast::Journal journal,
|
|
endpoint_type remote_address,
|
|
ConstBufferSequence const& buffers,
|
|
stream_type&& stream);
|
|
|
|
void
|
|
run();
|
|
|
|
std::shared_ptr<WSSession>
|
|
websocketUpgrade() override;
|
|
|
|
private:
|
|
void
|
|
do_request() override;
|
|
|
|
void
|
|
do_close() override;
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <class Handler>
|
|
template <class ConstBufferSequence>
|
|
PlainHTTPPeer<Handler>::PlainHTTPPeer(
|
|
Port const& port,
|
|
Handler& handler,
|
|
boost::asio::io_context& ioc,
|
|
beast::Journal journal,
|
|
endpoint_type remote_endpoint,
|
|
ConstBufferSequence const& buffers,
|
|
stream_type&& stream)
|
|
: BaseHTTPPeer<Handler, PlainHTTPPeer>(
|
|
port,
|
|
handler,
|
|
ioc.get_executor(),
|
|
journal,
|
|
remote_endpoint,
|
|
buffers)
|
|
, stream_(std::move(stream))
|
|
, socket_(stream_.socket())
|
|
{
|
|
// Set TCP_NODELAY on loopback interfaces,
|
|
// otherwise Nagle's algorithm makes Env
|
|
// tests run slower on Linux systems.
|
|
//
|
|
if (remote_endpoint.address().is_loopback())
|
|
socket_.set_option(boost::asio::ip::tcp::no_delay{true});
|
|
}
|
|
|
|
template <class Handler>
|
|
void
|
|
PlainHTTPPeer<Handler>::run()
|
|
{
|
|
if (!this->handler_.onAccept(this->session(), this->remote_address_))
|
|
{
|
|
util::spawn(
|
|
this->strand_,
|
|
std::bind(&PlainHTTPPeer::do_close, this->shared_from_this()));
|
|
return;
|
|
}
|
|
|
|
if (!socket_.is_open())
|
|
return;
|
|
|
|
util::spawn(
|
|
this->strand_,
|
|
std::bind(
|
|
&PlainHTTPPeer::do_read,
|
|
this->shared_from_this(),
|
|
std::placeholders::_1));
|
|
}
|
|
|
|
template <class Handler>
|
|
std::shared_ptr<WSSession>
|
|
PlainHTTPPeer<Handler>::websocketUpgrade()
|
|
{
|
|
auto ws = this->ios().template emplace<PlainWSPeer<Handler>>(
|
|
this->port_,
|
|
this->handler_,
|
|
this->remote_address_,
|
|
std::move(this->message_),
|
|
std::move(stream_),
|
|
this->journal_);
|
|
return ws;
|
|
}
|
|
|
|
template <class Handler>
|
|
void
|
|
PlainHTTPPeer<Handler>::do_request()
|
|
{
|
|
++this->request_count_;
|
|
auto const what = this->handler_.onHandoff(
|
|
this->session(), std::move(this->message_), this->remote_address_);
|
|
if (what.moved)
|
|
return;
|
|
boost::system::error_code ec;
|
|
if (what.response)
|
|
{
|
|
// half-close on Connection: close
|
|
if (!what.keep_alive)
|
|
socket_.shutdown(socket_type::shutdown_receive, ec);
|
|
if (ec)
|
|
return this->fail(ec, "request");
|
|
return this->write(what.response, what.keep_alive);
|
|
}
|
|
|
|
// Perform half-close when Connection: close and not SSL
|
|
if (!beast::rfc2616::is_keep_alive(this->message_))
|
|
socket_.shutdown(socket_type::shutdown_receive, ec);
|
|
if (ec)
|
|
return this->fail(ec, "request");
|
|
// legacy
|
|
this->handler_.onRequest(this->session());
|
|
}
|
|
|
|
template <class Handler>
|
|
void
|
|
PlainHTTPPeer<Handler>::do_close()
|
|
{
|
|
boost::system::error_code ec;
|
|
socket_.shutdown(socket_type::shutdown_send, ec);
|
|
}
|
|
|
|
} // namespace ripple
|
|
|
|
#endif
|