Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Support JSON resources format #8

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 7 additions & 6 deletions examples/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down