Skip to content

Commit

Permalink
build: fix domain mathcer algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
LEXUGE committed Jan 24, 2023
1 parent 714ced1 commit f6bca6b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dmatcher/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,23 @@ impl Domain {
pub fn matches(&self, domain: &Dname<Bytes>) -> bool {
let mut ptr = &self.root;
for lv in domain.iter().rev() {
// We have reached the end of our rule set, breaking
if ptr.next_lvs.is_empty() {
return true;
break;
}
// If not empty...
ptr = match ptr.next_lvs.get(&lv.to_owned()) {
Some(v) => v,
None => return false,
};
}
// The domain provided is a superset of our rules, this is considered as not mathed.
// e.g. domain: "apple.com", rule: "apps.apple.com"
false

// If we exhausted our rule set, then this is a match
// e.g. apps.apple.com is a match for apple.com
ptr.next_lvs.is_empty()
// Otherwise:
// If there are still rules left but we have reached the end of our test case, then it is not a match.
// e.g. apple.com is not a match for apps.apple.com
}
}

Expand Down Expand Up @@ -125,6 +130,7 @@ mod tests {
matcher.insert(&dname!("temai.m.taobao.com"));
matcher.insert(&dname!("tui.taobao.com"));
assert_eq!(matcher.matches(&dname!("a.tui.taobao.com")), true);
assert_eq!(matcher.matches(&dname!("tejia.taobao.com")), true);
assert_eq!(matcher.matches(&dname!("m.taobao.com")), false);
assert_eq!(matcher.matches(&dname!("taobao.com")), false);
}
Expand Down

0 comments on commit f6bca6b

Please sign in to comment.