/[pub]/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.8 by wakaba, Sun Jul 1 10:02:24 2007 UTC revision 1.18 by wakaba, Sun Sep 2 08:40:49 2007 UTC
# Line 2  Line 2 
2  use strict;  use strict;
3    
4  use lib qw[/home/httpd/html/www/markup/html/whatpm  use lib qw[/home/httpd/html/www/markup/html/whatpm
5             /home/wakaba/work/manakai/lib             /home/wakaba/work/manakai2/lib];
            /home/wakaba/public_html/-temp/wiki/lib];  
6  use CGI::Carp qw[fatalsToBrowser];  use CGI::Carp qw[fatalsToBrowser];
7  use Scalar::Util qw[refaddr];  use Scalar::Util qw[refaddr];
8    use Time::HiRes qw/time/;
 use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module  
9    
10  sub htescape ($) {  sub htescape ($) {
11    my $s = $_[0];    my $s = $_[0];
# Line 15  sub htescape ($) { Line 13  sub htescape ($) {
13    $s =~ s/</&lt;/g;    $s =~ s/</&lt;/g;
14    $s =~ s/>/&gt;/g;    $s =~ s/>/&gt;/g;
15    $s =~ s/"/&quot;/g;    $s =~ s/"/&quot;/g;
16    $s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf '<var>U+%04X</var>', ord $1!ge;    $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;    return $s;
20  } # htescape  } # htescape
21    
22  my $http = SuikaWiki::Input::HTTP->new;    use Message::CGI::HTTP;
23      my $http = Message::CGI::HTTP->new;
 ## TODO: _charset_  
24    
25    if ($http->meta_variable ('PATH_INFO') ne '/') {    if ($http->get_meta_variable ('PATH_INFO') ne '/') {
26      print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400";      print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400";
27      exit;      exit;
28    }    }
29    
30    my $input_format = $http->parameter ('i') || 'text/html';    binmode STDOUT, ':utf8';
31    my $inner_html_element = $http->parameter ('e');    $| = 1;
32    my $input_uri = 'thismessage:/';  
33      require Message::DOM::DOMImplementation;
34    my $s = $http->parameter ('s');    my $dom = Message::DOM::DOMImplementation->new;
   if (length $s > 1000_000) {  
     print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long";  
     exit;  
   }  
35    
36    load_text_catalog ('en'); ## TODO: conneg    load_text_catalog ('en'); ## TODO: conneg
37    
# Line 50  my $http = SuikaWiki::Input::HTTP->new; Line 45  my $http = SuikaWiki::Input::HTTP->new;
45  <link rel="stylesheet" href="../cc-style.css" type="text/css">  <link rel="stylesheet" href="../cc-style.css" type="text/css">
46  </head>  </head>
47  <body>  <body>
48  <h1>Web Document Conformance Checker (<em>beta</em>)</h1>  <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">  <div id="document-info" class="section">
60  <dl>  <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>  <dt>Document URI</dt>
64      <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>&gt;</code></dd>      <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input->{uri}]}">@{[htescape $input->{uri}]}</a>&gt;</code></dd>
 <dt>Internet Media Type</dt>  
     <dd><code class="MIME" lang="en">@{[htescape $input_format]}</code></dd>  
65  ]; # no </dl> yet  ]; # no </dl> yet
66    push @nav, ['#document-info' => 'Information'];    push @nav, ['#document-info' => 'Information'];
67    
68    require Message::DOM::DOMImplementation;  if (defined $input->{s}) {
69    my $dom = Message::DOM::DOMImplementation->____new;    $char_length = length $input->{s};
   my $doc;  
   my $el;  
   
   if ($input_format eq 'text/html') {  
     require Encode;  
     require Whatpm::HTML;  
       
     $s = Encode::decode ('utf-8', $s);  
70    
71      print STDOUT qq[    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>  <dt>Character Encoding</dt>
78      <dd>(none)</dd>      <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>  </dl>
83  </div>  </div>
84    ];
85    
86  <div id="source-string" class="section">    print_http_header_section ($input);
87  <h2>Document Source</h2>  
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      push @nav, ['#source-string' => 'Source'];    for (@nav) {
128      print_source_string (\$s);      print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>];
129      print STDOUT qq[    }
130  </div>    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">  <div id="parse-errors" class="section">
192  <h2>Parse Errors</h2>  <h2>Parse Errors</h2>
193    
194  <dl>  <dl>];
 ];  
