Skip to content

Commit

Permalink
feat(body): deprecate to_bytes() and aggregate() (#3466)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Dec 8, 2023
1 parent ad50497 commit 7f382ad
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/client_json.rs
Expand Up @@ -28,6 +28,7 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
let res = client.get(url).await?;

// asynchronously aggregate the chunks of the body
#[allow(deprecated)]
let body = hyper::body::aggregate(res).await?;

// try to parse as json with serde_json
Expand Down
1 change: 1 addition & 0 deletions examples/echo.rs
Expand Up @@ -34,6 +34,7 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
// So here we do `.await` on the future, waiting on concatenating the full body,
// then afterwards the content can be reversed. Only then can we return a `Response`.
(&Method::POST, "/echo/reversed") => {
#[allow(deprecated)]
let whole_body = hyper::body::to_bytes(req.into_body()).await?;

let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>();
Expand Down
1 change: 1 addition & 0 deletions examples/params.rs
Expand Up @@ -17,6 +17,7 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
(&Method::GET, "/") | (&Method::GET, "/post") => Ok(Response::new(INDEX.into())),
(&Method::POST, "/post") => {
// Concatenate the body...
#[allow(deprecated)]
let b = hyper::body::to_bytes(req).await?;
// Parse the request body. form_urlencoded::parse
// always succeeds, but in general parsing may
Expand Down
1 change: 1 addition & 0 deletions examples/web_api.rs
Expand Up @@ -40,6 +40,7 @@ async fn client_request_response(client: &Client<HttpConnector>) -> Result<Respo

async fn api_post_response(req: Request<Body>) -> Result<Response<Body>> {
// Aggregate the body...
#[allow(deprecated)]
let whole_body = hyper::body::aggregate(req).await?;
// Decode as JSON...
let mut data: serde_json::Value = serde_json::from_reader(whole_body.reader())?;
Expand Down
7 changes: 7 additions & 0 deletions src/body/aggregate.rs
Expand Up @@ -13,6 +13,13 @@ use crate::common::buf::BufList;
/// Care needs to be taken if the remote is untrusted. The function doesn't implement any length
/// checks and an malicious peer might make it consume arbitrary amounts of memory. Checking the
/// `Content-Length` is a possibility, but it is not strictly mandated to be present.
#[cfg_attr(
feature = "deprecated",
deprecated(
note = "This function has been replaced by a method on the `hyper::body::HttpBody` trait. Use `.collect().await?.aggregate()` instead."
)
)]
#[cfg_attr(feature = "deprecated", allow(deprecated))]
pub async fn aggregate<T>(body: T) -> Result<impl Buf, T::Error>
where
T: HttpBody,
Expand Down
2 changes: 2 additions & 0 deletions src/body/mod.rs
Expand Up @@ -19,9 +19,11 @@ pub use bytes::{Buf, Bytes};
pub use http_body::Body as HttpBody;
pub use http_body::SizeHint;

#[cfg_attr(feature = "deprecated", allow(deprecated))]
pub use self::aggregate::aggregate;
pub use self::body::{Body, Sender};
pub(crate) use self::length::DecodedLength;
#[cfg_attr(feature = "deprecated", allow(deprecated))]
pub use self::to_bytes::to_bytes;

mod aggregate;
Expand Down
7 changes: 7 additions & 0 deletions src/body/to_bytes.rs
Expand Up @@ -44,6 +44,13 @@ use super::HttpBody;
/// # Ok(())
/// # }
/// ```
#[cfg_attr(
feature = "deprecated",
deprecated(
note = "This function has been replaced by a method on the `hyper::body::HttpBody` trait. Use `.collect().await?.to_bytes()` instead."
)
)]
#[cfg_attr(feature = "deprecated", allow(deprecated))]
pub async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error>
where
T: HttpBody,
Expand Down
3 changes: 2 additions & 1 deletion tests/client.rs
Expand Up @@ -12,6 +12,7 @@ use std::task::{Context, Poll};
use std::thread;
use std::time::Duration;

#[allow(deprecated)]
use hyper::body::to_bytes as concat;
use hyper::{Body, Client, Method, Request, StatusCode};

Expand Down Expand Up @@ -3202,7 +3203,7 @@ mod conn {
let resp = client.send_request(req).await.expect("send_request");
assert!(resp.status().is_success());

let body = hyper::body::to_bytes(resp.into_body())
let body = concat(resp.into_body())
.await
.expect("get response body with no error");

Expand Down
2 changes: 2 additions & 0 deletions tests/support/mod.rs
Expand Up @@ -374,6 +374,7 @@ async fn async_test(cfg: __TestConfig) {
func(&req.headers());
}
let sbody = sreq.body;
#[allow(deprecated)]
hyper::body::to_bytes(req).map_ok(move |body| {
assert_eq!(body.as_ref(), sbody.as_slice(), "client body");

Expand Down Expand Up @@ -433,6 +434,7 @@ async fn async_test(cfg: __TestConfig) {
for func in &cheaders {
func(&res.headers());
}
#[allow(deprecated)]
hyper::body::to_bytes(res)
})
.map_ok(move |body| {
Expand Down

0 comments on commit 7f382ad

Please sign in to comment.