Skip to content

Commit

Permalink
Merge pull request #184 from brave/feature-redirect-url
Browse files Browse the repository at this point in the history
`redirect-url` filter option
  • Loading branch information
antonok-edm authored Sep 17, 2021
2 parents c8fb7c5 + 900a528 commit b7f29af
Show file tree
Hide file tree
Showing 19 changed files with 648 additions and 333 deletions.
27 changes: 13 additions & 14 deletions benches/bench_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use criterion::*;
use serde::{Deserialize, Serialize};

use adblock::utils::rules_from_lists;
use adblock::lists::FilterFormat;
use adblock::blocker::{Blocker, BlockerOptions};
use adblock::request::Request;
use adblock::url_parser::parse_url;
Expand All @@ -24,7 +23,7 @@ fn load_requests() -> Vec<TestRequest> {
}

fn get_blocker(rules: &Vec<String>) -> Blocker {
let (network_filters, _) = adblock::lists::parse_filters(rules, false, FilterFormat::Standard);
let (network_filters, _) = adblock::lists::parse_filters(rules, false, Default::default());

let blocker_options = BlockerOptions {
enable_optimizations: true,
Expand Down Expand Up @@ -103,7 +102,7 @@ fn rule_match(c: &mut Criterion) {
"data/easylist.to/easylist/easylist.txt".to_owned(),
"data/easylist.to/easylist/easyprivacy.txt".to_owned()
]);
let engine = Engine::from_rules(&rules, FilterFormat::Standard);
let engine = Engine::from_rules(&rules, Default::default());
b.iter(|| bench_rule_matching(&engine, &elep_req))
},
);
Expand All @@ -113,7 +112,7 @@ fn rule_match(c: &mut Criterion) {
let rules = rules_from_lists(&vec![
"data/easylist.to/easylist/easylist.txt".to_owned(),
]);
let engine = Engine::from_rules(&rules, FilterFormat::Standard);
let engine = Engine::from_rules(&rules, Default::default());
b.iter(|| bench_rule_matching(&engine, &el_req))
},
);
Expand All @@ -123,7 +122,7 @@ fn rule_match(c: &mut Criterion) {
let rules = rules_from_lists(&vec![
"data/slim-list.txt".to_owned()
]);
let engine = Engine::from_rules(&rules, FilterFormat::Standard);
let engine = Engine::from_rules(&rules, Default::default());
b.iter(|| bench_rule_matching(&engine, &slim_req))
},
);
Expand Down Expand Up @@ -208,7 +207,7 @@ fn serialization(c: &mut Criterion) {
String::from("data/easylist.to/easylist/easyprivacy.txt")
]);

let engine = Engine::from_rules(&full_rules, FilterFormat::Standard);
let engine = Engine::from_rules(&full_rules, Default::default());
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
},
);
Expand All @@ -219,7 +218,7 @@ fn serialization(c: &mut Criterion) {
String::from("data/easylist.to/easylist/easylist.txt"),
]);

let engine = Engine::from_rules(&full_rules, FilterFormat::Standard);
let engine = Engine::from_rules(&full_rules, Default::default());
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
}
);
Expand All @@ -230,7 +229,7 @@ fn serialization(c: &mut Criterion) {
String::from("data/slim-list.txt"),
]);

let engine = Engine::from_rules(&full_rules, FilterFormat::Standard);
let engine = Engine::from_rules(&full_rules, Default::default());
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
}
);
Expand All @@ -251,7 +250,7 @@ fn deserialization(c: &mut Criterion) {
String::from("data/easylist.to/easylist/easyprivacy.txt")
]);

let engine = Engine::from_rules(&full_rules, FilterFormat::Standard);
let engine = Engine::from_rules(&full_rules, Default::default());
let serialized = engine.serialize_raw().unwrap();

