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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations) (download)
Sun Jul 15 16:39:10 2007 UTC (16 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.8: +260 -60 lines
++ ChangeLog	15 Jul 2007 16:39:03 -0000
2007-07-16  Wakaba  <wakaba@suika.fam.cx>

	* cc-interface.en.html: Content-Type options are extended.
	Charset options are added.  URI input box is added.

	* cc.cgi (get_input_document): New.  Support for URI input.
	(#document-info): Present request URI, documen URI,
	base URI (HTTP-level), internet media type, and
	charset (if any).
	(print_http_header_section): New.
	(print_source_string_section): Renamed from |print_source_string|
	and it now generate entire section.  Use charset
	information obtained by parsing (for this reason the section
	now follows the parse error section).
	(HTML parse mode): Support for charsets (alpha).
	(XML parse mode): Support for |application/xml|
	and |text/xml|.  Support for charsets.
	(#result-summary): Show error message section,
	rather than simple HTTP error, for any input error.
	(print_document_tree): Present |xml_version|, |xml_encoding|,
	and |xml_standalone|.
	(get_node_path): Return |/| if the input is the |Document| node.
	(WDCC::LWPUA): New package.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24