Struct actix_web::middleware::Compress
source · [−]#[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 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
sourceimpl<S, B> Transform<S, ServiceRequest> for Compress where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
impl<S, B> Transform<S, ServiceRequest> for Compress where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
type Response = ServiceResponse<EitherBody<Encoder<B>, BoxBody>>
type Response = ServiceResponse<EitherBody<Encoder<B>, BoxBody>>
Responses produced by the service.
type Transform = CompressMiddleware<S>
type Transform = CompressMiddleware<S>
The TransformService
value created by this factory
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>>
The future response value.
sourcefn new_transform(&self, service: S) -> Self::Future
fn new_transform(&self, service: S) -> Self::Future
Creates and returns a new Transform component, asynchronously
Auto Trait Implementations
impl RefUnwindSafe for Compress
impl Send for Compress
impl Sync for Compress
impl Unpin for Compress
impl UnwindSafe for Compress
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more