2017-11-30 23:42:20 +01:00
|
|
|
use std::fmt;
|
2017-10-24 08:25:32 +02:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
2017-10-24 08:39:01 +02:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2017-11-30 23:42:20 +01:00
|
|
|
use futures::Stream;
|
2017-10-24 08:25:32 +02:00
|
|
|
|
2017-11-30 23:42:20 +01:00
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
pub(crate) type BodyStream = Box<Stream<Item=Bytes, Error=Error>>;
|
2017-11-03 21:35:34 +01:00
|
|
|
|
2017-10-24 08:25:32 +02:00
|
|
|
|
|
|
|
/// Represents various types of http message body.
|
|
|
|
pub enum Body {
|
|
|
|
/// Empty response. `Content-Length` header is set to `0`
|
|
|
|
Empty,
|
|
|
|
/// Specific response body.
|
2017-11-10 22:42:32 +01:00
|
|
|
Binary(Binary),
|
2017-10-24 08:25:32 +02:00
|
|
|
/// Unspecified streaming response. Developer is responsible for setting
|
|
|
|
/// right `Content-Length` or `Transfer-Encoding` headers.
|
2017-11-30 23:42:20 +01:00
|
|
|
Streaming(BodyStream),
|
2017-10-24 08:25:32 +02:00
|
|
|
/// Upgrade connection.
|
2017-11-30 23:42:20 +01:00
|
|
|
Upgrade(BodyStream),
|
|
|
|
/// Special body type for actor streaming response.
|
|
|
|
StreamingContext,
|
|
|
|
/// Special body type for actor upgrade response.
|
|
|
|
UpgradeContext,
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents various types of binary body.
|
|
|
|
/// `Content-Length` header is set to length of the body.
|
2017-11-27 19:39:47 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
2017-11-10 22:42:32 +01:00
|
|
|
pub enum Binary {
|
2017-10-24 08:25:32 +02:00
|
|
|
/// Bytes body
|
|
|
|
Bytes(Bytes),
|
|
|
|
/// Static slice
|
|
|
|
Slice(&'static [u8]),
|
|
|
|
/// Shared bytes body
|
|
|
|
SharedBytes(Rc<Bytes>),
|
2017-10-24 08:49:27 +02:00
|
|
|
/// Shared stirng body
|
|
|
|
SharedString(Rc<String>),
|
2017-10-24 08:25:32 +02:00
|
|
|
/// Shared bytes body
|
|
|
|
#[doc(hidden)]
|
|
|
|
ArcSharedBytes(Arc<Bytes>),
|
2017-10-24 08:49:27 +02:00
|
|
|
/// Shared string body
|
|
|
|
#[doc(hidden)]
|
|
|
|
ArcSharedString(Arc<String>),
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Body {
|
2017-11-20 05:55:37 +01:00
|
|
|
/// Does this body streaming.
|
2017-12-14 01:44:35 +01:00
|
|
|
#[inline]
|
2017-11-20 05:55:37 +01:00
|
|
|
pub fn is_streaming(&self) -> bool {
|
2017-10-24 08:25:32 +02:00
|
|
|
match *self {
|
2017-11-30 23:42:20 +01:00
|
|
|
Body::Streaming(_) | Body::StreamingContext
|
|
|
|
| Body::Upgrade(_) | Body::UpgradeContext => true,
|
2017-10-24 08:25:32 +02:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 01:44:23 +01:00
|
|
|
|
|
|
|
/// Is this binary body.
|
2017-12-14 01:44:35 +01:00
|
|
|
#[inline]
|
2017-11-09 01:44:23 +01:00
|
|
|
pub fn is_binary(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Body::Binary(_) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:25:32 +02:00
|
|
|
|
2017-10-24 08:39:01 +02:00
|
|
|
/// Create body from slice (copy)
|
2017-10-26 01:25:26 +02:00
|
|
|
pub fn from_slice(s: &[u8]) -> Body {
|
2017-11-10 22:42:32 +01:00
|
|
|
Body::Binary(Binary::Bytes(Bytes::from(s)))
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:42:20 +01:00
|
|
|
impl PartialEq for Body {
|
|
|
|
fn eq(&self, other: &Body) -> bool {
|
|
|
|
match *self {
|
|
|
|
Body::Empty => match *other {
|
|
|
|
Body::Empty => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Body::Binary(ref b) => match *other {
|
|
|
|
Body::Binary(ref b2) => b == b2,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Body::StreamingContext => match *other {
|
|
|
|
Body::StreamingContext => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Body::UpgradeContext => match *other {
|
|
|
|
Body::UpgradeContext => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Body::Streaming(_) | Body::Upgrade(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Body {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Body::Empty => write!(f, "Body::Empty"),
|
|
|
|
Body::Binary(ref b) => write!(f, "Body::Binary({:?})", b),
|
|
|
|
Body::Streaming(_) => write!(f, "Body::Streaming(_)"),
|
|
|
|
Body::Upgrade(_) => write!(f, "Body::Upgrade(_)"),
|
|
|
|
Body::StreamingContext => write!(f, "Body::StreamingContext"),
|
|
|
|
Body::UpgradeContext => write!(f, "Body::UpgradeContext"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl<T> From<T> for Body where T: Into<Binary>{
|
2017-10-24 08:39:01 +02:00
|
|
|
fn from(b: T) -> Body {
|
|
|
|
Body::Binary(b.into())
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl Binary {
|
2017-12-14 01:44:35 +01:00
|
|
|
#[inline]
|
2017-10-26 01:25:26 +02:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
2017-12-14 01:44:35 +01:00
|
|
|
#[inline]
|
2017-10-24 08:39:01 +02:00
|
|
|
pub fn len(&self) -> usize {
|
2017-10-26 01:25:26 +02:00
|
|
|
match *self {
|
2017-11-10 22:42:32 +01:00
|
|
|
Binary::Bytes(ref bytes) => bytes.len(),
|
|
|
|
Binary::Slice(slice) => slice.len(),
|
|
|
|
Binary::SharedBytes(ref bytes) => bytes.len(),
|
|
|
|
Binary::ArcSharedBytes(ref bytes) => bytes.len(),
|
|
|
|
Binary::SharedString(ref s) => s.len(),
|
|
|
|
Binary::ArcSharedString(ref s) => s.len(),
|
2017-10-24 08:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create binary body from slice
|
2017-11-10 22:42:32 +01:00
|
|
|
pub fn from_slice(s: &[u8]) -> Binary {
|
|
|
|
Binary::Bytes(Bytes::from(s))
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<&'static str> for Binary {
|
|
|
|
fn from(s: &'static str) -> Binary {
|
|
|
|
Binary::Slice(s.as_ref())
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<&'static [u8]> for Binary {
|
|
|
|
fn from(s: &'static [u8]) -> Binary {
|
|
|
|
Binary::Slice(s)
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<Vec<u8>> for Binary {
|
|
|
|
fn from(vec: Vec<u8>) -> Binary {
|
|
|
|
Binary::Bytes(Bytes::from(vec))
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<String> for Binary {
|
|
|
|
fn from(s: String) -> Binary {
|
|
|
|
Binary::Bytes(Bytes::from(s))
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl<'a> From<&'a String> for Binary {
|
|
|
|
fn from(s: &'a String) -> Binary {
|
|
|
|
Binary::Bytes(Bytes::from(AsRef::<[u8]>::as_ref(&s)))
|
2017-10-30 06:50:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<Bytes> for Binary {
|
|
|
|
fn from(s: Bytes) -> Binary {
|
|
|
|
Binary::Bytes(s)
|
2017-10-24 08:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<BytesMut> for Binary {
|
|
|
|
fn from(s: BytesMut) -> Binary {
|
|
|
|
Binary::Bytes(s.freeze())
|
2017-10-24 08:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<Rc<Bytes>> for Binary {
|
|
|
|
fn from(body: Rc<Bytes>) -> Binary {
|
|
|
|
Binary::SharedBytes(body)
|
2017-10-24 08:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl<'a> From<&'a Rc<Bytes>> for Binary {
|
|
|
|
fn from(body: &'a Rc<Bytes>) -> Binary {
|
|
|
|
Binary::SharedBytes(Rc::clone(body))
|
2017-10-24 08:44:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<Arc<Bytes>> for Binary {
|
|
|
|
fn from(body: Arc<Bytes>) -> Binary {
|
|
|
|
Binary::ArcSharedBytes(body)
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl<'a> From<&'a Arc<Bytes>> for Binary {
|
|
|
|
fn from(body: &'a Arc<Bytes>) -> Binary {
|
|
|
|
Binary::ArcSharedBytes(Arc::clone(body))
|
2017-10-24 08:44:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<Rc<String>> for Binary {
|
|
|
|
fn from(body: Rc<String>) -> Binary {
|
|
|
|
Binary::SharedString(body)
|
2017-10-24 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl<'a> From<&'a Rc<String>> for Binary {
|
|
|
|
fn from(body: &'a Rc<String>) -> Binary {
|
|
|
|
Binary::SharedString(Rc::clone(body))
|
2017-10-24 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl From<Arc<String>> for Binary {
|
|
|
|
fn from(body: Arc<String>) -> Binary {
|
|
|
|
Binary::ArcSharedString(body)
|
2017-10-24 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl<'a> From<&'a Arc<String>> for Binary {
|
|
|
|
fn from(body: &'a Arc<String>) -> Binary {
|
|
|
|
Binary::ArcSharedString(Arc::clone(body))
|
2017-10-24 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:42:32 +01:00
|
|
|
impl AsRef<[u8]> for Binary {
|
2017-10-24 08:25:32 +02:00
|
|
|
fn as_ref(&self) -> &[u8] {
|
2017-10-26 01:25:26 +02:00
|
|
|
match *self {
|
2017-11-10 22:42:32 +01:00
|
|
|
Binary::Bytes(ref bytes) => bytes.as_ref(),
|
|
|
|
Binary::Slice(slice) => slice,
|
|
|
|
Binary::SharedBytes(ref bytes) => bytes.as_ref(),
|
|
|
|
Binary::ArcSharedBytes(ref bytes) => bytes.as_ref(),
|
|
|
|
Binary::SharedString(ref s) => s.as_bytes(),
|
|
|
|
Binary::ArcSharedString(ref s) => s.as_bytes(),
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 09:09:52 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2017-11-20 05:55:37 +01:00
|
|
|
#[test]
|
|
|
|
fn test_body_is_streaming() {
|
|
|
|
assert_eq!(Body::Empty.is_streaming(), false);
|
|
|
|
assert_eq!(Body::Binary(Binary::from("")).is_streaming(), false);
|
2017-11-30 23:42:20 +01:00
|
|
|
// assert_eq!(Body::Streaming.is_streaming(), true);
|
2017-11-20 05:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 05:32:37 +01:00
|
|
|
#[test]
|
|
|
|
fn test_is_empty() {
|
|
|
|
assert_eq!(Binary::from("").is_empty(), true);
|
|
|
|
assert_eq!(Binary::from("test").is_empty(), false);
|
|
|
|
}
|
|
|
|
|
2017-10-24 09:09:52 +02:00
|
|
|
#[test]
|
|
|
|
fn test_static_str() {
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from("test").len(), 4);
|
|
|
|
assert_eq!(Binary::from("test").as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_static_bytes() {
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(b"test".as_ref()).len(), 4);
|
|
|
|
assert_eq!(Binary::from(b"test".as_ref()).as_ref(), "test".as_bytes());
|
|
|
|
assert_eq!(Binary::from_slice(b"test".as_ref()).len(), 4);
|
|
|
|
assert_eq!(Binary::from_slice(b"test".as_ref()).as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec() {
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(Vec::from("test")).len(), 4);
|
|
|
|
assert_eq!(Binary::from(Vec::from("test")).as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bytes() {
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(Bytes::from("test")).len(), 4);
|
|
|
|
assert_eq!(Binary::from(Bytes::from("test")).as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rc_bytes() {
|
|
|
|
let b = Rc::new(Bytes::from("test"));
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
|
|
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
|
|
|
assert_eq!(Binary::from(&b).len(), 4);
|
|
|
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 06:50:21 +01:00
|
|
|
#[test]
|
|
|
|
fn test_ref_string() {
|
|
|
|
let b = Rc::new("test".to_owned());
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(&b).len(), 4);
|
|
|
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
2017-10-30 06:50:21 +01:00
|
|
|
}
|
|
|
|
|
2017-10-24 09:09:52 +02:00
|
|
|
#[test]
|
|
|
|
fn test_rc_string() {
|
|
|
|
let b = Rc::new("test".to_owned());
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
|
|
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
|
|
|
assert_eq!(Binary::from(&b).len(), 4);
|
|
|
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arc_bytes() {
|
|
|
|
let b = Arc::new(Bytes::from("test"));
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
|
|
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
|
|
|
assert_eq!(Binary::from(&b).len(), 4);
|
|
|
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arc_string() {
|
|
|
|
let b = Arc::new("test".to_owned());
|
2017-11-10 22:42:32 +01:00
|
|
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
|
|
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
|
|
|
assert_eq!(Binary::from(&b).len(), 4);
|
|
|
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
2017-11-20 05:32:37 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string() {
|
|
|
|
let b = "test".to_owned();
|
|
|
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
|
|
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
|
|
|
assert_eq!(Binary::from(&b).len(), 4);
|
|
|
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bytes_mut() {
|
|
|
|
let b = BytesMut::from("test");
|
|
|
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
|
|
|
assert_eq!(Binary::from(b).as_ref(), "test".as_bytes());
|
|
|
|
}
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|