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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations) (download)
Sat Jul 21 04:58:17 2007 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.14: +12 -5 lines
++ ChangeLog	21 Jul 2007 04:58:03 -0000
2007-07-21  Wakaba  <wakaba@suika.fam.cx>

	* cc-style.css: Don't remove bullet of the document node
	in the document tree.  Don't collapse white space
	in the |code| elements.

	* cc.cgi: Put identifiers and class names into |code|
	element.
	(get_text): Template for attribute values are supported.

	* error-descriotion-source.en.xml: More error descriptions
	are included.  Use |{@}| attribute value insertion macro
	for errors in which it should be useful.

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 use Scalar::Util qw[refaddr];
9
10 use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module
11
12 sub htescape ($) {
13 my $s = $_[0];
14 $s =~ s/&/&amp;/g;
15 $s =~ s/</&lt;/g;
16 $s =~ s/>/&gt;/g;
17 $s =~ s/"/&quot;/g;
18 $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{
19 sprintf '<var>U+%04X</var>', ord $1;
20 }ge;
21 return $s;
22 } # htescape
23
24 my $http = SuikaWiki::Input::HTTP->new;
25
26 ## TODO: _charset_
27
28 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 binmode STDOUT, ':utf8';
34 $| = 1;
35
36 require Message::DOM::DOMImplementation;
37 my $dom = Message::DOM::DOMImplementation->new;
38
39 load_text_catalog ('en'); ## TODO: conneg
40
41 my @nav;
42 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 <link rel="stylesheet" href="../cc-style.css" type="text/css">
49 </head>
50 <body>
51 <h1><a href="../cc-interface">Web Document Conformance Checker</a>
52 (<em>beta</em>)</h1>
53 ];
54
55 $| = 0;
56 my $input = get_input_document ($http, $dom);
57 my $inner_html_element = $http->parameter ('e');
58
59 print qq[
60 <div id="document-info" class="section">
61 <dl>
62 <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 <dt>Document URI</dt>
65 <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input->{uri}]}">@{[htescape $input->{uri}]}</a>&gt;</code></dd>
66 ]; # no </dl> yet
67 push @nav, ['#document-info' => 'Information'];
68
69 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 my $doc;
87 my $el;
88
89 if ($input->{media_type} eq 'text/html') {
90 require Encode;
91 require Whatpm::HTML;
92
93 $input->{charset} ||= 'ISO-8859-1'; ## TODO: for now.
94
95 my $t = Encode::decode ($input->{charset}, $input->{s});
96
97 print STDOUT qq[
98 <div id="parse-errors" class="section">
99 <h2>Parse Errors</h2>
100
101 <dl>];
102 push @nav, ['#parse-errors' => 'Parse Error'];
103
104 my $onerror = sub {
105 my (%opt) = @_;
106 my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level});
107 if ($opt{column} > 0) {
108 print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n];
109 } else {
110 $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 }
113 $type =~ tr/ /-/;
114 $type =~ s/\|/%7C/g;
115 $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]];
116 print STDOUT qq[<dd class="$cls">$msg</dd>\n];
117 };
118
119 $doc = $dom->create_document;
120 if (defined $inner_html_element and length $inner_html_element) {
121 $el = $doc->create_element_ns
122 ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]);
123 Whatpm::HTML->set_inner_html ($el, $t, $onerror);
124 } else {
125 Whatpm::HTML->parse_string ($t => $doc, $onerror);
126 }
127
128 print STDOUT qq[</dl>
129 </div>
130 ];
131
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 require Message::DOM::XMLParserTemp;
139
140 print STDOUT qq[
141 <div id="parse-errors" class="section">
142 <h2>Parse Errors</h2>
143
144 <dl>];
145 push @nav, ['#parse-errors' => 'Parse Error'];
146
147 my $onerror = sub {
148 my $err = shift;
149 my $line = $err->location->line_number;
150 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 return 1;
154 };
155
156 open my $fh, '<', \($input->{s});
157 $doc = Message::DOM::XMLParserTemp->parse_byte_stream
158 ($fh => $dom, $onerror, charset => $input->{charset});
159
160 print STDOUT qq[</dl>
161 </div>
162
163 ];
164 print_source_string_section (\($input->{s}), $doc->input_encoding);
165 } else {
166 ## TODO: Change HTTP status code??
167 print STDOUT qq[
168 <div id="result-summary" class="section">
169 <p><em>Media type <code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> is not supported!</em></p>
170 </div>
171 ];
172 push @nav, ['#result-summary' => 'Result'];
173 }
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 push @nav, ['#document-tree' => 'Tree'];
182
183 print_document_tree ($el || $doc);
184
185 print STDOUT qq[
186 </div>
187
188 <div id="document-errors" class="section">
189 <h2>Document Errors</h2>
190
191 <dl>];
192 push @nav, ['#document-errors' => 'Document Error'];
193
194 require Whatpm::ContentChecker;
195 my $onerror = sub {
196 my %opt = @_;
197 my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}, $opt{node});
198 $type =~ tr/ /-/;
199 $type =~ s/\|/%7C/g;
200 $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]];
201 print STDOUT qq[<dt class="$cls">] . get_node_link ($opt{node}) .
202 qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n";
203 };
204
205 my $elements;
206 if ($el) {
207 $elements = Whatpm::ContentChecker->check_element ($el, $onerror);
208 } else {
209 $elements = Whatpm::ContentChecker->check_document ($doc, $onerror);
210 }
211
212 print STDOUT qq[</dl>
213 </div>
214 ];
215
216 if (@{$elements->{table}}) {
217 require JSON;
218
219 push @nav, ['#tables' => 'Tables'];
220 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 print STDOUT qq[<div class="section" id="table-$i"><h3>] .
235 get_node_link ($table_el) . q[</h3>];
236
237 ## TODO: Make |ContentChecker| return |form_table| result
238 ## so that this script don't have to run the algorithm twice.
239 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 $_->{is_header} = $_->{is_header} ? 1 : 0;
261 }
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
273 if (keys %{$elements->{id}}) {
274 push @nav, ['#identifiers' => 'IDs'];
275 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><code>@{[htescape $id]}</code></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 if (keys %{$elements->{term}}) {
291 push @nav, ['#terms' => 'Terms'];
292 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
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><code>@{[htescape $class]}</code></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 }
324
325 ## TODO: Show result
326 } 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
339 print STDOUT qq[
340 <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 </body>
348 </html>
349 ];
350
351 exit;
352
353 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 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 }
409 print STDOUT "</ol></div>";
410 } # print_input_string_section
411
412 sub print_document_tree ($) {
413 my $node = shift;
414 my $r = '<ol class="xoxo">';
415
416 my @node = ($node);
417 while (@node) {
418 my $child = shift @node;
419 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 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 '</code>'; ## ISSUE: case
430
431 if ($child->has_attributes) {
432 $r .= '<ul class="attributes">';
433 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }
434 @{$child->attributes}) {
435 $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case?
436 $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children
437 }
438 $r .= '</ul>';
439 }
440
441 if ($child->has_child_nodes) {
442 $r .= '<ol class="children">';
443 unshift @node, @{$child->child_nodes}, '</ol></li>';
444 } else {
445 $r .= '</li>';
446 }
447 } elsif ($nt == $child->TEXT_NODE) {
448 $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>';
449 } elsif ($nt == $child->CDATA_SECTION_NODE) {
450 $r .= qq'<li id="$node_id" class="tree-cdata"><code>&lt;[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]&gt;</code></li>';
451 } elsif ($nt == $child->COMMENT_NODE) {
452 $r .= qq'<li id="$node_id" class="tree-comment"><code>&lt;!--</code><q lang="">' . htescape ($child->data) . '</q><code>--&gt;</code></li>';
453 } elsif ($nt == $child->DOCUMENT_NODE) {
454 $r .= qq'<li id="$node_id" class="tree-document">Document';
455 $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 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 $r .= qq[</ul>];
468 if ($child->has_child_nodes) {
469 $r .= '<ol class="children">';
470 unshift @node, @{$child->child_nodes}, '</ol></li>';
471 }
472 } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {
473 $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 $r .= '</ul></li>';
478 } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {
479 $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 } else {
481 $r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error
482 }
483 }
484
485 $r .= '</ol>';
486 print STDOUT $r;
487 } # print_document_tree
488
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 @r = ('') unless @r;
505 $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 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 {
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, $node) = @_;
538 $type = $level . ':' . $type if defined $level;
539 my @arg;
540 {
541 if (defined $Msg->{$type}) {
542 my $msg = $Msg->{$type}->[1];
543 $msg =~ s{<var>\$([0-9]+)</var>}{
544 defined $arg[$1] ? htescape ($arg[$1]) : '(undef)';
545 }ge;
546 $msg =~ s{<var>{\@([A-Za-z0-9:_.-]+)}</var>}{
547 UNIVERSAL::can ($node, 'get_attribute_ns')
548 ? htescape ($node->get_attribute_ns (undef, $1)) : ''
549 }ge;
550 $msg =~ s{<var>{\@}</var>}{
551 UNIVERSAL::can ($node, 'value') ? htescape ($node->value) : ''
552 }ge;
553 return ($type, $Msg->{$type}->[0], $msg);
554 } elsif ($type =~ s/:([^:]*)$//) {
555 unshift @arg, $1;
556 redo;
557 }
558 }
559 return ($type, '', htescape ($_[0]));
560 } # get_text
561
562 }
563
564 sub get_input_document ($$) {
565 my ($http, $dom) = @_;
566
567 my $request_uri = $http->parameter ('uri');
568 my $r = {};
569 if (defined $request_uri and length $request_uri) {
570 my $uri = $dom->create_uri_reference ($request_uri);
571 unless ({
572 http => 1,
573 }->{lc $uri->uri_scheme}) {
574 return {uri => $request_uri, request_uri => $request_uri,
575 error_status_text => 'URI scheme not allowed'};
576 }
577
578 require Message::Util::HostPermit;
579 my $host_permit = new Message::Util::HostPermit;
580 $host_permit->add_rule (<<EOH);
581 Allow host=suika port=80
582 Deny host=suika
583 Allow host=suika.fam.cx port=80
584 Deny host=suika.fam.cx
585 Deny host=localhost
586 Deny host=*.localdomain
587 Deny ipv4=0.0.0.0/8
588 Deny ipv4=10.0.0.0/8
589 Deny ipv4=127.0.0.0/8
590 Deny ipv4=169.254.0.0/16
591 Deny ipv4=172.0.0.0/11
592 Deny ipv4=192.0.2.0/24
593 Deny ipv4=192.88.99.0/24
594 Deny ipv4=192.168.0.0/16
595 Deny ipv4=198.18.0.0/15
596 Deny ipv4=224.0.0.0/4
597 Deny ipv4=255.255.255.255/32
598 Deny ipv6=0::0/0
599 Allow host=*
600 EOH
601 unless ($host_permit->check ($uri->uri_host, $uri->uri_port || 80)) {
602 return {uri => $request_uri, request_uri => $request_uri,
603 error_status_text => 'Connection to the host is forbidden'};
604 }
605
606 require LWP::UserAgent;
607 my $ua = WDCC::LWPUA->new;
608 $ua->{wdcc_dom} = $dom;
609 $ua->{wdcc_host_permit} = $host_permit;
610 $ua->agent ('Mozilla'); ## TODO: for now.
611 $ua->parse_head (0);
612 $ua->protocols_allowed ([qw/http/]);
613 $ua->max_size (1000_000);
614 my $req = HTTP::Request->new (GET => $request_uri);
615 my $res = $ua->request ($req);
616 if ($res->is_success or $http->parameter ('error-page')) {
617 $r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and <base>. ## TODO: Use our own code!
618 $r->{uri} = $res->request->uri;
619 $r->{request_uri} = $request_uri;
620
621 ## TODO: More strict parsing...
622 my $ct = $res->header ('Content-Type');
623 if (defined $ct and $ct =~ m#^([0-9A-Za-z._+-]+/[0-9A-Za-z._+-]+)#) {
624 $r->{media_type} = lc $1;
625 }
626 if (defined $ct and $ct =~ /;\s*charset\s*=\s*"?(\S+)"?/i) {
627 $r->{charset} = lc $1;
628 $r->{charset} =~ tr/\\//d;
629 }
630
631 my $input_charset = $http->parameter ('charset');
632 if (defined $input_charset and length $input_charset) {
633 $r->{charset_overridden}
634 = (not defined $r->{charset} or $r->{charset} ne $input_charset);
635 $r->{charset} = $input_charset;
636 }
637
638 $r->{s} = ''.$res->content;
639 } else {
640 $r->{uri} = $res->request->uri;
641 $r->{request_uri} = $request_uri;
642 $r->{error_status_text} = $res->status_line;
643 }
644
645 $r->{header_field} = [];
646 $res->scan (sub {
647 push @{$r->{header_field}}, [$_[0], $_[1]];
648 });
649 $r->{header_status_code} = $res->code;
650 $r->{header_status_text} = $res->message;
651 } else {
652 $r->{s} = ''.$http->parameter ('s');
653 $r->{uri} = q<thismessage:/>;
654 $r->{request_uri} = q<thismessage:/>;
655 $r->{base_uri} = q<thismessage:/>;
656 $r->{charset} = ''.$http->parameter ('_charset_');
657 $r->{charset} =~ s/\s+//g;
658 $r->{charset} = 'utf-8' if $r->{charset} eq '';
659 $r->{header_field} = [];
660 }
661
662 my $input_format = $http->parameter ('i');
663 if (defined $input_format and length $input_format) {
664 $r->{media_type_overridden}
665 = (not defined $r->{media_type} or $input_format ne $r->{media_type});
666 $r->{media_type} = $input_format;
667 }
668 if (defined $r->{s} and not defined $r->{media_type}) {
669 $r->{media_type} = 'text/html';
670 $r->{media_type_overridden} = 1;
671 }
672
673 if ($r->{media_type} eq 'text/xml') {
674 unless (defined $r->{charset}) {
675 $r->{charset} = 'us-ascii';
676 } elsif ($r->{charset_overridden} and $r->{charset} eq 'us-ascii') {
677 $r->{charset_overridden} = 0;
678 }
679 }
680
681 if (length $r->{s} > 1000_000) {
682 $r->{error_status_text} = 'Entity-body too large';
683 delete $r->{s};
684 return $r;
685 }
686
687 return $r;
688 } # get_input_document
689
690 package WDCC::LWPUA;
691 BEGIN { push our @ISA, 'LWP::UserAgent'; }
692
693 sub redirect_ok {
694 my $ua = shift;
695 unless ($ua->SUPER::redirect_ok (@_)) {
696 return 0;
697 }
698
699 my $uris = $_[1]->header ('Location');
700 return 0 unless $uris;
701 my $uri = $ua->{wdcc_dom}->create_uri_reference ($uris);
702 unless ({
703 http => 1,
704 }->{lc $uri->uri_scheme}) {
705 return 0;
706 }
707 unless ($ua->{wdcc_host_permit}->check ($uri->uri_host, $uri->uri_port || 80)) {
708 return 0;
709 }
710 return 1;
711 } # redirect_ok
712
713 =head1 AUTHOR
714
715 Wakaba <w@suika.fam.cx>.
716
717 =head1 LICENSE
718
719 Copyright 2007 Wakaba <w@suika.fam.cx>
720
721 This library is free software; you can redistribute it
722 and/or modify it under the same terms as Perl itself.
723
724 =cut
725
726 ## $Date: 2007/07/17 14:28:20 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24