@@ -69,18 +69,16 @@ use combinators::{
69
69
Add2 , Base , End , Inject , InjectAll , Link , Map , MapErr , MapErrs , Path , Then , TryMap , TryThen ,
70
70
} ;
71
71
use handler:: { Chain , Handler , NotFound } ;
72
- use reply:: Reply ;
72
+ use reply:: { redirect , Reply } ;
73
73
use tree:: { Cluster , Node , Params , Parser , PathSpec , Route , Segment } ;
74
74
75
75
use frunk_core:: {
76
76
hlist:: { HList , HNil } ,
77
77
indices:: Here ,
78
78
} ;
79
79
use futures:: future:: { ready, BoxFuture , FutureExt , NeverError , Ready } ;
80
- use http:: { Extensions , HeaderMap , HeaderValue , Uri , Version } ;
81
- use hyper:: {
82
- header:: LOCATION , server:: conn:: AddrStream , service:: Service , Body , Method , Request , StatusCode ,
83
- } ;
80
+ use http:: { Extensions , HeaderMap , Uri , Version } ;
81
+ use hyper:: { server:: conn:: AddrStream , service:: Service , Body , Method , Request } ;
84
82
use std:: {
85
83
borrow:: Cow ,
86
84
collections:: HashMap ,
@@ -152,15 +150,7 @@ impl Service<Request<Body>> for AppService {
152
150
}
153
151
154
152
/// The set of request scoped state that contexts are initialized with.
155
- pub type Init = R ! [
156
- Body ,
157
- Method ,
158
- Uri ,
159
- Version ,
160
- HeaderMap <HeaderValue >,
161
- Extensions ,
162
- SocketAddr ,
163
- ] ;
153
+ pub type Init = R ! [ Body , Method , Uri , Version , HeaderMap , Extensions , SocketAddr ] ;
164
154
165
155
/// Contains routes and handlers for a given http application.
166
156
///
@@ -321,11 +311,12 @@ impl App {
321
311
}
322
312
}
323
313
324
- fn lookup_route ( & self , method : & Method , path : & str ) -> Option < Route < ' _ , dyn Handler > > {
314
+ fn lookup_route ( & self , method : & Method , path : & str ) -> Route < ' _ , dyn Handler > {
325
315
method_idx ( method)
326
316
. map ( |i| & self . common [ i] )
327
317
. or_else ( || self . custom . get ( method) )
328
318
. map ( |n| n. lookup ( path) )
319
+ . unwrap_or_default ( )
329
320
}
330
321
331
322
fn dispatch ( & self , req : Request < Body > , addr : SocketAddr ) -> BoxFuture < ' static , Response > {
@@ -341,30 +332,21 @@ impl App {
341
332
Cow :: Owned ( s)
342
333
}
343
334
344
- let method = req. method ( ) ;
345
335
let path = req. uri ( ) . path ( ) ;
336
+ let route = self . lookup_route ( req. method ( ) , path) ;
346
337
347
- match self . lookup_route ( method, path) {
348
- Some ( Route { entry : Some ( h) , .. } ) => h. handle ( req, addr) ,
349
-
350
- Some ( Route { tsr : true , .. } ) if method != Method :: CONNECT && path != "/" => {
351
- let code = if let Method :: GET = * method {
352
- StatusCode :: MOVED_PERMANENTLY
353
- } else {
354
- StatusCode :: PERMANENT_REDIRECT
355
- } ;
356
-
357
- let resp = hyper:: Response :: builder ( )
358
- . status ( code)
359
- . header ( LOCATION , & * swap_trailing_slash ( path) )
360
- . body ( Body :: empty ( ) )
361
- . unwrap ( ) ;
362
-
363
- Box :: pin ( async { resp } )
364
- }
338
+ // there's a handler matching this uri
339
+ if let Some ( h) = route. entry {
340
+ return h. handle ( req, addr) ;
341
+ }
365
342
366
- _ => self . not_found . handle ( req, addr) ,
343
+ // there's a handler matching this uri if the trailing slash is swapped
344
+ if route. tsr && req. method ( ) != Method :: CONNECT && path != "/" {
345
+ return ready ( redirect ( false , swap_trailing_slash ( path) ) ) . boxed ( ) ;
367
346
}
347
+
348
+ // there's no handler matching this uri
349
+ self . not_found . handle ( req, addr)
368
350
}
369
351
370
352
/// Create a test client for this app.
0 commit comments