@@ -3,13 +3,14 @@ use crate::error::ServiceError;
3
3
use crate :: file_stream:: FileStream ;
4
4
use crate :: { spawn_blocking, AxumResponse , ConduitResponse } ;
5
5
6
+ use std:: collections:: BTreeMap ;
6
7
use std:: error:: Error ;
7
8
use std:: future:: Future ;
8
9
use std:: pin:: Pin ;
9
10
use std:: sync:: Arc ;
10
11
11
12
use axum:: body:: { Body , HttpBody } ;
12
- use axum:: extract:: Extension ;
13
+ use axum:: extract:: { rejection :: PathRejection , Extension , FromRequestParts , Path } ;
13
14
use axum:: handler:: Handler as AxumHandler ;
14
15
use axum:: response:: IntoResponse ;
15
16
use conduit:: { Handler , RequestExt } ;
@@ -57,18 +58,29 @@ where
57
58
{
58
59
type Future = Pin < Box < dyn Future < Output = AxumResponse > + Send > > ;
59
60
60
- fn call ( self , request : Request < Body > , _state : S ) -> Self :: Future {
61
+ fn call ( self , request : Request < Body > , state : S ) -> Self :: Future {
61
62
Box :: pin ( async move {
62
63
if let Err ( response) = check_content_length ( & request) {
63
64
return response. into_response ( ) ;
64
65
}
65
66
66
- let ( parts, body) = request. into_parts ( ) ;
67
+ let ( mut parts, body) = request. into_parts ( ) ;
68
+
69
+ // Make `axum::Router` path params available to `conduit` compat
70
+ // handlers. (see [RequestParamsExt] below)
71
+ match Params :: from_request_parts ( & mut parts, & state) . await {
72
+ Ok ( path) => {
73
+ parts. extensions . insert ( path) ;
74
+ }
75
+ Err ( PathRejection :: MissingPathParams ( _) ) => { }
76
+ Err ( err) => return err. into_response ( ) ,
77
+ } ;
67
78
68
79
let full_body = match hyper:: body:: to_bytes ( body) . await {
69
80
Ok ( body) => body,
70
81
Err ( err) => return server_error_response ( & err) ,
71
82
} ;
83
+
72
84
let request = Request :: from_parts ( parts, full_body) ;
73
85
74
86
let Self ( handler) = self ;
@@ -174,3 +186,15 @@ fn check_content_length(request: &Request<Body>) -> Result<(), AxumResponse> {
174
186
175
187
Ok ( ( ) )
176
188
}
189
+
190
+ pub type Params = Path < BTreeMap < String , String > > ;
191
+
192
+ pub trait RequestParamsExt < ' a > {
193
+ fn axum_params ( self ) -> Option < & ' a Params > ;
194
+ }
195
+
196
+ impl < ' a > RequestParamsExt < ' a > for & ' a ( dyn RequestExt + ' a ) {
197
+ fn axum_params ( self ) -> Option < & ' a Params > {
198
+ self . extensions ( ) . get :: < Params > ( )
199
+ }
200
+ }
0 commit comments