18#include <boost/algorithm/string.hpp>
58 const char *
Name()
override {
return "HTTP"; }
83 int code =
objError.find_value(
"code").getInt<
int>();
93 req->
WriteHeader(
"Content-Type",
"application/json");
110 std::vector<std::string>
vFields;
125 static const unsigned int KEY_SIZE = 32;
150 if (
strAuth.substr(0, 6) !=
"Basic ") {
207 std::pair<bool, std::string> method =
208 req->
GetHeader(
"access-control-request-method");
221 req->
GetHeader(
"access-control-request-headers");
228 if (method.second !=
"POST") {
245 req->
WriteHeader(
"Access-Control-Allow-Credentials",
"true");
259 req->
WriteHeader(
"Access-Control-Allow-Methods", method.second);
285 req->
WriteHeader(
"Access-Control-Allow-Credentials",
"true");
290 req->
WriteHeader(
"Access-Control-Expose-Headers",
"WWW-Authenticate");
304 "JSONRPC server handles only POST requests");
319 LogPrintf(
"ThreadRPCServer incorrect password attempt from %s\n",
348 LogPrintf(
"RPC User %s not allowed to call any methods\n",
358 LogPrintf(
"RPC User %s not allowed to call method %s\n",
375 "Invalid Request object");
379 std::string strMethod =
383 "RPC User %s not allowed to call method %s\n",
384 jreq.authUser, strMethod);
397 req->
WriteHeader(
"Content-Type",
"application/json");
402 }
catch (
const std::exception &
e) {
411 LogPrintf(
"Using random cookie authentication.\n");
416 LogPrintf(
"Config options rpcuser and rpcpassword will soon be "
417 "deprecated. Locally-run instances may remove rpcuser to use "
418 "cookie-based auth, or may be replaced with rpcauth. Please "
419 "see share/rpcauth for rpcauth auth generation.\n");
427 LogPrintf(
"Using rpcauth authentication.\n");
437 if (pos != std::string::npos) {
443 std::set_intersection(
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
A hasher class for HMAC-SHA-256.
CHMAC_SHA256 & Write(const uint8_t *data, size_t len)
void Finalize(uint8_t hash[OUTPUT_SIZE])
std::string ToString() const
void trigger(struct timeval *tv)
Trigger the event.
bool ProcessHTTPRequest(HTTPRequest *request)
static bool DelegateHTTPRequest(HTTPRPCRequestProcessor *requestProcessor, HTTPRequest *request)
Simple one-shot callback timer to be used by the RPC mechanism to e.g.
HTTPRPCTimer(struct event_base *eventBase, std::function< void()> &func, int64_t millis)
const char * Name() override
Implementation name.
RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis) override
Factory function for timers.
HTTPRPCTimerInterface(struct event_base *_base)
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Get the request header specified by hdr, or an empty string.
std::string GetURI() const
Get requested URI.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
RequestMethod GetRequestMethod() const
Get request method.
std::string ReadBody()
Read request body.
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
UniValue ExecuteCommand(const Config &config, const JSONRPCRequest &request) const
Attempts to execute an RPC command from the given request.
Opaque base class for timers returned by NewTimerFunc.
const std::string & get_str() const
const UniValue & find_value(std::string_view key) const
const UniValue & get_obj() const
virtual bool HasWalletSupport() const =0
Is the wallet component enabled.
const WalletInitInterface & g_wallet_init_interface
static bool checkCORS(HTTPRequest *req)
static const char * WWW_AUTH_HEADER_DATA
WWW-Authenticate to present with 401 Unauthorized response.
void InterruptHTTPRPC()
Interrupt HTTP RPC subsystem.
void StopHTTPRPC()
Stop HTTP RPC subsystem.
static std::unique_ptr< HTTPRPCTimerInterface > httpRPCTimerInterface
static std::string strRPCCORSDomain
static bool multiUserAuthorized(std::string strUserPass)
static bool g_rpc_whitelist_default
static bool RPCAuthorized(const std::string &strAuth, std::string &strAuthUsernameOut)
static std::string strRPCUserColonPass
static bool InitRPCAuthentication()
static std::map< std::string, std::set< std::string > > g_rpc_whitelist
bool StartHTTPRPC(HTTPRPCRequestProcessor &httpRPCRequestProcessor)
Start HTTP RPC subsystem.
static const int64_t RPC_AUTH_BRUTE_FORCE_DELAY
RPC auth failure delay to make brute-forcing expensive.
static void JSONErrorReply(HTTPRequest *req, const UniValue &objError, const UniValue &id)
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
static struct event_base * eventBase
HTTP module state.
struct event_base * EventBase()
Return evhttp event base.
#define LogPrint(category,...)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
UniValue JSONRPCError(int code, const std::string &message)
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
@ HTTP_INTERNAL_SERVER_ERROR
@ RPC_INVALID_REQUEST
Standard JSON-RPC 2.0 errors.
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
std::string JSONRPCExecBatch(const Config &config, RPCServer &rpcServer, const JSONRPCRequest &jreq, const UniValue &vReq)
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
void UninterruptibleSleep(const std::chrono::microseconds &n)
const UniValue NullUniValue
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::optional< std::vector< uint8_t > > DecodeBase64(std::string_view str)