pub struct Compression<S, P = DefaultPredicate> { /* private fields */ }
compression-br
or compression-deflate
or compression-gzip
or compression-zstd
only.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 gzip(self, enable: bool) -> Self
Available on crate feature compression-gzip
only.
pub fn gzip(self, enable: bool) -> Self
compression-gzip
only.Sets whether to enable the gzip encoding.
Sourcepub fn deflate(self, enable: bool) -> Self
Available on crate feature compression-deflate
only.
pub fn deflate(self, enable: bool) -> Self
compression-deflate
only.Sets whether to enable the Deflate encoding.
Sourcepub fn br(self, enable: bool) -> Self
Available on crate feature compression-br
only.
pub fn br(self, enable: bool) -> Self
compression-br
only.Sets whether to enable the Brotli encoding.
Sourcepub fn zstd(self, enable: bool) -> Self
Available on crate feature compression-zstd
only.
pub fn zstd(self, enable: bool) -> Self
compression-zstd
only.Sets whether to enable the Zstd encoding.
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 moreSource§impl<ReqBody, ResBody, S, P> Service<Request<ReqBody>> for Compression<S, P>
impl<ReqBody, ResBody, S, P> Service<Request<ReqBody>> for Compression<S, P>
Source§type Response = Response<CompressionBody<ResBody>>
type Response = Response<CompressionBody<ResBody>>
Source§type Future = ResponseFuture<<S as Service<Request<ReqBody>>>::Future, P>
type Future = ResponseFuture<<S as Service<Request<ReqBody>>>::Future, P>
impl<S: Copy, P: Copy> Copy for Compression<S, P>
Auto Trait Implementations§
impl<S, P> Freeze for Compression<S, P>
impl<S, P> RefUnwindSafe for Compression<S, P>where
S: RefUnwindSafe,
P: RefUnwindSafe,
impl<S, P> Send for Compression<S, P>
impl<S, P> Sync for Compression<S, P>
impl<S, P> Unpin for Compression<S, P>
impl<S, P> UnwindSafe for Compression<S, P>where
S: UnwindSafe,
P: 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Service
, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready
method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready
method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready
method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>
)
to a different value, regardless of whether the future succeeds or
fails. Read more