File size: 2,842 Bytes
9552aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Hostname matching function from SVR.JS rewritten from JavaScript to Rust
pub fn match_hostname(hostname: Option<&str>, req_hostname: Option<&str>) -> bool {
  if hostname.is_none() || hostname == Some("*") {
    return true;
  }

  if let (Some(hostname), Some(req_hostname)) = (hostname, req_hostname) {
    if hostname.starts_with("*.") && hostname != "*." {
      let hostnames_root = &hostname[2..];
      if req_hostname == hostnames_root
        || (req_hostname.len() > hostnames_root.len()
          && req_hostname.ends_with(&format!(".{}", hostnames_root)[..]))
      {
        return true;
      }
    } else if req_hostname == hostname {
      return true;
    }
  }

  false
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn should_return_true_if_hostname_is_undefined() {
    assert!(match_hostname(None, Some("example.com")));
  }

  #[test]
  fn should_return_true_if_hostname_is_star() {
    assert!(match_hostname(Some("*"), Some("example.com")));
  }

  #[test]
  fn should_return_true_if_req_hostname_matches_hostname_exactly() {
    assert!(match_hostname(Some("example.com"), Some("example.com")));
  }

  #[test]
  fn should_return_false_if_req_hostname_does_not_match_hostname_exactly() {
    assert!(!match_hostname(Some("example.com"), Some("example.org")));
  }

  #[test]
  fn should_return_true_if_hostname_starts_with_star_dot_and_req_hostname_matches_the_root() {
    assert!(match_hostname(
      Some("*.example.com"),
      Some("sub.example.com")
    ));
  }

  #[test]
  fn should_return_false_if_hostname_starts_with_star_dot_and_req_hostname_does_not_match_the_root()
  {
    assert!(!match_hostname(Some("*.example.com"), Some("example.org")));
  }

  #[test]
  fn should_return_true_if_hostname_starts_with_star_dot_and_req_hostname_is_the_root() {
    assert!(match_hostname(Some("*.example.com"), Some("example.com")));
  }

  #[test]
  fn should_return_false_if_hostname_is_star_dot() {
    assert!(!match_hostname(Some("*."), Some("example.com")));
  }

  #[test]
  fn should_return_false_if_req_hostname_is_undefined() {
    assert!(!match_hostname(Some("example.com"), None));
  }

  #[test]
  fn should_return_false_if_hostname_does_not_start_with_star_dot_and_req_hostname_does_not_match()
  {
    assert!(!match_hostname(
      Some("sub.example.com"),
      Some("example.com")
    ));
  }

  #[test]
  fn should_return_true_if_hostname_starts_with_star_dot_and_req_hostname_matches_the_root_with_additional_subdomains(
  ) {
    assert!(match_hostname(
      Some("*.example.com"),
      Some("sub.sub.example.com")
    ));
  }

  #[test]
  fn should_return_false_if_hostname_starts_with_star_dot_and_req_hostname_does_not_match_the_root_with_additional_subdomains(
  ) {
    assert!(!match_hostname(
      Some("*.example.com"),
      Some("sub.sub.example.org")
    ));
  }
}