From 2d9f3490aa04393a12854680aa3e6d6117ba2407 Mon Sep 17 00:00:00 2001 From: Lucio Franco Date: Thu, 16 May 2019 17:21:42 -0400 Subject: [PATCH] feat(body): implement `http_body::Body` for `hyper::Body` 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`. --- Cargo.toml | 2 ++ src/body/body.rs | 31 +++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 3 files changed, 35 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 09b25ed1f5..bc1ec2d2c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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 } diff --git a/src/body/body.rs b/src/body/body.rs index 1b769f4aa5..cf88c9f235 100644 --- a/src/body/body.rs +++ b/src/body/body.rs @@ -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; @@ -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, Self::Error> { + ::poll_data(self) + } + + fn poll_trailers(&mut self) -> Poll, Self::Error> { + ::poll_trailers(self) + } + + fn is_end_stream(&self) -> bool { + ::is_end_stream(self) + } + + fn size_hint(&self) -> SizeHint { + let mut hint = SizeHint::default(); + + let content_length = ::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; diff --git a/src/lib.rs b/src/lib.rs index d15fbaa163..c624d7bee2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ 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; @@ -29,6 +30,7 @@ extern crate itoa; #[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;