| 15 | $s =~ s/</</g; | $s =~ s/</</g; | 
| 16 | $s =~ s/>/>/g; | $s =~ s/>/>/g; | 
| 17 | $s =~ s/"/"/g; | $s =~ s/"/"/g; | 
| 18 | $s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf '<var>U+%04X</var>', ord $1!ge; | $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{ | 
| 19 |  | sprintf '<var>U+%04X</var>', ord $1; | 
| 20 |  | }ge; | 
| 21 | return $s; | return $s; | 
| 22 | } # htescape | } # htescape | 
| 23 |  |  | 
| 25 |  |  | 
| 26 | ## TODO: _charset_ | ## TODO: _charset_ | 
| 27 |  |  | 
| 28 | my $input_format = $http->parameter ('i') || 'text/html'; | if ($http->meta_variable ('PATH_INFO') ne '/') { | 
| 29 | my $inner_html_element = $http->parameter ('e'); | print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; | 
|  | my $input_uri = 'thismessage:/'; |  | 
|  |  |  | 
|  | my $s = $http->parameter ('s'); |  | 
|  | if (length $s > 1000_000) { |  | 
|  | print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long"; |  | 
| 30 | exit; | exit; | 
| 31 | } | } | 
| 32 |  |  | 
| 33 |  | binmode STDOUT, ':utf8'; | 
| 34 |  | $| = 1; | 
| 35 |  |  | 
| 36 |  | require Message::DOM::DOMImplementation; | 
| 37 |  | my $dom = Message::DOM::DOMImplementation->new; | 
| 38 |  |  | 
| 39 |  | load_text_catalog ('en'); ## TODO: conneg | 
| 40 |  |  | 
| 41 | my @nav; | my @nav; | 
| 42 | print STDOUT qq[Content-Type: text/html; charset=utf-8 | print STDOUT qq[Content-Type: text/html; charset=utf-8 | 
| 43 |  |  | 
| 48 | <link rel="stylesheet" href="../cc-style.css" type="text/css"> | <link rel="stylesheet" href="../cc-style.css" type="text/css"> | 
| 49 | </head> | </head> | 
| 50 | <body> | <body> | 
| 51 | <h1>Web Document Conformance Checker (<em>beta</em>)</h1> | <h1><a href="../cc-interface">Web Document Conformance Checker</a> | 
| 52 |  | (<em>beta</em>)</h1> | 
| 53 |  | ]; | 
| 54 |  |  | 
| 55 |  | $| = 0; | 
| 56 |  | my $input = get_input_document ($http, $dom); | 
| 57 |  | my $inner_html_element = $http->parameter ('e'); | 
| 58 |  |  | 
| 59 | <div id="document-info" section="section"> | print qq[ | 
| 60 |  | <div id="document-info" class="section"> | 
| 61 | <dl> | <dl> | 
| 62 |  | <dt>Request URI</dt> | 
| 63 |  | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{request_uri}]}">@{[htescape $input->{request_uri}]}</a>></code></dd> | 
| 64 | <dt>Document URI</dt> | <dt>Document URI</dt> | 
| 65 | <dd><code class="URI" lang=""><<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>></code></dd> | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{uri}]}">@{[htescape $input->{uri}]}</a>></code></dd> | 
|  | <dt>Internet Media Type</dt> |  | 
|  | <dd><code class="MIME" lang="en">@{[htescape $input_format]}</code></dd> |  | 
| 66 | ]; # no </dl> yet | ]; # no </dl> yet | 
| 67 | push @nav, ['#document-info' => 'Information']; | push @nav, ['#document-info' => 'Information']; | 
| 68 |  |  | 
| 69 | require Message::DOM::DOMImplementation; | if (defined $input->{s}) { | 
| 70 | my $dom = Message::DOM::DOMImplementation->____new; |  | 
| 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 |  | </dl> | 
| 81 |  | </div> | 
| 82 |  | ]; | 
| 83 |  |  | 
| 84 |  | print_http_header_section ($input); | 
| 85 |  |  | 
| 86 | my $doc; | my $doc; | 
| 87 | my $el; | my $el; | 
| 88 |  |  | 
| 89 | if ($input_format eq 'text/html') { | if ($input->{media_type} eq 'text/html') { | 
| 90 | require Encode; | require Encode; | 
| 91 | require Whatpm::HTML; | require Whatpm::HTML; | 
| 92 |  |  | 
| 93 |  | $input->{charset} ||= 'ISO-8859-1'; ## TODO: for now. | 
| 94 |  |  | 
| 95 | $s = Encode::decode ('utf-8', $s); | my $t = Encode::decode ($input->{charset}, $input->{s}); | 
| 96 |  |  | 
| 97 | print STDOUT qq[ | print STDOUT qq[ | 
|  | <dt>Character Encoding</dt> |  | 
|  | <dd>(none)</dd> |  | 
|  | </dl> |  | 
|  | </div> |  | 
|  |  |  | 
|  | <div id="source-string" class="section"> |  | 
|  | <h2>Document Source</h2> |  | 
|  | ]; |  | 
|  | push @nav, ['#source-string' => 'Source']; |  | 
|  | print_source_string (\$s); |  | 
|  | print STDOUT qq[ |  | 
|  | </div> |  | 
|  |  |  | 
| 98 | <div id="parse-errors" class="section"> | <div id="parse-errors" class="section"> | 
| 99 | <h2>Parse Errors</h2> | <h2>Parse Errors</h2> | 
| 100 |  |  | 
| 101 | <ul> | <dl>]; | 
|  | ]; |  | 
| 102 | push @nav, ['#parse-errors' => 'Parse Error']; | push @nav, ['#parse-errors' => 'Parse Error']; | 
| 103 |  |  | 
| 104 | my $onerror = sub { | my $onerror = sub { | 
| 105 | my (%opt) = @_; | my (%opt) = @_; | 
| 106 |  | my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}); | 
| 107 | if ($opt{column} > 0) { | if ($opt{column} > 0) { | 
| 108 | print STDOUT qq[<li><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}: ]; | print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n]; | 
| 109 | } else { | } else { | 
| 110 | $opt{line}--; | $opt{line} = $opt{line} - 1 || 1; | 
| 111 | print STDOUT qq[<li><a href="#line-$opt{line}">Line $opt{line}</a>: ]; | print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n]; | 
| 112 | } | } | 
| 113 | print STDOUT qq[@{[htescape $opt{type}]}</li>\n]; | $type =~ tr/ /-/; | 
| 114 |  | $type =~ s/\|/%7C/g; | 
| 115 |  | $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; | 
| 116 |  | print STDOUT qq[<dd class="$cls">$msg</dd>\n]; | 
| 117 | }; | }; | 
| 118 |  |  | 
| 119 | $doc = $dom->create_document; | $doc = $dom->create_document; | 
| 120 | if (defined $inner_html_element and length $inner_html_element) { | if (defined $inner_html_element and length $inner_html_element) { | 
| 121 | $el = $doc->create_element_ns | $el = $doc->create_element_ns | 
| 122 | ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); | ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); | 
| 123 | Whatpm::HTML->set_inner_html ($el, $s, $onerror); | Whatpm::HTML->set_inner_html ($el, $t, $onerror); | 
| 124 | } else { | } else { | 
| 125 | Whatpm::HTML->parse_string ($s => $doc, $onerror); | Whatpm::HTML->parse_string ($t => $doc, $onerror); | 
| 126 | } | } | 
| 127 |  |  | 
| 128 | print STDOUT qq[ | print STDOUT qq[</dl> | 
|  | </ul> |  | 
| 129 | </div> | </div> | 
| 130 | ]; | ]; | 
|  | } elsif ($input_format eq 'application/xhtml+xml') { |  | 
|  | require Message::DOM::XMLParserTemp; |  | 
|  | require Encode; |  | 
|  |  |  | 
|  | my $t = Encode::decode ('utf-8', $s); |  | 
| 131 |  |  | 
| 132 | print STDOUT qq[ | print_source_string_section (\($input->{s}), $input->{charset}); | 
| 133 | <dt>Character Encoding</dt> | } elsif ({ | 
| 134 | <dd>(none)</dd> | 'text/xml' => 1, | 
| 135 | </dl> | 'application/xhtml+xml' => 1, | 
| 136 | </div> | 'application/xml' => 1, | 
| 137 |  | }->{$input->{media_type}}) { | 
| 138 |  | require Message::DOM::XMLParserTemp; | 
| 139 |  |  | 
|  | <div id="source-string" class="section"> |  | 
|  | <h2>Document Source</h2> |  | 
|  | ]; |  | 
|  | push @nav, ['#source-string' => 'Source']; |  | 
|  | print_source_string (\$t); |  | 
| 140 | print STDOUT qq[ | print STDOUT qq[ | 
|  | </div> |  | 
|  |  |  | 
| 141 | <div id="parse-errors" class="section"> | <div id="parse-errors" class="section"> | 
| 142 | <h2>Parse Errors</h2> | <h2>Parse Errors</h2> | 
| 143 |  |  | 
| 144 | <ul> | <dl>]; | 
|  | ]; |  | 
| 145 | push @nav, ['#parse-errors' => 'Parse Error']; | push @nav, ['#parse-errors' => 'Parse Error']; | 
| 146 |  |  | 
| 147 | my $onerror = sub { | my $onerror = sub { | 
| 148 | my $err = shift; | my $err = shift; | 
| 149 | my $line = $err->location->line_number; | my $line = $err->location->line_number; | 
| 150 | print STDOUT qq[<li><a href="#line-$line">Line $line</a> column ]; | print STDOUT qq[<dt><a href="#line-$line">Line $line</a> column ]; | 
| 151 | print STDOUT $err->location->column_number, ": "; | print STDOUT $err->location->column_number, "</dt><dd>"; | 
| 152 | print STDOUT htescape $err->text, "</li>\n"; | print STDOUT htescape $err->text, "</dd>\n"; | 
| 153 | return 1; | return 1; | 
| 154 | }; | }; | 
| 155 |  |  | 
| 156 | open my $fh, '<', \$s; | open my $fh, '<', \($input->{s}); | 
| 157 | $doc = Message::DOM::XMLParserTemp->parse_byte_stream | $doc = Message::DOM::XMLParserTemp->parse_byte_stream | 
| 158 | ($fh => $dom, $onerror, charset => 'utf-8'); | ($fh => $dom, $onerror, charset => $input->{charset}); | 
| 159 |  |  | 
| 160 | print STDOUT qq[ | print STDOUT qq[</dl> | 
|  | </ul> |  | 
| 161 | </div> | </div> | 
| 162 |  |  | 
| 163 | ]; | ]; | 
| 164 |  | print_source_string_section (\($input->{s}), $doc->input_encoding); | 
| 165 | } else { | } else { | 
| 166 |  | ## TODO: Change HTTP status code?? | 
| 167 | print STDOUT qq[ | print STDOUT qq[ | 
|  | </dl> |  | 
|  |  |  | 
| 168 | <div id="result-summary" class="section"> | <div id="result-summary" class="section"> | 
| 169 | <p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p> | <p><em>Media type <code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> is not supported!</em></p> | 
| 170 | </div> | </div> | 
| 171 | ]; | ]; | 
| 172 | push @nav, ['#result-summary' => 'Result']; | push @nav, ['#result-summary' => 'Result']; | 
| 188 | <div id="document-errors" class="section"> | <div id="document-errors" class="section"> | 
| 189 | <h2>Document Errors</h2> | <h2>Document Errors</h2> | 
| 190 |  |  | 
| 191 | <ul> | <dl>]; | 
|  | ]; |  | 
| 192 | push @nav, ['#document-errors' => 'Document Error']; | push @nav, ['#document-errors' => 'Document Error']; | 
| 193 |  |  | 
| 194 | require Whatpm::ContentChecker; | require Whatpm::ContentChecker; | 
| 195 | my $onerror = sub { | my $onerror = sub { | 
| 196 | my %opt = @_; | my %opt = @_; | 
| 197 | print STDOUT qq[<li><a href="#node-@{[refaddr $opt{node}]}">], | my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}, $opt{node}); | 
| 198 | htescape get_node_path ($opt{node}), | $type =~ tr/ /-/; | 
| 199 | "</a>: ", htescape $opt{type}, "</li>\n"; | $type =~ s/\|/%7C/g; | 
| 200 |  | $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; | 
| 201 |  | print STDOUT qq[<dt class="$cls">] . get_node_link ($opt{node}) . | 
| 202 |  | qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n"; | 
| 203 | }; | }; | 
| 204 |  |  | 
| 205 |  | my $elements; | 
| 206 | if ($el) { | if ($el) { | 
| 207 | Whatpm::ContentChecker->check_element ($el, $onerror); | $elements = Whatpm::ContentChecker->check_element ($el, $onerror); | 
| 208 | } else { | } else { | 
| 209 | Whatpm::ContentChecker->check_document ($doc, $onerror); | $elements = Whatpm::ContentChecker->check_document ($doc, $onerror); | 
| 210 | } | } | 
| 211 |  |  | 
| 212 | print STDOUT qq[ | print STDOUT qq[</dl> | 
|  | </ul> |  | 
| 213 | </div> | </div> | 
| 214 | ]; | ]; | 
| 215 |  |  | 
| 216 |  | if (@{$elements->{table}}) { | 
| 217 |  | require JSON; | 
| 218 |  |  | 
| 219 |  | push @nav, ['#tables' => 'Tables']; | 
| 220 |  | print STDOUT qq[ | 
| 221 |  | <div id="tables" class="section"> | 
| 222 |  | <h2>Tables</h2> | 
| 223 |  |  | 
| 224 |  | <!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]--> | 
| 225 |  | <script src="../table-script.js" type="text/javascript"></script> | 
| 226 |  | <noscript> | 
| 227 |  | <p><em>Structure of tables are visualized here if scripting is enabled.</em></p> | 
| 228 |  | </noscript> | 
| 229 |  | ]; | 
| 230 |  |  | 
| 231 |  | my $i = 0; | 
| 232 |  | for my $table_el (@{$elements->{table}}) { | 
| 233 |  | $i++; | 
| 234 |  | print STDOUT qq[<div class="section" id="table-$i"><h3>] . | 
| 235 |  | get_node_link ($table_el) . q[</h3>]; | 
| 236 |  |  | 
| 237 |  | ## TODO: Make |ContentChecker| return |form_table| result | 
| 238 |  | ## so that this script don't have to run the algorithm twice. | 
| 239 |  | my $table = Whatpm::HTMLTable->form_table ($table_el); | 
| 240 |  |  | 
| 241 |  | for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { | 
| 242 |  | next unless $_; | 
| 243 |  | delete $_->{element}; | 
| 244 |  | } | 
| 245 |  |  | 
| 246 |  | for (@{$table->{row_group}}) { | 
| 247 |  | next unless $_; | 
| 248 |  | next unless $_->{element}; | 
| 249 |  | $_->{type} = $_->{element}->manakai_local_name; | 
| 250 |  | delete $_->{element}; | 
| 251 |  | } | 
| 252 |  |  | 
| 253 |  | for (@{$table->{cell}}) { | 
| 254 |  | next unless $_; | 
| 255 |  | for (@{$_}) { | 
| 256 |  | next unless $_; | 
| 257 |  | for (@$_) { | 
| 258 |  | $_->{id} = refaddr $_->{element} if defined $_->{element}; | 
| 259 |  | delete $_->{element}; | 
| 260 |  | $_->{is_header} = $_->{is_header} ? 1 : 0; | 
| 261 |  | } | 
| 262 |  | } | 
| 263 |  | } | 
| 264 |  |  | 
| 265 |  | print STDOUT '</div><script type="text/javascript">tableToCanvas ('; | 
| 266 |  | print STDOUT JSON::objToJson ($table); | 
| 267 |  | print STDOUT qq[, document.getElementById ('table-$i'));</script>]; | 
| 268 |  | } | 
| 269 |  |  | 
| 270 |  | print STDOUT qq[</div>]; | 
| 271 |  | } | 
| 272 |  |  | 
| 273 |  | if (keys %{$elements->{id}}) { | 
| 274 |  | push @nav, ['#identifiers' => 'IDs']; | 
| 275 |  | print STDOUT qq[ | 
| 276 |  | <div id="identifiers" class="section"> | 
| 277 |  | <h2>Identifiers</h2> | 
| 278 |  |  | 
| 279 |  | <dl> | 
| 280 |  | ]; | 
| 281 |  | for my $id (sort {$a cmp $b} keys %{$elements->{id}}) { | 
| 282 |  | print STDOUT qq[<dt><code>@{[htescape $id]}</code></dt>]; | 
| 283 |  | for (@{$elements->{id}->{$id}}) { | 
| 284 |  | print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>]; | 
| 285 |  | } | 
| 286 |  | } | 
| 287 |  | print STDOUT qq[</dl></div>]; | 
| 288 |  | } | 
| 289 |  |  | 
| 290 |  | if (keys %{$elements->{term}}) { | 
| 291 |  | push @nav, ['#terms' => 'Terms']; | 
| 292 |  | print STDOUT qq[ | 
| 293 |  | <div id="terms" class="section"> | 
| 294 |  | <h2>Terms</h2> | 
| 295 |  |  | 
| 296 |  | <dl> | 
| 297 |  | ]; | 
| 298 |  | for my $term (sort {$a cmp $b} keys %{$elements->{term}}) { | 
| 299 |  | print STDOUT qq[<dt>@{[htescape $term]}</dt>]; | 
| 300 |  | for (@{$elements->{term}->{$term}}) { | 
| 301 |  | print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>]; | 
| 302 |  | } | 
| 303 |  | } | 
| 304 |  | print STDOUT qq[</dl></div>]; | 
| 305 |  | } | 
| 306 |  |  | 
| 307 |  | if (keys %{$elements->{class}}) { | 
| 308 |  | push @nav, ['#classes' => 'Classes']; | 
| 309 |  | print STDOUT qq[ | 
| 310 |  | <div id="classes" class="section"> | 
| 311 |  | <h2>Classes</h2> | 
| 312 |  |  | 
| 313 |  | <dl> | 
| 314 |  | ]; | 
| 315 |  | for my $class (sort {$a cmp $b} keys %{$elements->{class}}) { | 
| 316 |  | print STDOUT qq[<dt><code>@{[htescape $class]}</code></dt>]; | 
| 317 |  | for (@{$elements->{class}->{$class}}) { | 
| 318 |  | print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>]; | 
| 319 |  | } | 
| 320 |  | } | 
| 321 |  | print STDOUT qq[</dl></div>]; | 
| 322 |  | } | 
| 323 | } | } | 
| 324 |  |  | 
| 325 | ## TODO: Show result | ## TODO: Show result | 
| 326 |  | } else { | 
| 327 |  | print STDOUT qq[ | 
| 328 |  | </dl> | 
| 329 |  | </div> | 
| 330 |  |  | 
| 331 |  | <div class="section" id="result-summary"> | 
| 332 |  | <p><em><strong>Input Error</strong>: @{[htescape ($input->{error_status_text})]}</em></p> | 
| 333 |  | </div> | 
| 334 |  | ]; | 
| 335 |  | push @nav, ['#result-summary' => 'Result']; | 
| 336 |  |  | 
| 337 |  | } | 
| 338 |  |  | 
| 339 | print STDOUT qq[ | print STDOUT qq[ | 
| 340 | <ul class="navigation" id="nav-items"> | <ul class="navigation" id="nav-items"> | 
| 350 |  |  | 
| 351 | exit; | exit; | 
| 352 |  |  | 
| 353 | sub print_source_string ($) { | sub print_http_header_section ($) { | 
| 354 | my $s = $_[0]; | my $input = shift; | 
| 355 | my $i = 1; | return unless defined $input->{header_status_code} or | 
| 356 | print STDOUT qq[<ol lang="">\n]; | defined $input->{header_status_text} or | 
| 357 | while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { | @{$input->{header_field}}; | 
| 358 | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |  | 
| 359 | $i++; | push @nav, ['#source-header' => 'HTTP Header']; | 
| 360 |  | print STDOUT qq[<div id="source-header" class="section"> | 
| 361 |  | <h2>HTTP Header</h2> | 
| 362 |  |  | 
| 363 |  | <p><strong>Note</strong>: Due to the limitation of the | 
| 364 |  | network library in use, the content of this section might | 
| 365 |  | not be the real header.</p> | 
| 366 |  |  | 
| 367 |  | <table><tbody> | 
| 368 |  | ]; | 
| 369 |  |  | 
| 370 |  | if (defined $input->{header_status_code}) { | 
| 371 |  | print STDOUT qq[<tr><th scope="row">Status code</th>]; | 
| 372 |  | print STDOUT qq[<td><code>@{[htescape ($input->{header_status_code})]}</code></td></tr>]; | 
| 373 |  | } | 
| 374 |  | if (defined $input->{header_status_text}) { | 
| 375 |  | print STDOUT qq[<tr><th scope="row">Status text</th>]; | 
| 376 |  | print STDOUT qq[<td><code>@{[htescape ($input->{header_status_text})]}</code></td></tr>]; | 
| 377 | } | } | 
| 378 | if ($$s =~ /\G([^\x0A]+)/gc) { |  | 
| 379 | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; | for (@{$input->{header_field}}) { | 
| 380 |  | print STDOUT qq[<tr><th scope="row"><code>@{[htescape ($_->[0])]}</code></th>]; | 
| 381 |  | print STDOUT qq[<td><code>@{[htescape ($_->[1])]}</code></td></tr>]; | 
| 382 | } | } | 
| 383 | print STDOUT "</ol>"; |  | 
| 384 | } # print_input_string | print STDOUT qq[</tbody></table></div>]; | 
| 385 |  | } # print_http_header_section | 
| 386 |  |  | 
| 387 |  | sub print_source_string_section ($$) { | 
| 388 |  | require Encode; | 
| 389 |  | my $enc = Encode::find_encoding ($_[1]); ## TODO: charset name -> Perl name | 
| 390 |  | return unless $enc; | 
| 391 |  |  | 
| 392 |  | my $s = \($enc->decode (${$_[0]})); | 
| 393 |  | my $i = 1; | 
| 394 |  | push @nav, ['#source-string' => 'Source']; | 
| 395 |  | print STDOUT qq[<div id="source-string" class="section"> | 
| 396 |  | <h2>Document Source</h2> | 
| 397 |  | <ol lang="">\n]; | 
| 398 |  | if (length $$s) { | 
| 399 |  | while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { | 
| 400 |  | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; | 
| 401 |  | $i++; | 
| 402 |  | } | 
| 403 |  | if ($$s =~ /\G([^\x0A]+)/gc) { | 
| 404 |  | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; | 
| 405 |  | } | 
| 406 |  | } else { | 
| 407 |  | print STDOUT q[<li id="line-1"></li>]; | 
| 408 |  | } | 
| 409 |  | print STDOUT "</ol></div>"; | 
| 410 |  | } # print_input_string_section | 
| 411 |  |  | 
| 412 | sub print_document_tree ($) { | sub print_document_tree ($) { | 
| 413 | my $node = shift; | my $node = shift; | 
| 424 | my $node_id = 'node-'.refaddr $child; | my $node_id = 'node-'.refaddr $child; | 
| 425 | my $nt = $child->node_type; | my $nt = $child->node_type; | 
| 426 | if ($nt == $child->ELEMENT_NODE) { | if ($nt == $child->ELEMENT_NODE) { | 
| 427 | $r .= qq'<li id="$node_id"><code>' . htescape ($child->tag_name) . | my $child_nsuri = $child->namespace_uri; | 
| 428 |  | $r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) . | 
| 429 | '</code>'; ## ISSUE: case | '</code>'; ## ISSUE: case | 
| 430 |  |  | 
| 431 | if ($child->has_attributes) { | if ($child->has_attributes) { | 
| 432 | $r .= '<ul class="attributes">'; | $r .= '<ul class="attributes">'; | 
| 433 | for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, 'node-'.refaddr $_] } | for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] } | 
| 434 | @{$child->attributes}) { | @{$child->attributes}) { | 
| 435 | $r .= qq'<li id="$attr->[2]"><code>' . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? | $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? | 
| 436 | $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children | $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children | 
| 437 | } | } | 
| 438 | $r .= '</ul>'; | $r .= '</ul>'; | 
| 439 | } | } | 
| 440 |  |  | 
| 441 | if ($node->has_child_nodes) { | if ($child->has_child_nodes) { | 
| 442 | $r .= '<ol class="children">'; | $r .= '<ol class="children">'; | 
| 443 | unshift @node, @{$child->child_nodes}, '</ol>'; | unshift @node, @{$child->child_nodes}, '</ol></li>'; | 
| 444 |  | } else { | 
| 445 |  | $r .= '</li>'; | 
| 446 | } | } | 
| 447 | } elsif ($nt == $child->TEXT_NODE) { | } elsif ($nt == $child->TEXT_NODE) { | 
| 448 | $r .= qq'<li id="$node_id"><q>' . htescape ($child->data) . '</q></li>'; | $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>'; | 
| 449 | } elsif ($nt == $child->CDATA_SECTION_NODE) { | } elsif ($nt == $child->CDATA_SECTION_NODE) { | 
| 450 | $r .= qq'<li id="$node_id"><code><[CDATA[</code><q>' . htescape ($child->data) . '</q><code>]]></code></li>'; | $r .= qq'<li id="$node_id" class="tree-cdata"><code><[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]></code></li>'; | 
| 451 | } elsif ($nt == $child->COMMENT_NODE) { | } elsif ($nt == $child->COMMENT_NODE) { | 
| 452 | $r .= qq'<li id="$node_id"><code><!--</code><q>' . htescape ($child->data) . '</q><code>--></code></li>'; | $r .= qq'<li id="$node_id" class="tree-comment"><code><!--</code><q lang="">' . htescape ($child->data) . '</q><code>--></code></li>'; | 
| 453 | } elsif ($nt == $child->DOCUMENT_NODE) { | } elsif ($nt == $child->DOCUMENT_NODE) { | 
| 454 | $r .= qq'<li id="$node_id">Document</li>'; | $r .= qq'<li id="$node_id" class="tree-document">Document'; | 
| 455 |  | $r .= qq[<ul class="attributes">]; | 
| 456 |  | $r .= qq[<li>@{[scalar get_text ('manakaiIsHTML:'.($child->manakai_is_html?1:0))]}</li>]; | 
| 457 |  | $r .= qq[<li>@{[scalar get_text ('manakaiCompatMode:'.$child->manakai_compat_mode)]}</li>]; | 
| 458 |  | unless ($child->manakai_is_html) { | 
| 459 |  | $r .= qq[<li>XML version = <code>@{[htescape ($child->xml_version)]}</code></li>]; | 
| 460 |  | if (defined $child->xml_encoding) { | 
| 461 |  | $r .= qq[<li>XML encoding = <code>@{[htescape ($child->xml_encoding)]}</code></li>]; | 
| 462 |  | } else { | 
| 463 |  | $r .= qq[<li>XML encoding = (null)</li>]; | 
| 464 |  | } | 
| 465 |  | $r .= qq[<li>XML standalone = @{[$child->xml_standalone ? 'true' : 'false']}</li>]; | 
| 466 |  | } | 
| 467 |  | $r .= qq[</ul>]; | 
| 468 | if ($child->has_child_nodes) { | if ($child->has_child_nodes) { | 
| 469 | $r .= '<ol>'; | $r .= '<ol class="children">'; | 
| 470 | unshift @node, @{$child->child_nodes}, '</ol>'; | unshift @node, @{$child->child_nodes}, '</ol></li>'; | 
| 471 | } | } | 
| 472 | } elsif ($nt == $child->DOCUMENT_TYPE_NODE) { | } elsif ($nt == $child->DOCUMENT_TYPE_NODE) { | 
| 473 | $r .= qq'<li id="$node_id"><code><!DOCTYPE></code><ul>'; | $r .= qq'<li id="$node_id" class="tree-doctype"><code><!DOCTYPE></code><ul class="attributes">'; | 
| 474 | $r .= '<li>Name = <q>@{[htescape ($child->name)]}</q></li>'; | $r .= qq[<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>]; | 
| 475 | $r .= '<li>Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>'; | $r .= qq[<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>]; | 
| 476 | $r .= '<li>System identifier = <q>@{[htescape ($child->system_id)]}</q></li>'; | $r .= qq[<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>]; | 
| 477 | $r .= '</ul></li>'; | $r .= '</ul></li>'; | 
| 478 | } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) { | } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) { | 
| 479 | $r .= qq'<li id="$node_id"><code><?@{[htescape ($child->target)]}?></code>'; | $r .= qq'<li id="$node_id" class="tree-id"><code><?@{[htescape ($child->target)]}</code> <q>@{[htescape ($child->data)]}</q><code>?></code></li>'; | 
|  | $r .= '<ul><li>@{[htescape ($child->data)]}</li></ul></li>'; |  | 
| 480 | } else { | } else { | 
| 481 | $r .= qq'<li id="$node_id">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error | $r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error | 
| 482 | } | } | 
| 483 | } | } | 
| 484 |  |  | 
| 501 | $rs = '"' . $node->data . '"'; | $rs = '"' . $node->data . '"'; | 
| 502 | $node = $node->parent_node; | $node = $node->parent_node; | 
| 503 | } elsif ($node->node_type == 9) { | } elsif ($node->node_type == 9) { | 
| 504 |  | @r = ('') unless @r; | 
| 505 | $rs = ''; | $rs = ''; | 
| 506 | $node = $node->parent_node; | $node = $node->parent_node; | 
| 507 | } else { | } else { | 
| 513 | return join '/', @r; | return join '/', @r; | 
| 514 | } # get_node_path | } # get_node_path | 
| 515 |  |  | 
| 516 |  | sub get_node_link ($) { | 
| 517 |  | return qq[<a href="#node-@{[refaddr $_[0]]}">] . | 
| 518 |  | htescape (get_node_path ($_[0])) . qq[</a>]; | 
| 519 |  | } # get_node_link | 
| 520 |  |  | 
| 521 |  | { | 
| 522 |  | my $Msg = {}; | 
| 523 |  |  | 
| 524 |  | sub load_text_catalog ($) { | 
| 525 |  | my $lang = shift; # MUST be a canonical lang name | 
| 526 |  | open my $file, '<', "cc-msg.$lang.txt" or die "$0: cc-msg.$lang.txt: $!"; | 
| 527 |  | while (<$file>) { | 
| 528 |  | if (s/^([^;]+);([^;]*);//) { | 
| 529 |  | my ($type, $cls, $msg) = ($1, $2, $_); | 
| 530 |  | $msg =~ tr/\x0D\x0A//d; | 
| 531 |  | $Msg->{$type} = [$cls, $msg]; | 
| 532 |  | } | 
| 533 |  | } | 
| 534 |  | } # load_text_catalog | 
| 535 |  |  | 
| 536 |  | sub get_text ($) { | 
| 537 |  | my ($type, $level, $node) = @_; | 
| 538 |  | $type = $level . ':' . $type if defined $level; | 
| 539 |  | my @arg; | 
| 540 |  | { | 
| 541 |  | if (defined $Msg->{$type}) { | 
| 542 |  | my $msg = $Msg->{$type}->[1]; | 
| 543 |  | $msg =~ s{<var>\$([0-9]+)</var>}{ | 
| 544 |  | defined $arg[$1] ? htescape ($arg[$1]) : '(undef)'; | 
| 545 |  | }ge; | 
| 546 |  | $msg =~ s{<var>{\@([A-Za-z0-9:_.-]+)}</var>}{ | 
| 547 |  | UNIVERSAL::can ($node, 'get_attribute_ns') | 
| 548 |  | ? htescape ($node->get_attribute_ns (undef, $1)) : '' | 
| 549 |  | }ge; | 
| 550 |  | $msg =~ s{<var>{\@}</var>}{ | 
| 551 |  | UNIVERSAL::can ($node, 'value') ? htescape ($node->value) : '' | 
| 552 |  | }ge; | 
| 553 |  | return ($type, $Msg->{$type}->[0], $msg); | 
| 554 |  | } elsif ($type =~ s/:([^:]*)$//) { | 
| 555 |  | unshift @arg, $1; | 
| 556 |  | redo; | 
| 557 |  | } | 
| 558 |  | } | 
| 559 |  | return ($type, '', htescape ($_[0])); | 
| 560 |  | } # get_text | 
| 561 |  |  | 
| 562 |  | } | 
| 563 |  |  | 
| 564 |  | sub get_input_document ($$) { | 
| 565 |  | my ($http, $dom) = @_; | 
| 566 |  |  | 
| 567 |  | my $request_uri = $http->parameter ('uri'); | 
| 568 |  | my $r = {}; | 
| 569 |  | if (defined $request_uri and length $request_uri) { | 
| 570 |  | my $uri = $dom->create_uri_reference ($request_uri); | 
| 571 |  | unless ({ | 
| 572 |  | http => 1, | 
| 573 |  | }->{lc $uri->uri_scheme}) { | 
| 574 |  | return {uri => $request_uri, request_uri => $request_uri, | 
| 575 |  | error_status_text => 'URI scheme not allowed'}; | 
| 576 |  | } | 
| 577 |  |  | 
| 578 |  | require Message::Util::HostPermit; | 
| 579 |  | my $host_permit = new Message::Util::HostPermit; | 
| 580 |  | $host_permit->add_rule (<<EOH); | 
| 581 |  | Allow host=suika port=80 | 
| 582 |  | Deny host=suika | 
| 583 |  | Allow host=suika.fam.cx port=80 | 
| 584 |  | Deny host=suika.fam.cx | 
| 585 |  | Deny host=localhost | 
| 586 |  | Deny host=*.localdomain | 
| 587 |  | Deny ipv4=0.0.0.0/8 | 
| 588 |  | Deny ipv4=10.0.0.0/8 | 
| 589 |  | Deny ipv4=127.0.0.0/8 | 
| 590 |  | Deny ipv4=169.254.0.0/16 | 
| 591 |  | Deny ipv4=172.0.0.0/11 | 
| 592 |  | Deny ipv4=192.0.2.0/24 | 
| 593 |  | Deny ipv4=192.88.99.0/24 | 
| 594 |  | Deny ipv4=192.168.0.0/16 | 
| 595 |  | Deny ipv4=198.18.0.0/15 | 
| 596 |  | Deny ipv4=224.0.0.0/4 | 
| 597 |  | Deny ipv4=255.255.255.255/32 | 
| 598 |  | Deny ipv6=0::0/0 | 
| 599 |  | Allow host=* | 
| 600 |  | EOH | 
| 601 |  | unless ($host_permit->check ($uri->uri_host, $uri->uri_port || 80)) { | 
| 602 |  | return {uri => $request_uri, request_uri => $request_uri, | 
| 603 |  | error_status_text => 'Connection to the host is forbidden'}; | 
| 604 |  | } | 
| 605 |  |  | 
| 606 |  | require LWP::UserAgent; | 
| 607 |  | my $ua = WDCC::LWPUA->new; | 
| 608 |  | $ua->{wdcc_dom} = $dom; | 
| 609 |  | $ua->{wdcc_host_permit} = $host_permit; | 
| 610 |  | $ua->agent ('Mozilla'); ## TODO: for now. | 
| 611 |  | $ua->parse_head (0); | 
| 612 |  | $ua->protocols_allowed ([qw/http/]); | 
| 613 |  | $ua->max_size (1000_000); | 
| 614 |  | my $req = HTTP::Request->new (GET => $request_uri); | 
| 615 |  | my $res = $ua->request ($req); | 
| 616 |  | if ($res->is_success or $http->parameter ('error-page')) { | 
| 617 |  | $r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and <base>. ## TODO: Use our own code! | 
| 618 |  | $r->{uri} = $res->request->uri; | 
| 619 |  | $r->{request_uri} = $request_uri; | 
| 620 |  |  | 
| 621 |  | ## TODO: More strict parsing... | 
| 622 |  | my $ct = $res->header ('Content-Type'); | 
| 623 |  | if (defined $ct and $ct =~ m#^([0-9A-Za-z._+-]+/[0-9A-Za-z._+-]+)#) { | 
| 624 |  | $r->{media_type} = lc $1; | 
| 625 |  | } | 
| 626 |  | if (defined $ct and $ct =~ /;\s*charset\s*=\s*"?(\S+)"?/i) { | 
| 627 |  | $r->{charset} = lc $1; | 
| 628 |  | $r->{charset} =~ tr/\\//d; | 
| 629 |  | } | 
| 630 |  |  | 
| 631 |  | my $input_charset = $http->parameter ('charset'); | 
| 632 |  | if (defined $input_charset and length $input_charset) { | 
| 633 |  | $r->{charset_overridden} | 
| 634 |  | = (not defined $r->{charset} or $r->{charset} ne $input_charset); | 
| 635 |  | $r->{charset} = $input_charset; | 
| 636 |  | } | 
| 637 |  |  | 
| 638 |  | $r->{s} = ''.$res->content; | 
| 639 |  | } else { | 
| 640 |  | $r->{uri} = $res->request->uri; | 
| 641 |  | $r->{request_uri} = $request_uri; | 
| 642 |  | $r->{error_status_text} = $res->status_line; | 
| 643 |  | } | 
| 644 |  |  | 
| 645 |  | $r->{header_field} = []; | 
| 646 |  | $res->scan (sub { | 
| 647 |  | push @{$r->{header_field}}, [$_[0], $_[1]]; | 
| 648 |  | }); | 
| 649 |  | $r->{header_status_code} = $res->code; | 
| 650 |  | $r->{header_status_text} = $res->message; | 
| 651 |  | } else { | 
| 652 |  | $r->{s} = ''.$http->parameter ('s'); | 
| 653 |  | $r->{uri} = q<thismessage:/>; | 
| 654 |  | $r->{request_uri} = q<thismessage:/>; | 
| 655 |  | $r->{base_uri} = q<thismessage:/>; | 
| 656 |  | $r->{charset} = ''.$http->parameter ('_charset_'); | 
| 657 |  | $r->{charset} =~ s/\s+//g; | 
| 658 |  | $r->{charset} = 'utf-8' if $r->{charset} eq ''; | 
| 659 |  | $r->{header_field} = []; | 
| 660 |  | } | 
| 661 |  |  | 
| 662 |  | my $input_format = $http->parameter ('i'); | 
| 663 |  | if (defined $input_format and length $input_format) { | 
| 664 |  | $r->{media_type_overridden} | 
| 665 |  | = (not defined $r->{media_type} or $input_format ne $r->{media_type}); | 
| 666 |  | $r->{media_type} = $input_format; | 
| 667 |  | } | 
| 668 |  | if (defined $r->{s} and not defined $r->{media_type}) { | 
| 669 |  | $r->{media_type} = 'text/html'; | 
| 670 |  | $r->{media_type_overridden} = 1; | 
| 671 |  | } | 
| 672 |  |  | 
| 673 |  | if ($r->{media_type} eq 'text/xml') { | 
| 674 |  | unless (defined $r->{charset}) { | 
| 675 |  | $r->{charset} = 'us-ascii'; | 
| 676 |  | } elsif ($r->{charset_overridden} and $r->{charset} eq 'us-ascii') { | 
| 677 |  | $r->{charset_overridden} = 0; | 
| 678 |  | } | 
| 679 |  | } | 
| 680 |  |  | 
| 681 |  | if (length $r->{s} > 1000_000) { | 
| 682 |  | $r->{error_status_text} = 'Entity-body too large'; | 
| 683 |  | delete $r->{s}; | 
| 684 |  | return $r; | 
| 685 |  | } | 
| 686 |  |  | 
| 687 |  | return $r; | 
| 688 |  | } # get_input_document | 
| 689 |  |  | 
| 690 |  | package WDCC::LWPUA; | 
| 691 |  | BEGIN { push our @ISA, 'LWP::UserAgent'; } | 
| 692 |  |  | 
| 693 |  | sub redirect_ok { | 
| 694 |  | my $ua = shift; | 
| 695 |  | unless ($ua->SUPER::redirect_ok (@_)) { | 
| 696 |  | return 0; | 
| 697 |  | } | 
| 698 |  |  | 
| 699 |  | my $uris = $_[1]->header ('Location'); | 
| 700 |  | return 0 unless $uris; | 
| 701 |  | my $uri = $ua->{wdcc_dom}->create_uri_reference ($uris); | 
| 702 |  | unless ({ | 
| 703 |  | http => 1, | 
| 704 |  | }->{lc $uri->uri_scheme}) { | 
| 705 |  | return 0; | 
| 706 |  | } | 
| 707 |  | unless ($ua->{wdcc_host_permit}->check ($uri->uri_host, $uri->uri_port || 80)) { | 
| 708 |  | return 0; | 
| 709 |  | } | 
| 710 |  | return 1; | 
| 711 |  | } # redirect_ok | 
| 712 |  |  | 
| 713 | =head1 AUTHOR | =head1 AUTHOR | 
| 714 |  |  | 
| 715 | Wakaba <w@suika.fam.cx>. | Wakaba <w@suika.fam.cx>. |