@@ -9,6 +9,7 @@ use jsonrpc_http_server::{
9
9
hyper, AccessControlAllowOrigin , CloseHandle , DomainsValidation , RequestMiddleware ,
10
10
RequestMiddlewareAction , ServerBuilder ,
11
11
} ;
12
+ use regex:: Regex ;
12
13
use solana_ledger:: { bank_forks:: BankForks , blockstore:: Blockstore } ;
13
14
use solana_sdk:: hash:: Hash ;
14
15
use std:: {
@@ -28,13 +29,17 @@ pub struct JsonRpcService {
28
29
close_handle : Option < CloseHandle > ,
29
30
}
30
31
31
- #[ derive( Default ) ]
32
32
struct RpcRequestMiddleware {
33
33
ledger_path : PathBuf ,
34
+ snapshot_archive_path_regex : Regex ,
34
35
}
35
36
impl RpcRequestMiddleware {
36
37
pub fn new ( ledger_path : PathBuf ) -> Self {
37
- Self { ledger_path }
38
+ Self {
39
+ ledger_path,
40
+ snapshot_archive_path_regex : Regex :: new ( r"/snapshot-\d+-[[:alnum:]]+\.tar\.bz2$" )
41
+ . unwrap ( ) ,
42
+ }
38
43
}
39
44
40
45
fn not_found ( ) -> hyper:: Response < hyper:: Body > {
@@ -51,9 +56,19 @@ impl RpcRequestMiddleware {
51
56
. unwrap ( )
52
57
}
53
58
54
- fn get ( & self , filename : & str ) -> RequestMiddlewareAction {
55
- info ! ( "get {}" , filename) ;
56
- let filename = self . ledger_path . join ( filename) ;
59
+ fn is_get_path ( & self , path : & str ) -> bool {
60
+ match path {
61
+ "/genesis.tar.bz2" => true ,
62
+ _ => self . snapshot_archive_path_regex . is_match ( path) ,
63
+ }
64
+ }
65
+
66
+ fn get ( & self , path : & str ) -> RequestMiddlewareAction {
67
+ let filename = self . ledger_path . join (
68
+ path. split_at ( 1 ) . 1 , // Drop leading '/' from path
69
+ ) ;
70
+ info ! ( "get {} -> {:?}" , path, filename) ;
71
+
57
72
RequestMiddlewareAction :: Respond {
58
73
should_validate_hosts : true ,
59
74
response : Box :: new (
@@ -73,13 +88,14 @@ impl RpcRequestMiddleware {
73
88
impl RequestMiddleware for RpcRequestMiddleware {
74
89
fn on_request ( & self , request : hyper:: Request < hyper:: Body > ) -> RequestMiddlewareAction {
75
90
trace ! ( "request uri: {}" , request. uri( ) ) ;
76
- match request. uri ( ) . path ( ) {
77
- "/snapshot.tar.bz2" => self . get ( "snapshot.tar.bz2" ) ,
78
- "/genesis.tar.bz2" => self . get ( "genesis.tar.bz2" ) ,
79
- _ => RequestMiddlewareAction :: Proceed {
91
+
92
+ if self . is_get_path ( request. uri ( ) . path ( ) ) {
93
+ self . get ( request. uri ( ) . path ( ) )
94
+ } else {
95
+ RequestMiddlewareAction :: Proceed {
80
96
should_continue_on_invalid_cors : false ,
81
97
request,
82
- } ,
98
+ }
83
99
}
84
100
}
85
101
}
@@ -234,4 +250,25 @@ mod tests {
234
250
rpc_service. exit ( ) ;
235
251
rpc_service. join ( ) . unwrap ( ) ;
236
252
}
253
+
254
+ #[ test]
255
+ fn test_is_get_path ( ) {
256
+ let rrm = RpcRequestMiddleware :: new ( PathBuf :: from ( "/" ) ) ;
257
+
258
+ assert ! ( rrm. is_get_path( "/genesis.tar.bz2" ) ) ;
259
+ assert ! ( !rrm. is_get_path( "genesis.tar.bz2" ) ) ;
260
+
261
+ assert ! ( !rrm. is_get_path( "/snapshot.tar.bz2" ) ) ;
262
+
263
+ assert ! (
264
+ rrm. is_get_path( "/snapshot-100-AvFf9oS8A8U78HdjT9YG2sTTThLHJZmhaMn2g8vkWYnr.tar.bz2" )
265
+ ) ;
266
+ assert ! ( !rrm. is_get_path(
267
+ "/snapshot-notaslotnumber-AvFf9oS8A8U78HdjT9YG2sTTThLHJZmhaMn2g8vkWYnr.tar.bz2"
268
+ ) ) ;
269
+
270
+ assert ! ( !rrm. is_get_path( "/" ) ) ;
271
+ assert ! ( !rrm. is_get_path( ".." ) ) ;
272
+ assert ! ( !rrm. is_get_path( "🎣" ) ) ;
273
+ }
237
274
}
0 commit comments