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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.12

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24