File tree Expand file tree Collapse file tree 3 files changed +8
-5
lines changed Expand file tree Collapse file tree 3 files changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ pub struct Server {
42
42
pub blocked_routes : HashSet < String > ,
43
43
pub version_id_cache_size : u64 ,
44
44
pub version_id_cache_ttl : Duration ,
45
+ pub cdn_user_agent : String ,
45
46
}
46
47
47
48
impl Default for Server {
@@ -147,6 +148,8 @@ impl Default for Server {
147
148
version_id_cache_ttl : Duration :: from_secs (
148
149
env_optional ( "VERSION_ID_CACHE_TTL" ) . unwrap_or ( DEFAULT_VERSION_ID_CACHE_TTL ) ,
149
150
) ,
151
+ cdn_user_agent : dotenv:: var ( "WEB_CDN_USER_AGENT" )
152
+ . unwrap_or_else ( |_| "Amazon CloudFront" . into ( ) ) ,
150
153
}
151
154
}
152
155
}
Original file line number Diff line number Diff line change 8
8
//! 0.17 (released alongside rustc 1.17).
9
9
10
10
use super :: prelude:: * ;
11
- use std :: env ;
11
+ use crate :: middleware :: app :: RequestApp ;
12
12
13
13
use crate :: util:: request_header;
14
14
15
15
#[ derive( Default ) ]
16
16
pub struct RequireUserAgent {
17
- cdn_user_agent : String ,
18
17
handler : Option < Box < dyn Handler > > ,
19
18
}
20
19
21
20
impl AroundMiddleware for RequireUserAgent {
22
21
fn with_handler ( & mut self , handler : Box < dyn Handler > ) {
23
- self . cdn_user_agent =
24
- env:: var ( "WEB_CDN_USER_AGENT" ) . unwrap_or_else ( |_| "Amazon CloudFront" . into ( ) ) ;
25
22
self . handler = Some ( handler) ;
26
23
}
27
24
}
28
25
29
26
impl Handler for RequireUserAgent {
30
27
fn call ( & self , req : & mut dyn RequestExt ) -> AfterResult {
28
+ let cdn_user_agent = & req. app ( ) . config . cdn_user_agent ;
29
+
31
30
let agent = request_header ( req, header:: USER_AGENT ) ;
32
- let has_user_agent = !agent. is_empty ( ) && agent != self . cdn_user_agent ;
31
+ let has_user_agent = !agent. is_empty ( ) && agent != cdn_user_agent;
33
32
let is_download = req. path ( ) . ends_with ( "download" ) ;
34
33
if !has_user_agent && !is_download {
35
34
add_custom_metadata ( req, "cause" , "no user agent" ) ;
Original file line number Diff line number Diff line change @@ -362,6 +362,7 @@ fn simple_config() -> config::Server {
362
362
blocked_routes : HashSet :: new ( ) ,
363
363
version_id_cache_size : 10000 ,
364
364
version_id_cache_ttl : Duration :: from_secs ( 5 * 60 ) ,
365
+ cdn_user_agent : "Amazon CloudFront" . to_string ( ) ,
365
366
}
366
367
}
367
368
You can’t perform that action at this time.
0 commit comments