Parent Directory
|
Revision Log
Scoring bug fixes
| 1 | wakaba | 1.1 | #!/usr/bin/perl |
| 2 | use strict; | ||
| 3 | |||
| 4 | use lib qw[/home/httpd/html/www/markup/html/whatpm | ||
| 5 | wakaba | 1.16 | /home/wakaba/work/manakai2/lib]; |
| 6 | wakaba | 1.1 | use CGI::Carp qw[fatalsToBrowser]; |
| 7 | wakaba | 1.2 | use Scalar::Util qw[refaddr]; |
| 8 | wakaba | 1.16 | use Time::HiRes qw/time/; |
| 9 | wakaba | 1.1 | |
| 10 | wakaba | 1.2 | sub htescape ($) { |
| 11 | my $s = $_[0]; | ||
| 12 | $s =~ s/&/&/g; | ||
| 13 | $s =~ s/</</g; | ||
| 14 | $s =~ s/>/>/g; | ||
| 15 | $s =~ s/"/"/g; | ||
| 16 | wakaba | 1.12 | $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{ |
| 17 | sprintf '<var>U+%04X</var>', ord $1; | ||
| 18 | }ge; | ||
| 19 | wakaba | 1.2 | return $s; |
| 20 | } # htescape | ||
| 21 | |||
| 22 | wakaba | 1.16 | use Message::CGI::HTTP; |
| 23 | my $http = Message::CGI::HTTP->new; | ||
| 24 | wakaba | 1.1 | |
| 25 | wakaba | 1.16 | if ($http->get_meta_variable ('PATH_INFO') ne '/') { |
| 26 | wakaba | 1.8 | print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; |
| 27 | exit; | ||
| 28 | } | ||
| 29 | |||
| 30 | wakaba | 1.12 | binmode STDOUT, ':utf8'; |
| 31 | wakaba | 1.14 | $| = 1; |
| 32 | wakaba | 1.12 | |
| 33 | wakaba | 1.9 | require Message::DOM::DOMImplementation; |
| 34 | my $dom = Message::DOM::DOMImplementation->new; | ||
| 35 | |||
| 36 | wakaba | 1.7 | load_text_catalog ('en'); ## TODO: conneg |
| 37 | |||
| 38 | wakaba | 1.3 | my @nav; |
| 39 | wakaba | 1.2 | print STDOUT qq[Content-Type: text/html; charset=utf-8 |
| 40 | |||
| 41 | <!DOCTYPE html> | ||
| 42 | <html lang="en"> | ||
| 43 | <head> | ||
| 44 | <title>Web Document Conformance Checker (BETA)</title> | ||
| 45 | wakaba | 1.3 | <link rel="stylesheet" href="../cc-style.css" type="text/css"> |
| 46 | wakaba | 1.2 | </head> |
| 47 | <body> | ||
| 48 | wakaba | 1.13 | <h1><a href="../cc-interface">Web Document Conformance Checker</a> |
| 49 | (<em>beta</em>)</h1> | ||
| 50 | wakaba | 1.14 | ]; |
| 51 | wakaba | 1.2 | |
| 52 | wakaba | 1.14 | $| = 0; |
| 53 | my $input = get_input_document ($http, $dom); | ||
| 54 | wakaba | 1.16 | my $inner_html_element = $http->get_parameter ('e'); |
| 55 | my $char_length = 0; | ||
| 56 | my %time; | ||
| 57 | wakaba | 1.14 | |
| 58 | print qq[ | ||
| 59 | wakaba | 1.4 | <div id="document-info" class="section"> |
| 60 | wakaba | 1.2 | <dl> |
| 61 | wakaba | 1.9 | <dt>Request URI</dt> |
| 62 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{request_uri}]}">@{[htescape $input->{request_uri}]}</a>></code></dd> | ||
| 63 | wakaba | 1.2 | <dt>Document URI</dt> |
| 64 | wakaba | 1.9 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{uri}]}">@{[htescape $input->{uri}]}</a>></code></dd> |
| 65 | wakaba | 1.2 | ]; # no </dl> yet |
| 66 | wakaba | 1.3 | push @nav, ['#document-info' => 'Information']; |
| 67 | wakaba | 1.1 | |
| 68 | wakaba | 1.9 | if (defined $input->{s}) { |
| 69 | wakaba | 1.16 | $char_length = length $input->{s}; |
| 70 | wakaba | 1.9 | |
| 71 | print STDOUT qq[ | ||
| 72 | <dt>Base URI</dt> | ||
| 73 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{base_uri}]}">@{[htescape $input->{base_uri}]}</a>></code></dd> | ||
| 74 | <dt>Internet Media Type</dt> | ||
| 75 | <dd><code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> | ||
| 76 | @{[$input->{media_type_overridden} ? '<em>(overridden)</em>' : '']}</dd> | ||
| 77 | <dt>Character Encoding</dt> | ||
| 78 | <dd>@{[defined $input->{charset} ? '<code class="charset" lang="en">'.htescape ($input->{charset}).'</code>' : '(none)']} | ||
| 79 | @{[$input->{charset_overridden} ? '<em>(overridden)</em>' : '']}</dd> | ||
| 80 | wakaba | 1.16 | <dt>Length</dt> |
| 81 | <dd>$char_length byte@{[$char_length == 1 ? '' : 's']}</dd> | ||
| 82 | wakaba | 1.9 | </dl> |
| 83 | </div> | ||
| 84 | ]; | ||
| 85 | |||
| 86 | wakaba | 1.20 | my $result = {conforming_min => 1, conforming_max => 1}; |
| 87 | wakaba | 1.19 | print_http_header_section ($input, $result); |
| 88 | wakaba | 1.9 | |
| 89 | wakaba | 1.1 | my $doc; |
| 90 | my $el; | ||
| 91 | |||
| 92 | wakaba | 1.9 | if ($input->{media_type} eq 'text/html') { |
| 93 | wakaba | 1.19 | ($doc, $el) = print_syntax_error_html_section ($input, $result); |
| 94 | wakaba | 1.9 | print_source_string_section (\($input->{s}), $input->{charset}); |
| 95 | } elsif ({ | ||
| 96 | 'text/xml' => 1, | ||
| 97 | wakaba | 1.16 | 'application/atom+xml' => 1, |
| 98 | 'application/rss+xml' => 1, | ||
| 99 | 'application/svg+xml' => 1, | ||
| 100 | wakaba | 1.9 | 'application/xhtml+xml' => 1, |
| 101 | 'application/xml' => 1, | ||
| 102 | }->{$input->{media_type}}) { | ||
| 103 | wakaba | 1.19 | ($doc, $el) = print_syntax_error_xml_section ($input, $result); |
| 104 | wakaba | 1.9 | print_source_string_section (\($input->{s}), $doc->input_encoding); |
| 105 | wakaba | 1.2 | } else { |
| 106 | wakaba | 1.9 | ## TODO: Change HTTP status code?? |
| 107 | wakaba | 1.18 | print_result_unknown_type_section ($input); |
| 108 | wakaba | 1.2 | } |
| 109 | |||
| 110 | if (defined $doc or defined $el) { | ||
| 111 | wakaba | 1.18 | print_structure_dump_section ($doc, $el); |
| 112 | wakaba | 1.19 | my $elements = print_structure_error_section ($doc, $el, $result); |
| 113 | wakaba | 1.18 | print_table_section ($elements->{table}) if @{$elements->{table}}; |
| 114 | print_id_section ($elements->{id}) if keys %{$elements->{id}}; | ||
| 115 | print_term_section ($elements->{term}) if keys %{$elements->{term}}; | ||
| 116 | print_class_section ($elements->{class}) if keys %{$elements->{class}}; | ||
| 117 | wakaba | 1.1 | } |
| 118 | |||
| 119 | wakaba | 1.19 | print_result_section ($result); |
| 120 | wakaba | 1.9 | } else { |
| 121 | wakaba | 1.18 | print STDOUT qq[</dl></div>]; |
| 122 | print_result_input_error_section ($input); | ||
| 123 | wakaba | 1.9 | } |
| 124 | wakaba | 1.3 | |
| 125 | wakaba | 1.2 | print STDOUT qq[ |
| 126 | wakaba | 1.3 | <ul class="navigation" id="nav-items"> |
| 127 | ]; | ||
| 128 | for (@nav) { | ||
| 129 | print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>]; | ||
| 130 | } | ||
| 131 | print STDOUT qq[ | ||
| 132 | </ul> | ||
| 133 | wakaba | 1.2 | </body> |
| 134 | </html> | ||
| 135 | ]; | ||
| 136 | wakaba | 1.1 | |
| 137 | wakaba | 1.16 | for (qw/decode parse parse_xml check/) { |
| 138 | next unless defined $time{$_}; | ||
| 139 | open my $file, '>>', ".cc-$_.txt" or die ".cc-$_.txt: $!"; | ||
| 140 | print $file $char_length, "\t", $time{$_}, "\n"; | ||
| 141 | } | ||
| 142 | |||
| 143 | wakaba | 1.1 | exit; |
| 144 | |||
| 145 | wakaba | 1.19 | sub add_error ($$$) { |
| 146 | my ($layer, $err, $result) = @_; | ||
| 147 | if (defined $err->{level}) { | ||
| 148 | if ($err->{level} eq 's') { | ||
| 149 | $result->{$layer}->{should}++; | ||
| 150 | $result->{$layer}->{score_min} -= 2; | ||
| 151 | $result->{conforming_min} = 0; | ||
| 152 | } elsif ($err->{level} eq 'w' or $err->{level} eq 'g') { | ||
| 153 | $result->{$layer}->{warning}++; | ||
| 154 | } elsif ($err->{level} eq 'unsupported') { | ||
| 155 | $result->{$layer}->{unsupported}++; | ||
| 156 | $result->{unsupported} = 1; | ||
| 157 | } else { | ||
| 158 | $result->{$layer}->{must}++; | ||
| 159 | $result->{$layer}->{score_max} -= 2; | ||
| 160 | $result->{$layer}->{score_min} -= 2; | ||
| 161 | $result->{conforming_min} = 0; | ||
| 162 | $result->{conforming_max} = 0; | ||
| 163 | } | ||
| 164 | } else { | ||
| 165 | $result->{$layer}->{must}++; | ||
| 166 | $result->{$layer}->{score_max} -= 2; | ||
| 167 | $result->{$layer}->{score_min} -= 2; | ||
| 168 | $result->{conforming_min} = 0; | ||
| 169 | $result->{conforming_max} = 0; | ||
| 170 | } | ||
| 171 | } # add_error | ||
| 172 | |||
| 173 | sub print_http_header_section ($$) { | ||
| 174 | my ($input, $result) = @_; | ||
| 175 | wakaba | 1.9 | return unless defined $input->{header_status_code} or |
| 176 | defined $input->{header_status_text} or | ||
| 177 | @{$input->{header_field}}; | ||
| 178 | |||
| 179 | push @nav, ['#source-header' => 'HTTP Header']; | ||
| 180 | print STDOUT qq[<div id="source-header" class="section"> | ||
| 181 | <h2>HTTP Header</h2> | ||
| 182 | |||
| 183 | <p><strong>Note</strong>: Due to the limitation of the | ||
| 184 | network library in use, the content of this section might | ||
| 185 | not be the real header.</p> | ||
| 186 | |||
| 187 | <table><tbody> | ||
| 188 | ]; | ||
| 189 | |||
| 190 | if (defined $input->{header_status_code}) { | ||
| 191 | print STDOUT qq[<tr><th scope="row">Status code</th>]; | ||
| 192 | print STDOUT qq[<td><code>@{[htescape ($input->{header_status_code})]}</code></td></tr>]; | ||
| 193 | } | ||
| 194 | if (defined $input->{header_status_text}) { | ||
| 195 | print STDOUT qq[<tr><th scope="row">Status text</th>]; | ||
| 196 | print STDOUT qq[<td><code>@{[htescape ($input->{header_status_text})]}</code></td></tr>]; | ||
| 197 | } | ||
| 198 | |||
| 199 | for (@{$input->{header_field}}) { | ||
| 200 | print STDOUT qq[<tr><th scope="row"><code>@{[htescape ($_->[0])]}</code></th>]; | ||
| 201 | print STDOUT qq[<td><code>@{[htescape ($_->[1])]}</code></td></tr>]; | ||
| 202 | } | ||
| 203 | |||
| 204 | print STDOUT qq[</tbody></table></div>]; | ||
| 205 | } # print_http_header_section | ||
| 206 | |||
| 207 | wakaba | 1.19 | sub print_syntax_error_html_section ($$) { |
| 208 | my ($input, $result) = @_; | ||
| 209 | wakaba | 1.18 | |
| 210 | require Encode; | ||
| 211 | require Whatpm::HTML; | ||
| 212 | |||
| 213 | $input->{charset} ||= 'ISO-8859-1'; ## TODO: for now. | ||
| 214 | |||
| 215 | my $time1 = time; | ||
| 216 | my $t = Encode::decode ($input->{charset}, $input->{s}); | ||
| 217 | $time{decode} = time - $time1; | ||
| 218 | |||
| 219 | print STDOUT qq[ | ||
| 220 | <div id="parse-errors" class="section"> | ||
| 221 | <h2>Parse Errors</h2> | ||
| 222 | |||
| 223 | <dl>]; | ||
| 224 | push @nav, ['#parse-errors' => 'Parse Error']; | ||
| 225 | |||
| 226 | my $onerror = sub { | ||
| 227 | my (%opt) = @_; | ||
| 228 | my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}); | ||
| 229 | if ($opt{column} > 0) { | ||
| 230 | print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n]; | ||
| 231 | } else { | ||
| 232 | $opt{line} = $opt{line} - 1 || 1; | ||
| 233 | print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n]; | ||
| 234 | } | ||
| 235 | $type =~ tr/ /-/; | ||
| 236 | $type =~ s/\|/%7C/g; | ||
| 237 | $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; | ||
| 238 | print STDOUT qq[<dd class="$cls">$msg</dd>\n]; | ||
| 239 | wakaba | 1.19 | |
| 240 | add_error ('syntax', \%opt => $result); | ||
| 241 | wakaba | 1.18 | }; |
| 242 | |||
| 243 | my $doc = $dom->create_document; | ||
| 244 | my $el; | ||
| 245 | $time1 = time; | ||
| 246 | if (defined $inner_html_element and length $inner_html_element) { | ||
| 247 | $el = $doc->create_element_ns | ||
| 248 | ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); | ||
| 249 | Whatpm::HTML->set_inner_html ($el, $t, $onerror); | ||
| 250 | } else { | ||
| 251 | Whatpm::HTML->parse_string ($t => $doc, $onerror); | ||
| 252 | } | ||
| 253 | $time{parse} = time - $time1; | ||
| 254 | |||
| 255 | print STDOUT qq[</dl></div>]; | ||
| 256 | |||
| 257 | return ($doc, $el); | ||
| 258 | } # print_syntax_error_html_section | ||
| 259 | |||
| 260 | wakaba | 1.19 | sub print_syntax_error_xml_section ($$) { |
| 261 | my ($input, $result) = @_; | ||
| 262 | wakaba | 1.18 | |
| 263 | require Message::DOM::XMLParserTemp; | ||
| 264 | |||
| 265 | print STDOUT qq[ | ||
| 266 | <div id="parse-errors" class="section"> | ||
| 267 | <h2>Parse Errors</h2> | ||
| 268 |