1 # Poor Man's CGI Module for Perl
3 # (c) 2002--2007 Martin Mares <mj@ucw.cz>
4 # Slightly modified by Tomas Valla <tom@ucw.cz>
6 # This software may be freely distributed and used according to the terms
7 # of the GNU Lesser General Public License.
15 # The somewhat hairy Perl export mechanism
17 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
20 @EXPORT = qw(&html_escape &url_escape &url_param_escape &self_ref &self_form);
29 $x =~ s/([^-\$_.!*'(),0-9A-Za-z\x80-\xff])/"%".unpack('H2',$1)/ge;
33 sub url_param_escape($) {
49 ### Analysing RFC 822 Style Headers ###
51 sub rfc822_prepare($) {
53 # Convert all %'s and backslash escapes to %xx escapes
55 $x =~ s/\\(.)/"%".unpack("H2",$1)/ge;
56 # Remove all comments, beware, they can be nested (unterminated comments are closed at EOL automatically)
57 while ($x =~ s/^(("[^"]*"|[^"(])*(\([^)]*)*)(\([^()]*(\)|$))/$1 /) { }
58 # Remove quotes and escape dangerous characters inside (again closing at the end automatically)
59 $x =~ s{"([^"]*)("|$)}{my $z=$1; $z =~ s/([^0-9a-zA-Z%_-])/"%".unpack("H2",$1)/ge; $z;}ge;
60 # All control characters are properly escaped, tokens are clearly visible.
61 # Finally remove all unnecessary spaces.
64 $x =~ s{\s*([()<>@,;:\\"/\[\]?=])\s*}{$1}g;
68 sub rfc822_deescape($) {
70 $x =~ s/%(..)/pack("H2",$1)/ge;
74 ### Reading of HTTP headers ###
79 return $ENV{"HTTP_$h"} || $ENV{"$h"};
82 ### Parsing of Arguments ###
86 sub parse_arg_string($) {
89 foreach $_ (split /[&:]/,$s) {
90 (/^([^=]+)=(.*)$/) or next;
91 my $arg = $arg_table->{$1} or next;
94 s/%(..)/pack("c",hex $1)/eg;
98 if (my $rx = $arg->{'check'}) {
99 if (!/^$rx$/) { $_ = $arg->{'default'}; }
102 my $r = ref($arg->{'var'});
103 if ($r eq 'SCALAR') {
104 ${$arg->{'var'}} = $_;
105 } elsif ($r eq 'ARRAY') {
106 push @{$arg->{'var'}}, $_;
112 $arg_table = shift @_;
113 foreach my $a (values %$arg_table) {
114 my $r = ref($a->{'var'});
115 defined($a->{'default'}) or $a->{'default'}="";
116 if ($r eq 'SCALAR') {
117 ${$a->{'var'}} = $a->{'default'};
118 } elsif ($r eq 'ARRAY') {
122 defined $ENV{"GATEWAY_INTERFACE"} or die "Not called as a CGI script";
123 my $method = $ENV{"REQUEST_METHOD"};
124 if ($method eq "GET") {
125 parse_arg_string($ENV{"QUERY_STRING"});
126 } elsif ($method eq "POST") {
127 if ($ENV{"CONTENT_TYPE"} =~ /^application\/x-www-form-urlencoded\b/i) {
130 parse_arg_string($_);
133 return "Unknown content type for POST data";
136 return "Unknown request method";
140 ### Generating Self-ref URL's ###
142 sub make_out_args($) {
143 my ($overrides) = @_;
145 foreach my $name (keys %$arg_table) {
146 my $arg = $arg_table->{$name};
147 defined($arg->{'var'}) || next;
148 defined($arg->{'pass'}) && !$arg->{'pass'} && !exists $overrides->{$name} && next;
150 if (!defined($value = $overrides->{$name})) {
151 if (exists $overrides->{$name}) {
152 $value = $arg->{'default'};
154 $value = ${$arg->{'var'}};
157 if ($value ne $arg->{'default'}) {
158 $out->{$name} = $value;
166 my $out = make_out_args(\%h);
167 return "?" . join(':', map { "$_=" . url_param_escape($out->{$_}) } sort keys %$out);
172 my $out = make_out_args(\%h);
173 return join('', map { "<input type=hidden name=$_ value='" . html_escape($out->{$_}) . "'>\n" } sort keys %$out);
180 if ($x !~ /^[a-zA-Z0-9%]+$/) {
181 $x =~ s/([\\\"])/\\$1/g;
187 sub set_cookie($$@) {
189 my $value = shift @_;
191 $other{'version'} = 1 unless defined $other{'version'};
192 print "Set-Cookie: $key=", cookie_esc($value);
193 foreach my $k (keys %other) {
194 print ";$k=", cookie_esc($other{$k});
199 sub parse_cookies() {
200 my $h = http_get("Cookie") or return ();
202 while (my ($padding,$name,$val,$xx,$rest) = ($h =~ /\s*([,;]\s*)*([^ =]+)=([^ =,;\"]*|\"([^\"\\]|\\.)*\")(\s.*|;.*|$)/)) {
206 $val =~ s/\\(.)/$1/g;
208 push @cook, $name, $val;