Skip to content

Commit 6bd9642

Browse files
authored
middleware/require_user_agent: Move cdn_user_agent field into server config (#5688)
1 parent 1f32ebe commit 6bd9642

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub struct Server {
4242
pub blocked_routes: HashSet<String>,
4343
pub version_id_cache_size: u64,
4444
pub version_id_cache_ttl: Duration,
45+
pub cdn_user_agent: String,
4546
}
4647

4748
impl Default for Server {
@@ -147,6 +148,8 @@ impl Default for Server {
147148
version_id_cache_ttl: Duration::from_secs(
148149
env_optional("VERSION_ID_CACHE_TTL").unwrap_or(DEFAULT_VERSION_ID_CACHE_TTL),
149150
),
151+
cdn_user_agent: dotenv::var("WEB_CDN_USER_AGENT")
152+
.unwrap_or_else(|_| "Amazon CloudFront".into()),
150153
}
151154
}
152155
}

src/middleware/require_user_agent.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,27 @@
88
//! 0.17 (released alongside rustc 1.17).
99
1010
use super::prelude::*;
11-
use std::env;
11+
use crate::middleware::app::RequestApp;
1212

1313
use crate::util::request_header;
1414

1515
#[derive(Default)]
1616
pub struct RequireUserAgent {
17-
cdn_user_agent: String,
1817
handler: Option<Box<dyn Handler>>,
1918
}
2019

2120
impl AroundMiddleware for RequireUserAgent {
2221
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());
2522
self.handler = Some(handler);
2623
}
2724
}
2825

2926
impl Handler for RequireUserAgent {
3027
fn call(&self, req: &mut dyn RequestExt) -> AfterResult {
28+
let cdn_user_agent = &req.app().config.cdn_user_agent;
29+
3130
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;
3332
let is_download = req.path().ends_with("download");
3433
if !has_user_agent && !is_download {
3534
add_custom_metadata(req, "cause", "no user agent");

src/tests/util/test_app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ fn simple_config() -> config::Server {
362362
blocked_routes: HashSet::new(),
363363
version_id_cache_size: 10000,
364364
version_id_cache_ttl: Duration::from_secs(5 * 60),
365+
cdn_user_agent: "Amazon CloudFront".to_string(),
365366
}
366367
}
367368

0 commit comments

Comments
 (0)