diff --git a/content/docs/response.md b/content/docs/response.md
index 32d9f36..79681a1 100644
--- a/content/docs/response.md
+++ b/content/docs/response.md
@@ -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
diff --git a/examples/responses/src/auto.rs b/examples/responses/src/auto.rs
index bd0f14e..1af1078 100644
--- a/examples/responses/src/auto.rs
+++ b/examples/responses/src/auto.rs
@@ -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")
diff --git a/examples/responses/src/brotli.rs b/examples/responses/src/brotli.rs
index 5694b28..ebdf7e2 100644
--- a/examples/responses/src/brotli.rs
+++ b/examples/responses/src/brotli.rs
@@ -6,14 +6,18 @@ fn index_br() -> HttpResponse {
.encoding(ContentEncoding::Br)
.body("data")
}
-//
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();
}
+//
diff --git a/examples/responses/src/brotli_two.rs b/examples/responses/src/brotli_two.rs
new file mode 100644
index 0000000..cdd79f4
--- /dev/null
+++ b/examples/responses/src/brotli_two.rs
@@ -0,0 +1,21 @@
+//
+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();
+}
+//
diff --git a/examples/responses/src/compress.rs b/examples/responses/src/compress.rs
new file mode 100644
index 0000000..4bd40d9
--- /dev/null
+++ b/examples/responses/src/compress.rs
@@ -0,0 +1,21 @@
+//
+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();
+}
+//
diff --git a/examples/responses/src/identity.rs b/examples/responses/src/identity.rs
index 63fb968..2d759d1 100644
--- a/examples/responses/src/identity.rs
+++ b/examples/responses/src/identity.rs
@@ -1,5 +1,7 @@
//
-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")
}
-//
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();
}
+//
diff --git a/examples/responses/src/identity_two.rs b/examples/responses/src/identity_two.rs
index 8e6020f..494e9d9 100644
--- a/examples/responses/src/identity_two.rs
+++ b/examples/responses/src/identity_two.rs
@@ -1,6 +1,7 @@
//
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();
}
diff --git a/examples/responses/src/main.rs b/examples/responses/src/main.rs
index 1d63343..61e4679 100644
--- a/examples/responses/src/main.rs
+++ b/examples/responses/src/main.rs
@@ -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;
diff --git a/examples/url-dispatch/Cargo.toml b/examples/url-dispatch/Cargo.toml
index 761a7b6..b7914ca 100644
--- a/examples/url-dispatch/Cargo.toml
+++ b/examples/url-dispatch/Cargo.toml
@@ -5,7 +5,7 @@ edition = "2018"
workspace = "../"
[dependencies]
-actix = "0.7"
+actix = "0.8"
actix-web = "1.0"
futures = "0.1"
openssl = "0.10"