Changeset View
Changeset View
Standalone View
Standalone View
src/bitcoin-cli.cpp
Show First 20 Lines • Show All 367 Lines • ▼ Show 20 Lines | raii_evhttp_request req = | ||||
obtain_evhttp_request(http_request_done, (void *)&response); | obtain_evhttp_request(http_request_done, (void *)&response); | ||||
if (req == nullptr) throw std::runtime_error("create http request failed"); | if (req == nullptr) throw std::runtime_error("create http request failed"); | ||||
#if LIBEVENT_VERSION_NUMBER >= 0x02010300 | #if LIBEVENT_VERSION_NUMBER >= 0x02010300 | ||||
evhttp_request_set_error_cb(req.get(), http_error_cb); | evhttp_request_set_error_cb(req.get(), http_error_cb); | ||||
#endif | #endif | ||||
// Get credentials | // Get credentials | ||||
std::string strRPCUserColonPass; | std::string strRPCUserColonPass; | ||||
bool failedToGetAuthCookie = false; | |||||
if (gArgs.GetArg("-rpcpassword", "") == "") { | if (gArgs.GetArg("-rpcpassword", "") == "") { | ||||
// Try fall back to cookie-based authentication if no password is | // Try fall back to cookie-based authentication if no password is | ||||
// provided | // provided | ||||
if (!GetAuthCookie(&strRPCUserColonPass)) { | if (!GetAuthCookie(&strRPCUserColonPass)) { | ||||
throw std::runtime_error(strprintf( | failedToGetAuthCookie = true; | ||||
_("Could not locate RPC credentials. No authentication cookie " | |||||
"could be found, and RPC password is not set. See " | |||||
"-rpcpassword and -stdinrpcpass. Configuration file: (%s)"), | |||||
GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)) | |||||
.string() | |||||
.c_str())); | |||||
} | } | ||||
} else { | } else { | ||||
strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + | strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + | ||||
gArgs.GetArg("-rpcpassword", ""); | gArgs.GetArg("-rpcpassword", ""); | ||||
} | } | ||||
struct evkeyvalq *output_headers = | struct evkeyvalq *output_headers = | ||||
evhttp_request_get_output_headers(req.get()); | evhttp_request_get_output_headers(req.get()); | ||||
Show All 30 Lines | #endif | ||||
req.release(); | req.release(); | ||||
if (r != 0) { | if (r != 0) { | ||||
throw CConnectionFailed("send http request failed"); | throw CConnectionFailed("send http request failed"); | ||||
} | } | ||||
event_base_dispatch(base.get()); | event_base_dispatch(base.get()); | ||||
if (response.status == 0) { | if (response.status == 0) { | ||||
throw CConnectionFailed(strprintf( | std::string responseErrorMessage; | ||||
"couldn't connect to server: %s (code %d)\n(make sure server is " | if (response.error != -1) { | ||||
"running and you are connecting to the correct RPC port)", | responseErrorMessage = | ||||
http_errorstring(response.error), response.error)); | strprintf(" (error code %d - \"%s\")", response.error, | ||||
http_errorstring(response.error)); | |||||
} | |||||
throw CConnectionFailed( | |||||
strprintf("Could not connect to the server %s:%d%s\n\nMake sure " | |||||
"the bitcoind server is running and that you are " | |||||
"connecting to the correct RPC port.", | |||||
host, port, responseErrorMessage)); | |||||
} else if (response.status == HTTP_UNAUTHORIZED) { | } else if (response.status == HTTP_UNAUTHORIZED) { | ||||
if (failedToGetAuthCookie) { | |||||
throw std::runtime_error(strprintf( | |||||
_("Could not locate RPC credentials. No authentication cookie " | |||||
"could be found, and RPC password is not set. See " | |||||
"-rpcpassword and -stdinrpcpass. Configuration file: (%s)"), | |||||
GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)) | |||||
.string() | |||||
.c_str())); | |||||
} else { | |||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"incorrect rpcuser or rpcpassword (authorization failed)"); | "Authorization failed: Incorrect rpcuser or rpcpassword"); | ||||
} | |||||
} else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && | } else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && | ||||
response.status != HTTP_NOT_FOUND && | response.status != HTTP_NOT_FOUND && | ||||
response.status != HTTP_INTERNAL_SERVER_ERROR) { | response.status != HTTP_INTERNAL_SERVER_ERROR) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
strprintf("server returned HTTP error %d", response.status)); | strprintf("server returned HTTP error %d", response.status)); | ||||
} else if (response.body.empty()) { | } else if (response.body.empty()) { | ||||
throw std::runtime_error("no response from server"); | throw std::runtime_error("no response from server"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 158 Lines • Show Last 20 Lines |