/[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.53 by wakaba, Sun Jul 20 14:58:24 2008 UTC revision 1.56 by wakaba, Mon Jul 21 08:39:12 2008 UTC
# Line 44  my $out; Line 44  my $out;
44  ]);  ]);
45    
46    my $input = get_input_document ($http, $dom);    my $input = get_input_document ($http, $dom);
47    
48    $out->input ($input);    $out->input ($input);
49    $out->unset_flush;    $out->unset_flush;
50    
51    my $char_length = 0;    my $result = WebHACC::Result->new;
52      $result->output ($out);
53      $result->{conforming_min} = 1;
54      $result->{conforming_max} = 1;
55    
56    $out->start_section (id => 'document-info', title => 'Information');    $out->html ('<script src="../cc-script.js"></script>');
   $out->html (qq[<dl>  
 <dt>Request URL</dt>  
     <dd>]);  
   $out->url ($input->{request_uri});  
   $out->html (q[<dt>Document URL<!-- HTML5 document's address? -->  
     <dd>]);  
   $out->url ($input->{uri}, id => 'anchor-document-url');  
   $out->html (q[  
     <script>  
       document.title = '<'  
           + document.getElementById ('anchor-document-url').href + '> \\u2014 '  
           + document.title;  
     </script>]);  
   ## NOTE: no </dl> yet  
   
   if (defined $input->{s}) {  
     $char_length = length $input->{s};  
   
     $out->html (qq[<dt>Base URI<dd>]);  
     $out->url ($input->{base_uri});  
     $out->html (qq[<dt>Internet Media Type</dt>  
     <dd><code class="MIME" lang="en">]);  
     $out->text ($input->{media_type});  
     $out->html (qq[</code> ]);  
     if ($input->{media_type_overridden}) {  
       $out->html ('<em>(overridden)</em>');  
     } elsif (defined $input->{official_type}) {  
       if ($input->{media_type} eq $input->{official_type}) {  
         #  
       } else {  
         $out->html ('<em>(sniffed; official type is: <code class=MIME lang=en>');  
         $out->text ($input->{official_type});  
         $out->html ('</code>)');  
       }  
     } else {  
       $out->html ('<em>(sniffed)</em>');  
     }  
     $out->html (q[<dt>Character Encoding<dd>]);  
     if (defined $input->{charset}) {  
       $out->html ('<code class="charset" lang="en">');  
       $out->text ($input->{charset});  
       $out->html ('</code>');  
     } else {  
       $out->text ('(none)');  
     }  
     $out->html (' <em>overridden</em>') if $input->{charset_overridden};  
     $out->html (qq[  
 <dt>Length</dt>  
     <dd>$char_length byte@{[$char_length == 1 ? '' : 's']}</dd>  
 </dl>  
57    
58  <script src="../cc-script.js"></script>    check_and_print ($input => $result => $out);
59  ]);    
60      $out->end_section;    $result->generate_result_section;
   
     my $result = WebHACC::Result->new;  
     $result->{conforming_min} = 1;  
     $result->{conforming_max} = 1;  
     check_and_print ($input => $result => $out);  
     print_result_section ($result);  
   } else {  
     $out->html ('</dl>');  
     $out->end_section;  
     print_result_input_error_section ($input);  
   }  
61    
62    $out->nav_list;    $out->nav_list;
63    
64    exit;    exit;
65  }  }
66    
 sub add_error ($$$) {  
   my ($layer, $err, $result) = @_;  
   if (defined $err->{level}) {  
     if ($err->{level} eq 's') {  
       $result->{$layer}->{should}++;  
       $result->{$layer}->{score_min} -= 2;  
       $result->{conforming_min} = 0;  
     } elsif ($err->{level} eq 'w' or $err->{level} eq 'g') {  
       $result->{$layer}->{warning}++;  
     } elsif ($err->{level} eq 'u' or $err->{level} eq 'unsupported') {  
       $result->{$layer}->{unsupported}++;  
       $result->{unsupported} = 1;  
     } elsif ($err->{level} eq 'i') {  
       #  
     } else {  
       $result->{$layer}->{must}++;  
       $result->{$layer}->{score_max} -= 2;  
       $result->{$layer}->{score_min} -= 2;  
       $result->{conforming_min} = 0;  
       $result->{conforming_max} = 0;  
     }  
   } else {  
     $result->{$layer}->{must}++;  
     $result->{$layer}->{score_max} -= 2;  
     $result->{$layer}->{score_min} -= 2;  
     $result->{conforming_min} = 0;  
     $result->{conforming_max} = 0;  
   }  
 } # add_error  
   
