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 &self_ref &self_form);
27 $x =~ s/([^-\$_.!*'(),0-9A-Za-z\x80-\xff])/"%".unpack('H2',$1)/ge;
40 ### Analysing RFC 822 Style Headers ###
42 sub rfc822_prepare($) {
44 # Convert all %'s and backslash escapes to %xx escapes
46 $x =~ s/\\(.)/"%".unpack("H2",$1)/ge;
47 # Remove all comments, beware, they can be nested (unterminated comments are closed at EOL automatically)
48 while ($x =~ s/^(("[^"]*"|[^"(])*(\([^)]*)*)(\([^()]*(\)|$))/$1 /) { }
49 # Remove quotes and escape dangerous characters inside (again closing at the end automatically)
50 $x =~ s{"([^"]*)("|$)}{my $z=$1; $z =~ s/([^0-9a-zA-Z%_-])/"%".unpack("H2",$1)/ge; $z;}ge;
51 # All control characters are properly escaped, tokens are clearly visible.
52 # Finally remove all unnecessary spaces.
55 $x =~ s{\s*([()<>@,;:\\"/\[\]?=])\s*}{$1}g;
59 sub rfc822_deescape($) {
61 $x =~ s/%(..)/pack("H2",$1)/ge;
65 ### Reading of HTTP headers ###
70 return $ENV{"HTTP_$h"} || $ENV{"$h"};
73 ### Parsing of Arguments ###
77 sub parse_arg_string($) {
80 foreach $_ (split /[&:]/,$s) {
81 (/^([^=]+)=(.*)$/) or next;
82 my $arg = $arg_table->{$1} or next;
85 s/%(..)/pack("c",hex $1)/eg;
89 if (my $rx = $arg->{'check'}) {
90 if (!/^$rx$/) { $_ = $arg->{'default'}; }
93 my $r = ref($arg->{'var'});
95 ${$arg->{'var'}} = $_;
96 } elsif ($r eq 'ARRAY') {
97 push @{$arg->{'var'}}, $_;
103 $arg_table = shift @_;
104 foreach my $a (values %$arg_table) {
105 my $r = ref($a->{'var'});
106 defined($a->{'default'}) or $a->{'default'}="";
107 if ($r eq 'SCALAR') {
108 ${$a->{'var'}} = $a->{'default'};
109 } elsif ($r eq 'ARRAY') {
113 defined $ENV{"GATEWAY_INTERFACE"} or die "Not called as a CGI script";
114 my $method = $ENV{"REQUEST_METHOD"};
115 if ($method eq "GET") {
116 parse_arg_string($ENV{"QUERY_STRING"});
117 } elsif ($method eq "POST") {
118 if ($ENV{"CONTENT_TYPE"} =~ /^application\/x-www-form-urlencoded\b/i) {
121 parse_arg_string($_);
124 return "Unknown content type for POST data";
127 return "Unknown request method";
131 ### Generating Self-ref URL's ###
133 sub make_out_args($) {
134 my ($overrides) = @_;
136 foreach my $name (keys %$arg_table) {
137 my $arg = $arg_table->{$name};
138 defined($arg->{'var'}) || next;
139 defined($arg->{'pass'}) && !$arg->{'pass'} && !exists $overrides->{$name} && next;
141 if (!defined($value = $overrides->{$name})) {
142 if (exists $overrides->{$name}) {
143 $value = $arg->{'default'};
145 $value = ${$arg->{'var'}};
148 if ($value ne $arg->{'default'}) {
149 $out->{$name} = $value;
157 my $out = make_out_args(\%h);
158 return "?" . join(':', map { "$_=" . url_escape($out->{$_}) } sort keys %$out);
163 my $out = make_out_args(\%h);
164 return join('', map { "<input type=hidden name=$_ value='" . html_escape($out->{$_}) . "'>\n" } sort keys %$out);
171 if ($x !~ /^[a-zA-Z0-9%]+$/) {
172 $x =~ s/([\\\"])/\\$1/g;
178 sub set_cookie($$@) {
180 my $value = shift @_;
182 $other{'version'} = 1 unless defined $other{'version'};
183 print "Set-Cookie: $key=", cookie_esc($value);
184 foreach my $k (keys %other) {
185 print ";$k=", cookie_esc($other{$k});
190 sub parse_cookies() {
191 my $h = http_get("Cookie") or return ();
193 while (my ($padding,$name,$val,$xx,$rest) = ($h =~ /\s*([,;]\s*)*([^ =]+)=([^ =,;\"]*|\"([^\"\\]|\\.)*\")(\s.*|;.*|$)/)) {
197 $val =~ s/\\(.)/$1/g;
199 push @cook, $name, $val;