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