Skip to content

Commit 0888623

Browse files
authoredOct 24, 2022
feat(body): update Body trait to use Frames (#3020)
The `Body` trait was adjusted to be forwards compatible with adding new frame types. That resulted in changing from `poll_data` and `poll_trailers` to a single `poll_frame` function. More can be learned from the proposal in #2840. Closes #3010 BREAKING CHANGE: The polling functions of the `Body` trait have been redesigned. The free functions `hyper::body::to_bytes` and `aggregate` have been removed. Similar functionality is on `http_body_util::BodyExt::collect`.
1 parent 91e83b7 commit 0888623

19 files changed

+222
-319
lines changed
 

‎Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ futures-core = { version = "0.3", default-features = false }
2727
futures-channel = "0.3"
2828
futures-util = { version = "0.3", default-features = false }
2929
http = "0.2"
30-
http-body = { git = "https://github.com/hyperium/http-body", rev = "6d7dd17" }
31-
http-body-util = { git = "https://github.com/hyperium/http-body", rev = "6d7dd17" }
30+
http-body = { git = "https://github.com/hyperium/http-body", rev = "0e20ca9" }
31+
http-body-util = { git = "https://github.com/hyperium/http-body", rev = "0e20ca9" }
3232
httpdate = "1.0"
3333
httparse = "1.6"
3434
h2 = { version = "0.3.9", optional = true }

‎benches/body.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ extern crate test;
66
use bytes::Buf;
77
use futures_util::stream;
88
use futures_util::StreamExt;
9-
use http_body_util::StreamBody;
9+
use http_body::Frame;
10+
use http_body_util::{BodyExt, StreamBody};
1011

1112
macro_rules! bench_stream {
1213
($bencher:ident, bytes: $bytes:expr, count: $count:expr, $total_ident:ident, $body_pat:pat, $block:expr) => {{
@@ -21,7 +22,8 @@ macro_rules! bench_stream {
2122
$bencher.iter(|| {
2223
rt.block_on(async {
2324
let $body_pat = StreamBody::new(
24-
stream::iter(__s.iter()).map(|&s| Ok::<_, std::convert::Infallible>(s)),
25+
stream::iter(__s.iter())
26+
.map(|&s| Ok::<_, std::convert::Infallible>(Frame::data(s))),
2527
);
2628

2729
$block;
@@ -39,7 +41,7 @@ macro_rules! benches {
3941
#[bench]
4042
fn $name(b: &mut test::Bencher) {
4143
bench_stream!(b, bytes: $bytes, count: $count, total, body, {
42-
let buf = hyper::body::aggregate(body).await.unwrap();
44+
let buf = BodyExt::collect(body).await.unwrap().aggregate();
4345
assert_eq!(buf.remaining(), total);
4446
});
4547
}
@@ -55,7 +57,7 @@ macro_rules! benches {
5557
bench_stream!(b, bytes: $bytes, count: $count, total, mut body, {
5658
let mut vec = Vec::new();
5759
while let Some(chunk) = body.next().await {
58-
vec.extend_from_slice(&chunk.unwrap());
60+
vec.extend_from_slice(&chunk.unwrap().into_data().unwrap());
5961
}
6062
assert_eq!(vec.len(), total);
6163
});
@@ -70,7 +72,7 @@ macro_rules! benches {
7072
#[bench]
7173
fn $name(b: &mut test::Bencher) {
7274
bench_stream!(b, bytes: $bytes, count: $count, total, body, {
73-
let bytes = hyper::body::to_bytes(body).await.unwrap();
75+
let bytes = BodyExt::collect(body).await.unwrap().to_bytes();
7476
assert_eq!(bytes.len(), total);
7577
});
7678
}

‎benches/server.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use futures_util::{stream, StreamExt};
1313
use http_body_util::{BodyExt, Full, StreamBody};
1414
use tokio::sync::oneshot;
1515

16+
use hyper::body::Frame;
1617
use hyper::server::conn::http1;
1718
use hyper::service::service_fn;
1819
use hyper::Response;
@@ -109,7 +110,7 @@ fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
109110
bench_server!(b, ("content-length", "1000000"), move || {
110111
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
111112
BodyExt::boxed(StreamBody::new(
112-
stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)),
113+
stream::iter(S.iter()).map(|&s| Ok::<_, String>(Frame::data(s))),
113114
))
114115
})
115116
}
@@ -133,7 +134,7 @@ fn throughput_chunked_many_chunks(b: &mut test::Bencher) {
133134
bench_server!(b, ("transfer-encoding", "chunked"), || {
134135
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
135136
BodyExt::boxed(StreamBody::new(
136-
stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)),
137+
stream::iter(S.iter()).map(|&s| Ok::<_, String>(Frame::data(s))),
137138
))
138139
})
139140
}

‎examples/client.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use std::env;
44

55
use bytes::Bytes;
6-
use http_body_util::Empty;
7-
use hyper::{body::Body as _, Request};
6+
use http_body_util::{BodyExt, Empty};
7+
use hyper::Request;
88
use tokio::io::{self, AsyncWriteExt as _};
99
use tokio::net::TcpStream;
1010

@@ -62,9 +62,11 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
6262

6363
// Stream the body, writing each chunk to stdout as we get it
6464
// (instead of buffering and printing at the end).
65-
while let Some(next) = res.data().await {
66-
let chunk = next?;
67-
io::stdout().write_all(&chunk).await?;
65+
while let Some(next) = res.frame().await {
66+
let frame = next?;
67+
if let Some(chunk) = frame.data_ref() {
68+
io::stdout().write_all(&chunk).await?;
69+
}
6870
}
6971

7072
println!("\n\nDone!");

‎examples/client_json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![warn(rust_2018_idioms)]
33

44
use bytes::Bytes;
5-
use http_body_util::Empty;
5+
use http_body_util::{BodyExt, Empty};
66
use hyper::{body::Buf, Request};
77
use serde::Deserialize;
88
use tokio::net::TcpStream;
@@ -48,7 +48,7 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
4848
let res = sender.send_request(req).await?;
4949

5050
// asynchronously aggregate the chunks of the body
51-
let body = hyper::body::aggregate(res).await?;
51+
let body = res.collect().await?.aggregate();
5252

5353
// try to parse as json with serde_json
5454
let users = serde_json::from_reader(body.reader())?;

‎examples/echo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async fn echo(req: Request<Recv>) -> Result<Response<BoxBody<Bytes, hyper::Error
5050
return Ok(resp);
5151
}
5252

53-
let whole_body = hyper::body::to_bytes(req.into_body()).await?;
53+
let whole_body = req.collect().await?.to_bytes();
5454

5555
let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>();
5656
Ok(Response::new(full(reversed_body)))

‎examples/params.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async fn param_example(
2525
(&Method::GET, "/") | (&Method::GET, "/post") => Ok(Response::new(full(INDEX))),
2626
(&Method::POST, "/post") => {
2727
// Concatenate the body...
28-
let b = hyper::body::to_bytes(req).await?;
28+
let b = req.collect().await?.to_bytes();
2929
// Parse the request body. form_urlencoded::parse
3030
// always succeeds, but in general parsing may
3131
// fail (for example, an invalid post of json), so

‎examples/single_threaded.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use std::net::SocketAddr;
66
use std::rc::Rc;
77
use tokio::net::TcpListener;
88

9-
use hyper::body::{Body as HttpBody, Bytes};
10-
use hyper::header::{HeaderMap, HeaderValue};
9+
use hyper::body::{Body as HttpBody, Bytes, Frame};
1110
use hyper::service::service_fn;
1211
use hyper::{Error, Response};
1312
use std::marker::PhantomData;
@@ -33,18 +32,11 @@ impl HttpBody for Body {
3332
type Data = Bytes;
3433
type Error = Error;
3534

36-
fn poll_data(
35+
fn poll_frame(
3736
self: Pin<&mut Self>,
3837
_: &mut Context<'_>,
39-
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
40-
Poll::Ready(self.get_mut().data.take().map(Ok))
41-
}
42-
43-
fn poll_trailers(
44-
self: Pin<&mut Self>,
45-
_: &mut Context<'_>,
46-
) -> Poll<Result<Option<HeaderMap<HeaderValue>>, Self::Error>> {
47-
Poll::Ready(Ok(None))
38+
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
39+
Poll::Ready(self.get_mut().data.take().map(|d| Ok(Frame::data(d))))
4840
}
4941
}
5042

‎examples/web_api.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async fn client_request_response() -> Result<Response<BoxBody>> {
4848

4949
async fn api_post_response(req: Request<Recv>) -> Result<Response<BoxBody>> {
5050
// Aggregate the body...
51-
let whole_body = hyper::body::aggregate(req).await?;
51+
let whole_body = req.collect().await?.aggregate();
5252
// Decode as JSON...
5353
let mut data: serde_json::Value = serde_json::from_reader(whole_body.reader())?;
5454
// Change the JSON...

‎src/body/aggregate.rs

-31
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.