pub struct Compression<S, P = DefaultPredicate> { /* private fields */ }
Expand description
Compress response bodies of the underlying service.
This uses the Accept-Encoding
header to pick an appropriate encoding and adds the
Content-Encoding
header to responses.
See the module docs for more details.
Implementations§
Source§impl<S> Compression<S, DefaultPredicate>
impl<S> Compression<S, DefaultPredicate>
Sourcepub fn new(service: S) -> Compression<S, DefaultPredicate>
pub fn new(service: S) -> Compression<S, DefaultPredicate>
Creates a new Compression
wrapping the service
.
Source§impl<S, P> Compression<S, P>
impl<S, P> Compression<S, P>
Sourcepub fn into_inner(self) -> S
pub fn into_inner(self) -> S
Consumes self
, returning the underlying service.
Sourcepub fn layer() -> CompressionLayer
pub fn layer() -> CompressionLayer
Returns a new Layer
that wraps services with a Compression
middleware.
Sourcepub fn quality(self, quality: CompressionLevel) -> Self
pub fn quality(self, quality: CompressionLevel) -> Self
Sets the compression quality.
Sourcepub fn no_gzip(self) -> Self
pub fn no_gzip(self) -> Self
Disables the gzip encoding.
This method is available even if the gzip
crate feature is disabled.
Sourcepub fn no_deflate(self) -> Self
pub fn no_deflate(self) -> Self
Disables the Deflate encoding.
This method is available even if the deflate
crate feature is disabled.
Sourcepub fn no_br(self) -> Self
pub fn no_br(self) -> Self
Disables the Brotli encoding.
This method is available even if the br
crate feature is disabled.
Sourcepub fn no_zstd(self) -> Self
pub fn no_zstd(self) -> Self
Disables the Zstd encoding.
This method is available even if the zstd
crate feature is disabled.
Sourcepub fn compress_when<C>(self, predicate: C) -> Compression<S, C>where
C: Predicate,
pub fn compress_when<C>(self, predicate: C) -> Compression<S, C>where
C: Predicate,
Replace the current compression predicate.
Predicates are used to determine whether a response should be compressed or not.
The default predicate is DefaultPredicate
. See its documentation for more
details on which responses it wont compress.
§Changing the compression predicate
use tower_http::compression::{
Compression,
predicate::{Predicate, NotForContentType, DefaultPredicate},
};
use tower::util::service_fn;
// Placeholder service_fn
let service = service_fn(|_: ()| async {
Ok::<_, std::io::Error>(http::Response::new(()))
});
// build our custom compression predicate
// its recommended to still include `DefaultPredicate` as part of
// custom predicates
let predicate = DefaultPredicate::new()
// don't compress responses who's `content-type` starts with `application/json`
.and(NotForContentType::new("application/json"));
let service = Compression::new(service).compress_when(predicate);
See predicate
for more utilities for building compression predicates.
Responses that are already compressed (ie have a content-encoding
header) will never be
recompressed, regardless what they predicate says.
Trait Implementations§
Source§impl<S: Clone, P: Clone> Clone for Compression<S, P>
impl<S: Clone, P: Clone> Clone for Compression<S, P>
Source§fn clone(&self) -> Compression<S, P>
fn clone(&self) -> Compression<S, P>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more