blib/lib/WWW/RobotRules/Parser.pm

filter: Statementstmt 82.1% 46 / 56 Branchbran 80.0% 24 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 100% 3 / 3 totaltotal 82.4% 80 / 97 CC 17 SCAR 24.4
CC
17
Cov
81%
parse
16.1
parse_uri
6.0
BEGIN
1.0
CRAP 11.5
↑ Summary
1 # $Id: /mirror/perl/WWW-RobotRules-Parser/trunk/lib/WWW/RobotRules/Parser.pm 31629 2007-12-01T13:31:37.690053Z daisuke $
2 #
3 # Copyright (c) 2006-2007 Daisuke Maki <daisuke@endeworks.jp>
4 # A lot of this code is based on WWW::RobotRules.
5  
6 package WWW::RobotRules::Parser;
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 BEGIN
13 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 $VERSION = '0.04001';
15 }
16  
17 6 ▶sub new { bless { }, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18  
19 sub parse_uri
20 0 ▶{
Subroutine parse_uri: not called CC=2 SCAR=17.9
Pod documented
21 0 my $self = shift;
22 0 my $uri = shift;
23  
24 0 require LWP::UserAgent;
25 0 my $ua = LWP::UserAgent->new;
26 0 my $res = $ua->get($uri);
27  
28 0 ▶ if (! $res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
29 0 require Carp;
30 0 Carp::croak("Failed to retrieve $uri: Got HTTP code " . $res->code);
31 }
32
33 0 $self->parse($uri, $res->content);
34 }
35  
36 sub parse
37 6 ▶{
Subroutine parse: called CC=16 SCAR=27.8
Pod documented
38 6 my $self = shift;
39 6 my $robot_txt_uri = shift;
40 6 my $text = shift;
41  
42 6 ▶ $robot_txt_uri = URI->new($robot_txt_uri) if ($robot_txt_uri);
Branch: 1 branch
BranchTrueFalseTotal
if $robot_txt_uri 6 0 6
43  
44 6 my %result;
45 # blank lines are significant, so turn CRLF into LF to avoid generating
46 # false ones
47 6 $text =~ s/\015\012/\012/g;
48  
49 6 my $ua_pattern;
50 # split at \012 or \015
51 6 for (split(/[\012\015]/, $text)) {
52 # Lines containing only a comment are discarded completely, and
53 # therefore do not indicate a record boundary
54 50 ▶ next if /^\s*\#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*\#/ 11 39 50
55  
56 39 s/\s*\#.*//; # Remove comments at end-of-line
57  
58 39 ▶ if (/^\s*User-Agent\s*:\s*(.*)/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*User-Agent\s*:\s*(.*)/i) { } 14 25 39
elsif (/^\s*Disallow\s*:\s*(.*)/i) { } 19 6 25
59 14 $ua_pattern = $1;
60 14 $ua_pattern =~ s/\s+$//;
61 } elsif (/^\s*Disallow\s*:\s*(.*)/i) {
62 19 ▶ if (! defined $ua_pattern) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua_pattern) 0 19 19
63 0 $ua_pattern = '*';
64 }
65  
66 19 my $disallow = $1;
67 19 $disallow =~ s/\s+$//;
68 19 ▶ if (length $disallow) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $disallow) 16 3 19
69 16 ▶ if ($robot_txt_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_txt_uri) 16 0 16
70 16 my $ignore;
71 16 eval {
72 16 my $u = URI->new_abs($disallow, $robot_txt_uri);
73 16 ▶ $ignore++ if $u->scheme ne $robot_txt_uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $u->scheme ne $robot_txt_uri->scheme 2 14 16
74 16 ▶ $ignore++ if lc($u->host) ne lc($robot_txt_uri->host);
Branch: 1 branch
BranchTrueFalseTotal
if lc $u->host ne lc $robot_txt_uri->host 2 13 15
75 15 ▶ $ignore++ if $u->port ne $robot_txt_uri->port;
Branch: 1 branch
BranchTrueFalseTotal
if $u->port ne $robot_txt_uri->port 2 13 15
76 15 $disallow = $u->path_query;
77 15 ▶ $disallow = "/" unless length $disallow;
Branch: 1 branch
BranchTrueFalseTotal
unless length $disallow 3 12 15
78 };
79 16 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 15 16
80 15 ▶ next if $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore 4 11 15
81 }
82 11 push @{$result{$ua_pattern}}, $disallow;
83 }
84 }
85 }
86 6 ▶ return wantarray ? %result : \%result
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
87 }
88  
89 2 1;
90  
91 __END__
↑ Summary