this_env/utils/
domain_utils.rs

1//this.env/crate/src/middleware/domain_utils.rs
2/// Extracts the root domain from a full host (e.g., "admin.example.com" → "example.com").
3pub fn extract_root_domain(host: &str) -> Option<String> {
4    let parts: Vec<&str> = host.split('.').collect();
5    if parts.len() >= 2 {
6        Some(format!("{}.{}", parts[parts.len() - 2], parts[parts.len() - 1]))
7    } else {
8        None
9    }
10}
11
12/// Determines the parent domain if this is a subdomain.
13pub fn determine_parent(host: &str) -> Option<String> {
14    if let Some(root) = extract_root_domain(host) {
15        if host != root {
16            Some(root)
17        } else {
18            None
19        }
20    } else {
21        None
22    }
23}
24
25/// Returns (root_domain, subdomain) where subdomain is None if host == root.
26pub fn split_host(host: &str) -> (String, Option<String>) {
27    let root = extract_root_domain(host).unwrap_or_else(|| host.to_string());
28    if host == root {
29        (root, None)
30    } else {
31        let sub = host.trim_end_matches(&format!(".{}", &root)).to_string();
32        (root, Some(sub))
33    }
34}
35
36/// Quick check whether this request originates from localhost.
37pub fn is_localhost(host: &str) -> bool {
38    host.starts_with("localhost") || host.starts_with("127.0.0.1")
39}