mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-28 07:20:12 +01:00
Implement FromRequest for BytesMut (#2583)
Co-authored-by: Yann Simon <yann.simon@commercetools.com>
This commit is contained in:
parent
69fac0a065
commit
352b7cf044
1 changed files with 32 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
|||
use super::{rejection::*, FromRequest, FromRequestParts, Request};
|
||||
use crate::{body::Body, RequestExt};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use http::{request::Parts, Extensions, HeaderMap, Method, Uri, Version};
|
||||
use http_body_util::BodyExt;
|
||||
use std::convert::Infallible;
|
||||
|
@ -71,6 +71,37 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequest<S> for BytesMut
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = BytesRejection;
|
||||
|
||||
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let mut body = req.into_limited_body();
|
||||
let mut bytes = BytesMut::new();
|
||||
body_to_bytes_mut(&mut body, &mut bytes).await?;
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
async fn body_to_bytes_mut(body: &mut Body, bytes: &mut BytesMut) -> Result<(), BytesRejection> {
|
||||
while let Some(frame) = body
|
||||
.frame()
|
||||
.await
|
||||
.transpose()
|
||||
.map_err(FailedToBufferBody::from_err)?
|
||||
{
|
||||
let Ok(data) = frame.into_data() else {
|
||||
return Ok(());
|
||||
};
|
||||
bytes.put(data);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequest<S> for Bytes
|
||||
where
|
||||
|
|
Loading…
Reference in a new issue