jsonrpc.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "jsonrpc.h"
  31. #include "jsonrpc.compat.inc"
  32. #include "core/io/json.h"
  33. JSONRPC::JSONRPC() {
  34. }
  35. JSONRPC::~JSONRPC() {
  36. }
  37. void JSONRPC::_bind_methods() {
  38. ClassDB::bind_method(D_METHOD("set_method", "name", "callback"), &JSONRPC::set_method);
  39. ClassDB::bind_method(D_METHOD("process_action", "action", "recurse"), &JSONRPC::process_action, DEFVAL(false));
  40. ClassDB::bind_method(D_METHOD("process_string", "action"), &JSONRPC::process_string);
  41. ClassDB::bind_method(D_METHOD("make_request", "method", "params", "id"), &JSONRPC::make_request);
  42. ClassDB::bind_method(D_METHOD("make_response", "result", "id"), &JSONRPC::make_response);
  43. ClassDB::bind_method(D_METHOD("make_notification", "method", "params"), &JSONRPC::make_notification);
  44. ClassDB::bind_method(D_METHOD("make_response_error", "code", "message", "id"), &JSONRPC::make_response_error, DEFVAL(Variant()));
  45. BIND_ENUM_CONSTANT(PARSE_ERROR);
  46. BIND_ENUM_CONSTANT(INVALID_REQUEST);
  47. BIND_ENUM_CONSTANT(METHOD_NOT_FOUND);
  48. BIND_ENUM_CONSTANT(INVALID_PARAMS);
  49. BIND_ENUM_CONSTANT(INTERNAL_ERROR);
  50. }
  51. Dictionary JSONRPC::make_response_error(int p_code, const String &p_message, const Variant &p_id) const {
  52. Dictionary dict;
  53. dict["jsonrpc"] = "2.0";
  54. Dictionary err;
  55. err["code"] = p_code;
  56. err["message"] = p_message;
  57. dict["error"] = err;
  58. dict["id"] = p_id;
  59. return dict;
  60. }
  61. Dictionary JSONRPC::make_response(const Variant &p_value, const Variant &p_id) {
  62. Dictionary dict;
  63. dict["jsonrpc"] = "2.0";
  64. dict["id"] = p_id;
  65. dict["result"] = p_value;
  66. return dict;
  67. }
  68. Dictionary JSONRPC::make_notification(const String &p_method, const Variant &p_params) {
  69. Dictionary dict;
  70. dict["jsonrpc"] = "2.0";
  71. dict["method"] = p_method;
  72. dict["params"] = p_params;
  73. return dict;
  74. }
  75. Dictionary JSONRPC::make_request(const String &p_method, const Variant &p_params, const Variant &p_id) {
  76. Dictionary dict;
  77. dict["jsonrpc"] = "2.0";
  78. dict["method"] = p_method;
  79. dict["params"] = p_params;
  80. dict["id"] = p_id;
  81. return dict;
  82. }
  83. Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elements) {
  84. Variant ret;
  85. if (p_action.get_type() == Variant::DICTIONARY) {
  86. Dictionary dict = p_action;
  87. String method = dict.get("method", "");
  88. if (method.begins_with("$/")) {
  89. return ret;
  90. }
  91. Array args;
  92. if (dict.has("params")) {
  93. Variant params = dict.get("params", Variant());
  94. if (params.get_type() == Variant::ARRAY) {
  95. args = params;
  96. } else {
  97. args.push_back(params);
  98. }
  99. }
  100. Variant id;
  101. if (dict.has("id")) {
  102. id = dict["id"];
  103. // 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
  104. if (id.get_type() == Variant::FLOAT && id.operator float() == (float)(id.operator int())) {
  105. id = id.operator int();
  106. }
  107. }
  108. if (methods.has(method)) {
  109. Variant call_ret = methods[method].callv(args);
  110. if (id.get_type() != Variant::NIL) {
  111. ret = make_response(call_ret, id);
  112. }
  113. } else {
  114. ret = make_response_error(JSONRPC::METHOD_NOT_FOUND, "Method not found: " + method, id);
  115. }
  116. } else if (p_action.get_type() == Variant::ARRAY && p_process_arr_elements) {
  117. Array arr = p_action;
  118. int size = arr.size();
  119. if (size) {
  120. Array arr_ret;
  121. for (int i = 0; i < size; i++) {
  122. const Variant &var = arr.get(i);
  123. arr_ret.push_back(process_action(var));
  124. }
  125. ret = arr_ret;
  126. } else {
  127. ret = make_response_error(JSONRPC::INVALID_REQUEST, "Invalid Request");
  128. }
  129. } else {
  130. ret = make_response_error(JSONRPC::INVALID_REQUEST, "Invalid Request");
  131. }
  132. return ret;
  133. }
  134. String JSONRPC::process_string(const String &p_input) {
  135. if (p_input.is_empty()) {
  136. return String();
  137. }
  138. Variant ret;
  139. JSON json;
  140. if (json.parse(p_input) == OK) {
  141. ret = process_action(json.get_data(), true);
  142. } else {
  143. ret = make_response_error(JSONRPC::PARSE_ERROR, "Parse error");
  144. }
  145. if (ret.get_type() == Variant::NIL) {
  146. return "";
  147. }
  148. return ret.to_json_string();
  149. }
  150. void JSONRPC::set_method(const String &p_name, const Callable &p_callback) {
  151. methods[p_name] = p_callback;
  152. }