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