2a4190c062dd845c4e9c1f341a157799ffa3c0c9
[perl/plp/.git] / PLP / Fields.pm
1 package PLP::Fields;
2
3 use strict;
4
5 # Has only one function: doit(), which ties the hashes %get, %post, %fields
6 # and %header in PLP::Script. Also generates %cookie immediately.
7 sub doit {
8
9         # %get
10         
11         my $get = \%PLP::Script::get;
12         if (length $ENV{QUERY_STRING}){
13                 for (split /[&;]/, $ENV{QUERY_STRING}) {
14                         my @keyval = split /=/, $_, 2;
15                         PLP::Functions::DecodeURI(@keyval);
16                         $get->{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/;
17                         push @{ $get->{ '@' . $keyval[0] } }, $keyval[1];
18                 }
19         }
20
21         # %post
22
23         tie %PLP::Script::post, 'PLP::Tie::Delay', 'PLP::Script::post', sub {
24                 my %post;
25                 my $post;
26                 
27                 return \%post if $ENV{CONTENT_TYPE} !~
28                         m!^(?:application/x-www-form-urlencoded|$)!;
29                 
30                 if ($ENV{MOD_PERL}) {
31                         $post = Apache->request->content;
32                 } else {
33                         read *STDIN, $post, $ENV{CONTENT_LENGTH};
34                 }
35                 
36                 return \%post unless defined $post and length $post;
37                 
38                 for (split /&/, $post) {
39                         my @keyval = split /=/, $_, 2;
40                         PLP::Functions::DecodeURI(@keyval);
41                         $post{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/;
42                         push @{ $post{ '@' . $keyval[0] } }, $keyval[1];
43                 }
44                 
45                 return \%post;
46         };
47
48         # %fields
49
50         tie %PLP::Script::fields, 'PLP::Tie::Delay', 'PLP::Script::fields', sub {
51                 return { %PLP::Script::get, %PLP::Script::post };
52         };
53
54         # %header
55
56         tie %PLP::Script::header, 'PLP::Tie::Headers';
57
58         # %cookie
59
60         if (defined $ENV{HTTP_COOKIE} and length $ENV{HTTP_COOKIE}) {
61                 for (split /; ?/, $ENV{HTTP_COOKIE}) {
62                         my @keyval = split /=/, $_, 2;
63                         $PLP::Script::cookie{$keyval[0]} ||= $keyval[1];
64                 }
65         }
66 }
67
68 1;
69
70 =head1 NAME
71
72 PLP::Fields - Special hashes for PLP
73
74 =head1 DESCRIPTION
75
76 For your convenience, PLP uses hashes to put things in. Some of these are tied
77 hashes, so they contain a bit magic. For example, building the hash can be
78 delayed until you actually use the hash.
79
80 =over 10
81
82 =item C<%get> and C<%post>
83
84 These are built from the C<key=value&key=value> (or C<key=value;key=value>
85 strings in query string and post content. C<%post> is not built if the content
86 type is not C<application/x-www-form-urlencoded>. In post content, the
87 semi-colon is not a valid separator.
88
89 %post isn't built until it is used, to speed up your script if you
90 don't use it. Because POST content can only be read once, you can C<use CGI;>
91 and just never access C<%post> to avoid its building.
92
93 With a query string of C<key=firstvalue&key=secondvalue>, C<$get{key}> will
94 contain only C<secondvalue>. You can access both elements by using the array
95 reference C<$get{'@key'}>, which will contain C<[ 'firstvalue', 'secondvalue'
96 ]>.
97
98 =item C<%fields>
99
100 This hash combines %get and %post, and triggers creation of %post. POST gets
101 precedence over GET (note: not even the C<@>-keys contain both values).
102
103 This hash is built on first use, just like %post.
104
105 =item C<%cookie>, C<%cookies>
106
107 This is built immediately, because cookies are usually short in length. Cookies
108 are B<not> automatically url-decoded.
109
110 =item C<%header>, C<%headers>
111
112 In this hash, you can set headers. Underscores are converted to normal minus
113 signs, so you can leave out quotes. The hash is case insensitive: the case used
114 when sending the headers is the one you used first. The following are equal:
115
116     $header{CONTENT_TYPE}
117     $header{'Content-Type'}
118     $header{Content_Type}
119     $headers{CONTENT_type}
120
121 =back
122
123 =head1 AUTHOR
124
125 Juerd Waalboer <juerd@cpan.org>
126
127 Current maintainer: Mischa POSLAWSKY <shiar@cpan.org>
128
129 =cut
130