blib/lib/Plack/Middleware/DetectRobots.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 90.0% 27 / 30 Conditioncond 93.3% 14 / 15 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 95.4% 126 / 132 CC 22 SCAR 21.3
CC
22
Cov
95%
call
14.0
_read_list
7.1
prepare_app
2.0
CRAP 8.4
↑ Summary
1  
2 package Plack::Middleware::DetectRobots;
3 5 $Plack::Middleware::DetectRobots::VERSION = '0.03';
4 # ABSTRACT: Automatically set a flag in the environment if a robot client is detected
5  
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Plack::Util::Accessor qw( env_key basic_check extended_check generic_check local_regexp );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Regexp::Assemble qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶sub prepare_app {
Subroutine prepare_app: called CC=2 SCAR=6.9
Pod documented
15 4 my $self = shift;
16 4 ▶ $self->basic_check(1) unless defined $self->basic_check;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->basic_check 3 1 4
17 4 return;
18 }
19  
20 66 ▶sub call {
Subroutine call: called CC=14 SCAR=26.4
Pod documented
21 66 my ( $self, $env ) = @_;
22  
23 66 state $reList = _read_list();
24 66 state $basic = _assemble( $reList, 'basic' );
25 66 state $extended = _assemble( $reList, 'extended' );
26 66 state $generic = _assemble( $reList, 'generic' );
27 66 $reList = undef;
28  
29 66 ▶ my $key = defined( $self->env_key ) ? $self->env_key : 'robot_client';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->env_key ? : 0 66 66
30  
31 66 my $ua = $env->{'HTTP_USER_AGENT'};
32  
33 66 $env->{$key} = 0;
34  
35 66 my $local = $self->local_regexp;
36 66 ▶ if ( defined($local) and ( ref $local eq ref qr// ) and ( $ua =~ $local ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $local and ref $local eq ref qr// and $ua =~ /$local/) 1 65 66
Condition83%5/6
defined $local and ref $local eq ref qr// and $ua =~ /$local/
!ll&&!rl&&r
6321
defined $local and ref $local eq ref qr//
!ll&&!rl&&r
6303
MC/DC66%2/3
defined $local and ref $local eq ref qr// and $ua =~ /$local/
defined $local ref $local eq ref qr// $ua =~ /$local/
Truth table75%3/4
A and B and C
A= defined $localB= ref $local eq ref qr//C= $ua =~ /$local/
ABCresult
0XX0
10X0
1100
1111
37 1 $env->{$key} = 'LOCAL';
38 }
39  
40 66 ▶ if ( !$env->{$key} and $self->basic_check ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $env->{$key} and $self->basic_check) 63 3 66
Condition100%3/3
not $env->{$key} and $self->basic_check
!ll&&!rl&&r
1263
MC/DC100%2/2
not $env->{$key} and $self->basic_check
not $env->{$key} $self->basic_check
Truth table100%3/3
not(A) and B
A= not $env->{$key}B= $self->basic_check
ABresult
0X0
100
111
41 63 ▶ if ( $ua =~ $basic ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua =~ /$basic/) 21 42 63
42 21 $env->{$key} = 'BASIC';
43 }
44 }
45  
46 66 ▶ if ( !$env->{$key} and $self->extended_check ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $env->{$key} and $self->extended_check) 14 52 66
Condition100%3/3
not $env->{$key} and $self->extended_check
!ll&&!rl&&r
223014
MC/DC100%2/2
not $env->{$key} and $self->extended_check
not $env->{$key} $self->extended_check
Truth table100%3/3
not(A) and B
A= not $env->{$key}B= $self->extended_check
ABresult
0X0
100
111
47 14 ▶ if ( $ua =~ $extended ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua =~ /$extended/) 4 10 14
48 4 $env->{$key} = 'EXTENDED';
49 }
50 }
51  
52 66 ▶ if ( !$env->{$key} and $self->generic_check ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $env->{$key} and $self->generic_check) 14 52 66
Condition100%3/3
not $env->{$key} and $self->generic_check
!ll&&!rl&&r
262614
MC/DC100%2/2
not $env->{$key} and $self->generic_check
not $env->{$key} $self->generic_check
Truth table100%3/3
not(A) and B
A= not $env->{$key}B= $self->generic_check
ABresult
0X0
100
111
53 14 ▶ if ( $ua =~ $generic ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua =~ /$generic/) 4 10 14
54 4 $env->{$key} = 'GENERIC';
55 }
56 }
57  
58 66 return $self->app->($env);
59 } ## end sub call
60  
61 12 ▶sub _assemble {
Subroutine _assemble: called CC=2 SCAR=6.9
62 12 my ( $bots, $type ) = @_;
63  
64 12 my $ra = Regexp::Assemble->new( flags => 'i' );
65 12 foreach my $r ( @{ $bots->{$type} } ) {
66 3176 $ra->add($r);
67 }
68  
69 12 return $ra->re;
70 }
71  
72 4 ▶sub _read_list {
Subroutine _read_list: called CC=7 SCAR=19.6
73 4 my $bots = { basic => [], extended => [], generic => [], };
74 4 my $currentType = 'basic';
75  
76 4 state $pos = tell(Plack::Middleware::DetectRobots::DATA);
77 4 ▶ if ( $ENV{'HARNESS_ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HARNESS_ACTIVE"}) 4 0 4
78 4 seek( Plack::Middleware::DetectRobots::DATA, $pos, 0 );
79 }
80  
81 4 ▶ while (<Plack::Middleware::DetectRobots::DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 3180 4 3184
82 3180 chomp;
83 3180 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 4 3176 3180
84 3176 ▶ $currentType = 'extended' if /\A##\s+EXTENDED/;
Branch: 1 branch
BranchTrueFalseTotal
if /\A##\s+EXTENDED/ 4 3172 3176
85 3176 ▶ $currentType = 'generic' if /\A##\s+GENERIC/;
Branch: 1 branch
BranchTrueFalseTotal
if /\A##\s+GENERIC/ 4 3172 3176
86  
87 3176 push @{ $bots->{$currentType} }, $_;
88 }
89  
90 4 ▶ if ( !$ENV{'HARNESS_ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"HARNESS_ACTIVE"}) 0 4 4
91 0 close Plack::Middleware::DetectRobots::DATA;
92 }
93  
94 4 return $bots;
95 }
96  
97 5 1;
98  
99 =pod
100  
101 =encoding utf-8
102  
103 =head1 NAME
104  
105 Plack::Middleware::DetectRobots - Automatically set a flag in the environment if a robot client is detected
106  
107 =head1 VERSION
108  
109 version 0.03
110  
111 =head1 SYNOPSIS
112  
113 use Plack::Builder;
114  
115 my $app = sub { ... } # as usual
116  
117 builder {
118 enable 'DetectRobots';
119 # or: enable 'DetectRobots', env_key => 'psgix.robot_client';
120 # or: enable 'DetectRobots', extended_check => 1, generic_check => 1;
121 $app;
122 };
123  
124 # ... and later ...
125
126 if ( $env->{'robot_client'} ) {
127 # ... do something ...
128 }
129  
130 =head1 DESCRIPTION
131  
132 This Plack middleware uses the list of robots that is part of the
133 L<AWStats log analyzer|http://awstats.org/> software package to
134 analyse the C<User-Agent> HTTP header and to set an environment
135 flag to either a true or false value depending on the detection
136 of a robot client.
137  
138 Once activated it checks the User-Agent HTTP header against a
139 basic list of patterns for common bots.
140  
141 If you activate the appropriate options, it can also use an extended
142 list for the detection of less common bots (cf. C<extended_check>)
143 and / or a list of quite generic patterns to detect unknown bots
144 (cf. C<generic_check>).
145  
146 You may also pass in your own regular expression as a string for
147 further checks (cf. <local_regexp>).
148  
149 The checks are executed in this order:
150  
151 B<1.> Local regular expression
152  
153 B<2.> Basic check
154  
155 B<3.> Extended check
156  
157 B<4.> Generic check
158  
159 If a check yields a positive result (i.e.: detects a bot) the
160 remaining checks are skipped.
161  
162 Depending on the check which detected a bot, the environment flag
163 is set to one of these values: C<LOCAL>, C<BASIC>, C<EXTENDED>, or
164 C<GENERIC>.
165  
166 If no bot is detected, the flag is set to C<0>.
167  
168 The default name of the flag in the environment is C<robot_client>,
169 but this can be customized by setting the C<env_key> option when
170 enabling this middleware.
171  
172 It might make sense to use C<psgix.robot_client> by default instead,
173 but the PSGI spec states that the "'psgix.' prefix is reserved for
174 officially blessed extensions" - which does not apply to this module.
175 You may, however, set the key to C<psgix.robot_client> yourself
176 by using the C<env_key> option mentioned before.
177  
178 =head1 WARNING
179  
180 This software is currently considered BETA and still needs to
181 be seriously tested!
182  
183 =head1 ROBOTS LIST
184  
185 Based on B<Revision 2d289e, 2014-11-20> of
186 L<http://sourceforge.net/p/awstats/code/ci/develop/tree/wwwroot/cgi-bin/lib/robots.pm>.
187  
188 B<Note:> that list might be somewhat dated, as I did not find bingbot
189 in the list of common bots (only in the extended list) while it's
190 predecessor msnbot was considered common.
191  
192 =head1 CONFIGURATION
193  
194 You may specify the following option when enabling the middleware:
195  
196 =over 4
197  
198 =item C<env_key>
199  
200 Set the name of the entry in the environment hash.
201  
202 =item C<basic_check>
203  
204 You may deactivate the standard checks by setting this option to
205 a false value. E.g. if your are only interested in obscure bots
206 or in your local pattern checks.
207  
208 By setting this option to a false value while simultaneously
209 passing a regular expression to C<local_regexp> one can imitate
210 the behaviour of L<Plack::Middleware::BotDetector>.
211  
212 =item C<extended_check>
213  
214 Determines if an extended list of less often seen robots is also
215 checked for.
216 By default, only common robots are checked for, because the extended
217 check requires a rather large and complex regular expression.
218 Set this param to a true value to change the default behaviour.
219  
220 =item C<generic_check>
221  
222 Determines if the User-Agent string is also analysed to determine
223 if it contains certain strings that generically identify the
224 client as a bot, e.g. "spider" or "crawler"
225 By default, this check is not performed, even though it uses only
226 a relatively short and simple regex..
227 Set this param to a true value to change the default behaviour.
228  
229 =item C<local_regexp>
230  
231 You may optionally pass in your own regular expression (as a Regexp
232 object using C<qr//>) to check for additional patterns in the
233 User-Agent string.
234  
235 =back
236  
237 =head1 SEE ALSO
238  
239 L<Plack>, L<Plack::Middleware>, L<Plack::Middleware::BotDetector>,
240 L<http://awstats.org/>
241  
242 The functionality provided by C<Plack::Middleware::BotDetector> is
243 basically the same as that of this module, but it requires you to
244 pass in your own regular expression and does not include a default
245 list of known bots.
246  
247 =head1 AUTHOR
248  
249 Heiko Jansen <hjansen@cpan.org>
250  
251 =head1 COPYRIGHT AND LICENSE
252  
253 This software is copyright (c) 2015 by Heiko Jansen.
254  
255 This is free software; you can redistribute it and/or modify it under
256 the same terms as the Perl 5 programming language system itself.
257  
258 =cut
259  
260 __DATA__
↑ Summary