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]; |
|
use Time::HiRes qw/time/; |
|
7 |
|
|
8 |
use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module |
require WebHACC::Input; |
9 |
|
|
10 |
my $http = SuikaWiki::Input::HTTP->new; |
{ |
11 |
|
require Message::CGI::HTTP; |
12 |
|
my $http = Message::CGI::HTTP->new; |
13 |
|
|
14 |
|
require WebHACC::Output; |
15 |
|
my $out = WebHACC::Output->new; |
16 |
|
$out->handle (*STDOUT); |
17 |
|
$out->set_utf8; |
18 |
|
|
19 |
## TODO: _charset_ |
if ($http->get_meta_variable ('PATH_INFO') ne '/') { |
20 |
|
$out->http_error (404); |
|
my @mode = split m#/#, scalar $http->meta_variable ('PATH_INFO'), -1; |
|
|
shift @mode if @mode and $mode[0] == ''; |
|
|
## 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"; |
|
21 |
exit; |
exit; |
22 |
} |
} |
|
my $char_length = length $s; |
|
|
my %time; |
|
|
my $time1; |
|
|
my $time2; |
|
|
|
|
|
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; |
|
|
|
|
23 |
|
|
24 |
print STDOUT "#errors\n"; |
## TODO: We need real conneg support... |
25 |
|
my $primary_language = 'en'; |
26 |
my $onerror = sub { |
if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) { |
27 |
my (%opt) = @_; |
$primary_language = 'ja'; |
28 |
print STDOUT "$opt{line},$opt{column},$opt{type}\n"; |
} |
29 |
}; |
$out->load_text_catalog ($primary_language); |
30 |
|
|
31 |
$doc = $dom->create_document; |
$out->set_flush; |
32 |
$time1 = time; |
$out->http_header; |
33 |
if (length $mode[1]) { |
$out->html_header; |
34 |
$el = $doc->create_element_ns |
$out->unset_flush; |
35 |
('http://www.w3.org/1999/xhtml', [undef, $mode[1]]); |
|
36 |
Whatpm::HTML->set_inner_html ($el, $s, $onerror); |
$out->generate_input_section ($http); |
37 |
} else { |
|
38 |
Whatpm::HTML->parse_string ($s => $doc, $onerror); |
my $u = $http->get_parameter ('uri'); |
39 |
|
my $s = $http->get_parameter ('s'); |
40 |
|
if ((not defined $u or not length $u) and |
41 |
|
(not defined $s or not length $s)) { |
42 |
|
exit; |
43 |
} |
} |
|
$time2 = time; |
|
|
$time{parse} = $time2 - $time1; |
|
44 |
|
|
45 |
print "#document\n"; |
require WebHACC::Result; |
46 |
|
my $result = WebHACC::Result->new; |
47 |
|
$result->{conforming_min} = 1; |
48 |
|
$result->{conforming_max} = 1; |
49 |
|
$result->output ($out); |
50 |
|
|
51 |
my $out; |
require WebHACC::Input; |
52 |
if ($mode[2] eq 'html') { |
my $input = WebHACC::Input->get_document ($http => $result => $out); |
|
$time1 = time; |
|
|
$out = Whatpm::HTML->get_inner_html ($el || $doc); |
|
|
$time2 = time; |
|
|
$time{serialize_html} = $time2 - $time1; |
|
|
} else { # test |
|
|
$time1 = time; |
|
|
$out = test_serialize ($el || $doc); |
|
|
$time2 = time; |
|
|
$time{serialize_test} = $time2 - $time1; |
|
|
} |
|
|
print STDOUT Encode::encode ('utf-8', $$out); |
|
|
print STDOUT "\n"; |
|
|
} elsif (@mode == 3 and $mode[0] eq 'xhtml' and |
|
|
($mode[2] eq 'html' or $mode[2] eq 'test')) { |
|
|
print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; |
|
|
|
|
|
require Message::DOM::XMLParserTemp; |
|
|
print STDOUT "#errors\n"; |
|
|
|
|
|
my $onerror = sub { |
|
|
my $err = shift; |
|
|
print STDOUT $err->location->line_number, ","; |
|
|
print STDOUT $err->location->column_number, ","; |
|
|
print STDOUT $err->text, "\n"; |
|
|
return 1; |
|
|
}; |
|
|
|
|
|
open my $fh, '<', \$s; |
|
|
my $time1 = time; |
|
|
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
|
|
($fh => $dom, $onerror, charset => 'utf-8'); |
|
|
my $time2 = time; |
|
|
$time{parse_xml} = $time2 - $time1; |
|
|
|
|
|
print "#document\n"; |
|
|
|
|
|
my $out; |
|
|
if ($mode[2] eq 'html') { |
|
|
## TODO: Use XHTML serializer |
|
|
#$out = Whatpm::HTML->get_inner_html ($doc); |
|
|
} else { # test |
|
|
$time1 = time; |
|
|
$out = test_serialize ($doc); |
|
|
$time2 = time; |
|
|
$time{serialize_test} = $time2 - $time1; |
|
|
} |
|
|
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; |
|
|
} |
|
53 |
|
|
54 |
if ($http->parameter ('dom5')) { |
check_and_print ($input => $result => $out); |
55 |
require Whatpm::ContentChecker; |
|
56 |
my $onerror = sub { |
$result->generate_result_section; |
|
my %opt = @_; |
|
|
print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n"; |
|
|
}; |
|
|
print STDOUT "#domerrors\n"; |
|
|
$time1 = time; |
|
|
if ($el) { |
|
|
Whatpm::ContentChecker->check_element ($el, $onerror); |
|
|
} else { |
|
|
Whatpm::ContentChecker->check_document ($doc, $onerror); |
|
|
} |
|
|
$time2 = time; |
|
|
$time{check} = $time2 - $time1; |
|
|
} |
|
57 |
|
|
58 |
print STDOUT "#log\n"; |
$out->nav_list; |
|
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"; |
|
|
} |
|
59 |
|
|
60 |
exit; |
exit; |
61 |
|
} |
62 |
|
|
63 |
sub test_serialize ($) { |
sub check_and_print ($$$) { |
64 |
my $node = shift; |
my ($input, $result, $out) = @_; |
65 |
my $r = ''; |
my $original_input = $out->input; |
66 |
|
$out->input ($input); |
67 |
my @node = map { [$_, ''] } @{$node->child_nodes}; |
|
68 |
while (@node) { |
$input->generate_info_section ($result); |
69 |
my $child = shift @node; |
|
70 |
my $nt = $child->[0]->node_type; |
$input->generate_transfer_sections ($result); |
71 |
if ($nt == $child->[0]->ELEMENT_NODE) { |
|
72 |
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
unless (defined $input->{s}) { |
73 |
|
$result->{conforming_min} = 0; |
74 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
return; |
75 |
@{$child->[0]->attributes}) { |
} |
76 |
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
|
77 |
$r .= $attr->[1] . '"' . "\x0A"; |
my $checker_class = { |
78 |
} |
'text/cache-manifest' => 'WebHACC::Language::CacheManifest', |
79 |
|
'text/css' => 'WebHACC::Language::CSS', |
80 |
unshift @node, |
'text/html' => 'WebHACC::Language::HTML', |
81 |
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
'text/x-webidl' => 'WebHACC::Language::WebIDL', |
82 |
} elsif ($nt == $child->[0]->TEXT_NODE) { |
|
83 |
$r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
'text/xml' => 'WebHACC::Language::XML', |
84 |
} elsif ($nt == $child->[0]->CDATA_SECTION_NODE) { |
'application/atom+xml' => 'WebHACC::Language::XML', |
85 |
$r .= '| ' . $child->[1] . '<![CDATA[' . $child->[0]->data . "]]>\x0A"; |
'application/rss+xml' => 'WebHACC::Language::XML', |
86 |
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
'image/svg+xml' => 'WebHACC::Language::XML', |
87 |
$r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
'application/xhtml+xml' => 'WebHACC::Language::XML', |
88 |
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
'application/xml' => 'WebHACC::Language::XML', |
89 |
$r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A"; |
## TODO: Should we make all XML MIME Types fall |
90 |
} elsif ($nt == $child->[0]->PROCESSING_INSTRUCTION_NODE) { |
## into this category? |
91 |
$r .= '| ' . $child->[1] . '<?' . $child->[0]->target . ' ' . |
|
92 |
$child->[0]->data . "?>\x0A"; |
## NOTE: This type has different model from normal XML types. |
93 |
} else { |
'application/rdf+xml' => 'WebHACC::Language::XML', |
94 |
$r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error |
}->{$input->{media_type}} || 'WebHACC::Language::Default'; |
95 |
} |
|
96 |
|
eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@"; |
97 |
|
my $checker = $checker_class->new; |
98 |
|
$checker->input ($input); |
99 |
|
$checker->output ($out); |
100 |
|
$checker->result ($result); |
101 |
|
|
102 |
|
## TODO: A cache manifest MUST be text/cache-manifest |
103 |
|
## TODO: WebIDL media type "text/x-webidl" |
104 |
|
|
105 |
|
$checker->generate_syntax_error_section; |
106 |
|
$checker->generate_source_string_section; |
107 |
|
|
108 |
|
my @subdoc; |
109 |
|
$checker->onsubdoc (sub { |
110 |
|
push @subdoc, shift; |
111 |
|
}); |
112 |
|
|
113 |
|
$checker->generate_structure_dump_section; |
114 |
|
$checker->generate_structure_error_section; |
115 |
|
$checker->generate_additional_sections; |
116 |
|
|
117 |
|
my $id_prefix = 0; |
118 |
|
for my $_subinput (@subdoc) { |
119 |
|
my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix); |
120 |
|
$subinput->{$_} = $_subinput->{$_} for keys %$_subinput; |
121 |
|
$subinput->{base_uri} = $subinput->{container_node}->base_uri |
122 |
|
unless defined $subinput->{base_uri}; |
123 |
|
$subinput->{parent_input} = $input; |
124 |
|
|
125 |
|
$subinput->start_section ($result); |
126 |
|
check_and_print ($subinput => $result => $out); |
127 |
|
$subinput->end_section ($result); |
128 |
} |
} |
|
|
|
|
return \$r; |
|
|
} # test_serialize |
|
129 |
|
|
130 |
sub get_node_path ($) { |
$out->input ($original_input); |
131 |
my $node = shift; |
} # check_and_print |
|
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; |
|
|
} else { |
|
|
$rs = '#' . $node->node_type; |
|
|
$node = $node->parent_node; |
|
|
} |
|
|
unshift @r, $rs; |
|
|
} |
|
|
return join '/', @r; |
|
|
} # get_node_path |
|
132 |
|
|
133 |
=head1 AUTHOR |
=head1 AUTHOR |
134 |
|
|
136 |
|
|
137 |
=head1 LICENSE |
=head1 LICENSE |
138 |
|
|
139 |
Copyright 2007 Wakaba <w@suika.fam.cx> |
Copyright 2007-2008 Wakaba <w@suika.fam.cx> |
140 |
|
|
141 |
This library is free software; you can redistribute it |
This library is free software; you can redistribute it |
142 |
and/or modify it under the same terms as Perl itself. |
and/or modify it under the same terms as Perl itself. |