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 <test/jtx/JSONRPCClient.h>
20 #include <ripple/json/json_reader.h>
21 #include <ripple/json/to_string.h>
22 #include <ripple/protocol/jss.h>
23 #include <ripple/server/Port.h>
24 #include <boost/beast/http/message.hpp>
25 #include <boost/beast/http/dynamic_body.hpp>
26 #include <boost/beast/http/string_body.hpp>
27 #include <boost/beast/http/read.hpp>
28 #include <boost/beast/http/write.hpp>
29 #include <boost/asio.hpp>
30 #include <string>
31 
32 namespace ripple {
33 namespace test {
34 
36 {
37  static
38  boost::asio::ip::tcp::endpoint
40  {
41  auto& log = std::cerr;
42  ParsedPort common;
43  parse_Port (common, cfg["server"], log);
44  for (auto const& name : cfg.section("server").values())
45  {
46  if (! cfg.exists(name))
47  continue;
48  ParsedPort pp;
49  parse_Port(pp, cfg[name], log);
50  if(pp.protocol.count("http") == 0)
51  continue;
52  using namespace boost::asio::ip;
53  if(pp.ip && pp.ip->is_unspecified())
54  *pp.ip = pp.ip->is_v6() ? address{address_v6::loopback()} : 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
64  buffer_string (ConstBufferSequence const& b)
65  {
66  using namespace boost::asio;
67  std::string s;
68  s.resize(buffer_size(b));
69  buffer_copy(buffer(&s[0], s.size()), b);
70  return s;
71  }
72 
73  boost::asio::ip::tcp::endpoint ep_;
74  boost::asio::io_service ios_;
75  boost::asio::ip::tcp::socket stream_;
76  boost::beast::multi_buffer bin_;
77  boost::beast::multi_buffer bout_;
78  unsigned rpc_version_;
79 
80 public:
81  explicit
82  JSONRPCClient(Config const& cfg, unsigned rpc_version)
83  : ep_(getEndpoint(cfg))
84  , stream_(ios_)
85  , rpc_version_(rpc_version)
86  {
87  stream_.connect(ep_);
88  }
89 
90  ~JSONRPCClient() override
91  {
92  //stream_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
93  //stream_.close();
94  }
95 
96  /*
97  Return value is an Object type with up to three keys:
98  status
99  error
100  result
101  */
103  invoke(std::string const& cmd,
104  Json::Value const& params) override
105  {
106  using namespace boost::beast::http;
107  using namespace boost::asio;
108  using namespace std::string_literals;
109 
110  request<string_body> req;
111  req.method(boost::beast::http::verb::post);
112  req.target("/");
113  req.version(11);
114  req.insert("Content-Type", "application/json; charset=UTF-8");
115  req.insert("Host", ep_);
116  {
117  Json::Value jr;
118  jr[jss::method] = cmd;
119  if (rpc_version_ == 2)
120  {
121  jr[jss::jsonrpc] = "2.0";
122  jr[jss::ripplerpc] = "2.0";
123  jr[jss::id] = 5;
124  }
125  if(params)
126  {
127  Json::Value& ja = jr[jss::params] = Json::arrayValue;
128  ja.append(params);
129  }
130  req.body() = to_string(jr);
131  }
132  req.prepare_payload();
133  write(stream_, req);
134 
135  response<dynamic_body> res;
136  read(stream_, bin_, res);
137 
138  Json::Reader jr;
139  Json::Value jv;
140  jr.parse(buffer_string(res.body().data()), jv);
141  if(jv["result"].isMember("error"))
142  jv["error"] = jv["result"]["error"];
143  if(jv["result"].isMember("status"))
144  jv["status"] = jv["result"]["status"];
145  return jv;
146  }
147 
148  unsigned 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 } // test
161 } // 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:82
ripple::test::AbstractClient
Definition: AbstractClient.h:33
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
std::string::size
T size(T... args)
ripple::ParsedPort::ip
boost::optional< boost::asio::ip::address > ip
Definition: Port.h:99
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:73
ripple::test::JSONRPCClient::getEndpoint
static boost::asio::ip::tcp::endpoint getEndpoint(BasicConfig const &cfg)
Definition: JSONRPCClient.cpp:39
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
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:77
ripple::ParsedPort
Definition: Port.h:81
ripple::test::JSONRPCClient::invoke
Json::Value invoke(std::string const &cmd, Json::Value const &params) override
Submit a command synchronously.
Definition: JSONRPCClient.cpp:103
ripple::Section::values
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition: BasicConfig.h:78
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:907
ripple::test::JSONRPCClient::bin_
boost::beast::multi_buffer bin_
Definition: JSONRPCClient.cpp:76
ripple::Config
Definition: Config.h:67
boost::asio
Definition: Overlay.h:42
ripple::test::JSONRPCClient::ios_
boost::asio::io_service ios_
Definition: JSONRPCClient.cpp:74
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:100
ripple::test::JSONRPCClient::rpc_version_
unsigned rpc_version_
Definition: JSONRPCClient.cpp:78
ripple::test::JSONRPCClient::~JSONRPCClient
~JSONRPCClient() override
Definition: JSONRPCClient.cpp:90
Json::Reader::parse
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Definition: json_reader.cpp:76
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:75
ripple::test::JSONRPCClient::buffer_string
static std::string buffer_string(ConstBufferSequence const &b)
Definition: JSONRPCClient.cpp:64
ripple::ParsedPort::protocol
std::set< std::string, boost::beast::iless > protocol
Definition: Port.h:86
std::unique_ptr
STL class.
ripple::BasicConfig
Holds unparsed configuration information.
Definition: BasicConfig.h:177
ripple::BasicConfig::exists
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Definition: BasicConfig.cpp:134
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::BasicConfig::section
Section & section(std::string const &name)
Returns the section with the given name.
Definition: BasicConfig.cpp:140
string