From c76056bc7e8afddf3fdabb961a3e549a004c5b17 Mon Sep 17 00:00:00 2001 From: Yinuo Deng Date: Fri, 14 Apr 2023 21:43:31 +0800 Subject: [PATCH 1/5] Set mime type for stream response if it is not set by the user --- actix-web/src/response/builder.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/actix-web/src/response/builder.rs b/actix-web/src/response/builder.rs index 120d4c358..c454ee6da 100644 --- a/actix-web/src/response/builder.rs +++ b/actix-web/src/response/builder.rs @@ -323,6 +323,17 @@ impl HttpResponseBuilder { S: Stream> + 'static, E: Into + '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)); + } + self.body(BodyStream::new(stream)) } From f70c36eff006526f66f61a4c0ff53f9f5369028b Mon Sep 17 00:00:00 2001 From: Yinuo Deng Date: Fri, 14 Apr 2023 23:32:45 +0800 Subject: [PATCH 2/5] Automatically use no_chunking if user specifies stream size --- actix-web/src/response/builder.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/actix-web/src/response/builder.rs b/actix-web/src/response/builder.rs index c454ee6da..bcbb01000 100644 --- a/actix-web/src/response/builder.rs +++ b/actix-web/src/response/builder.rs @@ -334,6 +334,27 @@ impl HttpResponseBuilder { 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::() { //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)) } From c66233e22593ae42bfccbc3395f7dd2069de32db Mon Sep 17 00:00:00 2001 From: Yinuo Deng Date: Fri, 14 Apr 2023 23:59:23 +0800 Subject: [PATCH 3/5] Run rustfmt on c76056b and f70c36e --- actix-web/src/response/builder.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/actix-web/src/response/builder.rs b/actix-web/src/response/builder.rs index bcbb01000..b814cc3fc 100644 --- a/actix-web/src/response/builder.rs +++ b/actix-web/src/response/builder.rs @@ -345,8 +345,10 @@ impl HttpResponseBuilder { 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::() { //length is now of type u64 + if let Ok(length) = length { + // length is now of type &str + if let Ok(length) = length.parse::() { + //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 From e6b8af5aad7ecac884f2f4b7c8daee29aa327970 Mon Sep 17 00:00:00 2001 From: Yinuo Deng Date: Sat, 15 Apr 2023 00:06:29 +0800 Subject: [PATCH 4/5] Updated CHANGES.md for c76056b and f70c36e --- actix-web/CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index d5989982a..35046c790 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -10,6 +10,8 @@ ### Changed - 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 From 0ae1bbd25dccb0ec98c148aa6e82b05bae34d1b8 Mon Sep 17 00:00:00 2001 From: Yinuo Deng Date: Sat, 15 Apr 2023 00:10:32 +0800 Subject: [PATCH 5/5] Updated documentation comments for c76056b and f70c36e --- actix-web/src/response/builder.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/actix-web/src/response/builder.rs b/actix-web/src/response/builder.rs index b814cc3fc..2daacc055 100644 --- a/actix-web/src/response/builder.rs +++ b/actix-web/src/response/builder.rs @@ -317,6 +317,10 @@ impl HttpResponseBuilder { /// Set a streaming body and build the `HttpResponse`. /// /// `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] pub fn streaming(&mut self, stream: S) -> HttpResponse where