pub struct Compress { /* private fields */ }Expand description
Middleware for compressing response body.
Bodies with a known size below 1KiB are sent uncompressed.
Use BodyEncoding trait for overriding response compression.
To disable compression set encoding to ContentEncoding::Identity value.
The encoding is negotiated from the request’s Accept-Encoding header.
identity is used instead of compression only if the client gives it a
strictly higher weight, explicitly or via *. Responses whose encoding is
negotiated get Vary: Accept-Encoding, so caches keep one copy per
encoding. The header is not added if compression is disabled, if the
response selects its encoding with BodyEncoding or already has a
Content-Encoding header.
Each compressed response keeps its encoder until the body ends, up to
1.5MiB for zstd, see crate::http::encoding.
use ntex::web::{self, middleware, App, HttpResponse};
fn main() {
let app = App::default()
.middleware(middleware::Compress::default())
.service(
web::resource("/test")
.route(web::get().to(async || { HttpResponse::Ok() }))
.route(web::head().to(async || { HttpResponse::MethodNotAllowed() }))
);
}Implementations§
Source§impl Compress
impl Compress
Sourcepub fn new(encoding: ContentEncoding) -> Self
pub fn new(encoding: ContentEncoding) -> Self
Create new Compress middleware with the specified encoding.
Use Compress::default() to select the encoding automatically
(ContentEncoding::Auto).
Trait Implementations§
Source§impl<S, St> Middleware<S, St> for Compress
impl<S, St> Middleware<S, St> for Compress
Source§type Service = CompressMiddleware<S>
type Service = CompressMiddleware<S>
Self::create. Read moreSource§fn create(&self, _: &St, service: S) -> Self::Service
fn create(&self, _: &St, service: S) -> Self::Service
service using the supplied construction state. Read more