diff options
author | Matthew Zipkin <pinheadmz@gmail.com> | 2023-07-07 14:31:18 -0400 |
---|---|---|
committer | Matthew Zipkin <pinheadmz@gmail.com> | 2024-05-14 10:32:43 -0400 |
commit | 2ca1460ae3a7217eaa8c5972515bf622bedadfce (patch) | |
tree | 5812d18bb8eb28d68d666ae26cc1232cd8f00d3a /src/rpc | |
parent | a64a2b77e09bff784a2635ba19ff4aa6582bb5a5 (diff) | |
download | bitcoin-2ca1460ae3a7217eaa8c5972515bf622bedadfce.tar.xz |
rpc: identify JSON-RPC 2.0 requests
Diffstat (limited to 'src/rpc')
-rw-r--r-- | src/rpc/request.cpp | 19 | ||||
-rw-r--r-- | src/rpc/request.h | 6 |
2 files changed, 25 insertions, 0 deletions
diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp index 12726ecc88..08e0658561 100644 --- a/src/rpc/request.cpp +++ b/src/rpc/request.cpp @@ -167,6 +167,25 @@ void JSONRPCRequest::parse(const UniValue& valRequest) // Parse id now so errors from here on will have the id id = request.find_value("id"); + // Check for JSON-RPC 2.0 (default 1.1) + m_json_version = JSONRPCVersion::V1_LEGACY; + const UniValue& jsonrpc_version = request.find_value("jsonrpc"); + if (!jsonrpc_version.isNull()) { + if (!jsonrpc_version.isStr()) { + throw JSONRPCError(RPC_INVALID_REQUEST, "jsonrpc field must be a string"); + } + // The "jsonrpc" key was added in the 2.0 spec, but some older documentation + // incorrectly included {"jsonrpc":"1.0"} in a request object, so we + // maintain that for backwards compatibility. + if (jsonrpc_version.get_str() == "1.0") { + m_json_version = JSONRPCVersion::V1_LEGACY; + } else if (jsonrpc_version.get_str() == "2.0") { + m_json_version = JSONRPCVersion::V2; + } else { + throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported"); + } + } + // Parse method const UniValue& valMethod{request.find_value("method")}; if (valMethod.isNull()) diff --git a/src/rpc/request.h b/src/rpc/request.h index 116ebb8aac..8b72172695 100644 --- a/src/rpc/request.h +++ b/src/rpc/request.h @@ -11,6 +11,11 @@ #include <univalue.h> +enum class JSONRPCVersion { + V1_LEGACY, + V2 +}; + UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id); UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id); UniValue JSONRPCError(int code, const std::string& message); @@ -35,6 +40,7 @@ public: std::string authUser; std::string peerAddr; std::any context; + JSONRPCVersion m_json_version = JSONRPCVersion::V1_LEGACY; void parse(const UniValue& valRequest); }; |