@@ -9,7 +9,7 @@ use mithril_common::MITHRIL_API_VERSION;
9
9
use reqwest:: header:: { HeaderMap , HeaderValue } ;
10
10
use std:: sync:: Arc ;
11
11
use warp:: http:: Method ;
12
- use warp:: Filter ;
12
+ use warp:: { Filter , Rejection } ;
13
13
14
14
/// Routes
15
15
pub fn routes (
@@ -24,15 +24,29 @@ pub fn routes(
24
24
"mithril-api-version" ,
25
25
HeaderValue :: from_static ( MITHRIL_API_VERSION ) ,
26
26
) ;
27
- let header_must_be = warp:: header:: exact ( "API_VERSION" , MITHRIL_API_VERSION ) ;
28
- warp:: any ( ) . and ( warp:: path ( SERVER_BASE_PATH ) ) . and (
29
- certificate_routes:: routes ( dependency_manager. clone ( ) )
30
- . or ( snapshot_routes:: routes ( dependency_manager. clone ( ) ) )
31
- . or ( signer_routes:: routes ( dependency_manager. clone ( ) ) )
32
- . or ( signatures_routes:: routes ( dependency_manager. clone ( ) ) )
33
- . or ( epoch_routes:: routes ( dependency_manager) )
34
- . and ( header_must_be)
35
- . with ( cors)
36
- . with ( warp:: reply:: with:: headers ( headers) ) ,
37
- )
27
+ warp:: any ( )
28
+ . and ( header_must_be ( ) )
29
+ . and ( warp:: path ( SERVER_BASE_PATH ) )
30
+ . and (
31
+ certificate_routes:: routes ( dependency_manager. clone ( ) )
32
+ . or ( snapshot_routes:: routes ( dependency_manager. clone ( ) ) )
33
+ . or ( signer_routes:: routes ( dependency_manager. clone ( ) ) )
34
+ . or ( signatures_routes:: routes ( dependency_manager. clone ( ) ) )
35
+ . or ( epoch_routes:: routes ( dependency_manager) )
36
+ . with ( cors)
37
+ . with ( warp:: reply:: with:: headers ( headers) ) ,
38
+ )
39
+ }
40
+
41
+ /// API Version verification
42
+ fn header_must_be ( ) -> impl Filter < Extract = ( ) , Error = Rejection > + Copy {
43
+ warp:: header:: optional ( "mithril-api-version" )
44
+ . and_then ( |maybe_header : Option < String > | async move {
45
+ match maybe_header {
46
+ None => Ok ( ( ) ) ,
47
+ Some ( version) if version == MITHRIL_API_VERSION => Ok ( ( ) ) ,
48
+ Some ( _version) => Err ( warp:: reject ( ) ) ,
49
+ }
50
+ } )
51
+ . untuple_one ( )
38
52
}
0 commit comments