/[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.1 by wakaba, Wed Jun 27 11:08:03 2007 UTC revision 1.66 by wakaba, Mon Sep 15 02:55:12 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl -d:DProf
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      $out->generate_input_section ($http);
37    
38      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      }
44    
45    print STDOUT "#errors\n";    require WebHACC::Result;
46      my $result = WebHACC::Result->new;
47      $result->output ($out);
48    
49    my $onerror = sub {    require WebHACC::Input;
50      my (%opt) = @_;    my $input = WebHACC::Input->get_document ($http => $result => $out);
     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);  
   } else {  
     Whatpm::HTML->parse_string ($s => $doc, $onerror);  
   }  
   $time2 = time;  
   $time{parse} = $time2 - $time1;  
51    
52    print "#document\n";    check_and_print ($input => $result => $out);
53      
54      $out->nav_list;
55    
   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";  
56    exit;    exit;
57  }  }
58    
59    if ($http->parameter ('dom5')) {  sub check_and_print ($$$) {
60      require Whatpm::ContentChecker;    my ($input, $result, $out) = @_;
61      my $onerror = sub {    my $original_input = $out->input;
62        my %opt = @_;    $out->input ($input);
63        print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n";  
64      };    $input->generate_info_section ($result);
65      print STDOUT "#domerrors\n";  
66      $time1 = time;    $input->generate_transfer_sections ($result);
67      if ($el) {  
68        Whatpm::ContentChecker->check_element ($el, $onerror);    unless (defined $input->{s}) {
69      } else {      ## NOTE: This is an error of the implementation.
70        Whatpm::ContentChecker->check_document ($doc, $onerror);      $result->layer_uncertain ('transfer');
71      }      $result->generate_result_section;
72      $time2 = time;  
73      $time{check} = $time2 - $time1;      $out->input ($original_input);
74    }      return;
75      }
76    print STDOUT "#log\n";  
77    for (qw/decode parse parse_xml serialize_html serialize_xml serialize_test    my $checker_class = {
78            check/) {      'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
79      next unless defined $time{$_};      'text/css' => 'WebHACC::Language::CSS',
80      print STDOUT {      'text/html' => 'WebHACC::Language::HTML',
81        decode => 'bytes->chars',      'text/x-h2h' => 'WebHACC::Language::H2H',
82        parse => 'html5(chars)->dom5',      'text/x-webidl' => 'WebHACC::Language::WebIDL',
83        parse_xml => 'xml1(chars)->dom5',  
84        serialize_html => 'dom5->html5(char)',      'text/xml' => 'WebHACC::Language::XML',
85        serialize_xml => 'dom5->xml1(char)',      'application/atom+xml' => 'WebHACC::Language::XML',
86        serialize_test => 'dom5->test(char)',      'application/rss+xml' => 'WebHACC::Language::XML',
87        check => 'dom5 check',      'image/svg+xml' => 'WebHACC::Language::XML',
88      }->{$_};      'application/xhtml+xml' => 'WebHACC::Language::XML',
89      print STDOUT "\t", $time{$_}, "s\n";      'application/xml' => 'WebHACC::Language::XML',
90      open my $file, '>>', ".manakai-$_.txt" or die ".manakai-$_.txt: $!";      ## TODO: Should we make all XML MIME Types fall
91      print $file $char_length, "\t", $time{$_}, "\n";      ## into this category?
92    
93        ## NOTE: This type has different model from normal XML types.
94        'application/rdf+xml' => 'WebHACC::Language::XML',
95      }->{$input->{media_type}} || 'WebHACC::Language::Default';
96    
97      eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
98      my $checker = $checker_class->new;
99      $checker->input ($input);
100      $checker->output ($out);
101      $checker->result ($result);
102    
103      ## TODO: A cache manifest MUST be text/cache-manifest
104      ## TODO: WebIDL media type "text/x-webidl"
105    
106      $checker->generate_syntax_error_section;
107      $checker->generate_source_string_section;
108    
109      my @subdoc;
110      $checker->onsubdoc (sub {
111        push @subdoc, shift;
112      });
113    
114      $checker->generate_structure_dump_section;
115      $checker->generate_structure_error_section;
116      $checker->generate_additional_sections;
117    
118      my $id_prefix = 0;
119      for my $_subinput (@subdoc) {
120        my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix);
121        $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
122        $subinput->{base_uri} = $subinput->{container_node}->base_uri
123            unless defined $subinput->{base_uri};
124        $subinput->{parent_input} = $input;
125    
126        my $subresult = WebHACC::Result->new;
127        $subresult->output ($out);
128        $subresult->parent_result ($result);
129    
130        $subinput->start_section ($subresult);
131        check_and_print ($subinput => $subresult => $out);
132        $subinput->end_section ($subresult);
133    }    }
134    
135  exit;    $result->generate_result_section;
136    
137  sub test_serialize ($) {    $out->input ($original_input);
138    my $node = shift;  } # check_and_print
   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  
139    
140  =head1 AUTHOR  =head1 AUTHOR
141    
# Line 229  Wakaba <w@suika.fam.cx>. Line 143  Wakaba <w@suika.fam.cx>.
143    
144  =head1 LICENSE  =head1 LICENSE
145    
146  Copyright 2007 Wakaba <w@suika.fam.cx>  Copyright 2007-2008 Wakaba <w@suika.fam.cx>
147    
148  This library is free software; you can redistribute it  This library is free software; you can redistribute it
149  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.66

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24