Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/jsonrpc/jsonrpc.cpp
10277 views
1
/**************************************************************************/
2
/* jsonrpc.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "jsonrpc.h"
32
#include "jsonrpc.compat.inc"
33
34
#include "core/io/json.h"
35
36
JSONRPC::JSONRPC() {
37
}
38
39
JSONRPC::~JSONRPC() {
40
}
41
42
void JSONRPC::_bind_methods() {
43
ClassDB::bind_method(D_METHOD("set_method", "name", "callback"), &JSONRPC::set_method);
44
ClassDB::bind_method(D_METHOD("process_action", "action", "recurse"), &JSONRPC::process_action, DEFVAL(false));
45
ClassDB::bind_method(D_METHOD("process_string", "action"), &JSONRPC::process_string);
46
47
ClassDB::bind_method(D_METHOD("make_request", "method", "params", "id"), &JSONRPC::make_request);
48
ClassDB::bind_method(D_METHOD("make_response", "result", "id"), &JSONRPC::make_response);
49
ClassDB::bind_method(D_METHOD("make_notification", "method", "params"), &JSONRPC::make_notification);
50
ClassDB::bind_method(D_METHOD("make_response_error", "code", "message", "id"), &JSONRPC::make_response_error, DEFVAL(Variant()));
51
52
BIND_ENUM_CONSTANT(PARSE_ERROR);
53
BIND_ENUM_CONSTANT(INVALID_REQUEST);
54
BIND_ENUM_CONSTANT(METHOD_NOT_FOUND);
55
BIND_ENUM_CONSTANT(INVALID_PARAMS);
56
BIND_ENUM_CONSTANT(INTERNAL_ERROR);
57
}
58
59
Dictionary JSONRPC::make_response_error(int p_code, const String &p_message, const Variant &p_id) const {
60
Dictionary dict;
61
dict["jsonrpc"] = "2.0";
62
63
Dictionary err;
64
err["code"] = p_code;
65
err["message"] = p_message;
66
67
dict["error"] = err;
68
dict["id"] = p_id;
69
70
return dict;
71
}
72
73
Dictionary JSONRPC::make_response(const Variant &p_value, const Variant &p_id) {
74
Dictionary dict;
75
dict["jsonrpc"] = "2.0";
76
dict["id"] = p_id;
77
dict["result"] = p_value;
78
return dict;
79
}
80
81
Dictionary JSONRPC::make_notification(const String &p_method, const Variant &p_params) {
82
Dictionary dict;
83
dict["jsonrpc"] = "2.0";
84
dict["method"] = p_method;
85
dict["params"] = p_params;
86
return dict;
87
}
88
89
Dictionary JSONRPC::make_request(const String &p_method, const Variant &p_params, const Variant &p_id) {
90
Dictionary dict;
91
dict["jsonrpc"] = "2.0";
92
dict["method"] = p_method;
93
dict["params"] = p_params;
94
dict["id"] = p_id;
95
return dict;
96
}
97
98
Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elements) {
99
Variant ret;
100
if (p_action.get_type() == Variant::DICTIONARY) {
101
Dictionary dict = p_action;
102
String method = dict.get("method", "");
103
104
Array args;
105
if (dict.has("params")) {
106
Variant params = dict.get("params", Variant());
107
if (params.get_type() == Variant::ARRAY) {
108
args = params;
109
} else {
110
args.push_back(params);
111
}
112
}
113
114
/// A Notification is a Request object without an "id" member.
115
bool is_notification = !dict.has("id");
116
117
Variant id;
118
if (!is_notification) {
119
id = dict["id"];
120
121
// Account for implementations that discern between int and float on the json serialization level, by using an int if there is a .0 fraction. See #100914
122
if (id.get_type() == Variant::FLOAT && id.operator float() == (float)(id.operator int())) {
123
id = id.operator int();
124
}
125
}
126
127
if (methods.has(method)) {
128
Variant call_ret = methods[method].callv(args);
129
ret = make_response(call_ret, id);
130
} else {
131
ret = make_response_error(JSONRPC::METHOD_NOT_FOUND, "Method not found: " + method, id);
132
}
133
134
/// The Server MUST NOT reply to a Notification
135
if (is_notification) {
136
ret = Variant();
137
}
138
} else if (p_action.get_type() == Variant::ARRAY && p_process_arr_elements) {
139
Array arr = p_action;
140
if (!arr.is_empty()) {
141
Array arr_ret;
142
for (const Variant &var : arr) {
143
Variant res = process_action(var);
144
145
if (res.get_type() != Variant::NIL) {
146
arr_ret.push_back(res);
147
}
148
}
149
150
/// If there are no Response objects contained within the Response array as it is to be sent to the client, the server MUST NOT return an empty Array and should return nothing at all.
151
if (!arr_ret.is_empty()) {
152
ret = arr_ret;
153
}
154
} else {
155
/// If the batch rpc call itself fails to be recognized ... as an Array with at least one value, the response from the Server MUST be a single Response object.
156
ret = make_response_error(JSONRPC::INVALID_REQUEST, "Invalid Request");
157
}
158
} else {
159
ret = make_response_error(JSONRPC::INVALID_REQUEST, "Invalid Request");
160
}
161
return ret;
162
}
163
164
String JSONRPC::process_string(const String &p_input) {
165
if (p_input.is_empty()) {
166
return String();
167
}
168
169
Variant ret;
170
JSON json;
171
if (json.parse(p_input) == OK) {
172
ret = process_action(json.get_data(), true);
173
} else {
174
ret = make_response_error(JSONRPC::PARSE_ERROR, "Parse error");
175
}
176
177
if (ret.get_type() == Variant::NIL) {
178
return "";
179
}
180
return ret.to_json_string();
181
}
182
183
void JSONRPC::set_method(const String &p_name, const Callable &p_callback) {
184
methods[p_name] = p_callback;
185
}
186
187