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