pub struct CappedDecoder<R: Read> { /* private fields */ }Expand description
A size-capped decompression reader.
Wraps any R: Read (typically a raw decoder like MultiGzDecoder or ZlibDecoder) and
enforces the configured decompressed-size cap so that a compression bomb cannot drive
unbounded memory allocation.
Construct via the typed class methods (CappedDecoder::gzip, CappedDecoder::zlib,
CappedDecoder::zstd) rather than by wrapping a raw decoder directly. Read the whole payload
into memory with CappedDecoder::decompress, or stream it through CappedDecoder::into_reader.
Implementations§
Source§impl<R: Read> CappedDecoder<R>
impl<R: Read> CappedDecoder<R>
Sourcepub fn decompress(self) -> Result<Vec<u8>>
pub fn decompress(self) -> Result<Vec<u8>>
Reads all decompressed bytes into a Vec, returning an error if the output exceeds the
configured cap.
§Errors
Returns an error if reading from the underlying decoder fails, or
DecompressedSizeLimitExceeded if the decompressed output exceeds the cap.
Sourcepub fn into_reader(self) -> CappedReader<R> ⓘ
pub fn into_reader(self) -> CappedReader<R> ⓘ
Converts the decoder into a streaming CappedReader that enforces the cap as bytes are
read, rather than buffering the whole payload up front.
Prefer this over consuming a raw decoder directly: the returned reader errors out (instead
of silently truncating) the moment the decompressed output would exceed the cap, so a
streaming consumer such as io::copy, serde_json::from_reader, or BufReader cannot
process a truncated-but-valid-looking payload.
Source§impl<S: Read> CappedDecoder<MultiGzDecoder<S>>
impl<S: Read> CappedDecoder<MultiGzDecoder<S>>
Sourcepub fn gzip(reader: S) -> Self
pub fn gzip(reader: S) -> Self
Creates a capped gzip decoder using the global decompressed-size cap.
Sourcepub fn gzip_with_limit(reader: S, limit: usize) -> Self
pub fn gzip_with_limit(reader: S, limit: usize) -> Self
Creates a capped gzip decoder using an explicit decompressed-size cap.
Source§impl<S: Read> CappedDecoder<ZlibDecoder<S>>
impl<S: Read> CappedDecoder<ZlibDecoder<S>>
Sourcepub fn zlib(reader: S) -> Self
pub fn zlib(reader: S) -> Self
Creates a capped zlib/deflate decoder using the global decompressed-size cap.
Sourcepub fn zlib_with_limit(reader: S, limit: usize) -> Self
pub fn zlib_with_limit(reader: S, limit: usize) -> Self
Creates a capped zlib/deflate decoder using an explicit decompressed-size cap.
Source§impl<S: Read> CappedDecoder<Decoder<'static, BufReader<S>>>
impl<S: Read> CappedDecoder<Decoder<'static, BufReader<S>>>
Sourcepub fn zstd(reader: S) -> Result<Self>
pub fn zstd(reader: S) -> Result<Self>
Creates a capped zstd decoder using the global decompressed-size cap.
Also constrains the decoder’s internal window allocation via window_log_max so a crafted
frame cannot request a large window before the decompressed-size cap trips. The window is
derived from the cap only (zstd_window_log_max); for HTTP Content-Encoding: zstd use
zstd_http, which applies the tighter RFC 9659 ceiling.
§Errors
Returns an error if the zstd decoder cannot be initialized (e.g. invalid header).
Sourcepub fn zstd_with_limit(reader: S, limit: usize) -> Result<Self>
pub fn zstd_with_limit(reader: S, limit: usize) -> Result<Self>
Creates a capped zstd decoder using an explicit decompressed-size cap, with the window
derived from that cap only (zstd_window_log_max).
§Errors
Returns an error if the zstd decoder cannot be initialized (e.g. invalid header).
Sourcepub fn zstd_http(reader: S) -> Result<Self>
pub fn zstd_http(reader: S) -> Result<Self>
Creates a capped zstd decoder for HTTP Content-Encoding: zstd using the global
decompressed-size cap, clamping the decoder window to the RFC 9659 8 MB ceiling
(http_zstd_window_log_max).
§Errors
Returns an error if the zstd decoder cannot be initialized (e.g. invalid header).
Sourcepub fn zstd_http_with_limit(reader: S, limit: usize) -> Result<Self>
pub fn zstd_http_with_limit(reader: S, limit: usize) -> Result<Self>
Creates a capped zstd decoder for HTTP Content-Encoding: zstd using an explicit
decompressed-size cap, clamping the decoder window to the RFC 9659 8 MB ceiling
(http_zstd_window_log_max).
§Errors
Returns an error if the zstd decoder cannot be initialized (e.g. invalid header).
Auto Trait Implementations§
impl<R> Freeze for CappedDecoder<R>where
R: Freeze,
impl<R> RefUnwindSafe for CappedDecoder<R>where
R: RefUnwindSafe,
impl<R> Send for CappedDecoder<R>where
R: Send,
impl<R> Sync for CappedDecoder<R>where
R: Sync,
impl<R> Unpin for CappedDecoder<R>where
R: Unpin,
impl<R> UnsafeUnpin for CappedDecoder<R>where
R: UnsafeUnpin,
impl<R> UnwindSafe for CappedDecoder<R>where
R: UnwindSafe,
Blanket Implementations§
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<Source, Target> OctetsInto<Target> for Sourcewhere
Target: OctetsFrom<Source>,
impl<Source, Target> OctetsInto<Target> for Sourcewhere
Target: OctetsFrom<Source>,
type Error = <Target as OctetsFrom<Source>>::Error
§fn try_octets_into(
self,
) -> Result<Target, <Source as OctetsInto<Target>>::Error>
fn try_octets_into( self, ) -> Result<Target, <Source as OctetsInto<Target>>::Error>
§fn octets_into(self) -> Targetwhere
Self::Error: Into<Infallible>,
fn octets_into(self) -> Targetwhere
Self::Error: Into<Infallible>,
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg] or
a color-specific method, such as [OwoColorize::green], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more