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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations) (download)
Tue Jul 17 13:52:54 2007 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.12: +22 -3 lines
++ ChangeLog	17 Jul 2007 13:52:41 -0000
2007-07-17  Wakaba  <wakaba@suika.fam.cx>

	* alert.png, error.png, info.png: New images.

	* LICENSE: New image file names are added.

	* cc-interface.en.html: Link to the style sheet is added.

	* cc-style.css: Duplicate identifiers and terms
	are decorated by icon.

	* cc.cgi: |h1| links to the interface page.
	(#identifiers): New section.

	* error-description-source.en.xml: More error types are
	added.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24