--- test/html-webhacc/cc.cgi 2007/06/27 11:08:03 1.1 +++ test/html-webhacc/cc.cgi 2008/07/20 14:58:24 1.53 @@ -1,227 +1,758 @@ #!/usr/bin/perl use strict; +use utf8; use lib qw[/home/httpd/html/www/markup/html/whatpm - /home/wakaba/work/manakai/lib - /home/wakaba/public_html/-temp/wiki/lib]; + /home/wakaba/work/manakai2/lib]; use CGI::Carp qw[fatalsToBrowser]; -use Time::HiRes qw/time/; +use Scalar::Util qw[refaddr]; -use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module + require WebHACC::Input; + require WebHACC::Result; + require WebHACC::Output; -my $http = SuikaWiki::Input::HTTP->new; +my $out; -## TODO: _charset_ - -my @mode = split m#/#, scalar $http->meta_variable ('PATH_INFO'), -1; -shift @mode if @mode and $mode[0] == ''; -## TODO: decode unreserved characters + require Message::DOM::DOMImplementation; + my $dom = Message::DOM::DOMImplementation->new; +{ + use Message::CGI::HTTP; + my $http = Message::CGI::HTTP->new; - 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"; + if ($http->get_meta_variable ('PATH_INFO') ne '/') { + print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; exit; } - my $char_length = length $s; - my %time; - my $time1; - my $time2; + + load_text_catalog ('en'); ## TODO: conneg - require Message::DOM::DOMImplementation; - my $dom = Message::DOM::DOMImplementation->____new; -# $| = 1; - my $doc; - my $el; - -if (@mode == 3 and $mode[0] eq 'html' and - ($mode[2] eq 'html' or $mode[2] eq 'test')) { - print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; - - require Encode; - require Whatpm::HTML; - - $time1 = time; - $s = Encode::decode ('utf-8', $s); - $time2 = time; - $time{decode} = $time2 - $time1; - - - print STDOUT "#errors\n"; - - my $onerror = sub { - my (%opt) = @_; - print STDOUT "$opt{line},$opt{column},$opt{type}\n"; - }; - - $doc = $dom->create_document; - $time1 = time; - if (length $mode[1]) { - $el = $doc->create_element_ns - ('http://www.w3.org/1999/xhtml', [undef, $mode[1]]); - Whatpm::HTML->set_inner_html ($el, $s, $onerror); + $out = WebHACC::Output->new; + $out->handle (*STDOUT); + $out->set_utf8; + $out->set_flush; + $out->html (qq[Content-Type: text/html; charset=utf-8 + + + +
+]);
+ $out->text ($input->{media_type});
+ $out->html (qq[
]);
+ if ($input->{media_type_overridden}) {
+ $out->html ('(overridden)');
+ } elsif (defined $input->{official_type}) {
+ if ($input->{media_type} eq $input->{official_type}) {
+ #
+ } else {
+ $out->html ('(sniffed; official type is: ');
+ $out->text ($input->{official_type});
+ $out->html ('
)');
+ }
+ } else {
+ $out->html ('(sniffed)');
+ }
+ $out->html (q[');
+ $out->text ($input->{charset});
+ $out->html ('
');
+ } else {
+ $out->text ('(none)');
+ }
+ $out->html (' overridden') if $input->{charset_overridden};
+ $out->html (qq[
+@{[htescape $subinput->{media_type}]}
+ <$ebaseuri>
Note: Due to the limitation of the +network library in use, the content of this section might +not be the real header.
+ +Status code | ]; + print STDOUT qq[]; + $out->code ($input->{header_status_code}); + } + if (defined $input->{header_status_text}) { + print STDOUT qq[ |
---|---|
Status text | ]; + print STDOUT qq[]; + $out->code ($input->{header_status_text}); + } + + for (@{$input->{header_field}}) { + print STDOUT qq[ |
]; + $out->code ($_->[0]); + print STDOUT qq[ | ]; + $out->code ($_->[1]); } - print STDOUT "#log\n"; - for (qw/decode parse parse_xml serialize_html serialize_xml serialize_test - check/) { - next unless defined $time{$_}; - print STDOUT { - decode => 'bytes->chars', - parse => 'html5(chars)->dom5', - parse_xml => 'xml1(chars)->dom5', - serialize_html => 'dom5->html5(char)', - serialize_xml => 'dom5->xml1(char)', - serialize_test => 'dom5->test(char)', - check => 'dom5 check', - }->{$_}; - print STDOUT "\t", $time{$_}, "s\n"; - open my $file, '>>', ".manakai-$_.txt" or die ".manakai-$_.txt: $!"; - print $file $char_length, "\t", $time{$_}, "\n"; - } - -exit; - -sub test_serialize ($) { - my $node = shift; - my $r = ''; - - my @node = map { [$_, ''] } @{$node->child_nodes}; - while (@node) { - my $child = shift @node; - my $nt = $child->[0]->node_type; - if ($nt == $child->[0]->ELEMENT_NODE) { - $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? - - for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } - @{$child->[0]->attributes}) { - $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? - $r .= $attr->[1] . '"' . "\x0A"; + print STDOUT qq[ |
@{[htescape $id]}
<' . $euri .
+ '>
';
+ } 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[] . htescape ($resource->{value}) . '
';
+ if (defined $resource->{datatype}) {
+ my $euri = htescape ($resource->{datatype});
+ $r .= '^^<' . $euri .
+ '>
';
+ } 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');
-sub get_node_path ($) {
- my $node = shift;
- my @r;
- while (defined $node) {
- my $rs;
- if ($node->node_type == 1) {
- $rs = $node->manakai_local_name;
- $node = $node->parent_node;
- } elsif ($node->node_type == 2) {
- $rs = '@' . $node->manakai_local_name;
- $node = $node->owner_element;
- } elsif ($node->node_type == 3) {
- $rs = '"' . $node->data . '"';
- $node = $node->parent_node;
- } elsif ($node->node_type == 9) {
- $rs = '';
- $node = $node->parent_node;
+ if ($result->{unsupported} and $result->{conforming_max}) {
+ print STDOUT qq[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.
]; + } elsif ($result->{conforming_min}) { + print STDOUT qq[No conformance-error is + found in this document.
]; + } elsif ($result->{conforming_max}) { + print STDOUT qq[This document + is likely non-conforming, but in rare case + it might be conforming.
]; + } else { + print STDOUT qq[This document is + non-conforming.
]; + } + + print STDOUT qq[+ | MUST‐level +Errors | +SHOULD‐level +Errors | +Warnings | +Score | ||
---|---|---|---|---|---|---|
$label | $result->{$_->[1]}->{must}$uncertain | $result->{$_->[1]}->{should}$uncertain | $result->{$_->[1]}->{warning}$uncertain | ]; + if ($uncertain) { + print qq[−∞..$result->{$_->[1]}->{score_max}]; + } elsif ($result->{$_->[1]}->{score_min} != $result->{$_->[1]}->{score_max}) { + print qq[ | $result->{$_->[1]}->{score_min}..$result->{$_->[1]}->{score_max}]; } else { - $rs = '#' . $node->node_type; - $node = $node->parent_node; + print qq[ | $result->{$_->[1]}->{score_min}]; + } + print qq[ / 20]; + } + + $score_max += $score_base; + + print STDOUT qq[ + |
Semantics | 0? | 0? | 0? | −∞..$score_base / 20 + | ||
Total | +$must_error? | +$should_error? | +$warning? | +−∞..$score_max / 100 + |
Important: This conformance checking service +is under development. The result above might be wrong.
]; + $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[ +Input Error: @{[htescape ($input->{error_status_text})]}
]; + $out->end_section; +} # print_result_input_error_section + +{ + my $Msg = {}; + +sub load_text_catalog ($) { +# my $self = shift; + my $lang = shift; # MUST be a canonical lang name + open my $file, '<:utf8', "cc-msg.$lang.txt" + or die "$0: cc-msg.$lang.txt: $!"; + while (<$file>) { + if (s/^([^;]+);([^;]*);//) { + my ($type, $cls, $msg) = ($1, $2, $_); + $msg =~ tr/\x0D\x0A//d; + $Msg->{$type} = [$cls, $msg]; + } + } +} # load_text_catalog + +sub get_text ($;$$) { +# my $self = shift; + my ($type, $level, $node) = @_; + $type = $level . ':' . $type if defined $level; + $level = 'm' unless defined $level; + my @arg; + { + if (defined $Msg->{$type}) { + my $msg = $Msg->{$type}->[1]; + $msg =~ s{\$([0-9]+)}{ + defined $arg[$1] ? ($arg[$1]) : '(undef)'; + }ge; ##BUG: ^ must be escaped + $msg =~ s{{\@([A-Za-z0-9:_.-]+)}}{ + UNIVERSAL::can ($node, 'get_attribute_ns') + ? ($node->get_attribute_ns (undef, $1)) : '' + }ge; ## BUG: ^ must be escaped + $msg =~ s{{\@}}{ ## BUG: v must be escaped + UNIVERSAL::can ($node, 'value') ? ($node->value) : '' + }ge; + $msg =~ s{{local-name}}{ + UNIVERSAL::can ($node, 'manakai_local_name') + ? ($node->manakai_local_name) : '' + }ge; ## BUG: ^ must be escaped + $msg =~ s{{element-local-name}}{ + (UNIVERSAL::can ($node, 'owner_element') and + $node->owner_element) + ? ($node->owner_element->manakai_local_name) + : '' ## BUG: ^ must be escaped + }ge; + return ($type, 'level-' . $level . ' ' . $Msg->{$type}->[0], $msg); + } elsif ($type =~ s/:([^:]*)$//) { + unshift @arg, $1; + redo; } - unshift @r, $rs; } - return join '/', @r; -} # get_node_path + return ($type, 'level-'.$level, ($_[0])); + ## BUG: ^ must be escaped +} # get_text + +} + +sub get_input_document ($$) { + my ($http, $dom) = @_; + + my $request_uri = $http->get_parameter ('uri'); + my $r = WebHACC::Input->new; + if (defined $request_uri and length $request_uri) { + my $uri = $dom->create_uri_reference ($request_uri); + unless ({ + http => 1, + }->{lc $uri->uri_scheme}) { + return {uri => $request_uri, request_uri => $request_uri, + error_status_text => 'URI scheme not allowed'}; + } + + require Message::Util::HostPermit; + my $host_permit = new Message::Util::HostPermit; + $host_permit->add_rule (<