Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimisations and `document` request and rule handling fixes #35

Merged
merged 10 commits into from Jun 28, 2019

sorts out clippy warnings

  • Loading branch information
AndriusA committed Jun 21, 2019
commit 5326339096cfd0b8f0a52c51b63e7ebe212552e9
@@ -157,20 +157,19 @@ impl Blocker {
});

#[cfg(feature = "metrics")]
println!("");
println!();

// only match redirects if we have them set up
let redirect: Option<String> = filter.as_ref().and_then(|f| {
// Filter redirect option is set
if let Some(redirect) = f.redirect.as_ref() {
// And we have a matching redirect resource
if let Some(resource) = self.resources.get_resource(redirect) {
let mut data_url: String;
if resource.content_type.contains(';') {
data_url = format!("data:{},{}", resource.content_type, resource.data);
let data_url = if resource.content_type.contains(';') {
format!("data:{},{}", resource.content_type, resource.data)
} else {
data_url = format!("data:{};base64,{}", resource.content_type, base64::encode(&resource.data));
}
format!("data:{};base64,{}", resource.content_type, base64::encode(&resource.data))
};
Some(data_url.trim().to_owned())
} else {
// TOOD: handle error - throw?
@@ -297,42 +296,40 @@ impl Blocker {
}
}

pub fn filter_add<'a>(&'a mut self, filter: NetworkFilter) -> Result<&'a mut Blocker, BlockerError> {
pub fn filter_add(&mut self, filter: NetworkFilter) -> Result<&mut Blocker, BlockerError> {
if filter.is_badfilter() {
return Err(BlockerError::BadFilterAddUnsupported)
Err(BlockerError::BadFilterAddUnsupported)
} else if self.filter_exists(&filter) == Ok(true) {
Err(BlockerError::FilterExists)
} else if filter.is_csp() {
self.csp.filter_add(filter);
Ok(self)
} else if filter.is_exception() {
self.exceptions.filter_add(filter);
Ok(self)
} else if filter.is_important() {
self.importants.filter_add(filter);
Ok(self)
} else if filter.is_redirect() {
self.redirects.filter_add(filter);
Ok(self)
} else if filter.tag.is_some() {
self.tagged_filters_all.push(filter);
let tags_enabled = HashSet::from_iter(self.tags_enabled().into_iter());
Ok(self.tags_with_set(tags_enabled))
} else {
if filter.is_csp() {
self.csp.filter_add(filter);
Ok(self)
} else if filter.is_exception() {
self.exceptions.filter_add(filter);
Ok(self)
} else if filter.is_important() {
self.importants.filter_add(filter);
Ok(self)
} else if filter.is_redirect() {
self.redirects.filter_add(filter);
Ok(self)
} else if filter.tag.is_some() {
self.tagged_filters_all.push(filter);
let tags_enabled = HashSet::from_iter(self.tags_enabled().into_iter());
Ok(self.tags_with_set(tags_enabled))
} else {
self.filters.filter_add(filter);
Ok(self)
}
self.filters.filter_add(filter);
Ok(self)
}
}

pub fn with_tags<'a>(&'a mut self, tags: &[&str]) -> &'a mut Blocker {
let tag_set: HashSet<String> = HashSet::from_iter(tags.into_iter().map(|&t| String::from(t)));
let tag_set: HashSet<String> = HashSet::from_iter(tags.iter().map(|&t| String::from(t)));
self.tags_with_set(tag_set)
}

