Struct tower_http::compression::Compression
source · pub struct Compression<S, P = DefaultPredicate> { /* private fields */ }
compression-br
or compression-deflate
or compression-gzip
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 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 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>where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
ResBody: Body,
P: Predicate,
impl<ReqBody, ResBody, S, P> Service<Request<ReqBody>> for Compression<S, P>where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
ResBody: Body,
P: Predicate,
§type Response = Response<CompressionBody<ResBody>>
type Response = Response<CompressionBody<ResBody>>
§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> RefUnwindSafe for Compression<S, P>where
P: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, P> Send for Compression<S, P>where
P: Send,
S: Send,
impl<S, P> Sync for Compression<S, P>where
P: Sync,
S: Sync,
impl<S, P> Unpin for Compression<S, P>where
P: Unpin,
S: Unpin,
impl<S, P> UnwindSafe for Compression<S, P>where
P: UnwindSafe,
S: UnwindSafe,
Blanket Implementations§
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 Twhere
T: Service<Request> + ?Sized,
impl<T, Request> ServiceExt<Request> for Twhere
T: Service<Request> + ?Sized,
source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
source§fn ready_and(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready_and(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
ServiceExt::ready
method insteadsource§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 with the providing request once it is ready.source§fn call_all<S>(self, reqs: S) -> CallAll<Self, S>where
Self: Sized,
Self::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
S: Stream<Item = Request>,
fn call_all<S>(self, reqs: S) -> CallAll<Self, S>where
Self: Sized,
Self::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
S: Stream<Item = Request>,
source§fn and_then<F>(self, f: F) -> AndThen<Self, F>where
Self: Sized,
F: Clone,
fn and_then<F>(self, f: F) -> AndThen<Self, F>where
Self: Sized,
F: Clone,
poll_ready
method. Read moresource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>where
Self: Sized,
F: FnOnce(Self::Response) -> Response + Clone,
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>where
Self: Sized,
F: FnOnce(Self::Response) -> Response + Clone,
poll_ready
method. Read moresource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>where
Self: Sized,
F: FnOnce(Self::Error) -> Error + Clone,
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>where
Self: Sized,
F: FnOnce(Self::Error) -> Error + Clone,
poll_ready
method. Read moresource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>where
Self: Sized,
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Result<Response, Error> + Clone,
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>where
Self: Sized,
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Result<Response, Error> + Clone,
Result<Self::Response, Self::Error>
)
to a different value, regardless of whether the future succeeds or
fails. Read more