diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index d6f149466..51ffe3bce 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -3,7 +3,7 @@ ## Unreleased - `actix_web::response::builder::HttpResponseBuilder::streaming()` now sets `Content-Type` to `application/octet-stream` if `Content-Type` does not exist. -- `actix_web::response::builder::HttpResponseBuilder::streaming()` now calls `actix_web::response::builder::HttpResponseBuilder::no_chunking()` if `Content-Length` is set by user. +- `actix_web::response::builder::HttpResponseBuilder::streaming()` now calls `actix_web::response::builder::HttpResponseBuilder::no_chunking()` and returns `SizedStream` if `Content-Length` is set by user. - Add `ws` crate feature (on-by-default) which forwards to `actix-http` and guards some of its `ResponseError` impls. - Add public export for `EitherExtractError` in `error` module. diff --git a/actix-web/src/response/builder.rs b/actix-web/src/response/builder.rs index b81ce3568..6ea23b42e 100644 --- a/actix-web/src/response/builder.rs +++ b/actix-web/src/response/builder.rs @@ -11,7 +11,7 @@ use futures_core::Stream; use serde::Serialize; use crate::{ - body::{BodyStream, BoxBody, MessageBody}, + body::{BodyStream, BoxBody, MessageBody, SizedStream}, dev::Extensions, error::{Error, JsonPayloadError}, http::{ @@ -335,17 +335,18 @@ impl HttpResponseBuilder { } } - if let Some(parts) = self.inner() { - if let Some(length) = parts.headers.get(header::CONTENT_LENGTH) { - if let Ok(length) = length.to_str() { - if let Ok(length) = length.parse::() { - self.no_chunking(length); - } - } - } - } + let content_length = self + .inner() + .and_then(|parts| parts.headers.get(header::CONTENT_LENGTH)) + .and_then(|value| value.to_str().ok()) + .and_then(|value| value.parse::().ok()); - self.body(BodyStream::new(stream)) + if let Some(len) = content_length { + self.no_chunking(len); + self.body(SizedStream::new(len, stream)) + } else { + self.body(BodyStream::new(stream)) + } } /// Set a JSON body and build the `HttpResponse`.