2019-03-05 22:05:34 +01:00
|
|
|
// Copyright (c) 2016-2019, The Monero Project
|
2017-09-05 18:20:27 +02:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "message.h"
|
|
|
|
#include "daemon_rpc_version.h"
|
|
|
|
#include "serialization/json_object.h"
|
|
|
|
|
|
|
|
#include "rapidjson/writer.h"
|
|
|
|
#include "rapidjson/stringbuffer.h"
|
|
|
|
|
|
|
|
namespace cryptonote
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace rpc
|
|
|
|
{
|
|
|
|
|
|
|
|
const char* Message::STATUS_OK = "OK";
|
|
|
|
const char* Message::STATUS_RETRY = "Retry";
|
|
|
|
const char* Message::STATUS_FAILED = "Failed";
|
|
|
|
const char* Message::STATUS_BAD_REQUEST = "Invalid request type";
|
|
|
|
const char* Message::STATUS_BAD_JSON = "Malformed json";
|
|
|
|
|
2017-10-26 22:39:22 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
constexpr const char error_field[] = "error";
|
|
|
|
constexpr const char id_field[] = "id";
|
|
|
|
constexpr const char method_field[] = "method";
|
|
|
|
constexpr const char params_field[] = "params";
|
|
|
|
constexpr const char result_field[] = "result";
|
|
|
|
}
|
|
|
|
|
2017-09-05 18:20:27 +02:00
|
|
|
rapidjson::Value Message::toJson(rapidjson::Document& doc) const
|
|
|
|
{
|
|
|
|
rapidjson::Value val(rapidjson::kObjectType);
|
|
|
|
|
|
|
|
auto& al = doc.GetAllocator();
|
|
|
|
|
|
|
|
val.AddMember("status", rapidjson::StringRef(status.c_str()), al);
|
|
|
|
val.AddMember("error_details", rapidjson::StringRef(error_details.c_str()), al);
|
2017-09-05 18:20:40 +02:00
|
|
|
INSERT_INTO_JSON_OBJECT(val, doc, rpc_version, DAEMON_RPC_VERSION_ZMQ);
|
2017-09-05 18:20:27 +02:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Message::fromJson(rapidjson::Value& val)
|
|
|
|
{
|
|
|
|
GET_FROM_JSON_OBJECT(val, status, status);
|
|
|
|
GET_FROM_JSON_OBJECT(val, error_details, error_details);
|
|
|
|
GET_FROM_JSON_OBJECT(val, rpc_version, rpc_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FullMessage::FullMessage(const std::string& request, Message* message)
|
|
|
|
{
|
|
|
|
doc.SetObject();
|
|
|
|
|
2017-10-26 22:39:22 +02:00
|
|
|
doc.AddMember(method_field, rapidjson::StringRef(request.c_str()), doc.GetAllocator());
|
|
|
|
doc.AddMember(params_field, message->toJson(doc), doc.GetAllocator());
|
2017-09-05 18:20:27 +02:00
|
|
|
|
|
|
|
// required by JSON-RPC 2.0 spec
|
|
|
|
doc.AddMember("jsonrpc", rapidjson::Value("2.0"), doc.GetAllocator());
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMessage::FullMessage(Message* message)
|
|
|
|
{
|
|
|
|
doc.SetObject();
|
|
|
|
|
|
|
|
// required by JSON-RPC 2.0 spec
|
|
|
|
doc.AddMember("jsonrpc", "2.0", doc.GetAllocator());
|
|
|
|
|
|
|
|
if (message->status == Message::STATUS_OK)
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
doc.AddMember(result_field, message->toJson(doc), doc.GetAllocator());
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cryptonote::rpc::error err;
|
|
|
|
|
|
|
|
err.error_str = message->status;
|
|
|
|
err.message = message->error_details;
|
|
|
|
|
|
|
|
INSERT_INTO_JSON_OBJECT(doc, doc, error, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMessage::FullMessage(const std::string& json_string, bool request)
|
|
|
|
{
|
|
|
|
doc.Parse(json_string.c_str());
|
2017-12-10 16:36:15 +01:00
|
|
|
if (doc.HasParseError() || !doc.IsObject())
|
2017-09-05 18:20:27 +02:00
|
|
|
{
|
|
|
|
throw cryptonote::json::PARSE_FAIL();
|
|
|
|
}
|
|
|
|
|
|
|
|
OBJECT_HAS_MEMBER_OR_THROW(doc, "jsonrpc")
|
|
|
|
|
|
|
|
if (request)
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
OBJECT_HAS_MEMBER_OR_THROW(doc, method_field)
|
|
|
|
OBJECT_HAS_MEMBER_OR_THROW(doc, params_field)
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
if (!doc.HasMember(result_field) && !doc.HasMember(error_field))
|
2017-09-05 18:20:27 +02:00
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
throw cryptonote::json::MISSING_KEY("error/result");
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FullMessage::getJson()
|
|
|
|
{
|
|
|
|
|
2017-10-26 22:39:22 +02:00
|
|
|
if (!doc.HasMember(id_field))
|
2017-09-05 18:20:27 +02:00
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
doc.AddMember(id_field, rapidjson::Value("unused"), doc.GetAllocator());
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::StringBuffer buf;
|
|
|
|
|
|
|
|
rapidjson::Writer<rapidjson::StringBuffer> writer(buf);
|
|
|
|
|
|
|
|
doc.Accept(writer);
|
|
|
|
|
|
|
|
return std::string(buf.GetString(), buf.GetSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FullMessage::getRequestType() const
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
OBJECT_HAS_MEMBER_OR_THROW(doc, method_field)
|
|
|
|
return doc[method_field].GetString();
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::Value& FullMessage::getMessage()
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
if (doc.HasMember(params_field))
|
2017-09-05 18:20:27 +02:00
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
return doc[params_field];
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
2017-10-26 22:39:22 +02:00
|
|
|
else if (doc.HasMember(result_field))
|
2017-09-05 18:20:27 +02:00
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
return doc[result_field];
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//else
|
2017-10-26 22:39:22 +02:00
|
|
|
OBJECT_HAS_MEMBER_OR_THROW(doc, error_field)
|
|
|
|
return doc[error_field];
|
2017-09-05 18:20:27 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::Value FullMessage::getMessageCopy()
|
|
|
|
{
|
|
|
|
rapidjson::Value& val = getMessage();
|
|
|
|
|
|
|
|
return rapidjson::Value(val, doc.GetAllocator());
|
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::Value& FullMessage::getID()
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
OBJECT_HAS_MEMBER_OR_THROW(doc, id_field)
|
|
|
|
return doc[id_field];
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FullMessage::setID(rapidjson::Value& id)
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
auto itr = doc.FindMember(id_field);
|
2017-09-05 18:20:27 +02:00
|
|
|
if (itr != doc.MemberEnd())
|
|
|
|
{
|
|
|
|
itr->value = id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-26 22:39:22 +02:00
|
|
|
doc.AddMember(id_field, id, doc.GetAllocator());
|
2017-09-05 18:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cryptonote::rpc::error FullMessage::getError()
|
|
|
|
{
|
|
|
|
cryptonote::rpc::error err;
|
|
|
|
err.use = false;
|
2017-10-26 22:39:22 +02:00
|
|
|
if (doc.HasMember(error_field))
|
2017-09-05 18:20:27 +02:00
|
|
|
{
|
|
|
|
GET_FROM_JSON_OBJECT(doc, err, error);
|
|
|
|
err.use = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMessage FullMessage::requestMessage(const std::string& request, Message* message)
|
|
|
|
{
|
|
|
|
return FullMessage(request, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMessage FullMessage::requestMessage(const std::string& request, Message* message, rapidjson::Value& id)
|
|
|
|
{
|
|
|
|
auto mes = requestMessage(request, message);
|
|
|
|
mes.setID(id);
|
|
|
|
return mes;
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMessage FullMessage::responseMessage(Message* message)
|
|
|
|
{
|
|
|
|
return FullMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMessage FullMessage::responseMessage(Message* message, rapidjson::Value& id)
|
|
|
|
{
|
|
|
|
auto mes = responseMessage(message);
|
|
|
|
mes.setID(id);
|
|
|
|
return mes;
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMessage* FullMessage::timeoutMessage()
|
|
|
|
{
|
|
|
|
auto *full_message = new FullMessage();
|
|
|
|
|
|
|
|
auto& doc = full_message->doc;
|
|
|
|
auto& al = full_message->doc.GetAllocator();
|
|
|
|
|
|
|
|
doc.SetObject();
|
|
|
|
|
|
|
|
// required by JSON-RPC 2.0 spec
|
|
|
|
doc.AddMember("jsonrpc", "2.0", al);
|
|
|
|
|
|
|
|
cryptonote::rpc::error err;
|
|
|
|
|
|
|
|
err.error_str = "RPC request timed out.";
|
|
|
|
INSERT_INTO_JSON_OBJECT(doc, doc, err, err);
|
|
|
|
|
|
|
|
return full_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convenience functions for bad input
|
|
|
|
std::string BAD_REQUEST(const std::string& request)
|
|
|
|
{
|
|
|
|
Message fail;
|
|
|
|
fail.status = Message::STATUS_BAD_REQUEST;
|
|
|
|
fail.error_details = std::string("\"") + request + "\" is not a valid request.";
|
|
|
|
|
|
|
|
FullMessage fail_response = FullMessage::responseMessage(&fail);
|
|
|
|
|
|
|
|
return fail_response.getJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BAD_REQUEST(const std::string& request, rapidjson::Value& id)
|
|
|
|
{
|
|
|
|
Message fail;
|
|
|
|
fail.status = Message::STATUS_BAD_REQUEST;
|
|
|
|
fail.error_details = std::string("\"") + request + "\" is not a valid request.";
|
|
|
|
|
|
|
|
FullMessage fail_response = FullMessage::responseMessage(&fail, id);
|
|
|
|
|
|
|
|
return fail_response.getJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BAD_JSON(const std::string& error_details)
|
|
|
|
{
|
|
|
|
Message fail;
|
|
|
|
fail.status = Message::STATUS_BAD_JSON;
|
|
|
|
fail.error_details = error_details;
|
|
|
|
|
|
|
|
FullMessage fail_response = FullMessage::responseMessage(&fail);
|
|
|
|
|
|
|
|
return fail_response.getJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace rpc
|
|
|
|
|
|
|
|
} // namespace cryptonote
|