Skip to main content

better_fetch/
streaming.rs

1//! Streaming HTTP responses (`send_stream`).
2//!
3//! Use [`RequestBuilder::send_stream`](crate::RequestBuilder::send_stream) for large or chunked
4//! bodies. The buffered [`Response`](crate::Response) from [`RequestBuilder::send`](crate::RequestBuilder::send)
5//! remains the default for JSON APIs.
6
7use std::path::Path;
8use std::pin::Pin;
9use std::task::{Context, Poll};
10
11use bytes::{Bytes, BytesMut};
12use futures_util::{Future, Stream};
13use http::{HeaderMap, StatusCode};
14
15use crate::cancel::CancellationToken;
16use crate::error::Error;
17use crate::response::Response;
18use crate::Result;
19use tokio_util::sync::WaitForCancellationFutureOwned;
20
21/// Byte stream yielding `Result<Bytes>` chunks from the transport.
22pub type BodyStream = Pin<Box<dyn Stream<Item = Result<Bytes>> + Send + Sync>>;
23
24/// HTTP response with a streaming body.
25///
26/// Status and headers are available immediately. Consume the body via [`Self::bytes_stream`]
27/// or buffer it with [`Self::collect`].
28///
29/// # Examples
30///
31/// ```no_run
32/// # use better_fetch::{Client, Result};
33/// # use futures_util::StreamExt;
34/// # #[tokio::main]
35/// # async fn main() -> Result<()> {
36/// let client = Client::new("https://httpbin.org")?;
37/// let mut stream = client.get("/stream/5").send_stream().await?;
38/// while let Some(chunk) = stream.bytes_stream().next().await {
39///     let chunk = chunk?;
40///     println!("got {} bytes", chunk.len());
41/// }
42/// # Ok(())
43/// # }
44/// ```
45pub struct StreamingResponse {
46    status: StatusCode,
47    headers: HeaderMap,
48    url: Option<url::Url>,
49    body: BodyStream,
50    max_response_bytes: Option<u64>,
51    #[cfg(feature = "json")]
52    json_parser: Option<crate::json_parser::JsonParserFn>,
53    #[cfg(feature = "schema-validate")]
54    response_schema: Option<crate::schema_validate::StreamResponseSchemaCtx>,
55}
56
57impl StreamingResponse {
58    pub(crate) fn new(
59        status: StatusCode,
60        headers: HeaderMap,
61        body: BodyStream,
62        url: Option<url::Url>,
63        max_response_bytes: Option<u64>,
64        #[cfg(feature = "json")] json_parser: Option<crate::json_parser::JsonParserFn>,
65        #[cfg(feature = "schema-validate")] response_schema: Option<
66            crate::schema_validate::StreamResponseSchemaCtx,
67        >,
68    ) -> Self {
69        Self {
70            status,
71            headers,
72            url,
73            body,
74            max_response_bytes,
75            #[cfg(feature = "json")]
76            json_parser,
77            #[cfg(feature = "schema-validate")]
78            response_schema,
79        }
80    }
81
82    /// HTTP status code.
83    pub fn status(&self) -> StatusCode {
84        self.status
85    }
86
87    /// Response headers.
88    pub fn headers(&self) -> &HeaderMap {
89        &self.headers
90    }
91
92    /// Final request URL when available.
93    pub fn url(&self) -> Option<&url::Url> {
94        self.url.as_ref()
95    }
96
97    /// Returns `true` for 2xx status codes.
98    pub fn is_success(&self) -> bool {
99        self.status.is_success()
100    }
101
102    /// Returns an error if the status is not success (does not read the body).
103    #[must_use = "call `?` or handle the error explicitly"]
104    pub fn error_for_status(&self) -> Result<()> {
105        if self.status.is_success() {
106            return Ok(());
107        }
108        Err(Error::http_error_for_status(self.status, None))
109    }
110
111    /// Mutable reference to the response body stream.
112    pub fn bytes_stream(&mut self) -> &mut BodyStream {
113        &mut self.body
114    }
115
116    /// Buffers the full body into a [`Response`].
117    ///
118    /// Respects [`ClientBuilder::max_response_bytes`](crate::ClientBuilder::max_response_bytes) when
119    /// configured on the request or client (the limit is enforced on the underlying stream).
120    ///
121    /// # Examples
122    ///
123    /// ```no_run
124    /// # use better_fetch::{Client, Result};
125    /// # #[tokio::main]
126    /// # async fn main() -> Result<()> {
127    /// let client = Client::new("https://api.example.com")?;
128    /// let buffered = client.get("/data").send_stream().await?.collect().await?;
129    /// let text = buffered.into_text()?;
130    /// # Ok(())
131    /// # }
132    /// ```
133    pub async fn collect(self) -> Result<Response> {
134        self.error_for_status()?;
135        let bytes = accumulate_stream(self.body, self.max_response_bytes).await?;
136        let response = Response::new(
137            self.status,
138            self.headers,
139            bytes,
140            self.url,
141            #[cfg(feature = "json")]
142            self.json_parser,
143        );
144        #[cfg(feature = "schema-validate")]
145        if let Some(ctx) = self.response_schema {
146            crate::schema_validate::validate_response_if_registered(
147                &ctx.registry,
148                &ctx.route_path,
149                &ctx.method,
150                &response,
151            )?;
152        }
153        Ok(response)
154    }
155
156    /// Splits into status, headers, and the body stream.
157    pub fn into_parts(self) -> (StatusCode, HeaderMap, BodyStream) {
158        (self.status, self.headers, self.body)
159    }
160
161    /// Writes the response body to `path`, returning the number of bytes written.
162    ///
163    /// Enforces `max_bytes` when set (same semantics as [`accumulate_stream`](crate::streaming::accumulate_stream)).
164    /// Checks for success status before writing.
165    pub async fn stream_to_file(
166        mut self,
167        path: impl AsRef<Path>,
168        max_bytes: Option<u64>,
169    ) -> Result<u64> {
170        use futures_util::StreamExt;
171        use tokio::io::AsyncWriteExt;
172
173        self.error_for_status()?;
174        let limit = max_bytes.or(self.max_response_bytes);
175        let mut file = tokio::fs::File::create(path.as_ref())
176            .await
177            .map_err(|e| Error::Io(format!("create file: {e}")))?;
178        let mut written: u64 = 0;
179
180        while let Some(chunk) = self.body.next().await {
181            let chunk = chunk?;
182            let chunk_len = u64::try_from(chunk.len())
183                .map_err(|_| Error::Config("chunk size overflow".into()))?;
184            let new_written = written
185                .checked_add(chunk_len)
186                .ok_or_else(|| Error::Config("response body length overflow".into()))?;
187            if let Some(limit) = limit {
188                if new_written > limit {
189                    return Err(Error::BodyTooLarge { limit });
190                }
191            }
192            file.write_all(&chunk)
193                .await
194                .map_err(|e| Error::Io(format!("write file: {e}")))?;
195            written = new_written;
196        }
197
198        file.flush()
199            .await
200            .map_err(|e| Error::Io(format!("flush file: {e}")))?;
201        Ok(written)
202    }
203
204    /// Buffers the stream (up to `max_bytes`) and parses `text/event-stream` events.
205    pub async fn read_sse_events(
206        self,
207        max_bytes: Option<u64>,
208    ) -> Result<Vec<crate::sse::SseEvent>> {
209        crate::sse::read_sse_from_bytes(self.body, max_bytes.or(self.max_response_bytes)).await
210    }
211
212    /// Incrementally parses SSE events from the response body as a [`Stream`](futures_util::Stream).
213    ///
214    /// Respects `max_bytes` when set on the request (same as [`Self::collect`]).
215    pub fn sse_events(self) -> crate::sse::SseEventStream {
216        crate::sse::SseEventStream::new(self.body, self.max_response_bytes)
217    }
218}
219
220impl std::fmt::Debug for StreamingResponse {
221    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
222        f.debug_struct("StreamingResponse")
223            .field("status", &self.status)
224            .field("headers", &self.headers)
225            .field("url", &self.url)
226            .field("body", &"<stream>")
227            .finish()
228    }
229}
230
231pub(crate) fn wrap_max_bytes(stream: BodyStream, limit: u64) -> BodyStream {
232    Box::pin(MaxBytesStream {
233        inner: stream,
234        limit,
235        read: 0,
236        limit_hit: false,
237    })
238}
239
240pub(crate) fn wrap_cancellation(stream: BodyStream, token: CancellationToken) -> BodyStream {
241    Box::pin(CancelBodyStream {
242        inner: stream,
243        cancelled: token.cancelled_owned(),
244    })
245}
246
247/// Default maximum bytes read from a streaming body when evaluating a custom retry predicate.
248pub(crate) const RETRY_BODY_PEEK_DEFAULT: u64 = 64 * 1024;
249
250/// Reads up to `limit` bytes from `body` for retry predicate evaluation.
251pub(crate) async fn drain_body_for_retry(body: BodyStream, limit: u64) -> Result<Bytes> {
252    accumulate_stream(body, Some(limit)).await
253}
254
255/// Accumulates a body stream into a single buffer, optionally enforcing `limit`.
256pub(crate) async fn accumulate_stream(mut body: BodyStream, limit: Option<u64>) -> Result<Bytes> {
257    use futures_util::StreamExt;
258
259    let mut buf = BytesMut::new();
260    while let Some(chunk) = body.next().await {
261        let chunk = chunk?;
262        let new_len = buf
263            .len()
264            .checked_add(chunk.len())
265            .ok_or_else(|| Error::Config("response body length overflow".into()))?;
266        if let Some(limit) = limit {
267            if new_len as u64 > limit {
268                return Err(Error::BodyTooLarge { limit });
269            }
270        }
271        buf.reserve(chunk.len());
272        buf.extend_from_slice(&chunk);
273        debug_assert_eq!(buf.len(), new_len);
274    }
275    Ok(buf.freeze())
276}
277
278/// Creates a single-chunk body stream from bytes.
279pub fn body_stream_from_bytes(bytes: Bytes) -> BodyStream {
280    Box::pin(futures_util::stream::once(async move { Ok(bytes) }))
281}
282
283struct MaxBytesStream {
284    inner: BodyStream,
285    limit: u64,
286    read: u64,
287    /// Set after the first [`Error::BodyTooLarge`]; further polls end the stream.
288    limit_hit: bool,
289}
290
291impl Stream for MaxBytesStream {
292    type Item = Result<Bytes>;
293
294    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
295        if self.limit_hit {
296            return Poll::Ready(None);
297        }
298
299        match Pin::new(&mut self.inner).poll_next(cx) {
300            Poll::Ready(Some(Ok(chunk))) => {
301                let chunk_len = u64::try_from(chunk.len()).unwrap_or(u64::MAX);
302                let new_read = self.read.saturating_add(chunk_len);
303                if new_read > self.limit {
304                    self.limit_hit = true;
305                    // Drop `chunk` without yielding it; caller must stop after the error.
306                    return Poll::Ready(Some(Err(Error::BodyTooLarge { limit: self.limit })));
307                }
308                self.read = new_read;
309                Poll::Ready(Some(Ok(chunk)))
310            }
311            other => other,
312        }
313    }
314}
315
316pin_project_lite::pin_project! {
317    struct CancelBodyStream {
318        #[pin]
319        inner: BodyStream,
320        #[pin]
321        cancelled: WaitForCancellationFutureOwned,
322    }
323}
324
325impl Stream for CancelBodyStream {
326    type Item = Result<Bytes>;
327
328    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
329        let mut this = self.project();
330        if this.cancelled.as_mut().poll(cx).is_ready() {
331            return Poll::Ready(Some(Err(Error::Cancelled)));
332        }
333        match this.inner.poll_next(cx) {
334            Poll::Ready(item) => Poll::Ready(item),
335            Poll::Pending => {
336                let _ = this.cancelled.as_mut().poll(cx);
337                Poll::Pending
338            }
339        }
340    }
341}
342
343#[cfg(test)]
344mod tests {
345    use super::*;
346    use futures_util::{stream, StreamExt};
347
348    fn stream_from_chunks(chunks: Vec<Result<Bytes>>) -> BodyStream {
349        Box::pin(stream::iter(chunks))
350    }
351
352    #[tokio::test]
353    async fn max_bytes_ends_stream_after_limit_error() {
354        let inner = stream_from_chunks(vec![
355            Ok(Bytes::from_static(b"1234")),
356            Ok(Bytes::from_static(b"5678")),
357        ]);
358        let mut limited = wrap_max_bytes(inner, 5);
359
360        let first = limited.next().await.unwrap().unwrap();
361        assert_eq!(first.as_ref(), b"1234");
362
363        let err = limited.next().await.unwrap().unwrap_err();
364        assert!(err.is_body_too_large());
365        assert_eq!(err.body_too_large_limit(), Some(5));
366
367        // Must not replay the oversized chunk or spin forever.
368        assert!(limited.next().await.is_none());
369        assert!(limited.next().await.is_none());
370    }
371
372    #[tokio::test]
373    async fn max_bytes_allows_exact_limit() {
374        let inner = stream_from_chunks(vec![
375            Ok(Bytes::from_static(b"abc")),
376            Ok(Bytes::from_static(b"de")),
377        ]);
378        let mut limited = wrap_max_bytes(inner, 5);
379        assert_eq!(limited.next().await.unwrap().unwrap().as_ref(), b"abc");
380        assert_eq!(limited.next().await.unwrap().unwrap().as_ref(), b"de");
381        assert!(limited.next().await.is_none());
382    }
383
384    #[tokio::test]
385    async fn cancel_wakes_pending_inner_read() {
386        use std::sync::atomic::{AtomicBool, Ordering};
387        use std::sync::Arc;
388
389        let released = Arc::new(AtomicBool::new(false));
390        let released_cb = released.clone();
391        let inner: BodyStream = Box::pin(futures_util::stream::poll_fn(move |cx| {
392            if released_cb.load(Ordering::SeqCst) {
393                return Poll::Ready(None);
394            }
395            cx.waker().wake_by_ref();
396            Poll::Pending
397        }));
398
399        let token = CancellationToken::new();
400        let cancel = token.clone();
401        let mut wrapped = wrap_cancellation(inner, token);
402
403        let read = tokio::spawn(async move {
404            use futures_util::StreamExt;
405            wrapped.next().await
406        });
407
408        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
409        cancel.cancel();
410        released.store(true, Ordering::SeqCst);
411
412        let item = read.await.unwrap();
413        assert!(matches!(item, Some(Err(e)) if e.is_cancelled()));
414    }
415
416    #[tokio::test]
417    async fn cancel_checked_between_chunks() {
418        let inner = stream_from_chunks(vec![
419            Ok(Bytes::from_static(b"a")),
420            Ok(Bytes::from_static(b"b")),
421        ]);
422        let token = CancellationToken::new();
423        let cancel = token.clone();
424        let mut wrapped = wrap_cancellation(inner, token);
425
426        assert_eq!(wrapped.next().await.unwrap().unwrap().as_ref(), b"a");
427        cancel.cancel();
428        let err = wrapped.next().await.unwrap().unwrap_err();
429        assert!(err.is_cancelled());
430    }
431}