Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
feat: Refine the host & pathing filter (#311)
Browse files Browse the repository at this point in the history
* feat: Refine the host & pathing filter
  • Loading branch information
ncloudioj authored Nov 5, 2021
1 parent 8157b2f commit 99ee20e
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 129 deletions.
19 changes: 14 additions & 5 deletions adm_settings_test.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
{"DEFAULT": {
"advertiser_hosts": ["example.com"],
"advertiser_urls": [{ "host": "example.com" }],
"impression_hosts": ["example.net"],
"click_hosts":["example.com"]
},
"Acme": {
"advertiser_hosts": ["www.acme.biz", "acme.biz"],
"advertiser_urls": [
{ "host": "www.acme.biz" },
{ "host": "acme.biz" }
],
"position": 0
},
"Dunder Mifflin": {
"advertiser_hosts": ["www.dunderm.biz", "dunderm.biz"],
"advertiser_urls": [
{ "host": "www.dunderm.biz" },
{ "host": "dunderm.biz" }
],
"position": 1
},
"Los Pollos Hermanos": {
"advertiser_hosts": ["www.lph-nm.biz", "lph-mx.co.mx"]
"advertiser_urls": [
{ "host": "www.lph-nm.biz" },
{ "host": "lph-mx.co.mx" }
]
}
}
}
34 changes: 17 additions & 17 deletions integration-tests/volumes/contile/adm_settings.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"Example COM": {
"advertiser_hosts": [
"www.example.com",
"www.example.co.uk",
"www.example.it",
"www.example.com.au",
"www.example.ca",
"www.example.de",
"www.example.fr",
"www.example.es",
"www.example.in",
"www.example.com.mx"
"advertiser_urls": [
{ "host": "www.example.com" },
{ "host": "www.example.co.uk" },
{ "host": "www.example.it" },
{ "host": "www.example.com.au" },
{ "host": "www.example.ca" },
{ "host": "www.example.de" },
{ "host": "www.example.fr" },
{ "host": "www.example.es" },
{ "host": "www.example.in" },
{ "host": "www.example.com.mx" }
],
"click_hosts": [],
"impression_hosts": [],
"include_regions": [],
"position": 1
},
"Example ORG": {
"advertiser_hosts": [
"www.example.org"
"advertiser_urls": [
{ "host": "www.example.org" }
],
"click_hosts": [
"example.org"
Expand All @@ -31,8 +31,8 @@
"position": 1
},
"DunBroch": {
"advertiser_hosts": [
"www.dunbroch.co.uk"
"advertiser_urls": [
{ "host": "www.dunbroch.co.uk" }
],
"click_hosts": [
"dunbroch.co.uk"
Expand All @@ -46,7 +46,7 @@
"position": 2
},
"DEFAULT": {
"advertiser_hosts": [],
"advertiser_urls": [],
"click_hosts": [
"example.com"
],
Expand All @@ -67,4 +67,4 @@
"CN"
]
}
}
}
108 changes: 88 additions & 20 deletions src/adm/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,35 @@ impl AdmFilter {
}

// do a quick string comparison between the supplied host and the provided filter.
let mut host = format!(
"{}{}",
parsed
.host()
.ok_or_else(|| HandlerErrorKind::MissingHost(species, url.to_string()))?,
parsed.path()
);
if !host.ends_with('/') {
host.push('/');
let host = parsed
.host()
.ok_or_else(|| HandlerErrorKind::MissingHost(species, url.to_string()))?
.to_string();
let mut path = Cow::from(parsed.path());
if !path.ends_with('/') {
path.to_mut().push('/');
}
for filter in &filter.advertiser_hosts {
let mut filter = Cow::from(filter);
if !filter.ends_with('/') {
filter.to_mut().push('/');
};
if host.starts_with(filter.as_ref()) {
return Ok(());

for filter in &filter.advertiser_urls {
if !host.eq(&filter.host) {
continue;
}

if let Some(ref paths) = filter.paths {
for rule in paths {
match rule.matching.as_str() {
// Note that the orignal path is used for exact matching
"exact" if rule.value == parsed.path() => return Ok(()),
"prefix" if path.starts_with(&rule.value) => return Ok(()),
_ => continue,
}
}
} else {
// Host matches without any path filters, matching succeeds.
return Ok(());
};
}

Err(HandlerErrorKind::InvalidHost(species, url.to_string()).into())
}

Expand Down Expand Up @@ -294,7 +304,7 @@ impl AdmFilter {
return None;
}

let adv_filter = if filter.advertiser_hosts.is_empty() {
let adv_filter = if filter.advertiser_urls.is_empty() {
default
} else {
filter
Expand Down Expand Up @@ -433,7 +443,27 @@ mod tests {
#[test]
fn check_advertiser() {
let s = r#"{
"advertiser_hosts": ["acme.biz/ca", "black_friday.acme.biz/ca"],
"advertiser_urls": [
{
"host": "acme.biz",
"paths": [
{ "value": "/ca/", "matching": "prefix" }
]
},
{
"host": "black_friday.acme.biz",
"paths": [
{ "value": "/ca/", "matching": "prefix" }
]
},
{
"host": "acme.biz",
"paths": [
{ "value": "/usa", "matching": "exact" }
]
}
],
"position": 0
}"#;
let settings: AdmAdvertiserFilterSettings = serde_json::from_str(s).unwrap();
Expand Down Expand Up @@ -477,15 +507,47 @@ mod tests {
.check_advertiser(&settings, &mut tile, &mut tags)
.is_err());

//good, extra element in host
//Good, extra element in host
tile.advertiser_url = "https://black_friday.acme.biz/ca/".to_owned();
assert!(filter
.check_advertiser(&settings, &mut tile, &mut tags)
.is_ok());

//Good, extra matching
tile.advertiser_url = "https://acme.biz/usa".to_owned();
assert!(filter
.check_advertiser(&settings, &mut tile, &mut tags)
.is_ok());

// Bad, path doesn't match exactly
tile.advertiser_url = "https://acme.biz/usa/".to_owned();
assert!(filter
.check_advertiser(&settings, &mut tile, &mut tags)
.is_err());

// "Traditional host. "
let s = r#"{
"advertiser_hosts": ["acme.biz/ca", "www.acme.co/foo.bar"],
"advertiser_urls": [
{
"host": "acme.biz",
"paths": [
{ "value": "/ca/", "matching": "prefix" }
]
},
{
"host": "www.acme.co",
"paths": [
{ "value": "/foo.bar/", "matching": "prefix" }
]
},
{
"host": "acme.biz",
"paths": [
{ "value": "/", "matching": "exact" }
]
}
],
"position": 0
}"#;
let settings: AdmAdvertiserFilterSettings = serde_json::from_str(s).unwrap();
Expand Down Expand Up @@ -525,5 +587,11 @@ mod tests {
assert!(filter
.check_advertiser(&settings, &mut tile, &mut tags)
.is_err());

//Good, matches exact host and path
tile.advertiser_url = "https://acme.biz/".to_owned();
assert!(filter
.check_advertiser(&settings, &mut tile, &mut tags)
.is_ok());
}
}
Loading

0 comments on commit 99ee20e

Please sign in to comment.