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