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
+637
−44
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f1cae2f
Implements uBO style polyfills for requests matched on rules with `re…
AndriusA 9adddd6
Merge branch 'master' into feature-redirect-injection
AndriusA d6530eb
Adds Node wrappers for handling redirect resources
AndriusA b20dfe3
fixes benchmarks to work with renamed util functions
AndriusA 9fcff72
Merge branch 'master' into feature-redirect-injection
AndriusA bb14efe
Merge branch 'master' into feature-redirect-injection
AndriusA e5df684
adds test for deserialization with redirect resources
AndriusA 7d64942
messagepack based serialization/deserialization, gzip-compressed
AndriusA e943a18
adds APIs to insert and query individual redirect resources
AndriusA File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Implements uBO style polyfills for requests matched on rules with `re…
…direct` option Closes #27
- Loading branch information
Some generated files are not rendered by default. Learn more.
Oops, something went wrong.
| @@ -0,0 +1,195 @@ | ||
| use std::collections::HashMap; | ||
| use regex::Regex; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| pub struct Resource { | ||
| pub content_type: String, | ||
| pub data: String | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| pub struct Resources { | ||
| pub resources: HashMap<String, Resource> | ||
| } | ||
|
|
||
| impl Resources { | ||
| pub fn parse(data: &str) -> Resources { | ||
| let chunks = data.split("\n\n"); | ||
| let mut type_to_resource: HashMap<String, HashMap<String, String>> = HashMap::new(); | ||
|
|
||
| lazy_static! { | ||
| static ref COMMENTS_RE: Regex = Regex::new(r"(?m:^\s*#.*$)").unwrap(); | ||
| } | ||
|
|
||
| for chunk in chunks { | ||
| let resource: String = COMMENTS_RE.replace_all(&chunk, "").to_string(); | ||
| let resource: String = resource.trim().to_owned(); | ||
| if resource.is_empty() { | ||
| continue; | ||
| } | ||
| let first_new_line = resource.find("\n"); | ||
| let first_new_line_pos; | ||
| // No new line, but appears to encode mime type and teh content is not base64, so can be empty | ||
| if first_new_line.is_none() && resource.contains(" ") && resource.contains("/") && !resource.contains(";base64") { | ||
| first_new_line_pos = resource.len(); | ||
| } else if first_new_line.is_none() { | ||
| continue; | ||
| } else { | ||
| first_new_line_pos = first_new_line.unwrap(); | ||
| } | ||
| let (first_line, body) = resource.split_at(first_new_line_pos); | ||
| let mut first_line_items = first_line.split_ascii_whitespace(); | ||
| let (name, rtype) = ( | ||
| first_line_items.next(), | ||
| first_line_items.next() | ||
| ); | ||
| if name.is_none() || rtype.is_none() { | ||
| continue; | ||
| } | ||
| let rtype = rtype.unwrap().to_owned(); | ||
| let name = name.unwrap().to_owned(); | ||
| let body = body.trim().to_owned(); | ||
|
|
||
| let ttr = type_to_resource.entry(rtype).or_insert(HashMap::new()); | ||
| ttr.insert(name, body); | ||
| } | ||
|
|
||
| // Create a mapping from resource name to { contentType, data } | ||
| // used for request redirection. | ||
| let mut resources: HashMap<String, Resource> = HashMap::new(); | ||
| for (content_type, type_resources) in type_to_resource { | ||
| for (name, resource) in type_resources { | ||
| resources.insert(name, Resource { | ||
| content_type: content_type.to_owned(), | ||
| data: resource | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| Resources { | ||
| resources, | ||
| } | ||
| } | ||
|
|
||
| pub fn get_resource(&self, name: &str) -> Option<&Resource> { | ||
| self.resources.get(name) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| use super::*; | ||
| use crate::utils; | ||
|
|
||
| #[test] | ||
| fn parses_empty_resources() { | ||
| let resources = Resources::parse(""); | ||
| assert!(resources.resources.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parses_one_resource() { | ||
| let resources_str = "foo application/javascript\ncontent"; | ||
| let resources = Resources::parse(resources_str); | ||
| assert!(resources.resources.is_empty() == false); | ||
| let mut expected = HashMap::new(); | ||
| expected.insert("foo".to_owned(), Resource { | ||
| content_type: "application/javascript".to_owned(), | ||
| data: "content".to_owned() | ||
| }); | ||
| assert_eq!(resources.resources, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parses_two_resources() { | ||
| let resources_str = r###" | ||
| foo application/javascript | ||
| content1 | ||
| pixel.png image/png;base64 | ||
| content2"###; | ||
| let resources = Resources::parse(resources_str); | ||
| assert!(resources.resources.is_empty() == false); | ||
| let mut expected = HashMap::new(); | ||
| expected.insert("foo".to_owned(), Resource { | ||
| content_type: "application/javascript".to_owned(), | ||
| data: "content1".to_owned() | ||
| }); | ||
| expected.insert("pixel.png".to_owned(), Resource { | ||
| content_type: "image/png;base64".to_owned(), | ||
| data: "content2".to_owned() | ||
| }); | ||
| assert_eq!(resources.resources, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn robust_to_weird_format() { | ||
| let resources_str = r###" | ||
| # Comment | ||
| # Comment 2 | ||
| foo application/javascript | ||
| content1 | ||
| # Comment 3 | ||
| # Type missing | ||
| pixel.png | ||
| content | ||
| # Content missing | ||
| pixel.png image/png;base64 | ||
| # This one is good! | ||
| pixel.png image/png;base64 | ||
| content2 | ||
| "###; | ||
|
|
||
| let resources = Resources::parse(resources_str); | ||
| assert!(resources.resources.is_empty() == false); | ||
| let mut expected = HashMap::new(); | ||
| expected.insert("foo".to_owned(), Resource { | ||
| content_type: "application/javascript".to_owned(), | ||
| data: "content1".to_owned() | ||
| }); | ||
| expected.insert("pixel.png".to_owned(), Resource { | ||
| content_type: "image/png;base64".to_owned(), | ||
| data: "content2".to_owned() | ||
| }); | ||
| assert_eq!(resources.resources, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parses_noop_resources() { | ||
| let resources_str = r###" | ||
| nooptext text/plain | ||
| noopcss text/css | ||
| "###; | ||
| let resources = Resources::parse(resources_str); | ||
| assert!(resources.resources.is_empty() == false); | ||
| let mut expected = HashMap::new(); | ||
| expected.insert("nooptext".to_owned(), Resource { | ||
| content_type: "text/plain".to_owned(), | ||
| data: "".to_owned() | ||
| }); | ||
| expected.insert("noopcss".to_owned(), Resource { | ||
| content_type: "text/css".to_owned(), | ||
| data: "".to_owned() | ||
| }); | ||
| assert_eq!(resources.resources, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn handles_ubo_resources() { | ||
| let resources_lines = utils::read_file_lines("data/uBlockOrigin/resources.txt"); | ||
| let resources_str = resources_lines.join("\n"); | ||
| assert!(!resources_str.is_empty()); | ||
| let resources = Resources::parse(&resources_str); | ||
| assert!(resources.resources.is_empty() == false); | ||
| assert_eq!(resources.resources.len(), 110); | ||
| } | ||
| } |
Oops, something went wrong.
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Could this be logged somehow, even if its just mirroring the "didn't understand filter: X" stuff the current lib does? Would be a nice, noisy reminder if there is some new filter format we don't support, something like that