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

Implements uBO style polyfills for requests redirects #29

Merged
merged 9 commits into from Jun 18, 2019

Merge branch 'master' into feature-redirect-injection

Make sure deserialization is backwards-comaptible by not deserializing redirect polyfill resources
  • Loading branch information
AndriusA committed Jun 13, 2019
commit bb14efe995864c174224a210496c0aca66944565
@@ -120,6 +120,41 @@ declare_types! {
engine.with_resources(&resources);
}
Ok(JsNull::new().upcast())

}
method tagExists(mut cx) {
let tag: String = cx.argument::<JsString>(0)?.value();

let this = cx.this();
let result = {
let guard = cx.lock();
let mut engine = this.borrow(&guard);
engine.tag_exists(&tag)
};
Ok(cx.boolean(result).upcast())
}

method clearTags(mut cx) {
let mut this = cx.this();
let guard = cx.lock();
{
let mut engine = this.borrow_mut(&guard);
// enabling an empty list of tags disables all tags
engine.tags_enable(&[]);
}
Ok(JsNull::new().upcast())
}

method addFilter(mut cx) {
let filter: String = cx.argument::<JsString>(0)?.value();

let mut this = cx.this();
let guard = cx.lock();
{
let mut engine = this.borrow_mut(&guard);
engine.filter_add(&filter);
}
Ok(JsNull::new().upcast())
}
}
}
@@ -64,7 +64,8 @@ pub struct Blocker {
load_cosmetic_filters: bool,
load_network_filters: bool,

resources: Option<Resources>
#[serde(skip_deserializing)]
resources: Resources
}

impl Blocker {
@@ -113,27 +114,22 @@ impl Blocker {

// only match redirects if we have them set up
let redirect: Option<String> = filter.as_ref().and_then(|f| {
// If there is a match
if let Some(blocker_redirects) = self.resources.as_ref() {
// Filter redirect option is set
if let Some(redirect) = f.redirect.as_ref() {
// And we have a matching redirect resource
if let Some(resource) = blocker_redirects.get_resource(redirect) {
let mut data_url: String;
if resource.content_type.contains(';') {
data_url = format!("data:{},{}", resource.content_type, resource.data);
} else {
data_url = format!("data:{};base64,{}", resource.content_type, base64::encode(&resource.data));
}
Some(data_url.trim().to_owned())
// 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);
} else {
// TOOD: handle error - throw?
if self.debug {
eprintln!("Matched rule with redirect option but did not find corresponding resource to send");
}
None
data_url = format!("data:{};base64,{}", resource.content_type, base64::encode(&resource.data));
}
Some(data_url.trim().to_owned())
} else {
// TOOD: handle error - throw?
if self.debug {
eprintln!("Matched rule with redirect option but did not find corresponding resource to send");
}
None
}
} else {
@@ -232,7 +228,7 @@ impl Blocker {
load_cosmetic_filters: options.load_cosmetic_filters,
load_network_filters: options.load_network_filters,

resources: None
resources: Resources::default()
}
}

@@ -318,7 +314,7 @@ impl Blocker {

pub fn with_resources<'a>(&'a mut self, resources: &'a str) -> &'a mut Blocker {
let resources = Resources::parse(resources);
self.resources = Some(resources);
self.resources = resources;
self
}
}
@@ -137,6 +137,10 @@ impl Engine {
self.blocker.with_resources(resources);
self
}

pub fn tag_exists(&self, tag: &str) -> bool {
self.blocker.tags_enabled().contains(&tag.to_owned())
}
}


@@ -13,6 +13,14 @@ pub struct Resources {
pub resources: HashMap<String, Resource>
}

impl Default for Resources {
fn default() -> Resources {
Resources {
resources: HashMap::new()
}
}
}

impl Resources {
pub fn parse(data: &str) -> Resources {
let chunks = data.split("\n\n");
You are viewing a condensed version of this merge commit. You can view the full changes here.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.