pub fn tags_enable<'a>(&'a mut self, tags: &[&str]) -> &'a mut Blocker {
let tag_set: HashSet<String> = HashSet::from_iter(tags.into_iter().map(|&t| String::from(t)))
let tag_set: HashSet<String> = HashSet::from_iter(tags.iter().map(|&t| String::from(t)))
.union(&self.tags_enabled)
.cloned()
.collect();
@@ -341,7 +338,7 @@ impl Blocker {

pub fn tags_disable<'a>(&'a mut self, tags: &[&str]) -> &'a mut Blocker {
let tag_set: HashSet<String> = self.tags_enabled
.difference(&HashSet::from_iter(tags.into_iter().map(|&t| String::from(t))))
.difference(&HashSet::from_iter(tags.iter().map(|&t| String::from(t))))
.cloned()
.collect();
self.tags_with_set(tag_set)
@@ -351,7 +348,7 @@ impl Blocker {
self.tags_enabled = tags_enabled;
let filters: Vec<NetworkFilter> = self.tagged_filters_all.iter()
.filter(|n| n.tag.is_some() && self.tags_enabled.contains(n.tag.as_ref().unwrap()))
.map(|n| n.clone())
.cloned()
.collect();
self.filters_tagged = NetworkFilterList::new(filters, self.enable_optimizations);
self
@@ -361,12 +358,12 @@ impl Blocker {
self.tags_enabled.iter().cloned().collect()
}

pub fn with_resources<'a>(&'a mut self, resources: Resources) -> &'a mut Blocker {
pub fn with_resources(&mut self, resources: Resources) -> &mut Blocker {
self.resources = resources;
self
}

pub fn resource_add<'a>(&'a mut self, key: String, resource: Resource) -> &'a mut Blocker {
pub fn resource_add(&mut self, key: String, resource: Resource) -> &mut Blocker {
self.resources.add_resource(key, resource);
self
}
@@ -460,7 +457,7 @@ impl NetworkFilterList {
}
}

