mirror of
https://github.com/actix/actix-website
synced 2024-11-23 16:31:08 +01:00
Updates Compression section.
This commit is contained in:
parent
387807ee6e
commit
0601997b41
@ -20,32 +20,39 @@ instance multiple times, the builder will panic.
|
||||
|
||||
# Content encoding
|
||||
|
||||
Actix-web automatically *compresses* payloads. The following codecs are supported:
|
||||
Actix-web can automatically *compresses* payloads with the [*Compress middleware*][compressmidddleware].
|
||||
The following codecs are supported:
|
||||
|
||||
* Brotli
|
||||
* Gzip
|
||||
* Deflate
|
||||
* Identity
|
||||
|
||||
{{< include-example 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.
|
||||
`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` use `ContentEncoding::Br`:
|
||||
For example, to enable `brotli` for a single handler use `ContentEncoding::Br`:
|
||||
|
||||
{{< include-example example="responses" file="brotli.rs" section="brotli" >}}
|
||||
|
||||
or for the entire application:
|
||||
|
||||
{{< include-example 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:
|
||||
|
||||
{{< include-example 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
|
||||
set the content encoding to `Identity` to avoid compressing the already compressed
|
||||
data and set the `content-encoding` header manually:
|
||||
|
||||
{{< include-example example="responses" file="identity_two.rs" section="identity-two" >}}
|
||||
@ -76,3 +83,4 @@ is enabled automatically.
|
||||
{{< include-example example="responses" file="chunked.rs" section="chunked" >}}
|
||||
|
||||
[responsebuilder]: https://docs.rs/actix-web/1.0.2/actix_web/dev/struct.HttpResponseBuilder.html
|
||||
[compressmidddleware]: https://docs.rs/actix-web/1.0.2/actix_web/middleware/struct.Compress.html
|
||||
|
@ -10,8 +10,7 @@ pub fn main() {
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// v- disable compression for all routes
|
||||
.wrap(middleware::Compress::new(ContentEncoding::Identity))
|
||||
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
|
@ -6,14 +6,18 @@ fn index_br() -> HttpResponse {
|
||||
.encoding(ContentEncoding::Br)
|
||||
.body("data")
|
||||
}
|
||||
// </brotli>
|
||||
|
||||
pub fn main() {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_web::{middleware, web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index_br)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index_br))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </brotli>
|
||||
|
21
examples/responses/src/brotli_two.rs
Normal file
21
examples/responses/src/brotli_two.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// <brotli-two>
|
||||
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
|
||||
|
||||
fn index_br() -> HttpResponse {
|
||||
HttpResponse::Ok().body("data")
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
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:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </brotli-two>
|
21
examples/responses/src/compress.rs
Normal file
21
examples/responses/src/compress.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// <compress>
|
||||
use actix_web::{http::ContentEncoding, middleware, HttpResponse};
|
||||
|
||||
fn index_br() -> HttpResponse {
|
||||
HttpResponse::Ok().body("data")
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index_br))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </compress>
|
@ -1,5 +1,7 @@
|
||||
// <identity>
|
||||
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
|
||||
use actix_web::{
|
||||
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpResponse,
|
||||
};
|
||||
|
||||
fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
@ -7,14 +9,18 @@ fn index() -> HttpResponse {
|
||||
.encoding(ContentEncoding::Identity)
|
||||
.body("data")
|
||||
}
|
||||
// </identity>
|
||||
|
||||
pub fn main() {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </identity>
|
||||
|
@ -1,6 +1,7 @@
|
||||
// <identity-two>
|
||||
use actix_web::{
|
||||
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
|
||||
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpRequest,
|
||||
HttpResponse,
|
||||
};
|
||||
|
||||
static HELLO_WORLD: &[u8] = &[
|
||||
@ -20,9 +21,13 @@ pub fn index(_req: HttpRequest) -> HttpResponse {
|
||||
pub fn main() {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
pub mod auto;
|
||||
pub mod brotli;
|
||||
pub mod chunked;
|
||||
pub mod compress;
|
||||
pub mod identity;
|
||||
pub mod identity_two;
|
||||
pub mod json_resp;
|
||||
|
@ -5,7 +5,7 @@ edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix = "0.7"
|
||||
actix = "0.8"
|
||||
actix-web = "1.0"
|
||||
futures = "0.1"
|
||||
openssl = "0.10"
|
||||
|
Loading…
Reference in New Issue
Block a user