195    push @nav, ['#parse-errors' => 'Parse Error'];    push @nav, ['#parse-errors' => 'Parse Error'];
196    
197    my $onerror = sub {    my $onerror = sub {
198      my (%opt) = @_;      my (%opt) = @_;
199      my ($cls, $msg) = get_text ($opt{type}, $opt{level});      my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level});
200      if ($opt{column} > 0) {      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];        print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n];
202      } else {      } else {
203        $opt{line} = $opt{line} - 1 || 1;        $opt{line} = $opt{line} - 1 || 1;
204        print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n];        print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n];
205      }      }
206      $opt{type} =~ tr/ /-/;      $type =~ tr/ /-/;
207      $opt{type} =~ s/\|/%7C/g;      $type =~ s/\|/%7C/g;
208      $msg .= qq[ [<a href="../error-description#$opt{type}">Description</a>]];      $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]];
209      print STDOUT qq[<dd class="$cls">$msg</dd>\n];      print STDOUT qq[<dd class="$cls">$msg</dd>\n];
210    };    };
211    
212    $doc = $dom->create_document;    my $doc = $dom->create_document;
213      my $el;
214      $time1 = time;
215    if (defined $inner_html_element and length $inner_html_element) {    if (defined $inner_html_element and length $inner_html_element) {
216      $el = $doc->create_element_ns      $el = $doc->create_element_ns
217          ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]);          ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]);
218      Whatpm::HTML->set_inner_html ($el, $s, $onerror);      Whatpm::HTML->set_inner_html ($el, $t, $onerror);
219    } else {    } else {
220      Whatpm::HTML->parse_string ($s => $doc, $onerror);      Whatpm::HTML->parse_string ($t => $doc, $onerror);
221    }    }
222      $time{parse} = time - $time1;
223    
224    print STDOUT qq[    print STDOUT qq[</dl></div>];
 </dl>  
 </div>  
 ];  
   } elsif ($input_format eq 'application/xhtml+xml') {  
     require Message::DOM::XMLParserTemp;  
     require Encode;  
       
     my $t = Encode::decode ('utf-8', $s);  
225    
226      print STDOUT qq[    return ($doc, $el);
227  <dt>Character Encoding</dt>  } # print_syntax_error_html_section
     <dd>(none)</dd>  
 </dl>  
 </div>  
   
 <div id="source-string" class="section">  
 <h2>Document Source</h2>  
 ];  
     push @nav, ['#source-string' => 'Source'];  
     print_source_string (\$t);  
     print STDOUT qq[  
 </div>  
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">  <div id="parse-errors" class="section">
236  <h2>Parse Errors</h2>  <h2>Parse Errors</h2>
237    
# Line 156  my $http = SuikaWiki::Input::HTTP->new; Line 247  my $http = SuikaWiki::Input::HTTP->new;
247      return 1;      return 1;
248    };    };
249    
250    open my $fh, '<', \$s;    my $time1 = time;
251    $doc = Message::DOM::XMLParserTemp->parse_byte_stream    open my $fh, '<', \($input->{s});
252        ($fh => $dom, $onerror, charset => 'utf-8');    my $doc = Message::DOM::XMLParserTemp->parse_byte_stream
253          ($fh => $dom, $onerror, charset => $input->{charset});
254      print STDOUT qq[</dl>    $time{parse_xml} = time - $time1;
255  </div>  
256  ];    print STDOUT qq[</dl></div>];
257    } else {  
258      print STDOUT qq[    return ($doc, undef);
259  </dl>  } # print_syntax_error_xml_section
260  </div>  
261    sub print_source_string_section ($$) {
262  <div id="result-summary" class="section">    require Encode;
263  <p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p>    my $enc = Encode::find_encoding ($_[1]); ## TODO: charset name -> Perl name
264  </div>    return unless $enc;
265  ];  
266      push @nav, ['#result-summary' => 'Result'];    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    if (defined $doc or defined $el) {  <h2>Document Source</h2>
271      print STDOUT qq[  <ol lang="">\n];
 <div id="document-tree" class="section">  
 <h2>Document Tree</h2>  
 ];  
     push @nav, ['#document-tree' => 'Tree'];  
   
     print_document_tree ($el || $doc);  
   
     print STDOUT qq[  
 </div>  
   
 <div id="document-errors" class="section">  
 <h2>Document Errors</h2>  
   
 <dl>];  
     push @nav, ['#document-errors' => 'Document Error'];  
   
     require Whatpm::ContentChecker;  
     my $onerror = sub {  
       my %opt = @_;  
       my ($cls, $msg) = get_text ($opt{type}, $opt{level});  
       $opt{type} = $opt{level} . ':' . $opt{type} if defined $opt{level};  
       $opt{type} =~ tr/ /-/;  
       $opt{type} =~ s/\|/%7C/g;  
       $msg .= qq[ [<a href="../error-description#$opt{type}">Description</a>]];  
       print STDOUT qq[<dt class="$cls">] . get_node_link ($opt{node}) .  
           qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n";  
     };  
   
     my $elements;  
     if ($el) {  
       $elements = Whatpm::ContentChecker->check_element ($el, $onerror);  
     } else {  
       $elements = Whatpm::ContentChecker->check_document ($doc, $onerror);  
     }  
   
     print STDOUT qq[</dl>  
 </div>  
 ];  
   
     if (@{$elements->{table}}) {  
       require JSON;  
   
       print STDOUT qq[  
 <div id="tables" class="section">  
 <h2>Tables</h2>  
   
 <!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->  
 <script src="../table-script.js" type="text/javascript"></script>  
 <noscript>  
 <p><em>Structure of tables are visualized here if scripting is enabled.</em></p>  
 </noscript>  
 ];  
   
       my $i = 0;  
       for my $table_el (@{$elements->{table}}) {  
         $i++;  
         print STDOUT qq[<div class="section" id="table-$i"><h3>] .  
             get_node_link ($table_el) . q[</h3>];  
           
         my $table = Whatpm::HTMLTable->form_table ($table_el);  
           
         for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) {  
           next unless $_;  
           delete $_->{element};  
         }  
           
         for (@{$table->{row_group}}) {  
           next unless $_;  
           next unless $_->{element};  
           $_->{type} = $_->{element}->manakai_local_name;  
           delete $_->{element};  
         }  
           
         for (@{$table->{cell}}) {  
           next unless $_;  
           for (@{$_}) {  
             next unless $_;  
             for (@$_) {  
               $_->{id} = refaddr $_->{element} if defined $_->{element};  
               delete $_->{element};  
             }  
           }  
         }  
           
         print STDOUT '</div><script type="text/javascript">tableToCanvas (';  
         print STDOUT JSON::objToJson ($table);  
         print STDOUT qq[, document.getElementById ('table-$i'));</script>];  
       }  
       
       print STDOUT qq[</div>];  
     }  
   
     if (keys %{$elements->{term}}) {  
       print STDOUT qq[  
 <div id="terms" class="section">  
 <h2>Terms</h2>  
   
 <dl>  
 ];  
       for my $term (sort {$a cmp $b} keys %{$elements->{term}}) {  
         print STDOUT qq[<dt>@{[htescape $term]}</dt>];  
         for (@{$elements->{term}->{$term}}) {  
           print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>];  
         }  
       }  
       print STDOUT qq[</dl></div>];  
     }  
   }  
   
   ## TODO: Show result  
   
   print STDOUT qq[  
 <ul class="navigation" id="nav-items">  
 ];  
   for (@nav) {  
     print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>];  
   }  
   print STDOUT qq[  
 </ul>  
 </body>  
 </html>  
 ];  
   
 exit;  
   
 sub print_source_string ($) {  
   my $s = $_[0];  
   my $i = 1;  
   print STDOUT qq[<ol lang="">\n];  
272    if (length $$s) {    if (length $$s) {
273      while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) {      while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) {
274        print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n";        print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n";
# Line 318  sub print_source_string ($) { Line 280  sub print_source_string ($) {
280    } else {    } else {
281      print STDOUT q[<li id="line-1"></li>];      print STDOUT q[<li id="line-1"></li>];
282    }    }
283    print STDOUT "</ol>";    print STDOUT "</ol></div>";
284  } # print_input_string  } # print_input_string_section
285    
286  sub print_document_tree ($) {  sub print_document_tree ($) {
287    my $node = shift;    my $node = shift;
# Line 367  sub print_document_tree ($) { Line 329  sub print_document_tree ($) {
329        $r .= qq[<ul class="attributes">];        $r .= qq[<ul class="attributes">];
330        $r .= qq[<li>@{[scalar get_text ('manakaiIsHTML:'.($child->manakai_is_html?1:0))]}</li>];        $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>];        $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>];        $r .= qq[</ul>];
342        if ($child->has_child_nodes) {        if ($child->has_child_nodes) {
343          $r .= '<ol class="children">';          $r .= '<ol class="children">';
# Line 389  sub print_document_tree ($) { Line 360  sub print_document_tree ($) {
360    print STDOUT $r;    print STDOUT $r;
361  } # print_document_tree  } # 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 ($) {  sub get_node_path ($) {
547    my $node = shift;    my $node = shift;
548    my @r;    my @r;
# Line 404  sub get_node_path ($) { Line 558  sub get_node_path ($) {
558        $rs = '"' . $node->data . '"';        $rs = '"' . $node->data . '"';
559        $node = $node->parent_node;        $node = $node->parent_node;
560      } elsif ($node->node_type == 9) {      } elsif ($node->node_type == 9) {
561          @r = ('') unless @r;
562        $rs = '';        $rs = '';
563        $node = $node->parent_node;        $node = $node->parent_node;
564      } else {      } else {
# Line 436  sub load_text_catalog ($) { Line 591  sub load_text_catalog ($) {
591  } # load_text_catalog  } # load_text_catalog
592    
593  sub get_text ($) {  sub get_text ($) {
594    my ($type, $level) = @_;    my ($type, $level, $node) = @_;
595    $type = $level . ':' . $type if defined $level;    $type = $level . ':' . $type if defined $level;
596    my @arg;    my @arg;
597    {    {
598      if (defined $Msg->{$type}) {      if (defined $Msg->{$type}) {
599        my $msg = $Msg->{$type}->[1];        my $msg = $Msg->{$type}->[1];
600        $msg =~ s/\$([0-9]+)/defined $arg[$1] ? htescape ($arg[$1]) : '(undef)'/ge;        $msg =~ s{<var>\$([0-9]+)</var>}{
601        return ($Msg->{$type}->[0], $msg);          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/:([^:]*)$//) {      } elsif ($type =~ s/:([^:]*)$//) {
622        unshift @arg, $1;        unshift @arg, $1;
623        redo;        redo;
624      }      }
625    }    }
626    return ('', htescape ($_[0]));    return ($type, '', htescape ($_[0]));
627  } # get_text  } # 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  =head1 AUTHOR
782    
783  Wakaba <w@suika.fam.cx>.  Wakaba <w@suika.fam.cx>.

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.18

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24