mirror of
https://github.com/actix/actix-website
synced 2025-03-11 18:52:58 +01:00
Updates Compression section.
This commit is contained in:
parent
387807ee6e
commit
0601997b41
@ -20,25 +20,32 @@ instance multiple times, the builder will panic.
|
|||||||
|
|
||||||
# Content encoding
|
# 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
|
* Brotli
|
||||||
* Gzip
|
* Gzip
|
||||||
* Deflate
|
* Deflate
|
||||||
* Identity
|
* Identity
|
||||||
|
|
||||||
|
{{< include-example example="responses" file="compress.rs" section="compress" >}}
|
||||||
|
|
||||||
Response payload is compressed based on the *encoding* parameter from the
|
Response payload is compressed based on the *encoding* parameter from the
|
||||||
`middleware::BodyEncoding` trait. By default, `ContentEncoding::Auto` is
|
`middleware::BodyEncoding` trait. By default, `ContentEncoding::Auto` is used. If
|
||||||
used. If `ContentEncoding::Auto` is selected, then the compression depends
|
`ContentEncoding::Auto` is selected, then the compression depends on the request's
|
||||||
on the request's `Accept-Encoding` header.
|
`Accept-Encoding` header.
|
||||||
|
|
||||||
> `ContentEncoding::Identity` can be used to disable compression.
|
> `ContentEncoding::Identity` can be used to disable compression.
|
||||||
> If another content encoding is selected, the compression is enforced for that codec.
|
> 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" >}}
|
{{< 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
|
In this case we explicitly disable content compression by setting content encoding to
|
||||||
an `Identity` value:
|
an `Identity` value:
|
||||||
|
|
||||||
@ -76,3 +83,4 @@ is enabled automatically.
|
|||||||
{{< include-example example="responses" file="chunked.rs" section="chunked" >}}
|
{{< include-example example="responses" file="chunked.rs" section="chunked" >}}
|
||||||
|
|
||||||
[responsebuilder]: https://docs.rs/actix-web/1.0.2/actix_web/dev/struct.HttpResponseBuilder.html
|
[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(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
// v- disable compression for all routes
|
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||||
.wrap(middleware::Compress::new(ContentEncoding::Identity))
|
|
||||||
.route("/", web::get().to(index))
|
.route("/", web::get().to(index))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
|
@ -6,14 +6,18 @@ fn index_br() -> HttpResponse {
|
|||||||
.encoding(ContentEncoding::Br)
|
.encoding(ContentEncoding::Br)
|
||||||
.body("data")
|
.body("data")
|
||||||
}
|
}
|
||||||
// </brotli>
|
|
||||||
|
|
||||||
pub fn main() {
|
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)))
|
HttpServer::new(|| {
|
||||||
.bind("127.0.0.1:8088")
|
App::new()
|
||||||
.unwrap()
|
.wrap(middleware::Compress::default())
|
||||||
.run()
|
.route("/", web::get().to(index_br))
|
||||||
.unwrap();
|
})
|
||||||
|
.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>
|
// <identity>
|
||||||
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
|
use actix_web::{
|
||||||
|
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
fn index() -> HttpResponse {
|
fn index() -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -7,14 +9,18 @@ fn index() -> HttpResponse {
|
|||||||
.encoding(ContentEncoding::Identity)
|
.encoding(ContentEncoding::Identity)
|
||||||
.body("data")
|
.body("data")
|
||||||
}
|
}
|
||||||
// </identity>
|
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
HttpServer::new(|| {
|
||||||
.bind("127.0.0.1:8088")
|
App::new()
|
||||||
.unwrap()
|
.wrap(middleware::Compress::default())
|
||||||
.run()
|
.route("/", web::get().to(index))
|
||||||
.unwrap();
|
})
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
// </identity>
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// <identity-two>
|
// <identity-two>
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
|
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpRequest,
|
||||||
|
HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
static HELLO_WORLD: &[u8] = &[
|
static HELLO_WORLD: &[u8] = &[
|
||||||
@ -20,9 +21,13 @@ pub fn index(_req: HttpRequest) -> HttpResponse {
|
|||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
HttpServer::new(|| {
|
||||||
.bind("127.0.0.1:8088")
|
App::new()
|
||||||
.unwrap()
|
.wrap(middleware::Compress::default())
|
||||||
.run()
|
.route("/", web::get().to(index))
|
||||||
.unwrap();
|
})
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
pub mod auto;
|
pub mod auto;
|
||||||
pub mod brotli;
|
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;
|
||||||
|
@ -5,7 +5,7 @@ edition = "2018"
|
|||||||
workspace = "../"
|
workspace = "../"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.7"
|
actix = "0.8"
|
||||||
actix-web = "1.0"
|
actix-web = "1.0"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
openssl = "0.10"
|
openssl = "0.10"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user