📄 cgi.pm
字号:
package CGI;require 5.004;use Carp 'croak';# See the bottom of this file for the POD documentation. Search for the# string '=head'.# You can run this file through either pod2man or pod2html to produce pretty# documentation in manual or html file format (these utilities are part of the# Perl 5 distribution).# Copyright 1995-1998 Lincoln D. Stein. All rights reserved.# It may be used and modified freely, but I do request that this copyright# notice remain attached to the file. You may modify this module as you # wish, but if you redistribute a modified version, please attach a note# listing the modifications you have made.# The most recent version and complete docs are available at:# http://stein.cshl.org/WWW/software/CGI/$CGI::revision = '$Id: CGI.pm,v 1.234 2007/04/16 16:58:46 lstein Exp $';$CGI::VERSION='3.29';# HARD-CODED LOCATION FOR FILE UPLOAD TEMPORARY FILES.# UNCOMMENT THIS ONLY IF YOU KNOW WHAT YOU'RE DOING.# $CGITempFile::TMPDIRECTORY = '/usr/tmp';use CGI::Util qw(rearrange make_attributes unescape escape expires ebcdic2ascii ascii2ebcdic);#use constant XHTML_DTD => ['-//W3C//DTD XHTML Basic 1.0//EN',# 'http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd'];use constant XHTML_DTD => ['-//W3C//DTD XHTML 1.0 Transitional//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'];{ local $^W = 0; $TAINTED = substr("$0$^X",0,0);}$MOD_PERL = 0; # no mod_perl by default@SAVED_SYMBOLS = ();# >>>>> Here are some globals that you might want to adjust <<<<<<sub initialize_globals { # Set this to 1 to enable copious autoloader debugging messages $AUTOLOAD_DEBUG = 0; # Set this to 1 to generate XTML-compatible output $XHTML = 1; # Change this to the preferred DTD to print in start_html() # or use default_dtd('text of DTD to use'); $DEFAULT_DTD = [ '-//W3C//DTD HTML 4.01 Transitional//EN', 'http://www.w3.org/TR/html4/loose.dtd' ] ; # Set this to 1 to enable NOSTICKY scripts # or: # 1) use CGI qw(-nosticky) # 2) $CGI::nosticky(1) $NOSTICKY = 0; # Set this to 1 to enable NPH scripts # or: # 1) use CGI qw(-nph) # 2) CGI::nph(1) # 3) print header(-nph=>1) $NPH = 0; # Set this to 1 to enable debugging from @ARGV # Set to 2 to enable debugging from STDIN $DEBUG = 1; # Set this to 1 to make the temporary files created # during file uploads safe from prying eyes # or do... # 1) use CGI qw(:private_tempfiles) # 2) CGI::private_tempfiles(1); $PRIVATE_TEMPFILES = 0; # Set this to 1 to generate automatic tab indexes $TABINDEX = 0; # Set this to 1 to cause files uploaded in multipart documents # to be closed, instead of caching the file handle # or: # 1) use CGI qw(:close_upload_files) # 2) $CGI::close_upload_files(1); # Uploads with many files run out of file handles. # Also, for performance, since the file is already on disk, # it can just be renamed, instead of read and written. $CLOSE_UPLOAD_FILES = 0; # Set this to a positive value to limit the size of a POSTing # to a certain number of bytes: $POST_MAX = -1; # Change this to 1 to disable uploads entirely: $DISABLE_UPLOADS = 0; # Automatically determined -- don't change $EBCDIC = 0; # Change this to 1 to suppress redundant HTTP headers $HEADERS_ONCE = 0; # separate the name=value pairs by semicolons rather than ampersands $USE_PARAM_SEMICOLONS = 1; # Do not include undefined params parsed from query string # use CGI qw(-no_undef_params); $NO_UNDEF_PARAMS = 0; # Other globals that you shouldn't worry about. undef $Q; $BEEN_THERE = 0; $DTD_PUBLIC_IDENTIFIER = ""; undef @QUERY_PARAM; undef %EXPORT; undef $QUERY_CHARSET; undef %QUERY_FIELDNAMES; undef %QUERY_TMPFILES; # prevent complaints by mod_perl 1;}# ------------------ START OF THE LIBRARY ------------*end_form = \&endform;# make mod_perlhappyinitialize_globals();# FIGURE OUT THE OS WE'RE RUNNING UNDER# Some systems support the $^O variable. If not# available then require() the Config libraryunless ($OS) { unless ($OS = $^O) { require Config; $OS = $Config::Config{'osname'}; }}if ($OS =~ /^MSWin/i) { $OS = 'WINDOWS';} elsif ($OS =~ /^VMS/i) { $OS = 'VMS';} elsif ($OS =~ /^dos/i) { $OS = 'DOS';} elsif ($OS =~ /^MacOS/i) { $OS = 'MACINTOSH';} elsif ($OS =~ /^os2/i) { $OS = 'OS2';} elsif ($OS =~ /^epoc/i) { $OS = 'EPOC';} elsif ($OS =~ /^cygwin/i) { $OS = 'CYGWIN';} else { $OS = 'UNIX';}# Some OS logic. Binary mode enabled on DOS, NT and VMS$needs_binmode = $OS=~/^(WINDOWS|DOS|OS2|MSWin|CYGWIN)/;# This is the default class for the CGI object to use when all else fails.$DefaultClass = 'CGI' unless defined $CGI::DefaultClass;# This is where to look for autoloaded routines.$AutoloadClass = $DefaultClass unless defined $CGI::AutoloadClass;# The path separator is a slash, backslash or semicolon, depending# on the paltform.$SL = { UNIX => '/', OS2 => '\\', EPOC => '/', CYGWIN => '/', WINDOWS => '\\', DOS => '\\', MACINTOSH => ':', VMS => '/' }->{$OS};# This no longer seems to be necessary# Turn on NPH scripts by default when running under IIS server!# $NPH++ if defined($ENV{'SERVER_SOFTWARE'}) && $ENV{'SERVER_SOFTWARE'}=~/IIS/;$IIS++ if defined($ENV{'SERVER_SOFTWARE'}) && $ENV{'SERVER_SOFTWARE'}=~/IIS/;# Turn on special checking for Doug MacEachern's modperlif (exists $ENV{MOD_PERL}) { # mod_perl handlers may run system() on scripts using CGI.pm; # Make sure so we don't get fooled by inherited $ENV{MOD_PERL} if (exists $ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2) { $MOD_PERL = 2; require Apache2::Response; require Apache2::RequestRec; require Apache2::RequestUtil; require Apache2::RequestIO; require APR::Pool; } else { $MOD_PERL = 1; require Apache; }}# Turn on special checking for ActiveState's PerlEx$PERLEX++ if defined($ENV{'GATEWAY_INTERFACE'}) && $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-PerlEx/;# Define the CRLF sequence. I can't use a simple "\r\n" because the meaning# of "\n" is different on different OS's (sometimes it generates CRLF, sometimes LF# and sometimes CR). The most popular VMS web server# doesn't accept CRLF -- instead it wants a LR. EBCDIC machines don't# use ASCII, so \015\012 means something different. I find this all # really annoying.$EBCDIC = "\t" ne "\011";if ($OS eq 'VMS') { $CRLF = "\n";} elsif ($EBCDIC) { $CRLF= "\r\n";} else { $CRLF = "\015\012";}if ($needs_binmode) { $CGI::DefaultClass->binmode(\*main::STDOUT); $CGI::DefaultClass->binmode(\*main::STDIN); $CGI::DefaultClass->binmode(\*main::STDERR);}%EXPORT_TAGS = ( ':html2'=>['h1'..'h6',qw/p br hr ol ul li dl dt dd menu code var strong em tt u i b blockquote pre img a address cite samp dfn html head base body Link nextid title meta kbd start_html end_html input Select option comment charset escapeHTML/], ':html3'=>[qw/div table caption th td TR Tr sup Sub strike applet Param embed basefont style span layer ilayer font frameset frame script small big Area Map/], ':html4'=>[qw/abbr acronym bdo col colgroup del fieldset iframe ins label legend noframes noscript object optgroup Q thead tbody tfoot/], ':netscape'=>[qw/blink fontsize center/], ':form'=>[qw/textfield textarea filefield password_field hidden checkbox checkbox_group submit reset defaults radio_group popup_menu button autoEscape scrolling_list image_button start_form end_form startform endform start_multipart_form end_multipart_form isindex tmpFileName uploadInfo URL_ENCODED MULTIPART/], ':cgi'=>[qw/param upload path_info path_translated request_uri url self_url script_name cookie Dump raw_cookie request_method query_string Accept user_agent remote_host content_type remote_addr referer server_name server_software server_port server_protocol virtual_port virtual_host remote_ident auth_type http append save_parameters restore_parameters param_fetch remote_user user_name header redirect import_names put Delete Delete_all url_param cgi_error/], ':ssl' => [qw/https/], ':cgi-lib' => [qw/ReadParse PrintHeader HtmlTop HtmlBot SplitParam Vars/], ':html' => [qw/:html2 :html3 :html4 :netscape/], ':standard' => [qw/:html2 :html3 :html4 :form :cgi/], ':push' => [qw/multipart_init multipart_start multipart_end multipart_final/], ':all' => [qw/:html2 :html3 :netscape :form :cgi :internal :html4/] );# Custom 'can' method for both autoloaded and non-autoloaded subroutines.# Author: Cees Hek <cees@sitesuite.com.au>sub can { my($class, $method) = @_; # See if UNIVERSAL::can finds it. if (my $func = $class -> SUPER::can($method) ){ return $func; } # Try to compile the function. eval { # _compile looks at $AUTOLOAD for the function name. local $AUTOLOAD = join "::", $class, $method; &_compile; }; # Now that the function is loaded (if it exists) # just use UNIVERSAL::can again to do the work. return $class -> SUPER::can($method);}# to import symbols into callersub import { my $self = shift; # This causes modules to clash. undef %EXPORT_OK; undef %EXPORT; $self->_setup_symbols(@_); my ($callpack, $callfile, $callline) = caller; # To allow overriding, search through the packages # Till we find one in which the correct subroutine is defined. my @packages = ($self,@{"$self\:\:ISA"}); foreach $sym (keys %EXPORT) { my $pck; my $def = ${"$self\:\:AutoloadClass"} || $DefaultClass; foreach $pck (@packages) { if (defined(&{"$pck\:\:$sym"})) { $def = $pck; last; } } *{"${callpack}::$sym"} = \&{"$def\:\:$sym"}; }}sub compile { my $pack = shift; $pack->_setup_symbols('-compile',@_);}sub expand_tags { my($tag) = @_; return ("start_$1","end_$1") if $tag=~/^(?:\*|start_|end_)(.+)/; my(@r); return ($tag) unless $EXPORT_TAGS{$tag}; foreach (@{$EXPORT_TAGS{$tag}}) { push(@r,&expand_tags($_)); } return @r;}#### Method: new# The new routine. This will check the current environment# for an existing query string, and initialize itself, if so.####sub new { my($class,@initializer) = @_; my $self = {}; bless $self,ref $class || $class || $DefaultClass; # always use a tempfile $self->{'use_tempfile'} = 1; if (ref($initializer[0]) && (UNIVERSAL::isa($initializer[0],'Apache') || UNIVERSAL::isa($initializer[0],'Apache2::RequestRec') )) { $self->r(shift @initializer); } if (ref($initializer[0]) && (UNIVERSAL::isa($initializer[0],'CODE'))) { $self->upload_hook(shift @initializer, shift @initializer); $self->{'use_tempfile'} = shift @initializer if (@initializer > 0); } if ($MOD_PERL) { if ($MOD_PERL == 1) { $self->r(Apache->request) unless $self->r; my $r = $self->r; $r->register_cleanup(\&CGI::_reset_globals); } else { # XXX: once we have the new API # will do a real PerlOptions -SetupEnv check $self->r(Apache2::RequestUtil->request) unless $self->r; my $r = $self->r; $r->subprocess_env unless exists $ENV{REQUEST_METHOD}; $r->pool->cleanup_register(\&CGI::_reset_globals); } undef $NPH; } $self->_reset_globals if $PERLEX; $self->init(@initializer); return $self;}# We provide a DESTROY method so that we can ensure that# temporary files are closed (via Fh->DESTROY) before they# are unlinked (via CGITempFile->DESTROY) because it is not# possible to unlink an open file on Win32. We explicitly# call DESTROY on each, rather than just undefing them and# letting Perl DESTROY them by garbage collection, in case the# user is still holding any reference to them as well.sub DESTROY { my $self = shift; if ($OS eq 'WINDOWS') { foreach my $href (values %{$self->{'.tmpfiles'}}) { $href->{hndl}->DESTROY if defined $href->{hndl}; $href->{name}->DESTROY if defined $href->{name}; } }}sub r { my $self = shift; my $r = $self->{'.r'}; $self->{'.r'} = shift if @_; $r;}sub upload_hook { my $self; if (ref $_[0] eq 'CODE') { $CGI::Q = $self = $CGI::DefaultClass->new(@_); } else { $self = shift; } my ($hook,$data,$use_tempfile) = @_; $self->{'.upload_hook'} = $hook; $self->{'.upload_data'} = $data; $self->{'use_tempfile'} = $use_tempfile if defined $use_tempfile;}#### Method: param# Returns the value(s)of a named parameter.# If invoked in a list context, returns the# entire list. Otherwise returns the first# member of the list.# If name is not provided, return a list of all# the known parameters names available.# If more than one argument is provided, the# second and subsequent arguments are used to# set the value of the parameter.####sub param { my($self,@p) = self_or_default(@_); return $self->all_parameters unless @p; my($name,$value,@other); # For compatibility between old calling style and use_named_parameters() style, # we have to special case for a single parameter present. if (@p > 1) { ($name,$value,@other) = rearrange([NAME,[DEFAULT,VALUE,VALUES]],@p); my(@values);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -