Changes for Beast version 59

This commit is contained in:
Vinnie Falco
2017-06-12 01:03:19 -07:00
committed by Miguel Portilla
parent 49bdf2e72d
commit 61316c7f95
36 changed files with 367 additions and 366 deletions

View File

@@ -22,8 +22,7 @@
#include <ripple/app/misc/detail/Work.h> #include <ripple/app/misc/detail/Work.h>
#include <ripple/protocol/BuildInfo.h> #include <ripple/protocol/BuildInfo.h>
#include <beast/core/placeholders.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/http/empty_body.hpp> #include <beast/http/empty_body.hpp>
#include <beast/http/read.hpp> #include <beast/http/read.hpp>
#include <beast/http/write.hpp> #include <beast/http/write.hpp>
@@ -61,7 +60,7 @@ protected:
socket_type socket_; socket_type socket_;
request_type req_; request_type req_;
response_type res_; response_type res_;
beast::streambuf read_buf_; beast::multi_buffer read_buf_;
public: public:
WorkBase( WorkBase(
@@ -132,8 +131,8 @@ WorkBase<Impl>::run()
resolver_.async_resolve( resolver_.async_resolve(
query_type{host_, port_}, query_type{host_, port_},
strand_.wrap (std::bind(&WorkBase::onResolve, impl().shared_from_this(), strand_.wrap (std::bind(&WorkBase::onResolve, impl().shared_from_this(),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::iterator))); std::placeholders::_2)));
} }
template<class Impl> template<class Impl>
@@ -171,24 +170,23 @@ WorkBase<Impl>::onResolve(error_code const& ec, resolver_type::iterator it)
socket_.async_connect(*it, socket_.async_connect(*it,
strand_.wrap (std::bind(&Impl::onConnect, impl().shared_from_this(), strand_.wrap (std::bind(&Impl::onConnect, impl().shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
template<class Impl> template<class Impl>
void void
WorkBase<Impl>::onStart() WorkBase<Impl>::onStart()
{ {
req_.method = "GET"; req_.method(beast::http::verb::get);
req_.url = path_.empty() ? "/" : path_; req_.target(path_.empty() ? "/" : path_);
req_.version = 11; req_.version = 11;
req_.fields.replace ( req_.replace (
"Host", host_ + ":" + port_); "Host", host_ + ":" + port_);
req_.fields.replace ("User-Agent", BuildInfo::getFullVersionString()); req_.replace ("User-Agent", BuildInfo::getFullVersionString());
beast::http::prepare (req_); req_.prepare();
beast::http::async_write(impl().stream(), req_, beast::http::async_write(impl().stream(), req_,
strand_.wrap (std::bind (&WorkBase::onRequest, strand_.wrap (std::bind (&WorkBase::onRequest,
impl().shared_from_this(), beast::asio::placeholders::error))); impl().shared_from_this(), std::placeholders::_1)));
} }
template<class Impl> template<class Impl>
@@ -200,7 +198,7 @@ WorkBase<Impl>::onRequest(error_code const& ec)
beast::http::async_read (impl().stream(), read_buf_, res_, beast::http::async_read (impl().stream(), read_buf_, res_,
strand_.wrap (std::bind (&WorkBase::onResponse, strand_.wrap (std::bind (&WorkBase::onResponse,
impl().shared_from_this(), beast::asio::placeholders::error))); impl().shared_from_this(), std::placeholders::_1)));
} }
template<class Impl> template<class Impl>

View File

@@ -144,7 +144,7 @@ ValidatorSite::setTimer ()
timer_.expires_at (next->nextRefresh); timer_.expires_at (next->nextRefresh);
timer_.async_wait (std::bind (&ValidatorSite::onTimer, this, timer_.async_wait (std::bind (&ValidatorSite::onTimer, this,
std::distance (sites_.begin (), next), std::distance (sites_.begin (), next),
beast::asio::placeholders::error)); std::placeholders::_1));
} }
} }
@@ -205,12 +205,12 @@ ValidatorSite::onSiteFetch(
detail::response_type&& res, detail::response_type&& res,
std::size_t siteIdx) std::size_t siteIdx)
{ {
if (! ec && res.status != 200) if (! ec && res.result() != beast::http::status::ok)
{ {
std::lock_guard <std::mutex> lock{sites_mutex_}; std::lock_guard <std::mutex> lock{sites_mutex_};
JLOG (j_.warn()) << JLOG (j_.warn()) <<
"Request for validator list at " << "Request for validator list at " <<
sites_[siteIdx].uri << " returned " << res.status; sites_[siteIdx].uri << " returned " << res.result_int();
} }
else if (! ec) else if (! ec)
{ {

View File

@@ -21,7 +21,6 @@
#include <ripple/basics/ResolverAsio.h> #include <ripple/basics/ResolverAsio.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/beast/net/IPAddressConversion.h> #include <ripple/beast/net/IPAddressConversion.h>
#include <beast/core/placeholders.hpp>
#include <ripple/beast/core/WaitableEvent.h> #include <ripple/beast/core/WaitableEvent.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <atomic> #include <atomic>
@@ -336,8 +335,8 @@ public:
m_resolver.async_resolve (query, std::bind ( m_resolver.async_resolve (query, std::bind (
&ResolverAsioImpl::do_finish, this, name, &ResolverAsioImpl::do_finish, this, name,
beast::asio::placeholders::error, handler, std::placeholders::_1, handler,
beast::asio::placeholders::iterator, std::placeholders::_2,
CompletionCounter (this))); CompletionCounter (this)));
} }

View File

@@ -22,7 +22,7 @@
#include <ripple/basics/win32_workaround.h> #include <ripple/basics/win32_workaround.h>
#include <ripple/beast/xor_shift_engine.h> #include <ripple/beast/xor_shift_engine.h>
#include <beast/core/detail/is_call_possible.hpp> #include <beast/core/detail/type_traits.hpp>
#include <boost/thread/tss.hpp> #include <boost/thread/tss.hpp>
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
@@ -52,7 +52,7 @@ namespace detail {
// Determines if a type can be called like an Engine // Determines if a type can be called like an Engine
template <class Engine, class Result = typename Engine::result_type> template <class Engine, class Result = typename Engine::result_type>
using is_engine = using is_engine =
beast::detail::is_call_possible<Engine, Result()>; beast::detail::is_invocable<Engine, Result()>;
} }
/** Return the default random engine. /** Return the default random engine.

View File

@@ -24,7 +24,6 @@
#include <ripple/beast/insight/GaugeImpl.h> #include <ripple/beast/insight/GaugeImpl.h>
#include <ripple/beast/insight/MeterImpl.h> #include <ripple/beast/insight/MeterImpl.h>
#include <ripple/beast/insight/StatsDCollector.h> #include <ripple/beast/insight/StatsDCollector.h>
#include <beast/core/placeholders.hpp>
#include <ripple/beast/core/List.h> #include <ripple/beast/core/List.h>
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@@ -383,8 +382,8 @@ public:
#endif #endif
m_socket.async_send (buffers, std::bind ( m_socket.async_send (buffers, std::bind (
&StatsDCollectorImp::on_send, this, keepAlive, &StatsDCollectorImp::on_send, this, keepAlive,
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
buffers.clear (); buffers.clear ();
size = 0; size = 0;
} }
@@ -400,8 +399,8 @@ public:
#endif #endif
m_socket.async_send (buffers, std::bind ( m_socket.async_send (buffers, std::bind (
&StatsDCollectorImp::on_send, this, keepAlive, &StatsDCollectorImp::on_send, this, keepAlive,
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
} }
} }
@@ -411,7 +410,7 @@ public:
m_timer.expires_from_now(1s); m_timer.expires_from_now(1s);
m_timer.async_wait (std::bind ( m_timer.async_wait (std::bind (
&StatsDCollectorImp::on_timer, this, &StatsDCollectorImp::on_timer, this,
beast::asio::placeholders::error)); std::placeholders::_1));
} }
void on_timer (boost::system::error_code ec) void on_timer (boost::system::error_code ec)

View File

@@ -20,6 +20,8 @@
#ifndef BEAST_RFC2616_HPP #ifndef BEAST_RFC2616_HPP
#define BEAST_RFC2616_HPP #define BEAST_RFC2616_HPP
#include <beast/http/message.hpp>
#include <beast/http/rfc7230.hpp>
#include <boost/range/algorithm/equal.hpp> #include <boost/range/algorithm/equal.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range.hpp>
#include <boost/utility/string_ref.hpp> #include <boost/utility/string_ref.hpp>
@@ -464,6 +466,17 @@ token_in_list(boost::string_ref const& value,
return false; return false;
} }
template<bool isRequest, class Body, class Fields>
bool
is_keep_alive(beast::http::message<isRequest, Body, Fields> const& m)
{
if(m.version <= 10)
return beast::http::token_list{
m[beast::http::field::connection]}.exists("keep-alive");
return ! beast::http::token_list{
m[beast::http::field::connection]}.exists("close");
}
} // rfc2616 } // rfc2616
} // beast } // beast

View File

@@ -22,7 +22,6 @@
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/random.h> #include <ripple/basics/random.h>
#include <ripple/beast/core/CurrentThreadName.h> #include <ripple/beast/core/CurrentThreadName.h>
#include <beast/core/placeholders.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <cmath> #include <cmath>
@@ -164,12 +163,12 @@ public:
socket_.async_receive_from (buffer (buf_, 256), socket_.async_receive_from (buffer (buf_, 256),
ep_, std::bind( ep_, std::bind(
&SNTPClientImp::onRead, this, &SNTPClientImp::onRead, this,
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
timer_.expires_from_now(NTP_QUERY_FREQUENCY); timer_.expires_from_now(NTP_QUERY_FREQUENCY);
timer_.async_wait(std::bind( timer_.async_wait(std::bind(
&SNTPClientImp::onTimer, this, &SNTPClientImp::onTimer, this,
beast::asio::placeholders::error)); std::placeholders::_1));
// VFALCO Is it correct to launch the thread // VFALCO Is it correct to launch the thread
// here after queuing I/O? // here after queuing I/O?
@@ -222,7 +221,7 @@ public:
timer_.expires_from_now(NTP_QUERY_FREQUENCY); timer_.expires_from_now(NTP_QUERY_FREQUENCY);
timer_.async_wait(std::bind( timer_.async_wait(std::bind(
&SNTPClientImp::onTimer, this, &SNTPClientImp::onTimer, this,
beast::asio::placeholders::error)); std::placeholders::_1));
} }
void void
@@ -286,8 +285,8 @@ public:
socket_.async_receive_from(buffer(buf_, 256), socket_.async_receive_from(buffer(buf_, 256),
ep_, std::bind(&SNTPClientImp::onRead, this, ep_, std::bind(&SNTPClientImp::onRead, this,
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@@ -339,8 +338,8 @@ public:
boost::asio::ip::udp::v4 (), best->first, "ntp"); boost::asio::ip::udp::v4 (), best->first, "ntp");
resolver_.async_resolve (query, std::bind ( resolver_.async_resolve (query, std::bind (
&SNTPClientImp::resolveComplete, this, &SNTPClientImp::resolveComplete, this,
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::iterator)); std::placeholders::_2));
JLOG(j_.trace()) << JLOG(j_.trace()) <<
"SNTPClock: Resolve pending for " << best->first; "SNTPClock: Resolve pending for " << best->first;
return true; return true;
@@ -396,8 +395,8 @@ public:
reinterpret_cast<std::uint32_t*> (SNTPQueryData)[NTP_OFF_XMITTS_FRAC] = query.nonce; reinterpret_cast<std::uint32_t*> (SNTPQueryData)[NTP_OFF_XMITTS_FRAC] = query.nonce;
socket_.async_send_to(buffer(SNTPQueryData, 48), socket_.async_send_to(buffer(SNTPQueryData, 48),
*sel, std::bind (&SNTPClientImp::onSend, this, *sel, std::bind (&SNTPClientImp::onSend, this,
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
} }
} }

View File

@@ -23,7 +23,6 @@
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/beast/net/IPAddressConversion.h> #include <ripple/beast/net/IPAddressConversion.h>
#include <beast/core/bind_handler.hpp> #include <beast/core/bind_handler.hpp>
#include <beast/core/placeholders.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>
@@ -185,8 +184,8 @@ public:
std::bind ( std::bind (
&AutoSocket::handle_autodetect, &AutoSocket::handle_autodetect,
this, cbFunc, this, cbFunc,
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
} }
} }

View File

@@ -23,7 +23,6 @@
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/net/HTTPClient.h> #include <ripple/net/HTTPClient.h>
#include <ripple/net/AutoSocket.h> #include <ripple/net/AutoSocket.h>
#include <beast/core/placeholders.hpp>
#include <ripple/beast/core/LexicalCast.h> #include <ripple/beast/core/LexicalCast.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>
@@ -196,7 +195,7 @@ public:
std::bind ( std::bind (
&HTTPClientImp::handleDeadline, &HTTPClientImp::handleDeadline,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error)); std::placeholders::_1));
} }
if (!mShutdown) if (!mShutdown)
@@ -207,8 +206,8 @@ public:
std::bind ( std::bind (
&HTTPClientImp::handleResolve, &HTTPClientImp::handleResolve,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::iterator)); std::placeholders::_2));
} }
if (mShutdown) if (mShutdown)
@@ -246,7 +245,7 @@ public:
mSocket.async_shutdown (std::bind ( mSocket.async_shutdown (std::bind (
&HTTPClientImp::handleShutdown, &HTTPClientImp::handleShutdown,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error)); std::placeholders::_1));
} }
} }
@@ -285,7 +284,7 @@ public:
std::bind ( std::bind (
&HTTPClientImp::handleConnect, &HTTPClientImp::handleConnect,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error)); std::placeholders::_1));
} }
} }
@@ -325,7 +324,7 @@ public:
std::bind ( std::bind (
&HTTPClientImp::handleRequest, &HTTPClientImp::handleRequest,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error)); std::placeholders::_1));
} }
else else
{ {
@@ -354,8 +353,8 @@ public:
mRequest, mRequest,
std::bind (&HTTPClientImp::handleWrite, std::bind (&HTTPClientImp::handleWrite,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
} }
} }
@@ -379,8 +378,8 @@ public:
"\r\n\r\n", "\r\n\r\n",
std::bind (&HTTPClientImp::handleHeader, std::bind (&HTTPClientImp::handleHeader,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
} }
} }
@@ -430,8 +429,8 @@ public:
boost::asio::transfer_all (), boost::asio::transfer_all (),
std::bind (&HTTPClientImp::handleData, std::bind (&HTTPClientImp::handleData,
shared_from_this (), shared_from_this (),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred)); std::placeholders::_2));
} }
} }

View File

@@ -23,7 +23,6 @@
#include <ripple/overlay/impl/PeerImp.h> #include <ripple/overlay/impl/PeerImp.h>
#include <ripple/overlay/impl/Tuning.h> #include <ripple/overlay/impl/Tuning.h>
#include <ripple/json/json_reader.h> #include <ripple/json/json_reader.h>
#include <beast/core/to_string.hpp>
#include <beast/http/read.hpp> #include <beast/http/read.hpp>
#include <beast/http/write.hpp> #include <beast/http/write.hpp>
@@ -81,7 +80,7 @@ ConnectAttempt::run()
error_code ec; error_code ec;
stream_.next_layer().async_connect (remote_endpoint_, stream_.next_layer().async_connect (remote_endpoint_,
strand_.wrap (std::bind (&ConnectAttempt::onConnect, strand_.wrap (std::bind (&ConnectAttempt::onConnect,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -138,7 +137,7 @@ ConnectAttempt::setTimer()
timer_.async_wait(strand_.wrap(std::bind( timer_.async_wait(strand_.wrap(std::bind(
&ConnectAttempt::onTimer, shared_from_this(), &ConnectAttempt::onTimer, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void
@@ -186,7 +185,7 @@ ConnectAttempt::onConnect (error_code ec)
stream_.set_verify_mode (boost::asio::ssl::verify_none); stream_.set_verify_mode (boost::asio::ssl::verify_none);
stream_.async_handshake (boost::asio::ssl::stream_base::client, stream_.async_handshake (boost::asio::ssl::stream_base::client,
strand_.wrap (std::bind (&ConnectAttempt::onHandshake, strand_.wrap (std::bind (&ConnectAttempt::onHandshake,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
} }
void void
@@ -221,12 +220,12 @@ ConnectAttempt::onHandshake (error_code ec)
overlay_.setup().public_ip, overlay_.setup().public_ip,
beast::IPAddressConversion::from_asio(remote_endpoint_), beast::IPAddressConversion::from_asio(remote_endpoint_),
app_); app_);
appendHello (req_.fields, hello); appendHello (req_, hello);
setTimer(); setTimer();
beast::http::async_write(stream_, req_, beast::http::async_write(stream_, req_,
strand_.wrap (std::bind (&ConnectAttempt::onWrite, strand_.wrap (std::bind (&ConnectAttempt::onWrite,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
} }
void void
@@ -241,7 +240,7 @@ ConnectAttempt::onWrite (error_code ec)
return fail("onWrite", ec); return fail("onWrite", ec);
beast::http::async_read(stream_, read_buf_, response_, beast::http::async_read(stream_, read_buf_, response_,
strand_.wrap(std::bind(&ConnectAttempt::onRead, strand_.wrap(std::bind(&ConnectAttempt::onRead,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
} }
void void
@@ -260,7 +259,7 @@ ConnectAttempt::onRead (error_code ec)
setTimer(); setTimer();
return stream_.async_shutdown(strand_.wrap(std::bind( return stream_.async_shutdown(strand_.wrap(std::bind(
&ConnectAttempt::onShutdown, shared_from_this(), &ConnectAttempt::onShutdown, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
if(ec) if(ec)
return fail("onRead", ec); return fail("onRead", ec);
@@ -290,26 +289,32 @@ ConnectAttempt::makeRequest (bool crawl,
request_type request_type
{ {
request_type m; request_type m;
m.method = "GET"; m.method(beast::http::verb::get);
m.url = "/"; m.target("/");
m.version = 11; m.version = 11;
m.fields.insert ("User-Agent", BuildInfo::getFullVersionString()); m.insert ("User-Agent", BuildInfo::getFullVersionString());
m.fields.insert ("Upgrade", "RTXP/1.2"); m.insert ("Upgrade", "RTXP/1.2");
//std::string("RTXP/") + to_string (BuildInfo::getCurrentProtocol())); //std::string("RTXP/") + to_string (BuildInfo::getCurrentProtocol()));
m.fields.insert ("Connection", "Upgrade"); m.insert ("Connection", "Upgrade");
m.fields.insert ("Connect-As", "Peer"); m.insert ("Connect-As", "Peer");
m.fields.insert ("Crawl", crawl ? "public" : "private"); m.insert ("Crawl", crawl ? "public" : "private");
return m; return m;
} }
void void
ConnectAttempt::processResponse() ConnectAttempt::processResponse()
{ {
if (response_.status == 503) if (response_.result() == beast::http::status::service_unavailable)
{ {
Json::Value json; Json::Value json;
Json::Reader r; Json::Reader r;
auto const success = r.parse(beast::to_string(response_.body.data()), json); std::string s;
s.reserve(boost::asio::buffer_size(response_.body.data()));
for(auto const& buffer : response_.body.data())
s.append(
boost::asio::buffer_cast<char const*>(buffer),
boost::asio::buffer_size(buffer));
auto const success = r.parse(s, json);
if (success) if (success)
{ {
if (json.isObject() && json.isMember("peer-ips")) if (json.isObject() && json.isMember("peer-ips"))
@@ -339,11 +344,11 @@ ConnectAttempt::processResponse()
if (! OverlayImpl::isPeerUpgrade(response_)) if (! OverlayImpl::isPeerUpgrade(response_))
{ {
JLOG(journal_.info()) << JLOG(journal_.info()) <<
"HTTP Response: " << response_.status << " " << response_.reason; "HTTP Response: " << response_.result() << " " << response_.reason();
return close(); return close();
} }
auto hello = parseHello (false, response_.fields, journal_); auto hello = parseHello (false, response_, journal_);
if(! hello) if(! hello)
return fail("processResponse: Bad TMHello"); return fail("processResponse: Bad TMHello");

View File

@@ -31,11 +31,10 @@
#include <ripple/beast/asio/ssl_bundle.h> #include <ripple/beast/asio/ssl_bundle.h>
#include <ripple/beast/net/IPAddressConversion.h> #include <ripple/beast/net/IPAddressConversion.h>
#include <ripple/beast/utility/WrappedSink.h> #include <ripple/beast/utility/WrappedSink.h>
#include <beast/core/placeholders.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/http/message.hpp>
#include <beast/http/empty_body.hpp> #include <beast/http/empty_body.hpp>
#include <beast/http/parser_v1.hpp> #include <beast/http/string_body.hpp>
#include <beast/http/parser.hpp>
#include <boost/asio/basic_waitable_timer.hpp> #include <boost/asio/basic_waitable_timer.hpp>
#include <boost/asio/buffers_iterator.hpp> #include <boost/asio/buffers_iterator.hpp>
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
@@ -59,7 +58,7 @@ private:
beast::http::request<beast::http::empty_body>; beast::http::request<beast::http::empty_body>;
using response_type = using response_type =
beast::http::response<beast::http::streambuf_body>; beast::http::response<beast::http::dynamic_body>;
Application& app_; Application& app_;
std::uint32_t const id_; std::uint32_t const id_;
@@ -72,7 +71,7 @@ private:
std::unique_ptr<beast::asio::ssl_bundle> ssl_bundle_; std::unique_ptr<beast::asio::ssl_bundle> ssl_bundle_;
beast::asio::ssl_bundle::socket_type& socket_; beast::asio::ssl_bundle::socket_type& socket_;
beast::asio::ssl_bundle::stream_type& stream_; beast::asio::ssl_bundle::stream_type& stream_;
beast::streambuf read_buf_; beast::multi_buffer read_buf_;
response_type response_; response_type response_;
PeerFinder::Slot::ptr slot_; PeerFinder::Slot::ptr slot_;
request_type req_; request_type req_;

View File

@@ -102,7 +102,7 @@ OverlayImpl::Timer::run()
timer_.expires_from_now (std::chrono::seconds(1)); timer_.expires_from_now (std::chrono::seconds(1));
timer_.async_wait(overlay_.strand_.wrap( timer_.async_wait(overlay_.strand_.wrap(
std::bind(&Timer::on_timer, shared_from_this(), std::bind(&Timer::on_timer, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void
@@ -127,7 +127,7 @@ OverlayImpl::Timer::on_timer (error_code ec)
timer_.expires_from_now (std::chrono::seconds(1)); timer_.expires_from_now (std::chrono::seconds(1));
timer_.async_wait(overlay_.strand_.wrap(std::bind( timer_.async_wait(overlay_.strand_.wrap(std::bind(
&Timer::on_timer, shared_from_this(), &Timer::on_timer, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -219,7 +219,7 @@ OverlayImpl::onHandoff (std::unique_ptr <beast::asio::ssl_bundle>&& ssl_bundle,
{ {
auto const types = beast::rfc2616::split_commas( auto const types = beast::rfc2616::split_commas(
request.fields["Connect-As"]); request["Connect-As"]);
if (std::find_if(types.begin(), types.end(), if (std::find_if(types.begin(), types.end(),
[](std::string const& s) [](std::string const& s)
{ {
@@ -229,12 +229,12 @@ OverlayImpl::onHandoff (std::unique_ptr <beast::asio::ssl_bundle>&& ssl_bundle,
handoff.moved = false; handoff.moved = false;
handoff.response = makeRedirectResponse(slot, request, handoff.response = makeRedirectResponse(slot, request,
remote_endpoint.address()); remote_endpoint.address());
handoff.keep_alive = is_keep_alive(request); handoff.keep_alive = beast::rfc2616::is_keep_alive(request);
return handoff; return handoff;
} }
} }
auto hello = parseHello (true, request.fields, journal); auto hello = parseHello (true, request, journal);
if(! hello) if(! hello)
{ {
m_peerFinder->on_closed(slot); m_peerFinder->on_closed(slot);
@@ -285,7 +285,7 @@ OverlayImpl::onHandoff (std::unique_ptr <beast::asio::ssl_bundle>&& ssl_bundle,
handoff.moved = false; handoff.moved = false;
handoff.response = makeRedirectResponse(slot, request, handoff.response = makeRedirectResponse(slot, request,
remote_endpoint.address()); remote_endpoint.address());
handoff.keep_alive = is_keep_alive(request); handoff.keep_alive = beast::rfc2616::is_keep_alive(request);
return handoff; return handoff;
} }
@@ -313,27 +313,39 @@ OverlayImpl::onHandoff (std::unique_ptr <beast::asio::ssl_bundle>&& ssl_bundle,
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class Fields>
static
bool
is_upgrade(beast::http::header<true, Fields> const& req)
{
if(req.version < 11)
return false;
if(req.method() != beast::http::verb::get)
return false;
if(! beast::http::token_list{req["Connection"]}.exists("upgrade"))
return false;
return true;
}
template<class Fields>
static
bool
is_upgrade(beast::http::header<false, Fields> const& req)
{
if(req.version < 11)
return false;
if(! beast::http::token_list{req["Connection"]}.exists("upgrade"))
return false;
return true;
}
bool bool
OverlayImpl::isPeerUpgrade(http_request_type const& request) OverlayImpl::isPeerUpgrade(http_request_type const& request)
{ {
if (! is_upgrade(request)) if (! is_upgrade(request))
return false; return false;
auto const versions = parse_ProtocolVersions( auto const versions = parse_ProtocolVersions(
request.fields["Upgrade"]); request["Upgrade"]);
if (versions.size() == 0)
return false;
return true;
}
bool
OverlayImpl::isPeerUpgrade(http_response_type const& response)
{
if (! is_upgrade(response))
return false;
if(response.status != 101)
return false;
auto const versions = parse_ProtocolVersions(
response.fields["Upgrade"]);
if (versions.size() == 0) if (versions.size() == 0)
return false; return false;
return true; return true;
@@ -353,11 +365,11 @@ OverlayImpl::makeRedirectResponse (PeerFinder::Slot::ptr const& slot,
{ {
beast::http::response<json_body> msg; beast::http::response<json_body> msg;
msg.version = request.version; msg.version = request.version;
msg.status = 503; msg.result(beast::http::status::service_unavailable);
msg.reason = "Service Unavailable"; msg.insert("Server", BuildInfo::getFullVersionString());
msg.fields.insert("Server", BuildInfo::getFullVersionString()); msg.insert("Remote-Address", remote_address);
msg.fields.insert("Remote-Address", remote_address.to_string()); msg.insert("Content-Type", "application/json");
msg.fields.insert("Content-Type", "application/json"); msg.insert(beast::http::field::connection, "close");
msg.body = Json::objectValue; msg.body = Json::objectValue;
{ {
auto const result = m_peerFinder->redirect(slot); auto const result = m_peerFinder->redirect(slot);
@@ -365,7 +377,7 @@ OverlayImpl::makeRedirectResponse (PeerFinder::Slot::ptr const& slot,
for (auto const& _ : m_peerFinder->redirect(slot)) for (auto const& _ : m_peerFinder->redirect(slot))
ips.append(_.address.to_string()); ips.append(_.address.to_string());
} }
prepare(msg, beast::http::connection::close); msg.prepare();
return std::make_shared<SimpleWriter>(msg); return std::make_shared<SimpleWriter>(msg);
} }
@@ -377,12 +389,12 @@ OverlayImpl::makeErrorResponse (PeerFinder::Slot::ptr const& slot,
{ {
beast::http::response<beast::http::string_body> msg; beast::http::response<beast::http::string_body> msg;
msg.version = request.version; msg.version = request.version;
msg.status = 400; msg.result(beast::http::status::bad_request);
msg.reason = "Bad Request"; msg.insert("Server", BuildInfo::getFullVersionString());
msg.fields.insert("Server", BuildInfo::getFullVersionString()); msg.insert("Remote-Address", remote_address.to_string());
msg.fields.insert("Remote-Address", remote_address.to_string()); msg.insert(beast::http::field::connection, "close");
msg.body = text; msg.body = text;
prepare(msg, beast::http::connection::close); msg.prepare();
return std::make_shared<SimpleWriter>(msg); return std::make_shared<SimpleWriter>(msg);
} }
@@ -827,17 +839,17 @@ bool
OverlayImpl::processRequest (http_request_type const& req, OverlayImpl::processRequest (http_request_type const& req,
Handoff& handoff) Handoff& handoff)
{ {
if (req.url != "/crawl") if (req.target() != "/crawl")
return false; return false;
beast::http::response<json_body> msg; beast::http::response<json_body> msg;
msg.version = req.version; msg.version = req.version;
msg.status = 200; msg.result(beast::http::status::ok);
msg.reason = "OK"; msg.insert("Server", BuildInfo::getFullVersionString());
msg.fields.insert("Server", BuildInfo::getFullVersionString()); msg.insert("Content-Type", "application/json");
msg.fields.insert("Content-Type", "application/json"); msg.insert("Connection", "close");
msg.body["overlay"] = crawl(); msg.body["overlay"] = crawl();
prepare(msg, beast::http::connection::close); msg.prepare();
handoff.response = std::make_shared<SimpleWriter>(msg); handoff.response = std::make_shared<SimpleWriter>(msg);
return true; return true;
} }

View File

@@ -248,9 +248,21 @@ public:
bool bool
isPeerUpgrade (http_request_type const& request); isPeerUpgrade (http_request_type const& request);
template<class Body>
static static
bool bool
isPeerUpgrade (http_response_type const& response); isPeerUpgrade (beast::http::response<Body> const& response)
{
if (! is_upgrade(response))
return false;
if(response.result() != beast::http::status::switching_protocols)
return false;
auto const versions = parse_ProtocolVersions(
response["Upgrade"]);
if (versions.size() == 0)
return false;
return true;
}
static static
std::string std::string

View File

@@ -47,6 +47,7 @@
#include <ripple/protocol/JsonFields.h> #include <ripple/protocol/JsonFields.h>
#include <ripple/beast/core/SemanticVersion.h> #include <ripple/beast/core/SemanticVersion.h>
#include <ripple/beast/utility/weak_fn.h> #include <ripple/beast/utility/weak_fn.h>
#include <beast/core/ostream.hpp>
#include <beast/http/write.hpp> #include <beast/http/write.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/asio/io_service.hpp> #include <boost/asio/io_service.hpp>
@@ -91,7 +92,7 @@ PeerImp::PeerImp (Application& app, id_t id, endpoint_type remote_endpoint,
, fee_ (Resource::feeLightPeer) , fee_ (Resource::feeLightPeer)
, slot_ (slot) , slot_ (slot)
, request_(std::move(request)) , request_(std::move(request))
, headers_(request_.fields) , headers_(request_)
{ {
} }
@@ -225,8 +226,8 @@ PeerImp::send (Message::pointer const& m)
boost::asio::async_write (stream_, boost::asio::buffer( boost::asio::async_write (stream_, boost::asio::buffer(
send_queue_.front()->getBuffer()), strand_.wrap(std::bind( send_queue_.front()->getBuffer()), strand_.wrap(std::bind(
&PeerImp::onWriteMessage, shared_from_this(), &PeerImp::onWriteMessage, shared_from_this(),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
} }
void void
@@ -248,7 +249,7 @@ PeerImp::crawl() const
auto const iter = headers_.find("Crawl"); auto const iter = headers_.find("Crawl");
if (iter == headers_.end()) if (iter == headers_.end())
return false; return false;
return beast::detail::ci_equal(iter->second, "public"); return beast::detail::ci_equal(iter->value(), "public");
} }
std::string std::string
@@ -482,7 +483,7 @@ PeerImp::gracefulClose()
return; return;
setTimer(); setTimer();
stream_.async_shutdown(strand_.wrap(std::bind(&PeerImp::onShutdown, stream_.async_shutdown(strand_.wrap(std::bind(&PeerImp::onShutdown,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
} }
void void
@@ -498,7 +499,7 @@ PeerImp::setTimer()
return; return;
} }
timer_.async_wait(strand_.wrap(std::bind(&PeerImp::onTimer, timer_.async_wait(strand_.wrap(std::bind(&PeerImp::onTimer,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
} }
// convenience for ignoring the error code // convenience for ignoring the error code
@@ -609,9 +610,9 @@ void PeerImp::doAccept()
// TODO Apply headers to connection state. // TODO Apply headers to connection state.
beast::write (write_buffer_, makeResponse( beast::ostream(write_buffer_) << makeResponse(
! overlay_.peerFinder().config().peerPrivate, ! overlay_.peerFinder().config().peerPrivate,
request_, remote_address_, *sharedValue)); request_, remote_address_, *sharedValue);
auto const protocol = BuildInfo::make_protocol(hello_.protoversion()); auto const protocol = BuildInfo::make_protocol(hello_.protoversion());
JLOG(journal_.info()) << "Protocol: " << to_string(protocol); JLOG(journal_.info()) << "Protocol: " << to_string(protocol);
@@ -657,17 +658,16 @@ PeerImp::makeResponse (bool crawl,
uint256 const& sharedValue) uint256 const& sharedValue)
{ {
http_response_type resp; http_response_type resp;
resp.status = 101; resp.result(beast::http::status::switching_protocols);
resp.reason = "Switching Protocols";
resp.version = req.version; resp.version = req.version;
resp.fields.insert("Connection", "Upgrade"); resp.insert("Connection", "Upgrade");
resp.fields.insert("Upgrade", "RTXP/1.2"); resp.insert("Upgrade", "RTXP/1.2");
resp.fields.insert("Connect-AS", "Peer"); resp.insert("Connect-As", "Peer");
resp.fields.insert("Server", BuildInfo::getFullVersionString()); resp.insert("Server", BuildInfo::getFullVersionString());
resp.fields.insert("Crawl", crawl ? "public" : "private"); resp.insert("Crawl", crawl ? "public" : "private");
protocol::TMHello hello = buildHello(sharedValue, protocol::TMHello hello = buildHello(sharedValue,
overlay_.setup().public_ip, remote, app_); overlay_.setup().public_ip, remote, app_);
appendHello(resp.fields, hello); appendHello(resp, hello);
return resp; return resp;
} }
@@ -696,8 +696,8 @@ PeerImp::onWriteResponse (error_code ec, std::size_t bytes_transferred)
stream_.async_write_some (write_buffer_.data(), stream_.async_write_some (write_buffer_.data(),
strand_.wrap (std::bind (&PeerImp::onWriteResponse, strand_.wrap (std::bind (&PeerImp::onWriteResponse,
shared_from_this(), beast::asio::placeholders::error, shared_from_this(), std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -771,8 +771,8 @@ PeerImp::onReadMessage (error_code ec, std::size_t bytes_transferred)
// Timeout on writes only // Timeout on writes only
stream_.async_read_some (read_buffer_.prepare (Tuning::readBufferBytes), stream_.async_read_some (read_buffer_.prepare (Tuning::readBufferBytes),
strand_.wrap (std::bind (&PeerImp::onReadMessage, strand_.wrap (std::bind (&PeerImp::onReadMessage,
shared_from_this(), beast::asio::placeholders::error, shared_from_this(), std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
} }
void void
@@ -801,15 +801,15 @@ PeerImp::onWriteMessage (error_code ec, std::size_t bytes_transferred)
return boost::asio::async_write (stream_, boost::asio::buffer( return boost::asio::async_write (stream_, boost::asio::buffer(
send_queue_.front()->getBuffer()), strand_.wrap(std::bind( send_queue_.front()->getBuffer()), strand_.wrap(std::bind(
&PeerImp::onWriteMessage, shared_from_this(), &PeerImp::onWriteMessage, shared_from_this(),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
} }
if (gracefulClose_) if (gracefulClose_)
{ {
return stream_.async_shutdown(strand_.wrap(std::bind( return stream_.async_shutdown(strand_.wrap(std::bind(
&PeerImp::onShutdown, shared_from_this(), &PeerImp::onShutdown, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
} }

View File

@@ -35,11 +35,10 @@
#include <ripple/protocol/STValidation.h> #include <ripple/protocol/STValidation.h>
#include <ripple/beast/core/ByteOrder.h> #include <ripple/beast/core/ByteOrder.h>
#include <ripple/beast/net/IPAddressConversion.h> #include <ripple/beast/net/IPAddressConversion.h>
#include <beast/core/placeholders.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/core/streambuf.hpp>
#include <ripple/beast/asio/ssl_bundle.h> #include <ripple/beast/asio/ssl_bundle.h>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
#include <beast/http/parser_v1.hpp> #include <beast/http/parser.hpp>
#include <ripple/beast/utility/WrappedSink.h> #include <ripple/beast/utility/WrappedSink.h>
#include <cstdint> #include <cstdint>
#include <deque> #include <deque>
@@ -151,11 +150,11 @@ private:
Resource::Consumer usage_; Resource::Consumer usage_;
Resource::Charge fee_; Resource::Charge fee_;
PeerFinder::Slot::ptr slot_; PeerFinder::Slot::ptr slot_;
beast::streambuf read_buffer_; beast::multi_buffer read_buffer_;
http_request_type request_; http_request_type request_;
http_response_type response_; http_response_type response_;
beast::http::fields const& headers_; beast::http::fields const& headers_;
beast::streambuf write_buffer_; beast::multi_buffer write_buffer_;
std::queue<Message::pointer> send_queue_; std::queue<Message::pointer> send_queue_;
bool gracefulClose_ = false; bool gracefulClose_ = false;
int large_sendq_ = 0; int large_sendq_ = 0;
@@ -502,7 +501,7 @@ PeerImp::PeerImp (Application& app, std::unique_ptr<beast::asio::ssl_bundle>&& s
, fee_ (Resource::feeLightPeer) , fee_ (Resource::feeLightPeer)
, slot_ (std::move(slot)) , slot_ (std::move(slot))
, response_(std::move(response)) , response_(std::move(response))
, headers_(response_.fields) , headers_(response_)
{ {
read_buffer_.commit (boost::asio::buffer_copy(read_buffer_.prepare( read_buffer_.commit (boost::asio::buffer_copy(read_buffer_.prepare(
boost::asio::buffer_size(buffers)), buffers)); boost::asio::buffer_size(buffers)), buffers));

View File

@@ -22,7 +22,6 @@
#include <ripple/overlay/PeerSet.h> #include <ripple/overlay/PeerSet.h>
#include <ripple/core/JobQueue.h> #include <ripple/core/JobQueue.h>
#include <ripple/overlay/Overlay.h> #include <ripple/overlay/Overlay.h>
#include <beast/core/placeholders.hpp>
namespace ripple { namespace ripple {

View File

@@ -233,7 +233,7 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
auto const iter = h.find ("Upgrade"); auto const iter = h.find ("Upgrade");
if (iter == h.end()) if (iter == h.end())
return boost::none; return boost::none;
auto const versions = parse_ProtocolVersions(iter->second); auto const versions = parse_ProtocolVersions(iter->value().to_string());
if (versions.empty()) if (versions.empty())
return boost::none; return boost::none;
hello.set_protoversion( hello.set_protoversion(
@@ -250,10 +250,10 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
if (iter == h.end()) if (iter == h.end())
return boost::none; return boost::none;
auto const pk = parseBase58<PublicKey>( auto const pk = parseBase58<PublicKey>(
TokenType::TOKEN_NODE_PUBLIC, iter->second); TokenType::TOKEN_NODE_PUBLIC, iter->value().to_string());
if (!pk) if (!pk)
return boost::none; return boost::none;
hello.set_nodepublic (iter->second); hello.set_nodepublic (iter->value().to_string());
} }
{ {
@@ -262,14 +262,14 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
if (iter == h.end()) if (iter == h.end())
return boost::none; return boost::none;
// TODO Security Review // TODO Security Review
hello.set_nodeproof (beast::detail::base64_decode (iter->second)); hello.set_nodeproof (beast::detail::base64_decode (iter->value().to_string()));
} }
{ {
auto const iter = h.find (request ? auto const iter = h.find (request ?
"User-Agent" : "Server"); "User-Agent" : "Server");
if (iter != h.end()) if (iter != h.end())
hello.set_fullversion (iter->second); hello.set_fullversion (iter->value().to_string());
} }
{ {
@@ -277,7 +277,7 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
if (iter != h.end()) if (iter != h.end())
{ {
std::uint64_t nettime; std::uint64_t nettime;
if (! beast::lexicalCastChecked(nettime, iter->second)) if (! beast::lexicalCastChecked(nettime, iter->value().to_string()))
return boost::none; return boost::none;
hello.set_nettime (nettime); hello.set_nettime (nettime);
} }
@@ -288,7 +288,7 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
if (iter != h.end()) if (iter != h.end())
{ {
LedgerIndex ledgerIndex; LedgerIndex ledgerIndex;
if (! beast::lexicalCastChecked(ledgerIndex, iter->second)) if (! beast::lexicalCastChecked(ledgerIndex, iter->value().to_string()))
return boost::none; return boost::none;
hello.set_ledgerindex (ledgerIndex); hello.set_ledgerindex (ledgerIndex);
} }
@@ -297,13 +297,13 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
{ {
auto const iter = h.find ("Closed-Ledger"); auto const iter = h.find ("Closed-Ledger");
if (iter != h.end()) if (iter != h.end())
hello.set_ledgerclosed (beast::detail::base64_decode (iter->second)); hello.set_ledgerclosed (beast::detail::base64_decode (iter->value().to_string()));
} }
{ {
auto const iter = h.find ("Previous-Ledger"); auto const iter = h.find ("Previous-Ledger");
if (iter != h.end()) if (iter != h.end())
hello.set_ledgerprevious (beast::detail::base64_decode (iter->second)); hello.set_ledgerprevious (beast::detail::base64_decode (iter->value().to_string()));
} }
{ {
@@ -313,7 +313,7 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
bool valid; bool valid;
beast::IP::Address address; beast::IP::Address address;
std::tie (address, valid) = std::tie (address, valid) =
beast::IP::Address::from_string (iter->second); beast::IP::Address::from_string (iter->value().to_string());
if (!valid) if (!valid)
return boost::none; return boost::none;
if (address.is_v4()) if (address.is_v4())
@@ -328,7 +328,7 @@ parseHello (bool request, beast::http::fields const& h, beast::Journal journal)
bool valid; bool valid;
beast::IP::Address address; beast::IP::Address address;
std::tie (address, valid) = std::tie (address, valid) =
beast::IP::Address::from_string (iter->second); beast::IP::Address::from_string (iter->value().to_string());
if (!valid) if (!valid)
return boost::none; return boost::none;
if (address.is_v4()) if (address.is_v4())

View File

@@ -21,7 +21,6 @@
#define RIPPLE_PEERFINDER_CHECKER_H_INCLUDED #define RIPPLE_PEERFINDER_CHECKER_H_INCLUDED
#include <ripple/beast/net/IPAddressConversion.h> #include <ripple/beast/net/IPAddressConversion.h>
#include <beast/core/placeholders.hpp>
#include <boost/asio/detail/handler_invoke_helpers.hpp> #include <boost/asio/detail/handler_invoke_helpers.hpp>
#include <boost/asio/io_service.hpp> #include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
@@ -215,7 +214,7 @@ Checker<Protocol>::async_connect (
} }
op->socket_.async_connect (beast::IPAddressConversion::to_asio_endpoint ( op->socket_.async_connect (beast::IPAddressConversion::to_asio_endpoint (
endpoint), std::bind (&basic_async_op::operator(), op, endpoint), std::bind (&basic_async_op::operator(), op,
beast::asio::placeholders::error)); std::placeholders::_1));
} }
template <class Protocol> template <class Protocol>

View File

@@ -52,26 +52,16 @@
namespace ripple { namespace ripple {
// Returns `true` if the HTTP request is a Websockets Upgrade
// http://en.wikipedia.org/wiki/HTTP/1.1_Upgrade_header#Use_with_WebSockets
static
bool
isWebsocketUpgrade(
http_request_type const& request)
{
if (is_upgrade(request))
return beast::detail::ci_equal(
request.fields["Upgrade"], "websocket");
return false;
}
static static
bool bool
isStatusRequest( isStatusRequest(
http_request_type const& request) http_request_type const& request)
{ {
return request.version >= 11 && request.url == "/" && return
request.body.size() == 0 && request.method == "GET"; request.version >= 11 &&
request.target() == "/" &&
request.body.size() == 0 &&
request.method() == beast::http::verb::get;
} }
static static
@@ -83,12 +73,12 @@ unauthorizedResponse(
Handoff handoff; Handoff handoff;
response<string_body> msg; response<string_body> msg;
msg.version = request.version; msg.version = request.version;
msg.status = 401; msg.result(beast::http::status::unauthorized);
msg.reason = "Unauthorized"; msg.insert("Server", BuildInfo::getFullVersionString());
msg.fields.insert("Server", BuildInfo::getFullVersionString()); msg.insert("Content-Type", "text/html");
msg.fields.insert("Content-Type", "text/html"); msg.insert("Connection", "close");
msg.body = "Invalid protocol."; msg.body = "Invalid protocol.";
prepare(msg, beast::http::connection::close); msg.prepare();
handoff.response = std::make_shared<SimpleWriter>(msg); handoff.response = std::make_shared<SimpleWriter>(msg);
return handoff; return handoff;
} }
@@ -189,7 +179,7 @@ ServerHandlerImp::onHandoff (Session& session,
(session.port().protocol.count("wss") > 0) || (session.port().protocol.count("wss") > 0) ||
(session.port().protocol.count("wss2") > 0); (session.port().protocol.count("wss2") > 0);
if(isWebsocketUpgrade(request)) if(beast::websocket::is_upgrade(request))
{ {
if(is_ws) if(is_ws)
{ {
@@ -229,7 +219,7 @@ ServerHandlerImp::onHandoff (Session& session,
boost::asio::ip::tcp::endpoint remote_address) -> boost::asio::ip::tcp::endpoint remote_address) ->
Handoff Handoff
{ {
if(isWebsocketUpgrade(request)) if(beast::websocket::is_upgrade(request))
{ {
if (session.port().protocol.count("ws2") > 0 || if (session.port().protocol.count("ws2") > 0 ||
session.port().protocol.count("ws") > 0) session.port().protocol.count("ws") > 0)
@@ -275,10 +265,10 @@ build_map(beast::http::fields const& h)
std::map <std::string, std::string> c; std::map <std::string, std::string> c;
for (auto const& e : h) for (auto const& e : h)
{ {
auto key (e.first); auto key (e.name_string().to_string());
// TODO Replace with safe C++14 version // TODO Replace with safe C++14 version
std::transform (key.begin(), key.end(), key.begin(), ::tolower); std::transform (key.begin(), key.end(), key.begin(), ::tolower);
c [key] = e.second; c [key] = e.value().to_string();
} }
return c; return c;
} }
@@ -312,7 +302,7 @@ ServerHandlerImp::onRequest (Session& session)
// Check user/password authorization // Check user/password authorization
if (! authorized ( if (! authorized (
session.port(), build_map(session.request().fields))) session.port(), build_map(session.request())))
{ {
HTTPReply (403, "Forbidden", makeOutput (session), app_.journal ("RPC")); HTTPReply (403, "Forbidden", makeOutput (session), app_.journal ("RPC"));
session.close (true); session.close (true);
@@ -342,7 +332,7 @@ ServerHandlerImp::onWSMessage(
jvResult[jss::type] = jss::error; jvResult[jss::type] = jss::error;
jvResult[jss::error] = "jsonInvalid"; jvResult[jss::error] = "jsonInvalid";
jvResult[jss::value] = buffers_to_string(buffers); jvResult[jss::value] = buffers_to_string(buffers);
beast::streambuf sb; beast::multi_buffer sb;
Json::stream(jvResult, Json::stream(jvResult,
[&sb](auto const p, auto const n) [&sb](auto const p, auto const n)
{ {
@@ -368,7 +358,7 @@ ServerHandlerImp::onWSMessage(
this->processSession(session, c, jv); this->processSession(session, c, jv);
auto const s = to_string(jr); auto const s = to_string(jr);
auto const n = s.length(); auto const n = s.length();
beast::streambuf sb(n); beast::multi_buffer sb(n);
sb.commit(boost::asio::buffer_copy( sb.commit(boost::asio::buffer_copy(
sb.prepare(n), boost::asio::buffer(s.c_str(), n))); sb.prepare(n), boost::asio::buffer(s.c_str(), n)));
session->send(std::make_shared< session->send(std::make_shared<
@@ -511,23 +501,23 @@ ServerHandlerImp::processSession (std::shared_ptr<Session> const& session,
[&] [&]
{ {
auto const iter = auto const iter =
session->request().fields.find( session->request().find(
"X-Forwarded-For"); "X-Forwarded-For");
if(iter != session->request().fields.end()) if(iter != session->request().end())
return iter->second; return iter->value().to_string();
return std::string{}; return std::string{};
}(), }(),
[&] [&]
{ {
auto const iter = auto const iter =
session->request().fields.find( session->request().find(
"X-User"); "X-User");
if(iter != session->request().fields.end()) if(iter != session->request().end())
return iter->second; return iter->value().to_string();
return std::string{}; return std::string{};
}()); }());
if(is_keep_alive(session->request())) if(beast::rfc2616::is_keep_alive(session->request()))
session->complete(); session->complete();
else else
session->close (true); session->close (true);
@@ -738,8 +728,7 @@ ServerHandlerImp::statusResponse(
std::string reason; std::string reason;
if (app_.serverOkay(reason)) if (app_.serverOkay(reason))
{ {
msg.status = 200; msg.result(beast::http::status::ok);
msg.reason = "OK";
msg.body = "<!DOCTYPE html><html><head><title>" + systemName() + msg.body = "<!DOCTYPE html><html><head><title>" + systemName() +
" Test page for rippled</title></head><body><h1>" + " Test page for rippled</title></head><body><h1>" +
systemName() + " Test</h1><p>This page shows rippled http(s) " systemName() + " Test</h1><p>This page shows rippled http(s) "
@@ -747,15 +736,15 @@ ServerHandlerImp::statusResponse(
} }
else else
{ {
msg.status = 500; msg.result(beast::http::status::internal_server_error);
msg.reason = "Internal Server Error";
msg.body = "<HTML><BODY>Server cannot accept clients: " + msg.body = "<HTML><BODY>Server cannot accept clients: " +
reason + "</BODY></HTML>"; reason + "</BODY></HTML>";
} }
msg.version = request.version; msg.version = request.version;
msg.fields.insert("Server", BuildInfo::getFullVersionString()); msg.insert("Server", BuildInfo::getFullVersionString());
msg.fields.insert("Content-Type", "text/html"); msg.insert("Content-Type", "text/html");
prepare(msg, beast::http::connection::close); msg.insert("Connection", "close");
msg.prepare();
handoff.response = std::make_shared<SimpleWriter>(msg); handoff.response = std::make_shared<SimpleWriter>(msg);
return handoff; return handoff;
} }

View File

@@ -42,17 +42,17 @@ public:
: InfoSub(source) : InfoSub(source)
, ws_(ws) , ws_(ws)
{ {
auto const& h = ws->request().fields; auto const& h = ws->request();
auto it = h.find("X-User"); auto it = h.find("X-User");
if (it != h.end() && if (it != h.end() &&
isIdentified( isIdentified(
ws->port(), beast::IPAddressConversion::from_asio( ws->port(), beast::IPAddressConversion::from_asio(
ws->remote_endpoint()).address(), it->second)) ws->remote_endpoint()).address(), it->value().to_string()))
{ {
user_ = it->second; user_ = it->value().to_string();
it = h.find("X-Forwarded-For"); it = h.find("X-Forwarded-For");
if (it != h.end()) if (it != h.end())
fwdfor_ = it->second; fwdfor_ = it->value().to_string();
} }
} }
@@ -74,7 +74,7 @@ public:
auto sp = ws_.lock(); auto sp = ws_.lock();
if(! sp) if(! sp)
return; return;
beast::streambuf sb; beast::multi_buffer sb;
stream(jv, stream(jv,
[&](void const* data, std::size_t n) [&](void const* data, std::size_t n)
{ {

View File

@@ -21,7 +21,7 @@
#define RIPPLE_RPC_JSON_BODY_H #define RIPPLE_RPC_JSON_BODY_H
#include <ripple/json/json_value.h> #include <ripple/json/json_value.h>
#include <beast/core/streambuf.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
namespace ripple { namespace ripple {
@@ -31,21 +31,28 @@ struct json_body
{ {
using value_type = Json::Value; using value_type = Json::Value;
class writer class reader
{ {
beast::streambuf sb_; using dynamic_buffer_type = beast::multi_buffer;
dynamic_buffer_type buffer_;
public: public:
using const_buffers_type =
typename dynamic_buffer_type::const_buffers_type;
using is_deferred = std::false_type;
template<bool isRequest, class Fields> template<bool isRequest, class Fields>
explicit explicit
writer(beast::http::message< reader(beast::http::message<
isRequest, json_body, Fields> const& m) noexcept isRequest, json_body, Fields> const& m)
{ {
stream(m.body, stream(m.body,
[&](void const* data, std::size_t n) [&](void const* data, std::size_t n)
{ {
sb_.commit(boost::asio::buffer_copy( buffer_.commit(boost::asio::buffer_copy(
sb_.prepare(n), boost::asio::buffer(data, n))); buffer_.prepare(n), boost::asio::buffer(data, n)));
}); });
} }
@@ -54,18 +61,15 @@ struct json_body
{ {
} }
std::uint64_t boost::optional<std::pair<const_buffers_type, bool>>
content_length() const noexcept get(beast::error_code& ec)
{ {
return sb_.size(); return {{buffer_.data(), false}};
} }
template<class Writef> void
bool finish(beast::error_code&)
write(beast::error_code&, Writef&& wf) noexcept
{ {
wf(sb_.data());
return true;
} }
}; };
}; };

View File

@@ -22,16 +22,16 @@
#include <ripple/server/Writer.h> #include <ripple/server/Writer.h>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
#include <beast/http/streambuf_body.hpp> #include <beast/http/dynamic_body.hpp>
#include <memory> #include <memory>
namespace ripple { namespace ripple {
using http_request_type = using http_request_type =
beast::http::request<beast::http::streambuf_body>; beast::http::request<beast::http::dynamic_body>;
using http_response_type = using http_response_type =
beast::http::response<beast::http::streambuf_body>; beast::http::response<beast::http::dynamic_body>;
/** Used to indicate the result of a server connection handoff. */ /** Used to indicate the result of a server connection handoff. */
struct Handoff struct Handoff

View File

@@ -21,8 +21,8 @@
#define RIPPLE_SERVER_SIMPLEWRITER_H_INCLUDED #define RIPPLE_SERVER_SIMPLEWRITER_H_INCLUDED
#include <ripple/server/Writer.h> #include <ripple/server/Writer.h>
#include <beast/core/streambuf.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/core/write_dynabuf.hpp> #include <beast/core/ostream.hpp>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
#include <beast/http/write.hpp> #include <beast/http/write.hpp>
#include <utility> #include <utility>
@@ -32,7 +32,7 @@ namespace ripple {
/// Deprecated: Writer that serializes a HTTP/1 message /// Deprecated: Writer that serializes a HTTP/1 message
class SimpleWriter : public Writer class SimpleWriter : public Writer
{ {
beast::streambuf sb_; beast::multi_buffer sb_;
public: public:
template<bool isRequest, class Body, class Headers> template<bool isRequest, class Body, class Headers>
@@ -40,7 +40,7 @@ public:
SimpleWriter(beast::http::message< SimpleWriter(beast::http::message<
isRequest, Body, Headers> const& msg) isRequest, Body, Headers> const& msg)
{ {
beast::write(sb_, msg); beast::ostream(sb_) << msg;
} }
bool bool

View File

@@ -23,7 +23,7 @@
#include <ripple/server/Handoff.h> #include <ripple/server/Handoff.h>
#include <ripple/server/Port.h> #include <ripple/server/Port.h>
#include <ripple/server/Writer.h> #include <ripple/server/Writer.h>
#include <beast/core/prepare_buffers.hpp> #include <beast/core/buffer_prefix.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
#include <boost/logic/tribool.hpp> #include <boost/logic/tribool.hpp>
@@ -98,7 +98,7 @@ public:
n_ = sb_.size(); n_ = sb_.size();
done = true; done = true;
} }
auto const pb = beast::prepare_buffers(n_, sb_.data()); auto const pb = beast::buffer_prefix(n_, sb_.data());
std::vector<boost::asio::const_buffer> vb ( std::vector<boost::asio::const_buffer> vb (
std::distance(pb.begin(), pb.end())); std::distance(pb.begin(), pb.end()));
std::copy(pb.begin(), pb.end(), std::back_inserter(vb)); std::copy(pb.begin(), pb.end(), std::back_inserter(vb));

View File

@@ -24,12 +24,11 @@
#include <ripple/server/Session.h> #include <ripple/server/Session.h>
#include <ripple/server/impl/io_list.h> #include <ripple/server/impl/io_list.h>
#include <ripple/beast/net/IPAddressConversion.h> #include <ripple/beast/net/IPAddressConversion.h>
#include <beast/core/placeholders.hpp>
#include <ripple/beast/asio/ssl_error.h> // for is_short_read? #include <ripple/beast/asio/ssl_error.h> // for is_short_read?
#include <beast/http/read.hpp> #include <beast/http/read.hpp>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
#include <beast/http/parser_v1.hpp> #include <beast/http/parser.hpp>
#include <beast/http/streambuf_body.hpp> #include <beast/http/dynamic_body.hpp>
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp> #include <boost/asio/ssl/stream.hpp>
#include <boost/asio/streambuf.hpp> #include <boost/asio/streambuf.hpp>
@@ -288,7 +287,7 @@ start_timer()
return fail(ec, "start_timer"); return fail(ec, "start_timer");
timer_.async_wait(strand_.wrap(std::bind( timer_.async_wait(strand_.wrap(std::bind(
&BaseHTTPPeer<Handler, Impl>::on_timer, impl().shared_from_this(), &BaseHTTPPeer<Handler, Impl>::on_timer, impl().shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
// Convenience for discarding the error code // Convenience for discarding the error code
@@ -328,6 +327,8 @@ do_read(yield_context do_yield)
beast::http::async_read(impl().stream_, beast::http::async_read(impl().stream_,
read_buf_, message_, do_yield[ec]); read_buf_, message_, do_yield[ec]);
cancel_timer(); cancel_timer();
if(ec == beast::http::error::end_of_stream)
return do_close();
if(ec) if(ec)
return fail(ec, "http::read"); return fail(ec, "http::read");
do_request(); do_request();
@@ -361,8 +362,8 @@ on_write(error_code const& ec,
using namespace beast::asio; using namespace beast::asio;
return boost::asio::async_write(impl().stream_, v, return boost::asio::async_write(impl().stream_, v,
strand_.wrap(std::bind(&BaseHTTPPeer::on_write, strand_.wrap(std::bind(&BaseHTTPPeer::on_write,
impl().shared_from_this(), placeholders::error, impl().shared_from_this(), std::placeholders::_1,
placeholders::bytes_transferred))); std::placeholders::_2)));
} }
if(! complete_) if(! complete_)
return; return;

View File

@@ -25,7 +25,7 @@
#include <ripple/beast/utility/rngfill.h> #include <ripple/beast/utility/rngfill.h>
#include <ripple/crypto/csprng.h> #include <ripple/crypto/csprng.h>
#include <beast/websocket.hpp> #include <beast/websocket.hpp>
#include <beast/core/streambuf.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
#include <cassert> #include <cassert>
@@ -48,9 +48,8 @@ private:
friend class BasePeer<Handler, Impl>; friend class BasePeer<Handler, Impl>;
http_request_type request_; http_request_type request_;
beast::websocket::opcode op_; beast::multi_buffer rb_;
beast::streambuf rb_; beast::multi_buffer wb_;
beast::streambuf wb_;
std::list<std::shared_ptr<WSMsg>> wq_; std::list<std::shared_ptr<WSMsg>> wq_;
bool do_close_ = false; bool do_close_ = false;
beast::websocket::close_reason cr_; beast::websocket::close_reason cr_;
@@ -105,25 +104,6 @@ public:
complete() override; complete() override;
protected: protected:
struct identity
{
template<class Body, class Headers>
void
operator()(beast::http::message<true, Body, Headers>& req) const
{
req.fields.replace("User-Agent",
BuildInfo::getFullVersionString());
}
template<class Body, class Headers>
void
operator()(beast::http::message<false, Body, Headers>& resp) const
{
resp.fields.replace("Server",
BuildInfo::getFullVersionString());
}
};
Impl& Impl&
impl() impl()
{ {
@@ -199,17 +179,21 @@ run()
if(! strand_.running_in_this_thread()) if(! strand_.running_in_this_thread())
return strand_.post(std::bind( return strand_.post(std::bind(
&BaseWSPeer::run, impl().shared_from_this())); &BaseWSPeer::run, impl().shared_from_this()));
impl().ws_.set_option(beast::websocket::decorate(identity{}));
impl().ws_.set_option(port().pmd_options); impl().ws_.set_option(port().pmd_options);
impl().ws_.set_option(beast::websocket::ping_callback{ impl().ws_.ping_callback(
std::bind(&BaseWSPeer::on_ping_pong, this, std::bind(&BaseWSPeer::on_ping_pong, this,
std::placeholders::_1, std::placeholders::_2)}); std::placeholders::_1, std::placeholders::_2));
using namespace beast::asio; using namespace beast::asio;
start_timer(); start_timer();
close_on_timer_ = true; close_on_timer_ = true;
impl().ws_.async_accept(request_, strand_.wrap(std::bind( impl().ws_.async_accept_ex(request_,
&BaseWSPeer::on_ws_handshake, impl().shared_from_this(), [](auto & res)
placeholders::error))); {
res.replace(beast::http::field::server,
BuildInfo::getFullVersionString());
},
strand_.wrap(std::bind(&BaseWSPeer::on_ws_handshake,
impl().shared_from_this(), std::placeholders::_1)));
} }
template<class Handler, class Impl> template<class Handler, class Impl>
@@ -227,7 +211,7 @@ send(std::shared_ptr<WSMsg> w)
{ {
JLOG(this->j_.info()) << JLOG(this->j_.info()) <<
"closing slow client"; "closing slow client";
cr_.code = static_cast<beast::websocket::close_code::value>(4000); cr_.code = static_cast<beast::websocket::close_code>(4000);
cr_.reason = "Client is too slow."; cr_.reason = "Client is too slow.";
wq_.erase(std::next(wq_.begin()), wq_.end()); wq_.erase(std::next(wq_.begin()), wq_.end());
close(); close();
@@ -250,7 +234,7 @@ close()
if(wq_.empty()) if(wq_.empty())
impl().ws_.async_close({}, strand_.wrap(std::bind( impl().ws_.async_close({}, strand_.wrap(std::bind(
&BaseWSPeer::on_close, impl().shared_from_this(), &BaseWSPeer::on_close, impl().shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
template<class Handler, class Impl> template<class Handler, class Impl>
@@ -305,12 +289,12 @@ on_write(error_code const& ec)
impl().ws_.async_write_frame( impl().ws_.async_write_frame(
result.first, result.second, strand_.wrap(std::bind( result.first, result.second, strand_.wrap(std::bind(
&BaseWSPeer::on_write, impl().shared_from_this(), &BaseWSPeer::on_write, impl().shared_from_this(),
placeholders::error))); std::placeholders::_1)));
else else
impl().ws_.async_write_frame( impl().ws_.async_write_frame(
result.first, result.second, strand_.wrap(std::bind( result.first, result.second, strand_.wrap(std::bind(
&BaseWSPeer::on_write_fin, impl().shared_from_this(), &BaseWSPeer::on_write_fin, impl().shared_from_this(),
placeholders::error))); std::placeholders::_1)));
} }
template<class Handler, class Impl> template<class Handler, class Impl>
@@ -324,7 +308,7 @@ on_write_fin(error_code const& ec)
if(do_close_) if(do_close_)
impl().ws_.async_close(cr_, strand_.wrap(std::bind( impl().ws_.async_close(cr_, strand_.wrap(std::bind(
&BaseWSPeer::on_close, impl().shared_from_this(), &BaseWSPeer::on_close, impl().shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
else if(! wq_.empty()) else if(! wq_.empty())
on_write({}); on_write({});
} }
@@ -338,9 +322,9 @@ do_read()
return strand_.post(std::bind( return strand_.post(std::bind(
&BaseWSPeer::do_read, impl().shared_from_this())); &BaseWSPeer::do_read, impl().shared_from_this()));
using namespace beast::asio; using namespace beast::asio;
impl().ws_.async_read(op_, rb_, strand_.wrap( impl().ws_.async_read(rb_, strand_.wrap(
std::bind(&BaseWSPeer::on_read, std::bind(&BaseWSPeer::on_read,
impl().shared_from_this(), placeholders::error))); impl().shared_from_this(), std::placeholders::_1)));
} }
template<class Handler, class Impl> template<class Handler, class Impl>
@@ -385,7 +369,7 @@ start_timer()
return fail(ec, "start_timer"); return fail(ec, "start_timer");
timer_.async_wait(strand_.wrap(std::bind( timer_.async_wait(strand_.wrap(std::bind(
&BaseWSPeer<Handler, Impl>::on_timer, impl().shared_from_this(), &BaseWSPeer<Handler, Impl>::on_timer, impl().shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
// Convenience for discarding the error code // Convenience for discarding the error code

View File

@@ -26,8 +26,7 @@
#include <ripple/server/impl/PlainHTTPPeer.h> #include <ripple/server/impl/PlainHTTPPeer.h>
#include <ripple/server/impl/SSLHTTPPeer.h> #include <ripple/server/impl/SSLHTTPPeer.h>
#include <ripple/beast/asio/ssl_bundle.h> #include <ripple/beast/asio/ssl_bundle.h>
#include <beast/core/placeholders.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/core/streambuf.hpp>
#include <boost/asio/basic_waitable_timer.hpp> #include <boost/asio/basic_waitable_timer.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/asio/io_service.hpp> #include <boost/asio/io_service.hpp>
@@ -225,7 +224,7 @@ do_detect(boost::asio::yield_context do_yield)
{ {
bool ssl; bool ssl;
error_code ec; error_code ec;
beast::streambuf buf(16); beast::multi_buffer buf(16);
timer_.expires_from_now(std::chrono::seconds(15)); timer_.expires_from_now(std::chrono::seconds(15));
std::tie(ec, ssl) = detect_ssl(socket_, buf, do_yield); std::tie(ec, ssl) = detect_ssl(socket_, buf, do_yield);
error_code unused; error_code unused;

View File

@@ -20,6 +20,7 @@
#ifndef RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED #ifndef RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED
#define RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED #define RIPPLE_SERVER_PLAINHTTPPEER_H_INCLUDED
#include <ripple/beast/rfc2616.h>
#include <ripple/server/impl/BaseHTTPPeer.h> #include <ripple/server/impl/BaseHTTPPeer.h>
#include <ripple/server/impl/PlainWSPeer.h> #include <ripple/server/impl/PlainWSPeer.h>
#include <memory> #include <memory>
@@ -132,7 +133,7 @@ do_request()
} }
// Perform half-close when Connection: close and not SSL // Perform half-close when Connection: close and not SSL
if (! is_keep_alive(this->message_)) if (! beast::rfc2616::is_keep_alive(this->message_))
stream_.shutdown(socket_type::shutdown_receive, ec); stream_.shutdown(socket_type::shutdown_receive, ec);
if (ec) if (ec)
return this->fail(ec, "request"); return this->fail(ec, "request");

View File

@@ -23,7 +23,6 @@
#include <ripple/server/impl/BaseHTTPPeer.h> #include <ripple/server/impl/BaseHTTPPeer.h>
#include <ripple/server/WSSession.h> #include <ripple/server/WSSession.h>
#include <ripple/beast/asio/ssl_bundle.h> #include <ripple/beast/asio/ssl_bundle.h>
#include <beast/core/placeholders.hpp>
#include <beast/websocket/ssl.hpp> #include <beast/websocket/ssl.hpp>
#include <memory> #include <memory>

View File

@@ -17,7 +17,6 @@
*/ */
//============================================================================== //==============================================================================
#include <beast/core/placeholders.hpp>
#include <beast/core/detail/base64.hpp> #include <beast/core/detail/base64.hpp>
#include <beast/http.hpp> #include <beast/http.hpp>
#include <ripple/app/misc/ValidatorSite.h> #include <ripple/app/misc/ValidatorSite.h>
@@ -91,7 +90,7 @@ public:
acceptor_.listen(boost::asio::socket_base::max_connections); acceptor_.listen(boost::asio::socket_base::max_connections);
acceptor_.async_accept(sock_, acceptor_.async_accept(sock_,
std::bind(&http_sync_server::on_accept, this, std::bind(&http_sync_server::on_accept, this,
beast::asio::placeholders::error)); std::placeholders::_1));
} }
~http_sync_server() ~http_sync_server()
@@ -134,14 +133,14 @@ private:
std::thread{lambda{++id_, *this, std::move(sock_)}}.detach(); std::thread{lambda{++id_, *this, std::move(sock_)}}.detach();
acceptor_.async_accept(sock_, acceptor_.async_accept(sock_,
std::bind(&http_sync_server::on_accept, this, std::bind(&http_sync_server::on_accept, this,
beast::asio::placeholders::error)); std::placeholders::_1));
} }
void void
do_peer(int id, socket_type&& sock0) do_peer(int id, socket_type&& sock0)
{ {
socket_type sock(std::move(sock0)); socket_type sock(std::move(sock0));
beast::streambuf sb; beast::multi_buffer sb;
error_code ec; error_code ec;
for(;;) for(;;)
{ {
@@ -149,44 +148,41 @@ private:
beast::http::read(sock, sb, req, ec); beast::http::read(sock, sb, req, ec);
if(ec) if(ec)
break; break;
auto path = req.url; auto path = req.target().to_string();
if(path != "/validators") if(path != "/validators")
{ {
resp_type res; resp_type res;
res.status = 404; res.result(beast::http::status::not_found);
res.reason = "Not Found";
res.version = req.version; res.version = req.version;
res.fields.insert("Server", "http_sync_server"); res.insert("Server", "http_sync_server");
res.fields.insert("Content-Type", "text/html"); res.insert("Content-Type", "text/html");
res.body = "The file '" + path + "' was not found"; res.body = "The file '" + path + "' was not found";
prepare(res); res.prepare();
write(sock, res, ec); write(sock, res, ec);
if(ec) if(ec)
break; break;
} }
resp_type res; resp_type res;
res.status = 200; res.result(beast::http::status::ok);
res.reason = "OK";
res.version = req.version; res.version = req.version;
res.fields.insert("Server", "http_sync_server"); res.insert("Server", "http_sync_server");
res.fields.insert("Content-Type", "application/json"); res.insert("Content-Type", "application/json");
res.body = list_; res.body = list_;
try try
{ {
prepare(res); res.prepare();
} }
catch(std::exception const& e) catch(std::exception const& e)
{ {
res = {}; res = {};
res.status = 500; res.result(beast::http::status::internal_server_error);
res.reason = "Internal Error";
res.version = req.version; res.version = req.version;
res.fields.insert("Server", "http_sync_server"); res.insert("Server", "http_sync_server");
res.fields.insert("Content-Type", "text/html"); res.insert("Content-Type", "text/html");
res.body = res.body =
std::string{"An internal error occurred"} + e.what(); std::string{"An internal error occurred"} + e.what();
prepare(res); res.prepare();
} }
write(sock, res, ec); write(sock, res, ec);
if(ec) if(ec)

View File

@@ -44,7 +44,7 @@
#include <ripple/protocol/STAmount.h> #include <ripple/protocol/STAmount.h>
#include <ripple/protocol/STObject.h> #include <ripple/protocol/STObject.h>
#include <ripple/protocol/STTx.h> #include <ripple/protocol/STTx.h>
#include <beast/core/detail/is_call_possible.hpp> #include <beast/core/detail/type_traits.hpp>
#include <ripple/beast/unit_test.h> #include <ripple/beast/unit_test.h>
#include <functional> #include <functional>
#include <string> #include <string>
@@ -642,7 +642,7 @@ protected:
FN const&... fN) FN const&... fN)
{ {
maybe_invoke(stx, f, maybe_invoke(stx, f,
beast::detail::is_call_possible<F, beast::detail::is_invocable<F,
void(Env&, STTx const&)>()); void(Env&, STTx const&)>());
invoke(stx, fN...); invoke(stx, fN...);
} }
@@ -676,7 +676,7 @@ protected:
FN const&... fN) FN const&... fN)
{ {
maybe_invoke(jt, f, maybe_invoke(jt, f,
beast::detail::is_call_possible<F, beast::detail::is_invocable<F,
void(Env&, JTx&)>()); void(Env&, JTx&)>());
invoke(jt, fN...); invoke(jt, fN...);
} }

View File

@@ -23,7 +23,7 @@
#include <ripple/protocol/JsonFields.h> #include <ripple/protocol/JsonFields.h>
#include <ripple/server/Port.h> #include <ripple/server/Port.h>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
#include <beast/http/streambuf_body.hpp> #include <beast/http/dynamic_body.hpp>
#include <beast/http/string_body.hpp> #include <beast/http/string_body.hpp>
#include <beast/http/read.hpp> #include <beast/http/read.hpp>
#include <beast/http/write.hpp> #include <beast/http/write.hpp>
@@ -74,8 +74,8 @@ class JSONRPCClient : public AbstractClient
boost::asio::ip::tcp::endpoint ep_; boost::asio::ip::tcp::endpoint ep_;
boost::asio::io_service ios_; boost::asio::io_service ios_;
boost::asio::ip::tcp::socket stream_; boost::asio::ip::tcp::socket stream_;
beast::streambuf bin_; beast::multi_buffer bin_;
beast::streambuf bout_; beast::multi_buffer bout_;
unsigned rpc_version_; unsigned rpc_version_;
public: public:
@@ -109,12 +109,11 @@ public:
using namespace std::string_literals; using namespace std::string_literals;
request<string_body> req; request<string_body> req;
req.method = "POST"; req.method(beast::http::verb::post);
req.url = "/"; req.target("/");
req.version = 11; req.version = 11;
req.fields.insert("Content-Type", "application/json; charset=UTF-8"); req.insert("Content-Type", "application/json; charset=UTF-8");
req.fields.insert("Host", req.insert("Host", ep_);
ep_.address().to_string() + ":" + std::to_string(ep_.port()));
{ {
Json::Value jr; Json::Value jr;
jr[jss::method] = cmd; jr[jss::method] = cmd;
@@ -131,10 +130,10 @@ public:
} }
req.body = to_string(jr); req.body = to_string(jr);
} }
prepare(req); req.prepare();
write(stream_, req); write(stream_, req);
response<streambuf_body> res; response<dynamic_body> res;
read(stream_, bin_, res); read(stream_, bin_, res);
Json::Reader jr; Json::Reader jr;

View File

@@ -24,8 +24,7 @@
#include <ripple/json/to_string.h> #include <ripple/json/to_string.h>
#include <ripple/protocol/JsonFields.h> #include <ripple/protocol/JsonFields.h>
#include <ripple/server/Port.h> #include <ripple/server/Port.h>
#include <beast/core/placeholders.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/websocket.hpp> #include <beast/websocket.hpp>
#include <condition_variable> #include <condition_variable>
@@ -95,8 +94,7 @@ class WSClientImpl : public WSClient
std::thread thread_; std::thread thread_;
boost::asio::ip::tcp::socket stream_; boost::asio::ip::tcp::socket stream_;
beast::websocket::stream<boost::asio::ip::tcp::socket&> ws_; beast::websocket::stream<boost::asio::ip::tcp::socket&> ws_;
beast::websocket::opcode op_; beast::multi_buffer rb_;
beast::streambuf rb_;
bool peerClosed_ = false; bool peerClosed_ = false;
@@ -139,9 +137,9 @@ public:
stream_.connect(ep); stream_.connect(ep);
ws_.handshake(ep.address().to_string() + ws_.handshake(ep.address().to_string() +
":" + std::to_string(ep.port()), "/"); ":" + std::to_string(ep.port()), "/");
ws_.async_read(op_, rb_, ws_.async_read(rb_,
strand_.wrap(std::bind(&WSClientImpl::on_read_msg, strand_.wrap(std::bind(&WSClientImpl::on_read_msg,
this, beast::asio::placeholders::error))); this, std::placeholders::_1)));
} }
catch(std::exception&) catch(std::exception&)
{ {
@@ -278,9 +276,9 @@ private:
msgs_.push_front(m); msgs_.push_front(m);
cv_.notify_all(); cv_.notify_all();
} }
ws_.async_read(op_, rb_, strand_.wrap( ws_.async_read(rb_, strand_.wrap(
std::bind(&WSClientImpl::on_read_msg, std::bind(&WSClientImpl::on_read_msg,
this, beast::asio::placeholders::error))); this, std::placeholders::_1)));
} }
// Called when the read op terminates // Called when the read op terminates

View File

@@ -21,7 +21,6 @@
#include <ripple/basics/make_SSLContext.h> #include <ripple/basics/make_SSLContext.h>
#include <ripple/beast/core/CurrentThreadName.h> #include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/beast/unit_test.h> #include <ripple/beast/unit_test.h>
#include <beast/core/placeholders.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@@ -211,7 +210,7 @@ private:
{ {
acceptor_.async_accept(socket_, strand_.wrap(std::bind( acceptor_.async_accept(socket_, strand_.wrap(std::bind(
&Acceptor::on_accept, shared_from_this(), &Acceptor::on_accept, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void
@@ -237,7 +236,7 @@ private:
p->run(); p->run();
acceptor_.async_accept(socket_, strand_.wrap(std::bind( acceptor_.async_accept(socket_, strand_.wrap(std::bind(
&Acceptor::on_accept, shared_from_this(), &Acceptor::on_accept, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
}; };
@@ -281,10 +280,10 @@ private:
{ {
timer_.expires_from_now(std::chrono::seconds(3)); timer_.expires_from_now(std::chrono::seconds(3));
timer_.async_wait(strand_.wrap(std::bind(&Connection::on_timer, timer_.async_wait(strand_.wrap(std::bind(&Connection::on_timer,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
stream_.async_handshake(stream_type::server, strand_.wrap( stream_.async_handshake(stream_type::server, strand_.wrap(
std::bind(&Connection::on_handshake, shared_from_this(), std::bind(&Connection::on_handshake, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void
@@ -319,8 +318,8 @@ private:
#if 1 #if 1
boost::asio::async_read_until(stream_, buf_, "\n", strand_.wrap( boost::asio::async_read_until(stream_, buf_, "\n", strand_.wrap(
std::bind(&Connection::on_read, shared_from_this(), std::bind(&Connection::on_read, shared_from_this(),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
#else #else
close(); close();
#endif #endif
@@ -334,7 +333,7 @@ private:
server_.test_.log << "[server] read: EOF" << std::endl; server_.test_.log << "[server] read: EOF" << std::endl;
return stream_.async_shutdown(strand_.wrap(std::bind( return stream_.async_shutdown(strand_.wrap(std::bind(
&Connection::on_shutdown, shared_from_this(), &Connection::on_shutdown, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
if (ec) if (ec)
return fail("read", ec); return fail("read", ec);
@@ -344,8 +343,8 @@ private:
write(buf_, "BYE\n"); write(buf_, "BYE\n");
boost::asio::async_write(stream_, buf_.data(), strand_.wrap( boost::asio::async_write(stream_, buf_.data(), strand_.wrap(
std::bind(&Connection::on_write, shared_from_this(), std::bind(&Connection::on_write, shared_from_this(),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
} }
void void
@@ -356,7 +355,7 @@ private:
return fail("write", ec); return fail("write", ec);
stream_.async_shutdown(strand_.wrap(std::bind( stream_.async_shutdown(strand_.wrap(std::bind(
&Connection::on_shutdown, shared_from_this(), &Connection::on_shutdown, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void
@@ -432,10 +431,10 @@ private:
{ {
timer_.expires_from_now(std::chrono::seconds(3)); timer_.expires_from_now(std::chrono::seconds(3));
timer_.async_wait(strand_.wrap(std::bind(&Connection::on_timer, timer_.async_wait(strand_.wrap(std::bind(&Connection::on_timer,
shared_from_this(), beast::asio::placeholders::error))); shared_from_this(), std::placeholders::_1)));
socket_.async_connect(endpoint(), strand_.wrap(std::bind( socket_.async_connect(endpoint(), strand_.wrap(std::bind(
&Connection::on_connect, shared_from_this(), &Connection::on_connect, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void
@@ -469,7 +468,7 @@ private:
return fail("connect", ec); return fail("connect", ec);
stream_.async_handshake(stream_type::client, strand_.wrap( stream_.async_handshake(stream_type::client, strand_.wrap(
std::bind(&Connection::on_handshake, shared_from_this(), std::bind(&Connection::on_handshake, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void
@@ -482,12 +481,12 @@ private:
#if 1 #if 1
boost::asio::async_write(stream_, buf_.data(), strand_.wrap( boost::asio::async_write(stream_, buf_.data(), strand_.wrap(
std::bind(&Connection::on_write, shared_from_this(), std::bind(&Connection::on_write, shared_from_this(),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
#else #else
stream_.async_shutdown(strand_.wrap(std::bind( stream_.async_shutdown(strand_.wrap(std::bind(
&Connection::on_shutdown, shared_from_this(), &Connection::on_shutdown, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
#endif #endif
} }
@@ -500,12 +499,12 @@ private:
#if 1 #if 1
boost::asio::async_read_until(stream_, buf_, "\n", strand_.wrap( boost::asio::async_read_until(stream_, buf_, "\n", strand_.wrap(
std::bind(&Connection::on_read, shared_from_this(), std::bind(&Connection::on_read, shared_from_this(),
beast::asio::placeholders::error, std::placeholders::_1,
beast::asio::placeholders::bytes_transferred))); std::placeholders::_2)));
#else #else
stream_.async_shutdown(strand_.wrap(std::bind( stream_.async_shutdown(strand_.wrap(std::bind(
&Connection::on_shutdown, shared_from_this(), &Connection::on_shutdown, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
#endif #endif
} }
@@ -517,7 +516,7 @@ private:
buf_.commit(bytes_transferred); buf_.commit(bytes_transferred);
stream_.async_shutdown(strand_.wrap(std::bind( stream_.async_shutdown(strand_.wrap(std::bind(
&Connection::on_shutdown, shared_from_this(), &Connection::on_shutdown, shared_from_this(),
beast::asio::placeholders::error))); std::placeholders::_1)));
} }
void void

View File

@@ -25,7 +25,6 @@
#include <test/jtx/WSClient.h> #include <test/jtx/WSClient.h>
#include <test/jtx/JSONRPCClient.h> #include <test/jtx/JSONRPCClient.h>
#include <ripple/core/DeadlineTimer.h> #include <ripple/core/DeadlineTimer.h>
#include <beast/core/to_string.hpp>
#include <beast/http.hpp> #include <beast/http.hpp>
#include <beast/test/yield_to.hpp> #include <beast/test/yield_to.hpp>
#include <beast/websocket/detail/mask.hpp> #include <beast/websocket/detail/mask.hpp>
@@ -86,17 +85,18 @@ class ServerStatus_test :
using namespace beast::http; using namespace beast::http;
request<string_body> req; request<string_body> req;
req.url = "/"; req.target("/");
req.version = 11; req.version = 11;
req.fields.insert("Host", host + ":" + to_string(port)); req.insert("Host", host + ":" + to_string(port));
req.fields.insert("User-Agent", "test"); req.insert("User-Agent", "test");
req.method = "GET"; req.method(beast::http::verb::get);
req.fields.insert("Upgrade", "websocket"); req.insert("Upgrade", "websocket");
beast::websocket::detail::maskgen maskgen; beast::websocket::detail::maskgen maskgen;
std::string key = beast::websocket::detail::make_sec_ws_key(maskgen); beast::websocket::detail::sec_ws_key_type key;
req.fields.insert("Sec-WebSocket-Key", key); beast::websocket::detail::make_sec_ws_key(key, maskgen);
req.fields.insert("Sec-WebSocket-Version", "13"); req.insert("Sec-WebSocket-Key", key);
prepare(req, connection::upgrade); req.insert("Sec-WebSocket-Version", "13");
req.insert(beast::http::field::connection, "upgrade");
return req; return req;
} }
@@ -109,21 +109,21 @@ class ServerStatus_test :
using namespace beast::http; using namespace beast::http;
request<string_body> req; request<string_body> req;
req.url = "/"; req.target("/");
req.version = 11; req.version = 11;
req.fields.insert("Host", host + ":" + to_string(port)); req.insert("Host", host + ":" + to_string(port));
req.fields.insert("User-Agent", "test"); req.insert("User-Agent", "test");
if(body.empty()) if(body.empty())
{ {
req.method = "GET"; req.method(beast::http::verb::get);
} }
else else
{ {
req.method = "POST"; req.method(beast::http::verb::post);
req.fields.insert("Content-Type", "application/json; charset=UTF-8"); req.insert("Content-Type", "application/json; charset=UTF-8");
req.body = body; req.body = body;
} }
prepare(req); req.prepare();
return req; return req;
} }
@@ -142,7 +142,7 @@ class ServerStatus_test :
using namespace beast::http; using namespace beast::http;
io_service& ios = get_io_service(); io_service& ios = get_io_service();
ip::tcp::resolver r{ios}; ip::tcp::resolver r{ios};
beast::streambuf sb; beast::multi_buffer sb;
auto it = auto it =
r.async_resolve( r.async_resolve(
@@ -287,7 +287,7 @@ class ServerStatus_test :
doWSRequest(env, yield, false, resp, ec); doWSRequest(env, yield, false, resp, ec);
if(! BEAST_EXPECTS(! ec, ec.message())) if(! BEAST_EXPECTS(! ec, ec.message()))
return; return;
BEAST_EXPECT(resp.status == 401); BEAST_EXPECT(resp.result() == beast::http::status::unauthorized);
} }
//secure request //secure request
@@ -297,7 +297,7 @@ class ServerStatus_test :
doWSRequest(env, yield, true, resp, ec); doWSRequest(env, yield, true, resp, ec);
if(! BEAST_EXPECTS(! ec, ec.message())) if(! BEAST_EXPECTS(! ec, ec.message()))
return; return;
BEAST_EXPECT(resp.status == 401); BEAST_EXPECT(resp.result() == beast::http::status::unauthorized);
} }
} }
@@ -320,7 +320,7 @@ class ServerStatus_test :
doHTTPRequest(env, yield, false, resp, ec); doHTTPRequest(env, yield, false, resp, ec);
if(! BEAST_EXPECTS(! ec, ec.message())) if(! BEAST_EXPECTS(! ec, ec.message()))
return; return;
BEAST_EXPECT(resp.status == 200); BEAST_EXPECT(resp.result() == beast::http::status::ok);
} }
//secure request //secure request
@@ -330,7 +330,7 @@ class ServerStatus_test :
doHTTPRequest(env, yield, true, resp, ec); doHTTPRequest(env, yield, true, resp, ec);
if(! BEAST_EXPECTS(! ec, ec.message())) if(! BEAST_EXPECTS(! ec, ec.message()))
return; return;
BEAST_EXPECT(resp.status == 200); BEAST_EXPECT(resp.result() == beast::http::status::ok);
} }
}; };
@@ -362,7 +362,7 @@ class ServerStatus_test :
io_service& ios = get_io_service(); io_service& ios = get_io_service();
ip::tcp::resolver r{ios}; ip::tcp::resolver r{ios};
beast::streambuf sb; beast::multi_buffer sb;
auto it = auto it =
r.async_resolve( r.async_resolve(

View File

@@ -19,6 +19,7 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/basics/make_SSLContext.h> #include <ripple/basics/make_SSLContext.h>
#include <ripple/beast/rfc2616.h>
#include <ripple/server/Server.h> #include <ripple/server/Server.h>
#include <ripple/server/Session.h> #include <ripple/server/Session.h>
#include <ripple/beast/unit_test.h> #include <ripple/beast/unit_test.h>
@@ -123,7 +124,7 @@ public:
onRequest (Session& session) onRequest (Session& session)
{ {
session.write (std::string ("Hello, world!\n")); session.write (std::string ("Hello, world!\n"));
if (is_keep_alive(session.request())) if (beast::rfc2616::is_keep_alive(session.request()))
session.complete(); session.complete();
else else
session.close (true); session.close (true);