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