1
0
mirror of https://github.com/actix/actix-website synced 2024-11-23 16:31:08 +01:00

Update information on Compress middleware (#279)

This commit is contained in:
Kenneth Allen 2022-07-19 20:11:45 +10:00 committed by GitHub
parent 2904c4b861
commit bb42ca002c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 86 deletions

View File

@ -38,31 +38,17 @@ Actix Web can automatically _compress_ payloads with the [_Compress middleware_]
- Deflate
- Identity
<CodeBlock example="responses" file="compress.rs" section="compress" />
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.
A response's `Content-Encoding` header defaults to `ContentEncoding::Auto`, which performs automatic content compression negotiation based on the request's `Accept-Encoding` header.
<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
[compressmidddleware]: https://docs.rs/actix-web/4/actix_web/middleware/struct.Compress.html

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -1,7 +1,5 @@
pub mod auto;
pub mod brotli;
pub mod chunked;
pub mod compress;
pub mod identity;
pub mod identity_two;
pub mod json_resp;