mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Add RequestExt::{with_limited_body, into_limited_body} (#1420)
* Move RequestExt and RequestPartsExt into axum-core * Add RequestExt::into_limited_body … and use it for Bytes extraction. * Add RequestExt::with_limited_body … and use it for Multipart extraction. Co-authored-by: David Pedersen <[email protected]>
This commit is contained in:
co-authored by
David Pedersen
parent
be54583d98
commit
b94248191e
@@ -16,6 +16,7 @@ mod from_ref;
|
||||
mod request_parts;
|
||||
mod tuple;
|
||||
|
||||
pub(crate) use self::default_body_limit::DefaultBodyLimitKind;
|
||||
pub use self::{default_body_limit::DefaultBodyLimit, from_ref::FromRef};
|
||||
|
||||
mod private {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use super::{
|
||||
default_body_limit::DefaultBodyLimitKind, rejection::*, FromRequest, FromRequestParts,
|
||||
};
|
||||
use crate::BoxError;
|
||||
use super::{rejection::*, FromRequest, FromRequestParts};
|
||||
use crate::{BoxError, RequestExt};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use http::{request::Parts, HeaderMap, Method, Request, Uri, Version};
|
||||
@@ -84,27 +82,13 @@ where
|
||||
type Rejection = BytesRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
// update docs in `axum-core/src/extract/default_body_limit.rs` and
|
||||
// `axum/src/docs/extract.md` if this changes
|
||||
const DEFAULT_LIMIT: usize = 2_097_152; // 2 mb
|
||||
|
||||
let limit_kind = req.extensions().get::<DefaultBodyLimitKind>().copied();
|
||||
let bytes = match limit_kind {
|
||||
Some(DefaultBodyLimitKind::Disable) => crate::body::to_bytes(req.into_body())
|
||||
let bytes = match req.into_limited_body() {
|
||||
Ok(limited_body) => crate::body::to_bytes(limited_body)
|
||||
.await
|
||||
.map_err(FailedToBufferBody::from_err)?,
|
||||
Err(unlimited_body) => crate::body::to_bytes(unlimited_body)
|
||||
.await
|
||||
.map_err(FailedToBufferBody::from_err)?,
|
||||
Some(DefaultBodyLimitKind::Limit(limit)) => {
|
||||
let body = http_body::Limited::new(req.into_body(), limit);
|
||||
crate::body::to_bytes(body)
|
||||
.await
|
||||
.map_err(FailedToBufferBody::from_err)?
|
||||
}
|
||||
None => {
|
||||
let body = http_body::Limited::new(req.into_body(), DEFAULT_LIMIT);
|
||||
crate::body::to_bytes(body)
|
||||
.await
|
||||
.map_err(FailedToBufferBody::from_err)?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(bytes)
|
||||
|
||||
Reference in New Issue
Block a user