blib/lib/WebService/Validator/HTML/W3C/Fast.pm

filter: Statementstmt 25.8% 45 / 174 Branchbran 6.7% 5 / 74 Conditioncond 12.9% 4 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 65.0% 13 / 20 Podpod 20.0% 1 / 5 totaltotal 21.1% 68 / 321 CC 49 SCAR 54.2
CC
49
Cov
19%
new
509.1
_set_user_password
90.0
setup_stdin
72.0
CRAP 225.0
↑ Summary
1 package WebService::Validator::HTML::W3C::Fast;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use WebService::Validator::HTML::W3C();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Daemon();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Fcntl();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Temp();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Symbol();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use LWP::UserAgent();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use base qw(WebService::Validator::HTML::W3C);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '0.02';
16 1 our ($maximum_fork_attempts) = 4;
17  
18 =head1 NAME
19  
20 WebService::Validator::HTML::W3C::Fast - Access the W3Cs online HTML validator in a local persistent daemon
21  
22 =head1 SYNOPSIS
23  
24 use WebService::Validator::HTML::W3C::Fast;
25  
26 my $v = WebService::Validator::HTML::W3C::Fast->new(
27 validator_path => '/path/to/validator/check',
28 user => $username,
29 password => $password,
30 auto_launder_validator => 1,
31 );
32  
33 if ( $v->validate_markup(<<_HTML_) ) {
34 <?xml version="1.0"?>
35 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
36 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
37 <head><title></title></head><body></body></html>
38 _HTML_
39 if ( $v->is_valid ) {
40 printf ("%s is valid\n", $v->uri);
41 } else {
42 printf ("%s is not valid\n", $v->uri);
43 foreach my $error ( @{$v->errors} ) {
44 printf("%s at line %d\n", $error->msg,
45 $error->line);
46 }
47 }
48 } else {
49 printf ("Failed to validate the supplied markup: %s\n", $v->validator_error);
50 }
51  
52 =head1 DESCRIPTION
53  
54 WebService::Validator::HTML::W3C::Fast provides access a local version of
55 the W3C's Markup validator, via WebService::Validator::HTML::W3C. It loads up
56 a small HTTP::Daemon daemon, listening on a random high port on 127.0.0.1 and
57 loads the check cgi script into a mod_perl type persistent environment for speedy
58 checking of lots of documents.
59  
60 When running under taint-mode you will need to provide the auto_launder_validator
61 argument, otherwise taint will refuse to allow the module to string eval the cgi script.
62  
63 To discourage denial of service attacks, the local web server is protected via http
64 basic auth. You can specify the desired user name and password for the server, or it
65 will use srand and rand to generate a simple password.
66  
67 if validator_path is not supplied, the validator will attempt to guess at where the script
68 is, first looking at '/usr/share/w3c-markup-validator/cgi-bin/check', which is the location of
69 the cgi script used by fedora's w3c-markup-validator package. If this fails and no-one
70 supplies defaults that other operating systems use, the validator will croak().
71  
72 NOTE for debian. At the moment, debian's version of the check script depends on using the
73 open3 function for /usr/bin/onsgmls. I'm done a quick check on this and am not intending
74 to port to debian, rather, i intend to wait for debian to upgrade their source. If anyone
75 would like to fix this, i would be happy to apply supplied patches.
76  
77 The local HTTP::Daemon will occansionally check that the parent program is still present.
78 If the parent ever exits, the HTTP::Daemon will terminate as well. This is to prevent a
79 build up of HTTP::Daemons listening on high ports b/c a test script was aborted.
80  
81 =head1 SEE ALSO
82  
83 =head1 AUTHOR
84  
85 David Dick, E<lt>ddick@cpan.orgE<gt>
86  
87 =head1 COPYRIGHT AND LICENSE
88  
89 Copyright (C) 2009 by David Dick
90  
91 This library is free software; you can redistribute it and/or modify
92 it under the same terms as Perl itself, either Perl version 5.8.8 or,
93 at your option, any later version of Perl 5 you may have available.
94  
95 =cut
96  
97 1 ▶sub default_script_paths {
Subroutine default_script_paths: called CC=1 SCAR=0.0
Pod undocumented
98 1 return ('/usr/share/w3c-markup-validator/cgi-bin/check');
99 }
100  
101 1 ▶sub new {
Subroutine new: called CC=27 SCAR=62.3
Pod documented
102 1 my ($class, %params) = @_;
103 1 ▶ if ($params{validator_uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"validator_uri"}) 0 1 1
104 0 Carp::croak("The validator uri cannot be used here");
105 }
106 1 my (@default_script_paths) = default_script_paths();
107 1 ▶ my ($validator_path) = $params{validator_path} || shift @default_script_paths;
Condition33%1/3
$params{'validator_path'} || shift @default_script_paths
l!l&&r!l&&!r
010
MC/DC0%0/2
$params{'validator_path'} || shift @default_script_paths
$params{'validator_path'} shift @default_script_paths
Truth table33%1/3
A || B
A= $params{'validator_path'}B= shift @default_script_paths
ABresult
1X1
011
000
108 1 my ($handle) = Symbol::gensym();
109 1 ▶ while (not(sysopen($handle, $validator_path, Fcntl::O_RDONLY()))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $handle, $validator_path, 0) 1 0 1
110 1 ▶ if (($^E == POSIX::ENOENT()) && (not($params{validator_path})) && (@default_script_paths)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($^E == 2 and not $params{'validator_path'} and @default_script_paths) { } 0 1 1
elsif ($params{'validator_path'}) { } 0 1 1
elsif ($^E == 2 and not $params{'validator_path'}) { } 1 0 1
Condition33%2/6
$^E == 2 and not $params{"validator_path"} and @default_script_paths
!ll&&!rl&&r
010
$^E == 2 and not $params{"validator_path"}
!ll&&!rl&&r
001
MC/DC0%0/3
$^E == 2 and not $params{"validator_path"} and @default_script_paths
$^E == 2 not $params{"validator_path"} @default_script_paths
Truth table25%1/4
A and not(B) and C
A= $^E == 2B= not $params{"validator_path"}C= @default_script_paths
ABCresult
0XX0
10X0
1100
1111
111 0 $validator_path = shift @default_script_paths;
112 } elsif ($params{validator_path}) {
113 0 Carp::croak("Failed to open '$params{validator_path}' for reading:$^E")
114 ▶ } elsif (($^E == POSIX::ENOENT()) && (not($params{validator_path}))) {
Condition33%1/3
$^E == 2 and not $params{"validator_path"}
!ll&&!rl&&r
001
MC/DC0%0/2
$^E == 2 and not $params{"validator_path"}
$^E == 2 not $params{"validator_path"}
Truth table33%1/3
A and not(B)
A= $^E == 2B= not $params{"validator_path"}
ABresult
0X0
100
111
115 1 Carp::croak("Please specify the location of the w3c markup validator on the filesystem")
116 } else {
117 0 Carp::croak("Failed to open '$validator_path' for reading:$^E");
118 }
119 }
120 0 delete $params{validator_path};
121 0 my ($validator_source);
122 0 my ($result, $buffer);
123 0 ▶ while($result = read($handle, $buffer, 4096)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result = read($handle, $buffer, 4096)) 0 0 0
124 0 $validator_source .= $buffer;
125 }
126 0 ▶ unless (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
127 0 Carp::croak("Failed to read from '$validator_path':$^E");
128 }
129 0 ▶ unless (close($handle)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $handle) 0 0 0
130 0 Carp::croak("Failed to close '$validator_path':$^E");
131 }
132 0 $validator_source =~ s/exit/return/g;
133 0 ▶ if ($params{auto_launder_validator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"auto_launder_validator"}) 0 0 0
134 0 ▶ if ($validator_source =~ /^(.*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator_source =~ /^(.*)$/s) 0 0 0
135 0 ($validator_source) = ($1);
136 }
137 }
138 0 eval <<_HANDLER_;
139 sub handler {
140 my (\$connection) = \@_;
141 \$connection->send_status_line();
142 $validator_source
143 }
144 _HANDLER_
145 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
146 0 die($@);
147 }
148 0 my ($daemon) = HTTP::Daemon->new( 'LocalAddr' => '127.0.0.1', 'Blocking' => 0 );
149 0 my ($self) = WebService::Validator::HTML::W3C->new( 'validator_uri' => $daemon->url(), %params );
150 0 bless $self, $class;
151 0 my ($user, $password) = $self->_set_user_password(\%params);
152 0 my ($parent_pid) = $$;
153 0 my ($number_of_fork_attempts) = 0;
154 FORK: {
155 0 ▶ if (my $pid = fork()) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $pid = fork) { } 0 0 0
elsif (defined $pid) { } 0 0 0
elsif ($^E == 11 and $number_of_fork_attempts < $WebService::Validator::HTML::W3C::Fast::maximum_fork_attempts) { } 0 0 0
156 0 $self->{_internal_http_server_pid} = $pid;
157 0 return $self;
158 } elsif (defined $pid) {
159 0 my ($terminated) = 0;
160 0 eval {
161 0 $self = undef;
162 0 ▶ local $SIG{TERM} = sub { $terminated = 1; die("Caught a terminate signal\n"); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 ▶ local $SIG{INT} = sub { die("Caught an interrupt signal\n"); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 CHECKING: {
165 0 ▶ while(my $connection = $daemon->accept()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $connection = $daemon->accept) 0 0 0
166 0 my $request = $connection->get_request(1);
167 0 ▶ unless (check_authorisation($request, $user, $password)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (check_authorisation($request, $user, $password)) 0 0 0
168 0 die("Client did not authorise correctly\n");
169 }
170 0 local %ENV = setup_environment_hash($request);
171 0 local *STDIN = setup_stdin($connection);
172 0 local *STDOUT = *{$connection};
173 0 CGI::initialize_globals();
174 0 handler($connection);
175 0 ▶ unless (close($connection)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $connection) 0 0 0
176 0 die("Failed to close client socket:$^E");
177 }
178 }
179 0 ▶ if ($^E == POSIX::EWOULDBLOCK()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^E == 11) { } 0 0 0
180 0 ▶ if (kill(0, $parent_pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $parent_pid) 0 0 0
181 0 my ($rin) = '';
182 0 vec($rin,fileno($daemon),1) = 1;
183 0 select($rin, undef, undef, 5); # wait for 5 seconds before checking parent is still alive or for input
184 0 redo CHECKING;
185 }
186 } else {
187 0 die("Failed to accept a new connection:$^E");
188 }
189 }
190 };
191 0 ▶ if ($terminated) {
Branch: 2 branches
BranchTrueFalseTotal
if ($terminated) { } 0 0 0
elsif ($@) { } 0 0 0
192 0 CORE::exit(0);
193 } elsif ($@) {
194 0 print STDERR $@;
195 0 CORE::exit(1);
196 } else {
197 0 CORE::exit(0);
198 }
199 ▶ } elsif (($^E == POSIX::EAGAIN()) && ($number_of_fork_attempts < $maximum_fork_attempts)) {
Condition0%0/3
$^E == 11 and $number_of_fork_attempts < $WebService::Validator::HTML::W3C::Fast::maximum_fork_attempts
!ll&&!rl&&r
000
MC/DC0%0/2
$^E == 11 and $number_of_fork_attempts < $WebService::Validator::HTML::W3C::Fast::maximum_fork_attempts
$^E == 11 $number_of_fork_attempts < $WebService::Validator::HTML::W3C::Fast::maximum_fork_attempts
Truth table0%0/3
A and B
A= $^E == 11B= $number_of_fork_attempts < $WebService::Validator::HTML::W3C::Fast::maximum_fork_attempts
ABresult
0X0
100
111
200 0 $number_of_fork_attempts += 1;
201 0 sleep 5;
202 0 redo FORK;
203 } else {
204 0 die("Failed to fork:$^E");
205 }
206 }
207 }
208  
209 0 ▶sub setup_stdin {
Subroutine setup_stdin: not called CC=8 SCAR=42.8
Pod undocumented
210 0 my ($connection) = @_;
211 0 my $temp_input = File::Temp::tempfile();
212 0 ▶ unless ($temp_input) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($temp_input) 0 0 0
213 0 die("Failed to open a temporary file for writing:$^E");
214 }
215 0 my ($buffer) = $connection->read_buffer();
216 0 my ($remaining_length) = $ENV{CONTENT_LENGTH} - length($buffer);
217 0 ▶ unless ($temp_input->print($buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($temp_input->print($buffer)) 0 0 0
218 0 die("Failed to write to temporary file:$^E");
219 }
220 0 ▶ while ($remaining_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_length > 0) 0 0 0
221 0 my ($result) = $connection->sysread($buffer, 32*1024);
222 0 ▶ if (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 0 0 0
223 0 $remaining_length -= $result;
224 0 ▶ if ($result == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result == 0) { } 0 0 0
225 0 die("Client did not provide enough data");
226 } else {
227 0 ▶ unless ($temp_input->print($buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($temp_input->print($buffer)) 0 0 0
228 0 die("Failed to write to temporary file:$^E");
229 }
230 }
231 } else {
232 0 die("Client closed connection:$^E");
233 }
234 }
235 0 ▶ unless ($temp_input->seek(0, Fcntl::SEEK_SET())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($temp_input->seek(0, 0)) 0 0 0
236 0 die("Failed to seek to start of temporary file:$^E");
237 }
238 0 return *{$temp_input};
239 }
240  
241 0 ▶sub check_authorisation {
Subroutine check_authorisation: not called CC=4 SCAR=30.0
Pod undocumented
242 0 my ($request, $user, $password) = @_;
243 0 my ($authorised) = 0;
244 0 my ($auth) = $request->header('Authorization');
245 0 ▶ if ($auth =~ s/^Basic //) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth =~ s/^Basic //) 0 0 0
246 0 my ($decoded) = MIME::Base64::decode_base64($auth);
247 0 ▶ if ($decoded =~ s/^$user://) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoded =~ s/^$user://) 0 0 0
248 0 ▶ if ($decoded eq $password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoded eq $password) 0 0 0
249 0 $authorised = 1;
250 }
251 }
252 }
253 0 return $authorised;
254 }
255  
256 0 ▶sub setup_environment_hash {
Subroutine setup_environment_hash: not called CC=3 SCAR=24.8
Pod undocumented
257 0 my ($request) = @_;
258 0 my (%local_env);
259 0 $local_env{REQUEST_METHOD} = $request->method();
260 0 $local_env{REQUEST_URI} = $request->uri()->as_string();
261 0 $local_env{HTTP_USER_AGENT} = $request->header('User_Agent');
262 0 my ($length) = $request->header('Content_Length');
263 0 ▶ if ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) 0 0 0
264 0 $local_env{CONTENT_LENGTH} = $length;
265 }
266 0 my ($contentType) = $request->header('Content_Type');
267 0 ▶ if ($contentType) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contentType) 0 0 0
268 0 $local_env{CONTENT_TYPE} = $contentType;
269 }
270 0 return (%local_env);
271 }
272  
273 0 ▶sub _set_user_password {
Subroutine _set_user_password: not called CC=9 SCAR=45.0
274 0 my ($self, $params) = @_;
275 0 my ($ua) = $self->ua(); # Allow SUPER to create a ua if desired
276 0 ▶ unless ($ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 0 0 0
277 0 $ua = LWP::UserAgent->new( 'agent' => __PACKAGE__ . "/$VERSION" );
278 }
279 0 ▶ my ($headers) = $ua->default_headers() || HTTP::Headers->new();
Condition0%0/3
$ua->default_headers || 'HTTP::Headers'->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$ua->default_headers || 'HTTP::Headers'->new
$ua->default_headers 'HTTP::Headers'->new
Truth table0%0/3
A || B
A= $ua->default_headersB= 'HTTP::Headers'->new
ABresult
1X1
011
000
280 0 $headers->header('User_Agent' => $ua->agent());
281 0 my ($header_user, $header_pass) = $headers->authorization_basic();
282 0 ▶ my ($user) = $params->{user} || $header_user || 'Validator';
Condition0%0/5
$params->{'user'} || $header_user || 'Validator'
l!l
00
$params->{"user"} or $header_user
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{'user'} || $header_user || 'Validator'
$params->{"user"} $header_user
Truth table0%0/3
A or B
A= $params->{"user"}B= $header_user
ABresult
1X1
011
000
283 0 ▶ my ($password) = $params->{password} || $header_pass || '';
Condition0%0/5
$params->{'password'} || $header_pass || ''
l!l
00
$params->{"password"} or $header_pass
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{'password'} || $header_pass || ''
$params->{"password"} $header_pass
Truth table0%0/3
A or B
A= $params->{"password"}B= $header_pass
ABresult
1X1
011
000
284 0 ▶ unless ($password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 0 0 0
285 0 srand();
286 0 ▶ while(length($password) < 45) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $password < 45) 0 0 0
287 0 $password .= chr(int(rand(57)) + 65);
288 }
289 }
290 0 $headers->authorization_basic($user, $password);
291 0 $ua->default_headers($headers);
292 0 $self->ua($ua);
293 0 return ($user, $password);
294 }
295  
296 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
297 0 my ($self) = @_;
298 0 ▶ if ((exists $self->{_internal_http_server_pid}) && ($self->{_internal_http_server_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_internal_http_server_pid"} and $self->{"_internal_http_server_pid"}) 0 0 0
Condition0%0/3
exists $self->{"_internal_http_server_pid"} and $self->{"_internal_http_server_pid"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_internal_http_server_pid"} and $self->{"_internal_http_server_pid"}
exists $self->{"_internal_http_server_pid"} $self->{"_internal_http_server_pid"}
Truth table0%0/3
A and B
A= exists $self->{"_internal_http_server_pid"}B= $self->{"_internal_http_server_pid"}
ABresult
0X0
100
111
299 0 kill("TERM", $self->{_internal_http_server_pid});
300 }
301 }
302  
303 1 1;
↑ Summary