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, ErrorKind};
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/// [`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(
17    resp: reqwest::Response,
18    max_bytes: u64,
19) -> AppResult<Bytes> {
20    // Never honor a budget above the product hard ceiling (defense in depth).
21    let max_bytes = max_bytes.min(HARD_MAX_BODY_BYTES);
22    // When Content-Length is known and already over budget, fail without buffering.
23    // (With gzip, length may be compressed size or absent; still a useful early guard.)
24    let content_length = resp.content_length();
25    if let Some(n) = content_length
26        && n > max_bytes
27    {
28        // Permanent local budget — not a transport failure; do not auto-retry.
29        return Err(AppError::new(
30            ErrorKind::Budget,
31            format!("response body exceeds max_body_bytes ({max_bytes})"),
32        ));
33    }
34    // Pre-size when Content-Length is present to avoid realloc churn on large docs pages.
35    // Prefer try_reserve_exact when length is known; never with_capacity on external size.
36    let capacity = content_length
37        .map(|n| (n as usize).min(max_bytes as usize))
38        .unwrap_or(0);
39    let mut stream = resp.bytes_stream();
40    let mut buf: Vec<u8> = Vec::new();
41    if capacity > 0 {
42        buf.try_reserve_exact(capacity).map_err(|e| {
43            AppError::with_source(
44                ErrorKind::Network,
45                format!("failed to reserve {capacity} bytes for response body"),
46                e,
47            )
48        })?;
49    }
50    while let Some(chunk) = stream.next().await {
51        let chunk = chunk.map_err(|e| {
52            AppError::with_source(ErrorKind::Network, "failed reading response body", e)
53        })?;
54        if (buf.len() as u64).saturating_add(chunk.len() as u64) > max_bytes {
55            // Permanent local budget — not a transport failure; do not auto-retry.
56            return Err(AppError::new(
57                ErrorKind::Budget,
58                format!("response body exceeds max_body_bytes ({max_bytes})"),
59            ));
60        }
61        // Grow with try_reserve when stream chunks exceed Content-Length estimate.
62        let need = chunk.len();
63        if buf.capacity().saturating_sub(buf.len()) < need {
64            buf.try_reserve(need).map_err(|e| {
65                AppError::with_source(
66                    ErrorKind::Network,
67                    format!("failed to reserve {need} more body bytes"),
68                    e,
69                )
70            })?;
71        }
72        buf.extend_from_slice(&chunk);
73    }
74    Ok(Bytes::from(buf))
75}
76
77/// Decode body as UTF-8, stripping a leading UTF-8 BOM.
78///
79/// # Errors
80///
81/// Returns [`ErrorKind::Parse`] when the body is not valid UTF-8.
82pub fn decode_utf8(body: &Bytes) -> AppResult<String> {
83    let mut bytes = body.as_ref();
84    if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
85        bytes = &bytes[3..];
86    }
87    std::str::from_utf8(bytes)
88        .map(|s| s.to_string())
89        .map_err(|e| AppError::with_source(ErrorKind::Parse, "response body is not valid UTF-8", e))
90}