Skip to main content

docsrs_cli/http/
body.rs

1//! Response body budget streaming and UTF-8 decode.
2
3use bytes::Bytes;
4use futures_util::StreamExt;
5
6use crate::config::HARD_MAX_BODY_BYTES;
7use crate::error::{AppError, AppResult, ErrorDetail};
8
9/// Read a response body with a hard byte budget and fallible allocation.
10///
11/// Primary defense is [`HARD_MAX_BODY_BYTES`] (operators may only lower the cap).
12/// `try_reserve` / `try_reserve_exact` map allocation failure to
13/// [`crate::error::ErrorKind::Network`] instead of aborting via `with_capacity` on hostile sizes.
14/// On Linux overcommit, the allocator may still report success and the OOM killer
15/// can fire later — the hard ceiling remains the main bound.
16pub(super) async fn read_body_capped(resp: reqwest::Response, max_bytes: u64) -> AppResult<Bytes> {
17    // Never honor a budget above the product hard ceiling (defense in depth).
18    let max_bytes = max_bytes.min(HARD_MAX_BODY_BYTES);
19    // When Content-Length is known and already over budget, fail without buffering.
20    // (With gzip, length may be compressed size or absent; still a useful early guard.)
21    let content_length = resp.content_length();
22    if let Some(n) = content_length
23        && n > max_bytes
24    {
25        // Permanent local budget — not a transport failure; do not auto-retry.
26        return Err(AppError::of(ErrorDetail::BodyOverBudget { max_bytes }));
27    }
28    // Pre-size when Content-Length is present to avoid realloc churn on large docs pages.
29    // Prefer try_reserve_exact when length is known; never with_capacity on external size.
30    let capacity = content_length
31        .map(|n| (n as usize).min(max_bytes as usize))
32        .unwrap_or(0);
33    let mut stream = resp.bytes_stream();
34    let mut buf: Vec<u8> = Vec::new();
35    if capacity > 0 {
36        buf.try_reserve_exact(capacity).map_err(|e| {
37            AppError::of_with_source(ErrorDetail::BodyReserveFailed { bytes: capacity }, e)
38        })?;
39    }
40    while let Some(chunk) = stream.next().await {
41        let chunk = chunk.map_err(|e| AppError::of_with_source(ErrorDetail::BodyRead, e))?;
42        if (buf.len() as u64).saturating_add(chunk.len() as u64) > max_bytes {
43            // Permanent local budget — not a transport failure; do not auto-retry.
44            return Err(AppError::of(ErrorDetail::BodyOverBudget { max_bytes }));
45        }
46        // Grow with try_reserve when stream chunks exceed Content-Length estimate.
47        let need = chunk.len();
48        if buf.capacity().saturating_sub(buf.len()) < need {
49            buf.try_reserve(need).map_err(|e| {
50                AppError::of_with_source(ErrorDetail::BodyReserveFailed { bytes: need }, e)
51            })?;
52        }
53        buf.extend_from_slice(&chunk);
54    }
55    Ok(Bytes::from(buf))
56}
57
58/// Decode body as UTF-8, stripping a leading UTF-8 BOM.
59///
60/// # Errors
61///
62/// Returns [`crate::error::ErrorKind::Parse`] when the body is not valid UTF-8.
63pub fn decode_utf8(body: &Bytes) -> AppResult<String> {
64    let mut bytes = body.as_ref();
65    if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
66        bytes = &bytes[3..];
67    }
68    std::str::from_utf8(bytes)
69        .map(|s| s.to_string())
70        .map_err(|e| AppError::of_with_source(ErrorDetail::BodyNotUtf8, e))
71}