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  {
111  std::ostringstream ostr;
112  ostr << ep_;
113  req.insert("Host", ostr.str());
114  }
115  {
116  Json::Value jr;
117  jr[jss::method] = cmd;
118  if (rpc_version_ == 2)
119  {
120  jr[jss::jsonrpc] = "2.0";
121  jr[jss::ripplerpc] = "2.0";
122  jr[jss::id] = 5;
123  }
124  if (params)
125  {
126  Json::Value& ja = jr[jss::params] = Json::arrayValue;
127  ja.append(params);
128  }
129  req.body() = to_string(jr);
130  }
131  req.prepare_payload();
132  write(stream_, req);
133 
134  response<dynamic_body> res;
135  read(stream_, bin_, res);
136 
137  Json::Reader jr;
138  Json::Value jv;
139  jr.parse(buffer_string(res.body().data()), jv);
140  if (jv["result"].isMember("error"))
141  jv["error"] = jv["result"]["error"];
142  if (jv["result"].isMember("status"))
143  jv["status"] = jv["result"]["status"];
144  return jv;
145  }
146 
147  unsigned
148  version() const override
149  {
150  return rpc_version_;
151  }
152 };
153 
155 makeJSONRPCClient(Config const& cfg, unsigned rpc_version)
156 {
157  return std::make_unique<JSONRPCClient>(cfg, rpc_version);
158 }
159 
160 } // namespace test
161 } // 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::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:155
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::parse_Port
void parse_Port(ParsedPort &port, Section const &section, std::ostream &log)
Definition: Port.cpp:199
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:96
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:77
ripple::test::JSONRPCClient::version
unsigned version() const override
Get RPC 1.0 or RPC 2.0.
Definition: JSONRPCClient.cpp:148
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:89
boost::asio
Definition: Overlay.h:41
ripple::test::JSONRPCClient::ios_
boost::asio::io_service ios_
Definition: JSONRPCClient.cpp:73
std::ostringstream
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::JSONRPCClient::rpc_version_
unsigned rpc_version_
Definition: JSONRPCClient.cpp:77
ripple::ParsedPort::ip
std::optional< boost::asio::ip::address > ip
Definition: Port.h:114
ripple::ParsedPort::port
std::optional< std::uint16_t > port
Definition: Port.h:115
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:74
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
std::ostringstream::str
T str(T... args)
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
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:101
std::unique_ptr
STL class.
ripple::BasicConfig
Holds unparsed configuration information.
Definition: BasicConfig.h:215
ripple::BasicConfig::exists
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Definition: BasicConfig.cpp:121
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:127
string