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 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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)