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
Prev

adds APIs to insert and query individual redirect resources

  • Loading branch information
AndriusA committed Jun 18, 2019
commit e943a189fff6cda1667a3d879fc4851755da4a46
@@ -128,7 +128,7 @@ declare_types! {
let this = cx.this();
let result = {
let guard = cx.lock();
let mut engine = this.borrow(&guard);
let engine = this.borrow(&guard);
engine.tag_exists(&tag)
};
Ok(cx.boolean(result).upcast())
@@ -156,6 +156,33 @@ declare_types! {
}
Ok(JsNull::new().upcast())
}

method addResource(mut cx) {
let name: String = cx.argument::<JsString>(0)?.value();
let content_type: String = cx.argument::<JsString>(1)?.value();
let data: String = cx.argument::<JsString>(2)?.value();

let mut this = cx.this();
let guard = cx.lock();
{
let mut engine = this.borrow_mut(&guard);
engine.resource_add(&name, &content_type, &data);
}
Ok(JsNull::new().upcast())
}

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

let this = cx.this();
let result = {
let guard = cx.lock();
let engine = this.borrow(&guard);
engine.resource_get(&name)
};
let js_value = neon_serde::to_value(&mut cx, &result)?;
Ok(js_value)
}
}
}

@@ -8,7 +8,7 @@ use crate::filters::network::{NetworkFilter, NetworkMatchable, FilterError};
use crate::request::Request;
use crate::utils::{fast_hash, Hash};
use crate::optimizer;
use crate::resources::{Resources};
use crate::resources::{Resources, Resource};
use base64;

pub struct BlockerOptions {
@@ -311,11 +311,19 @@ impl Blocker {
self.tags_enabled.iter().cloned().collect()
}

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

pub fn resource_add<'a>(&'a mut self, key: String, resource: Resource) -> &'a mut Blocker {
self.resources.add_resource(key, resource);
self
}

pub fn resource_get(&self, key: &str) -> Option<&Resource> {
self.resources.get_resource(key)
}
}

#[derive(Serialize, Deserialize)]
@@ -3,6 +3,7 @@ use crate::blocker::{Blocker, BlockerError, BlockerOptions, BlockerResult};
use crate::lists::parse_filters;
use crate::request::Request;
use crate::filters::network::NetworkFilter;
use crate::resources::{Resources, Resource};
use rmps;
use flate2::write::GzEncoder;
use flate2::read::GzDecoder;
@@ -134,14 +135,28 @@ impl Engine {
pub fn tags_disable<'a>(&'a mut self, tags: &[&str]) -> () {
self.blocker.tags_disable(tags);
}

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

pub fn with_resources<'a>(&'a mut self, resources: &'a str) -> &'a mut Engine {
let resources = Resources::parse(resources);
self.blocker.with_resources(resources);
self
}

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

pub fn resource_add<'a>(&'a mut self, key: &str, content_type: &str, data: &str) -> &'a mut Engine {
let resource = Resource {
content_type: content_type.to_owned(),
data: data.to_owned()
};
self.blocker.resource_add(key.to_owned(), resource);
self
}

pub fn resource_get(&self, key: &str) -> Option<Resource> {
self.blocker.resource_get(key).cloned()
}
}

@@ -338,4 +353,37 @@ noopcss text/css
let serialized = engine.serialize().unwrap();
println!("Engine serialized: {:?}", serialized);
}

#[test]
fn redirect_resource_insertion_works() {
let mut engine = Engine::from_rules(&[
"ad-banner$redirect=nooptext".to_owned()
]);

engine.resource_add("nooptext", "text/plain", "");

let url = "http://example.com/ad-banner.gif";
let matched_rule = engine.check_network_urls(url, "", "");
assert!(matched_rule.matched, "Expected match for {}", url);
assert_eq!(matched_rule.redirect, Some("data:text/plain;base64,".to_owned()), "Expected redirect to contain resource");
}

#[test]
fn redirect_resource_lookup_works() {
let script = r#"
(function() {
;
})();
"#;

let mut engine = Engine::from_rules(&[]);

engine.resource_add("noopjs", "application/javascript", script);
let inserted_resource = engine.resource_get("noopjs");
assert!(inserted_resource.is_some());
let resource = inserted_resource.unwrap();
assert_eq!(resource.content_type, "application/javascript");
assert_eq!(&resource.data, script);
}
}
@@ -2,7 +2,7 @@ use std::collections::HashMap;
use regex::Regex;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Resource {
pub content_type: String,
pub data: String
@@ -83,6 +83,10 @@ impl Resources {
pub fn get_resource(&self, name: &str) -> Option<&Resource> {
self.resources.get(name)
}

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

#[cfg(test)]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.