From 32a37b72823fee9ba4664a6f344493eeda9752d4 Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Tue, 19 May 2020 00:46:31 +0200 Subject: [PATCH] Remove content_length from ResponseBuilder (#1491) * Remove content_length since it'll be overwritten by the response body. FIXES #1439 * Add setting of Content-Length to the no_chunking function * Add changes and migration documentations * Update MIGRATION.md Co-authored-by: Yuki Okushi Co-authored-by: Rob Ede Co-authored-by: Yuki Okushi --- MIGRATION.md | 2 ++ actix-http/CHANGES.md | 4 ++++ actix-http/src/response.rs | 10 +++------- actix-http/tests/test_openssl.rs | 2 +- actix-http/tests/test_rustls.rs | 2 +- actix-http/tests/test_server.rs | 2 +- tests/test_server.rs | 5 ++--- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index cb79e664b..d2e9735fb 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -6,6 +6,8 @@ * actix-http support for Actors messages was moved to actix-http crate and is enabled with feature `actors` +* content_length function is removed from actix-http. + You can set Content-Length by normally setting the response body or calling no_chunking function. * `BodySize::Sized64` variant has been removed. `BodySize::Sized` now receives a `u64` instead of a `usize`. diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index a383b6a11..1a71a0bc0 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -5,6 +5,7 @@ ### Changed * Bump minimum supported Rust version to 1.40 +* content_length function is removed, and you can set Content-Length by calling no_chunking function [#1439] * `BodySize::Sized64` variant has been removed. `BodySize::Sized` now receives a `u64` instead of a `usize`. @@ -13,6 +14,9 @@ * Support parsing of `SameSite=None` [#1503] +[#1439]: https://github.com/actix/actix-web/pull/1439 +[#1503]: https://github.com/actix/actix-web/pull/1503 + ## [2.0.0-alpha.3] - 2020-05-08 ### Fixed diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index 7a9b82df2..9086212f1 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -472,7 +472,9 @@ impl ResponseBuilder { /// Disable chunked transfer encoding for HTTP/1.1 streaming responses. #[inline] - pub fn no_chunking(&mut self) -> &mut Self { + pub fn no_chunking(&mut self, len: u64) -> &mut Self { + self.header(header::CONTENT_LENGTH, len); + if let Some(parts) = parts(&mut self.head, &self.err) { parts.no_chunking(true); } @@ -497,12 +499,6 @@ impl ResponseBuilder { self } - /// Set content length - #[inline] - pub fn content_length(&mut self, len: u64) -> &mut Self { - self.header(header::CONTENT_LENGTH, len) - } - /// Set a cookie /// /// ```rust diff --git a/actix-http/tests/test_openssl.rs b/actix-http/tests/test_openssl.rs index 4af3a0a47..e8aaa7fc4 100644 --- a/actix-http/tests/test_openssl.rs +++ b/actix-http/tests/test_openssl.rs @@ -275,7 +275,7 @@ async fn test_h2_head_binary() { let mut srv = test_server(move || { HttpService::build() .h2(|_| { - ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR)) + ok::<_, ()>(Response::Ok().body(STR)) }) .openssl(ssl_acceptor()) .map_err(|_| ()) diff --git a/actix-http/tests/test_rustls.rs b/actix-http/tests/test_rustls.rs index 1c5583b08..fff32ac8b 100644 --- a/actix-http/tests/test_rustls.rs +++ b/actix-http/tests/test_rustls.rs @@ -281,7 +281,7 @@ async fn test_h2_head_binary() { let mut srv = test_server(move || { HttpService::build() .h2(|_| { - ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR)) + ok::<_, ()>(Response::Ok().body(STR)) }) .rustls(ssl_acceptor()) }) diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index 1ec819434..35c71e37a 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -490,7 +490,7 @@ async fn test_h1_head_binary() { let mut srv = test_server(|| { HttpService::build() .h1(|_| { - ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR)) + ok::<_, ()>(Response::Ok().body(STR)) }) .tcp() }) diff --git a/tests/test_server.rs b/tests/test_server.rs index 1916b372c..a4dfa65a0 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -349,7 +349,7 @@ async fn test_body_br_streaming() { async fn test_head_binary() { let srv = test::start_with(test::config().h1(), || { App::new().service(web::resource("/").route( - web::head().to(move || HttpResponse::Ok().content_length(100).body(STR)), + web::head().to(move || HttpResponse::Ok().body(STR)), )) }); @@ -371,8 +371,7 @@ async fn test_no_chunking() { let srv = test::start_with(test::config().h1(), || { App::new().service(web::resource("/").route(web::to(move || { HttpResponse::Ok() - .no_chunking() - .content_length(STR.len() as u64) + .no_chunking(STR.len() as u64) .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) }))) });