📄 registrycooker.pm
字号:
# desc: reads the script in# args: $self - registry blessed object# rtrn: Apache2::Const::OK on success, some other code on failure# efct: initializes the CODE field with the source script########################################################################## reads the contents of the filesub read_script { my $self = shift; $self->debug("reading $self->{FILENAME}") if DEBUG & D_NOISE; $self->{CODE} = eval { $self->{REQ}->slurp_filename(0) }; # untainted if ($@) { $self->log_error("$@"); if (ref $@ eq 'APR::Error') { return Apache2::Const::FORBIDDEN if APR::Status::is_EACCES($@); return Apache2::Const::NOT_FOUND if APR::Status::is_ENOENT($@); } else { return Apache2::Const::SERVER_ERROR; } } return Apache2::Const::OK;}########################################################################## func: shebang_to_perl# dflt: shebang_to_perl# desc: parse the shebang line and convert command line switches# (defined in %switches) into a perl code.# args: $self - registry blessed object# rtrn: a Perl snippet to be put at the beginning of the CODE field# by caller#########################################################################my %switches = ( 'T' => sub { Apache2::ServerRec::warn("-T switch is ignored, enable " . "with 'PerlSwitches -T' in httpd.conf\n") unless ${^TAINT}; ""; }, 'w' => sub { "use warnings;\n" },);sub shebang_to_perl { my $self = shift; my ($line) = ${ $self->{CODE} } =~ /^(.*)$/m; my @cmdline = split /\s+/, $line; return "" unless @cmdline; return "" unless shift(@cmdline) =~ /^\#!/; my $prepend = ""; for my $s (@cmdline) { next unless $s =~ s/^-//; last if substr($s,0,1) eq "-"; for (split //, $s) { next unless exists $switches{$_}; $prepend .= $switches{$_}->(); } } return $prepend;}########################################################################## func: get_script_name# dflt: get_script_name# desc: get the script's name to set into $0# args: $self - registry blessed object# rtrn: path to the script's filename#########################################################################sub get_script_name { shift->{FILENAME};}########################################################################## func: chdir_file# dflt: NOP# desc: chdirs into $dir# args: $self - registry blessed object# $dir - a dir # rtrn: nothing (?or success/failure?)#########################################################################*chdir_file = \&NOP;sub chdir_file_normal { my ($self, $dir) = @_; $dir ||= File::Basename::dirname($self->{FILENAME}); $self->debug("chdir $dir") if DEBUG & D_NOISE; chdir $dir or die "Can't chdir to $dir: $!";}########################################################################## func: get_mark_line# dflt: get_mark_line# desc: generates the perl compiler #line directive# args: $self - registry blessed object# rtrn: returns the perl compiler #line directive#########################################################################sub get_mark_line { my $self = shift; $ModPerl::Registry::MarkLine ? "\n#line 1 $self->{FILENAME}\n" : "";}########################################################################## func: strip_end_data_segment# dflt: strip_end_data_segment# desc: remove the trailing non-code from $self->{CODE}# args: $self - registry blessed object# rtrn: nothing#########################################################################sub strip_end_data_segment { ${ +shift->{CODE} } =~ s/^__(END|DATA)__(.*)//ms;}########################################################################## func: compile# dflt: compile# desc: compile the code in $eval# args: $self - registry blessed object# $eval - a ref to a scalar with the code to compile# rtrn: success/failure# note: $r must not be in scope of compile(), scripts must do# my $r = shift; to get it off the args stack#########################################################################sub compile { my ($self, $eval) = @_; $self->debug("compiling $self->{FILENAME}") if DEBUG && D_COMPILE; ModPerl::Global::special_list_register(END => $self->{PACKAGE}); ModPerl::Global::special_list_clear( END => $self->{PACKAGE}); { # let the code define its own warn and strict level no strict; no warnings FATAL => 'all'; # because we use FATAL eval $$eval; } return $self->error_check;}########################################################################## func: error_check# dflt: error_check# desc: checks $@ for errors# args: $self - registry blessed object# rtrn: Apache2::Const::SERVER_ERROR if $@ is set, Apache2::Const::OK otherwise#########################################################################sub error_check { my $self = shift; # ModPerl::Util::exit() throws an exception object whose rc is # ModPerl::EXIT # (see modperl_perl_exit() and modperl_errsv() C functions) if ($@ && !(ref $@ eq 'APR::Error' && $@ == ModPerl::EXIT)) { $self->log_error($@); return Apache2::Const::SERVER_ERROR; } return Apache2::Const::OK;}########################################################################## func: install_aliases# dflt: install_aliases# desc: install the method aliases into $class# args: $class - the class to install the methods into# $rh_aliases - a ref to a hash with aliases mapping# rtrn: nothing#########################################################################sub install_aliases { my ($class, $rh_aliases) = @_; no strict 'refs'; while (my ($k,$v) = each %$rh_aliases) { if (my $sub = *{$v}{CODE}){ *{ $class . "::$k" } = $sub; } else { die "$class: $k aliasing failed; sub $v doesn't exist"; } }}### helper methodssub debug { my $self = shift; my $class = ref $self; $self->{REQ}->log_error("$$: $class: " . join '', @_);}sub log_error { my ($self, $msg) = @_; my $class = ref $self; $self->{REQ}->log_error($msg); $self->{REQ}->notes->set('error-notes' => $msg); $@{$self->{URI}} = $msg;}########################################################################## func: uncache_myself# dflt: uncache_myself# desc: unmark the package as cached by forgetting its modification time# args: none# rtrn: nothing# note: this is a function and not a method, it should be called from# the registry script, and using the caller() method we figure# out the package the script was compiled into########################################################################## this is a function should be called from the registry script, and# using the caller() method we figure out the package the script was# compiled into and trying to uncache it.## it's currently used only for testing purposes and not a part of the# public interface. it expects to find the compiled package in the# symbol table cache returned by cache_table_common(), if you override# cache_table() to point to another function, this function will fail.sub uncache_myself { my $package = scalar caller; my ($class) = __PACKAGE__->cache_table_common(); unless (defined $class) { Apache2->warn("$$: cannot figure out cache symbol table for $package"); return; } if (exists $class->{$package} && exists $class->{$package}{mtime}) { Apache2->warn("$$: uncaching $package\n") if DEBUG & D_COMPILE; delete $class->{$package}{mtime}; } else { Apache2->warn("$$: cannot find $package in cache"); }}# XXX: should go away when finfo() is ported to 2.0 (don't want to# depend on compat.pm)sub Apache2::RequestRec::my_finfo { my $r = shift; stat $r->filename; \*_;}1;__END__=head1 NAMEModPerl::RegistryCooker - Cook mod_perl 2.0 Registry Modules=head1 Synopsis # shouldn't be used as-is but sub-classed first # see ModPerl::Registry for an example=head1 DescriptionC<ModPerl::RegistryCooker> is used to create flexible and overridableregistry modules which emulate mod_cgi for Perl scripts. The conceptsare discussed in the manpage of the following modules:C<L<ModPerl::Registry>>, C<L<ModPerl::Registry>> andC<L<ModPerl::RegistryBB>>.C<ModPerl::RegistryCooker> has two purposes:=over=item *Provide ingredients that can be used by registry sub-classes=item *Provide a default behavior, which can be overriden in sub-classedMETA: in the future this functionality may move into a separate class.=backHere are the current overridable methods:META: these are all documented in RegistryCooker.pm, though not usingpod. please help to port these to pod and move the descriptions here.=over=item * new()create the class's object, bless it and return it my $obj = $class->new($r);C<$class> -- the registry class, usually C<__PACKAGE__> can be used.C<$r> -- C<L<Apache2::Request>> object.default: new()=item * init()initializes the data object's fields: C<REQ>, C<FILENAME>,C<URI>. Called from the new().default: init()=item * default_handler()default: default_handler()=item * run()default: run()=item * can_compile()default: can_compile()=item * make_namespace()default: make_namespace()=item * namespace_root()default: namespace_root()=item * namespace_from()If C<namespace_from_uri> is used and the script is called from thevirtual host, by default the virtual host name is prepended to the uriwhen package name for the compiled script is created. Sometimes thisbehavior is undesirable, e.g., when the same (physical) script isaccessed using the same path_info but different virtual hosts. In thatcase you can make the script compiled only once for all vhosts, byspecifying: $ModPerl::RegistryCooker::NameWithVirtualHost = 0;The drawback is that it affects the global environment and all otherscripts will be compiled ignoring virtual hosts.default: namespace_from()=item * is_cached()default: is_cached()=item * should_compile()default: should_compile()=item * flush_namespace()default: flush_namespace()=item * cache_table()default: cache_table()=item * cache_it()default: cache_it()=item * read_script()default: read_script()=item * shebang_to_perl()default: shebang_to_perl()=item * get_script_name()default: get_script_name()=item * chdir_file()default: chdir_file()=item * get_mark_line()default: get_mark_line()=item * compile()default: compile()=item * error_check()default: error_check()=item * strip_end_data_segment()default: strip_end_data_segment()=item * convert_script_to_compiled_handler()default: convert_script_to_compiled_handler()=back=head2 Special Predefined FunctionsThe following functions are implemented as constants.=over=item * NOP()Use when the function shouldn't do anything.=item * TRUE()Use when a function should always return a true value.=item * FALSE()Use when a function should always return a false value.=back=head1 Sub-classing TechniquesTo override the default C<ModPerl::RegistryCooker> methods, first,sub-class C<ModPerl::RegistryCooker> or one of its existingsub-classes, using C<use base>. Second, override the methods.Those methods that weren't overridden will be resolved at run timewhen used for the first time and cached for the future requests. Oneway to to shortcut this first run resolution is to use the symbolaliasing feature. For example to alias C<ModPerl::MyRegistry::flush_namespace>as C<ModPerl::RegistryCooker::flush_namespace>, you can do: package ModPerl::MyRegistry; use base qw(ModPerl::RegistryCooker); *ModPerl::MyRegistry::flush_namespace = \&ModPerl::RegistryCooker::flush_namespace; 1;In fact, it's a good idea to explicitly alias all the methods so youknow exactly what functions are used, rather then relying on thedefaults. For that purpose C<ModPerl::RegistryCooker> class methodinstall_aliases() can be used. Simply prepare a hash with method namesin the current package as keys and corresponding fully qualifiedmethods to be aliased for as values and pass it toinstall_aliases(). Continuing our example we could do: package ModPerl::MyRegistry; use base qw(ModPerl::RegistryCooker); my %aliases = ( flush_namespace => 'ModPerl::RegistryCooker::flush_namespace', ); __PACKAGE__->install_aliases(\%aliases); 1;The values use fully qualified packages so you can mix methods fromdifferent classes.=head1 ExamplesThe best examples are existing core registry modules:C<L<ModPerl::Registry>>, C<L<ModPerl::Registry>> andC<L<ModPerl::RegistryBB>>. Look at the source code and their manpagesto see how they subclass C<ModPerl::RegistryCooker>.For example by default C<L<ModPerl::Registry>> uses the script's pathwhen creating a package's namespace. If for example you want to use auri instead you can override it with: *ModPerl::MyRegistry::namespace_from = \&ModPerl::RegistryCooker::namespace_from_uri; 1;Since the C<namespace_from_uri> component already exists inC<ModPerl::RegistryCooker>. If you want to write your own method,e.g., that creates a namespace based on the inode, you can do: sub namespace_from_inode { my $self = shift; return (stat $self->[FILENAME])[1]; }META: when $r-E<gt>finfo will be ported it'll be more effecient. (stat $r-E<gt>finfo)[1]=head1 AuthorsDoug MacEachernStas Bekman=head1 See AlsoC<L<ModPerl::Registry|docs::2.0::api::ModPerl::Registry>>,C<L<ModPerl::RegistryBB|docs::2.0::api::ModPerl::RegistryBB>> andC<L<ModPerl::PerlRun|docs::2.0::api::ModPerl::PerlRun>>.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -