rippled
JSONRPCClient.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2016 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 #include <ripple/json/json_reader.h>
20 #include <ripple/json/to_string.h>
21 #include <ripple/protocol/jss.h>
22 #include <ripple/server/Port.h>
23 #include <boost/asio.hpp>
24 #include <boost/beast/http/dynamic_body.hpp>
25 #include <boost/beast/http/message.hpp>
26 #include <boost/beast/http/read.hpp>
27 #include <boost/beast/http/string_body.hpp>
28 #include <boost/beast/http/write.hpp>
29 #include <string>
30 #include <test/jtx/JSONRPCClient.h>
31 
32 namespace ripple {
33 namespace test {
34 
36 {
37  static boost::asio::ip::tcp::endpoint
39  {
40  auto& log = std::cerr;
41  ParsedPort common;
42  parse_Port(common, cfg["server"], log);
43  for (auto const& name : cfg.section("server").values())
44  {
45  if (!cfg.exists(name))
46  continue;
47  ParsedPort pp;
48  parse_Port(pp, cfg[name], log);
49  if (pp.protocol.count("http") == 0)
50  continue;
51  using namespace boost::asio::ip;
52  if (pp.ip && pp.ip->is_unspecified())
53  *pp.ip = pp.ip->is_v6() ? address{address_v6::loopback()}
54  : address{address_v4::loopback()};
55  return {*pp.ip, *pp.port};
56  }
57  Throw<std::runtime_error>("Missing HTTP port");
58  return {}; // Silence compiler control paths return value warning
59  }
60 
61  template <class ConstBufferSequence>
62  static std::string
63  buffer_string(ConstBufferSequence const& b)
64  {
65  using namespace boost::asio;
66  std::string s;
67  s.resize(buffer_size(b));
68  buffer_copy(buffer(&s[0], s.size()), b);
69  return s;
70  }
71 
72  boost::asio::ip::tcp::endpoint ep_;
73  boost::asio::io_service ios_;
74  boost::asio::ip::tcp::socket stream_;
75  boost::beast::multi_buffer bin_;
76  boost::beast::multi_buffer bout_;
77  unsigned rpc_version_;
78 
79 public:
80  explicit JSONRPCClient(Config const& cfg, unsigned rpc_version)
81  : ep_(getEndpoint(cfg)), stream_(ios_), rpc_version_(rpc_version)
82  {
83  stream_.connect(ep_);
84  }
85 
86  ~JSONRPCClient() override
87  {
88  // stream_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
89  // stream_.close();
90  }
91 
92  /*
93  Return value is an Object type with up to three keys:
94  status
95  error
96  result
97  */
99  invoke(std::string const& cmd, Json::Value const& params) override
100  {
101  using namespace boost::beast::http;
102  using namespace boost::asio;
103  using namespace std::string_literals;
104 
105  request<string_body> req;
106  req.method(boost::beast::http::verb::post);
107  req.target("/");
108  req.version(11);
109  req.insert("Content-Type", "application/json; charset=UTF-8");
110  req.insert("Host", ep_);
111  {
112  Json::Value jr;
113  jr[jss::method] = cmd;
114  if (rpc_version_ == 2)
115  {
116  jr[jss::jsonrpc] = "2.0";
117  jr[jss::ripplerpc] = "2.0";
118  jr[jss::id] = 5;
119  }
120  if (params)
121  {
122  Json::Value& ja = jr[jss::params] = Json::arrayValue;
123  ja.append(params);
124  }
125  req.body() = to_string(jr);
126  }
127  req.prepare_payload();
128  write(stream_, req);
129 
130  response<dynamic_body> res;
131  read(stream_, bin_, res);
132 
133  Json::Reader jr;
134  Json::Value jv;
135  jr.parse(buffer_string(res.body().data()), jv);
136  if (jv["result"].isMember("error"))
137  jv["error"] = jv["result"]["error"];
138  if (jv["result"].isMember("status"))
139  jv["status"] = jv["result"]["status"];
140  return jv;
141  }
142 
143  unsigned
144  version() const override
145  {
146  return rpc_version_;
147  }
148 };
149 
151 makeJSONRPCClient(Config const& cfg, unsigned rpc_version)
152 {
153  return std::make_unique<JSONRPCClient>(cfg, rpc_version);
154 }
155 
156 } // namespace test
157 } // namespace ripple
std::string::resize
T resize(T... args)
std::string
STL class.
ripple::test::JSONRPCClient::JSONRPCClient
JSONRPCClient(Config const &cfg, unsigned rpc_version)
Definition: JSONRPCClient.cpp:80
ripple::test::AbstractClient
Definition: AbstractClient.h:33
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
std::string::size
T size(T... args)
ripple::ParsedPort::ip
boost::optional< boost::asio::ip::address > ip
Definition: Port.h:108
ripple::test::makeJSONRPCClient
std::unique_ptr< AbstractClient > makeJSONRPCClient(Config const &cfg, unsigned rpc_version)
Returns a client using JSON-RPC over HTTP/S.
Definition: JSONRPCClient.cpp:151
std::cerr
ripple::test::JSONRPCClient::ep_
boost::asio::ip::tcp::endpoint ep_
Definition: JSONRPCClient.cpp:72
ripple::test::JSONRPCClient::getEndpoint
static boost::asio::ip::tcp::endpoint getEndpoint(BasicConfig const &cfg)
Definition: JSONRPCClient.cpp:38
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:42
ripple::parse_Port
void parse_Port(ParsedPort &port, Section const &section, std::ostream &log)
Definition: Port.cpp:139
Json::Reader
Unserialize a JSON document into a Value.
Definition: json_reader.h:36
ripple::test::JSONRPCClient::bout_
boost::beast::multi_buffer bout_
Definition: JSONRPCClient.cpp:76
ripple::ParsedPort
Definition: Port.h:90
ripple::test::JSONRPCClient::invoke
Json::Value invoke(std::string const &cmd, Json::Value const &params) override
Submit a command synchronously.
Definition: JSONRPCClient.cpp:99
ripple::Section::values
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition: BasicConfig.h:76
ripple::test::JSONRPCClient::version
unsigned version() const override
Get RPC 1.0 or RPC 2.0.
Definition: JSONRPCClient.cpp:144
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
ripple::test::JSONRPCClient::bin_
boost::beast::multi_buffer bin_
Definition: JSONRPCClient.cpp:75
ripple::Config
Definition: Config.h:67
boost::asio
Definition: Overlay.h:42
ripple::test::JSONRPCClient::ios_
boost::asio::io_service ios_
Definition: JSONRPCClient.cpp:73
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ParsedPort::port
boost::optional< std::uint16_t > port
Definition: Port.h:109
ripple::test::JSONRPCClient::rpc_version_
unsigned rpc_version_
Definition: JSONRPCClient.cpp:77
ripple::test::JSONRPCClient::~JSONRPCClient
~JSONRPCClient() override
Definition: JSONRPCClient.cpp:86
Json::Reader::parse
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Definition: json_reader.cpp:73
ripple::test::JSONRPCClient
Definition: JSONRPCClient.cpp:35
std::set::count
T count(T... args)
ripple::test::JSONRPCClient::stream_
boost::asio::ip::tcp::socket stream_
Definition: JSONRPCClient.cpp:74
ripple::test::JSONRPCClient::buffer_string
static std::string buffer_string(ConstBufferSequence const &b)
Definition: JSONRPCClient.cpp:63
ripple::ParsedPort::protocol
std::set< std::string, boost::beast::iless > protocol
Definition: Port.h:95
std::unique_ptr
STL class.
ripple::BasicConfig
Holds unparsed configuration information.
Definition: BasicConfig.h:178
ripple::BasicConfig::exists
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Definition: BasicConfig.cpp:132
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::BasicConfig::section
Section & section(std::string const &name)
Returns the section with the given name.
Definition: BasicConfig.cpp:138
string