use std::rc::Rc; use std::sync::Arc; use bytes::{Bytes, BytesMut}; /// Represents various types of http message body. #[derive(Debug)] pub enum Body { /// Empty response. `Content-Length` header is set to `0` Empty, /// Specific response body. Binary(BinaryBody), /// Streaming response body with specified length. Length(u64), /// Unspecified streaming response. Developer is responsible for setting /// right `Content-Length` or `Transfer-Encoding` headers. Streaming, /// Upgrade connection. Upgrade, } /// Represents various types of binary body. /// `Content-Length` header is set to length of the body. #[derive(Debug)] pub enum BinaryBody { /// Bytes body Bytes(Bytes), /// Static slice Slice(&'static [u8]), /// Shared bytes body SharedBytes(Rc), /// Shared stirng body SharedString(Rc), /// Shared bytes body #[doc(hidden)] ArcSharedBytes(Arc), /// Shared string body #[doc(hidden)] ArcSharedString(Arc), } impl Body { /// Does this body have payload. pub fn has_body(&self) -> bool { match *self { Body::Length(_) | Body::Streaming => true, _ => false } } /// Create body from slice (copy) pub fn from_slice<'a>(s: &'a [u8]) -> Body { Body::Binary(BinaryBody::Bytes(Bytes::from(s))) } } impl From for Body where T: Into{ fn from(b: T) -> Body { Body::Binary(b.into()) } } impl BinaryBody { pub fn len(&self) -> usize { match self { &BinaryBody::Bytes(ref bytes) => bytes.len(), &BinaryBody::Slice(slice) => slice.len(), &BinaryBody::SharedBytes(ref bytes) => bytes.len(), &BinaryBody::ArcSharedBytes(ref bytes) => bytes.len(), &BinaryBody::SharedString(ref s) => s.len(), &BinaryBody::ArcSharedString(ref s) => s.len(), } } /// Create binary body from slice pub fn from_slice<'a>(s: &'a [u8]) -> BinaryBody { BinaryBody::Bytes(Bytes::from(s)) } } impl From<&'static str> for BinaryBody { fn from(s: &'static str) -> BinaryBody { BinaryBody::Slice(s.as_ref()) } } impl From<&'static [u8]> for BinaryBody { fn from(s: &'static [u8]) -> BinaryBody { BinaryBody::Slice(s) } } impl From> for BinaryBody { fn from(vec: Vec) -> BinaryBody { BinaryBody::Bytes(Bytes::from(vec)) } } impl From for BinaryBody { fn from(s: String) -> BinaryBody { BinaryBody::Bytes(Bytes::from(s)) } } impl From for BinaryBody { fn from(s: Bytes) -> BinaryBody { BinaryBody::Bytes(s) } } impl From for BinaryBody { fn from(s: BytesMut) -> BinaryBody { BinaryBody::Bytes(s.freeze()) } } impl From> for BinaryBody { fn from(body: Rc) -> BinaryBody { BinaryBody::SharedBytes(body) } } impl<'a> From<&'a Rc> for BinaryBody { fn from(body: &'a Rc) -> BinaryBody { BinaryBody::SharedBytes(Rc::clone(body)) } } impl From> for BinaryBody { fn from(body: Arc) -> BinaryBody { BinaryBody::ArcSharedBytes(body) } } impl<'a> From<&'a Arc> for BinaryBody { fn from(body: &'a Arc) -> BinaryBody { BinaryBody::ArcSharedBytes(Arc::clone(body)) } } impl From> for BinaryBody { fn from(body: Rc) -> BinaryBody { BinaryBody::SharedString(body) } } impl<'a> From<&'a Rc> for BinaryBody { fn from(body: &'a Rc) -> BinaryBody { BinaryBody::SharedString(Rc::clone(body)) } } impl From> for BinaryBody { fn from(body: Arc) -> BinaryBody { BinaryBody::ArcSharedString(body) } } impl<'a> From<&'a Arc> for BinaryBody { fn from(body: &'a Arc) -> BinaryBody { BinaryBody::ArcSharedString(Arc::clone(body)) } } impl AsRef<[u8]> for BinaryBody { fn as_ref(&self) -> &[u8] { match self { &BinaryBody::Bytes(ref bytes) => bytes.as_ref(), &BinaryBody::Slice(slice) => slice, &BinaryBody::SharedBytes(ref bytes) => bytes.as_ref(), &BinaryBody::ArcSharedBytes(ref bytes) => bytes.as_ref(), &BinaryBody::SharedString(ref s) => s.as_bytes(), &BinaryBody::ArcSharedString(ref s) => s.as_bytes(), } } }