committed by
GitHub
parent
e6b2cbda03
commit
0460cd909e
@@ -13,6 +13,36 @@ use super::HttpBody;
|
||||
/// 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.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # async fn doc() -> hyper::Result<()> {
|
||||
/// use hyper::{body::HttpBody};
|
||||
///
|
||||
/// # let request = hyper::Request::builder()
|
||||
/// # .method(hyper::Method::POST)
|
||||
/// # .uri("http://httpbin.org/post")
|
||||
/// # .header("content-type", "application/json")
|
||||
/// # .body(hyper::Body::from(r#"{"library":"hyper"}"#)).unwrap();
|
||||
/// # let client = hyper::Client::new();
|
||||
/// let response = client.request(request).await?;
|
||||
///
|
||||
/// const MAX_ALLOWED_RESPONSE_SIZE: u64 = 1024;
|
||||
///
|
||||
/// let response_content_length = match response.body().size_hint().upper() {
|
||||
/// Some(v) => v,
|
||||
/// None => MAX_ALLOWED_RESPONSE_SIZE + 1 // Just to protect ourselves from a malicious response
|
||||
/// };
|
||||
///
|
||||
/// if response_content_length < MAX_ALLOWED_RESPONSE_SIZE {
|
||||
/// let body_bytes = hyper::body::to_bytes(response.into_body()).await?;
|
||||
/// println!("body: {:?}", body_bytes);
|
||||
/// }
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error>
|
||||
where
|
||||
T: HttpBody,
|
||||
|
||||
Reference in New Issue
Block a user