1use bytes::Bytes;
4use futures_util::StreamExt;
5
6use crate::config::HARD_MAX_BODY_BYTES;
7use crate::error::{AppError, AppResult, ErrorKind};
8
9pub(super) async fn read_body_capped(
17 resp: reqwest::Response,
18 max_bytes: u64,
19) -> AppResult<Bytes> {
20 let max_bytes = max_bytes.min(HARD_MAX_BODY_BYTES);
22 let content_length = resp.content_length();
25 if let Some(n) = content_length
26 && n > max_bytes
27 {
28 return Err(AppError::new(
30 ErrorKind::Budget,
31 format!("response body exceeds max_body_bytes ({max_bytes})"),
32 ));
33 }
34 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 return Err(AppError::new(
57 ErrorKind::Budget,
58 format!("response body exceeds max_body_bytes ({max_bytes})"),
59 ));
60 }
61 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
77pub 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}