pub fn filter_add<'a>(&'a mut self, filter: NetworkFilter) -> &'a mut NetworkFilterList {
pub fn filter_add(&mut self, filter: NetworkFilter) -> &mut NetworkFilterList {
let filter_tokens = filter.get_tokens();
let total_rules = vec_hashmap_len(&self.filter_map);
let filter_pointer = Arc::new(filter);
@@ -511,7 +508,7 @@ impl NetworkFilterList {
Ok(false)
}

pub fn check(&self, request: &Request, request_tokens: &Vec<Hash>) -> Option<&NetworkFilter> {
pub fn check(&self, request: &Request, request_tokens: &[Hash]) -> Option<&NetworkFilter> {
#[cfg(feature = "metrics")]
let mut filters_checked = 0;
#[cfg(feature = "metrics")]
@@ -89,7 +89,7 @@ impl Engine {
pub fn filter_exists(&self, filter: &str) -> bool {
let filter_parsed = NetworkFilter::parse(filter, true);
match filter_parsed
.map_err(|e| BlockerError::from(e))
.map_err(BlockerError::from)
.and_then(|f| self.blocker.filter_exists(&f)) {
Ok(exists) => exists,
Err(e) => {
@@ -107,7 +107,7 @@ impl Engine {
pub fn filter_add<'a>(&'a mut self, filter: &str) -> &'a mut Engine {
let filter_parsed = NetworkFilter::parse(filter, true);
match filter_parsed
.map_err(|e| BlockerError::from(e))
.map_err(BlockerError::from)
.and_then(|f| self.blocker.filter_add(f)) {
Ok(_b) => self,
Err(e) => {
@@ -128,11 +128,11 @@ impl Engine {
self
}

pub fn tags_enable<'a>(&'a mut self, tags: &[&str]) -> () {
pub fn tags_enable<'a>(&'a mut self, tags: &[&str]) {
self.blocker.tags_enable(tags);
}

pub fn tags_disable<'a>(&'a mut self, tags: &[&str]) -> () {
pub fn tags_disable<'a>(&'a mut self, tags: &[&str]) {
self.blocker.tags_disable(tags);
}

@@ -203,6 +203,7 @@ pub struct NetworkFilter {
}

impl NetworkFilter {
#[allow(clippy::cognitive_complexity)]
pub fn parse(line: &str, debug: bool) -> Result<NetworkFilter, FilterError> {
// Represent options as a bitmask
let mut mask: NetworkFilterMask = NetworkFilterMask::THIRD_PARTY
@@ -426,7 +427,7 @@ impl NetworkFilter {
// somewhere.

// If the first separator is a wildcard, included in in hostname
if first_separator_start < line.len() && line[first_separator_start..first_separator_start+1].starts_with("*") {
if first_separator_start < line.len() && line[first_separator_start..=first_separator_start].starts_with('*') {
mask.set(NetworkFilterMask::IS_HOSTNAME_REGEX, true);
}

@@ -835,7 +836,7 @@ fn compute_filter_id(
opt_domains: Option<&Vec<Hash>>,
opt_not_domains: Option<&Vec<Hash>>,
) -> Hash {
let mut hash: Hash = (5408 * 33) ^ (mask.bits as Hash);
let mut hash: Hash = (5408 * 33) ^ Hash::from(mask.bits);

if let Some(s) = csp {
let chars = s.chars();
@@ -992,18 +993,18 @@ fn is_anchored_by_hostname(filter_hostname: &str, hostname: &str, wildcard_filte
// Examples (filter_hostname, hostname):
// * (foo, foo.com)
// * (sub.foo, sub.foo.com)
wildcard_filter_hostname || filter_hostname.ends_with(".") || hostname[filter_hostname_len..].starts_with(".")
wildcard_filter_hostname || filter_hostname.ends_with('.') || hostname[filter_hostname_len..].starts_with('.')
} else if match_index == hostname_len - filter_hostname_len {
// `filter_hostname` is a suffix of `hostname`.
//
// Examples (filter_hostname, hostname):
// * (foo.com, sub.foo.com)
// * (com, foo.com)
filter_hostname.starts_with(".") || hostname[match_index - 1..].starts_with(".")
filter_hostname.starts_with('.') || hostname[match_index - 1..].starts_with('.')
} else {
// `filter_hostname` is infix of `hostname` and needs match full labels
(wildcard_filter_hostname || filter_hostname.ends_with(".") || hostname[filter_hostname_len..].starts_with("."))
&& (filter_hostname.starts_with(".") || hostname[match_index - 1..].starts_with("."))
(wildcard_filter_hostname || filter_hostname.ends_with('.') || hostname[filter_hostname_len..].starts_with('.'))
&& (filter_hostname.starts_with('.') || hostname[match_index - 1..].starts_with('.'))
}
}
else {
@@ -1375,7 +1376,7 @@ fn check_options(filter: &NetworkFilter, request: &request::Request) -> bool {
// If the union of included domains is recorded
if let Some(included_domains_union) = filter.opt_domains_union {
// If there isn't any source hash that matches the union, there's no match at all
if source_hashes.iter().all(|h| !(h & included_domains_union == *h)) {
if source_hashes.iter().all(|h| h & included_domains_union != *h) {
return false
}
}
@@ -37,7 +37,7 @@ pub fn parse_filters(
debug: bool,
) -> (Vec<NetworkFilter>, Vec<String>) {

let list_iter = list.into_iter();
let list_iter = list.iter();

let (network_filters, cosmetic_filters): (Vec<_>, Vec<_>) = list_iter
.map(|line| {
@@ -148,6 +148,7 @@ impl<'a> Request {
)
}

#[allow(clippy::too_many_arguments)]
fn from_detailed_parameters(
raw_type: &str,
url: &str,
@@ -36,10 +36,10 @@ impl Resources {
if resource.is_empty() {
continue;
}
let first_new_line = resource.find("\n");
let first_new_line = resource.find('\n');
let first_new_line_pos;
// No new line, but appears to encode mime type and teh content is not base64, so can be empty
if first_new_line.is_none() && resource.contains(" ") && resource.contains("/") && !resource.contains(";base64") {
if first_new_line.is_none() && resource.contains(' ') && resource.contains('/') && !resource.contains(";base64") {
first_new_line_pos = resource.len();
} else if first_new_line.is_none() {
continue;
@@ -59,7 +59,7 @@ impl Resources {
let name = name.unwrap().to_owned();
let body = body.trim().to_owned();

let ttr = type_to_resource.entry(rtype).or_insert(HashMap::new());
let ttr = type_to_resource.entry(rtype).or_insert_with(HashMap::new);
ttr.insert(name, body);
}

@@ -85,7 +85,7 @@ impl Resources {
}

pub fn add_resource(&mut self, name: String, resource: Resource) {
&self.resources.insert(name, resource);
self.resources.insert(name, resource);
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.