| 2 |
use strict; |
use strict; |
| 3 |
|
|
| 4 |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
| 5 |
/home/wakaba/work/manakai/lib |
/home/wakaba/work/manakai2/lib]; |
|
/home/wakaba/public_html/-temp/wiki/lib]; |
|
| 6 |
use CGI::Carp qw[fatalsToBrowser]; |
use CGI::Carp qw[fatalsToBrowser]; |
| 7 |
use Scalar::Util qw[refaddr]; |
use Scalar::Util qw[refaddr]; |
| 8 |
|
use Time::HiRes qw/time/; |
|
use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module |
|
| 9 |
|
|
| 10 |
sub htescape ($) { |
sub htescape ($) { |
| 11 |
my $s = $_[0]; |
my $s = $_[0]; |
| 19 |
return $s; |
return $s; |
| 20 |
} # htescape |
} # htescape |
| 21 |
|
|
| 22 |
my $http = SuikaWiki::Input::HTTP->new; |
use Message::CGI::HTTP; |
| 23 |
|
my $http = Message::CGI::HTTP->new; |
|
## TODO: _charset_ |
|
| 24 |
|
|
| 25 |
if ($http->meta_variable ('PATH_INFO') ne '/') { |
if ($http->get_meta_variable ('PATH_INFO') ne '/') { |
| 26 |
print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; |
print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; |
| 27 |
exit; |
exit; |
| 28 |
} |
} |
| 29 |
|
|
| 30 |
binmode STDOUT, ':utf8'; |
binmode STDOUT, ':utf8'; |
| 31 |
|
$| = 1; |
| 32 |
|
|
| 33 |
require Message::DOM::DOMImplementation; |
require Message::DOM::DOMImplementation; |
| 34 |
my $dom = Message::DOM::DOMImplementation->new; |
my $dom = Message::DOM::DOMImplementation->new; |
| 35 |
|
|
|
my $input = get_input_document ($http, $dom); |
|
|
my $inner_html_element = $http->parameter ('e'); |
|
|
|
|
| 36 |
load_text_catalog ('en'); ## TODO: conneg |
load_text_catalog ('en'); ## TODO: conneg |
| 37 |
|
|
| 38 |
my @nav; |
my @nav; |
| 45 |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
| 46 |
</head> |
</head> |
| 47 |
<body> |
<body> |
| 48 |
<h1>Web Document Conformance Checker (<em>beta</em>)</h1> |
<h1><a href="../cc-interface">Web Document Conformance Checker</a> |
| 49 |
|
(<em>beta</em>)</h1> |
| 50 |
|
]; |
| 51 |
|
|
| 52 |
|
$| = 0; |
| 53 |
|
my $input = get_input_document ($http, $dom); |
| 54 |
|
my $inner_html_element = $http->get_parameter ('e'); |
| 55 |
|
my $char_length = 0; |
| 56 |
|
my %time; |
| 57 |
|
my $time1; |
| 58 |
|
my $time2; |
| 59 |
|
|
| 60 |
|
print qq[ |
| 61 |
<div id="document-info" class="section"> |
<div id="document-info" class="section"> |
| 62 |
<dl> |
<dl> |
| 63 |
<dt>Request URI</dt> |
<dt>Request URI</dt> |
| 68 |
push @nav, ['#document-info' => 'Information']; |
push @nav, ['#document-info' => 'Information']; |
| 69 |
|
|
| 70 |
if (defined $input->{s}) { |
if (defined $input->{s}) { |
| 71 |
|
$char_length = length $input->{s}; |
| 72 |
|
|
| 73 |
print STDOUT qq[ |
print STDOUT qq[ |
| 74 |
<dt>Base URI</dt> |
<dt>Base URI</dt> |
| 79 |
<dt>Character Encoding</dt> |
<dt>Character Encoding</dt> |
| 80 |
<dd>@{[defined $input->{charset} ? '<code class="charset" lang="en">'.htescape ($input->{charset}).'</code>' : '(none)']} |
<dd>@{[defined $input->{charset} ? '<code class="charset" lang="en">'.htescape ($input->{charset}).'</code>' : '(none)']} |
| 81 |
@{[$input->{charset_overridden} ? '<em>(overridden)</em>' : '']}</dd> |
@{[$input->{charset_overridden} ? '<em>(overridden)</em>' : '']}</dd> |
| 82 |
|
<dt>Length</dt> |
| 83 |
|
<dd>$char_length byte@{[$char_length == 1 ? '' : 's']}</dd> |
| 84 |
</dl> |
</dl> |
| 85 |
</div> |
</div> |
| 86 |
]; |
]; |
| 95 |
require Whatpm::HTML; |
require Whatpm::HTML; |
| 96 |
|
|
| 97 |
$input->{charset} ||= 'ISO-8859-1'; ## TODO: for now. |
$input->{charset} ||= 'ISO-8859-1'; ## TODO: for now. |
| 98 |
|
|
| 99 |
|
$time1 = time; |
| 100 |
my $t = Encode::decode ($input->{charset}, $input->{s}); |
my $t = Encode::decode ($input->{charset}, $input->{s}); |
| 101 |
|
$time2 = time; |
| 102 |
|
$time{decode} = $time2 - $time1; |
| 103 |
|
|
| 104 |
print STDOUT qq[ |
print STDOUT qq[ |
| 105 |
<div id="parse-errors" class="section"> |
<div id="parse-errors" class="section"> |
| 124 |
}; |
}; |
| 125 |
|
|
| 126 |
$doc = $dom->create_document; |
$doc = $dom->create_document; |
| 127 |
|
$time1 = time; |
| 128 |
if (defined $inner_html_element and length $inner_html_element) { |
if (defined $inner_html_element and length $inner_html_element) { |
| 129 |
$el = $doc->create_element_ns |
$el = $doc->create_element_ns |
| 130 |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
| 132 |
} else { |
} else { |
| 133 |
Whatpm::HTML->parse_string ($t => $doc, $onerror); |
Whatpm::HTML->parse_string ($t => $doc, $onerror); |
| 134 |
} |
} |
| 135 |
|
$time2 = time; |
| 136 |
|
$time{parse} = $time2 - $time1; |
| 137 |
|
|
| 138 |
print STDOUT qq[</dl> |
print STDOUT qq[</dl> |
| 139 |
</div> |
</div> |
| 142 |
print_source_string_section (\($input->{s}), $input->{charset}); |
print_source_string_section (\($input->{s}), $input->{charset}); |
| 143 |
} elsif ({ |
} elsif ({ |
| 144 |
'text/xml' => 1, |
'text/xml' => 1, |
| 145 |
|
'application/atom+xml' => 1, |
| 146 |
|
'application/rss+xml' => 1, |
| 147 |
|
'application/svg+xml' => 1, |
| 148 |
'application/xhtml+xml' => 1, |
'application/xhtml+xml' => 1, |
| 149 |
'application/xml' => 1, |
'application/xml' => 1, |
| 150 |
}->{$input->{media_type}}) { |
}->{$input->{media_type}}) { |
| 166 |
return 1; |
return 1; |
| 167 |
}; |
}; |
| 168 |
|
|
| 169 |
|
$time1 = time; |
| 170 |
open my $fh, '<', \($input->{s}); |
open my $fh, '<', \($input->{s}); |
| 171 |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
| 172 |
($fh => $dom, $onerror, charset => $input->{charset}); |
($fh => $dom, $onerror, charset => $input->{charset}); |
| 173 |
|
$time2 = time; |
| 174 |
|
$time{parse_xml} = $time2 - $time1; |
| 175 |
|
|
| 176 |
print STDOUT qq[</dl> |
print STDOUT qq[</dl> |
| 177 |
</div> |
</div> |
| 210 |
require Whatpm::ContentChecker; |
require Whatpm::ContentChecker; |
| 211 |
my $onerror = sub { |
my $onerror = sub { |
| 212 |
my %opt = @_; |
my %opt = @_; |
| 213 |
my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}); |
my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}, $opt{node}); |
| 214 |
$type =~ tr/ /-/; |
$type =~ tr/ /-/; |
| 215 |
$type =~ s/\|/%7C/g; |
$type =~ s/\|/%7C/g; |
| 216 |
$msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; |
$msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; |
| 218 |
qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n"; |
qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n"; |
| 219 |
}; |
}; |
| 220 |
|
|
| 221 |
|
$time1 = time; |
| 222 |
my $elements; |
my $elements; |
| 223 |
if ($el) { |
if ($el) { |
| 224 |
$elements = Whatpm::ContentChecker->check_element ($el, $onerror); |
$elements = Whatpm::ContentChecker->check_element ($el, $onerror); |
| 225 |
} else { |
} else { |
| 226 |
$elements = Whatpm::ContentChecker->check_document ($doc, $onerror); |
$elements = Whatpm::ContentChecker->check_document ($doc, $onerror); |
| 227 |
} |
} |
| 228 |
|
$time2 = time; |
| 229 |
|
$time{check} = $time2 - $time1; |
| 230 |
|
|
| 231 |
print STDOUT qq[</dl> |
print STDOUT qq[</dl> |
| 232 |
</div> |
</div> |
| 235 |
if (@{$elements->{table}}) { |
if (@{$elements->{table}}) { |
| 236 |
require JSON; |
require JSON; |
| 237 |
|
|
| 238 |
|
push @nav, ['#tables' => 'Tables']; |
| 239 |
print STDOUT qq[ |
print STDOUT qq[ |
| 240 |
<div id="tables" class="section"> |
<div id="tables" class="section"> |
| 241 |
<h2>Tables</h2> |
<h2>Tables</h2> |
| 252 |
$i++; |
$i++; |
| 253 |
print STDOUT qq[<div class="section" id="table-$i"><h3>] . |
print STDOUT qq[<div class="section" id="table-$i"><h3>] . |
| 254 |
get_node_link ($table_el) . q[</h3>]; |
get_node_link ($table_el) . q[</h3>]; |
| 255 |
|
|
| 256 |
|
## TODO: Make |ContentChecker| return |form_table| result |
| 257 |
|
## so that this script don't have to run the algorithm twice. |
| 258 |
my $table = Whatpm::HTMLTable->form_table ($table_el); |
my $table = Whatpm::HTMLTable->form_table ($table_el); |
| 259 |
|
|
| 260 |
for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { |
for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { |
| 289 |
print STDOUT qq[</div>]; |
print STDOUT qq[</div>]; |
| 290 |
} |
} |
| 291 |
|
|
| 292 |
|
if (keys %{$elements->{id}}) { |
| 293 |
|
push @nav, ['#identifiers' => 'IDs']; |
| 294 |
|
print STDOUT qq[ |
| 295 |
|
<div id="identifiers" class="section"> |
| 296 |
|
<h2>Identifiers</h2> |
| 297 |
|
|
| 298 |
|
<dl> |
| 299 |
|
]; |
| 300 |
|
for my $id (sort {$a cmp $b} keys %{$elements->{id}}) { |
| 301 |
|
print STDOUT qq[<dt><code>@{[htescape $id]}</code></dt>]; |
| 302 |
|
for (@{$elements->{id}->{$id}}) { |
| 303 |
|
print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>]; |
| 304 |
|
} |
| 305 |
|
} |
| 306 |
|
print STDOUT qq[</dl></div>]; |
| 307 |
|
} |
| 308 |
|
|
| 309 |
if (keys %{$elements->{term}}) { |
if (keys %{$elements->{term}}) { |
| 310 |
|
push @nav, ['#terms' => 'Terms']; |
| 311 |
print STDOUT qq[ |
print STDOUT qq[ |
| 312 |
<div id="terms" class="section"> |
<div id="terms" class="section"> |
| 313 |
<h2>Terms</h2> |
<h2>Terms</h2> |
| 322 |
} |
} |
| 323 |
print STDOUT qq[</dl></div>]; |
print STDOUT qq[</dl></div>]; |
| 324 |
} |
} |
| 325 |
|
|
| 326 |
|
if (keys %{$elements->{class}}) { |
| 327 |
|
push @nav, ['#classes' => 'Classes']; |
| 328 |
|
print STDOUT qq[ |
| 329 |
|
<div id="classes" class="section"> |
| 330 |
|
<h2>Classes</h2> |
| 331 |
|
|
| 332 |
|
<dl> |
| 333 |
|
]; |
| 334 |
|
for my $class (sort {$a cmp $b} keys %{$elements->{class}}) { |
| 335 |
|
print STDOUT qq[<dt><code>@{[htescape $class]}</code></dt>]; |
| 336 |
|
for (@{$elements->{class}->{$class}}) { |
| 337 |
|
print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>]; |
| 338 |
|
} |
| 339 |
|
} |
| 340 |
|
print STDOUT qq[</dl></div>]; |
| 341 |
|
} |
| 342 |
} |
} |
| 343 |
|
|
| 344 |
## TODO: Show result |
## TODO: Show result |
| 367 |
</html> |
</html> |
| 368 |
]; |
]; |
| 369 |
|
|
| 370 |
|
for (qw/decode parse parse_xml check/) { |
| 371 |
|
next unless defined $time{$_}; |
| 372 |
|
open my $file, '>>', ".cc-$_.txt" or die ".cc-$_.txt: $!"; |
| 373 |
|
print $file $char_length, "\t", $time{$_}, "\n"; |
| 374 |
|
} |
| 375 |
|
|
| 376 |
exit; |
exit; |
| 377 |
|
|
| 378 |
sub print_http_header_section ($) { |
sub print_http_header_section ($) { |
| 559 |
} # load_text_catalog |
} # load_text_catalog |
| 560 |
|
|
| 561 |
sub get_text ($) { |
sub get_text ($) { |
| 562 |
my ($type, $level) = @_; |
my ($type, $level, $node) = @_; |
| 563 |
$type = $level . ':' . $type if defined $level; |
$type = $level . ':' . $type if defined $level; |
| 564 |
my @arg; |
my @arg; |
| 565 |
{ |
{ |
| 568 |
$msg =~ s{<var>\$([0-9]+)</var>}{ |
$msg =~ s{<var>\$([0-9]+)</var>}{ |
| 569 |
defined $arg[$1] ? htescape ($arg[$1]) : '(undef)'; |
defined $arg[$1] ? htescape ($arg[$1]) : '(undef)'; |
| 570 |
}ge; |
}ge; |
| 571 |
|
$msg =~ s{<var>{\@([A-Za-z0-9:_.-]+)}</var>}{ |
| 572 |
|
UNIVERSAL::can ($node, 'get_attribute_ns') |
| 573 |
|
? htescape ($node->get_attribute_ns (undef, $1)) : '' |
| 574 |
|
}ge; |
| 575 |
|
$msg =~ s{<var>{\@}</var>}{ |
| 576 |
|
UNIVERSAL::can ($node, 'value') ? htescape ($node->value) : '' |
| 577 |
|
}ge; |
| 578 |
|
$msg =~ s{<var>{local-name}</var>}{ |
| 579 |
|
UNIVERSAL::can ($node, 'manakai_local_name') |
| 580 |
|
? htescape ($node->manakai_local_name) : '' |
| 581 |
|
}ge; |
| 582 |
|
$msg =~ s{<var>{element-local-name}</var>}{ |
| 583 |
|
(UNIVERSAL::can ($node, 'owner_element') and |
| 584 |
|
$node->owner_element) |
| 585 |
|
? htescape ($node->owner_element->manakai_local_name) |
| 586 |
|
: '' |
| 587 |
|
}ge; |
| 588 |
return ($type, $Msg->{$type}->[0], $msg); |
return ($type, $Msg->{$type}->[0], $msg); |
| 589 |
} elsif ($type =~ s/:([^:]*)$//) { |
} elsif ($type =~ s/:([^:]*)$//) { |
| 590 |
unshift @arg, $1; |
unshift @arg, $1; |
| 599 |
sub get_input_document ($$) { |
sub get_input_document ($$) { |
| 600 |
my ($http, $dom) = @_; |
my ($http, $dom) = @_; |
| 601 |
|
|
| 602 |
my $request_uri = $http->parameter ('uri'); |
my $request_uri = $http->get_parameter ('uri'); |
| 603 |
my $r = {}; |
my $r = {}; |
| 604 |
if (defined $request_uri and length $request_uri) { |
if (defined $request_uri and length $request_uri) { |
| 605 |
my $uri = $dom->create_uri_reference ($request_uri); |
my $uri = $dom->create_uri_reference ($request_uri); |
| 648 |
$ua->max_size (1000_000); |
$ua->max_size (1000_000); |
| 649 |
my $req = HTTP::Request->new (GET => $request_uri); |
my $req = HTTP::Request->new (GET => $request_uri); |
| 650 |
my $res = $ua->request ($req); |
my $res = $ua->request ($req); |
| 651 |
if ($res->is_success or $http->parameter ('error-page')) { |
## TODO: 401 sets |is_success| true. |
| 652 |
|
if ($res->is_success or $http->get_parameter ('error-page')) { |
| 653 |
$r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and <base>. ## TODO: Use our own code! |
$r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and <base>. ## TODO: Use our own code! |
| 654 |
$r->{uri} = $res->request->uri; |
$r->{uri} = $res->request->uri; |
| 655 |
$r->{request_uri} = $request_uri; |
$r->{request_uri} = $request_uri; |
| 664 |
$r->{charset} =~ tr/\\//d; |
$r->{charset} =~ tr/\\//d; |
| 665 |
} |
} |
| 666 |
|
|
| 667 |
my $input_charset = $http->parameter ('charset'); |
my $input_charset = $http->get_parameter ('charset'); |
| 668 |
if (defined $input_charset and length $input_charset) { |
if (defined $input_charset and length $input_charset) { |
| 669 |
$r->{charset_overridden} |
$r->{charset_overridden} |
| 670 |
= (not defined $r->{charset} or $r->{charset} ne $input_charset); |
= (not defined $r->{charset} or $r->{charset} ne $input_charset); |
| 685 |
$r->{header_status_code} = $res->code; |
$r->{header_status_code} = $res->code; |
| 686 |
$r->{header_status_text} = $res->message; |
$r->{header_status_text} = $res->message; |
| 687 |
} else { |
} else { |
| 688 |
$r->{s} = ''.$http->parameter ('s'); |
$r->{s} = ''.$http->get_parameter ('s'); |
| 689 |
$r->{uri} = q<thismessage:/>; |
$r->{uri} = q<thismessage:/>; |
| 690 |
$r->{request_uri} = q<thismessage:/>; |
$r->{request_uri} = q<thismessage:/>; |
| 691 |
$r->{base_uri} = q<thismessage:/>; |
$r->{base_uri} = q<thismessage:/>; |
| 692 |
$r->{charset} = ''.$http->parameter ('_charset_'); |
$r->{charset} = ''.$http->get_parameter ('_charset_'); |
| 693 |
$r->{charset} =~ s/\s+//g; |
$r->{charset} =~ s/\s+//g; |
| 694 |
$r->{charset} = 'utf-8' if $r->{charset} eq ''; |
$r->{charset} = 'utf-8' if $r->{charset} eq ''; |
| 695 |
$r->{header_field} = []; |
$r->{header_field} = []; |
| 696 |
} |
} |
| 697 |
|
|
| 698 |
my $input_format = $http->parameter ('i'); |
my $input_format = $http->get_parameter ('i'); |
| 699 |
if (defined $input_format and length $input_format) { |
if (defined $input_format and length $input_format) { |
| 700 |
$r->{media_type_overridden} |
$r->{media_type_overridden} |
| 701 |
= (not defined $r->{media_type} or $input_format ne $r->{media_type}); |
= (not defined $r->{media_type} or $input_format ne $r->{media_type}); |