mirror of https://github.com/http-rs/http-types
Merge pull request #387 from halvko/f-unsettable-mime
Make 'mime' field in Body optional
This commit is contained in:
commit
014f301292
55
src/body.rs
55
src/body.rs
|
@ -56,7 +56,7 @@ pin_project_lite::pin_project! {
|
|||
pub struct Body {
|
||||
#[pin]
|
||||
reader: Box<dyn AsyncBufRead + Unpin + Send + Sync + 'static>,
|
||||
mime: Mime,
|
||||
mime: Option<Mime>,
|
||||
length: Option<u64>,
|
||||
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<u8>) -> 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,26 @@ 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.
|
||||
pub fn set_mime(&mut self, mime: impl Into<Mime>) {
|
||||
self.mime = mime.into();
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use http_types::Body;
|
||||
/// use http_types::mime;
|
||||
///
|
||||
/// let mut body = Body::empty();
|
||||
/// 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: Option<Mime>) {
|
||||
self.mime = mime;
|
||||
}
|
||||
|
||||
/// Create a Body by chaining another Body after this one, consuming both.
|
||||
|
@ -508,7 +521,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 +741,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 +757,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 +774,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 +791,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 +808,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 +825,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 +843,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 +869,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"
|
||||
|
|
|
@ -482,7 +482,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -387,7 +387,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue