/[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.1 by wakaba, Wed Jun 27 11:08:03 2007 UTC revision 1.60 by wakaba, Sat Jul 26 11:27:25 2008 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];
 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    }    }
23    my $char_length = length $s;  
24    my %time;    ## TODO: We need real conneg support...
25    my $time1;    my $primary_language = 'en';
26    my $time2;    if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) {
27        $primary_language = 'ja';
28    require Message::DOM::DOMImplementation;    }
29    my $dom = Message::DOM::DOMImplementation->____new;    $out->load_text_catalog ($primary_language);
 #  $| = 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;  
30        
31      $out->set_flush;
32      $out->http_header;
33      $out->html_header;
34      $out->unset_flush;
35    
36      require WebHACC::Result;
37      my $result = WebHACC::Result->new;
38      $result->{conforming_min} = 1;
39      $result->{conforming_max} = 1;
40      $result->output ($out);
41    
42    print STDOUT "#errors\n";    require WebHACC::Input;
43      my $input = WebHACC::Input->get_document ($http => $result => $out);
44    
45    my $onerror = sub {    check_and_print ($input => $result => $out);
46      my (%opt) = @_;    
47      print STDOUT "$opt{line},$opt{column},$opt{type}\n";    $result->generate_result_section;
   };  
   
   $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);  
   } else {  
     Whatpm::HTML->parse_string ($s => $doc, $onerror);  
   }  
   $time2 = time;  
   $time{parse} = $time2 - $time1;  
48    
49    print "#document\n";    $out->nav_list;
50    
   my $out;  
   if ($mode[2] eq 'html') {  
     $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";  
51    exit;    exit;
52  }  }
53    
54    if ($http->parameter ('dom5')) {  sub check_and_print ($$$) {
55      require Whatpm::ContentChecker;    my ($input, $result, $out) = @_;
56      my $onerror = sub {    my $original_input = $out->input;
57        my %opt = @_;    $out->input ($input);
58        print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n";  
59      };    $input->generate_info_section ($result);
60      print STDOUT "#domerrors\n";  
61      $time1 = time;    $input->generate_transfer_sections ($result);
62      if ($el) {  
63        Whatpm::ContentChecker->check_element ($el, $onerror);    unless (defined $input->{s}) {
64      } else {      $result->{conforming_min} = 0;
65        Whatpm::ContentChecker->check_document ($doc, $onerror);      return;
66      }    }
67      $time2 = time;  
68      $time{check} = $time2 - $time1;    my $checker_class = {
69        'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
70        'text/css' => 'WebHACC::Language::CSS',
71        'text/html' => 'WebHACC::Language::HTML',
72        'text/x-webidl' => 'WebHACC::Language::WebIDL',
73    
74        'text/xml' => 'WebHACC::Language::XML',
75        'application/atom+xml' => 'WebHACC::Language::XML',
76        'application/rss+xml' => 'WebHACC::Language::XML',
77        'image/svg+xml' => 'WebHACC::Language::XML',
78        'application/xhtml+xml' => 'WebHACC::Language::XML',
79        'application/xml' => 'WebHACC::Language::XML',
80        ## TODO: Should we make all XML MIME Types fall
81        ## into this category?
82    
83        ## NOTE: This type has different model from normal XML types.
84        'application/rdf+xml' => 'WebHACC::Language::XML',
85      }->{$input->{media_type}} || 'WebHACC::Language::Default';
86    
87      eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
88      my $checker = $checker_class->new;
89      $checker->input ($input);
90      $checker->output ($out);
91      $checker->result ($result);
92    
93      ## TODO: A cache manifest MUST be text/cache-manifest
94      ## TODO: WebIDL media type "text/x-webidl"
95    
96      $checker->generate_syntax_error_section;
97      $checker->generate_source_string_section;
98    
99      my @subdoc;
100      $checker->onsubdoc (sub {
101        push @subdoc, shift;
102      });
103    
104      $checker->generate_structure_dump_section;
105      $checker->generate_structure_error_section;
106      $checker->generate_additional_sections;
107    
108      my $id_prefix = 0;
109      for my $_subinput (@subdoc) {
110        my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix);
111        $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
112        $subinput->{base_uri} = $subinput->{container_node}->base_uri
113            unless defined $subinput->{base_uri};
114        $subinput->{parent_input} = $input;
115    
116        $subinput->start_section ($result);
117        check_and_print ($subinput => $result => $out);
118        $subinput->end_section ($result);
119    }    }
120    
121    print STDOUT "#log\n";    $out->input ($original_input);
122    for (qw/decode parse parse_xml serialize_html serialize_xml serialize_test  } # check_and_print
           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";  
       }  
         
       unshift @node,  
         map { [$_, $child->[1] . '  '] } @{$child->[0]->child_nodes};  
     } elsif ($nt == $child->[0]->TEXT_NODE) {  
       $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";  
     } elsif ($nt == $child->[0]->CDATA_SECTION_NODE) {  
       $r .= '| ' . $child->[1] . '<![CDATA[' . $child->[0]->data . "]]>\x0A";  
     } elsif ($nt == $child->[0]->COMMENT_NODE) {  
       $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";  
     } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {  
       $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";  
     } elsif ($nt == $child->[0]->PROCESSING_INSTRUCTION_NODE) {  
       $r .= '| ' . $child->[1] . '<?' . $child->[0]->target . ' ' .  
           $child->[0]->data . "?>\x0A";  
     } else {  
       $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error  
     }  
   }  
     
   return \$r;  
 } # test_serialize  
   
 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;  
     } else {  
       $rs = '#' . $node->node_type;  
       $node = $node->parent_node;  
     }  
     unshift @r, $rs;  
   }  
   return join '/', @r;  
 } # get_node_path  
123    
124  =head1 AUTHOR  =head1 AUTHOR
125    
# Line 229  Wakaba <w@suika.fam.cx>. Line 127  Wakaba <w@suika.fam.cx>.
127    
128  =head1 LICENSE  =head1 LICENSE
129    
130  Copyright 2007 Wakaba <w@suika.fam.cx>  Copyright 2007-2008 Wakaba <w@suika.fam.cx>
131    
132  This library is free software; you can redistribute it  This library is free software; you can redistribute it
133  and/or modify it under the same terms as Perl itself.  and/or modify it under the same terms as Perl itself.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24