1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
|
4 |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
5 |
/home/wakaba/work/manakai/lib |
6 |
/home/wakaba/public_html/-temp/wiki/lib]; |
7 |
use CGI::Carp qw[fatalsToBrowser]; |
8 |
use Scalar::Util qw[refaddr]; |
9 |
|
10 |
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; |
25 |
|
26 |
## TODO: _charset_ |
27 |
|
28 |
if ($http->meta_variable ('PATH_INFO') ne '/') { |
29 |
print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; |
30 |
exit; |
31 |
} |
32 |
|
33 |
binmode STDOUT, ':utf8'; |
34 |
|
35 |
require Message::DOM::DOMImplementation; |
36 |
my $dom = Message::DOM::DOMImplementation->new; |
37 |
|
38 |
my $input = get_input_document ($http, $dom); |
39 |
my $inner_html_element = $http->parameter ('e'); |
40 |
|
41 |
load_text_catalog ('en'); ## TODO: conneg |
42 |
|
43 |
my @nav; |
44 |
print STDOUT qq[Content-Type: text/html; charset=utf-8 |
45 |
|
46 |
<!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_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 { |
101 |
my (%opt) = @_; |
102 |
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; |
116 |
if (defined $inner_html_element and length $inner_html_element) { |
117 |
$el = $doc->create_element_ns |
118 |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
119 |
Whatpm::HTML->set_inner_html ($el, $t, $onerror); |
120 |
} else { |
121 |
Whatpm::HTML->parse_string ($t => $doc, $onerror); |
122 |
} |
123 |
|
124 |
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 |
<dl>]; |
141 |
push @nav, ['#parse-errors' => 'Parse Error']; |
142 |
|
143 |
my $onerror = sub { |
144 |
my $err = shift; |
145 |
my $line = $err->location->line_number; |
146 |
print STDOUT qq[<dt><a href="#line-$line">Line $line</a> column ]; |
147 |
print STDOUT $err->location->column_number, "</dt><dd>"; |
148 |
print STDOUT htescape $err->text, "</dd>\n"; |
149 |
return 1; |
150 |
}; |
151 |
|
152 |
open my $fh, '<', \($input->{s}); |
153 |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
154 |
($fh => $dom, $onerror, charset => $input->{charset}); |
155 |
|
156 |
print STDOUT qq[</dl> |
157 |
</div> |
158 |
|
159 |
]; |
160 |
print_source_string_section (\($input->{s}), $doc->input_encoding); |
161 |
} else { |
162 |
## TODO: Change HTTP status code?? |
163 |
print STDOUT qq[ |
164 |
<div id="result-summary" class="section"> |
165 |
<p><em>Media type <code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> is not supported!</em></p> |
166 |
</div> |
167 |
]; |
168 |
push @nav, ['#result-summary' => 'Result']; |
169 |
} |
170 |
|
171 |
|
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; |
191 |
my $onerror = sub { |
192 |
my %opt = @_; |
193 |
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 |
|
201 |
my $elements; |
202 |
if ($el) { |
203 |
$elements = Whatpm::ContentChecker->check_element ($el, $onerror); |
204 |
} else { |
205 |
$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 |
} |
299 |
} |
300 |
|
301 |
## TODO: Show result |
302 |
} else { |
303 |
print STDOUT qq[ |
304 |
</dl> |
305 |
</div> |
306 |
|
307 |
<div class="section" id="result-summary"> |
308 |
<p><em><strong>Input Error</strong>: @{[htescape ($input->{error_status_text})]}</em></p> |
309 |
</div> |
310 |
]; |
311 |
push @nav, ['#result-summary' => 'Result']; |
312 |
|
313 |
} |
314 |
|
315 |
print STDOUT qq[ |
316 |
<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; |
328 |
|
329 |
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; |
390 |
my $r = '<ol class="xoxo">'; |
391 |
|
392 |
my @node = ($node); |
393 |
while (@node) { |
394 |
my $child = shift @node; |
395 |
unless (ref $child) { |
396 |
$r .= $child; |
397 |
next; |
398 |
} |
399 |
|
400 |
my $node_id = 'node-'.refaddr $child; |
401 |
my $nt = $child->node_type; |
402 |
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 |
'</code>'; ## ISSUE: case |
406 |
|
407 |
if ($child->has_attributes) { |
408 |
$r .= '<ul class="attributes">'; |
409 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] } |
410 |
@{$child->attributes}) { |
411 |
$r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? |
412 |
$r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children |
413 |
} |
414 |
$r .= '</ul>'; |
415 |
} |
416 |
|
417 |
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 { |
457 |
$r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error |
458 |
} |
459 |
} |
460 |
|
461 |
$r .= '</ol>'; |
462 |
print STDOUT $r; |
463 |
} # print_document_tree |
464 |
|
465 |
sub get_node_path ($) { |
466 |
my $node = shift; |
467 |
my @r; |
468 |
while (defined $node) { |
469 |
my $rs; |
470 |
if ($node->node_type == 1) { |
471 |
$rs = $node->manakai_local_name; |
472 |
$node = $node->parent_node; |
473 |
} elsif ($node->node_type == 2) { |
474 |
$rs = '@' . $node->manakai_local_name; |
475 |
$node = $node->owner_element; |
476 |
} elsif ($node->node_type == 3) { |
477 |
$rs = '"' . $node->data . '"'; |
478 |
$node = $node->parent_node; |
479 |
} elsif ($node->node_type == 9) { |
480 |
@r = ('') unless @r; |
481 |
$rs = ''; |
482 |
$node = $node->parent_node; |
483 |
} else { |
484 |
$rs = '#' . $node->node_type; |
485 |
$node = $node->parent_node; |
486 |
} |
487 |
unshift @r, $rs; |
488 |
} |
489 |
return join '/', @r; |
490 |
} # 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 |
683 |
|
684 |
Wakaba <w@suika.fam.cx>. |
685 |
|
686 |
=head1 LICENSE |
687 |
|
688 |
Copyright 2007 Wakaba <w@suika.fam.cx> |
689 |
|
690 |
This library is free software; you can redistribute it |
691 |
and/or modify it under the same terms as Perl itself. |
692 |
|
693 |
=cut |
694 |
|
695 |
## $Date: 2007/07/16 13:56:26 $ |