nautilus_network/http/
stream.rs1use bytes::Bytes;
19use http::StatusCode;
20use http_body_util::BodyExt;
21use hyper::body::Incoming;
22
23use super::{HttpClientError, client::REQUEST_TIMEOUT_MESSAGE, error::transport_error};
24use crate::dst::time::Instant;
25
26#[derive(Debug)]
32pub struct HttpResponseStream {
33 pub(super) response: http::Response<Incoming>,
34 pub(super) deadline: Option<Instant>,
35 pub(super) origin: Option<String>,
36 #[cfg(all(feature = "simulation", madsim))]
37 pub(super) _connection: super::simulation::Connection,
38}
39
40impl HttpResponseStream {
41 #[must_use]
43 pub fn status(&self) -> StatusCode {
44 self.response.status()
45 }
46
47 pub async fn chunk(&mut self) -> Result<Option<Bytes>, HttpClientError> {
53 read_chunk(self.response.body_mut(), self.deadline)
54 .await
55 .map_err(|e| response_error(e, self.origin.as_deref()))
56 }
57}
58
59pub(super) async fn read_chunk<B>(
60 body: &mut B,
61 deadline: Option<Instant>,
62) -> Result<Option<Bytes>, HttpClientError>
63where
64 B: http_body::Body<Data = Bytes> + Unpin,
65 B::Error: std::error::Error + 'static,
66{
67 loop {
68 let frame = match deadline {
69 Some(deadline) => {
70 if Instant::now() >= deadline {
71 return Err(HttpClientError::TimeoutError(
72 REQUEST_TIMEOUT_MESSAGE.into(),
73 ));
74 }
75 tokio::select! {
76 biased;
77 () = crate::dst::time::sleep_until(deadline) => return Err(HttpClientError::TimeoutError(REQUEST_TIMEOUT_MESSAGE.into())),
78 frame = body.frame() => frame,
79 }
80 }
81 None => body.frame().await,
82 };
83 let Some(frame) = frame else {
84 return Ok(None);
85 };
86
87 if let Ok(chunk) = frame.map_err(|e| transport_error(&e))?.into_data() {
88 return Ok(Some(chunk));
89 }
90 }
91}
92
93pub(super) fn response_error(error: HttpClientError, origin: Option<&str>) -> HttpClientError {
94 match (error, origin) {
95 (HttpClientError::TransportError(message), Some(origin)) => {
96 HttpClientError::TransportError(format!("{message} for origin ({origin})"))
97 }
98 (error, _) => error,
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use std::io;
105
106 use http::{HeaderMap, HeaderValue, header::HeaderName};
107 use http_body::Frame;
108 use http_body_util::StreamBody;
109 use rstest::rstest;
110
111 use super::*;
112
113 #[tokio::test]
114 async fn read_chunk_skips_trailers_and_preserves_data() {
115 let trailers = HeaderMap::from_iter([(
116 HeaderName::from_static("x-checksum"),
117 HeaderValue::from_static("receipt-83"),
118 )]);
119 let frames: Vec<Result<_, io::Error>> = vec![
120 Ok(Frame::data(Bytes::from_static(b"first"))),
121 Ok(Frame::data(Bytes::from_static(b"second"))),
122 Ok(Frame::trailers(trailers)),
123 ];
124 let mut body = StreamBody::new(futures_util::stream::iter(frames));
125
126 let first = read_chunk(&mut body, None).await.unwrap();
127 let second = read_chunk(&mut body, None).await.unwrap();
128 let end = read_chunk(&mut body, None).await.unwrap();
129
130 assert_eq!(first, Some(Bytes::from_static(b"first")));
131 assert_eq!(second, Some(Bytes::from_static(b"second")));
132 assert_eq!(end, None);
133 }
134
135 #[rstest]
136 #[case::transport(io::ErrorKind::UnexpectedEof, false)]
137 #[case::timeout(io::ErrorKind::TimedOut, true)]
138 #[tokio::test]
139 async fn read_chunk_propagates_body_error(#[case] kind: io::ErrorKind, #[case] timeout: bool) {
140 let frames = vec![Err::<Frame<Bytes>, _>(io::Error::new(kind, "body failure"))];
141 let mut body = StreamBody::new(futures_util::stream::iter(frames));
142
143 let error = read_chunk(&mut body, None).await.unwrap_err();
144
145 match (error, timeout) {
146 (HttpClientError::TimeoutError(message), true)
147 | (HttpClientError::TransportError(message), false) => {
148 assert_eq!(message, "body failure");
149 }
150 (error, _) => panic!("unexpected classification: {error:?}"),
151 }
152 }
153}