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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (hide annotations) (download)
Sun Sep 2 07:59:01 2007 UTC (17 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.16: +11 -1 lines
++ ChangeLog	2 Sep 2007 07:58:58 -0000
2007-08-26  Wakaba  <wakaba@suika.fam.cx>

	* cc.cgi: New catalog macros |local-name| and |element-local-name|.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24