Compare commits

..

2 Commits

Author SHA1 Message Date
Keith Cirkel 968aab7e6d
Merge 98e0cc4049 into a3f95ee1ef 2025-11-15 22:29:56 -08:00
Yuki Okushi a3f95ee1ef
feat: improve `HttpResponseBuilder::streaming` with SizedStream (#3829) 2025-11-16 15:22:29 +09:00
2 changed files with 13 additions and 12 deletions

View File

@ -3,7 +3,7 @@
## Unreleased ## 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 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 `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. - Add public export for `EitherExtractError` in `error` module.

View File

@ -11,7 +11,7 @@ use futures_core::Stream;
use serde::Serialize; use serde::Serialize;
use crate::{ use crate::{
body::{BodyStream, BoxBody, MessageBody}, body::{BodyStream, BoxBody, MessageBody, SizedStream},
dev::Extensions, dev::Extensions,
error::{Error, JsonPayloadError}, error::{Error, JsonPayloadError},
http::{ http::{
@ -335,18 +335,19 @@ impl HttpResponseBuilder {
} }
} }
if let Some(parts) = self.inner() { let content_length = self
if let Some(length) = parts.headers.get(header::CONTENT_LENGTH) { .inner()
if let Ok(length) = length.to_str() { .and_then(|parts| parts.headers.get(header::CONTENT_LENGTH))
if let Ok(length) = length.parse::<u64>() { .and_then(|value| value.to_str().ok())
self.no_chunking(length); .and_then(|value| value.parse::<u64>().ok());
}
}
}
}
if let Some(len) = content_length {
self.no_chunking(len);
self.body(SizedStream::new(len, stream))
} else {
self.body(BodyStream::new(stream)) self.body(BodyStream::new(stream))
} }
}
/// Set a JSON body and build the `HttpResponse`. /// Set a JSON body and build the `HttpResponse`.
/// ///