blib/lib/WWW/DoctypeGrabber.pm

filter: Statementstmt 86.7% 72 / 83 Branchbran 66.6% 24 / 36 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.6% 11 / 12 Podpod 100% 2 / 2 totaltotal 77.7% 112 / 144 CC 24 SCAR 22.2
CC
24
Cov
79%
_parse_doctype
16.4
new
6.7
_set_error
6.0
CRAP 9.2
↑ Summary
1 package WWW::DoctypeGrabber;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.008';
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use overload q|""|, sub { shift->doctype; };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
12 1 __PACKAGE__->mk_group_accessors( simple => qw(
13 ua
14 error
15 doctype
16 result
17 raw
18 ));
19  
20 2 ▶sub new {
Subroutine new: called CC=6 SCAR=19.0
Pod documented
21 2 my $self = bless {}, shift;
22 2 ▶ croak "Must have even number of arguments to new()"
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 2 2
23 if @_ & 1;
24  
25 2 my %args = @_;
26 2 $args{ +lc } = delete $args{ $_ } for keys %args;
27 2 ▶ $args{timeout} ||= 30;
Condition50%1/2
$args{"timeout"} ||= 30
l!l
02
MC/DC0%0/1
$args{"timeout"} ||= 30
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
28 2 ▶ $args{max_size} ||= 500;
Condition50%1/2
$args{"max_size"} ||= 500
l!l
02
MC/DC0%0/1
$args{"max_size"} ||= 500
$args{"max_size"}
Truth table50%1/2
A
A= $args{"max_size"}
Aresult
11
00
29 2 ▶ $args{ua} ||= LWP::UserAgent->new(
Condition33%1/3
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", 30, "max_size", 500, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.13) Gecko/20080325 Ubuntu/7.10 (gutsy) Firefox/2.0.0.13")
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", 30, "max_size", 500, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.13) Gecko/20080325 Ubuntu/7.10 (gutsy) Firefox/2.0.0.13")
$args{"ua"} "LWP::UserAgent"->new("timeout", 30, "max_size", 500, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.13) Gecko/20080325 Ubuntu/7.10 (gutsy) Firefox/2.0.0.13")
Truth table33%1/3
A ||= B
A= $args{"ua"}B= "LWP::UserAgent"->new("timeout", 30, "max_size", 500, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.13) Gecko/20080325 Ubuntu/7.10 (gutsy) Firefox/2.0.0.13")
ABresult
1X1
011
000
30 timeout => 30,
31 max_size => 500,
32 agent => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.13)'
33 . ' Gecko/20080325 Ubuntu/7.10 (gutsy) Firefox/2.0.0.13',
34 );
35  
36 2 $self->ua( $args{ua} );
37 2 $self->raw( $args{raw} );
38 2 return $self;
39 }
40  
41 7 ▶sub grab {
Subroutine grab: called CC=4 SCAR=14.6
Pod documented
42 7 my ( $self, $uri ) = @_;
43  
44 7 $self->$_(undef) for qw(error doctype result);
45  
46 7 ▶ $uri = "http://$uri"
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^\w+://] 0 7 7
47 unless $uri =~ m{^\w+://};
48  
49 7 my $response = $self->ua->get( $uri );
50 7 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 7 0 7
51 7 return $self->result(
52 $self->_parse_doctype(
53 $response->content,
54 $response->header('Content-type'),
55 )
56 );
57 }
58 else {
59 0 return $self->_set_error( $response, 'net' );
60 }
61 }
62  
63 7 ▶sub _parse_doctype {
Subroutine _parse_doctype: called CC=15 SCAR=27.9
64 7 my ( $self, $content, $mime ) = @_;
65  
66 7 my %parse_result = (
67 xml_prolog => 0,
68 non_white_space => 0,
69 has_doctype => 0,
70 doctype => '',
71 mime => $mime,
72 );
73  
74 DOCTYPE_PARSE: {
75 7 ▶ if ( my ( $pre_text ) = $content =~ /(.+)(?=<!DOCTYPE)/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($pre_text) = $content =~ /(.+)(?=<!DOCTYPE)/is) 1 6 7
76 1 ▶ if ( my $xml_count = $pre_text =~ s/<\?xml[^>]+?\?>//ig ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xml_count = $pre_text =~ s/<\?xml[^>]+?\?>//gi) 1 0 1
77 1 $parse_result{xml_prolog} = $xml_count;
78 }
79 1 $pre_text =~ s/\s+//g;
80 1 $parse_result{non_white_space} = length $pre_text;
81 }
82  
83 7 ▶ if ( my ( $doctype_string ) = $content =~ m{(<!DOCTYPE[^>]+>)}i ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($doctype_string) = $content =~ /(<!DOCTYPE[^>]+>)/i) { } 5 2 7
elsif ($self->raw) { } 0 2 2
84 5 $parse_result{has_doctype} = 1;
85 5 $doctype_string =~ s/\s+/ /g;
86 5 $doctype_string =~ s/^\s+|\s+$//g;
87 5 ▶ $self->raw
Branch: 1 branch
BranchTrueFalseTotal
$self->raw and return $self->doctype($doctype_string) 1 4 5
88 and return $self->doctype($doctype_string);
89  
90 4 ▶ if ( $doctype_string
Branch: 1 branch
BranchTrueFalseTotal
if ($doctype_string =~ s[^<!DOCTYPE html PUBLIC "-//W3C//DTD ][]i) { } 3 1 4
91 =~ s{^<!DOCTYPE html PUBLIC "-//W3C//DTD }{}i
92 ) {
93 3 my @doctype_bits = ();
94 3 my ( $type ) = $doctype_string =~ m{^[^/]+?(?=//)}g;
95 3 ▶ if ( !defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 3 3
96 0 $parse_result{doctype} = 'Invalid/Unknown';
97  
98 0 last DOCTYPE_PARSE;
99 }
100 3 ▶ $type =~ /^HTML 4.01$/i
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^HTML 4.01$/i and $type .= ' Strict' 1 2 3
101 and $type .= ' Strict';
102  
103 3 push @doctype_bits, $type;
104 3 ▶ if ( my ( $dtd_uri ) =
Branch: 1 branch
BranchTrueFalseTotal
if (my($dtd_uri) = $doctype_string =~ /\s"(\S+)"\s*>$/) 3 0 3
105 $doctype_string =~ m{\s"(\S+)"\s*>$}
106 ) {
107 3 my $dtd_uris = $self->_get_dtd_uri_table;
108 3 ▶ if ( exists $dtd_uris->{ $type } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dtd_uris->{$type}) { } 3 0 3
109 3 ▶ if ( $dtd_uris->{ $type } eq $dtd_uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dtd_uris->{$type} eq $dtd_uri) { } 3 0 3
110 3 push @doctype_bits, '+ url';
111 }
112 else {
113 0 push @doctype_bits, '+ Invalid url';
114 }
115 }
116 else {
117 0 push @doctype_bits, '+ Unknown url';
118 }
119 }
120 3 $parse_result{doctype} = join q| |, @doctype_bits;
121 }
122 else {
123 1 $parse_result{doctype} = $doctype_string;
124 }
125 }
126 elsif( $self->raw ) {
127 0 return $self->doctype('NO DOCTYPE');
128 }
129 }
130  
131 6 ▶ if ( $parse_result{has_doctype} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse_result{'has_doctype'}) { } 4 2 6
132 4 my @bits;
133 $parse_result{xml_prolog}
134 4 ▶ and push @bits, "+ $parse_result{xml_prolog} XML prolog";
Branch: 1 branch
BranchTrueFalseTotal
$parse_result{"xml_prolog"} and push @bits, qq[+ $parse_result{"xml_prolog"} XML prolog] 1 3 4
135  
136 $parse_result{non_white_space}
137 4 ▶ and push @bits, "+ $parse_result{non_white_space} "
Branch: 1 branch
BranchTrueFalseTotal
$parse_result{"non_white_space"} and push @bits, qq[+ $parse_result{"non_white_space"} ] . 'non-whitespace characters' 0 4 4
138 . "non-whitespace characters";
139  
140 4 $self->doctype( join q| |, $parse_result{doctype}, @bits );
141 }
142 else {
143 2 $self->doctype('NO DOCTYPE');
144 }
145  
146 6 return \%parse_result;
147 }
148  
149 3 ▶sub _get_dtd_uri_table {
Subroutine _get_dtd_uri_table: called CC=1 SCAR=0.0
150 return {
151 3 'HTML 4.01 Strict' =>
152 'http://www.w3.org/TR/html4/strict.dtd',
153  
154 'HTML 4.01 Transitional' =>
155 'http://www.w3.org/TR/html4/loose.dtd',
156  
157 'HTML 4.01 Frameset' =>
158 'http://www.w3.org/TR/html4/frameset.dtd',
159  
160 'XHTML 1.0 Strict' =>
161 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd',
162  
163 'XHTML 1.0 Transitional' =>
164 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd',
165  
166 'XHTML 1.0 Frameset' =>
167 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd',
168  
169 'XHTML 1.1' =>
170 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd',
171  
172 'XHTML Basic 1.0' =>
173 'http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd',
174  
175 'XHTML Basic 1.1' =>
176 'http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd',
177  
178 'XHTML 1.1 plus MathML 2.0 plus SVG 1.1' =>
179 'http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd',
180 };
181 }
182  
183 0 ▶sub _set_error {
Subroutine _set_error: not called CC=2 SCAR=17.9
184 0 my ( $self, $error_or_resp, $is_net ) = @_;
185 0 ▶ if ( $is_net ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_net) { } 0 0 0
186 0 $self->error( 'Network error: ' . $error_or_resp->status_line );
187 }
188 else {
189 0 $self->error( $error_or_resp );
190 }
191 0 return;
192 }
193  
194 1 1;
195 __END__
↑ Summary