| 1 | package WWW::RobotRules::Parser::MultiValue; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # core | ||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # cpan | ||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Text::Glob qw(match_glob); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Hash::MultiValue; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||||
| 16 | 3 | new => 1, | |||||||||||||||||||||||||||
| 17 | ro => [qw(agent)], | ||||||||||||||||||||||||||||
| 18 | 3 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use constant { | ||||||||||||||||||||||||||||
| 21 | 3 | WILDCARD => 'wc', | |||||||||||||||||||||||||||
| 22 | ME => 'me', | ||||||||||||||||||||||||||||
| 23 | TRANSLATOR => { | ||||||||||||||||||||||||||||
| 24 | allow => '_translate_path_pattern', | ||||||||||||||||||||||||||||
| 25 | disallow => '_translate_path_pattern', | ||||||||||||||||||||||||||||
| 26 | 'crawl-delay' => '_translate_delay', | ||||||||||||||||||||||||||||
| 27 | 'request-rate' => '_translate_rate', | ||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | 3 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub _uri { | ||||||||||||||||||||||||||||
| 32 | 92 | ▶ | my ($uri) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_uri: called CC=5 SCAR=19.4
| |||||||||||||||||||||||||||||
| 33 | 92 | ▶ | $uri = URI->new($uri.q()) unless blessed($uri) && $uri->isa('URI'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($uri) and $uri->isa("URI")
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("URI")
builtin::blessed($uri)
$uri->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("URI")
| |||||||||||||||||||||||||||||
| 34 | 92 | ▶ | return unless $uri->can('host') && $uri->can('port'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$uri->can("host") and $uri->can("port")
MC/DC0%0/2
$uri->can("host") and $uri->can("port")
$uri->can("host")
$uri->can("port")
Truth table33%1/3
A and B
A= $uri->can("host")B= $uri->can("port")
| |||||||||||||||||||||||||||||
| 35 | 92 | return $uri; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub _domain { | ||||||||||||||||||||||||||||
| 39 | 92 | ▶ | my ($uri) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_domain: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 92 | return sprintf '%s:%d', $uri->host, $uri->port; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub _rules { | ||||||||||||||||||||||||||||
| 44 | 172 | ▶ | my ($self, $domain) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_rules: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 45 | 172 | ▶ | return $self->{rules}->{$domain} //= Hash::MultiValue->new; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"rules"}{$domain} //= "Hash::MultiValue"->new
MC/DC0%0/2
$self->{"rules"}{$domain} //= "Hash::MultiValue"->new
$self->{"rules"}{$domain}
"Hash::MultiValue"->new
Truth table66%2/3
A //= B
A= $self->{"rules"}{$domain}B= "Hash::MultiValue"->new
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub rules_for { | ||||||||||||||||||||||||||||
| 49 | 16 | ▶ | my ($self, $uri) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rules_for: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 16 | ▶ | $uri = _uri($uri) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | or return Hash::MultiValue->new; | ||||||||||||||||||||||||||||
| 52 | 16 | my $path_query = $uri->path_query; | |||||||||||||||||||||||||||
| 53 | 16 | my $domain = _domain($uri); | |||||||||||||||||||||||||||
| 54 | 16 | return $self->_rules($domain); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub parse { | ||||||||||||||||||||||||||||
| 58 | 20 | ▶ | my ($self, $robots_txt_uri, $txt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 20 | ▶ | $robots_txt_uri = _uri($robots_txt_uri) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | or return; | ||||||||||||||||||||||||||||
| 61 | 20 | my $domain = _domain($robots_txt_uri); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 20 | my $ua = WILDCARD; | |||||||||||||||||||||||||||
| 64 | 20 | my $anon_rules = Hash::MultiValue->new; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 20 | ▶ | $txt = ($txt//'') =~ s|\r\n|\n|gr; | ||||||||||||||||||||||||||
|
Condition50%1/2
$txt // ""
MC/DC0%0/1
$txt // ""
$txt
Truth table50%1/2
A
A= $txt
| |||||||||||||||||||||||||||||
| 67 | 20 | for my $line (split /[\r\n]/, $txt) { | |||||||||||||||||||||||||||
| 68 | 135 | $line =~ s/(?:^\s*|\s*$|\s*[#].*$)//g; | |||||||||||||||||||||||||||
| 69 | 135 | ▶ | next if $line =~ /^\s*$/; # skip empty line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 108 | ▶ | if ($line =~ /^User-Agent\s*:\s*(.*)$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 17 | $ua = $self->match_ua($1); | |||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 91 | ▶ | next unless $ua; # skip directives for other UA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 69 | ▶ | if ($line =~ /^([^:]+?)\s*:\s*(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 69 | my ($rule, $value) = (lc $1, $2); | |||||||||||||||||||||||||||
| 78 | 69 | ▶ | if (my $method = TRANSLATOR->{$rule}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 69 | ($rule, $value) = $self->$method( | |||||||||||||||||||||||||||
| 80 | $rule, $value, $robots_txt_uri, | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 69 | ▶ | next unless $rule; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 58 | ▶ | if ($ua eq ME) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 30 | $self->_rules($domain)->add($rule => $value); | |||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||
| 88 | 28 | $anon_rules->add($rule => $value); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Add rules for default UA as a lower precedence | ||||||||||||||||||||||||||||
| 95 | $self->_rules($domain)->add($_ => $anon_rules->get_all($_)) | ||||||||||||||||||||||||||||
| 96 | 20 | for $anon_rules->keys; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 20 | return $self; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub match_ua { | ||||||||||||||||||||||||||||
| 102 | 29 | ▶ | my ($self, $pattern) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match_ua: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 29 | ▶ | return WILDCARD if $pattern eq '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 21 | ▶ | return ME if index(lc $self->_short_agent, lc($pattern)) >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 12 | return undef; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub _match_path ($$) { | ||||||||||||||||||||||||||||
| 109 | 198 | ▶ | my ($str, $pattern) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_match_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 198 | local $Text::Glob::strict_leading_dot = 0; | |||||||||||||||||||||||||||
| 111 | 198 | local $Text::Glob::strict_wildcard_slash = 0; | |||||||||||||||||||||||||||
| 112 | 198 | return match_glob($pattern.'*', $str); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub allows { | ||||||||||||||||||||||||||||
| 116 | 56 | ▶ | my ($self, $uri) = @_; | ||||||||||||||||||||||||||
|
Subroutine
allows: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 56 | ▶ | $uri = _uri($uri) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | or return; | ||||||||||||||||||||||||||||
| 119 | 56 | my $path_query = $uri->path_query; | |||||||||||||||||||||||||||
| 120 | 56 | my $domain = _domain($uri); | |||||||||||||||||||||||||||
| 121 | 56 | for my $pattern ($self->_rules($domain)->get_all('allow')) { | |||||||||||||||||||||||||||
| 122 | 136 | ▶ | return 1 if _match_path $path_query, $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 42 | for my $pattern ($self->_rules($domain)->get_all('disallow')) { | |||||||||||||||||||||||||||
| 125 | 62 | ▶ | return 0 if _match_path $path_query, $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 22 | return 1; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub delay_for { | ||||||||||||||||||||||||||||
| 131 | 16 | ▶ | my ($self, $uri, $base) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delay_for: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 16 | my ($delay) = $self->rules_for($uri)->get_all('crawl-delay'); | |||||||||||||||||||||||||||
| 133 | 16 | ▶ | $delay *= ( $base || 1 ) if defined $delay; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$base || 1
MC/DC100%1/1
$base || 1
$base
Truth table100%2/2
A
A= $base
| |||||||||||||||||||||||||||||
| 134 | 16 | return $delay; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub _short_agent { | ||||||||||||||||||||||||||||
| 138 | 21 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_short_agent: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 139 | 21 | my $name = $self->agent; | |||||||||||||||||||||||||||
| 140 | 21 | ▶ | $name = $1 if $name =~ m!^(\S+)!; # first word | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 21 | $name =~ s!/.*$!!; # no version | |||||||||||||||||||||||||||
| 142 | 21 | return $name; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub _translate_path_pattern { | ||||||||||||||||||||||||||||
| 146 | 50 | ▶ | my ($self, $key, $value, $base_uri) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_translate_path_pattern: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 50 | my $ignore; | |||||||||||||||||||||||||||
| 149 | 50 | eval { | |||||||||||||||||||||||||||
| 150 | 50 | my $uri = URI->new_abs($value, $base_uri); | |||||||||||||||||||||||||||
| 151 | 50 | ▶ | $ignore++ unless $uri->scheme eq $base_uri->scheme; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 50 | ▶ | $ignore++ unless lc($uri->host) eq lc($base_uri->host); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 50 | ▶ | $ignore++ unless $uri->port eq $base_uri->port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||
| 155 | 50 | ▶ | return () if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 50 | ▶ | return () if $ignore; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 43 | return ($key, $value); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub _translate_delay { # into delay in milliseconds | ||||||||||||||||||||||||||||
| 162 | 7 | ▶ | my ($self, $key, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_translate_delay: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 163 | 7 | ▶ | return () unless $value =~ qr!\A[0-9.]+\z!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 6 | return ('crawl-delay', $value); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | sub _translate_rate { # into delay in milliseconds | ||||||||||||||||||||||||||||
| 168 | 12 | ▶ | my ($self, $key, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_translate_rate: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 169 | 12 | ▶ | return () unless $value =~ qr!\A([0-9.]+)\s*/\s*([0-9.]+)\z!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 11 | ▶ | return () unless $1+0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 9 | return ('crawl-delay', $2 / $1); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 3 | 1; | |||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||