This commit is contained in:
Yinuo Deng 2023-06-09 15:15:19 +01:00 committed by GitHub
commit 03cd664f46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -10,6 +10,8 @@
### Changed ### Changed
- Handler functions can now receive up to 16 extractor parameters. - Handler functions can now receive up to 16 extractor parameters.
- `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. [#2306]
## 4.3.1 - 2023-02-26 ## 4.3.1 - 2023-02-26

View File

@ -317,12 +317,50 @@ impl HttpResponseBuilder {
/// Set a streaming body and build the `HttpResponse`. /// Set a streaming body and build the `HttpResponse`.
/// ///
/// `HttpResponseBuilder` can not be used after this call. /// `HttpResponseBuilder` can not be used after this call.
///
/// if `Content-Type` is not set, then it is automatically set to `application/octet-stream`.
///
/// if `Content-Length` is set, then `no_chunk()` is automatically called.
#[inline] #[inline]
pub fn streaming<S, E>(&mut self, stream: S) -> HttpResponse pub fn streaming<S, E>(&mut self, stream: S) -> HttpResponse
where where
S: Stream<Item = Result<Bytes, E>> + 'static, S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static, E: Into<BoxError> + 'static,
{ {
// Set mime type to application/octet-stream if it is not set
let contains_mime = if let Some(parts) = self.inner() {
parts.headers.contains_key(header::CONTENT_TYPE)
} else {
true
};
if !contains_mime {
self.insert_header((header::CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM));
}
let contains_length = if let Some(parts) = self.inner() {
parts.headers.contains_key(header::CONTENT_LENGTH)
} else {
false
};
if contains_length {
// Since contains_length is true, these two lines will not panic
let parts = self.inner().unwrap();
let length = parts.headers.get(header::CONTENT_LENGTH).unwrap().to_str();
if let Ok(length) = length {
// length is now of type &str
if let Ok(length) = length.parse::<u64>() {
//length is now of type u64
// Set no_chunking
// Since no_chuking() uses insert_header(),
// this will not lead to duplicated header even if it exists
self.no_chunking(length);
}
}
}
self.body(BodyStream::new(stream)) self.body(BodyStream::new(stream))
} }