Changeset View
Changeset View
Standalone View
Standalone View
src/rest.cpp
Show First 20 Lines • Show All 652 Lines • ▼ Show 20 Lines | switch (rf) { | ||||
default: { | default: { | ||||
return RESTERR(req, HTTP_NOT_FOUND, | return RESTERR(req, HTTP_NOT_FOUND, | ||||
"output format not found (available: " + | "output format not found (available: " + | ||||
AvailableDataFormatsString() + ")"); | AvailableDataFormatsString() + ")"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static bool rest_blockhash_by_height(Config &config, HTTPRequest *req, | |||||
const std::string &str_uri_part) { | |||||
if (!CheckWarmup(req)) { | |||||
return false; | |||||
} | |||||
std::string height_str; | |||||
const RetFormat rf = ParseDataFormat(height_str, str_uri_part); | |||||
int32_t blockheight; | |||||
if (!ParseInt32(height_str, &blockheight) || blockheight < 0) { | |||||
return RESTERR(req, HTTP_BAD_REQUEST, | |||||
"Invalid height: " + SanitizeString(height_str)); | |||||
} | |||||
CBlockIndex *pblockindex = nullptr; | |||||
{ | |||||
LOCK(cs_main); | |||||
if (blockheight > ::ChainActive().Height()) { | |||||
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range"); | |||||
} | |||||
pblockindex = ::ChainActive()[blockheight]; | |||||
} | |||||
switch (rf) { | |||||
case RetFormat::BINARY: { | |||||
CDataStream ss_blockhash(SER_NETWORK, PROTOCOL_VERSION); | |||||
ss_blockhash << pblockindex->GetBlockHash(); | |||||
req->WriteHeader("Content-Type", "application/octet-stream"); | |||||
req->WriteReply(HTTP_OK, ss_blockhash.str()); | |||||
return true; | |||||
} | |||||
case RetFormat::HEX: { | |||||
req->WriteHeader("Content-Type", "text/plain"); | |||||
req->WriteReply(HTTP_OK, | |||||
pblockindex->GetBlockHash().GetHex() + "\n"); | |||||
return true; | |||||
} | |||||
case RetFormat::JSON: { | |||||
req->WriteHeader("Content-Type", "application/json"); | |||||
UniValue resp = UniValue(UniValue::VOBJ); | |||||
resp.pushKV("blockhash", pblockindex->GetBlockHash().GetHex()); | |||||
req->WriteReply(HTTP_OK, resp.write() + "\n"); | |||||
return true; | |||||
} | |||||
default: { | |||||
return RESTERR(req, HTTP_NOT_FOUND, | |||||
"output format not found (available: " + | |||||
AvailableDataFormatsString() + ")"); | |||||
} | |||||
} | |||||
} | |||||
static const struct { | static const struct { | ||||
const char *prefix; | const char *prefix; | ||||
bool (*handler)(Config &config, HTTPRequest *req, | bool (*handler)(Config &config, HTTPRequest *req, | ||||
const std::string &strReq); | const std::string &strReq); | ||||
} uri_prefixes[] = { | } uri_prefixes[] = { | ||||
{"/rest/tx/", rest_tx}, | {"/rest/tx/", rest_tx}, | ||||
{"/rest/block/notxdetails/", rest_block_notxdetails}, | {"/rest/block/notxdetails/", rest_block_notxdetails}, | ||||
{"/rest/block/", rest_block_extended}, | {"/rest/block/", rest_block_extended}, | ||||
{"/rest/chaininfo", rest_chaininfo}, | {"/rest/chaininfo", rest_chaininfo}, | ||||
{"/rest/mempool/info", rest_mempool_info}, | {"/rest/mempool/info", rest_mempool_info}, | ||||
{"/rest/mempool/contents", rest_mempool_contents}, | {"/rest/mempool/contents", rest_mempool_contents}, | ||||
{"/rest/headers/", rest_headers}, | {"/rest/headers/", rest_headers}, | ||||
{"/rest/getutxos", rest_getutxos}, | {"/rest/getutxos", rest_getutxos}, | ||||
{"/rest/blockhashbyheight/", rest_blockhash_by_height}, | |||||
}; | }; | ||||
void StartREST() { | void StartREST() { | ||||
for (size_t i = 0; i < ARRAYLEN(uri_prefixes); i++) { | for (size_t i = 0; i < ARRAYLEN(uri_prefixes); i++) { | ||||
RegisterHTTPHandler(uri_prefixes[i].prefix, false, | RegisterHTTPHandler(uri_prefixes[i].prefix, false, | ||||
uri_prefixes[i].handler); | uri_prefixes[i].handler); | ||||
} | } | ||||
} | } | ||||
void InterruptREST() {} | void InterruptREST() {} | ||||
void StopREST() { | void StopREST() { | ||||
for (size_t i = 0; i < ARRAYLEN(uri_prefixes); i++) { | for (size_t i = 0; i < ARRAYLEN(uri_prefixes); i++) { | ||||
UnregisterHTTPHandler(uri_prefixes[i].prefix, false); | UnregisterHTTPHandler(uri_prefixes[i].prefix, false); | ||||
} | } | ||||
} | } |