Skip to content

Commit

Permalink
Optimize the NO_PROXY domain matcher (#1728)
Browse files Browse the repository at this point in the history
This removes some panic bits for the index and slice access.
  • Loading branch information
nickelc authored Jan 16, 2023
1 parent 6f714f4 commit cda9c34
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,14 @@ impl DomainMatcher {
fn contains(&self, domain: &str) -> bool {
let domain_len = domain.len();
for d in self.0.iter() {
if d == domain || (d.starts_with('.') && &d[1..] == domain) {
if d == domain || d.strip_prefix('.') == Some(domain) {
return true;
} else if domain.ends_with(d) {
if d.starts_with('.') {
// If the first character of d is a dot, that means the first character of domain
// must also be a dot, so we are looking at a subdomain of d and that matches
return true;
} else if domain.as_bytes()[domain_len - d.len() - 1] == b'.' {
} else if domain.as_bytes().get(domain_len - d.len() - 1) == Some(&b'.') {
// Given that d is a prefix of domain, if the prior character in domain is a dot
// then that means we must be matching a subdomain of d, and that matches
return true;
Expand Down Expand Up @@ -1103,6 +1103,26 @@ mod tests {
}
}

#[test]
fn test_domain_matcher() {
let domains = vec![".foo.bar".into(), "bar.foo".into()];
let matcher = DomainMatcher(domains);

// domains match with leading `.`
assert!(matcher.contains("foo.bar"));
// subdomains match with leading `.`
assert!(matcher.contains("www.foo.bar"));

// domains match with no leading `.`
assert!(matcher.contains("bar.foo"));
// subdomains match with no leading `.`
assert!(matcher.contains("www.bar.foo"));

// non-subdomain string prefixes don't match
assert!(!matcher.contains("notfoo.bar"));
assert!(!matcher.contains("notbar.foo"));
}

// Smallest possible content for a mutex
struct MutexInner;

Expand Down

0 comments on commit cda9c34

Please sign in to comment.