Skip to content

Commit a3f95ee

Browse files
authored
feat: improve HttpResponseBuilder::streaming with SizedStream (#3829)
1 parent e1da110 commit a3f95ee

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

actix-web/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Unreleased
44

55
- `actix_web::response::builder::HttpResponseBuilder::streaming()` now sets `Content-Type` to `application/octet-stream` if `Content-Type` does not exist.
6-
- `actix_web::response::builder::HttpResponseBuilder::streaming()` now calls `actix_web::response::builder::HttpResponseBuilder::no_chunking()` if `Content-Length` is set by user.
6+
- `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.
77
- Add `ws` crate feature (on-by-default) which forwards to `actix-http` and guards some of its `ResponseError` impls.
88
- Add public export for `EitherExtractError` in `error` module.
99

actix-web/src/response/builder.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use futures_core::Stream;
1111
use serde::Serialize;
1212

1313
use crate::{
14-
body::{BodyStream, BoxBody, MessageBody},
14+
body::{BodyStream, BoxBody, MessageBody, SizedStream},
1515
dev::Extensions,
1616
error::{Error, JsonPayloadError},
1717
http::{
@@ -335,17 +335,18 @@ impl HttpResponseBuilder {
335335
}
336336
}
337337

338-
if let Some(parts) = self.inner() {
339-
if let Some(length) = parts.headers.get(header::CONTENT_LENGTH) {
340-
if let Ok(length) = length.to_str() {
341-
if let Ok(length) = length.parse::<u64>() {
342-
self.no_chunking(length);
343-
}
344-
}
345-
}
338+
let content_length = self
339+
.inner()
340+
.and_then(|parts| parts.headers.get(header::CONTENT_LENGTH))
341+
.and_then(|value| value.to_str().ok())
342+
.and_then(|value| value.parse::<u64>().ok());
343+
344+
if let Some(len) = content_length {
345+
self.no_chunking(len);
346+
self.body(SizedStream::new(len, stream))
347+
} else {
348+
self.body(BodyStream::new(stream))
346349
}
347-
348-
self.body(BodyStream::new(stream))
349350
}
350351

351352
/// Set a JSON body and build the `HttpResponse`.

0 commit comments

Comments
 (0)