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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations) (download)
Sun Sep 2 08:40:49 2007 UTC (17 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.17: +278 -246 lines
++ ChangeLog	2 Sep 2007 08:40:45 -0000
2007-09-02  Wakaba  <wakaba@suika.fam.cx>

	* cc.cgi: Report document sections are now output by
	corresponding subroutines.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24