From 6d8b25fdf8b77dcd2763f3f8baa6640816b612c6 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 27 Apr 2016 13:18:47 -0400 Subject: [PATCH] Use new beast::http::message: --- Builds/VisualStudio2015/RippleD.vcxproj | 34 ++++++--- .../VisualStudio2015/RippleD.vcxproj.filters | 60 +++++++++++----- SConstruct | 2 +- src/ripple/beast/deprecated_http.h | 61 ++++++++++------ src/ripple/overlay/impl/ConnectAttempt.cpp | 44 ++++-------- src/ripple/overlay/impl/ConnectAttempt.h | 19 +++-- src/ripple/server/Handoff.h | 4 ++ src/ripple/server/JsonWriter.h | 2 +- src/ripple/server/ServerHandler.h | 5 -- src/ripple/server/SimpleWriter.h | 5 +- src/ripple/server/impl/ServerHandlerImp.cpp | 7 -- src/ripple/test/impl/JSONRPCClient.cpp | 70 +++++++------------ 12 files changed, 168 insertions(+), 145 deletions(-) diff --git a/Builds/VisualStudio2015/RippleD.vcxproj b/Builds/VisualStudio2015/RippleD.vcxproj index 495589d1b0..8d5b61cab1 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj +++ b/Builds/VisualStudio2015/RippleD.vcxproj @@ -301,6 +301,8 @@ + + @@ -309,9 +311,7 @@ - - - + @@ -325,8 +325,6 @@ - - @@ -339,6 +337,8 @@ + + @@ -347,16 +347,22 @@ + + - - + + + + + + @@ -375,6 +381,8 @@ + + @@ -427,11 +435,17 @@ - - - + + + True + + + + + + diff --git a/Builds/VisualStudio2015/RippleD.vcxproj.filters b/Builds/VisualStudio2015/RippleD.vcxproj.filters index 4b9fabe3af..dabb74c7f2 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2015/RippleD.vcxproj.filters @@ -478,11 +478,14 @@ {D2A046B6-8986-92AC-082F-15B3A01B8E40} - - {04C492A5-B8E8-3B1F-4337-A067B40F268A} + + {D5859751-012E-81F5-D121-159FE9A25436} - - {1E761797-5D00-57DD-FEBA-D17E07E1788D} + + {F9ABC269-E86E-1034-C5DE-E9E5B48772DB} + + + {617863D3-DC6A-1DDA-ACA5-7DC92DCEAFB3} {96CE200F-2670-323E-3B37-B90DBC0EDA11} @@ -630,6 +633,9 @@ include\beast\detail\unit_test + + include\beast\detail + include\beast @@ -642,10 +648,7 @@ include\beast\http - - include\beast\http\detail - - + include\beast\http\detail @@ -666,9 +669,6 @@ include\beast\http\impl - - include\beast\http\impl - include\beast\http\impl @@ -687,6 +687,9 @@ include\beast\http + + include\beast\http + include\beast\http @@ -699,21 +702,30 @@ include\beast\http + + include\beast\http + include\beast\http include\beast\http - - include\beast\http - include\beast\http include\beast\impl + + include\beast\impl + + + include\beast\impl + + + include\beast\impl + include\beast\impl @@ -741,6 +753,9 @@ include\beast\websocket\detail + + include\beast\websocket\detail + include\beast\websocket\detail @@ -819,12 +834,21 @@ include\beast\websocket - - src + + include\beast + + + test\http\nodejs-parser - - src\http-parser + + test\http\nodejs-parser + + + test\http + + test\http + ed25519-donna diff --git a/SConstruct b/SConstruct index b3b9f63351..12074d2123 100644 --- a/SConstruct +++ b/SConstruct @@ -1110,7 +1110,7 @@ for tu_style in ['classic', 'unity']: cc_flags = {} object_builder.add_source_files( - 'src/beast/src/beast_http_nodejs_parser.cpp', + 'src/beast/test/http/nodejs_parser.cpp', 'src/ripple/beast/unity/beast_hash_unity.cpp', 'src/ripple/unity/beast.cpp', 'src/ripple/unity/lz4.c', diff --git a/src/ripple/beast/deprecated_http.h b/src/ripple/beast/deprecated_http.h index 215d66da53..27416bf720 100644 --- a/src/ripple/beast/deprecated_http.h +++ b/src/ripple/beast/deprecated_http.h @@ -23,7 +23,9 @@ #include #include #include -#include +#include +#include +#include #include #include #include @@ -340,39 +342,39 @@ write (Streambuf& stream, message const& m) { if (m.request()) { - http::detail::write (stream, to_string(m.method())); - http::detail::write (stream, " "); - http::detail::write (stream, m.url()); - http::detail::write (stream, " HTTP/"); - http::detail::write (stream, std::to_string(m.version().first)); - http::detail::write (stream, "."); - http::detail::write (stream, std::to_string(m.version().second)); + beast::write (stream, to_string(m.method())); + beast::write (stream, " "); + beast::write (stream, m.url()); + beast::write (stream, " HTTP/"); + beast::write (stream, std::to_string(m.version().first)); + beast::write (stream, "."); + beast::write (stream, std::to_string(m.version().second)); } else { - http::detail::write (stream, "HTTP/"); - http::detail::write (stream, std::to_string(m.version().first)); - http::detail::write (stream, "."); - http::detail::write (stream, std::to_string(m.version().second)); - http::detail::write (stream, " "); - http::detail::write (stream, std::to_string(m.status())); - http::detail::write (stream, " "); - http::detail::write (stream, m.reason()); + beast::write (stream, "HTTP/"); + beast::write (stream, std::to_string(m.version().first)); + beast::write (stream, "."); + beast::write (stream, std::to_string(m.version().second)); + beast::write (stream, " "); + beast::write (stream, std::to_string(m.status())); + beast::write (stream, " "); + beast::write (stream, m.reason()); } - http::detail::write (stream, "\r\n"); + beast::write (stream, "\r\n"); write_fields(stream, m.headers); - http::detail::write (stream, "\r\n"); + beast::write (stream, "\r\n"); } //------------------------------------------------------------------------------ class parser - : public beast::http::basic_parser + : public beast::http::nodejs_basic_parser { -// friend class basic_parser; - message& m_; std::function write_body_; + std::string field_; + std::string value_; public: parser(parser&&) = default; @@ -388,7 +390,7 @@ public: */ parser(std::function write_body, message& m, bool request) - : basic_parser(request) + : nodejs_basic_parser(request) , m_(m) , write_body_(std::move(write_body)) { @@ -396,7 +398,7 @@ public: } parser(message& m, body& b, bool request) - : basic_parser(request) + : nodejs_basic_parser(request) , m_(m) { write_body_ = [&b](void const* data, std::size_t size) @@ -407,6 +409,19 @@ public: } //private: + + void flush() + { + if(! value_.empty()) + { + rfc2616::trim_right_in_place(value_); + // VFALCO could std::move + m_.headers.insert(field_, value_); + field_.clear(); + value_.clear(); + } + } + void on_start() { diff --git a/src/ripple/overlay/impl/ConnectAttempt.cpp b/src/ripple/overlay/impl/ConnectAttempt.cpp index 4fb1d6feb0..37d775a1f9 100644 --- a/src/ripple/overlay/impl/ConnectAttempt.cpp +++ b/src/ripple/overlay/impl/ConnectAttempt.cpp @@ -23,6 +23,7 @@ #include #include #include +#include namespace ripple { @@ -218,48 +219,32 @@ ConnectAttempt::onHandshake (error_code ec) if (! sharedValue) return close(); // makeSharedValue logs - beast::deprecated_http::message req = makeRequest( - ! overlay_.peerFinder().config().peerPrivate, - remote_endpoint_.address()); + req_ = makeRequest(! overlay_.peerFinder().config().peerPrivate, + remote_endpoint_.address()); auto const hello = buildHello ( *sharedValue, overlay_.setup().public_ip, beast::IPAddressConversion::from_asio(remote_endpoint_), app_); - appendHello (req.headers, hello); - - beast::deprecated_http::write (write_buf_, req); + appendHello (req_.headers, hello); setTimer(); - stream_.async_write_some (write_buf_.data(), + beast::http::async_write(stream_, req_, strand_.wrap (std::bind (&ConnectAttempt::onWrite, - shared_from_this(), beast::asio::placeholders::error, - beast::asio::placeholders::bytes_transferred))); + shared_from_this(), beast::asio::placeholders::error))); } void -ConnectAttempt::onWrite (error_code ec, std::size_t bytes_transferred) +ConnectAttempt::onWrite (error_code ec) { cancelTimer(); - if(! stream_.next_layer().is_open()) return; if(ec == boost::asio::error::operation_aborted) return; if(ec) return fail("onWrite", ec); - JLOG(journal_.trace()) << - "onWrite: " << bytes_transferred << " bytes"; - - write_buf_.consume (bytes_transferred); - if (write_buf_.size() == 0) - return onRead (error_code(), 0); - - setTimer(); - stream_.async_write_some (write_buf_.data(), - strand_.wrap (std::bind (&ConnectAttempt::onWrite, - shared_from_this(), beast::asio::placeholders::error, - beast::asio::placeholders::bytes_transferred))); + onRead(error_code(), 0); } void @@ -330,14 +315,15 @@ ConnectAttempt::onShutdown (error_code ec) //-------------------------------------------------------------------------- -beast::deprecated_http::message +auto ConnectAttempt::makeRequest (bool crawl, - boost::asio::ip::address const& remote_address) + boost::asio::ip::address const& remote_address) -> + request_type { - beast::deprecated_http::message m; - m.method (beast::http::method_t::http_get); - m.url ("/"); - m.version (1, 1); + request_type m; + m.method = beast::http::method_t::http_get; + m.url = "/"; + m.version = 11; m.headers.insert ("User-Agent", BuildInfo::getFullVersionString()); m.headers.insert ("Upgrade", "RTXP/1.2"); //std::string("RTXP/") + to_string (BuildInfo::getCurrentProtocol())); diff --git a/src/ripple/overlay/impl/ConnectAttempt.h b/src/ripple/overlay/impl/ConnectAttempt.h index b18cd9f280..0814da4144 100644 --- a/src/ripple/overlay/impl/ConnectAttempt.h +++ b/src/ripple/overlay/impl/ConnectAttempt.h @@ -28,13 +28,14 @@ #include #include #include -#include #include -#include -#include -#include #include #include +#include +#include +#include +#include +#include #include #include #include @@ -51,8 +52,12 @@ class ConnectAttempt { private: using error_code = boost::system::error_code; + using endpoint_type = boost::asio::ip::tcp::endpoint; + using request_type = + beast::http::request; + Application& app_; std::uint32_t const id_; beast::WrappedSink sink_; @@ -70,6 +75,7 @@ private: beast::streambuf body_; beast::deprecated_http::parser parser_; PeerFinder::Slot::ptr slot_; + request_type req_; public: ConnectAttempt (Application& app, boost::asio::io_service& io_service, @@ -87,6 +93,7 @@ public: run(); private: + void close(); void fail (std::string const& reason); void fail (std::string const& name, error_code ec); @@ -95,12 +102,12 @@ private: void onTimer (error_code ec); void onConnect (error_code ec); void onHandshake (error_code ec); - void onWrite (error_code ec, std::size_t bytes_transferred); + void onWrite (error_code ec); void onRead (error_code ec, std::size_t bytes_transferred); void onShutdown (error_code ec); static - beast::deprecated_http::message + request_type makeRequest (bool crawl, boost::asio::ip::address const& remote_address); diff --git a/src/ripple/server/Handoff.h b/src/ripple/server/Handoff.h index 5c37b7a85e..45c4dc09a9 100644 --- a/src/ripple/server/Handoff.h +++ b/src/ripple/server/Handoff.h @@ -21,6 +21,7 @@ #define RIPPLE_SERVER_HANDOFF_H_INCLUDED #include +#include #include #include #include @@ -30,6 +31,9 @@ namespace ripple { using http_request_type = beast::http::request; +using http_response_type = + beast::deprecated_http::message; + /** Used to indicate the result of a server connection handoff. */ struct Handoff { diff --git a/src/ripple/server/JsonWriter.h b/src/ripple/server/JsonWriter.h index 805eea550c..8cfe953ecd 100644 --- a/src/ripple/server/JsonWriter.h +++ b/src/ripple/server/JsonWriter.h @@ -155,7 +155,7 @@ make_JsonWriter (beast::deprecated_http::message& m, Json::Value const& json) m.headers.insert("Content-Length", std::to_string(body.size())); m.headers.erase ("Content-Type"); m.headers.insert("Content-Type", "application/json"); - write(prebody, m); + beast::deprecated_http::write(prebody, m); return std::make_shared( std::move(prebody), std::move(body)); } diff --git a/src/ripple/server/ServerHandler.h b/src/ripple/server/ServerHandler.h index 4af6454560..467785c2ad 100644 --- a/src/ripple/server/ServerHandler.h +++ b/src/ripple/server/ServerHandler.h @@ -85,11 +85,6 @@ public: virtual Setup const& setup() const = 0; - - /** Fills in boilerplate HTTP header field values. */ - static - void - appendStandardFields (beast::deprecated_http::message& message); }; //------------------------------------------------------------------------------ diff --git a/src/ripple/server/SimpleWriter.h b/src/ripple/server/SimpleWriter.h index 1a364f5072..c9d51c1c65 100644 --- a/src/ripple/server/SimpleWriter.h +++ b/src/ripple/server/SimpleWriter.h @@ -23,6 +23,7 @@ #include #include #include +#include #include namespace ripple { @@ -96,8 +97,8 @@ private: message_.headers.erase("Content-Length"); message_.headers.insert("Content-Length", std::to_string(body_.size())); - write(streambuf_, message_); - beast::http::detail::write(streambuf_, body_); + beast::deprecated_http::write(streambuf_, message_); + beast::write(streambuf_, body_); } }; diff --git a/src/ripple/server/impl/ServerHandlerImp.cpp b/src/ripple/server/impl/ServerHandlerImp.cpp index d9a9d6a66b..a32ec188a1 100644 --- a/src/ripple/server/impl/ServerHandlerImp.cpp +++ b/src/ripple/server/impl/ServerHandlerImp.cpp @@ -687,13 +687,6 @@ ServerHandlerImp::authorized (Port const& port, //------------------------------------------------------------------------------ -void -ServerHandler::appendStandardFields (beast::deprecated_http::message& message) -{ -} - -//------------------------------------------------------------------------------ - void ServerHandler::Setup::makeContexts() { diff --git a/src/ripple/test/impl/JSONRPCClient.cpp b/src/ripple/test/impl/JSONRPCClient.cpp index c95fe14dec..55f24ecc9f 100644 --- a/src/ripple/test/impl/JSONRPCClient.cpp +++ b/src/ripple/test/impl/JSONRPCClient.cpp @@ -23,8 +23,11 @@ #include #include #include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -69,17 +72,19 @@ class JSONRPCClient : public AbstractClient return s; } + boost::asio::ip::tcp::endpoint ep_; boost::asio::io_service ios_; boost::asio::ip::tcp::socket stream_; - boost::asio::streambuf bin_; + beast::streambuf bin_; beast::streambuf bout_; public: explicit JSONRPCClient(Config const& cfg) - : stream_(ios_) - { - stream_.connect(getEndpoint(cfg)); + : ep_(getEndpoint(cfg)) + , stream_(ios_) + { + stream_.connect(ep_); } ~JSONRPCClient() override @@ -98,7 +103,17 @@ public: invoke(std::string const& cmd, Json::Value const& params) override { - std::string s; + using namespace beast::http; + using namespace boost::asio; + using namespace std::string_literals; + + request req; + req.method = method_t::http_post; + req.url = "/"; + req.version = 11; + req.headers.insert("Content-Type", "application/json; charset=UTF-8"); + req.headers.insert("Host", + ep_.address().to_string() + ":" + std::to_string(ep_.port())); { Json::Value jr; jr["method"] = cmd; @@ -107,47 +122,16 @@ public: Json::Value& ja = jr["params"] = Json::arrayValue; ja.append(params); } - s = to_string(jr); + req.body = to_string(jr); } + write(stream_, req); - using namespace boost::asio; - using namespace std::string_literals; - auto const r = - "POST / HTTP/1.1\r\n" - "Host: me\r\n" - "Connection: Keep-Alive\r\n"s + - "Content-Type: application/json; charset=UTF-8\r\n"s + - "Content-Length: " + std::to_string(s.size()) + "\r\n" - "\r\n" + s; - write(stream_, buffer(r)); - - read_until(stream_, bin_, "\r\n\r\n"); - beast::streambuf body; - beast::deprecated_http::message m; - beast::deprecated_http::parser p( - [&](void const* data, std::size_t size) - { - body.commit(buffer_copy( - body.prepare(size), const_buffer(data, size))); - }, m, false); - - for(;;) - { - boost::system::error_code ec; - auto used = p.write(bin_.data(), ec); - if(ec) - Throw(ec); - bin_.consume(used); - // VFALCO What do we do if bin_ still has data? - if(p.complete()) - break; - bin_.commit(stream_.read_some( - bin_.prepare(1024))); - } + response res; + read(stream_, bin_, res); Json::Reader jr; Json::Value jv; - jr.parse(buffer_string(body.data()), jv); + jr.parse(buffer_string(res.body.data()), jv); if(jv["result"].isMember("error")) jv["error"] = jv["result"]["error"]; if(jv["result"].isMember("status"))