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