67  sub check_and_print ($$$) {  sub check_and_print ($$$) {
68    my ($input, $result, $out) = @_;    my ($input, $result, $out) = @_;
69    my $original_input = $out->input;    my $original_input = $out->input;
70    $out->input ($input);    $out->input ($input);
71    
72    print_http_header_section ($input, $result);    $input->generate_info_section ($result);
73    
74    my @subdoc;    $input->generate_transfer_sections ($result);
75    
76      unless (defined $input->{s}) {
77        $result->{conforming_min} = 0;
78        return;
79      }
80    
81    my $checker_class = {    my $checker_class = {
82      'text/cache-manifest' => 'WebHACC::Language::CacheManifest',      'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
# Line 191  sub check_and_print ($$$) { Line 109  sub check_and_print ($$$) {
109    $checker->generate_syntax_error_section;    $checker->generate_syntax_error_section;
110    $checker->generate_source_string_section;    $checker->generate_source_string_section;
111    
112      my @subdoc;
113    $checker->onsubdoc (sub {    $checker->onsubdoc (sub {
114      push @subdoc, shift;      push @subdoc, shift;
115    });    });
# Line 203  sub check_and_print ($$$) { Line 122  sub check_and_print ($$$) {
122    
123    if (defined $doc or defined $el) {    if (defined $doc or defined $el) {
124    
     print_table_section ($input, $elements->{table}) if @{$elements->{table}};  
125      print_listing_section ({      print_listing_section ({
126        id => 'identifiers', label => 'IDs', heading => 'Identifiers',        id => 'identifiers', label => 'IDs', heading => 'Identifiers',
127      }, $input, $elements->{id}) if keys %{$elements->{id}};      }, $input, $elements->{id}) if keys %{$elements->{id}};
# Line 221  sub check_and_print ($$$) { Line 139  sub check_and_print ($$$) {
139    
140    my $id_prefix = 0;    my $id_prefix = 0;
141    for my $_subinput (@subdoc) {    for my $_subinput (@subdoc) {
142      my $subinput = WebHACC::Input->new;      my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix);
143      $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;      $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
     $subinput->id_prefix ('subdoc-' . ++$id_prefix);  
     $subinput->nested (1);  
144      $subinput->{base_uri} = $subinput->{container_node}->base_uri      $subinput->{base_uri} = $subinput->{container_node}->base_uri
145          unless defined $subinput->{base_uri};          unless defined $subinput->{base_uri};
146      my $ebaseuri = htescape ($subinput->{base_uri});      $subinput->{parent_input} = $input;
     $out->start_section (id => $subinput->id_prefix,  
                          title => qq[Subdocument #$id_prefix]);  
     print STDOUT qq[  
       <dl>  
       <dt>Internet Media Type</dt>  
         <dd><code class="MIME" lang="en">@{[htescape $subinput->{media_type}]}</code>  
       <dt>Container Node</dt>  
         <dd>@{[get_node_link ($input, $subinput->{container_node})]}</dd>  
       <dt>Base <abbr title="Uniform Resource Identifiers">URI</abbr></dt>  
         <dd><code class=URI>&lt;<a href="$ebaseuri">$ebaseuri</a>></code></dd>  
       </dl>];                
147    
148      $subinput->{id_prefix} .= '-';      $subinput->start_section ($result);
149      check_and_print ($subinput => $result => $out);      check_and_print ($subinput => $result => $out);
150        $subinput->end_section ($result);
     $out->end_section;  
151    }    }
152    
153    $out->input ($original_input);    $out->input ($original_input);
154  } # check_and_print  } # check_and_print
155    
 sub print_http_header_section ($$) {  
   my ($input, $result) = @_;  
   return unless defined $input->{header_status_code} or  
       defined $input->{header_status_text} or  
       @{$input->{header_field} or []};  
     
   $out->start_section (id => 'source-header', title => 'HTTP Header');  
   print STDOUT qq[<p><strong>Note</strong>: Due to the limitation of the  
 network library in use, the content of this section might  
 not be the real header.</p>  
   
 <table><tbody>  
 ];  
   
   if (defined $input->{header_status_code}) {  
     print STDOUT qq[<tr><th scope="row">Status code</th>];  
     print STDOUT qq[<td>];  
     $out->code ($input->{header_status_code});  
   }  
   if (defined $input->{header_status_text}) {  
     print STDOUT qq[<tr><th scope="row">Status text</th>];  
     print STDOUT qq[<td>];  
     $out->code ($input->{header_status_text});  
   }  
     
   for (@{$input->{header_field}}) {  
     print STDOUT qq[<tr><th scope="row">];  
     $out->code ($_->[0]);  
     print STDOUT qq[<td>];  
     $out->code ($_->[1]);  
   }  
   
   print STDOUT qq[</tbody></table>];  
   
   $out->end_section;  
 } # print_http_header_section  
   
 sub print_table_section ($$) {  
   my ($input, $tables) = @_;  
     
 #  push @nav, [qq[#$input->{id_prefix}tables] => 'Tables']  
 #      unless $input->{nested};  
   print STDOUT qq[  
 <div id="$input->{id_prefix}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>  
 ];  
     
   require JSON;  
     
   my $i = 0;  
   for my $table (@$tables) {  
     $i++;  
     print STDOUT qq[<div class="section" id="$input->{id_prefix}table-$i"><h3>] .  
         get_node_link ($input, $table->{element}) . q[</h3>];  
   
     delete $table->{element};  
   
     for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption},  
          @{$table->{row}}) {  
       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};  
           $_->{is_header} = $_->{is_header} ? 1 : 0;  
         }  
       }  
     }  
           
     print STDOUT '</div><script type="text/javascript">tableToCanvas (';  
     print STDOUT JSON::objToJson ($table);  
     print STDOUT qq[, document.getElementById ('$input->{id_prefix}table-$i')];  
     print STDOUT qq[, '$input->{id_prefix}');</script>];  
   }  
     
   print STDOUT qq[</div>];  
 } # print_table_section  
   
 sub print_listing_section ($$$) {  
   my ($opt, $input, $ids) = @_;  
     
 #  push @nav, ['#' . $input->{id_prefix} . $opt->{id} => $opt->{label}]  
 #      unless $input->{nested};  
   print STDOUT qq[  
 <div id="$input->{id_prefix}$opt->{id}" class="section">  
 <h2>$opt->{heading}</h2>  
   
 <dl>  
 ];  
   for my $id (sort {$a cmp $b} keys %$ids) {  
     print STDOUT qq[<dt><code>@{[htescape $id]}</code></dt>];  
     for (@{$ids->{$id}}) {  
       print STDOUT qq[<dd>].get_node_link ($input, $_).qq[</dd>];  
     }  
   }  
   print STDOUT qq[</dl></div>];  
 } # print_listing_section  
   
   
 sub print_rdf_section ($$$) {  
   my ($input, $rdfs) = @_;  
     
 #  push @nav, ['#' . $input->{id_prefix} . 'rdf' => 'RDF']  
 #      unless $input->{nested};  
   print STDOUT qq[  
 <div id="$input->{id_prefix}rdf" class="section">  
 <h2>RDF Triples</h2>  
   
 <dl>];  
   my $i = 0;  
   for my $rdf (@$rdfs) {  
     print STDOUT qq[<dt id="$input->{id_prefix}rdf-@{[$i++]}">];  
     print STDOUT get_node_link ($input, $rdf->[0]);  
     print STDOUT qq[<dd><dl>];  
     for my $triple (@{$rdf->[1]}) {  
       print STDOUT '<dt>' . get_node_link ($input, $triple->[0]) . '<dd>';  
       print STDOUT get_rdf_resource_html ($triple->[1]);  
       print STDOUT ' ';  
       print STDOUT get_rdf_resource_html ($triple->[2]);  
       print STDOUT ' ';  
       print STDOUT get_rdf_resource_html ($triple->[3]);  
     }  
     print STDOUT qq[</dl>];  
   }  
   print STDOUT qq[</dl></div>];  
 } # print_rdf_section  
   
 sub get_rdf_resource_html ($) {  
   my $resource = shift;  
   if (defined $resource->{uri}) {  
     my $euri = htescape ($resource->{uri});  
     return '<code class=uri>&lt;<a href="' . $euri . '">' . $euri .  
         '</a>></code>';  
   } elsif (defined $resource->{bnodeid}) {  
     return htescape ('_:' . $resource->{bnodeid});  
   } elsif ($resource->{nodes}) {  
     return '(rdf:XMLLiteral)';  
   } elsif (defined $resource->{value}) {  
     my $elang = htescape (defined $resource->{language}  
                               ? $resource->{language} : '');  
     my $r = qq[<q lang="$elang">] . htescape ($resource->{value}) . '</q>';  
     if (defined $resource->{datatype}) {  
       my $euri = htescape ($resource->{datatype});  
       $r .= '^^<code class=uri>&lt;<a href="' . $euri . '">' . $euri .  
           '</a>></code>';  
     } elsif (length $resource->{language}) {  
       $r .= '@' . htescape ($resource->{language});  
     }  
     return $r;  
   } else {  
     return '??';  
   }  
 } # get_rdf_resource_html  
   
 sub print_result_section ($) {  
   my $result = shift;  
   
   $out->start_section (id => 'result-summary',  
                        title => 'Result');  
   
   if ($result->{unsupported} and $result->{conforming_max}) {    
     print STDOUT qq[<p class=uncertain id=result-para>The conformance  
         checker cannot decide whether the document is conforming or  
         not, since the document contains one or more unsupported  
         features.  The document might or might not be conforming.</p>];  
   } elsif ($result->{conforming_min}) {  
     print STDOUT qq[<p class=PASS id=result-para>No conformance-error is  
         found in this document.</p>];  
   } elsif ($result->{conforming_max}) {  
     print STDOUT qq[<p class=SEE-RESULT id=result-para>This document  
         is <strong>likely <em>non</em>-conforming</strong>, but in rare case  
         it might be conforming.</p>];  
   } else {  
     print STDOUT qq[<p class=FAIL id=result-para>This document is  
         <strong><em>non</em>-conforming</strong>.</p>];  
   }  
   
   print STDOUT qq[<table>  
 <colgroup><col><colgroup><col><col><col><colgroup><col>  
 <thead>  
 <tr><th scope=col></th>  
 <th scope=col><a href="../error-description#level-m"><em class=rfc2119>MUST</em>‐level  
 Errors</a></th>  
 <th scope=col><a href="../error-description#level-s"><em class=rfc2119>SHOULD</em>‐level  
 Errors</a></th>  
 <th scope=col><a href="../error-description#level-w">Warnings</a></th>  
 <th scope=col>Score</th></tr></thead><tbody>];  
   
   my $must_error = 0;  
   my $should_error = 0;  
   my $warning = 0;  
   my $score_min = 0;  
   my $score_max = 0;  
   my $score_base = 20;  
   my $score_unit = $score_base / 100;  
   for (  
     [Transfer => 'transfer', ''],  
     [Character => 'char', ''],  
     [Syntax => 'syntax', '#parse-errors'],  
     [Structure => 'structure', '#document-errors'],  
   ) {  
     $must_error += ($result->{$_->[1]}->{must} += 0);  
     $should_error += ($result->{$_->[1]}->{should} += 0);  
     $warning += ($result->{$_->[1]}->{warning} += 0);  
     $score_min += (($result->{$_->[1]}->{score_min} *= $score_unit) += $score_base);  
     $score_max += (($result->{$_->[1]}->{score_max} *= $score_unit) += $score_base);  
   
     my $uncertain = $result->{$_->[1]}->{unsupported} ? '?' : '';  
     my $label = $_->[0];  
     if ($result->{$_->[1]}->{must} or  
         $result->{$_->[1]}->{should} or  
         $result->{$_->[1]}->{warning} or  
         $result->{$_->[1]}->{unsupported}) {  
       $label = qq[<a href="$_->[2]">$label</a>];  
     }  
   
     print STDOUT qq[<tr class="@{[$uncertain ? 'uncertain' : '']}"><th scope=row>$label</th><td class="@{[$result->{$_->[1]}->{must} ? 'FAIL' : '']}">$result->{$_->[1]}->{must}$uncertain</td><td class="@{[$result->{$_->[1]}->{should} ? 'SEE-RESULT' : '']}">$result->{$_->[1]}->{should}$uncertain</td><td>$result->{$_->[1]}->{warning}$uncertain</td>];  
     if ($uncertain) {  
       print qq[<td class="@{[$result->{$_->[1]}->{must} ? 'FAIL' : $result->{$_->[1]}->{should} ? 'SEE-RESULT' : '']}">&#x2212;&#x221E;..$result->{$_->[1]}->{score_max}];  
     } elsif ($result->{$_->[1]}->{score_min} != $result->{$_->[1]}->{score_max}) {  
       print qq[<td class="@{[$result->{$_->[1]}->{must} ? 'FAIL' : 'SEE-RESULT']}">$result->{$_->[1]}->{score_min}..$result->{$_->[1]}->{score_max}];  
     } else {  
       print qq[<td class="@{[$result->{$_->[1]}->{must} ? 'FAIL' : '']}">$result->{$_->[1]}->{score_min}];  
     }  
     print qq[ / 20];  
   }  
   
   $score_max += $score_base;  
   
   print STDOUT qq[  
 <tr class=uncertain><th scope=row>Semantics</th><td>0?</td><td>0?</td><td>0?</td><td>&#x2212;&#x221E;..$score_base / 20  
 </tbody>  
 <tfoot><tr class=uncertain><th scope=row>Total</th>  
 <td class="@{[$must_error ? 'FAIL' : '']}">$must_error?</td>  
 <td class="@{[$should_error ? 'SEE-RESULT' : '']}">$should_error?</td>  
 <td>$warning?</td>  
 <td class="@{[$must_error ? 'FAIL' : $should_error ? 'SEE-RESULT' : '']}"><strong>&#x2212;&#x221E;..$score_max</strong> / 100  
 </table>  
   
 <p><strong>Important</strong>: This conformance checking service  
 is <em>under development</em>.  The result above might be <em>wrong</em>.</p>];  
   $out->end_section;  
 } # print_result_section  
   
 sub print_result_input_error_section ($) {  
   my $input = shift;  
   $out->start_section (id => 'result-summary', title => 'Result');  
   print STDOUT qq[  
 <p><em><strong>Input Error</strong>: @{[htescape ($input->{error_status_text})]}</em></p>];  
   $out->end_section;  
 } # print_result_input_error_section  
156    
157  {  {
158    my $Msg = {};    my $Msg = {};
# Line 581  sub get_text ($;$$) { Line 215  sub get_text ($;$$) {
215  sub get_input_document ($$) {  sub get_input_document ($$) {
216    my ($http, $dom) = @_;    my ($http, $dom) = @_;
217    
218    my $request_uri = $http->get_parameter ('uri');    require Encode;
219      my $request_uri = Encode::decode ('utf-8', $http->get_parameter ('uri'));
220    my $r = WebHACC::Input->new;    my $r = WebHACC::Input->new;
221    if (defined $request_uri and length $request_uri) {    if (defined $request_uri and length $request_uri) {
222      my $uri = $dom->create_uri_reference ($request_uri);      my $uri = $dom->create_uri_reference ($request_uri);
223      unless ({      unless ({
224               http => 1,               http => 1,
225              }->{lc $uri->uri_scheme}) {              }->{lc $uri->uri_scheme}) {
226        return {uri => $request_uri, request_uri => $request_uri,        $r = WebHACC::Input::Error->new;
227                error_status_text => 'URI scheme not allowed'};        $r->{uri} = $request_uri;
228          $r->{request_uri} = $request_uri;
229          $r->{error_status_text} = 'URL scheme not allowed';
230      }      }
231    
232      require Message::Util::HostPermit;      require Message::Util::HostPermit;
# Line 616  Deny ipv6=0::0/0 Line 253  Deny ipv6=0::0/0
253  Allow host=*  Allow host=*
254  EOH  EOH
255      unless ($host_permit->check ($uri->uri_host, $uri->uri_port || 80)) {      unless ($host_permit->check ($uri->uri_host, $uri->uri_port || 80)) {
256        return {uri => $request_uri, request_uri => $request_uri,        my $r = WebHACC::Input::Error->new;
257                error_status_text => 'Connection to the host is forbidden'};        $r->{uri} = $request_uri;
258          $r->{request_uri} = $request_uri;
259          $r->{error_status_text} = 'Connection to the host is forbidden';
260          return $r;
261      }      }
262    
263      require LWP::UserAgent;      require LWP::UserAgent;

Legend:
Removed from v.1.53  
changed lines
  Added in v.1.56

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24