Skip to main content

read_body_capped

Function read_body_capped 

Source
pub async fn read_body_capped<S: ChunkSource>(
    source: &mut S,
    content_length: Option<u64>,
    cap: u64,
) -> Result<Vec<u8>, CompletionError>
Expand description

Reads a whole response body from source, refusing it once it would exceed cap bytes.

Use it for a body the transport decodes whole, such as the error body of a status outside the 2xx range or a JSON document like the gateway’s model list.

content_length is the length the response advertises, when the transport knows it. An advertised length over cap fails at once, before any chunk is read. The function also counts bytes as the chunks arrive, so a gateway that omits or misstates the length still cannot force an unbounded allocation before the body is decoded.

§Errors

Returns a MalformedResponse-kind CompletionError when the body would exceed cap, and the source’s own error when a read fails.