From f70c36eff006526f66f61a4c0ff53f9f5369028b Mon Sep 17 00:00:00 2001 From: Yinuo Deng Date: Fri, 14 Apr 2023 23:32:45 +0800 Subject: [PATCH] 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)) }