📄 cgi.pm
字号:
if (substr($p[0],0,1) eq '-') { @values = defined($value) ? (ref($value) && ref($value) eq 'ARRAY' ? @{$value} : $value) : (); } else { foreach ($value,@other) { push(@values,$_) if defined($_); } } # If values is provided, then we set it. if (@values or defined $value) { $self->add_parameter($name); $self->{$name}=[@values]; } } else { $name = $p[0]; } return unless defined($name) && $self->{$name}; my $charset = $self->charset || ''; my $utf8 = $charset eq 'utf-8'; if ($utf8) { eval "require Encode; 1;" if $utf8 && !Encode->can('decode'); # bring in these functions return wantarray ? map {Encode::decode(utf8=>$_) } @{$self->{$name}} : Encode::decode(utf8=>$self->{$name}->[0]); } else { return wantarray ? @{$self->{$name}} : $self->{$name}->[0]; }}sub self_or_default { return @_ if defined($_[0]) && (!ref($_[0])) &&($_[0] eq 'CGI'); unless (defined($_[0]) && (ref($_[0]) eq 'CGI' || UNIVERSAL::isa($_[0],'CGI')) # slightly optimized for common case ) { $Q = $CGI::DefaultClass->new unless defined($Q); unshift(@_,$Q); } return wantarray ? @_ : $Q;}sub self_or_CGI { local $^W=0; # prevent a warning if (defined($_[0]) && (substr(ref($_[0]),0,3) eq 'CGI' || UNIVERSAL::isa($_[0],'CGI'))) { return @_; } else { return ($DefaultClass,@_); }}######################################### THESE METHODS ARE MORE OR LESS PRIVATE# GO TO THE __DATA__ SECTION TO SEE MORE# PUBLIC METHODS######################################### Initialize the query object from the environment.# If a parameter list is found, this object will be set# to an associative array in which parameter names are keys# and the values are stored as lists# If a keyword list is found, this method creates a bogus# parameter list with the single parameter 'keywords'.sub init { my $self = shift; my($query_string,$meth,$content_length,$fh,@lines) = ('','','',''); my $is_xforms; my $initializer = shift; # for backward compatibility local($/) = "\n"; # set autoescaping on by default $self->{'escape'} = 1; # if we get called more than once, we want to initialize # ourselves from the original query (which may be gone # if it was read from STDIN originally.) if (defined(@QUERY_PARAM) && !defined($initializer)) { for my $name (@QUERY_PARAM) { my $val = $QUERY_PARAM{$name}; # always an arrayref; $self->param('-name'=>$name,'-value'=> $val); if (defined $val and ref $val eq 'ARRAY') { for my $fh (grep {defined(fileno($_))} @$val) { seek($fh,0,0); # reset the filehandle. } } } $self->charset($QUERY_CHARSET); $self->{'.fieldnames'} = {%QUERY_FIELDNAMES}; $self->{'.tmpfiles'} = {%QUERY_TMPFILES}; return; } $meth=$ENV{'REQUEST_METHOD'} if defined($ENV{'REQUEST_METHOD'}); $content_length = defined($ENV{'CONTENT_LENGTH'}) ? $ENV{'CONTENT_LENGTH'} : 0; $fh = to_filehandle($initializer) if $initializer; # set charset to the safe ISO-8859-1 $self->charset('ISO-8859-1'); METHOD: { # avoid unreasonably large postings if (($POST_MAX > 0) && ($content_length > $POST_MAX)) { #discard the post, unread $self->cgi_error("413 Request entity too large"); last METHOD; } # Process multipart postings, but only if the initializer is # not defined. if ($meth eq 'POST' && defined($ENV{'CONTENT_TYPE'}) && $ENV{'CONTENT_TYPE'}=~m|^multipart/form-data| && !defined($initializer) ) { my($boundary) = $ENV{'CONTENT_TYPE'} =~ /boundary=\"?([^\";,]+)\"?/; $self->read_multipart($boundary,$content_length); last METHOD; } # Process XForms postings. We know that we have XForms in the # following cases: # method eq 'POST' && content-type eq 'application/xml' # method eq 'POST' && content-type =~ /multipart\/related.+start=/ # There are more cases, actually, but for now, we don't support other # methods for XForm posts. # In a XForm POST, the QUERY_STRING is parsed normally. # If the content-type is 'application/xml', we just set the param # XForms:Model (referring to the xml syntax) param containing the # unparsed XML data. # In the case of multipart/related we set XForms:Model as above, but # the other parts are available as uploads with the Content-ID as the # the key. # See the URL below for XForms specs on this issue. # http://www.w3.org/TR/2006/REC-xforms-20060314/slice11.html#submit-options if ($meth eq 'POST' && defined($ENV{'CONTENT_TYPE'})) { if ($ENV{'CONTENT_TYPE'} eq 'application/xml') { my($param) = 'XForms:Model'; my($value) = ''; $self->add_parameter($param); $self->read_from_client(\$value,$content_length,0) if $content_length > 0; push (@{$self->{$param}},$value); $is_xforms = 1; } elsif ($ENV{'CONTENT_TYPE'} =~ /multipart\/related.+boundary=\"?([^\";,]+)\"?.+start=\"?\<?([^\"\>]+)\>?\"?/) { my($boundary,$start) = ($1,$2); my($param) = 'XForms:Model'; $self->add_parameter($param); my($value) = $self->read_multipart_related($start,$boundary,$content_length,0); push (@{$self->{$param}},$value); if ($MOD_PERL) { $query_string = $self->r->args; } else { $query_string = $ENV{'QUERY_STRING'} if defined $ENV{'QUERY_STRING'}; $query_string ||= $ENV{'REDIRECT_QUERY_STRING'} if defined $ENV{'REDIRECT_QUERY_STRING'}; } $is_xforms = 1; } } # If initializer is defined, then read parameters # from it. if (!$is_xforms && defined($initializer)) { if (UNIVERSAL::isa($initializer,'CGI')) { $query_string = $initializer->query_string; last METHOD; } if (ref($initializer) && ref($initializer) eq 'HASH') { foreach (keys %$initializer) { $self->param('-name'=>$_,'-value'=>$initializer->{$_}); } last METHOD; } if (defined($fh) && ($fh ne '')) { while (<$fh>) { chomp; last if /^=/; push(@lines,$_); } # massage back into standard format if ("@lines" =~ /=/) { $query_string=join("&",@lines); } else { $query_string=join("+",@lines); } last METHOD; } # last chance -- treat it as a string $initializer = $$initializer if ref($initializer) eq 'SCALAR'; $query_string = $initializer; last METHOD; } # If method is GET or HEAD, fetch the query from # the environment. if ($is_xforms || $meth=~/^(GET|HEAD)$/) { if ($MOD_PERL) { $query_string = $self->r->args; } else { $query_string = $ENV{'QUERY_STRING'} if defined $ENV{'QUERY_STRING'}; $query_string ||= $ENV{'REDIRECT_QUERY_STRING'} if defined $ENV{'REDIRECT_QUERY_STRING'}; } last METHOD; } if ($meth eq 'POST') { $self->read_from_client(\$query_string,$content_length,0) if $content_length > 0; # Some people want to have their cake and eat it too! # Uncomment this line to have the contents of the query string # APPENDED to the POST data. # $query_string .= (length($query_string) ? '&' : '') . $ENV{'QUERY_STRING'} if defined $ENV{'QUERY_STRING'}; last METHOD; } # If $meth is not of GET, POST or HEAD, assume we're being debugged offline. # Check the command line and then the standard input for data. # We use the shellwords package in order to behave the way that # UN*X programmers expect. if ($DEBUG) { my $cmdline_ret = read_from_cmdline(); $query_string = $cmdline_ret->{'query_string'}; if (defined($cmdline_ret->{'subpath'})) { $self->path_info($cmdline_ret->{'subpath'}); } } }# YL: Begin Change for XML handler 10/19/2001 if (!$is_xforms && $meth eq 'POST' && defined($ENV{'CONTENT_TYPE'}) && $ENV{'CONTENT_TYPE'} !~ m|^application/x-www-form-urlencoded| && $ENV{'CONTENT_TYPE'} !~ m|^multipart/form-data| ) { my($param) = 'POSTDATA' ; $self->add_parameter($param) ; push (@{$self->{$param}},$query_string); undef $query_string ; }# YL: End Change for XML handler 10/19/2001 # We now have the query string in hand. We do slightly # different things for keyword lists and parameter lists. if (defined $query_string && length $query_string) { if ($query_string =~ /[&=;]/) { $self->parse_params($query_string); } else { $self->add_parameter('keywords'); $self->{'keywords'} = [$self->parse_keywordlist($query_string)]; } } # Special case. Erase everything if there is a field named # .defaults. if ($self->param('.defaults')) { $self->delete_all(); } # Associative array containing our defined fieldnames $self->{'.fieldnames'} = {}; foreach ($self->param('.cgifields')) { $self->{'.fieldnames'}->{$_}++; } # Clear out our default submission button flag if present $self->delete('.submit'); $self->delete('.cgifields'); $self->save_request unless defined $initializer;}# FUNCTIONS TO OVERRIDE:# Turn a string into a filehandlesub to_filehandle { my $thingy = shift; return undef unless $thingy; return $thingy if UNIVERSAL::isa($thingy,'GLOB'); return $thingy if UNIVERSAL::isa($thingy,'FileHandle'); if (!ref($thingy)) { my $caller = 1; while (my $package = caller($caller++)) { my($tmp) = $thingy=~/[\':]/ ? $thingy : "$package\:\:$thingy"; return $tmp if defined(fileno($tmp)); } } return undef;}# send output to the browsersub put { my($self,@p) = self_or_default(@_); $self->print(@p);}# print to standard output (for overriding in mod_perl)sub print { shift; CORE::print(@_);}# get/set last cgi_errorsub cgi_error { my ($self,$err) = self_or_default(@_); $self->{'.cgi_error'} = $err if defined $err; return $self->{'.cgi_error'};}sub save_request { my($self) = @_; # We're going to play with the package globals now so that if we get called # again, we initialize ourselves in exactly the same way. This allows # us to have several of these objects. @QUERY_PARAM = $self->param; # save list of parameters foreach (@QUERY_PARAM) { next unless defined $_; $QUERY_PARAM{$_}=$self->{$_}; } $QUERY_CHARSET = $self->charset; %QUERY_FIELDNAMES = %{$self->{'.fieldnames'}}; %QUERY_TMPFILES = %{ $self->{'.tmpfiles'} || {} };}sub parse_params { my($self,$tosplit) = @_; my(@pairs) = split(/[&;]/,$tosplit); my($param,$value); foreach (@pairs) { ($param,$value) = split('=',$_,2); next unless defined $param; next if $NO_UNDEF_PARAMS and not defined $value; $value = '' unless defined $value; $param = unescape($param); $value = unescape($value); $self->add_parameter($param); push (@{$self->{$param}},$value); }}sub add_parameter { my($self,$param)=@_; return unless defined $param; push (@{$self->{'.parameters'}},$param) unless defined($self->{$param});}sub all_parameters { my $self = shift; return () unless defined($self) && $self->{'.parameters'}; return () unless @{$self->{'.parameters'}}; return @{$self->{'.parameters'}};}# put a filehandle into binary mode (DOS)sub binmode { return unless defined($_[1]) && defined fileno($_[1]); CORE::binmode($_[1]);}sub _make_tag_func { my ($self,$tagname) = @_; my $func = qq( sub $tagname { my (\$q,\$a,\@rest) = self_or_default(\@_); my(\$attr) = ''; if (ref(\$a) && ref(\$a) eq 'HASH') { my(\@attr) = make_attributes(\$a,\$q->{'escape'}); \$attr = " \@attr" if \@attr; } else { unshift \@rest,\$a if defined \$a; } ); if ($tagname=~/start_(\w+)/i) { $func .= qq! return "<\L$1\E\$attr>";} !; } elsif ($tagname=~/end_(\w+)/i) { $func .= qq! return "<\L/$1\E>"; } !; } else { $func .= qq# return \$XHTML ? "\L<$tagname\E\$attr />" : "\L<$tagname\E\$attr>" unless \@rest; my(\$tag,\$untag) = ("\L<$tagname\E\$attr>","\L</$tagname>\E"); my \@result = map { "\$tag\$_\$untag" } (ref(\$rest[0]) eq 'ARRAY') ? \@{\$rest[0]} : "\@rest"; return "\@result"; }#; }return $func;}sub AUTOLOAD { print STDERR "CGI::AUTOLOAD for $AUTOLOAD\n" if $CGI::AUTOLOAD_DEBUG; my $func = &_compile; goto &$func;}sub _compile { my($func) = $AUTOLOAD; my($pack,$func_name); { local($1,$2); # this fixes an obscure variable suicide problem. $func=~/(.+)::([^:]+)$/; ($pack,$func_name) = ($1,$2); $pack=~s/::SUPER$//; # fix another obscure problem $pack = ${"$pack\:\:AutoloadClass"} || $CGI::DefaultClass unless defined(${"$pack\:\:AUTOLOADED_ROUTINES"}); my($sub) = \%{"$pack\:\:SUBS"}; unless (%$sub) { my($auto) = \${"$pack\:\:AUTOLOADED_ROUTINES"}; local ($@,$!); eval "package $pack; $$auto"; croak("$AUTOLOAD: $@") if $@; $$auto = ''; # Free the unneeded storage (but don't undef it!!!) } my($code) = $sub->{$func_name}; $code = "sub $AUTOLOAD { }" if (!$code and $func_name eq 'DESTROY'); if (!$code) { (my $base = $func_name) =~ s/^(start_|end_)//i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -