mirror of
https://github.com/actix/actix-website
synced 2025-02-17 10:13:31 +01:00
Update information on Compress middleware (#279)
This commit is contained in:
parent
2904c4b861
commit
bb42ca002c
@ -38,31 +38,17 @@ Actix Web can automatically _compress_ payloads with the [_Compress middleware_]
|
|||||||
- Deflate
|
- Deflate
|
||||||
- Identity
|
- Identity
|
||||||
|
|
||||||
<CodeBlock example="responses" file="compress.rs" section="compress" />
|
A response's `Content-Encoding` header defaults to `ContentEncoding::Auto`, which performs automatic content compression negotiation based on the request's `Accept-Encoding` header.
|
||||||
|
|
||||||
Response payload is compressed based on the _encoding_ parameter from the `middleware::BodyEncoding` trait. By default, `ContentEncoding::Auto` is used. If `ContentEncoding::Auto` is selected, then the compression depends on the request's `Accept-Encoding` header.
|
|
||||||
|
|
||||||
> `ContentEncoding::Identity` can be used to disable compression. If another content encoding is selected, the compression is enforced for that codec.
|
|
||||||
|
|
||||||
For example, to enable `brotli` for a single handler use `ContentEncoding::Br`:
|
|
||||||
|
|
||||||
<CodeBlock example="responses" file="brotli.rs" section="brotli" />
|
|
||||||
|
|
||||||
or for the entire application:
|
|
||||||
|
|
||||||
<CodeBlock example="responses" file="brotli_two.rs" section="brotli-two" />
|
|
||||||
|
|
||||||
In this case we explicitly disable content compression by setting content encoding to an `Identity` value:
|
|
||||||
|
|
||||||
<CodeBlock example="responses" file="identity.rs" section="identity" />
|
|
||||||
|
|
||||||
When dealing with an already compressed body (for example when serving assets), set the content encoding to `Identity` to avoid compressing the already compressed data and set the `content-encoding` header manually:
|
|
||||||
|
|
||||||
<CodeBlock example="responses" file="identity_two.rs" section="identity-two" />
|
|
||||||
|
|
||||||
Also it is possible to set default content encoding on application level, by default `ContentEncoding::Auto` is used, which implies automatic content compression negotiation.
|
|
||||||
|
|
||||||
<CodeBlock example="responses" file="auto.rs" section="auto" />
|
<CodeBlock example="responses" file="auto.rs" section="auto" />
|
||||||
|
|
||||||
|
Explicitly disable content compression on a handler by setting `Content-Encoding` to an `Identity` value:
|
||||||
|
|
||||||
|
<CodeBlock example="responses" file="identity.rs" section="identity" />
|
||||||
|
|
||||||
|
When dealing with an already compressed body (for example, when serving pre-compressed assets), set the `Content-Encoding` header on the response manually to bypass the middleware:
|
||||||
|
|
||||||
|
<CodeBlock example="responses" file="identity_two.rs" section="identity-two" />
|
||||||
|
|
||||||
[responsebuilder]: https://docs.rs/actix-web/4/actix_web/struct.HttpResponseBuilder.html
|
[responsebuilder]: https://docs.rs/actix-web/4/actix_web/struct.HttpResponseBuilder.html
|
||||||
[compressmidddleware]: https://docs.rs/actix-web/4/actix_web/middleware/struct.Compress.html
|
[compressmidddleware]: https://docs.rs/actix-web/4/actix_web/middleware/struct.Compress.html
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
// <brotli>
|
|
||||||
use actix_web::{get, middleware, App, HttpResponse, HttpServer};
|
|
||||||
|
|
||||||
#[get("/")]
|
|
||||||
async fn index_br() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().body("data")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_web::main]
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
HttpServer::new(|| {
|
|
||||||
App::new()
|
|
||||||
.wrap(middleware::Compress::default())
|
|
||||||
.service(index_br)
|
|
||||||
})
|
|
||||||
.bind(("127.0.0.1", 8080))?
|
|
||||||
.run()
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
// </brotli>
|
|
@ -1,21 +0,0 @@
|
|||||||
// <brotli-two>
|
|
||||||
use actix_web::{http::ContentEncoding, dev::BodyEncoding, HttpResponse};
|
|
||||||
|
|
||||||
async fn index_br() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().body("data")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_web::main]
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
use actix_web::{middleware, web, App, HttpServer};
|
|
||||||
|
|
||||||
HttpServer::new(|| {
|
|
||||||
App::new()
|
|
||||||
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
|
||||||
.route("/", web::get().to(index_br))
|
|
||||||
})
|
|
||||||
.bind(("127.0.0.1", 8080))?
|
|
||||||
.run()
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
// </brotli-two>
|
|
@ -1,20 +0,0 @@
|
|||||||
// <compress>
|
|
||||||
use actix_web::{get, middleware, App, HttpResponse, HttpServer};
|
|
||||||
|
|
||||||
#[get("/")]
|
|
||||||
async fn index_br() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().body("data")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_web::main]
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
HttpServer::new(|| {
|
|
||||||
App::new()
|
|
||||||
.wrap(middleware::Compress::default())
|
|
||||||
.service(index_br)
|
|
||||||
})
|
|
||||||
.bind(("127.0.0.1", 8080))?
|
|
||||||
.run()
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
// </compress>
|
|
@ -1,7 +1,5 @@
|
|||||||
pub mod auto;
|
pub mod auto;
|
||||||
pub mod brotli;
|
|
||||||
pub mod chunked;
|
pub mod chunked;
|
||||||
pub mod compress;
|
|
||||||
pub mod identity;
|
pub mod identity;
|
||||||
pub mod identity_two;
|
pub mod identity_two;
|
||||||
pub mod json_resp;
|
pub mod json_resp;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user