Repository: bitcoin/bitcoin
File: src/rest.cpp (C++)

Code before:
static bool rest_block_extended(const std::any& context, HTTPRequest* req, const std::string& uri_part)
{
    return rest_block(context, req, uri_part, TxVerbosity::SHOW_DETAILS_AND_PREVOUT, /*part_offset=*/std::nullopt, /*part_size=*/std::nullopt);
}

static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& uri_part)
{
    return rest_block(context, req, uri_part, TxVerbosity::SHOW_TXID, /*part_offset=*/std::nullopt, /*part_size=*/std::nullopt);
}

static bool rest_block_part(const std::any& context, HTTPRequest* req, const std::string& uri_part)
{
    std::optional<size_t> part_offset{};
    std::optional<size_t> part_size{};
    try {
        const auto part_offset_str = req->GetQueryParameter("offset");
        if (part_offset_str.has_value()) {
            part_offset = ToIntegral<size_t>(*part_offset_str);
            if (!part_offset.has_value()) {
                return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Block part offset is invalid: %s", *part_offset_str));
            }
        }
        const auto part_size_str = req->GetQueryParameter("size");
        if (part_size_str.has_value()) {
            part_size = ToIntegral<size_t>(*part_size_str);
            if (!part_size.has_value()) {
                return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Block part size is invalid: %s", *part_size_str));
            }
        }
    } catch (const std::runtime_error& e) {
        return RESTERR(req, HTTP_BAD_REQUEST, e.what());
    }
    return rest_block(context, req, uri_part, /*tx_verbosity=*/std::nullopt, part_offset, part_size);
}

static bool rest_filter_header(const std::any& context, HTTPRequest* req, const std::string& uri_part)
{
    if (!CheckWarmup(req)) return false;

    std::string param;
    const RESTResponseFormat rf = ParseDataFormat(param, uri_part);

    std::vector<std::string> uri_parts = SplitString(param, '/');
    std::string raw_count;
    std::string raw_blockhash;
    if (uri_parts.size() == 3) {
        // deprecated path: /rest/blockfilterheaders/<filtertype>/<count>/<blockhash>
        raw_blockhash = uri_parts[2];
        raw_count = uri_parts[1];
    } else if (uri_parts.size() == 2) {
        // new path with query parameter: /rest/blockfilterheaders/<filtertype>/<blockhash>?count=<count>

Review comment (suggestion):
```suggestion
            if (!part_size.has_value()) [[unlikely]] {
```


Code after:
static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& uri_part)
{
    return rest_block(context, req, uri_part, TxVerbosity::SHOW_TXID);
}

static bool rest_block_part(const std::any& context, HTTPRequest* req, const std::string& uri_part)
{
    try {
        if (const auto opt_offset{ToIntegral<size_t>(req->GetQueryParameter("offset").value_or(""))}) {
            if (const auto opt_size{ToIntegral<size_t>(req->GetQueryParameter("size").value_or(""))}) {
                return rest_block(context, req, uri_part,
                                  /*tx_verbosity=*/std::nullopt,
                                  /*block_part=*/{{*opt_offset, *opt_size}});
            } else {
                return RESTERR(req, HTTP_BAD_REQUEST, "Block part size missing or invalid");
            }
        } else {
            return RESTERR(req, HTTP_BAD_REQUEST, "Block part offset missing or invalid");
        }
    } catch (const std::runtime_error& e) {
        return RESTERR(req, HTTP_BAD_REQUEST, e.what());
    }
}

static bool rest_filter_header(const std::any& context, HTTPRequest* req, const std::string& uri_part)
{
    if (!CheckWarmup(req)) return false;

    std::string param;
    const RESTResponseFormat rf = ParseDataFormat(param, uri_part);

    std::vector<std::string> uri_parts = SplitString(param, '/');
    std::string raw_count;
    std::string raw_blockhash;
    if (uri_parts.size() == 3) {
        // deprecated path: /rest/blockfilterheaders/<filtertype>/<count>/<blockhash>
        raw_blockhash = uri_parts[2];
        raw_count = uri_parts[1];
    } else if (uri_parts.size() == 2) {
        // new path with query parameter: /rest/blockfilterheaders/<filtertype>/<blockhash>?count=<count>
        raw_blockhash = uri_parts[1];
        try {
            raw_count = req->GetQueryParameter("count").value_or("5");
        } catch (const std::runtime_error& e) {
            return RESTERR(req, HTTP_BAD_REQUEST, e.what());
        }
    } else {
        return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/blockfilterheaders/<filtertype>/<blockhash>.<ext>?count=<count>");
    }

    const auto parsed_count{ToIntegral<size_t>(raw_count)};