15 |
$s =~ s/</</g; |
$s =~ s/</</g; |
16 |
$s =~ s/>/>/g; |
$s =~ s/>/>/g; |
17 |
$s =~ s/"/"/g; |
$s =~ s/"/"/g; |
18 |
$s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf '<var>U+%04X</var>', ord $1!ge; |
$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; |
return $s; |
22 |
} # htescape |
} # htescape |
23 |
|
|
30 |
exit; |
exit; |
31 |
} |
} |
32 |
|
|
33 |
my $input_format = $http->parameter ('i') || 'text/html'; |
binmode STDOUT, ':utf8'; |
34 |
my $inner_html_element = $http->parameter ('e'); |
$| = 1; |
|
my $input_uri = 'thismessage:/'; |
|
35 |
|
|
36 |
my $s = $http->parameter ('s'); |
require Message::DOM::DOMImplementation; |
37 |
if (length $s > 1000_000) { |
my $dom = Message::DOM::DOMImplementation->new; |
|
print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long"; |
|
|
exit; |
|
|
} |
|
38 |
|
|
39 |
load_text_catalog ('en'); ## TODO: conneg |
load_text_catalog ('en'); ## TODO: conneg |
40 |
|
|
48 |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
49 |
</head> |
</head> |
50 |
<body> |
<body> |
51 |
<h1>Web Document Conformance Checker (<em>beta</em>)</h1> |
<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"> |
<div id="document-info" class="section"> |
61 |
<dl> |
<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> |
<dt>Document URI</dt> |
65 |
<dd><code class="URI" lang=""><<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>></code></dd> |
<dd><code class="URI" lang=""><<a href="@{[htescape $input->{uri}]}">@{[htescape $input->{uri}]}</a>></code></dd> |
|
<dt>Internet Media Type</dt> |
|
|
<dd><code class="MIME" lang="en">@{[htescape $input_format]}</code></dd> |
|
66 |
]; # no </dl> yet |
]; # no </dl> yet |
67 |
push @nav, ['#document-info' => 'Information']; |
push @nav, ['#document-info' => 'Information']; |
68 |
|
|
69 |
require Message::DOM::DOMImplementation; |
if (defined $input->{s}) { |
70 |
my $dom = Message::DOM::DOMImplementation->____new; |
|
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_http_header_section ($input); |
85 |
|
|
86 |
my $doc; |
my $doc; |
87 |
my $el; |
my $el; |
88 |
|
|
89 |
if ($input_format eq 'text/html') { |
if ($input->{media_type} eq 'text/html') { |
90 |
require Encode; |
require Encode; |
91 |
require Whatpm::HTML; |
require Whatpm::HTML; |
|
|
|
|
$s = Encode::decode ('utf-8', $s); |
|
92 |
|
|
93 |
print STDOUT qq[ |
$input->{charset} ||= 'ISO-8859-1'; ## TODO: for now. |
94 |
<dt>Character Encoding</dt> |
|
95 |
<dd>(none)</dd> |
my $t = Encode::decode ($input->{charset}, $input->{s}); |
|
</dl> |
|
|
</div> |
|
96 |
|
|
|
<div id="source-string" class="section"> |
|
|
<h2>Document Source</h2> |
|
|
]; |
|
|
push @nav, ['#source-string' => 'Source']; |
|
|
print_source_string (\$s); |
|
97 |
print STDOUT qq[ |
print STDOUT qq[ |
|
</div> |
|
|
|
|
98 |
<div id="parse-errors" class="section"> |
<div id="parse-errors" class="section"> |
99 |
<h2>Parse Errors</h2> |
<h2>Parse Errors</h2> |
100 |
|
|
101 |
<dl> |
<dl>]; |
|
]; |
|
102 |
push @nav, ['#parse-errors' => 'Parse Error']; |
push @nav, ['#parse-errors' => 'Parse Error']; |
103 |
|
|
104 |
my $onerror = sub { |
my $onerror = sub { |
105 |
my (%opt) = @_; |
my (%opt) = @_; |
106 |
my ($cls, $msg) = get_text ($opt{type}, $opt{level}); |
my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}); |
107 |
if ($opt{column} > 0) { |
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]; |
print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n]; |
109 |
} else { |
} else { |
110 |
$opt{line} = $opt{line} - 1 || 1; |
$opt{line} = $opt{line} - 1 || 1; |
111 |
print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n]; |
print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n]; |
112 |
} |
} |
113 |
$opt{type} =~ tr/ /-/; |
$type =~ tr/ /-/; |
114 |
$opt{type} =~ s/\|/%7C/g; |
$type =~ s/\|/%7C/g; |
115 |
$msg .= qq[ [<a href="../error-description#$opt{type}">Description</a>]]; |
$msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; |
116 |
print STDOUT qq[<dd class="$cls">$msg</dd>\n]; |
print STDOUT qq[<dd class="$cls">$msg</dd>\n]; |
117 |
}; |
}; |
118 |
|
|
120 |
if (defined $inner_html_element and length $inner_html_element) { |
if (defined $inner_html_element and length $inner_html_element) { |
121 |
$el = $doc->create_element_ns |
$el = $doc->create_element_ns |
122 |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
('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 |
} |
} |
127 |
|
|
128 |
print STDOUT qq[ |
print STDOUT qq[</dl> |
|
</dl> |
|
129 |
</div> |
</div> |
130 |
]; |
]; |
131 |
} elsif ($input_format eq 'application/xhtml+xml') { |
|
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; |
require Message::DOM::XMLParserTemp; |
|
require Encode; |
|
|
|
|
|
my $t = Encode::decode ('utf-8', $s); |
|
139 |
|
|
140 |
print STDOUT qq[ |
print STDOUT qq[ |
|
<dt>Character Encoding</dt> |
|
|
<dd>(none)</dd> |
|
|
</dl> |
|
|
</div> |
|
|
|
|
|
<div id="source-string" class="section"> |
|
|
<h2>Document Source</h2> |
|
|
]; |
|
|
push @nav, ['#source-string' => 'Source']; |
|
|
print_source_string (\$t); |
|
|
print STDOUT qq[ |
|
|
</div> |
|
|
|
|
141 |
<div id="parse-errors" class="section"> |
<div id="parse-errors" class="section"> |
142 |
<h2>Parse Errors</h2> |
<h2>Parse Errors</h2> |
143 |
|
|
153 |
return 1; |
return 1; |
154 |
}; |
}; |
155 |
|
|
156 |
open my $fh, '<', \$s; |
open my $fh, '<', \($input->{s}); |
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 |
|
|
160 |
print STDOUT qq[</dl> |
print STDOUT qq[</dl> |
161 |
</div> |
</div> |
162 |
|
|
163 |
]; |
]; |
164 |
|
print_source_string_section (\($input->{s}), $doc->input_encoding); |
165 |
} else { |
} else { |
166 |
|
## TODO: Change HTTP status code?? |
167 |
print STDOUT qq[ |
print STDOUT qq[ |
|
</dl> |
|
|
</div> |
|
|
|
|
168 |
<div id="result-summary" class="section"> |
<div id="result-summary" class="section"> |
169 |
<p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p> |
<p><em>Media type <code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> is not supported!</em></p> |
170 |
</div> |
</div> |
171 |
]; |
]; |
172 |
push @nav, ['#result-summary' => 'Result']; |
push @nav, ['#result-summary' => 'Result']; |
194 |
require Whatpm::ContentChecker; |
require Whatpm::ContentChecker; |
195 |
my $onerror = sub { |
my $onerror = sub { |
196 |
my %opt = @_; |
my %opt = @_; |
197 |
my ($cls, $msg) = get_text ($opt{type}, $opt{level}); |
my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}, $opt{node}); |
198 |
$opt{type} = $opt{level} . ':' . $opt{type} if defined $opt{level}; |
$type =~ tr/ /-/; |
199 |
$opt{type} =~ tr/ /-/; |
$type =~ s/\|/%7C/g; |
200 |
$opt{type} =~ s/\|/%7C/g; |
$msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; |
|
$msg .= qq[ [<a href="../error-description#$opt{type}">Description</a>]]; |
|
201 |
print STDOUT qq[<dt class="$cls">] . get_node_link ($opt{node}) . |
print STDOUT qq[<dt class="$cls">] . get_node_link ($opt{node}) . |
202 |
qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n"; |
qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n"; |
203 |
}; |
}; |
216 |
if (@{$elements->{table}}) { |
if (@{$elements->{table}}) { |
217 |
require JSON; |
require JSON; |
218 |
|
|
219 |
|
push @nav, ['#tables' => 'Tables']; |
220 |
print STDOUT qq[ |
print STDOUT qq[ |
221 |
<div id="tables" class="section"> |
<div id="tables" class="section"> |
222 |
<h2>Tables</h2> |
<h2>Tables</h2> |
233 |
$i++; |
$i++; |
234 |
print STDOUT qq[<div class="section" id="table-$i"><h3>] . |
print STDOUT qq[<div class="section" id="table-$i"><h3>] . |
235 |
get_node_link ($table_el) . q[</h3>]; |
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); |
my $table = Whatpm::HTMLTable->form_table ($table_el); |
240 |
|
|
241 |
for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { |
for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { |
257 |
for (@$_) { |
for (@$_) { |
258 |
$_->{id} = refaddr $_->{element} if defined $_->{element}; |
$_->{id} = refaddr $_->{element} if defined $_->{element}; |
259 |
delete $_->{element}; |
delete $_->{element}; |
260 |
|
$_->{is_header} = $_->{is_header} ? 1 : 0; |
261 |
} |
} |
262 |
} |
} |
263 |
} |
} |
270 |
print STDOUT qq[</div>]; |
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}}) { |
if (keys %{$elements->{term}}) { |
291 |
|
push @nav, ['#terms' => 'Terms']; |
292 |
print STDOUT qq[ |
print STDOUT qq[ |
293 |
<div id="terms" class="section"> |
<div id="terms" class="section"> |
294 |
<h2>Terms</h2> |
<h2>Terms</h2> |
303 |
} |
} |
304 |
print STDOUT qq[</dl></div>]; |
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 |
|
} |
323 |
} |
} |
324 |
|
|
325 |
## TODO: Show result |
## TODO: Show result |
326 |
|
} else { |
327 |
|
print STDOUT qq[ |
328 |
|
</dl> |
329 |
|
</div> |
330 |
|
|
331 |
|
<div class="section" id="result-summary"> |
332 |
|
<p><em><strong>Input Error</strong>: @{[htescape ($input->{error_status_text})]}</em></p> |
333 |
|
</div> |
334 |
|
]; |
335 |
|
push @nav, ['#result-summary' => 'Result']; |
336 |
|
|
337 |
|
} |
338 |
|
|
339 |
print STDOUT qq[ |
print STDOUT qq[ |
340 |
<ul class="navigation" id="nav-items"> |
<ul class="navigation" id="nav-items"> |
350 |
|
|
351 |
exit; |
exit; |
352 |
|
|
353 |
sub print_source_string ($) { |
sub print_http_header_section ($) { |
354 |
my $s = $_[0]; |
my $input = shift; |
355 |
my $i = 1; |
return unless defined $input->{header_status_code} or |
356 |
print STDOUT qq[<ol lang="">\n]; |
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) { |
if (length $$s) { |
399 |
while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { |
while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { |
400 |
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
406 |
} else { |
} else { |
407 |
print STDOUT q[<li id="line-1"></li>]; |
print STDOUT q[<li id="line-1"></li>]; |
408 |
} |
} |
409 |
print STDOUT "</ol>"; |
print STDOUT "</ol></div>"; |
410 |
} # print_input_string |
} # print_input_string_section |
411 |
|
|
412 |
sub print_document_tree ($) { |
sub print_document_tree ($) { |
413 |
my $node = shift; |
my $node = shift; |
455 |
$r .= qq[<ul class="attributes">]; |
$r .= qq[<ul class="attributes">]; |
456 |
$r .= qq[<li>@{[scalar get_text ('manakaiIsHTML:'.($child->manakai_is_html?1:0))]}</li>]; |
$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>]; |
$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>]; |
$r .= qq[</ul>]; |
468 |
if ($child->has_child_nodes) { |
if ($child->has_child_nodes) { |
469 |
$r .= '<ol class="children">'; |
$r .= '<ol class="children">'; |
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 { |
534 |
} # load_text_catalog |
} # load_text_catalog |
535 |
|
|
536 |
sub get_text ($) { |
sub get_text ($) { |
537 |
my ($type, $level) = @_; |
my ($type, $level, $node) = @_; |
538 |
$type = $level . ':' . $type if defined $level; |
$type = $level . ':' . $type if defined $level; |
539 |
my @arg; |
my @arg; |
540 |
{ |
{ |
541 |
if (defined $Msg->{$type}) { |
if (defined $Msg->{$type}) { |
542 |
my $msg = $Msg->{$type}->[1]; |
my $msg = $Msg->{$type}->[1]; |
543 |
$msg =~ s/\$([0-9]+)/defined $arg[$1] ? htescape ($arg[$1]) : '(undef)'/ge; |
$msg =~ s{<var>\$([0-9]+)</var>}{ |
544 |
return ($Msg->{$type}->[0], $msg); |
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/:([^:]*)$//) { |
} elsif ($type =~ s/:([^:]*)$//) { |
555 |
unshift @arg, $1; |
unshift @arg, $1; |
556 |
redo; |
redo; |
557 |
} |
} |
558 |
} |
} |
559 |
return ('', htescape ($_[0])); |
return ($type, '', htescape ($_[0])); |
560 |
} # get_text |
} # 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>. |