MCPcopy Create free account
hub / github.com/LUX-Core/lux / CallRPC

Function CallRPC

src/lux-cli.cpp:152–224  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

150#endif
151
152UniValue CallRPC(const string& strMethod, const UniValue& params)
153{
154 std::string host = GetArg("-rpcconnect", "127.0.0.1");
155 int port = GetArg("-rpcport", BaseParams().RPCPort());
156
157 // Obtain event base
158 raii_event_base base = obtain_event_base();
159
160
161 // Synchronously look up hostname
162 raii_evhttp_connection evcon = obtain_evhttp_connection_base(base.get(), host, port);
163 evhttp_connection_set_timeout(evcon.get(), GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT));
164
165 HTTPReply response;
166 raii_evhttp_request req = obtain_evhttp_request(http_request_done, (void*)&response);
167 if (req == NULL)
168 throw runtime_error("create http request failed");
169#if LIBEVENT_VERSION_NUMBER >= 0x02010300
170 evhttp_request_set_error_cb(req.get(), http_error_cb);
171#endif
172
173 // Find credentials to use
174 std::string strRPCUserColonPass;
175 if (mapArgs["-rpcpassword"] == "") {
176 // Try fall back to cookie-based authentication if no password is provided
177 if (!GetAuthCookie(&strRPCUserColonPass)) {
178 throw runtime_error(strprintf(_("Could not locate RPC credentials. No authentication cookie could be found, and no rpcpassword is set in the configuration file (%s)"),
179 GetConfigFile().string().c_str()));
180 }
181 } else {
182 strRPCUserColonPass = mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"];
183 }
184
185 // HTTP basic authentication
186 struct evkeyvalq *output_headers = evhttp_request_get_output_headers(req.get());
187 assert(output_headers);
188 evhttp_add_header(output_headers, "Host", host.c_str());
189 evhttp_add_header(output_headers, "Connection", "close");
190 evhttp_add_header(output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str());
191
192 // Send request
193 std::string strRequest = JSONRPCRequest(strMethod, params, 1);
194 struct evbuffer *output_buffer = evhttp_request_get_output_buffer(req.get());
195 assert(output_buffer);
196 evbuffer_add(output_buffer, strRequest.data(), strRequest.size());
197
198 int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, "/");
199 req.release(); // ownership moved to evcon in above call
200 if (r != 0) {
201 throw CConnectionFailed("send http request failed");
202 }
203
204 event_base_dispatch(base.get());
205
206 if (response.status == 0)
207 throw CConnectionFailed("couldn't connect to server");
208 else if (response.status == HTTP_UNAUTHORIZED)
209 throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");

Callers 1

CommandLineRPCFunction · 0.70

Calls 15

GetArgFunction · 0.85
obtain_event_baseFunction · 0.85
obtain_evhttp_requestFunction · 0.85
GetAuthCookieFunction · 0.85
_Function · 0.85
GetConfigFileFunction · 0.85
EncodeBase64Function · 0.85
CConnectionFailedClass · 0.85
RPCPortMethod · 0.80
JSONRPCRequestClass · 0.70
getMethod · 0.45

Tested by

no test coverage detected