/[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.65 by wakaba, Sun Sep 14 14:36:12 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    # -d:DProf
3  use strict;  use strict;
4    
5  use lib qw[/home/httpd/html/www/markup/html/whatpm  use lib qw[/home/httpd/html/www/markup/html/whatpm
6             /home/wakaba/work/manakai/lib             /home/wakaba/work/manakai2/lib];
            /home/wakaba/public_html/-temp/wiki/lib];  
7  use CGI::Carp qw[fatalsToBrowser];  use CGI::Carp qw[fatalsToBrowser];
 use Time::HiRes qw/time/;  
8    
9  use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module    require WebHACC::Input;
10    
11  my $http = SuikaWiki::Input::HTTP->new;  {
12      require Message::CGI::HTTP;
13      my $http = Message::CGI::HTTP->new;
14    
15      require WebHACC::Output;
16      my $out = WebHACC::Output->new;
17      $out->handle (*STDOUT);
18      $out->set_utf8;
19    
20  ## TODO: _charset_    if ($http->get_meta_variable ('PATH_INFO') ne '/') {
21        $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";  
22      exit;      exit;
23    }    }
24    my $char_length = length $s;  
25    my %time;    ## TODO: We need real conneg support...
26    my $time1;    my $primary_language = 'en';
27    my $time2;    if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) {
28        $primary_language = 'ja';
29    require Message::DOM::DOMImplementation;    }
30    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;  
31        
32      $out->set_flush;
33      $out->http_header;
34      $out->html_header;
35      $out->unset_flush;
36    
37      $out->generate_input_section ($http);
38    
39      my $u = $http->get_parameter ('uri');
40      my $s = $http->get_parameter ('s');
41      if ((not defined $u or not length $u) and
42          (not defined $s or not length $s)) {
43        exit;
44      }
45    
46    print STDOUT "#errors\n";    require WebHACC::Result;
47      my $result = WebHACC::Result->new;
48      $result->output ($out);
49    
50    my $onerror = sub {    require WebHACC::Input;
51      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;  
52    
53    print "#document\n";    check_and_print ($input => $result => $out);
54      
55      $out->nav_list;
56    
   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";  
57    exit;    exit;
58  }  }
59    
60    if ($http->parameter ('dom5')) {  sub check_and_print ($$$) {
61      require Whatpm::ContentChecker;    my ($input, $result, $out) = @_;
62      my $onerror = sub {    my $original_input = $out->input;
63        my %opt = @_;    $out->input ($input);
64        print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n";  
65      };    $input->generate_info_section ($result);
66      print STDOUT "#domerrors\n";  
67      $time1 = time;    $input->generate_transfer_sections ($result);
68      if ($el) {  
69        Whatpm::ContentChecker->check_element ($el, $onerror);    unless (defined $input->{s}) {
70      } else {      ## NOTE: This is an error of the implementation.
71        Whatpm::ContentChecker->check_document ($doc, $onerror);      $result->layer_uncertain ('transfer');
72      }      $result->generate_result_section;
73      $time2 = time;  
74      $time{check} = $time2 - $time1;      $out->input ($original_input);
75    }      return;
76      }
77    print STDOUT "#log\n";  
78    for (qw/decode parse parse_xml serialize_html serialize_xml serialize_test    my $checker_class = {
79            check/) {      'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
80      next unless defined $time{$_};      'text/css' => 'WebHACC::Language::CSS',
81      print STDOUT {      'text/html' => 'WebHACC::Language::HTML',
82        decode => 'bytes->chars',      'text/x-h2h' => 'WebHACC::Language::H2H',
83        parse => 'html5(chars)->dom5',      'text/x-webidl' => 'WebHACC::Language::WebIDL',
84        parse_xml => 'xml1(chars)->dom5',  
85        serialize_html => 'dom5->html5(char)',      'text/xml' => 'WebHACC::Language::XML',
86        serialize_xml => 'dom5->xml1(char)',      'application/atom+xml' => 'WebHACC::Language::XML',
87        serialize_test => 'dom5->test(char)',      'application/rss+xml' => 'WebHACC::Language::XML',
88        check => 'dom5 check',      'image/svg+xml' => 'WebHACC::Language::XML',
89      }->{$_};      'application/xhtml+xml' => 'WebHACC::Language::XML',
90      print STDOUT "\t", $time{$_}, "s\n";      'application/xml' => 'WebHACC::Language::XML',
91      open my $file, '>>', ".manakai-$_.txt" or die ".manakai-$_.txt: $!";      ## TODO: Should we make all XML MIME Types fall
92      print $file $char_length, "\t", $time{$_}, "\n";      ## into this category?
93    
94        ## NOTE: This type has different model from normal XML types.
95        'application/rdf+xml' => 'WebHACC::Language::XML',
96      }->{$input->{media_type}} || 'WebHACC::Language::Default';
97    
98      eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
99      my $checker = $checker_class->new;
100      $checker->input ($input);
101      $checker->output ($out);
102      $checker->result ($result);
103    
104      ## TODO: A cache manifest MUST be text/cache-manifest
105      ## TODO: WebIDL media type "text/x-webidl"
106    
107      $checker->generate_syntax_error_section;
108      $checker->generate_source_string_section;
109    
110      my @subdoc;
111      $checker->onsubdoc (sub {
112        push @subdoc, shift;
113      });
114    
115      $checker->generate_structure_dump_section;
116      $checker->generate_structure_error_section;
117      $checker->generate_additional_sections;
118    
119      my $id_prefix = 0;
120      for my $_subinput (@subdoc) {
121        my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix);
122        $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
123        $subinput->{base_uri} = $subinput->{container_node}->base_uri
124            unless defined $subinput->{base_uri};
125        $subinput->{parent_input} = $input;
126    
127        my $subresult = WebHACC::Result->new;
128        $subresult->output ($out);
129        $subresult->parent_result ($result);
130    
131        $subinput->start_section ($subresult);
132        check_and_print ($subinput => $subresult => $out);
133        $subinput->end_section ($subresult);
134    }    }
135    
136  exit;    $result->generate_result_section;
137    
138  sub test_serialize ($) {    $out->input ($original_input);
139    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  
140    
141  =head1 AUTHOR  =head1 AUTHOR
142    
# Line 229  Wakaba <w@suika.fam.cx>. Line 144  Wakaba <w@suika.fam.cx>.
144    
145  =head1 LICENSE  =head1 LICENSE
146    
147  Copyright 2007 Wakaba <w@suika.fam.cx>  Copyright 2007-2008 Wakaba <w@suika.fam.cx>
148    
149  This library is free software; you can redistribute it  This library is free software; you can redistribute it
150  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.65

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24