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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations) (download)
Tue Jul 17 14:28:20 2007 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.13: +28 -4 lines
++ ChangeLog	17 Jul 2007 14:28:16 -0000
	* cc.cgi: Output HTTP and HTML header before try to
	fetch the specified resource for usability.  Add
	tables, identifiers, and terms section to the TOC.
	Generate classes list.

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24