MCPcopy Create free account
hub / github.com/bitcoinxt/bitcoinxt / HTTPReq_JSONRPC

Function HTTPReq_JSONRPC

src/httprpc.cpp:87–146  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

85}
86
87static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
88{
89 // JSONRPC handles only POST
90 if (req->GetRequestMethod() != HTTPRequest::POST) {
91 req->WriteReply(HTTP_BAD_METHOD, "JSONRPC server handles only POST requests");
92 return false;
93 }
94 // Check authorization
95 std::pair<bool, std::string> authHeader = req->GetHeader("authorization");
96 if (!authHeader.first) {
97 req->WriteReply(HTTP_UNAUTHORIZED);
98 return false;
99 }
100
101 if (!RPCAuthorized(authHeader.second)) {
102 LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", req->GetPeer().ToString());
103
104 /* Deter brute-forcing
105 If this results in a DoS the user really
106 shouldn't have their RPC port exposed. */
107 MilliSleep(250);
108
109 req->WriteReply(HTTP_UNAUTHORIZED);
110 return false;
111 }
112
113 JSONRequest jreq;
114 try {
115 // Parse request
116 UniValue valRequest;
117 if (!valRequest.read(req->ReadBody()))
118 throw JSONRPCError(RPC_PARSE_ERROR, "Parse error");
119
120 std::string strReply;
121 // singleton request
122 if (valRequest.isObject()) {
123 jreq.parse(valRequest);
124
125 UniValue result = tableRPC.execute(jreq.strMethod, jreq.params);
126
127 // Send reply
128 strReply = JSONRPCReply(result, NullUniValue, jreq.id);
129
130 // array of requests
131 } else if (valRequest.isArray())
132 strReply = JSONRPCExecBatch(valRequest.get_array());
133 else
134 throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
135
136 req->WriteHeader("Content-Type", "application/json");
137 req->WriteReply(HTTP_OK, strReply);
138 } catch (const UniValue& objError) {
139 JSONErrorReply(req, objError, jreq.id);
140 return false;
141 } catch (const std::exception& e) {
142 JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
143 return false;
144 }

Callers

nothing calls this directly

Calls 15

RPCAuthorizedFunction · 0.85
MilliSleepFunction · 0.85
JSONRPCErrorFunction · 0.85
JSONRPCReplyFunction · 0.85
JSONRPCExecBatchFunction · 0.85
JSONErrorReplyFunction · 0.85
GetRequestMethodMethod · 0.80
WriteReplyMethod · 0.80
GetHeaderMethod · 0.80
GetPeerMethod · 0.80
ReadBodyMethod · 0.80
isObjectMethod · 0.80

Tested by

no test coverage detected