X-Git-Url: http://git.shiar.nl/perl/plp/.git/blobdiff_plain/972d413021a3b834599c46ea8a05ae4c0e023029..4565100c67dd7b0344e9eb332296d5fa64e7611b:/PLP/Fields.pm diff --git a/PLP/Fields.pm b/PLP/Fields.pm index 14d30b6..7f07c4e 100644 --- a/PLP/Fields.pm +++ b/PLP/Fields.pm @@ -1,68 +1,126 @@ -#----------------------# - package PLP::Fields; -#----------------------# +package PLP::Fields; + use strict; -=head1 PLP::Fields +# Has only one function: doit(), which ties the hashes %get, %post, %fields +# and %header in PLP::Script. Also generates %cookie immediately. +sub doit { -Has only one function: doit(), which ties the hashes %get, %post, %fields and %header in -PLP::Script. Also generates %cookie immediately. + # %get + + my $get = \%PLP::Script::get; + if (length $ENV{QUERY_STRING}){ + for (split /[&;]/, $ENV{QUERY_STRING}) { + my @keyval = split /=/, $_, 2; + PLP::Functions::DecodeURI(@keyval); + $get->{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/; + push @{ $get->{ '@' . $keyval[0] } }, $keyval[1]; + } + } - PLP::Fields::doit(); + # %post -This module is part of the PLP internals. Don't use it yourself. + tie %PLP::Script::post, 'PLP::Tie::Delay', 'PLP::Script::post', sub { + my %post; + my $post; + + return \%post if $ENV{CONTENT_TYPE} !~ + m!^(?:application/x-www-form-urlencoded|$)!; + + $post = $PLP::read->($ENV{CONTENT_LENGTH}) if $ENV{CONTENT_LENGTH}; + + return \%post unless defined $post and length $post; + + for (split /&/, $post) { + my @keyval = split /=/, $_, 2; + PLP::Functions::DecodeURI(@keyval); + $post{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/; + push @{ $post{ '@' . $keyval[0] } }, $keyval[1]; + } + + return \%post; + }; -=cut + # %fields -sub doit { - tie %PLP::Script::get, 'PLP::Tie::Delay', 'PLP::Script::get', sub { - my %get; - my $get; - $get = $ENV{QUERY_STRING}; - if ($get ne ''){ - for (split /[&;]/, $get) { - my @keyval = split /=/, $_, 2; - PLP::Functions::DecodeURI(@keyval); - $get{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/; - push @{ $get{'@' . $keyval[0]} }, $keyval[1]; - } - } - return \%get; - }; - - tie %PLP::Script::post, 'PLP::Tie::Delay', 'PLP::Script::post', sub { - my %post; - my $post; - if ($ENV{MOD_PERL}) { - $post = Apache->request->content; - } else { - read(*STDIN, $post, $ENV{CONTENT_LENGTH}); - } - if (defined($post) && $post ne '' && - ($ENV{CONTENT_TYPE} eq '' || $ENV{CONTENT_TYPE} eq 'application/x-www-form-urlencoded')){ - for (split /&/, $post) { - my @keyval = split /=/, $_, 2; - PLP::Functions::DecodeURI(@keyval); - $post{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/; - push @{ $post{'@' . $keyval[0]} }, $keyval[1]; - } - } - return \%post; - }; + tie %PLP::Script::fields, 'PLP::Tie::Delay', 'PLP::Script::fields', sub { + return { %PLP::Script::get, %PLP::Script::post }; + }; - tie %PLP::Script::fields, 'PLP::Tie::Delay', 'PLP::Script::fields', sub { - $PLP::Script::get{PLPdummy}, $PLP::Script::post{PLPdummy}; # Trigger creation - return {%PLP::Script::get, %PLP::Script::post} - }; + # %header - tie %PLP::Script::header, 'PLP::Tie::Headers'; + tie %PLP::Script::header, 'PLP::Tie::Headers'; - if (defined($ENV{HTTP_COOKIE}) && $ENV{HTTP_COOKIE} ne ''){ - for (split /; ?/, $ENV{HTTP_COOKIE}) { - my @keyval = split /=/, $_, 2; - $PLP::Script::cookie{$keyval[0]} ||= $keyval[1]; - } - } + # %cookie + if (defined $ENV{HTTP_COOKIE} and length $ENV{HTTP_COOKIE}) { + for (split /; ?/, $ENV{HTTP_COOKIE}) { + my @keyval = split /=/, $_, 2; + $PLP::Script::cookie{$keyval[0]} ||= $keyval[1]; + } + } } + 1; + +=head1 NAME + +PLP::Fields - Special hashes for PLP + +=head1 DESCRIPTION + +For your convenience, PLP uses hashes to put things in. Some of these are tied +hashes, so they contain a bit magic. For example, building the hash can be +delayed until you actually use the hash. + +=over 10 + +=item C<%get> and C<%post> + +These are built from the C (or C +strings in query string and post content. C<%post> is not built if the content +type is not C. In post content, the +semi-colon is not a valid separator. + +%post isn't built until it is used, to speed up your script if you +don't use it. Because POST content can only be read once, you can C +and just never access C<%post> to avoid its building. + +With a query string of C, C<$get{key}> will +contain only C. You can access both elements by using the array +reference C<$get{'@key'}>, which will contain C<[ 'firstvalue', 'secondvalue' +]>. + +=item C<%fields> + +This hash combines %get and %post, and triggers creation of %post. POST gets +precedence over GET (note: not even the C<@>-keys contain both values). + +This hash is built on first use, just like %post. + +=item C<%cookie>, C<%cookies> + +This is built immediately, because cookies are usually short in length. Cookies +are B automatically url-decoded. + +=item C<%header>, C<%headers> + +In this hash, you can set headers. Underscores are converted to normal minus +signs, so you can leave out quotes. The hash is case insensitive: the case used +when sending the headers is the one you used first. The following are equal: + + $header{CONTENT_TYPE} + $header{'Content-Type'} + $header{Content_Type} + $headers{CONTENT_type} + +=back + +=head1 AUTHOR + +Juerd Waalboer + +Current maintainer: Mischa POSLAWSKY + +=cut +