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