pub enum StreamingBody<B> {
Buffered {
data: Option<Bytes>,
},
Streaming {
inner: B,
},
File {
reader: File,
buffer: BytesMut,
done: bool,
size: u64,
check: Option<Box<FileCheck>>,
},
}streaming only.Expand description
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)
- Disk-cached responses (streaming from file)
§Variants
- Buffered: Contains cached response data that can be sent immediately
- Streaming: Wraps an upstream body for streaming responses
- File: Streams from a
tokio::fs::Filein 64KB chunks (only withstreamingfeature)
Variants§
Implementations§
Source§impl<B> StreamingBody<B>
impl<B> StreamingBody<B>
Sourcepub fn buffered(data: Bytes) -> StreamingBody<B>
pub fn buffered(data: Bytes) -> StreamingBody<B>
Create a new buffered body from bytes.
The bytes are consumed on the first poll and sent as a single frame.
Sourcepub fn streaming(body: B) -> StreamingBody<B>
pub fn streaming(body: B) -> StreamingBody<B>
Create a new streaming body from an upstream body.
The upstream body is passed through without additional buffering.
Sourcepub fn from_file_with_size(file: File, size: u64) -> StreamingBody<B>
pub fn from_file_with_size(file: File, size: u64) -> StreamingBody<B>
Create a new file-streaming body from a tokio::fs::File with known size.
This allows streaming large cached responses from disk without loading the entire body into memory. Data is read in 64KB chunks.
§Cursor contract
The caller must position file at the start of the body bytes before
calling this function. Exactly size bytes are streamed from the
current cursor position; trailing file content is ignored and a file
shorter than size yields a stream error.
size is used to provide accurate size hints to downstream consumers.
Sourcepub fn from_file_verified(
file: File,
size: u64,
checksum: [u8; 32],
on_corrupt: impl FnOnce() + Send + 'static,
) -> StreamingBody<B>
pub fn from_file_verified( file: File, size: u64, checksum: [u8; 32], on_corrupt: impl FnOnce() + Send + 'static, ) -> StreamingBody<B>
Like from_file_with_size, but hashes
the streamed bytes and compares against checksum when the stream
completes. On mismatch the final frame is replaced with an error and
on_corrupt is invoked once.
Source§impl<B> StreamingBody<B>
impl<B> StreamingBody<B>
Trait Implementations§
Source§impl<B> Body for StreamingBody<B>
impl<B> Body for StreamingBody<B>
Source§type Error = StreamingError
type Error = StreamingError
Body might generate.Source§fn poll_frame(
self: Pin<&mut StreamingBody<B>>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<<StreamingBody<B> as Body>::Data>, <StreamingBody<B> as Body>::Error>>>
fn poll_frame( self: Pin<&mut StreamingBody<B>>, cx: &mut Context<'_>, ) -> Poll<Option<Result<Frame<<StreamingBody<B> as Body>::Data>, <StreamingBody<B> as Body>::Error>>>
Source§fn is_end_stream(&self) -> bool
fn is_end_stream(&self) -> bool
true when the end of stream has been reached. Read moreSource§impl<B> Debug for StreamingBody<B>where
B: Debug,
impl<B> Debug for StreamingBody<B>where
B: Debug,
Source§impl<B> From<Bytes> for StreamingBody<B>
impl<B> From<Bytes> for StreamingBody<B>
Source§fn from(bytes: Bytes) -> StreamingBody<B>
fn from(bytes: Bytes) -> StreamingBody<B>
impl<'__pin, B> Unpin for StreamingBody<B>where
<PinnedFieldsOfHelperStruct<__Origin<'__pin, B>> as PinnedFieldsOfHelperTrait>::Actual: Unpin,
Auto Trait Implementations§
impl<B> !Freeze for StreamingBody<B>
impl<B> !RefUnwindSafe for StreamingBody<B>
impl<B> !Sync for StreamingBody<B>
impl<B> !UnwindSafe for StreamingBody<B>
impl<B> Send for StreamingBody<B>where
B: Send,
impl<B> UnsafeUnpin for StreamingBody<B>where
B: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BodyExt for T
impl<T> BodyExt for T
Source§fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
Frame, if any.Source§fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
Source§fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
Source§fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
Source§fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
Source§fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
Source§fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
Collected body which will collect all the DATA frames
and trailers.Source§fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
Source§fn into_stream(self) -> BodyStream<Self>where
Self: Sized,
fn into_stream(self) -> BodyStream<Self>where
Self: Sized,
BodyStream.Source§fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
BodyDataStream.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more