1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! HTTP body types for streaming cache support.
//!
//! This module provides the [`StreamingBody`] type which allows HTTP cache middleware
//! to handle both cached (buffered) responses and streaming responses from upstream
//! servers without requiring full buffering of large responses.
//! This implementation provides efficient streaming capabilities for HTTP caching.
#![allow(missing_docs)]
use std::{
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
use http_body::{Body, Frame};
use pin_project_lite::pin_project;
use crate::error::StreamingError;
#[cfg(feature = "streaming")]
pin_project! {
/// A body type that can represent either buffered data from cache, streaming body from upstream,
/// or streaming from a file for file-based caching.
///
/// This enum allows the HTTP cache middleware to efficiently handle:
/// - Cached responses (buffered data)
/// - Cache misses (streaming from upstream)
/// - File-based cached responses (streaming from disk)
///
/// # Variants
///
/// - [`Buffered`](StreamingBody::Buffered): Contains cached response data that can be sent immediately
/// - [`Streaming`](StreamingBody::Streaming): Wraps an upstream body for streaming responses
/// - [`File`](StreamingBody::File): Streams directly from a file for zero-copy caching
///
/// # Example
///
/// ```rust
/// use http_cache::StreamingBody;
/// use bytes::Bytes;
/// use http_body_util::Full;
///
/// // Cached response - sent immediately from memory
/// let cached: StreamingBody<Full<Bytes>> = StreamingBody::buffered(Bytes::from("Hello from cache!"));
///
/// // Streaming response - passed through from upstream
/// # struct MyBody;
/// # impl http_body::Body for MyBody {
/// # type Data = bytes::Bytes;
/// # type Error = Box<dyn std::error::Error + Send + Sync>;
/// # fn poll_frame(
/// # self: std::pin::Pin<&mut Self>,
/// # _: &mut std::task::Context<'_>
/// # ) -> std::task::Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
/// # std::task::Poll::Ready(None)
/// # }
/// # }
/// let upstream_body = MyBody;
/// let streaming = StreamingBody::streaming(upstream_body);
/// ```
#[project = StreamingBodyProj]
pub enum StreamingBody<B> {
Buffered {
data: Option<Bytes>,
},
Streaming {
#[pin]
inner: B,
},
File {
#[pin]
file: crate::runtime::File,
buf: Vec<u8>,
finished: bool,
},
}
}
#[cfg(not(feature = "streaming"))]
pin_project! {
/// A body type that can represent either buffered data from cache or streaming body from upstream.
///
/// This enum allows the HTTP cache middleware to efficiently handle:
/// - Cached responses (buffered data)
/// - Cache misses (streaming from upstream)
///
/// # Variants
///
/// - [`Buffered`](StreamingBody::Buffered): Contains cached response data that can be sent immediately
/// - [`Streaming`](StreamingBody::Streaming): Wraps an upstream body for streaming responses
///
/// # Example
///
/// ```rust
/// use http_cache::StreamingBody;
/// use bytes::Bytes;
/// use http_body_util::Full;
///
/// // Cached response - sent immediately from memory
/// let cached: StreamingBody<Full<Bytes>> = StreamingBody::buffered(Bytes::from("Hello from cache!"));
///
/// // Streaming response - passed through from upstream
/// # struct MyBody;
/// # impl http_body::Body for MyBody {
/// # type Data = bytes::Bytes;
/// # type Error = Box<dyn std::error::Error + Send + Sync>;
/// # fn poll_frame(
/// # self: std::pin::Pin<&mut Self>,
/// # _: &mut std::task::Context<'_>
/// # ) -> std::task::Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
/// # std::task::Poll::Ready(None)
/// # }
/// # }
/// let upstream_body = MyBody;
/// let streaming = StreamingBody::streaming(upstream_body);
/// ```
#[project = StreamingBodyProj]
pub enum StreamingBody<B> {
Buffered {
data: Option<Bytes>,
},
Streaming {
#[pin]
inner: B,
},
}
}
impl<B> StreamingBody<B> {
/// Create a new buffered body from bytes
pub fn buffered(data: Bytes) -> Self {
Self::Buffered { data: Some(data) }
}
/// Create a new streaming body from an upstream body
pub fn streaming(body: B) -> Self {
Self::Streaming { inner: body }
}
/// Create a new file-based streaming body
#[cfg(feature = "streaming")]
pub fn from_file(file: crate::runtime::File) -> Self {
Self::File { file, buf: Vec::new(), finished: false }
}
}
impl<B> Body for StreamingBody<B>
where
B: Body + Unpin,
B::Error: Into<StreamingError>,
B::Data: Into<Bytes>,
{
type Data = Bytes;
type Error = StreamingError;
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
match self.as_mut().project() {
StreamingBodyProj::Buffered { data } => {
if let Some(bytes) = data.take() {
if bytes.is_empty() {
Poll::Ready(None)
} else {
Poll::Ready(Some(Ok(Frame::data(bytes))))
}
} else {
Poll::Ready(None)
}
}
StreamingBodyProj::Streaming { inner } => {
inner.poll_frame(cx).map(|opt| {
opt.map(|res| {
res.map(|frame| frame.map_data(Into::into))
.map_err(Into::into)
})
})
}
#[cfg(feature = "streaming")]
StreamingBodyProj::File { file, buf, finished } => {
if *finished {
return Poll::Ready(None);
}
// Prepare buffer
buf.resize(8192, 0);
cfg_if::cfg_if! {
if #[cfg(feature = "streaming-tokio")] {
use tokio::io::AsyncRead;
use crate::runtime::ReadBuf;
let mut read_buf = ReadBuf::new(buf);
match file.poll_read(cx, &mut read_buf) {
Poll::Pending => Poll::Pending,
Poll::Ready(Err(e)) => {
*finished = true;
Poll::Ready(Some(Err(StreamingError::new(e))))
}
Poll::Ready(Ok(())) => {
let n = read_buf.filled().len();
if n == 0 {
// EOF
*finished = true;
Poll::Ready(None)
} else {
let chunk = Bytes::copy_from_slice(&buf[..n]);
buf.clear();
Poll::Ready(Some(Ok(Frame::data(chunk))))
}
}
}
} else if #[cfg(feature = "streaming-smol")] {
use futures::io::AsyncRead;
match file.poll_read(cx, buf) {
Poll::Pending => Poll::Pending,
Poll::Ready(Err(e)) => {
*finished = true;
Poll::Ready(Some(Err(StreamingError::new(e))))
}
Poll::Ready(Ok(0)) => {
// EOF
*finished = true;
Poll::Ready(None)
}
Poll::Ready(Ok(n)) => {
let chunk = Bytes::copy_from_slice(&buf[..n]);
buf.clear();
Poll::Ready(Some(Ok(Frame::data(chunk))))
}
}
}
}
}
}
}
fn is_end_stream(&self) -> bool {
match self {
StreamingBody::Buffered { data } => data.is_none(),
StreamingBody::Streaming { inner } => inner.is_end_stream(),
#[cfg(feature = "streaming")]
StreamingBody::File { finished, .. } => *finished,
}
}
fn size_hint(&self) -> http_body::SizeHint {
match self {
StreamingBody::Buffered { data } => {
if let Some(bytes) = data {
let len = bytes.len() as u64;
http_body::SizeHint::with_exact(len)
} else {
http_body::SizeHint::with_exact(0)
}
}
StreamingBody::Streaming { inner } => inner.size_hint(),
#[cfg(feature = "streaming")]
StreamingBody::File { .. } => {
// We don't know the file size in advance without an additional stat call
http_body::SizeHint::default()
}
}
}
}
impl<B> From<Bytes> for StreamingBody<B> {
fn from(bytes: Bytes) -> Self {
Self::buffered(bytes)
}
}
#[cfg(feature = "streaming")]
impl<B> StreamingBody<B>
where
B: Body + Unpin + Send,
B::Error: Into<StreamingError>,
B::Data: Into<Bytes>,
{
/// Convert this streaming body into a stream of Bytes.
///
/// This method allows for streaming without collecting the entire body into memory first.
/// This is particularly useful for file-based cached responses which can stream
/// directly from disk.
pub fn into_bytes_stream(
self,
) -> impl futures_util::Stream<
Item = Result<Bytes, Box<dyn std::error::Error + Send + Sync>>,
> + Send {
use futures_util::TryStreamExt;
http_body_util::BodyStream::new(self)
.map_ok(|frame| {
// Extract data from frame, StreamingBody always produces Bytes
frame.into_data().unwrap_or_else(|_| Bytes::new())
})
.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> {
Box::new(std::io::Error::other(format!("Stream error: {e}")))
})
}
}