pub async fn read_response_bounded(
response: Response,
max_size: usize,
) -> AptosResult<Vec<u8>>Expand description
Reads a response body with an enforced size limit, aborting early if exceeded.
Unlike response.bytes().await? which buffers the entire response in memory
before any size check, this function:
- Pre-checks the
Content-Lengthheader (if present) to reject obviously oversized responses before reading any body data. - Reads the body incrementally via chunked streaming, aborting as soon as
the accumulated size exceeds
max_size.
This prevents memory exhaustion from malicious servers that send huge
responses (including chunked transfer-encoding without Content-Length).
ยงErrors
Returns AptosError::Api with error code RESPONSE_TOO_LARGE if the
response body exceeds max_size bytes.