logo
#[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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Responses produced by the service.

Errors produced by the service.

The TransformService value created by this factory

Errors produced while building a transform service.

The future response value.

Creates and returns a new Transform component, asynchronously

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more