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

Support JSON resources format #8

Merged
merged 1 commit into from Oct 28, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

support new resources format

  • Loading branch information
antonok-edm committed Sep 30, 2019
commit ad40b55022f826c1705dd2304ee3af7f20573f16

Some generated files are not rendered by default. Learn more.

@@ -5,7 +5,8 @@ authors = ["Brian R. Bondy <netzen@gmail.com>"]
edition = "2018"

[dependencies]
adblock="~0.1.32"
adblock="~0.1.35"
serde_json = "1.0"
libc = "0.2"

[lib]
@@ -193,17 +193,18 @@ void TestTags() {

void TestRedirects() {
Engine engine("-advertisement-$redirect=1x1-transparent.gif\n");
engine.addResources("# test\n"
"1x1-transparent.gif image/gif;base64\n"
"R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\n");
engine.addResources("[{\"name\": \"1x1-transparent.gif\","
"\"aliases\": [],"
"\"kind\": {\"mime\": \"image/gif\"},"
"\"content\":\"R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\"}]");
Check(true, false, false, "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==", "Testing redirects match", engine,
"http://example.com/-advertisement-icon.", "example.com", "example.com",
false, "image");
}

void TestRedirect() {
Engine engine("-advertisement-$redirect=test\n");
engine.addResource("test", "application/javascript", "alert(1)");
engine.addResource("test", "application/javascript", "YWxlcnQoMSk=");
Check(true, false, false, "data:application/javascript;base64,YWxlcnQoMSk=", "Testing single redirect match", engine,
"http://example.com/-advertisement-icon.", "example.com", "example.com",
false, "image");
@@ -232,7 +233,7 @@ void TestThirdParty() {

void TestDefaultLists() {
std::vector<FilterList>& default_lists = FilterList::GetDefaultLists();
assert(default_lists.size() == 7);
assert(default_lists.size() == 8);
FilterList& l = default_lists[0];
assert(l.uuid == "67F880F5-7602-4042-8A3D-01481FD7437A");
assert(l.url == "https://easylist.to/easylist/easylist.txt");
@@ -245,7 +246,7 @@ void TestDefaultLists() {
num_passed++;

// Includes Brave Disconnect list
FilterList& l2 = default_lists[6];
FilterList& l2 = default_lists[7];
assert(l2.uuid == "9FA0665A-8FC0-4590-A80A-3FF6117A1258");
assert(l2.url == "https://raw.githubusercontent.com"
"/brave/adblock-lists/master/brave-disconnect.txt");
@@ -36,7 +36,7 @@ void engine_add_resource(C_Engine *engine,
const char *data);

/**
* Adds a list of resources in uBlock resources format
* Adds a list of `Resource`s from JSON format
*/
void engine_add_resources(C_Engine *engine, const char *resources);

@@ -2,6 +2,7 @@ extern crate adblock;

use adblock::engine::Engine;
use adblock::filter_lists;
use adblock::resources::{Resource, ResourceType, MimeType};
use core::ptr;
use libc::size_t;
use std::ffi::CStr;
@@ -98,18 +99,28 @@ pub unsafe extern "C" fn engine_add_resource(
let key = CStr::from_ptr(key).to_str().unwrap();
let content_type = CStr::from_ptr(content_type).to_str().unwrap();
let data = CStr::from_ptr(data).to_str().unwrap();
let resource = Resource {
name: key.to_string(),
aliases: vec![],
kind: ResourceType::Mime(MimeType::from(content_type)),
content: data.to_string(),
};
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.resource_add(key, content_type, data);
engine.resource_add(resource);
}

/// Adds a list of resources in uBlock resources format
/// Adds a list of `Resource`s from JSON format
#[no_mangle]
pub unsafe extern "C" fn engine_add_resources(engine: *mut Engine, resources: *const c_char) {
let resources = CStr::from_ptr(resources).to_str().unwrap();
let resources: Vec<Resource> = serde_json::from_str(resources).unwrap_or_else(|e| {
eprintln!("Failed to parse JSON adblock resources: {}", e);
vec![]
});
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.with_resources(resources);
engine.with_resources(&resources);
}

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