Skip to content

Commit

Permalink
remove some filter methods from public API
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Aug 14, 2024
1 parent b50aed1 commit 400b0dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/filters/cosmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub enum CosmeticFilterOperator {
Xpath(String),
}

pub enum CosmeticFilterLocationType {
pub(crate) enum CosmeticFilterLocationType {
Entity,
NotEntity,
Hostname,
Expand All @@ -171,7 +171,7 @@ struct CosmeticFilterLocations {

impl CosmeticFilter {
#[inline]
pub fn locations_before_sharp(
pub(crate) fn locations_before_sharp(
line: &str,
sharp_index: usize,
) -> impl Iterator<Item = (CosmeticFilterLocationType, &str)> {
Expand Down Expand Up @@ -527,7 +527,7 @@ fn get_hashes_from_labels(hostname: &str, end: usize, start_of_domain: usize) ->

/// Returns a `Vec` of the hashes of all segments of `hostname` that may match an
/// entity-constrained rule.
pub fn get_entity_hashes_from_labels(hostname: &str, domain: &str) -> Vec<Hash> {
pub(crate) fn get_entity_hashes_from_labels(hostname: &str, domain: &str) -> Vec<Hash> {
if let Some((hostname_without_public_suffix, public_suffix)) =
get_hostname_without_public_suffix(hostname, domain)
{
Expand All @@ -545,7 +545,7 @@ pub fn get_entity_hashes_from_labels(hostname: &str, domain: &str) -> Vec<Hash>

/// Returns a `Vec` of the hashes of all segments of `hostname` that may match a
/// hostname-constrained rule.
pub fn get_hostname_hashes_from_labels(hostname: &str, domain: &str) -> Vec<Hash> {
pub(crate) fn get_hostname_hashes_from_labels(hostname: &str, domain: &str) -> Vec<Hash> {
get_hashes_from_labels(hostname, hostname.len(), hostname.len() - domain.len())
}

Expand Down
26 changes: 13 additions & 13 deletions src/filters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::regex_manager::RegexManager;
use crate::request;
use crate::utils::{self, Hash};

pub const TOKENS_BUFFER_SIZE: usize = 200;
pub(crate) const TOKENS_BUFFER_SIZE: usize = 200;

/// For now, only support `$removeparam` with simple alphanumeric/dash/underscore patterns.
static VALID_PARAM: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_\-]+$").unwrap());
Expand Down Expand Up @@ -1087,6 +1087,16 @@ impl NetworkFilter {
fn for_https(&self) -> bool {
self.mask.contains(NetworkFilterMask::FROM_HTTPS)
}

fn check_cpt_allowed(&self, cpt: &request::RequestType) -> bool {
match NetworkFilterMask::from(cpt) {
// TODO this is not ideal, but required to allow regexed exception rules without an
// explicit `$document` option to apply uBO-style.
// See also: https://github.com/uBlockOrigin/uBlock-issues/issues/1501
NetworkFilterMask::FROM_DOCUMENT => self.mask.contains(NetworkFilterMask::FROM_DOCUMENT) || self.is_exception(),
mask => self.mask.contains(mask),
}
}
}

impl fmt::Display for NetworkFilter {
Expand Down Expand Up @@ -1170,7 +1180,7 @@ fn compute_filter_id(
/// filters containing at least a * or ^ symbol. Because Regexes are expansive,
/// we try to convert some patterns to plain filters.
#[allow(clippy::trivial_regex)]
pub fn compile_regex(
pub(crate) fn compile_regex(
filter: &FilterPart,
is_right_anchor: bool,
is_left_anchor: bool,
Expand Down Expand Up @@ -1591,24 +1601,14 @@ fn check_pattern(
}
}

pub fn check_cpt_allowed(filter: &NetworkFilter, cpt: &request::RequestType) -> bool {
match NetworkFilterMask::from(cpt) {
// TODO this is not ideal, but required to allow regexed exception rules without an
// explicit `$document` option to apply uBO-style.
// See also: https://github.com/uBlockOrigin/uBlock-issues/issues/1501
NetworkFilterMask::FROM_DOCUMENT => filter.mask.contains(NetworkFilterMask::FROM_DOCUMENT) || filter.is_exception(),
mask => filter.mask.contains(mask),
}
}

fn check_options(filter: &NetworkFilter, request: &request::Request) -> bool {
// Bad filter never matches
if filter.is_badfilter() {
return false;
}
// We first discard requests based on type, protocol and party. This is really
// cheap and should be done first.
if !check_cpt_allowed(filter, &request.request_type)
if !filter.check_cpt_allowed(&request.request_type)
|| (request.is_https && !filter.for_https())
|| (request.is_http && !filter.for_http())
|| (!filter.first_party() && !request.is_third_party)
Expand Down

0 comments on commit 400b0dd

Please sign in to comment.