@@ -50,6 +50,11 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
5050 E : FromRequestParts < S > + ' static ,
5151 S : Send + Sync ;
5252
53+ /// Apply the [default body limit](crate::extract::DefaultBodyLimit).
54+ ///
55+ /// If it is disabled, return the request as-is in `Err`.
56+ fn with_limited_body ( self ) -> Result < Request < Limited < B > > , Request < B > > ;
57+
5358 /// Consumes the request, returning the body wrapped in [`Limited`] if a
5459 /// [default limit](crate::extract::DefaultBodyLimit) is in place, or not wrapped if the
5560 /// default limit is disabled.
@@ -112,19 +117,25 @@ where
112117 } )
113118 }
114119
115- fn into_limited_body ( self ) -> Result < Limited < B > , B > {
120+ fn with_limited_body ( self ) -> Result < Request < Limited < B > > , Request < B > > {
116121 // update docs in `axum-core/src/extract/default_body_limit.rs` and
117122 // `axum/src/docs/extract.md` if this changes
118123 const DEFAULT_LIMIT : usize = 2_097_152 ; // 2 mb
119124
120125 match self . extensions ( ) . get :: < DefaultBodyLimitKind > ( ) . copied ( ) {
121- Some ( DefaultBodyLimitKind :: Disable ) => Err ( self . into_body ( ) ) ,
126+ Some ( DefaultBodyLimitKind :: Disable ) => Err ( self ) ,
122127 Some ( DefaultBodyLimitKind :: Limit ( limit) ) => {
123- Ok ( http_body:: Limited :: new ( self . into_body ( ) , limit) )
128+ Ok ( self . map ( |b| http_body:: Limited :: new ( b , limit) ) )
124129 }
125- None => Ok ( http_body:: Limited :: new ( self . into_body ( ) , DEFAULT_LIMIT ) ) ,
130+ None => Ok ( self . map ( |b| http_body:: Limited :: new ( b , DEFAULT_LIMIT ) ) ) ,
126131 }
127132 }
133+
134+ fn into_limited_body ( self ) -> Result < Limited < B > , B > {
135+ self . with_limited_body ( )
136+ . map ( Request :: into_body)
137+ . map_err ( Request :: into_body)
138+ }
128139}
129140
130141#[ cfg( test) ]
0 commit comments