#[non_exhaustive]pub struct Compress;Expand description
Middleware for compressing response payloads.
§Encoding Negotiation
Compress will read the Accept-Encoding header to negotiate which compression codec to use.
Payloads are not compressed if the header is not sent. The compress-* feature flags are also
considered in this selection process.
§Pre-compressed Payload
If you are serving some data that is already using a compressed representation (e.g., a gzip
compressed HTML file from disk) you can signal this to Compress by setting an appropriate
Content-Encoding header. In addition to preventing double compressing the payload, this header
is required by the spec when using compressed representations and will inform the client that
the content should be uncompressed.
However, it is not advised to unconditionally serve encoded representations of content because
the client may not support it. The AcceptEncoding typed header has some utilities to help
perform manual encoding negotiation, if required. When negotiating content encoding, it is also
required by the spec to send a Vary: Accept-Encoding header.
A (naïve) example serving an pre-compressed Gzip file is included below.
§Examples
To enable automatic payload compression just include Compress as a top-level middleware:
use actix_web::{middleware, web, App, HttpResponse};
let app = App::new()
    .wrap(middleware::Compress::default())
    .default_service(web::to(|| async { HttpResponse::Ok().body("hello world") }));Pre-compressed Gzip file being served from disk with correct headers added to bypass middleware:
use actix_web::{middleware, http::header, web, App, HttpResponse, Responder};
async fn index_handler() -> actix_web::Result<impl Responder> {
    Ok(actix_files::NamedFile::open_async("./assets/index.html.gz").await?
        .customize()
        .insert_header(header::ContentEncoding::Gzip))
}
let app = App::new()
    .wrap(middleware::Compress::default())
    .default_service(web::to(index_handler));Trait Implementations§
Source§impl<S, B> Transform<S, ServiceRequest> for Compress
 
impl<S, B> Transform<S, ServiceRequest> for Compress
Source§type Response = ServiceResponse<EitherBody<Encoder<B>>>
 
type Response = ServiceResponse<EitherBody<Encoder<B>>>
Source§type Future = Ready<Result<<Compress as Transform<S, ServiceRequest>>::Transform, <Compress as Transform<S, ServiceRequest>>::InitError>>
 
type Future = Ready<Result<<Compress as Transform<S, ServiceRequest>>::Transform, <Compress as Transform<S, ServiceRequest>>::InitError>>
Source§fn new_transform(&self, service: S) -> Self::Future
 
fn new_transform(&self, service: S) -> Self::Future
Auto Trait Implementations§
impl Freeze for Compress
impl RefUnwindSafe for Compress
impl Send for Compress
impl Sync for Compress
impl Unpin for Compress
impl UnwindSafe for Compress
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> 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