Adblock/RegexFilter.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 98 CC 13 SCAR 34.1
CC
13
Cov
0%
_from_text
42.0
matches
42.0
new
12.0
CRAP 30.3
1 package WWW::Adblock::RegexFilter;
2  
3 use strict;
4 use warnings;
5 use 5.006;
6  
7 our $VERSION = "0.02";
8  
9 =head1 NAME
10  
11 WWW::Adblock::RegexFilter - implement a single Adblock filter
12  
13 =head1 DESCRIPTION
14  
15 Used by WWW::Adblock to implement a single filter. Should not be called by an end user.
16  
17 =head2 Methods
18  
19 =head3 new
20  
21 my $f = WWW::Adblock::Filter->new();
22  
23 Creates a new object. Returns undef on failure or itself on success.
24  
25 =cut
26  
27 sub new {
28 my ( $class, %args ) = @_;
29  
30 my $self = bless( {}, $class );
31  
32 # Create a blocking rule by default
33 $self->{type} = 'blocking';
34 $self->{case_sensitive} = 0;
35 $self->{domains} = undef;
36 $self->{regex} = undef;
37  
38 return unless exists $args{text};
39 return unless $self->_from_text( $args{text} );
40  
41 return $self;
42 }
43  
44 =head3
45  
46 $f->_from_text("filter text");
47  
48 Called by new to setup the filter.
49  
50 =cut
51  
52 sub _from_text {
53 my ( $self, $text ) = @_;
54  
55 return 0 unless defined $text;
56  
57 if ( $text =~ m/^@@(.+)$/ ) {
58 $text = $1;
59 $self->{type} = 'whitelist';
60 }
61  
62 # If this rule has options, parse them out
63 if ( $text =~ /\$(~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*)$/ ) {
64 $text = $`;
65 my @options = split( /,/, $1 );
66  
67 foreach my $o (@options) {
68 my ( $option, $value ) = split( /=/, $o, 2 );
69 $option = uc($option);
70  
71 # TODO: Add content type matching too. For URI filtering it
72 # doesn't help but it would be necessary for element
73 # filtering.
74 if ( $option eq "MATCH_CASE" ) {
75 $self->{case_sensitive} = 1;
76  
77 }
78 elsif ( $option eq "DOMAIN" && defined $value ) {
79 $self->{domains} = [ split( /\|/, $value ) ];
80  
81 }
82  
83 # Other options that aren't implemented
84 # "THIRD_PARTY", "~THIRD_PARTY", "COLLAPSE", "~COLLAPSE"
85 }
86 }
87  
88 $text =~ s/\*+/*/g; # Remove multiple wildcards
89 $text =~ s/^\*+//; # Remove leading wildcards
90 $text =~ s/\*+$//; # Remove trailing wildcards
91 $text =~ s/\^\|$/\^/; # Remove anchors following separator
92 $text =~ s/(\W)/\\$1/g; # Escape special symbols
93 $text =~ s/\\\*/.*/g; # Replace wildcards with .*
94  
95 # Separator placeholders (all ANSI characters but alpha or _%.-)
96 $text =~
97 s/\\\^/(?:[\x00-\x24\x26-\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\x80]|\$)/g;
98  
99 # Extended anchor
100 $text =~ s/^\\\|\\\|/^[\\w\\-]+:\/+(?!\/)(?:[^\/]+\.)?/;
101  
102 $text =~ s/^\\\|/^/; # Start anchor
103 $text =~ s/\\\|$/\$/; # End anchor
104  
105 $self->{regex} = qr/$text/;
106 return 1;
107 }
108  
109 sub matches {
110 my ( $self, $uri, $domain ) = @_;
111  
112 # TODO: Should support the other options (contentType, thirdParty)
113  
114 # If the domain is given and this rule is constrained to domains, check
115 # whether we should proceed
116 if ( defined $domain && defined $self->{domains} ) {
117 if ( !grep /^$domain$/, $self->{domains} ) {
118 return 0;
119 }
120 }
121  
122 if ( $uri =~ $self->{regex} ) {
123  
124 #print "$uri matched $r (mode " . $self->{type} . ")\n";
125 return 1 if $self->{type} eq 'blocking';
126 return 2 if $self->{type} eq 'whitelist';
127 }
128  
129 return 0;
130 }
131  
132 1;