/[suikacvs]/test/html-webhacc/cc.cgi
Suika

Contents of /test/html-webhacc/cc.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations) (download)
Mon Jul 16 13:56:26 2007 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.11: +6 -2 lines
++ ChangeLog	16 Jul 2007 13:55:43 -0000
	* cc.cgi: Set |STDOUT| encoding as |utf8|.
	(htescape): Escape some more invisible characters.

	* error-description-source.en.xml: More error data.

2007-07-16  Wakaba  <wakaba@suika.fam.cx>

	* error-description-source.en.xml: More error descriptions

1 wakaba 1.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 wakaba 1.2 use Scalar::Util qw[refaddr];
9 wakaba 1.1
10     use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module
11    
12 wakaba 1.2 sub htescape ($) {
13     my $s = $_[0];
14     $s =~ s/&/&amp;/g;
15     $s =~ s/</&lt;/g;
16     $s =~ s/>/&gt;/g;
17     $s =~ s/"/&quot;/g;
18 wakaba 1.12 $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 wakaba 1.2 return $s;
22     } # htescape
23    
24 wakaba 1.1 my $http = SuikaWiki::Input::HTTP->new;
25    
26     ## TODO: _charset_
27    
28 wakaba 1.8 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 wakaba 1.12 binmode STDOUT, ':utf8';
34    
35 wakaba 1.9 require Message::DOM::DOMImplementation;
36     my $dom = Message::DOM::DOMImplementation->new;
37    
38     my $input = get_input_document ($http, $dom);
39 wakaba 1.2 my $inner_html_element = $http->parameter ('e');
40    
41 wakaba 1.7 load_text_catalog ('en'); ## TODO: conneg
42    
43 wakaba 1.3 my @nav;
44 wakaba 1.2 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 wakaba 1.3 <link rel="stylesheet" href="../cc-style.css" type="text/css">
51 wakaba 1.2 </head>
52     <body>
53     <h1>Web Document Conformance Checker (<em>beta</em>)</h1>
54    
55 wakaba 1.4 <div id="document-info" class="section">
56 wakaba 1.2 <dl>
57 wakaba 1.9 <dt>Request URI</dt>
58     <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input->{request_uri}]}">@{[htescape $input->{request_uri}]}</a>&gt;</code></dd>
59 wakaba 1.2 <dt>Document URI</dt>
60 wakaba 1.9 <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input->{uri}]}">@{[htescape $input->{uri}]}</a>&gt;</code></dd>
61 wakaba 1.2 ]; # no </dl> yet
62 wakaba 1.3 push @nav, ['#document-info' => 'Information'];
63 wakaba 1.1
64 wakaba 1.9 if (defined $input->{s}) {
65    
66     print STDOUT qq[
67     <dt>Base URI</dt>
68     <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input->{base_uri}]}">@{[htescape $input->{base_uri}]}</a>&gt;</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 wakaba 1.1 my $doc;
82     my $el;
83    
84 wakaba 1.9 if ($input->{media_type} eq 'text/html') {
85 wakaba 1.2 require Encode;
86     require Whatpm::HTML;
87 wakaba 1.10
88     $input->{charset} ||= 'ISO-8859-1'; ## TODO: for now.
89 wakaba 1.2
90 wakaba 1.9 my $t = Encode::decode ($input->{charset}, $input->{s});
91 wakaba 1.2
92     print STDOUT qq[
93     <div id="parse-errors" class="section">
94     <h2>Parse Errors</h2>
95    
96 wakaba 1.10 <dl>];
97 wakaba 1.3 push @nav, ['#parse-errors' => 'Parse Error'];
98 wakaba 1.1
99     my $onerror = sub {
100     my (%opt) = @_;
101 wakaba 1.11 my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level});
102 wakaba 1.2 if ($opt{column} > 0) {
103 wakaba 1.7 print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n];
104 wakaba 1.2 } else {
105 wakaba 1.7 $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 wakaba 1.2 }
108 wakaba 1.11 $type =~ tr/ /-/;
109     $type =~ s/\|/%7C/g;
110     $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]];
111 wakaba 1.7 print STDOUT qq[<dd class="$cls">$msg</dd>\n];
112 wakaba 1.1 };
113    
114     $doc = $dom->create_document;
115 wakaba 1.2 if (defined $inner_html_element and length $inner_html_element) {
116 wakaba 1.1 $el = $doc->create_element_ns
117 wakaba 1.2 ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]);
118 wakaba 1.9 Whatpm::HTML->set_inner_html ($el, $t, $onerror);
119 wakaba 1.1 } else {
120 wakaba 1.9 Whatpm::HTML->parse_string ($t => $doc, $onerror);
121 wakaba 1.1 }
122    
123 wakaba 1.10 print STDOUT qq[</dl>
124 wakaba 1.2 </div>
125     ];
126 wakaba 1.9
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 wakaba 1.2 require Message::DOM::XMLParserTemp;
134    
135     print STDOUT qq[
136     <div id="parse-errors" class="section">
137     <h2>Parse Errors</h2>
138 wakaba 1.1
139 wakaba 1.7 <dl>];
140 wakaba 1.3 push @nav, ['#parse-errors' => 'Parse Error'];
141 wakaba 1.1
142     my $onerror = sub {
143     my $err = shift;
144 wakaba 1.2 my $line = $err->location->line_number;
145 wakaba 1.4 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 wakaba 1.1 return 1;
149     };
150    
151 wakaba 1.9 open my $fh, '<', \($input->{s});
152 wakaba 1.1 $doc = Message::DOM::XMLParserTemp->parse_byte_stream
153 wakaba 1.9 ($fh => $dom, $onerror, charset => $input->{charset});
154 wakaba 1.1
155 wakaba 1.7 print STDOUT qq[</dl>
156 wakaba 1.2 </div>
157 wakaba 1.9
158 wakaba 1.2 ];
159 wakaba 1.9 print_source_string_section (\($input->{s}), $doc->input_encoding);
160 wakaba 1.2 } else {
161 wakaba 1.9 ## TODO: Change HTTP status code??
162 wakaba 1.2 print STDOUT qq[
163 wakaba 1.3 <div id="result-summary" class="section">
164 wakaba 1.9 <p><em>Media type <code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> is not supported!</em></p>
165 wakaba 1.3 </div>
166 wakaba 1.2 ];
167 wakaba 1.3 push @nav, ['#result-summary' => 'Result'];
168 wakaba 1.2 }
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 wakaba 1.3 push @nav, ['#document-tree' => 'Tree'];
177 wakaba 1.2
178     print_document_tree ($el || $doc);
179    
180     print STDOUT qq[
181     </div>
182 wakaba 1.1
183 wakaba 1.2 <div id="document-errors" class="section">
184     <h2>Document Errors</h2>
185    
186 wakaba 1.7 <dl>];
187 wakaba 1.3 push @nav, ['#document-errors' => 'Document Error'];
188 wakaba 1.1
189     require Whatpm::ContentChecker;
190     my $onerror = sub {
191     my %opt = @_;
192 wakaba 1.11 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 wakaba 1.7 print STDOUT qq[<dt class="$cls">] . get_node_link ($opt{node}) .
197     qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n";
198 wakaba 1.1 };
199 wakaba 1.2
200 wakaba 1.5 my $elements;
201 wakaba 1.1 if ($el) {
202 wakaba 1.5 $elements = Whatpm::ContentChecker->check_element ($el, $onerror);
203 wakaba 1.1 } else {
204 wakaba 1.5 $elements = Whatpm::ContentChecker->check_document ($doc, $onerror);
205 wakaba 1.1 }
206 wakaba 1.2
207 wakaba 1.7 print STDOUT qq[</dl>
208 wakaba 1.2 </div>
209     ];
210 wakaba 1.5
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 wakaba 1.6 print STDOUT qq[<div class="section" id="table-$i"><h3>] .
229     get_node_link ($table_el) . q[</h3>];
230 wakaba 1.5
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 wakaba 1.11 $_->{is_header} = $_->{is_header} ? 1 : 0;
253 wakaba 1.5 }
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 wakaba 1.6
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 wakaba 1.1 }
281    
282 wakaba 1.2 ## TODO: Show result
283 wakaba 1.9 } 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 wakaba 1.3
296 wakaba 1.2 print STDOUT qq[
297 wakaba 1.3 <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 wakaba 1.2 </body>
305     </html>
306     ];
307 wakaba 1.1
308     exit;
309    
310 wakaba 1.9 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 wakaba 1.7 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 wakaba 1.2 }
366 wakaba 1.9 print STDOUT "</ol></div>";
367     } # print_input_string_section
368 wakaba 1.2
369     sub print_document_tree ($) {
370 wakaba 1.1 my $node = shift;
371 wakaba 1.2 my $r = '<ol class="xoxo">';
372 wakaba 1.1
373 wakaba 1.2 my @node = ($node);
374 wakaba 1.1 while (@node) {
375     my $child = shift @node;
376 wakaba 1.2 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 wakaba 1.4 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 wakaba 1.2 '</code>'; ## ISSUE: case
387    
388     if ($child->has_attributes) {
389     $r .= '<ul class="attributes">';
390 wakaba 1.4 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }
391 wakaba 1.2 @{$child->attributes}) {
392 wakaba 1.4 $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case?
393 wakaba 1.2 $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children
394     }
395     $r .= '</ul>';
396     }
397    
398 wakaba 1.7 if ($child->has_child_nodes) {
399 wakaba 1.2 $r .= '<ol class="children">';
400 wakaba 1.6 unshift @node, @{$child->child_nodes}, '</ol></li>';
401     } else {
402     $r .= '</li>';
403 wakaba 1.2 }
404     } elsif ($nt == $child->TEXT_NODE) {
405 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>';
406 wakaba 1.2 } elsif ($nt == $child->CDATA_SECTION_NODE) {
407 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-cdata"><code>&lt;[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]&gt;</code></li>';
408 wakaba 1.2 } elsif ($nt == $child->COMMENT_NODE) {
409 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-comment"><code>&lt;!--</code><q lang="">' . htescape ($child->data) . '</q><code>--&gt;</code></li>';
410 wakaba 1.2 } elsif ($nt == $child->DOCUMENT_NODE) {
411 wakaba 1.6 $r .= qq'<li id="$node_id" class="tree-document">Document';
412 wakaba 1.7 $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 wakaba 1.9 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 wakaba 1.7 $r .= qq[</ul>];
425 wakaba 1.2 if ($child->has_child_nodes) {
426 wakaba 1.7 $r .= '<ol class="children">';
427 wakaba 1.6 unshift @node, @{$child->child_nodes}, '</ol></li>';
428 wakaba 1.1 }
429 wakaba 1.2 } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {
430 wakaba 1.5 $r .= qq'<li id="$node_id" class="tree-doctype"><code>&lt;!DOCTYPE&gt;</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 wakaba 1.2 $r .= '</ul></li>';
435     } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {
436 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-id"><code>&lt;?@{[htescape ($child->target)]}</code> <q>@{[htescape ($child->data)]}</q><code>?&gt;</code></li>';
437 wakaba 1.1 } else {
438 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error
439 wakaba 1.1 }
440     }
441 wakaba 1.2
442     $r .= '</ol>';
443     print STDOUT $r;
444     } # print_document_tree
445 wakaba 1.1
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 wakaba 1.9 @r = ('') unless @r;
462 wakaba 1.1 $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 wakaba 1.6 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 wakaba 1.7 {
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 wakaba 1.10 $msg =~ s{<var>\$([0-9]+)</var>}{
501     defined $arg[$1] ? htescape ($arg[$1]) : '(undef)';
502     }ge;
503 wakaba 1.11 return ($type, $Msg->{$type}->[0], $msg);
504 wakaba 1.7 } elsif ($type =~ s/:([^:]*)$//) {
505     unshift @arg, $1;
506     redo;
507     }
508     }
509 wakaba 1.11 return ($type, '', htescape ($_[0]));
510 wakaba 1.7 } # get_text
511    
512     }
513    
514 wakaba 1.9 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 wakaba 1.1 =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 wakaba 1.12 ## $Date: 2007/07/16 10:55:11 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24