mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-26 20:40:29 +00:00
Add DefaultBodyLimit::apply
(#3368)
This commit is contained in:
parent
7eabf7e645
commit
8202c66a4d
@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
# Unreleased
|
||||
|
||||
- **added:** `DefaultBodyLimit::apply` for changing the `DefaultBodyLimit` inside extractors.
|
||||
([#3368])
|
||||
|
||||
[#3368]: https://github.com/tokio-rs/axum/pull/3366
|
||||
|
||||
# 0.5.2
|
||||
|
||||
- **added:** Implement `Stream::size_hint` for `BodyDataStream` ([#3195])
|
||||
|
@ -1,4 +1,5 @@
|
||||
use self::private::DefaultBodyLimitService;
|
||||
use http::Request;
|
||||
use tower_layer::Layer;
|
||||
|
||||
/// Layer for configuring the default request body limit.
|
||||
@ -151,6 +152,36 @@ impl DefaultBodyLimit {
|
||||
kind: DefaultBodyLimitKind::Limit(limit),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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`](bytes::Bytes), but limiting the body to 1 KB.
|
||||
///
|
||||
/// ```
|
||||
/// use axum::{
|
||||
/// extract::{DefaultBodyLimit, FromRequest, rejection::BytesRejection, Request},
|
||||
/// body::Bytes,
|
||||
/// };
|
||||
///
|
||||
/// struct Bytes1KB(Bytes);
|
||||
///
|
||||
/// impl<S: Sync> FromRequest<S> for Bytes1KB {
|
||||
/// type Rejection = BytesRejection;
|
||||
///
|
||||
/// async fn from_request(mut req: Request, _: &S) -> Result<Self, Self::Rejection> {
|
||||
/// DefaultBodyLimit::max(1024).apply(&mut req);
|
||||
/// Ok(Self(Bytes::from_request(req, &()).await?))
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn apply<B>(self, req: &mut Request<B>) {
|
||||
req.extensions_mut().insert(self.kind);
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Layer<S> for DefaultBodyLimit {
|
||||
|
Loading…
x
Reference in New Issue
Block a user