Skip to content

Commit

Permalink
feat(body): implement http_body::Body for hyper::Body
Browse files Browse the repository at this point in the history
This adds a `http_body::Body` impl for hypers `Body`. This should
allow us to start moving to a more generic body trait based on
`BufStream` and `http-body`.
  • Loading branch information
LucioFranco authored and seanmonstar committed May 16, 2019
1 parent 973f981 commit 2d9f349
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bytes = "0.4.4"
futures = "0.1.21"
futures-cpupool = { version = "0.1.6", optional = true }
http = "0.1.15"
http-body = "0.1"
httparse = "1.0"
h2 = "0.1.10"
iovec = "0.1"
Expand All @@ -30,6 +31,7 @@ log = "0.4"
net2 = { version = "0.2.32", optional = true }
time = "0.1"
tokio = { version = "0.1.14", optional = true, default-features = false, features = ["rt-full"] }
tokio-buf = "0.1"
tokio-executor = { version = "0.1.0", optional = true }
tokio-io = "0.1"
tokio-reactor = { version = "0.1", optional = true }
Expand Down
31 changes: 31 additions & 0 deletions src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;
use bytes::Bytes;
use futures::sync::{mpsc, oneshot};
use futures::{Async, Future, Poll, Stream, Sink, AsyncSink, StartSend};
use tokio_buf::SizeHint;
use h2;
use http::HeaderMap;

Expand Down Expand Up @@ -332,6 +333,36 @@ impl Payload for Body {
}
}

impl ::http_body::Body for Body {
type Data = Chunk;
type Error = ::Error;

fn poll_data(&mut self) -> Poll<Option<Self::Data>, Self::Error> {
<Self as Payload>::poll_data(self)
}

fn poll_trailers(&mut self) -> Poll<Option<HeaderMap>, Self::Error> {
<Self as Payload>::poll_trailers(self)
}

fn is_end_stream(&self) -> bool {
<Self as Payload>::is_end_stream(self)
}

fn size_hint(&self) -> SizeHint {
let mut hint = SizeHint::default();

let content_length = <Self as Payload>::content_length(self);

if let Some(size) = content_length {
hint.set_upper(size);
hint.set_lower(size)
}

hint
}
}

impl Stream for Body {
type Item = Chunk;
type Error = ::Error;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ extern crate bytes;
#[cfg(feature = "runtime")] extern crate futures_cpupool;
extern crate h2;
#[doc(hidden)] pub extern crate http;
extern crate http_body;
extern crate httparse;
extern crate iovec;
extern crate itoa;
#[macro_use] extern crate log;
#[cfg(feature = "runtime")] extern crate net2;
extern crate time;
#[cfg(feature = "runtime")] extern crate tokio;
extern crate tokio_buf;
#[cfg(feature = "runtime")] extern crate tokio_executor;
#[macro_use] extern crate tokio_io;
#[cfg(feature = "runtime")] extern crate tokio_reactor;
Expand Down

0 comments on commit 2d9f349

Please sign in to comment.