home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / RegistryLoader.pm < prev    next >
Encoding:
Perl POD Document  |  2004-09-17  |  10.2 KB  |  354 lines

  1. # Copyright 2001-2004 The Apache Software Foundation
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #     http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. package ModPerl::RegistryLoader;
  16.  
  17. use strict;
  18. use warnings;
  19.  
  20. use ModPerl::RegistryCooker ();
  21. use Apache::ServerUtil ();
  22. use APR::Pool ();
  23.  
  24. use Carp;
  25. use File::Spec ();
  26.  
  27. use Apache::Const -compile => qw(OK HTTP_OK OPT_EXECCGI);
  28.  
  29. our @ISA = ();
  30.  
  31. sub new {
  32.     my $class = shift;
  33.     my $self = bless {@_} => ref($class)||$class;
  34.     $self->{package} ||= 'ModPerl::Registry';
  35.     $self->{pool} = APR::Pool->new();
  36.     $self->load_package($self->{package});
  37.     return $self;
  38. }
  39.  
  40. sub handler {
  41.     my($self, $uri, $filename, $virthost) = @_;
  42.  
  43.     # set the inheritance rules at run time
  44.     @ISA = $self->{package};
  45.  
  46.     unless (defined $uri) {
  47.         $self->warn("uri is a required argument");
  48.         return;
  49.     }
  50.  
  51.     if (defined $filename) {
  52.         unless (-e $filename) {
  53.             $self->warn("Cannot find: $filename");
  54.             return;
  55.         }
  56.     }
  57.     else {
  58.         # try to translate URI->filename
  59.         if (exists $self->{trans} and ref($self->{trans}) eq 'CODE') {
  60.             no strict 'refs';
  61.             $filename = $self->{trans}->($uri);
  62.             unless (-e $filename) {
  63.                 $self->warn("Cannot find a translated from uri: $filename");
  64.                 return;
  65.             }
  66.         }
  67.         else {
  68.             # try to guess
  69.             (my $guess = $uri) =~ s|^/||;
  70.  
  71.             $self->warn("Trying to guess filename based on uri")
  72.                 if $self->{debug};
  73.  
  74.             $filename = File::Spec->catfile(Apache::ServerUtil::server_root,
  75.                                             $guess);
  76.             unless (-e $filename) {
  77.                 $self->warn("Cannot find guessed file: $filename",
  78.                             "provide \$filename or 'trans' sub");
  79.                 return;
  80.             }
  81.         }
  82.     }
  83.  
  84.     if ($self->{debug}) {
  85.         $self->warn("*** uri=$uri, filename=$filename");
  86.     }
  87.  
  88.     my $rl = bless {
  89.         uri      => $uri,
  90.         filename => $filename,
  91.         package  => $self->{package},
  92.     } => ref($self) || $self;
  93.  
  94.     $rl->{virthost} = $virthost if defined $virthost;
  95.  
  96.     # can't call SUPER::handler here, because it usually calls new()
  97.     # and then the ModPerlRegistryLoader::new() will get called,
  98.     # instead of the super class' new, so we implement the super
  99.     # class' handler here. Hopefully all other subclasses use the same
  100.     # handler.
  101.     __PACKAGE__->SUPER::new($rl)->default_handler();
  102.  
  103. }
  104.  
  105. # XXX: s/my_// for qw(my_finfo my_slurp_filename);
  106. # when when finfo() and slurp_filename() are ported to 2.0 and
  107. # RegistryCooker is starting to use them
  108.  
  109. sub get_server_name { return $_[0]->{virthost} if exists $_[0]->{virthost} }
  110. sub filename { shift->{filename} }
  111. sub status { Apache::HTTP_OK }
  112. sub my_finfo    { shift->{filename} }
  113. sub uri      { shift->{uri} }
  114. sub path_info {}
  115. sub allow_options { Apache::OPT_EXECCGI } #will be checked again at run-time
  116. sub log_error { shift; die @_ if $@; warn @_; }
  117. sub run { return Apache::OK } # don't run the script
  118. sub server { shift }
  119. sub is_virtual { exists shift->{virthost} }
  120.  
  121. # the preloaded file needs to be precompiled into the package
  122. # specified by the 'package' attribute, not RegistryLoader
  123. sub namespace_root {
  124.     join '::', ModPerl::RegistryCooker::NAMESPACE_ROOT,
  125.         shift->{REQ}->{package};
  126. }
  127.  
  128. # override Apache class methods called by Modperl::Registry*. normally
  129. # only available at request-time via blessed request_rec pointer
  130. sub slurp_filename {
  131.     my $r = shift;
  132.     my $tainted = @_ ? shift : 1;
  133.     my $filename = $r->filename;
  134.     open my $fh, $filename or die "can't open $filename: $!";
  135.     local $/;
  136.     my $code = <$fh>;
  137.     unless ($tainted) {
  138.         ($code) = $code =~ /(.*)/s; # untaint
  139.     }
  140.     close $fh;
  141.     return \$code;
  142. }
  143.  
  144. sub load_package {
  145.     my($self, $package) = @_;
  146.  
  147.     croak "package to load wasn't specified" unless defined $package;
  148.  
  149.     $package =~ s|::|/|g;
  150.     $package .= ".pm";
  151.     require $package;
  152. };
  153.  
  154. sub warn {
  155.     my $self = shift;
  156.     Apache->warn(__PACKAGE__ . ": @_\n");
  157. }
  158.  
  159. 1;
  160. __END__
  161.  
  162. =head1 NAME
  163.  
  164. ModPerl::RegistryLoader - Compile ModPerl::RegistryCooker scripts at server startup
  165.  
  166. =head1 Synopsis
  167.  
  168.   # in startup.pl
  169.   use ModPerl::RegistryLoader ();
  170.   use File::Spec ();
  171.   
  172.   # explicit uri => filename mapping
  173.   my $rlbb = ModPerl::RegistryLoader->new(
  174.       package => 'ModPerl::RegistryBB',
  175.       debug   => 1, # default 0
  176.   );
  177.  
  178.   $rlbb->handler($uri, $filename);
  179.   
  180.   ###
  181.   # uri => filename mapping using a helper function
  182.   sub trans {
  183.       my $uri = shift;
  184.       $uri =~ s|^/registry/|cgi-bin/|;
  185.       return File::Spec->catfile(Apache::ServerUtil::server_root, $uri);
  186.   }
  187.   my $rl = ModPerl::RegistryLoader->new(
  188.       package => 'ModPerl::Registry',
  189.       trans   => \&trans,
  190.   );
  191.   $rl->handler($uri);
  192.   
  193.   ###
  194.   $rlbb->handler($uri, $filename, $virtual_hostname);
  195.  
  196.  
  197. =head1 Description
  198.  
  199. This modules allows compilation of scripts, running under packages
  200. derived from C<ModPerl::RegistryCooker>, at server startup.  The
  201. script's handler routine is compiled by the parent server, of which
  202. children get a copy and thus saves some memory by initially sharing
  203. the compiled copy with the parent and saving the overhead of script's
  204. compilation on the first request in every httpd instance.
  205.  
  206. This module is of course useless for those running the
  207. C<L<ModPerl::PerlRun>> handler, because the scripts get recompiled on
  208. each request under this handler.
  209.  
  210. =head1 Methods
  211.  
  212. =over
  213.  
  214. =item new()
  215.  
  216. When creating a new C<ModPerl::RegistryLoader> object, one has to
  217. specify which of the C<ModPerl::RegistryCooker> derived modules to
  218. use. For example if a script is going to run under
  219. C<ModPerl::RegistryBB> the object is initialized as:
  220.  
  221.   my $rlbb = ModPerl::RegistryLoader->new(
  222.       package => 'ModPerl::RegistryBB',
  223.   );
  224.  
  225. If the package is not specified C<ModPerl::Registry> is assumed:
  226.  
  227.   my $rlbb = ModPerl::RegistryLoader->new();
  228.  
  229. To turn the debugging on, set the I<debug> attribute to a true value:
  230.  
  231.   my $rlbb = ModPerl::RegistryLoader->new(
  232.       package => 'ModPerl::RegistryBB',
  233.       debug   => 1,
  234.   );
  235.  
  236. Instead of specifying explicitly a filename for each uri passed to
  237. handler(), a special attribute I<trans> can be set to a subroutine to
  238. perform automatic remapping.
  239.  
  240.   my $rlbb = ModPerl::RegistryLoader->new(
  241.       package => 'ModPerl::RegistryBB',
  242.       trans   => \&trans,
  243.   );
  244.  
  245. See the handler() item for an example of using the I<trans> attribute.
  246.  
  247. =item handler()
  248.  
  249.   $rl->handler($uri, [$filename, [$virtual_hostname]]);
  250.  
  251. The handler() method takes argument of C<uri> and optionally of
  252. C<filename> and of C<virtual_hostname>.
  253.  
  254. URI to filename translation normally doesn't happen until HTTP request
  255. time, so we're forced to roll our own translation. If the filename is
  256. supplied it's used in translation.
  257.  
  258. If the filename is omitted and a C<trans> subroutine was not set in
  259. new(), the loader will try using the C<uri> relative to the
  260. C<ServerRoot> configuration directive.  For example:
  261.  
  262.   httpd.conf:
  263.   -----------
  264.   ServerRoot /usr/local/apache
  265.   Alias /registry/ /usr/local/apache/cgi-bin/
  266.  
  267.   startup.pl:
  268.   -----------
  269.   use ModPerl::RegistryLoader ();
  270.   my $rl = ModPerl::RegistryLoader->new(
  271.       package => 'ModPerl::Registry',
  272.   );
  273.   # preload /usr/local/apache/cgi-bin/test.pl
  274.   $rl->handler(/registry/test.pl);
  275.  
  276. To make the loader smarter about the URI-E<gt>filename translation,
  277. you may provide the C<new()> method with a C<trans()> function to
  278. translate the uri to filename.
  279.  
  280. The following example will pre-load all files ending with I<.pl> in
  281. the I<cgi-bin> directory relative to C<ServerRoot>.
  282.  
  283.   httpd.conf:
  284.   -----------
  285.   ServerRoot /usr/local/apache
  286.   Alias /registry/ /usr/local/apache/cgi-bin/
  287.  
  288.   startup.pl:
  289.   -----------
  290.   {
  291.       # test the scripts pre-loading by using trans sub
  292.       use ModPerl::RegistryLoader ();
  293.       use File::Spec ();
  294.       use DirHandle ();
  295.       use strict;
  296.   
  297.       my $dir = File::Spec->catdir(Apache::ServerUtil::server_root,
  298.                                   "cgi-bin");
  299.   
  300.       sub trans {
  301.           my $uri = shift; 
  302.           $uri =~ s|^/registry/|cgi-bin/|;
  303.           return File::Spec->catfile(Apache::ServerUtil::server_root,
  304.                                      $uri);
  305.       }
  306.   
  307.       my $rl = ModPerl::RegistryLoader->new(
  308.           package => "ModPerl::Registry",
  309.           trans   => \&trans,
  310.       );
  311.       my $dh = DirHandle->new($dir) or die $!;
  312.   
  313.       for my $file ($dh->read) {
  314.           next unless $file =~ /\.pl$/;
  315.           $rl->handler("/registry/$file");
  316.       }
  317.   }
  318.  
  319. If C<$virtual_hostname> argument is passed it'll be used in the
  320. creation of the package name the script will be compiled into for
  321. those registry handlers that use I<namespace_from_uri()> method.  See
  322. also the notes on C<$ModPerl::RegistryCooker::NameWithVirtualHost> in
  323. the C<L<ModPerl::RegistryCooker>> documentation.
  324.  
  325. Also
  326. explained in the C<L<ModPerl::RegistryLoader>> documentation, this
  327. only has an effect at run time if
  328. C<$ModPerl::RegistryCooker::NameWithVirtualHost> is set to true,
  329. otherwise the C<$virtual_hostname> argument is ignored.
  330.  
  331. =back
  332.  
  333.  
  334. =head1 Implementation Notes
  335.  
  336. C<ModPerl::RegistryLoader> performs a very simple job, at run time it
  337. loads and sub-classes the module passed via the I<package> attribute
  338. and overrides some of its functions, to emulate the run-time
  339. environment. This allows to preload the same script into different
  340. registry environments.
  341.  
  342. =head1 Authors
  343.  
  344. The original C<Apache::RegistryLoader> implemented by Doug MacEachern.
  345.  
  346. Stas Bekman did the porting to the new registry framework based on
  347. C<ModPerl::RegistryLoader>.
  348.  
  349. =head1 SEE ALSO
  350.  
  351. C<L<ModPerl::RegistryCooker>>, C<L<ModPerl::Registry>>,
  352. C<L<ModPerl::RegistryBB>>, C<L<ModPerl::PerlRun>>, Apache(3),
  353. mod_perl(3)
  354.