Debian Codegraph @

src/rpc/message.cpp

raw symbols SCIP
1 // Copyright (c) 2016-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "message.h"
30
31 #include "daemon_rpc_version.h"
32 #include "serialization/json_object.h"
33
34 namespace cryptonote
35 {
36
37 namespace rpc
38 {
39
40 const char* Message::STATUS_OK = "OK";
41 const char* Message::STATUS_RETRY = "Retry";
42 const char* Message::STATUS_FAILED = "Failed";
43 const char* Message::STATUS_BAD_REQUEST = "Invalid request type";
44 const char* Message::STATUS_BAD_JSON = "Malformed json";
45
46 namespace
47 {
48 constexpr const char error_field[] = "error";
49 constexpr const char id_field[] = "id";
50 constexpr const char method_field[] = "method";
51 constexpr const char params_field[] = "params";
52 constexpr const char result_field[] = "result";
53
54 const rapidjson::Value& get_method_field(const rapidjson::Value& src)
55 {
56 const auto member = src.FindMember(method_field);
57 if (member == src.MemberEnd())
58 throw cryptonote::json::MISSING_KEY{method_field};
59 if (!member->value.IsString())
60 throw cryptonote::json::WRONG_TYPE{"Expected string"};
61 return member->value;
62 }
63 }
64
65 void Message::toJson(rapidjson::Writer<epee::byte_stream>& dest) const
66 {
67 dest.StartObject();
68 INSERT_INTO_JSON_OBJECT(dest, rpc_version, DAEMON_RPC_VERSION_ZMQ);
69 doToJson(dest);
70 dest.EndObject();
71 }
72
73 void Message::fromJson(const rapidjson::Value& val)
74 {
75 GET_FROM_JSON_OBJECT(val, rpc_version, rpc_version);
76 }
77
78 FullMessage::FullMessage(std::string&& json_string, bool request)
79 : contents(std::move(json_string)), doc()
80 {
81 /* Insitu parsing does not copy data from `contents` to DOM,
82 accelerating string heavy content. */
83 doc.ParseInsitu(std::addressof(contents[0]));
84 if (doc.HasParseError() || !doc.IsObject())
85 {
86 throw cryptonote::json::PARSE_FAIL();
87 }
88
89 OBJECT_HAS_MEMBER_OR_THROW(doc, "jsonrpc")
90
91 if (request)
92 {
93 get_method_field(doc); // throws on errors
94 OBJECT_HAS_MEMBER_OR_THROW(doc, params_field)
95 }
96 else
97 {
98 if (!doc.HasMember(result_field) && !doc.HasMember(error_field))
99 {
100 throw cryptonote::json::MISSING_KEY("error/result");
101 }
102 }
103 }
104
105 std::string FullMessage::getRequestType() const
106 {
107 return get_method_field(doc).GetString();
108 }
109
110 const rapidjson::Value& FullMessage::getMessage() const
111 {
112 if (doc.HasMember(params_field))
113 {
114 return doc[params_field];
115 }
116 else if (doc.HasMember(result_field))
117 {
118 return doc[result_field];
119 }
120
121 //else
122 OBJECT_HAS_MEMBER_OR_THROW(doc, error_field)
123 return doc[error_field];
124
125 }
126
127 rapidjson::Value FullMessage::getMessageCopy()
128 {
129 return rapidjson::Value(getMessage(), doc.GetAllocator());
130 }
131
132 const rapidjson::Value& FullMessage::getID() const
133 {
134 OBJECT_HAS_MEMBER_OR_THROW(doc, id_field)
135 return doc[id_field];
136 }
137
138 cryptonote::rpc::error FullMessage::getError()
139 {
140 cryptonote::rpc::error err;
141 err.use = false;
142 if (doc.HasMember(error_field))
143 {
144 GET_FROM_JSON_OBJECT(doc, err, error);
145 err.use = true;
146 }
147
148 return err;
149 }
150
151 epee::byte_slice FullMessage::getRequest(const std::string& request, const Message& message, const unsigned id)
152 {
153 epee::byte_stream buffer;
154 {
155 rapidjson::Writer<epee::byte_stream> dest{buffer};
156
157 dest.StartObject();
158 INSERT_INTO_JSON_OBJECT(dest, jsonrpc, (boost::string_ref{"2.0", 3}));
159
160 dest.Key(id_field);
161 json::toJsonValue(dest, id);
162
163 dest.Key(method_field);
164 json::toJsonValue(dest, request);
165
166 dest.Key(params_field);
167 message.toJson(dest);
168
169 dest.EndObject();
170
171 if (!dest.IsComplete())
172 throw std::logic_error{"Invalid JSON tree generated"};
173 }
174 return epee::byte_slice{std::move(buffer)};
175 }
176
177
178 epee::byte_slice FullMessage::getResponse(const Message& message, const rapidjson::Value& id)
179 {
180 epee::byte_stream buffer;
181 {
182 rapidjson::Writer<epee::byte_stream> dest{buffer};
183
184 dest.StartObject();
185 INSERT_INTO_JSON_OBJECT(dest, jsonrpc, (boost::string_ref{"2.0", 3}));
186
187 dest.Key(id_field);
188 json::toJsonValue(dest, id);
189
190 if (message.status == Message::STATUS_OK)
191 {
192 dest.Key(result_field);
193 message.toJson(dest);
194 }
195 else
196 {
197 cryptonote::rpc::error err;
198
199 err.error_str = message.status;
200 err.message = message.error_details;
201
202 INSERT_INTO_JSON_OBJECT(dest, error, err);
203 }
204 dest.EndObject();
205
206 if (!dest.IsComplete())
207 throw std::logic_error{"Invalid JSON tree generated"};
208 }
209 return epee::byte_slice{std::move(buffer)};
210 }
211
212 // convenience functions for bad input
213 epee::byte_slice BAD_REQUEST(const std::string& request)
214 {
215 rapidjson::Value invalid;
216 return BAD_REQUEST(request, invalid);
217 }
218
219 epee::byte_slice BAD_REQUEST(const std::string& request, const rapidjson::Value& id)
220 {
221 Message fail;
222 fail.status = Message::STATUS_BAD_REQUEST;
223 fail.error_details = std::string("\"") + request + "\" is not a valid request.";
224 return FullMessage::getResponse(fail, id);
225 }
226
227 epee::byte_slice BAD_JSON(const std::string& error_details)
228 {
229 rapidjson::Value invalid;
230 Message fail;
231 fail.status = Message::STATUS_BAD_JSON;
232 fail.error_details = error_details;
233 return FullMessage::getResponse(fail, invalid);
234 }
235
236
237 } // namespace rpc
238
239 } // namespace cryptonote