| 207 | } |
| 208 | |
| 209 | static bool rest_block(HTTPRequest* req, |
| 210 | const std::string& strURIPart, |
| 211 | bool showTxDetails) |
| 212 | { |
| 213 | if (!CheckWarmup(req)) |
| 214 | return false; |
| 215 | std::string param, hashStr; |
| 216 | const RetFormat rf = ParseDataFormat(param, strURIPart); |
| 217 | std::vector<std::string> path; |
| 218 | boost::split(path, param, boost::is_any_of("/")); |
| 219 | bool legacy_format = false; |
| 220 | if (path.size() == 1) { |
| 221 | hashStr = path[0]; |
| 222 | } |
| 223 | else { |
| 224 | legacy_format = true; //use old rule if URI=/legacy/<BLOCK-HASH> |
| 225 | hashStr = path[1]; |
| 226 | } |
| 227 | |
| 228 | uint256 hash; |
| 229 | if (!ParseHashStr(hashStr, hash)) |
| 230 | return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr); |
| 231 | |
| 232 | CBlock block; |
| 233 | CBlockIndex* pblockindex = nullptr; |
| 234 | { |
| 235 | LOCK(cs_main); |
| 236 | pblockindex = LookupBlockIndex(hash); |
| 237 | if (!pblockindex) { |
| 238 | return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); |
| 239 | } |
| 240 | |
| 241 | if (IsBlockPruned(pblockindex)) |
| 242 | return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)"); |
| 243 | |
| 244 | if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) |
| 245 | return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); |
| 246 | } |
| 247 | int ser_flags = legacy_format ? SERIALIZE_BLOCK_LEGACY : 0; |
| 248 | CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags() | ser_flags); |
| 249 | ssBlock << block; |
| 250 | |
| 251 | switch (rf) { |
| 252 | case RetFormat::BINARY: { |
| 253 | std::string binaryBlock = ssBlock.str(); |
| 254 | req->WriteHeader("Content-Type", "application/octet-stream"); |
| 255 | req->WriteReply(HTTP_OK, binaryBlock); |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | case RetFormat::HEX: { |
| 260 | std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n"; |
| 261 | req->WriteHeader("Content-Type", "text/plain"); |
| 262 | req->WriteReply(HTTP_OK, strHex); |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | case RetFormat::JSON: { |
no test coverage detected