1
+ use crate :: body:: Body ;
1
2
use crate :: extract:: { DefaultBodyLimitKind , FromRequest , FromRequestParts } ;
2
3
use futures_util:: future:: BoxFuture ;
3
4
use http:: Request ;
4
5
use http_body:: Limited ;
5
6
6
7
mod sealed {
7
- pub trait Sealed < B > { }
8
- impl < B > Sealed < B > for http:: Request < B > { }
8
+ pub trait Sealed { }
9
+ impl Sealed for http:: Request < crate :: body :: Body > { }
9
10
}
10
11
11
12
/// Extension trait that adds additional methods to [`Request`].
12
- pub trait RequestExt < B > : sealed:: Sealed < B > + Sized {
13
+ pub trait RequestExt : sealed:: Sealed + Sized {
13
14
/// Apply an extractor to this `Request`.
14
15
///
15
16
/// This is just a convenience for `E::from_request(req, &())`.
@@ -23,6 +24,7 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
23
24
/// use axum::{
24
25
/// async_trait,
25
26
/// extract::FromRequest,
27
+ /// body::Body,
26
28
/// http::{header::CONTENT_TYPE, Request, StatusCode},
27
29
/// response::{IntoResponse, Response},
28
30
/// Form, Json, RequestExt,
@@ -31,17 +33,16 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
31
33
/// struct FormOrJson<T>(T);
32
34
///
33
35
/// #[async_trait]
34
- /// impl<S, B, T> FromRequest<S, B > for FormOrJson<T>
36
+ /// impl<S, T> FromRequest<S> for FormOrJson<T>
35
37
/// where
36
- /// Json<T>: FromRequest<(), B >,
37
- /// Form<T>: FromRequest<(), B >,
38
+ /// Json<T>: FromRequest<()>,
39
+ /// Form<T>: FromRequest<()>,
38
40
/// T: 'static,
39
- /// B: Send + 'static,
40
41
/// S: Send + Sync,
41
42
/// {
42
43
/// type Rejection = Response;
43
44
///
44
- /// async fn from_request(req: Request<B >, _state: &S) -> Result<Self, Self::Rejection> {
45
+ /// async fn from_request(req: Request<Body >, _state: &S) -> Result<Self, Self::Rejection> {
45
46
/// let content_type = req
46
47
/// .headers()
47
48
/// .get(CONTENT_TYPE)
@@ -70,7 +71,7 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
70
71
/// ```
71
72
fn extract < E , M > ( self ) -> BoxFuture < ' static , Result < E , E :: Rejection > >
72
73
where
73
- E : FromRequest < ( ) , B , M > + ' static ,
74
+ E : FromRequest < ( ) , M > + ' static ,
74
75
M : ' static ;
75
76
76
77
/// Apply an extractor that requires some state to this `Request`.
@@ -85,6 +86,7 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
85
86
/// ```
86
87
/// use axum::{
87
88
/// async_trait,
89
+ /// body::Body,
88
90
/// extract::{FromRef, FromRequest},
89
91
/// http::Request,
90
92
/// RequestExt,
@@ -95,15 +97,14 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
95
97
/// }
96
98
///
97
99
/// #[async_trait]
98
- /// impl<S, B > FromRequest<S, B > for MyExtractor
100
+ /// impl<S> FromRequest<S> for MyExtractor
99
101
/// where
100
102
/// String: FromRef<S>,
101
103
/// S: Send + Sync,
102
- /// B: Send + 'static,
103
104
/// {
104
105
/// type Rejection = std::convert::Infallible;
105
106
///
106
- /// async fn from_request(req: Request<B >, state: &S) -> Result<Self, Self::Rejection> {
107
+ /// async fn from_request(req: Request<Body >, state: &S) -> Result<Self, Self::Rejection> {
107
108
/// let requires_state = req.extract_with_state::<RequiresState, _, _>(state).await?;
108
109
///
109
110
/// Ok(Self { requires_state })
@@ -114,22 +115,21 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
114
115
/// struct RequiresState { /* ... */ }
115
116
///
116
117
/// #[async_trait]
117
- /// impl<S, B > FromRequest<S, B > for RequiresState
118
+ /// impl<S> FromRequest<S> for RequiresState
118
119
/// where
119
120
/// String: FromRef<S>,
120
121
/// S: Send + Sync,
121
- /// B: Send + 'static,
122
122
/// {
123
123
/// // ...
124
124
/// # type Rejection = std::convert::Infallible;
125
- /// # async fn from_request(req: Request<B >, _state: &S) -> Result<Self, Self::Rejection> {
125
+ /// # async fn from_request(req: Request<Body >, _state: &S) -> Result<Self, Self::Rejection> {
126
126
/// # todo!()
127
127
/// # }
128
128
/// }
129
129
/// ```
130
130
fn extract_with_state < E , S , M > ( self , state : & S ) -> BoxFuture < ' _ , Result < E , E :: Rejection > >
131
131
where
132
- E : FromRequest < S , B , M > + ' static ,
132
+ E : FromRequest < S , M > + ' static ,
133
133
S : Send + Sync ;
134
134
135
135
/// Apply a parts extractor to this `Request`.
@@ -145,6 +145,7 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
145
145
/// headers::{authorization::Bearer, Authorization},
146
146
/// http::Request,
147
147
/// response::{IntoResponse, Response},
148
+ /// body::Body,
148
149
/// Json, RequestExt, TypedHeader,
149
150
/// };
150
151
///
@@ -154,16 +155,15 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
154
155
/// }
155
156
///
156
157
/// #[async_trait]
157
- /// impl<S, B, T> FromRequest<S, B > for MyExtractor<T>
158
+ /// impl<S, T> FromRequest<S> for MyExtractor<T>
158
159
/// where
159
- /// B: Send + 'static,
160
160
/// S: Send + Sync,
161
- /// Json<T>: FromRequest<(), B >,
161
+ /// Json<T>: FromRequest<()>,
162
162
/// T: 'static,
163
163
/// {
164
164
/// type Rejection = Response;
165
165
///
166
- /// async fn from_request(mut req: Request<B >, _state: &S) -> Result<Self, Self::Rejection> {
166
+ /// async fn from_request(mut req: Request<Body >, _state: &S) -> Result<Self, Self::Rejection> {
167
167
/// let TypedHeader(auth_header) = req
168
168
/// .extract_parts::<TypedHeader<Authorization<Bearer>>>()
169
169
/// .await
@@ -197,6 +197,7 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
197
197
/// extract::{FromRef, FromRequest, FromRequestParts},
198
198
/// http::{request::Parts, Request},
199
199
/// response::{IntoResponse, Response},
200
+ /// body::Body,
200
201
/// Json, RequestExt,
201
202
/// };
202
203
///
@@ -206,17 +207,16 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
206
207
/// }
207
208
///
208
209
/// #[async_trait]
209
- /// impl<S, B, T> FromRequest<S, B > for MyExtractor<T>
210
+ /// impl<S, T> FromRequest<S> for MyExtractor<T>
210
211
/// where
211
212
/// String: FromRef<S>,
212
- /// Json<T>: FromRequest<(), B >,
213
+ /// Json<T>: FromRequest<()>,
213
214
/// T: 'static,
214
215
/// S: Send + Sync,
215
- /// B: Send + 'static,
216
216
/// {
217
217
/// type Rejection = Response;
218
218
///
219
- /// async fn from_request(mut req: Request<B >, state: &S) -> Result<Self, Self::Rejection> {
219
+ /// async fn from_request(mut req: Request<Body >, state: &S) -> Result<Self, Self::Rejection> {
220
220
/// let requires_state = req
221
221
/// .extract_parts_with_state::<RequiresState, _>(state)
222
222
/// .await
@@ -260,29 +260,26 @@ pub trait RequestExt<B>: sealed::Sealed<B> + Sized {
260
260
/// Apply the [default body limit](crate::extract::DefaultBodyLimit).
261
261
///
262
262
/// If it is disabled, return the request as-is in `Err`.
263
- fn with_limited_body ( self ) -> Result < Request < Limited < B > > , Request < B > > ;
263
+ fn with_limited_body ( self ) -> Result < Request < Limited < Body > > , Request < Body > > ;
264
264
265
265
/// Consumes the request, returning the body wrapped in [`Limited`] if a
266
266
/// [default limit](crate::extract::DefaultBodyLimit) is in place, or not wrapped if the
267
267
/// default limit is disabled.
268
- fn into_limited_body ( self ) -> Result < Limited < B > , B > ;
268
+ fn into_limited_body ( self ) -> Result < Limited < Body > , Body > ;
269
269
}
270
270
271
- impl < B > RequestExt < B > for Request < B >
272
- where
273
- B : Send + ' static ,
274
- {
271
+ impl RequestExt for Request < Body > {
275
272
fn extract < E , M > ( self ) -> BoxFuture < ' static , Result < E , E :: Rejection > >
276
273
where
277
- E : FromRequest < ( ) , B , M > + ' static ,
274
+ E : FromRequest < ( ) , M > + ' static ,
278
275
M : ' static ,
279
276
{
280
277
self . extract_with_state ( & ( ) )
281
278
}
282
279
283
280
fn extract_with_state < E , S , M > ( self , state : & S ) -> BoxFuture < ' _ , Result < E , E :: Rejection > >
284
281
where
285
- E : FromRequest < S , B , M > + ' static ,
282
+ E : FromRequest < S , M > + ' static ,
286
283
S : Send + Sync ,
287
284
{
288
285
E :: from_request ( self , state)
@@ -324,7 +321,7 @@ where
324
321
} )
325
322
}
326
323
327
- fn with_limited_body ( self ) -> Result < Request < Limited < B > > , Request < B > > {
324
+ fn with_limited_body ( self ) -> Result < Request < Limited < Body > > , Request < Body > > {
328
325
// update docs in `axum-core/src/extract/default_body_limit.rs` and
329
326
// `axum/src/docs/extract.md` if this changes
330
327
const DEFAULT_LIMIT : usize = 2_097_152 ; // 2 mb
@@ -338,7 +335,7 @@ where
338
335
}
339
336
}
340
337
341
- fn into_limited_body ( self ) -> Result < Limited < B > , B > {
338
+ fn into_limited_body ( self ) -> Result < Limited < Body > , Body > {
342
339
self . with_limited_body ( )
343
340
. map ( Request :: into_body)
344
341
. map_err ( Request :: into_body)
@@ -354,11 +351,10 @@ mod tests {
354
351
} ;
355
352
use async_trait:: async_trait;
356
353
use http:: Method ;
357
- use hyper:: Body ;
358
354
359
355
#[ tokio:: test]
360
356
async fn extract_without_state ( ) {
361
- let req = Request :: new ( ( ) ) ;
357
+ let req = Request :: new ( Body :: empty ( ) ) ;
362
358
363
359
let method: Method = req. extract ( ) . await . unwrap ( ) ;
364
360
@@ -376,7 +372,7 @@ mod tests {
376
372
377
373
#[ tokio:: test]
378
374
async fn extract_with_state ( ) {
379
- let req = Request :: new ( ( ) ) ;
375
+ let req = Request :: new ( Body :: empty ( ) ) ;
380
376
381
377
let state = "state" . to_owned ( ) ;
382
378
@@ -387,7 +383,10 @@ mod tests {
387
383
388
384
#[ tokio:: test]
389
385
async fn extract_parts_without_state ( ) {
390
- let mut req = Request :: builder ( ) . header ( "x-foo" , "foo" ) . body ( ( ) ) . unwrap ( ) ;
386
+ let mut req = Request :: builder ( )
387
+ . header ( "x-foo" , "foo" )
388
+ . body ( Body :: empty ( ) )
389
+ . unwrap ( ) ;
391
390
392
391
let method: Method = req. extract_parts ( ) . await . unwrap ( ) ;
393
392
@@ -397,7 +396,10 @@ mod tests {
397
396
398
397
#[ tokio:: test]
399
398
async fn extract_parts_with_state ( ) {
400
- let mut req = Request :: builder ( ) . header ( "x-foo" , "foo" ) . body ( ( ) ) . unwrap ( ) ;
399
+ let mut req = Request :: builder ( )
400
+ . header ( "x-foo" , "foo" )
401
+ . body ( Body :: empty ( ) )
402
+ . unwrap ( ) ;
401
403
402
404
let state = "state" . to_owned ( ) ;
403
405
@@ -417,15 +419,14 @@ mod tests {
417
419
}
418
420
419
421
#[ async_trait]
420
- impl < S , B > FromRequest < S , B > for WorksForCustomExtractor
422
+ impl < S > FromRequest < S > for WorksForCustomExtractor
421
423
where
422
424
S : Send + Sync ,
423
- B : Send + ' static ,
424
- String : FromRef < S > + FromRequest < ( ) , B > ,
425
+ String : FromRef < S > + FromRequest < ( ) > ,
425
426
{
426
- type Rejection = <String as FromRequest < ( ) , B > >:: Rejection ;
427
+ type Rejection = <String as FromRequest < ( ) > >:: Rejection ;
427
428
428
- async fn from_request ( mut req : Request < B > , state : & S ) -> Result < Self , Self :: Rejection > {
429
+ async fn from_request ( mut req : Request < Body > , state : & S ) -> Result < Self , Self :: Rejection > {
429
430
let RequiresState ( from_state) = req. extract_parts_with_state ( state) . await . unwrap ( ) ;
430
431
let method = req. extract_parts ( ) . await . unwrap ( ) ;
431
432
let body = req. extract ( ) . await ?;
0 commit comments