b.iter(|| {
Expand All @@ -267,7 +266,7 @@ fn deserialization(c: &mut Criterion) {
String::from("data/easylist.to/easylist/easylist.txt"),
]);

let engine = Engine::from_rules(&full_rules, FilterFormat::Standard);
let engine = Engine::from_rules(&full_rules, Default::default());
let serialized = engine.serialize_raw().unwrap();

b.iter(|| {
Expand All @@ -283,7 +282,7 @@ fn deserialization(c: &mut Criterion) {
String::from("data/slim-list.txt"),
]);

let engine = Engine::from_rules(&full_rules, FilterFormat::Standard);
let engine = Engine::from_rules(&full_rules, Default::default());
let serialized = engine.serialize_raw().unwrap();

b.iter(|| {
Expand Down Expand Up @@ -352,7 +351,7 @@ fn rule_match_browserlike_comparable(c: &mut Criterion) {
"data/easylist.to/easylist/easylist.txt".to_owned(),
"data/easylist.to/easylist/easyprivacy.txt".to_owned()
]);
let engine = Engine::from_rules_parametrised(&rules, FilterFormat::Standard, false, true);
let engine = Engine::from_rules_parametrised(&rules, Default::default(), false, true);
b.iter(|| bench_rule_matching_browserlike(&engine, &elep_req))
},
);
Expand All @@ -362,7 +361,7 @@ fn rule_match_browserlike_comparable(c: &mut Criterion) {
let rules = rules_from_lists(&vec![
"data/easylist.to/easylist/easylist.txt".to_owned(),
]);
let engine = Engine::from_rules_parametrised(&rules, FilterFormat::Standard, false, true);
let engine = Engine::from_rules_parametrised(&rules, Default::default(), false, true);
b.iter(|| bench_rule_matching_browserlike(&engine, &el_req))
},
);
Expand All @@ -372,7 +371,7 @@ fn rule_match_browserlike_comparable(c: &mut Criterion) {
let rules = rules_from_lists(&vec![
"data/slim-list.txt".to_owned()
]);
let engine = Engine::from_rules_parametrised(&rules, FilterFormat::Standard, false, true);
let engine = Engine::from_rules_parametrised(&rules, Default::default(), false, true);
b.iter(|| bench_rule_matching_browserlike(&engine, &slim))
},
);
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_redirect_performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn get_redirect_rules() -> Vec<NetworkFilter> {
let async_runtime = Runtime::new().expect("Could not start Tokio runtime");

let filters = async_runtime.block_on(get_all_filters());
let (network_filters, _) = adblock::lists::parse_filters(&filters, true, adblock::lists::FilterFormat::Standard);
let (network_filters, _) = adblock::lists::parse_filters(&filters, true, Default::default());

network_filters.into_iter()
.filter(|rule| {
Expand Down
5 changes: 2 additions & 3 deletions benches/bench_rules.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use criterion::*;
use once_cell::sync::Lazy;

use adblock::lists::FilterFormat;
use adblock::utils::{read_file_lines, rules_from_lists};
use adblock::blocker::{Blocker, BlockerOptions};

Expand Down Expand Up @@ -58,7 +57,7 @@ fn bench_parsing_impl(lists: &Vec<Vec<String>>) -> usize {
let mut dummy = 0;

for list in lists {
let (network_filters, _) = adblock::lists::parse_filters(list, false, FilterFormat::Standard);
let (network_filters, _) = adblock::lists::parse_filters(list, false, Default::default());
dummy = dummy + network_filters.len() % 1000000;
}

Expand All @@ -85,7 +84,7 @@ fn list_parse(c: &mut Criterion) {
}

fn get_blocker(rules: &Vec<String>) -> Blocker {
let (network_filters, _) = adblock::lists::parse_filters(rules, false, FilterFormat::Standard);
let (network_filters, _) = adblock::lists::parse_filters(rules, false, Default::default());

println!("Got {} network filters", network_filters.len());

Expand Down
9 changes: 9 additions & 0 deletions data/matching-test-requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -6183,6 +6183,15 @@
"type": "image",
"url": "https://ade.googlesyndication.com/ddm/"
},
{
"check": true,
"filters": [
"||cdn.taboola.com/libtrc/*/loader.js$script,redirect-url=http://noops.js,important,domain=cnet.com"
],
"sourceUrl": "https://www.cnet.com",
"type": "script",
"url": "https://cdn.taboola.com/libtrc/cbsinteractive-cnet/loader.js"
},
{
"check": true,
"filters": [
Expand Down
3 changes: 1 addition & 2 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use adblock::engine::Engine;
use adblock::lists::FilterFormat;

fn main() {
let rules = vec![
Expand All @@ -9,7 +8,7 @@ fn main() {
String::from("-advertisement/script."),
];

let blocker = Engine::from_rules_debug(&rules, FilterFormat::Standard);
let blocker = Engine::from_rules_debug(&rules, Default::default());
let blocker_result = blocker.check_network_urls("http://example.com/-advertisement-icon.", "http://example.com/helloworld", "image");

println!("Blocker result: {:?}", blocker_result);
Expand Down
3 changes: 1 addition & 2 deletions examples/generate-dat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use adblock::engine::Engine;
use adblock::lists::FilterFormat;

use std::fs::File;
use std::io::prelude::*;
Expand All @@ -12,7 +11,7 @@ fn main() {
];

// Serialize
let mut engine = Engine::from_rules_debug(&rules, FilterFormat::Standard);
let mut engine = Engine::from_rules_debug(&rules, Default::default());
engine.use_tags(&["twitter-embeds"]);
assert!(engine.check_network_urls("https://platform.twitter.com/widgets.js", "https://fmarier.github.io/brave-testing/social-widgets.html", "script").exception.is_some());
let serialized = engine.serialize_raw().expect("Could not serialize!");
Expand Down
30 changes: 15 additions & 15 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::cell::RefCell;
use std::sync::Mutex;
use std::path::Path;
use adblock::engine::Engine as EngineInternal;
use adblock::lists::{RuleTypes, FilterFormat, FilterSet as FilterSetInternal};
use adblock::lists::{RuleTypes, FilterFormat, FilterSet as FilterSetInternal, ParseOptions};
use adblock::resources::Resource;
use adblock::resources::resource_assembler::{assemble_web_accessible_resources, assemble_scriptlet_resources};

Expand All @@ -19,11 +19,11 @@ impl FilterSet {
fn new(debug: bool) -> Self {
Self(RefCell::new(FilterSetInternal::new(debug)))
}
fn add_filters(&self, rules: &[String], format: FilterFormat) {
self.0.borrow_mut().add_filters(rules, format)
fn add_filters(&self, rules: &[String], opts: ParseOptions) {
self.0.borrow_mut().add_filters(rules, opts)
}
fn add_filter(&self, filter: &str, format: FilterFormat) -> Result<(), adblock::lists::FilterParseError> {
self.0.borrow_mut().add_filter(filter, format)
fn add_filter(&self, filter: &str, opts: ParseOptions) -> Result<(), adblock::lists::FilterParseError> {
self.0.borrow_mut().add_filter(filter, opts)
}
fn into_content_blocking(&self, rule_types: RuleTypes) -> Result<(Vec<adblock::content_blocking::CbRule>, Vec<String>), ()> {
self.0.borrow().clone().into_content_blocking(rule_types)
Expand All @@ -47,14 +47,14 @@ fn filter_set_add_filters(mut cx: FunctionContext) -> JsResult<JsNull> {

// Take the first argument, which must be an array
let rules_handle: Handle<JsArray> = cx.argument(1)?;
// Second argument is the optional format of the rules, defaulting to
// FilterFormat::Standard
let format = match cx.argument_opt(2) {
Some(format_arg) => match neon_serde::from_value(&mut cx, format_arg) {
// Second argument is optional parse options. All fields are optional. ParseOptions::default()
// if unspecified.
let parse_opts = match cx.argument_opt(2) {
Some(parse_opts_arg) => match neon_serde::from_value(&mut cx, parse_opts_arg) {
Ok(v) => v,
Err(e) => cx.throw_error(e.to_string())?,
},
None => FilterFormat::Standard,
None => ParseOptions::default(),
};

// Convert a JsArray to a Rust Vec
Expand All @@ -67,7 +67,7 @@ fn filter_set_add_filters(mut cx: FunctionContext) -> JsResult<JsNull> {
rules.push(rule);
}

this.add_filters(&rules, format);
this.add_filters(&rules, parse_opts);

Ok(JsNull::new(&mut cx))
}
Expand All @@ -76,15 +76,15 @@ fn filter_set_add_filter(mut cx: FunctionContext) -> JsResult<JsBoolean> {
let this = cx.argument::<JsBox<FilterSet>>(0)?;

let filter: String = cx.argument::<JsString>(1)?.value(&mut cx);
let format = match cx.argument_opt(2) {
Some(format_arg) => match neon_serde::from_value(&mut cx, format_arg) {
let parse_opts = match cx.argument_opt(2) {
Some(parse_opts_arg) => match neon_serde::from_value(&mut cx, parse_opts_arg) {
Ok(v) => v,
Err(e) => cx.throw_error(e.to_string())?,
},
None => FilterFormat::Standard,
None => ParseOptions::default(),
};

let ok = this.add_filter(&filter, format).is_ok();
let ok = this.add_filter(&filter, parse_opts).is_ok();
// Return true/false depending on whether or not the filter could be added
Ok(JsBoolean::new(&mut cx, ok))
}
Expand Down
Loading

0 comments on commit b7f29af

Please sign in to comment.