Struct DefaultBodyLimit
#[must_use]
pub struct DefaultBodyLimit { /* private fields */ }
Layer for configuring the default request body limit.
For security reasons, Bytes will, by default, not accept bodies larger than 2MB. This also
applies to extractors that uses Bytes internally such as String, Json, and Form.
This middleware provides ways to configure that.
Note that if an extractor consumes the body directly with Body::poll_frame, or similar, the
default limit is not applied.
Difference between DefaultBodyLimit and RequestBodyLimit
DefaultBodyLimit and RequestBodyLimit serve similar functions but in different ways.
DefaultBodyLimit is local in that it only applies to FromRequest implementations that
explicitly apply it (or call another extractor that does). You can apply the limit with
RequestExt::with_limited_body or RequestExt::into_limited_body
RequestBodyLimit is applied globally to all requests, regardless of which extractors are
used or how the body is consumed.
Example
use ;
let app = new
.route
// change the default limit
.layer;
# let _: Router = app;
In general using DefaultBodyLimit is recommended but if you need to use third party
extractors and want to make sure a limit is also applied there then RequestBodyLimit should
be used.
Different limits for different routes
DefaultBodyLimit can also be selectively applied to have different limits for different
routes:
use ;
let app = new
// this route has a different limit
.route
// this route still has the default limit
.route;
# let _: Router = app;
Implementations
impl DefaultBodyLimit
const fn disable() -> SelfDisable the default request body limit.
This must be used to receive bodies larger than the default limit of 2MB using
Bytesor an extractor built on it such asString,Json,Form.Note that if you're accepting data from untrusted remotes it is recommend to add your own limit such as
tower_http::limit.Example
use ; use RequestBodyLimitLayer; let app: = new .route // Disable the default limit .layer // Set a different limit .layer;const fn max(limit: usize) -> SelfSet the default request body limit.
By default the limit of request body sizes that
Bytes::from_request(and other extractors built on top of it such asString,Json, andForm) is 2MB. This method can be used to change that limit.Example
use ; let app: = new .route // Replace the default of 2MB with 1024 bytes. .layer;fn apply<B>(self, req: &mut Request<B>)Apply a request body limit to the given request.
This can be used, for example, to modify the default body limit inside a specific extractor.
Example
An extractor similar to
Bytes, but limiting the body to 1 KB.use ; ;
Trait Implementations
impl Clone for DefaultBodyLimit
fn clone(&self) -> DefaultBodyLimit
impl Copy for DefaultBodyLimit
impl Debug for DefaultBodyLimit
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<S> Layer<S> for DefaultBodyLimit
type Service = DefaultBodyLimitService<S>;fn layer(&self, inner: S) -> Self::Service
Auto Trait Implementations
impl Freeze for DefaultBodyLimit
impl RefUnwindSafe for DefaultBodyLimit
impl Send for DefaultBodyLimit
impl Sync for DefaultBodyLimit
impl Unpin for DefaultBodyLimit
impl UnsafeUnpin for DefaultBodyLimit
impl UnwindSafe for DefaultBodyLimit
Blanket Implementations
impl<T> Any for DefaultBodyLimit
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for DefaultBodyLimit
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for DefaultBodyLimit
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for DefaultBodyLimit
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for DefaultBodyLimit
fn from(t: T) -> TReturns the argument unchanged.
impl<T> FromRef<T> for DefaultBodyLimit
where
T: Clone,
fn from_ref(input: &T) -> T
impl<T> Instrument for DefaultBodyLimit
impl<T> ToOwned for DefaultBodyLimit
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T> WithSubscriber for DefaultBodyLimit
impl<T, U> Into<U> for DefaultBodyLimit
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for DefaultBodyLimit
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for DefaultBodyLimit
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>