From 61536631927c333711cacd751ffdec740c7c8a35 Mon Sep 17 00:00:00 2001 From: Erik Funder Carstensen Date: Sat, 30 Oct 2021 20:52:10 +0200 Subject: [PATCH 1/3] Make 'mime' field in Body optional --- src/body.rs | 68 ++++++++++++++++++++++++++++++++++--------------- src/request.rs | 4 ++- src/response.rs | 4 ++- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/body.rs b/src/body.rs index 6f664e1..c263ae1 100644 --- a/src/body.rs +++ b/src/body.rs @@ -56,7 +56,7 @@ pin_project_lite::pin_project! { pub struct Body { #[pin] reader: Box, - mime: Mime, + mime: Option, length: Option, bytes_read: u64, } @@ -79,7 +79,7 @@ impl Body { pub fn empty() -> Self { Self { reader: Box::new(io::empty()), - mime: mime::BYTE_STREAM, + mime: Some(mime::BYTE_STREAM), length: Some(0), bytes_read: 0, } @@ -110,7 +110,7 @@ impl Body { ) -> Self { Self { reader: Box::new(reader), - mime: mime::BYTE_STREAM, + mime: Some(mime::BYTE_STREAM), length, bytes_read: 0, } @@ -153,7 +153,7 @@ impl Body { /// ``` pub fn from_bytes(bytes: Vec) -> Self { Self { - mime: mime::BYTE_STREAM, + mime: Some(mime::BYTE_STREAM), length: Some(bytes.len() as u64), reader: Box::new(io::Cursor::new(bytes)), bytes_read: 0, @@ -203,7 +203,7 @@ impl Body { /// ``` pub fn from_string(s: String) -> Self { Self { - mime: mime::PLAIN, + mime: Some(mime::PLAIN), length: Some(s.len() as u64), reader: Box::new(io::Cursor::new(s.into_bytes())), bytes_read: 0, @@ -253,7 +253,7 @@ impl Body { let body = Self { length: Some(bytes.len() as u64), reader: Box::new(io::Cursor::new(bytes)), - mime: mime::JSON, + mime: Some(mime::JSON), bytes_read: 0, }; Ok(body) @@ -322,7 +322,7 @@ impl Body { let body = Self { length: Some(bytes.len() as u64), reader: Box::new(io::Cursor::new(bytes)), - mime: mime::FORM, + mime: Some(mime::FORM), bytes_read: 0, }; Ok(body) @@ -444,7 +444,7 @@ impl Body { .unwrap_or(mime::BYTE_STREAM); Ok(Self { - mime, + mime: Some(mime), length: Some(len), reader: Box::new(io::BufReader::new(file)), bytes_read: 0, @@ -474,13 +474,41 @@ impl Body { } /// Returns the mime type of this Body. - pub fn mime(&self) -> &Mime { - &self.mime + pub fn mime(&self) -> Option<&Mime> { + self.mime.as_ref() } /// Sets the mime type of this Body. + /// + /// # Examples + /// ``` + /// use http_types::Body; + /// use http_types::mime::self; + /// + /// let mut body = Body::default(); + /// body.set_mime(Some(mime::CSS)); + /// assert_eq!(*body.mime(), mime::CSS); + /// ``` pub fn set_mime(&mut self, mime: impl Into) { - self.mime = mime.into(); + self.mime = Some(mime.into()); + } + + /// Unsets the mime type of this Body. + /// + /// # Examples + /// ``` + /// use http_types::Body; + /// use http_types::mime; + /// + /// let mut body = Body::default(); + /// assert!(body.mime().is_some()); + /// + /// body.unset_mime(); + /// assert!(body.mime().is_none()); + /// ``` + /// + pub fn unset_mime(&mut self) { + self.mime = None } /// Create a Body by chaining another Body after this one, consuming both. @@ -508,7 +536,7 @@ impl Body { let mime = if self.mime == other.mime { self.mime.clone() } else { - mime::BYTE_STREAM + Some(mime::BYTE_STREAM) }; let length = match (self.length, other.length) { (Some(l1), Some(l2)) => (l1 - self.bytes_read).checked_add(l2 - other.bytes_read), @@ -728,7 +756,7 @@ mod test { for buf_len in 1..13 { let mut body = Body::from("hello ").chain(Body::from("world")); assert_eq!(body.len(), Some(11)); - assert_eq!(body.mime(), &mime::PLAIN); + assert_eq!(body.mime(), Some(&mime::PLAIN)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" @@ -744,7 +772,7 @@ mod test { for buf_len in 1..13 { let mut body = Body::from(&b"hello "[..]).chain(Body::from("world")); assert_eq!(body.len(), Some(11)); - assert_eq!(body.mime(), &mime::BYTE_STREAM); + assert_eq!(body.mime(), Some(&mime::BYTE_STREAM)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" @@ -761,7 +789,7 @@ mod test { let mut body = Body::from_reader(Cursor::new("hello "), Some(6)).chain(Body::from("world")); assert_eq!(body.len(), Some(11)); - assert_eq!(body.mime(), &mime::BYTE_STREAM); + assert_eq!(body.mime(), Some(&mime::BYTE_STREAM)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" @@ -778,7 +806,7 @@ mod test { let mut body = Body::from_reader(Cursor::new("hello "), None).chain(Body::from("world")); assert_eq!(body.len(), None); - assert_eq!(body.mime(), &mime::BYTE_STREAM); + assert_eq!(body.mime(), Some(&mime::BYTE_STREAM)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" @@ -795,7 +823,7 @@ mod test { let mut body = Body::from("hello ").chain(Body::from_reader(Cursor::new("world"), None)); assert_eq!(body.len(), None); - assert_eq!(body.mime(), &mime::BYTE_STREAM); + assert_eq!(body.mime(), Some(&mime::BYTE_STREAM)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" @@ -812,7 +840,7 @@ mod test { let mut body = Body::from_reader(Cursor::new("hello xyz"), Some(6)) .chain(Body::from_reader(Cursor::new("world abc"), Some(5))); assert_eq!(body.len(), Some(11)); - assert_eq!(body.mime(), &mime::BYTE_STREAM); + assert_eq!(body.mime(), Some(&mime::BYTE_STREAM)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" @@ -830,7 +858,7 @@ mod test { .chain(Body::from(&b" "[..])) .chain(Body::from("world")); assert_eq!(body.len(), Some(11)); - assert_eq!(body.mime(), &mime::BYTE_STREAM); + assert_eq!(body.mime(), Some(&mime::BYTE_STREAM)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" @@ -856,7 +884,7 @@ mod test { let mut body = body1.chain(body2); assert_eq!(body.len(), Some(11)); - assert_eq!(body.mime(), &mime::BYTE_STREAM); + assert_eq!(body.mime(), Some(&mime::BYTE_STREAM)); assert_eq!( read_with_buffers_of_size(&mut body, buf_len).await?, "hello world" diff --git a/src/request.rs b/src/request.rs index 235dea9..f4a1758 100644 --- a/src/request.rs +++ b/src/request.rs @@ -478,7 +478,9 @@ impl Request { /// Copy MIME data from the body. fn copy_content_type_from_body(&mut self) { if self.header(CONTENT_TYPE).is_none() { - self.set_content_type(self.body.mime().clone()); + if let Some(mime) = self.body.mime().cloned() { + self.set_content_type(mime); + } } } diff --git a/src/response.rs b/src/response.rs index 4a55576..11bebf5 100644 --- a/src/response.rs +++ b/src/response.rs @@ -383,7 +383,9 @@ impl Response { /// Copy MIME data from the body. fn copy_content_type_from_body(&mut self) { if self.header(CONTENT_TYPE).is_none() { - self.set_content_type(self.body.mime().clone()); + if let Some(mime) = self.body.mime().cloned() { + self.set_content_type(mime); + } } } From abefece02aa15efb74c1b862f9f4d315e6ee5c02 Mon Sep 17 00:00:00 2001 From: Erik Funder Carstensen Date: Wed, 3 Nov 2021 11:58:22 +0100 Subject: [PATCH 2/3] Fix errors in doc tests --- src/body.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/body.rs b/src/body.rs index c263ae1..b0c600f 100644 --- a/src/body.rs +++ b/src/body.rs @@ -483,11 +483,11 @@ impl Body { /// # Examples /// ``` /// use http_types::Body; - /// use http_types::mime::self; + /// use http_types::mime; /// - /// let mut body = Body::default(); - /// body.set_mime(Some(mime::CSS)); - /// assert_eq!(*body.mime(), mime::CSS); + /// let mut body = Body::empty(); + /// body.set_mime(mime::CSS); + /// assert_eq!(body.mime(), Some(&mime::CSS)); /// ``` pub fn set_mime(&mut self, mime: impl Into) { self.mime = Some(mime.into()); @@ -500,7 +500,7 @@ impl Body { /// use http_types::Body; /// use http_types::mime; /// - /// let mut body = Body::default(); + /// let mut body = Body::empty(); /// assert!(body.mime().is_some()); /// /// body.unset_mime(); From f0d3be8eae0d4239bf717d56cb911d90045705ac Mon Sep 17 00:00:00 2001 From: Erik Funder Carstensen Date: Wed, 3 Nov 2021 16:51:27 +0100 Subject: [PATCH 3/3] Change signature of set_mime --- src/body.rs | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/body.rs b/src/body.rs index b0c600f..42a5c5d 100644 --- a/src/body.rs +++ b/src/body.rs @@ -486,29 +486,14 @@ impl Body { /// use http_types::mime; /// /// let mut body = Body::empty(); - /// body.set_mime(mime::CSS); + /// body.set_mime(Some(mime::CSS)); /// assert_eq!(body.mime(), Some(&mime::CSS)); + /// + /// body.set_mime(None); + /// assert_eq!(body.mime(), None); /// ``` - pub fn set_mime(&mut self, mime: impl Into) { - self.mime = Some(mime.into()); - } - - /// Unsets the mime type of this Body. - /// - /// # Examples - /// ``` - /// use http_types::Body; - /// use http_types::mime; - /// - /// let mut body = Body::empty(); - /// assert!(body.mime().is_some()); - /// - /// body.unset_mime(); - /// assert!(body.mime().is_none()); - /// ``` - /// - pub fn unset_mime(&mut self) { - self.mime = None + pub fn set_mime(&mut self, mime: Option) { + self.mime = mime; } /// Create a Body by chaining another Body after this one, consuming both.