Parent Directory
|
Revision Log
++ ChangeLog 17 Mar 2008 13:52:22 -0000 * cc.cgi (get_error_label): No-parent case was not considered. 2008-03-17 Wakaba <wakaba@suika.fam.cx>
| 1 | wakaba | 1.1 | #!/usr/bin/perl |
| 2 | use strict; | ||
| 3 | wakaba | 1.23 | use utf8; |
| 4 | wakaba | 1.1 | |
| 5 | use lib qw[/home/httpd/html/www/markup/html/whatpm | ||
| 6 | wakaba | 1.16 | /home/wakaba/work/manakai2/lib]; |
| 7 | wakaba | 1.1 | use CGI::Carp qw[fatalsToBrowser]; |
| 8 | wakaba | 1.2 | use Scalar::Util qw[refaddr]; |
| 9 | wakaba | 1.16 | use Time::HiRes qw/time/; |
| 10 | wakaba | 1.1 | |
| 11 | wakaba | 1.2 | sub htescape ($) { |
| 12 | my $s = $_[0]; | ||
| 13 | $s =~ s/&/&/g; | ||
| 14 | $s =~ s/</</g; | ||
| 15 | $s =~ s/>/>/g; | ||
| 16 | $s =~ s/"/"/g; | ||
| 17 | wakaba | 1.12 | $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{ |
| 18 | sprintf '<var>U+%04X</var>', ord $1; | ||
| 19 | }ge; | ||
| 20 | wakaba | 1.2 | return $s; |
| 21 | } # htescape | ||
| 22 | |||
| 23 | wakaba | 1.35 | my @nav; |
| 24 | my %time; | ||
| 25 | require Message::DOM::DOMImplementation; | ||
| 26 | my $dom = Message::DOM::DOMImplementation->new; | ||
| 27 | { | ||
| 28 | wakaba | 1.16 | use Message::CGI::HTTP; |
| 29 | my $http = Message::CGI::HTTP->new; | ||
| 30 | wakaba | 1.1 | |
| 31 | wakaba | 1.16 | if ($http->get_meta_variable ('PATH_INFO') ne '/') { |
| 32 | wakaba | 1.8 | print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; |
| 33 | exit; | ||
| 34 | } | ||
| 35 | |||
| 36 | wakaba | 1.12 | binmode STDOUT, ':utf8'; |
| 37 | wakaba | 1.14 | $| = 1; |
| 38 | wakaba | 1.12 | |
| 39 | wakaba | 1.7 | load_text_catalog ('en'); ## TODO: conneg |
| 40 | |||
| 41 | wakaba | 1.2 | print STDOUT qq[Content-Type: text/html; charset=utf-8 |
| 42 | |||
| 43 | <!DOCTYPE html> | ||
| 44 | <html lang="en"> | ||
| 45 | <head> | ||
| 46 | <title>Web Document Conformance Checker (BETA)</title> | ||
| 47 | wakaba | 1.3 | <link rel="stylesheet" href="../cc-style.css" type="text/css"> |
| 48 | wakaba | 1.2 | </head> |
| 49 | <body> | ||
| 50 | wakaba | 1.13 | <h1><a href="../cc-interface">Web Document Conformance Checker</a> |
| 51 | (<em>beta</em>)</h1> | ||
| 52 | wakaba | 1.14 | ]; |
| 53 | wakaba | 1.2 | |
| 54 | wakaba | 1.14 | $| = 0; |
| 55 | my $input = get_input_document ($http, $dom); | ||
| 56 | wakaba | 1.16 | my $char_length = 0; |
| 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.25 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{uri}]}" id=anchor-document-uri>@{[htescape $input->{uri}]}</a>></code> |
| 65 | <script> | ||
| 66 | document.title = '<' | ||
| 67 | + document.getElementById ('anchor-document-uri').href + '> \\u2014 ' | ||
| 68 | + document.title; | ||
| 69 | </script></dd> | ||
| 70 | wakaba | 1.2 | ]; # no </dl> yet |
| 71 | wakaba | 1.3 | push @nav, ['#document-info' => 'Information']; |
| 72 | wakaba | 1.1 | |
| 73 | wakaba | 1.9 | if (defined $input->{s}) { |
| 74 | wakaba | 1.16 | $char_length = length $input->{s}; |
| 75 | wakaba | 1.9 | |
| 76 | print STDOUT qq[ | ||
| 77 | <dt>Base URI</dt> | ||
| 78 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{base_uri}]}">@{[htescape $input->{base_uri}]}</a>></code></dd> | ||
| 79 | <dt>Internet Media Type</dt> | ||
| 80 | <dd><code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> | ||
| 81 | wakaba | 1.25 | @{[$input->{media_type_overridden} ? '<em>(overridden)</em>' : defined $input->{official_type} ? $input->{media_type} eq $input->{official_type} ? '' : '<em>(sniffed; official type is: <code class=MIME lang=en>'.htescape ($input->{official_type}).'</code>)' : '<em>(sniffed)</em>']}</dd> |
| 82 | wakaba | 1.9 | <dt>Character Encoding</dt> |
| 83 | <dd>@{[defined $input->{charset} ? '<code class="charset" lang="en">'.htescape ($input->{charset}).'</code>' : '(none)']} | ||
| 84 | @{[$input->{charset_overridden} ? '<em>(overridden)</em>' : '']}</dd> | ||
| 85 | wakaba | 1.16 | <dt>Length</dt> |
| 86 | <dd>$char_length byte@{[$char_length == 1 ? '' : 's']}</dd> | ||
| 87 | wakaba | 1.9 | </dl> |
| 88 | </div> | ||
| 89 | wakaba | 1.39 | |
| 90 | <script src="../cc-script.js"></script> | ||
| 91 | wakaba | 1.9 | ]; |
| 92 | |||
| 93 | wakaba | 1.35 | $input->{id_prefix} = ''; |
| 94 | #$input->{nested} = 0; | ||
| 95 | wakaba | 1.20 | my $result = {conforming_min => 1, conforming_max => 1}; |
| 96 | wakaba | 1.31 | check_and_print ($input => $result); |
| 97 | wakaba | 1.19 | print_result_section ($result); |
| 98 | wakaba | 1.9 | } else { |
| 99 | wakaba | 1.18 | print STDOUT qq[</dl></div>]; |
| 100 | print_result_input_error_section ($input); | ||
| 101 | wakaba | 1.9 | } |
| 102 | wakaba | 1.3 | |
| 103 | wakaba | 1.2 | print STDOUT qq[ |
| 104 | wakaba | 1.3 | <ul class="navigation" id="nav-items"> |
| 105 | ]; | ||
| 106 | for (@nav) { | ||
| 107 | print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>]; | ||
| 108 | } | ||
| 109 | print STDOUT qq[ | ||
| 110 | </ul> | ||
| 111 | wakaba | 1.2 | </body> |
| 112 | </html> | ||
| 113 | ]; | ||
| 114 | wakaba | 1.1 | |
| 115 | wakaba | 1.24 | for (qw/decode parse parse_html parse_xml parse_manifest |
| 116 | check check_manifest/) { | ||
| 117 | wakaba | 1.16 | next unless defined $time{$_}; |
| 118 | open my $file, '>>', ".cc-$_.txt" or die ".cc-$_.txt: $!"; | ||
| 119 | print $file $char_length, "\t", $time{$_}, "\n"; | ||
| 120 | } | ||
| 121 | |||
| 122 | wakaba | 1.1 | exit; |
| 123 | wakaba | 1.35 | } |
| 124 | wakaba | 1.1 | |
| 125 | wakaba | 1.19 | sub add_error ($$$) { |
| 126 | my ($layer, $err, $result) = @_; | ||
| 127 | if (defined $err->{level}) { | ||
| 128 | if ($err->{level} eq 's') { | ||
| 129 | $result->{$layer}->{should}++; | ||
| 130 | $result->{$layer}->{score_min} -= 2; | ||
| 131 | $result->{conforming_min} = 0; | ||
| 132 | } elsif ($err->{level} eq 'w' or $err->{level} eq 'g') { | ||
| 133 | $result->{$layer}->{warning}++; | ||
| 134 | wakaba | 1.30 | } elsif ($err->{level} eq 'u' or $err->{level} eq 'unsupported') { |
| 135 | wakaba | 1.19 | $result->{$layer}->{unsupported}++; |
| 136 | $result->{unsupported} = 1; | ||
| 137 | wakaba | 1.37 | } elsif ($err->{level} eq 'i') { |
| 138 | # | ||
| 139 | wakaba | 1.19 | } else { |
| 140 | $result->{$layer}->{must}++; | ||
| 141 | $result->{$layer}->{score_max} -= 2; | ||
| 142 | $result->{$layer}->{score_min} -= 2; | ||
| 143 | $result->{conforming_min} = 0; | ||
| 144 | $result->{conforming_max} = 0; | ||
| 145 | } | ||
| 146 | } else { | ||
| 147 | $result->{$layer}->{must}++; | ||
| 148 | $result->{$layer}->{score_max} -= 2; | ||
| 149 | $result->{$layer}->{score_min} -= 2; | ||
| 150 | $result->{conforming_min} = 0; | ||
| 151 | $result->{conforming_max} = 0; | ||
| 152 | } | ||
| 153 | } # add_error | ||
| 154 | |||
| 155 | wakaba | 1.31 | sub check_and_print ($$) { |
| 156 | my ($input, $result) = @_; | ||
| 157 | |||
| 158 | print_http_header_section ($input, $result); | ||
| 159 | |||
| 160 | my $doc; | ||
| 161 | my $el; | ||
| 162 | wakaba | 1.35 | my $cssom; |
| 163 | wakaba | 1.31 | my $manifest; |
| 164 | wakaba | 1.34 | my @subdoc; |
| 165 | wakaba | 1.31 | |
| 166 | if ($input->{media_type} eq 'text/html') { | ||
| 167 | ($doc, $el) = print_syntax_error_html_section ($input, $result); | ||
| 168 | print_source_string_section | ||
| 169 | wakaba | 1.35 | ($input, |
| 170 | \($input->{s}), | ||
| 171 | $input->{charset} || $doc->input_encoding); | ||
| 172 | wakaba | 1.31 | } elsif ({ |
| 173 | 'text/xml' => 1, | ||
| 174 | 'application/atom+xml' => 1, | ||
| 175 | 'application/rss+xml' => 1, | ||
| 176 | 'application/svg+xml' => 1, | ||
| 177 | 'application/xhtml+xml' => 1, | ||
| 178 | 'application/xml' => 1, | ||
| 179 | }->{$input->{media_type}}) { | ||
| 180 | ($doc, $el) = print_syntax_error_xml_section ($input, $result); | ||
| 181 | wakaba | 1.35 | print_source_string_section ($input, |
| 182 | \($input->{s}), | ||
| 183 | $doc->input_encoding); | ||
| 184 | } elsif ($input->{media_type} eq 'text/css') { | ||
| 185 | $cssom = print_syntax_error_css_section ($input, $result); | ||
| 186 | print_source_string_section | ||
| 187 | ($input, \($input->{s}), | ||
| 188 | $cssom->manakai_input_encoding); | ||
| 189 | wakaba | 1.31 | } elsif ($input->{media_type} eq 'text/cache-manifest') { |
| 190 | ## TODO: MUST be text/cache-manifest | ||
| 191 | $manifest = print_syntax_error_manifest_section ($input, $result); | ||
| 192 | wakaba | 1.35 | print_source_string_section ($input, \($input->{s}), |
| 193 | 'utf-8'); | ||
| 194 | wakaba | 1.31 | } else { |
| 195 | ## TODO: Change HTTP status code?? | ||
| 196 | print_result_unknown_type_section ($input, $result); | ||
| 197 | } | ||
| 198 | |||
| 199 | if (defined $doc or defined $el) { | ||
| 200 | wakaba | 1.34 | $doc->document_uri ($input->{uri}); |
| 201 | $doc->manakai_entity_base_uri ($input->{base_uri}); | ||
| 202 | wakaba | 1.32 | print_structure_dump_dom_section ($input, $doc, $el); |
| 203 | my $elements = print_structure_error_dom_section | ||
| 204 | wakaba | 1.34 | ($input, $doc, $el, $result, sub { |
| 205 | push @subdoc, shift; | ||
| 206 | }); | ||
| 207 | wakaba | 1.32 | print_table_section ($input, $elements->{table}) if @{$elements->{table}}; |
| 208 | wakaba | 1.33 | print_listing_section ({ |
| 209 | id => 'identifiers', label => 'IDs', heading => 'Identifiers', | ||
| 210 | }, $input, $elements->{id}) if keys %{$elements->{id}}; | ||
| 211 | print_listing_section ({ | ||
| 212 | id => 'terms', label => 'Terms', heading => 'Terms', | ||
| 213 | }, $input, $elements->{term}) if keys %{$elements->{term}}; | ||
| 214 | print_listing_section ({ | ||
| 215 | id => 'classes', label => 'Classes', heading => 'Classes', | ||
| 216 | }, $input, $elements->{class}) if keys %{$elements->{class}}; | ||
| 217 | wakaba | 1.35 | } elsif (defined $cssom) { |
| 218 | print_structure_dump_cssom_section ($input, $cssom); | ||
| 219 | ## TODO: CSSOM validation | ||
| 220 | wakaba | 1.36 | add_error ('structure', {level => 'u'} => $result); |
| 221 | wakaba | 1.31 | } elsif (defined $manifest) { |
| 222 | wakaba | 1.32 | print_structure_dump_manifest_section ($input, $manifest); |
| 223 | print_structure_error_manifest_section ($input, $manifest, $result); | ||
| 224 | wakaba | 1.31 | } |
| 225 | wakaba | 1.34 | |
| 226 | my $id_prefix = 0; | ||
| 227 | for my $subinput (@subdoc) { | ||
| 228 | $subinput->{id_prefix} = 'subdoc-' . ++$id_prefix; | ||
| 229 | $subinput->{nested} = 1; | ||
| 230 | $subinput->{base_uri} = $subinput->{container_node}->base_uri | ||
| 231 | unless defined $subinput->{base_uri}; | ||
| 232 | my $ebaseuri = htescape ($subinput->{base_uri}); | ||
| 233 | push @nav, ['#' . $subinput->{id_prefix} => 'Sub #' . $id_prefix]; | ||
| 234 | print STDOUT qq[<div id="$subinput->{id_prefix}" class=section> | ||
| 235 | <h2>Subdocument #$id_prefix</h2> | ||
| 236 | |||
| 237 | <dl> | ||
| 238 | <dt>Internet Media Type</dt> | ||
| 239 | <dd><code class="MIME" lang="en">@{[htescape $subinput->{media_type}]}</code> | ||
| 240 | <dt>Container Node</dt> | ||
| 241 | <dd>@{[get_node_link ($input, $subinput->{container_node})]}</dd> | ||
| 242 | <dt>Base <abbr title="Uniform Resource Identifiers">URI</abbr></dt> | ||
| 243 | <dd><code class=URI><<a href="$ebaseuri">$ebaseuri</a>></code></dd> | ||
| 244 | </dl>]; | ||
| 245 | |||
| 246 | wakaba | 1.35 | $subinput->{id_prefix} .= '-'; |
| 247 | wakaba | 1.34 | check_and_print ($subinput => $result); |
| 248 | |||
| 249 | print STDOUT qq[</div>]; | ||
| 250 | } | ||
| 251 | wakaba | 1.31 | } # check_and_print |
| 252 | |||
| 253 | wakaba | 1.19 | sub print_http_header_section ($$) { |
| 254 | my ($input, $result) = @_; | ||
| 255 | wakaba | 1.9 | return unless defined $input->{header_status_code} or |
| 256 | defined $input->{header_status_text} or | ||
| 257 | wakaba | 1.34 | @{$input->{header_field} or []}; |
| 258 | wakaba | 1.9 | |
| 259 | wakaba | 1.32 | push @nav, ['#source-header' => 'HTTP Header'] unless $input->{nested}; |
| 260 | print STDOUT qq[<div id="$input->{id_prefix}source-header" class="section"> | ||
| 261 | wakaba | 1.9 | <h2>HTTP Header</h2> |
| 262 | |||
| 263 | <p><strong>Note</strong>: Due to the limitation of the | ||
| 264 | network library in use, the content of this section might | ||
| 265 | not be the real header.</p> | ||
| 266 |