2018-11-18 22:48:42 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-11-15 10:54:11 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-11-18 22:48:42 +01:00
|
|
|
use std::{fmt, mem};
|
2017-10-24 08:25:32 +02:00
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2019-11-15 10:54:11 +01:00
|
|
|
use futures::Stream;
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2019-02-12 20:07:42 +01:00
|
|
|
use crate::error::Error;
|
2017-10-24 08:25:32 +02:00
|
|
|
|
2018-11-19 02:52:56 +01:00
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
2019-03-27 17:24:55 +01:00
|
|
|
/// Body size hint
|
|
|
|
pub enum BodySize {
|
2018-11-14 07:53:30 +01:00
|
|
|
None,
|
2018-11-18 22:48:42 +01:00
|
|
|
Empty,
|
2018-11-14 07:53:30 +01:00
|
|
|
Sized(usize),
|
2018-11-17 06:30:37 +01:00
|
|
|
Sized64(u64),
|
2018-11-18 05:21:28 +01:00
|
|
|
Stream,
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 17:24:55 +01:00
|
|
|
impl BodySize {
|
2019-02-06 20:44:15 +01:00
|
|
|
pub fn is_eof(&self) -> bool {
|
|
|
|
match self {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::None
|
|
|
|
| BodySize::Empty
|
|
|
|
| BodySize::Sized(0)
|
|
|
|
| BodySize::Sized64(0) => true,
|
2019-02-06 20:44:15 +01:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
/// Type that provides this trait can be streamed to a peer.
|
2019-11-15 10:54:11 +01:00
|
|
|
pub trait MessageBody: Unpin {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize;
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Result<Bytes, Error>>>;
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageBody for () {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Empty
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, _: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
|
|
|
Poll::Ready(None)
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 05:52:45 +01:00
|
|
|
impl<T: MessageBody> MessageBody for Box<T> {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
|
|
|
self.as_ref().size()
|
2019-03-26 05:52:45 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
|
|
|
self.as_mut().poll_next(cx)
|
2019-03-26 05:52:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 16:49:24 +01:00
|
|
|
pub enum ResponseBody<B> {
|
|
|
|
Body(B),
|
|
|
|
Other(Body),
|
|
|
|
}
|
|
|
|
|
2019-02-06 20:44:15 +01:00
|
|
|
impl ResponseBody<Body> {
|
|
|
|
pub fn into_body<B>(self) -> ResponseBody<B> {
|
|
|
|
match self {
|
|
|
|
ResponseBody::Body(b) => ResponseBody::Other(b),
|
|
|
|
ResponseBody::Other(b) => ResponseBody::Other(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 06:15:18 +01:00
|
|
|
impl<B> ResponseBody<B> {
|
|
|
|
pub fn take_body(&mut self) -> ResponseBody<B> {
|
|
|
|
std::mem::replace(self, ResponseBody::Other(Body::None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 16:49:24 +01:00
|
|
|
impl<B: MessageBody> ResponseBody<B> {
|
|
|
|
pub fn as_ref(&self) -> Option<&B> {
|
|
|
|
if let ResponseBody::Body(ref b) = self {
|
|
|
|
Some(b)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: MessageBody> MessageBody for ResponseBody<B> {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2018-11-21 16:49:24 +01:00
|
|
|
match self {
|
2019-04-10 21:24:17 +02:00
|
|
|
ResponseBody::Body(ref body) => body.size(),
|
|
|
|
ResponseBody::Other(ref body) => body.size(),
|
2018-11-21 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-11-21 16:49:24 +01:00
|
|
|
match self {
|
2019-11-15 10:54:11 +01:00
|
|
|
ResponseBody::Body(ref mut body) => body.poll_next(cx),
|
|
|
|
ResponseBody::Other(ref mut body) => body.poll_next(cx),
|
2018-11-21 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 06:19:12 +01:00
|
|
|
impl<B: MessageBody> Stream for ResponseBody<B> {
|
2019-11-15 10:54:11 +01:00
|
|
|
type Item = Result<Bytes, Error>;
|
2019-03-06 06:19:12 +01:00
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
self.get_mut().poll_next(cx)
|
2019-03-06 06:19:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
/// Represents various types of http message body.
|
|
|
|
pub enum Body {
|
|
|
|
/// Empty response. `Content-Length` header is not set.
|
|
|
|
None,
|
|
|
|
/// Zero sized response body. `Content-Length` header is set to `0`.
|
|
|
|
Empty,
|
|
|
|
/// Specific response body.
|
2017-10-24 08:25:32 +02:00
|
|
|
Bytes(Bytes),
|
2018-11-18 22:48:42 +01:00
|
|
|
/// Generic message body.
|
|
|
|
Message(Box<dyn MessageBody>),
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl Body {
|
|
|
|
/// Create body from slice (copy)
|
|
|
|
pub fn from_slice(s: &[u8]) -> Body {
|
|
|
|
Body::Bytes(Bytes::from(s))
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
2018-01-14 23:40:39 +01:00
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
/// Create body from generic message body.
|
|
|
|
pub fn from_message<B: MessageBody + 'static>(body: B) -> Body {
|
|
|
|
Body::Message(Box::new(body))
|
2018-01-14 23:40:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl MessageBody for Body {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2018-11-18 22:48:42 +01:00
|
|
|
match self {
|
2019-03-27 17:24:55 +01:00
|
|
|
Body::None => BodySize::None,
|
|
|
|
Body::Empty => BodySize::Empty,
|
|
|
|
Body::Bytes(ref bin) => BodySize::Sized(bin.len()),
|
2019-04-10 21:24:17 +02:00
|
|
|
Body::Message(ref body) => body.size(),
|
2018-01-14 23:40:39 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:25:32 +02:00
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-01-11 00:28:33 +01:00
|
|
|
match self {
|
2019-11-15 10:54:11 +01:00
|
|
|
Body::None => Poll::Ready(None),
|
|
|
|
Body::Empty => Poll::Ready(None),
|
2018-11-18 22:48:42 +01:00
|
|
|
Body::Bytes(ref mut bin) => {
|
2018-11-19 02:52:56 +01:00
|
|
|
let len = bin.len();
|
|
|
|
if len == 0 {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(None)
|
2018-11-18 22:48:42 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(mem::replace(bin, Bytes::new()))))
|
2018-11-18 22:48:42 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Body::Message(ref mut body) => body.poll_next(cx),
|
2018-01-11 00:28:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl PartialEq for Body {
|
|
|
|
fn eq(&self, other: &Body) -> bool {
|
|
|
|
match *self {
|
|
|
|
Body::None => match *other {
|
|
|
|
Body::None => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Body::Empty => match *other {
|
|
|
|
Body::Empty => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Body::Bytes(ref b) => match *other {
|
|
|
|
Body::Bytes(ref b2) => b == b2,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Body::Message(_) => false,
|
|
|
|
}
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl fmt::Debug for Body {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Body::None => write!(f, "Body::None"),
|
2019-04-10 21:24:17 +02:00
|
|
|
Body::Empty => write!(f, "Body::Empty"),
|
2018-11-18 22:48:42 +01:00
|
|
|
Body::Bytes(ref b) => write!(f, "Body::Bytes({:?})", b),
|
|
|
|
Body::Message(_) => write!(f, "Body::Message(_)"),
|
|
|
|
}
|
2017-10-30 06:50:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl From<&'static str> for Body {
|
|
|
|
fn from(s: &'static str) -> Body {
|
|
|
|
Body::Bytes(Bytes::from_static(s.as_ref()))
|
2017-10-24 08:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl From<&'static [u8]> for Body {
|
|
|
|
fn from(s: &'static [u8]) -> Body {
|
2019-01-29 19:14:00 +01:00
|
|
|
Body::Bytes(Bytes::from_static(s))
|
2017-10-24 08:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl From<Vec<u8>> for Body {
|
|
|
|
fn from(vec: Vec<u8>) -> Body {
|
|
|
|
Body::Bytes(Bytes::from(vec))
|
2017-10-24 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl From<String> for Body {
|
|
|
|
fn from(s: String) -> Body {
|
|
|
|
s.into_bytes().into()
|
2017-10-24 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl<'a> From<&'a String> for Body {
|
|
|
|
fn from(s: &'a String) -> Body {
|
|
|
|
Body::Bytes(Bytes::from(AsRef::<[u8]>::as_ref(&s)))
|
2018-03-04 19:33:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl From<Bytes> for Body {
|
|
|
|
fn from(s: Bytes) -> Body {
|
|
|
|
Body::Bytes(s)
|
2018-03-04 19:33:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl From<BytesMut> for Body {
|
|
|
|
fn from(s: BytesMut) -> Body {
|
|
|
|
Body::Bytes(s.freeze())
|
2017-10-24 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 09:09:52 +02:00
|
|
|
|
2019-09-25 05:33:52 +02:00
|
|
|
impl From<serde_json::Value> for Body {
|
|
|
|
fn from(v: serde_json::Value) -> Body {
|
|
|
|
Body::Bytes(v.to_string().into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 13:57:25 +02:00
|
|
|
impl<S> From<SizedStream<S>> for Body
|
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
S: Stream<Item = Result<Bytes, Error>> + Unpin + 'static,
|
2019-06-01 13:57:25 +02:00
|
|
|
{
|
|
|
|
fn from(s: SizedStream<S>) -> Body {
|
|
|
|
Body::from_message(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, E> From<BodyStream<S, E>> for Body
|
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
|
|
|
E: Into<Error> + Unpin + 'static,
|
2019-06-01 13:57:25 +02:00
|
|
|
{
|
|
|
|
fn from(s: BodyStream<S, E>) -> Body {
|
|
|
|
Body::from_message(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
impl MessageBody for Bytes {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(self.len())
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, _: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-11-14 07:53:30 +01:00
|
|
|
if self.is_empty() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(None)
|
2018-11-14 07:53:30 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(mem::replace(self, Bytes::new()))))
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
impl MessageBody for BytesMut {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(self.len())
|
2018-11-18 05:21:28 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, _: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-11-18 05:21:28 +01:00
|
|
|
if self.is_empty() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(None)
|
2018-11-18 05:21:28 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(mem::replace(self, BytesMut::new()).freeze())))
|
2018-11-18 05:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
impl MessageBody for &'static str {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(self.len())
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, _: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-11-14 07:53:30 +01:00
|
|
|
if self.is_empty() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(None)
|
2018-11-14 07:53:30 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(Bytes::from_static(
|
2018-11-14 07:53:30 +01:00
|
|
|
mem::replace(self, "").as_ref(),
|
|
|
|
))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageBody for &'static [u8] {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(self.len())
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, _: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-11-14 07:53:30 +01:00
|
|
|
if self.is_empty() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(None)
|
2018-11-14 07:53:30 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(Bytes::from_static(mem::replace(self, b"")))))
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageBody for Vec<u8> {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(self.len())
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, _: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-11-14 07:53:30 +01:00
|
|
|
if self.is_empty() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(None)
|
2018-11-14 07:53:30 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(Bytes::from(mem::replace(self, Vec::new())))))
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageBody for String {
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(self.len())
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, _: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
2018-11-14 07:53:30 +01:00
|
|
|
if self.is_empty() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(None)
|
2018-11-14 07:53:30 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(Bytes::from(
|
2018-11-14 07:53:30 +01:00
|
|
|
mem::replace(self, String::new()).into_bytes(),
|
|
|
|
))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
/// Type represent streaming body.
|
|
|
|
/// Response does not contain `content-length` header and appropriate transfer encoding is used.
|
|
|
|
pub struct BodyStream<S, E> {
|
2018-11-14 07:53:30 +01:00
|
|
|
stream: S,
|
2018-11-18 22:48:42 +01:00
|
|
|
_t: PhantomData<E>,
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl<S, E> BodyStream<S, E>
|
2018-11-14 07:53:30 +01:00
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>>,
|
2018-11-18 22:48:42 +01:00
|
|
|
E: Into<Error>,
|
2018-11-14 07:53:30 +01:00
|
|
|
{
|
|
|
|
pub fn new(stream: S) -> Self {
|
2018-11-18 22:48:42 +01:00
|
|
|
BodyStream {
|
|
|
|
stream,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl<S, E> MessageBody for BodyStream<S, E>
|
2018-11-14 07:53:30 +01:00
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + Unpin,
|
|
|
|
E: Into<Error> + Unpin,
|
2018-11-14 07:53:30 +01:00
|
|
|
{
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Stream
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
|
|
|
Pin::new(&mut self.stream)
|
|
|
|
.poll_next(cx)
|
|
|
|
.map(|res| res.map(|res| res.map_err(std::convert::Into::into)))
|
2018-11-18 22:48:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Type represent streaming body. This body implementation should be used
|
|
|
|
/// if total size of stream is known. Data get sent as is without using transfer encoding.
|
|
|
|
pub struct SizedStream<S> {
|
2019-06-01 13:57:25 +02:00
|
|
|
size: u64,
|
2018-11-18 22:48:42 +01:00
|
|
|
stream: S,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> SizedStream<S>
|
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
S: Stream<Item = Result<Bytes, Error>>,
|
2018-11-18 22:48:42 +01:00
|
|
|
{
|
2019-06-01 13:57:25 +02:00
|
|
|
pub fn new(size: u64, stream: S) -> Self {
|
2018-11-18 22:48:42 +01:00
|
|
|
SizedStream { size, stream }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> MessageBody for SizedStream<S>
|
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
S: Stream<Item = Result<Bytes, Error>> + Unpin,
|
2018-11-18 22:48:42 +01:00
|
|
|
{
|
2019-04-10 21:24:17 +02:00
|
|
|
fn size(&self) -> BodySize {
|
2019-06-01 13:57:25 +02:00
|
|
|
BodySize::Sized64(self.size)
|
2018-11-18 22:48:42 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
|
|
|
|
Pin::new(&mut self.stream).poll_next(cx)
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 09:09:52 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2018-11-18 22:48:42 +01:00
|
|
|
impl Body {
|
|
|
|
pub(crate) fn get_ref(&self) -> &[u8] {
|
|
|
|
match *self {
|
|
|
|
Body::Bytes(ref bin) => &bin,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 05:32:37 +01:00
|
|
|
}
|
|
|
|
|
2018-11-21 16:49:24 +01:00
|
|
|
impl ResponseBody<Body> {
|
|
|
|
pub(crate) fn get_ref(&self) -> &[u8] {
|
|
|
|
match *self {
|
|
|
|
ResponseBody::Body(ref b) => b.get_ref(),
|
|
|
|
ResponseBody::Other(ref b) => b.get_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 09:09:52 +02:00
|
|
|
#[test]
|
|
|
|
fn test_static_str() {
|
2019-04-10 21:24:17 +02:00
|
|
|
assert_eq!(Body::from("").size(), BodySize::Sized(0));
|
|
|
|
assert_eq!(Body::from("test").size(), BodySize::Sized(4));
|
2018-11-18 22:48:42 +01:00
|
|
|
assert_eq!(Body::from("test").get_ref(), b"test");
|
2019-04-10 21:24:17 +02:00
|
|
|
|
|
|
|
assert_eq!("test".size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(
|
|
|
|
"test".poll_next().unwrap(),
|
|
|
|
Async::Ready(Some(Bytes::from("test")))
|
|
|
|
);
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_static_bytes() {
|
2019-04-10 21:24:17 +02:00
|
|
|
assert_eq!(Body::from(b"test".as_ref()).size(), BodySize::Sized(4));
|
2018-11-18 22:48:42 +01:00
|
|
|
assert_eq!(Body::from(b"test".as_ref()).get_ref(), b"test");
|
|
|
|
assert_eq!(
|
2019-04-10 21:24:17 +02:00
|
|
|
Body::from_slice(b"test".as_ref()).size(),
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(4)
|
2018-11-18 22:48:42 +01:00
|
|
|
);
|
|
|
|
assert_eq!(Body::from_slice(b"test".as_ref()).get_ref(), b"test");
|
2019-04-10 21:24:17 +02:00
|
|
|
|
|
|
|
assert_eq!((&b"test"[..]).size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(
|
|
|
|
(&b"test"[..]).poll_next().unwrap(),
|
|
|
|
Async::Ready(Some(Bytes::from("test")))
|
|
|
|
);
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec() {
|
2019-04-10 21:24:17 +02:00
|
|
|
assert_eq!(Body::from(Vec::from("test")).size(), BodySize::Sized(4));
|
2018-11-18 22:48:42 +01:00
|
|
|
assert_eq!(Body::from(Vec::from("test")).get_ref(), b"test");
|
2019-04-10 21:24:17 +02:00
|
|
|
|
|
|
|
assert_eq!(Vec::from("test").size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(
|
|
|
|
Vec::from("test").poll_next().unwrap(),
|
|
|
|
Async::Ready(Some(Bytes::from("test")))
|
|
|
|
);
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bytes() {
|
2019-04-10 21:24:17 +02:00
|
|
|
let mut b = Bytes::from("test");
|
|
|
|
assert_eq!(Body::from(b.clone()).size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(Body::from(b.clone()).get_ref(), b"test");
|
|
|
|
|
|
|
|
assert_eq!(b.size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(
|
|
|
|
b.poll_next().unwrap(),
|
|
|
|
Async::Ready(Some(Bytes::from("test")))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bytes_mut() {
|
|
|
|
let mut b = BytesMut::from("test");
|
|
|
|
assert_eq!(Body::from(b.clone()).size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(Body::from(b.clone()).get_ref(), b"test");
|
|
|
|
|
|
|
|
assert_eq!(b.size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(
|
|
|
|
b.poll_next().unwrap(),
|
|
|
|
Async::Ready(Some(Bytes::from("test")))
|
|
|
|
);
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|
2017-11-20 05:32:37 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string() {
|
2019-04-10 21:24:17 +02:00
|
|
|
let mut b = "test".to_owned();
|
|
|
|
assert_eq!(Body::from(b.clone()).size(), BodySize::Sized(4));
|
2018-11-18 22:48:42 +01:00
|
|
|
assert_eq!(Body::from(b.clone()).get_ref(), b"test");
|
2019-04-10 21:24:17 +02:00
|
|
|
assert_eq!(Body::from(&b).size(), BodySize::Sized(4));
|
2018-11-18 22:48:42 +01:00
|
|
|
assert_eq!(Body::from(&b).get_ref(), b"test");
|
2019-04-10 21:24:17 +02:00
|
|
|
|
|
|
|
assert_eq!(b.size(), BodySize::Sized(4));
|
|
|
|
assert_eq!(
|
|
|
|
b.poll_next().unwrap(),
|
|
|
|
Async::Ready(Some(Bytes::from("test")))
|
|
|
|
);
|
2018-03-04 19:33:18 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 05:32:37 +01:00
|
|
|
#[test]
|
2019-04-10 21:24:17 +02:00
|
|
|
fn test_unit() {
|
|
|
|
assert_eq!(().size(), BodySize::Empty);
|
|
|
|
assert_eq!(().poll_next().unwrap(), Async::Ready(None));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_box() {
|
|
|
|
let mut val = Box::new(());
|
|
|
|
assert_eq!(val.size(), BodySize::Empty);
|
|
|
|
assert_eq!(val.poll_next().unwrap(), Async::Ready(None));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_body_eq() {
|
|
|
|
assert!(Body::None == Body::None);
|
|
|
|
assert!(Body::None != Body::Empty);
|
|
|
|
assert!(Body::Empty == Body::Empty);
|
|
|
|
assert!(Body::Empty != Body::None);
|
|
|
|
assert!(
|
|
|
|
Body::Bytes(Bytes::from_static(b"1"))
|
|
|
|
== Body::Bytes(Bytes::from_static(b"1"))
|
|
|
|
);
|
|
|
|
assert!(Body::Bytes(Bytes::from_static(b"1")) != Body::None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_body_debug() {
|
|
|
|
assert!(format!("{:?}", Body::None).contains("Body::None"));
|
|
|
|
assert!(format!("{:?}", Body::Empty).contains("Body::Empty"));
|
|
|
|
assert!(format!("{:?}", Body::Bytes(Bytes::from_static(b"1"))).contains("1"));
|
2018-01-11 00:28:33 +01:00
|
|
|
}
|
2019-09-25 05:33:52 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde_json() {
|
|
|
|
use serde_json::json;
|
|
|
|
assert_eq!(
|
|
|
|
Body::from(serde_json::Value::String("test".into())).size(),
|
|
|
|
BodySize::Sized(6)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Body::from(json!({"test-key":"test-value"})).size(),
|
|
|
|
BodySize::Sized(25)
|
|
|
|
);
|
|
|
|
}
|
2017-10-24 09:09:52 +02:00
|
|
|
}
|