chromiumoxide/handler/blockers/
intercept_manager.rs1include!(concat!(env!("OUT_DIR"), "/domain_map.rs"));
2
3impl NetworkInterceptManager {
4 pub fn new(url: &Option<Box<url::Url>>) -> NetworkInterceptManager {
5 if let Some(parsed_url) = url {
6 if let Some(domain) = parsed_url.domain() {
7 let mut domain_parts: Vec<&str> = domain.split('.').collect();
8
9 let base = DOMAIN_MAP.get(if domain_parts.len() >= 2 {
10 domain_parts[domain_parts.len() - 2]
11 } else {
12 domain
13 });
14 let base = if base.is_none() && domain_parts.len() >= 3 {
15 domain_parts.pop();
16 DOMAIN_MAP.get(&domain_parts.join("."))
17 } else {
18 base
19 };
20
21 return *base.unwrap_or(&NetworkInterceptManager::UNKNOWN);
22 }
23 }
24 NetworkInterceptManager::UNKNOWN
25 }
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31 use url::Url;
32
33 fn create_url(url: &str) -> Option<Box<Url>> {
35 Url::parse(url).ok().map(Box::new)
36 }
37
38 #[test]
39 fn test_known_domains() {
40 let cases = vec![
41 ("http://www.tiktok.com", NetworkInterceptManager::TIKTOK),
42 ("https://facebook.com", NetworkInterceptManager::FACEBOOK),
43 ("https://www.amazon.com", NetworkInterceptManager::AMAZON),
44 ("https://subdomain.x.com", NetworkInterceptManager::X),
45 (
46 "https://linkedin.com/in/someone",
47 NetworkInterceptManager::LINKEDIN,
48 ),
49 (
50 "https://www.netflix.com/browse",
51 NetworkInterceptManager::NETFLIX,
52 ),
53 ("https://medium.com", NetworkInterceptManager::MEDIUM),
54 ("https://sub.upwork.com", NetworkInterceptManager::UPWORK),
55 ("https://glassdoor.com", NetworkInterceptManager::GLASSDOOR),
56 ("https://ebay.com", NetworkInterceptManager::EBAY),
57 (
58 "https://nytimes.com/section/world",
59 NetworkInterceptManager::NYTIMES,
60 ),
61 (
62 "https://en.wikipedia.org/wiki/Rust",
63 NetworkInterceptManager::WIKIPEDIA,
64 ),
65 (
66 "https://market.tcgplayer.com",
67 NetworkInterceptManager::TCGPLAYER,
68 ),
69 ];
70
71 for (url, expected) in cases {
72 assert_eq!(NetworkInterceptManager::new(&create_url(url)), expected);
73 }
74 }
75
76 #[test]
77 fn test_unknown_domains() {
78 let cases = vec![
79 "https://www.unknown.com",
80 "http://subdomain.randomstuff.org",
81 "https://notindatabase.co.uk",
82 "https://another.unknown.site",
83 ];
84
85 for url in cases {
86 assert_eq!(
87 NetworkInterceptManager::new(&create_url(url)),
88 NetworkInterceptManager::UNKNOWN
89 );
90 }
91 }
92
93 #[test]
94 fn test_invalid_urls() {
95 let cases = vec!["not-a-url", "ftp://invalid.protocol.com", "http://", ""];
96
97 for url in cases {
98 assert_eq!(
99 NetworkInterceptManager::new(&create_url(url)),
100 NetworkInterceptManager::UNKNOWN
101 );
102 }
103 }
104}