1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

add h1/h2 payload

This commit is contained in:
Nikolay Kim 2019-02-07 13:39:15 -08:00
parent c4596b0bd6
commit 7d49a07f91
5 changed files with 44 additions and 11 deletions

View File

@ -85,6 +85,7 @@ mod service;
pub mod error; pub mod error;
pub mod h1; pub mod h1;
pub mod h2; pub mod h2;
pub mod payload;
pub mod test; pub mod test;
pub mod ws; pub mod ws;

32
src/payload.rs Normal file
View File

@ -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<Stream<Item = Bytes, Error = PayloadError>>),
}
impl From<RecvStream> 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<Option<Self::Item>, 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(),
}
}
}

View File

@ -10,8 +10,7 @@ use crate::error::PayloadError;
use crate::extensions::Extensions; use crate::extensions::Extensions;
use crate::httpmessage::HttpMessage; use crate::httpmessage::HttpMessage;
use crate::message::{Message, MessagePool, RequestHead}; use crate::message::{Message, MessagePool, RequestHead};
use crate::payload::Payload;
use crate::h1::Payload;
/// Request /// Request
pub struct Request<P = Payload> { pub struct Request<P = Payload> {
@ -39,7 +38,7 @@ impl Request<Payload> {
/// Create new Request instance /// Create new Request instance
pub fn new() -> Request<Payload> { pub fn new() -> Request<Payload> {
Request { Request {
payload: Some(Payload::empty()), payload: None,
inner: MessagePool::get_message(), inner: MessagePool::get_message(),
} }
} }
@ -55,9 +54,12 @@ impl<Payload> Request<Payload> {
} }
/// Create new Request instance /// Create new Request instance
pub fn set_payload<P>(self, payload: P) -> Request<P> { pub fn set_payload<I, P>(self, payload: I) -> Request<P>
where
I: Into<P>,
{
Request { Request {
payload: Some(payload), payload: Some(payload.into()),
inner: self.inner.clone(), inner: self.inner.clone(),
} }
} }

View File

@ -1,5 +1,3 @@
use h2::RecvStream;
mod senderror; mod senderror;
pub use self::senderror::{SendError, SendResponse}; pub use self::senderror::{SendError, SendResponse};

View File

@ -6,8 +6,8 @@ use cookie::Cookie;
use http::header::HeaderName; use http::header::HeaderName;
use http::{HeaderMap, HttpTryFrom, Method, Uri, Version}; use http::{HeaderMap, HttpTryFrom, Method, Uri, Version};
use crate::h1::Payload;
use crate::header::{Header, IntoHeaderValue}; use crate::header::{Header, IntoHeaderValue};
use crate::payload::Payload;
use crate::Request; use crate::Request;
/// Test `Request` builder /// Test `Request` builder
@ -125,9 +125,9 @@ impl TestRequest {
/// Set request payload /// Set request payload
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self { pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
let mut payload = Payload::empty(); let mut payload = crate::h1::Payload::empty();
payload.unread_data(data.into()); payload.unread_data(data.into());
self.payload = Some(payload); self.payload = Some(payload.into());
self self
} }
@ -151,7 +151,7 @@ impl TestRequest {
let mut req = if let Some(pl) = payload { let mut req = if let Some(pl) = payload {
Request::with_payload(pl) Request::with_payload(pl)
} else { } else {
Request::with_payload(Payload::empty()) Request::with_payload(crate::h1::Payload::empty().into())
}; };
let inner = req.inner_mut(); let inner = req.inner_mut();