diff --git a/src/lib.rs b/src/lib.rs index a3ca52ed..71582393 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,6 +85,7 @@ mod service; pub mod error; pub mod h1; pub mod h2; +pub mod payload; pub mod test; pub mod ws; diff --git a/src/payload.rs b/src/payload.rs new file mode 100644 index 00000000..ede1281e --- /dev/null +++ b/src/payload.rs @@ -0,0 +1,32 @@ +use bytes::Bytes; +use derive_more::From; +use futures::{Poll, Stream}; +use h2::RecvStream; + +use crate::error::PayloadError; + +#[derive(From)] +pub enum Payload { + H1(crate::h1::Payload), + H2(crate::h2::Payload), + Dyn(Box>), +} + +impl From for Payload { + fn from(v: RecvStream) -> Self { + Payload::H2(crate::h2::Payload::new(v)) + } +} + +impl Stream for Payload { + type Item = Bytes; + type Error = PayloadError; + + fn poll(&mut self) -> Poll, Self::Error> { + match self { + Payload::H1(ref mut pl) => pl.poll(), + Payload::H2(ref mut pl) => pl.poll(), + Payload::Dyn(ref mut pl) => pl.poll(), + } + } +} diff --git a/src/request.rs b/src/request.rs index 4df95d45..f6be69dd 100644 --- a/src/request.rs +++ b/src/request.rs @@ -10,8 +10,7 @@ use crate::error::PayloadError; use crate::extensions::Extensions; use crate::httpmessage::HttpMessage; use crate::message::{Message, MessagePool, RequestHead}; - -use crate::h1::Payload; +use crate::payload::Payload; /// Request pub struct Request

{ @@ -39,7 +38,7 @@ impl Request { /// Create new Request instance pub fn new() -> Request { Request { - payload: Some(Payload::empty()), + payload: None, inner: MessagePool::get_message(), } } @@ -55,9 +54,12 @@ impl Request { } /// Create new Request instance - pub fn set_payload

(self, payload: P) -> Request

{ + pub fn set_payload(self, payload: I) -> Request

+ where + I: Into

, + { Request { - payload: Some(payload), + payload: Some(payload.into()), inner: self.inner.clone(), } } diff --git a/src/service/mod.rs b/src/service/mod.rs index 3939ab99..83a40bd1 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -1,5 +1,3 @@ -use h2::RecvStream; - mod senderror; pub use self::senderror::{SendError, SendResponse}; diff --git a/src/test.rs b/src/test.rs index c68bbf8e..cd160e60 100644 --- a/src/test.rs +++ b/src/test.rs @@ -6,8 +6,8 @@ use cookie::Cookie; use http::header::HeaderName; use http::{HeaderMap, HttpTryFrom, Method, Uri, Version}; -use crate::h1::Payload; use crate::header::{Header, IntoHeaderValue}; +use crate::payload::Payload; use crate::Request; /// Test `Request` builder @@ -125,9 +125,9 @@ impl TestRequest { /// Set request payload pub fn set_payload>(mut self, data: B) -> Self { - let mut payload = Payload::empty(); + let mut payload = crate::h1::Payload::empty(); payload.unread_data(data.into()); - self.payload = Some(payload); + self.payload = Some(payload.into()); self } @@ -151,7 +151,7 @@ impl TestRequest { let mut req = if let Some(pl) = payload { Request::with_payload(pl) } else { - Request::with_payload(Payload::empty()) + Request::with_payload(crate::h1::Payload::empty().into()) }; let inner = req.inner_mut();