| 1 | ##-*- Mode: CPerl -*- | ||
| 2 | |||
| 3 | ## File: DTA::CAB::Server::HTTP::Handler::CGI.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: | ||
| 6 | ## + CAB HTTP Server: request handler: CGI form processing | ||
| 7 | ##====================================================================== | ||
| 8 | |||
| 9 | package DTA::CAB::Server::HTTP::Handler::CGI; | ||
| 10 | use DTA::CAB::Server::HTTP::Handler; | ||
| 11 | use HTTP::Status; | ||
| 12 | use URI::Escape qw(uri_escape uri_escape_utf8); | ||
| 13 | use CGI; | ||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | our @ISA = qw(DTA::CAB::Server::HTTP::Handler); | ||
| 18 | |||
| 19 | ##-------------------------------------------------------------- | ||
| 20 | ## Aliases | ||
| 21 | BEGIN { | ||
| 22 | DTA::CAB::Server::HTTP::Handler->registerAlias( | ||
| 23 | 'DTA::CAB::Server::Server::HTTP::Handler::CGI' => __PACKAGE__, | ||
| 24 | 'cgi' => __PACKAGE__, | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | |||
| 28 | ##-------------------------------------------------------------- | ||
| 29 | ## Methods | ||
| 30 | |||
| 31 | ## $h = $class_or_obj->new(%options) | ||
| 32 | ## + %options: | ||
| 33 | ## #encoding => $defaultEncoding, ##-- default encoding (UTF-8) | ||
| 34 | ## allowGet => $bool, ##-- allow GET requests? (default=1) | ||
| 35 | ## allowPost => $bool, ##-- allow POST requests? (default=1) | ||
| 36 | ## pushMode => $mode, ##-- push mode for addVars (dfefault='push') | ||
| 37 | ## prepare => \&prepare, ##-- CODE-ref for prepare() | ||
| 38 | ## run => \&run, ##-- CODE-ref for run() | ||
| 39 | ## finish => \&finish, ##-- CODE-ref for finish() | ||
| 40 | ## | ||
| 41 | ## + runtime %$h data: | ||
| 42 | ## #cgi => $cgiobj, ##-- CGI object (after cgiParse()) | ||
| 43 | ## #vars => \%vars, ##-- CGI variables (after cgiParse()) | ||
| 44 | ## #cgisrc => $cgisrc, ##-- CGI source (after cgiParse()) | ||
| 45 | sub new { | ||
| 46 | my $that = shift; | ||
| 47 | my $h = bless { | ||
| 48 | #encoding=>'UTF-8', ##-- default CGI parameter encoding | ||
| 49 | allowGet=>1, | ||
| 50 | allowPost=>1, | ||
| 51 | pushMode => 'push', | ||
| 52 | @_ | ||
| 53 | }, ref($that)||$that; | ||
| 54 | return $h; | ||
| 55 | } | ||
| 56 | |||
| 57 | ## $bool = $h->prepare($server) | ||
| 58 | ## + calls $h->{prepare}->($h,$server) if defined | ||
| 59 | sub prepare { | ||
| 60 | return $_[0]{prepare}->(@_) if ($_[0]{prepare}); | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | ## \%vars = $h->decodeVars(\%vars,%opts) | ||
| 65 | ## + decodes cgi-style variables using $h->decodeString($str,%opts) | ||
| 66 | ## + %opts: | ||
| 67 | ## vars => \@vars, ##-- list of vars to decode (default=keys(%vars)) | ||
| 68 | ## someKey => $someVal, ##-- passed to $h->decodeString() | ||
| 69 | sub decodeVars { | ||
| 70 | my ($h,$vars,%opts) = @_; | ||
| 71 | return undef if (!defined($vars)); | ||
| 72 | my $keys = $opts{vars} || [keys %$vars]; | ||
| 73 | my ($vref); | ||
| 74 | foreach (grep {exists $vars->{$_}} @$keys) { | ||
| 75 | $vref = \$vars->{$_}; | ||
| 76 | if (ref($$vref)) { | ||
| 77 | $h->decodeStringRef(\$_,%opts) foreach (@{$$vref}); | ||
| 78 | } else { | ||
| 79 | #$$vref = $h->decodeString($$vref,%opts); ##-- BUG here with YAML data (test-pp.t.yaml) UTF-8 flag not set after call! | ||
| 80 | $h->decodeStringRef($vref,%opts); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return $vars; | ||
| 84 | } | ||
| 85 | |||
| 86 | ## \$string = $h->decodeString(\$string,%opts); ##-- decodes in-place | ||
| 87 | ## $decoded = $h->decodeString( $string,%opts); ##-- decode by copy | ||
| 88 | ## + decodes string as UTF-8, optionally handling HTML-style escapes | ||
| 89 | ## + %opts: | ||
| 90 | ## allowHtmlEscapes => $bool, ##-- whether to handle HTML escapes (default=false) | ||
| 91 | ## #encoding => $enc, ##-- source encoding (default=$h->{encoding}; see also $h->requestEncoding()) | ||
| 92 | sub decodeString { | ||
| 93 | my ($h,$str,%opts) = @_; | ||
| 94 | return $h->decodeStringRef($str,%opts) if (ref($str)); | ||
| 95 | return ${$h->decodeStringRef(\$str,%opts)}; | ||
| 96 | } | ||
| 97 | |||
| 98 | ## \$string = $h->decodeStringRef(\$string,%opts); ##-- decodes in-place | ||
| 99 | ## + decodes string in-place as UTF-8, optionally handling HTML-style escapes | ||
| 100 | ## + %opts: | ||
| 101 | ## allowHtmlEscapes => $bool, ##-- whether to handle HTML escapes (default=false) | ||
| 102 | ## #encoding => $enc, ##-- source encoding (default=$h->{encoding}; see also $h->requestEncoding()) | ||
| 103 | sub decodeStringRef { | ||
| 104 | my ($h,$sref,%opts) = @_; | ||
| 105 | return $sref if (!defined($sref) || !ref($sref)); | ||
| 106 | utf8::decode($$sref) if (!utf8::is_utf8($$sref)); | ||
| 107 | if ($opts{allowHtmlEscapes}) { | ||
| 108 | $$sref =~ s/\&\#(\d+)\;/pack('U',$1)/eg; | ||
| 109 | $$sref =~ s/\&\#x([[:xdigit:]]+)\;/pack('U',hex($1))/eg; | ||
| 110 | } | ||
| 111 | return $$sref; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | ## \%vars = $h->trimVars(\%vars,%opts) | ||
| 116 | ## + trims leading and trailing whitespace from selected values in \%vars | ||
| 117 | ## + %opts: | ||
| 118 | ## vars => \@vars, ##-- list of vars to trim (default=keys(%vars)) | ||
| 119 | sub trimVars { | ||
| 120 | my ($h,$vars,%opts) = @_; | ||
| 121 | return undef if (!defined($vars)); | ||
| 122 | my $keys = $opts{vars} || [keys %$vars]; | ||
| 123 | my ($vref); | ||
| 124 | foreach (grep {exists $vars->{$_}} @$keys) { | ||
| 125 | $vref = \$vars->{$_}; | ||
| 126 | if (ref($$vref)) { | ||
| 127 | foreach (@{$$vref}) { | ||
| 128 | $_ =~ s/^\s+//; | ||
| 129 | $_ =~ s/\s+$//; | ||
| 130 | } | ||
| 131 | } else { | ||
| 132 | $$vref =~ s/^\s+//; | ||
| 133 | $$vref =~ s/\s+$//; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | return $vars; | ||
| 137 | } | ||
| 138 | |||
| 139 | ## \%vars = $h->addVars(\%vars,\%push,$mode='push') | ||
| 140 | ## + CGI-like variable push; destructively adds\%push onto \%vars | ||
| 141 | ## + if $mode is 'push', dups are treated as array push | ||
| 142 | ## + if $mode is 'clobber', dups in %push clobber values in %vars | ||
| 143 | ## + if $mode is 'keep', dups in %push are ignored | ||
| 144 | sub addVars { | ||
| 145 | my ($h,$vars,$push,$mode) = @_; | ||
| 146 | $mode = $h->{pushMode} if (!defined($mode)); | ||
| 147 | $mode = 'push' if (!defined($mode)); | ||
| 148 | if ($mode eq 'clobber') { | ||
| 149 | @$vars{keys %$push} = values %$push; | ||
| 150 | } | ||
| 151 | elsif ($mode eq 'keep') { | ||
| 152 | $vars->{$_} = $push->{$_} foreach (grep {!exists $vars->{$_}} keys %$push); | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | foreach (grep {defined($push->{$_})} keys %$push) { | ||
| 156 | if (!exists($vars->{$_})) { | ||
| 157 | $vars->{$_} = $push->{$_}; | ||
| 158 | } else { | ||
| 159 | $vars->{$_} = [ $vars->{$_} ] if (!ref($vars->{$_})); | ||
| 160 | push(@{$vars->{$_}}, ref($push->{$_}) ? @{$push->{$_}} : $push->{$_}); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | return $vars; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | ## \%params = $h->uriParams($hreq,%opts) | ||
| 169 | ## + gets GET-style parameters from $hreq->uri | ||
| 170 | ## + %opts: | ||
| 171 | ## #(none) | ||
| 172 | BEGIN { *uriParams = \&uriParams_uri; } | ||
| 173 | sub uriParams_uri { | ||
| 174 | my ($h,$hreq) = @_; | ||
| 175 | return {$hreq->uri->query_form}; | ||
| 176 | } | ||
| 177 | sub uriParams_CGI { | ||
| 178 | my ($h,$hreq) = @_; | ||
| 179 | if ($hreq->uri =~ m/\?(.*)$/) { | ||
| 180 | ##-- see also: $hreq->uri->query_form(), also URI::QueryParam | ||
| 181 | return scalar(CGI->new($1)->Vars); | ||
| 182 | } | ||
| 183 | return {}; | ||
| 184 | } | ||
| 185 | |||
| 186 | ## \%params = $h->contentParams($hreq,%opts) | ||
| 187 | ## + gets POST-style content parameters from $hreq | ||
| 188 | ## + if content-type is neither 'application/x-www-form-urlencoded' nor 'multipart/form-data', | ||
| 189 | ## but content is present, returns $hreq | ||
| 190 | ## + %opts: | ||
| 191 | ## defaultName => $name, ##-- default parameter name (default='POSTDATA') | ||
| 192 | ## #defaultCharset => $charset, ##-- default charset (always UTF-8) | ||
| 193 | sub contentParams { | ||
| 194 | my ($h,$hreq,%opts) = @_; | ||
| 195 | my $dkey = defined($opts{defaultName}) ? $opts{defaultName} : 'POSTDATA'; | ||
| 196 | #my $denc = defined($opts{defaultCharset}) ? $opts{defaultCharset} : $h->requestEncoding($hreq); | ||
| 197 | if ($hreq->content_type eq 'application/x-www-form-urlencoded') { | ||
| 198 | ##-- x-www-form-urlencoded | ||
| 199 | #return scalar(CGI->new($hreq->content)->Vars); ##-- : parse with CGI module | ||
| 200 | return {URI->new('?'.$hreq->content)->query_form}; | ||
| 201 | } | ||
| 202 | elsif ($hreq->content_type eq 'multipart/form-data') { | ||
| 203 | ##-- multipart/form-data: parse by hand | ||
| 204 | my $vars = {}; | ||
| 205 | my ($part,$name,$penc); | ||
| 206 | foreach $part ($hreq->parts) { | ||
| 207 | my $dis = $part->header('Content-Disposition'); | ||
| 208 | $penc = $h->messageEncoding($part); | ||
| 209 | $penc = 'UTF-8' if (!defined($penc)); | ||
| 210 | if ($dis =~ /^form-data\b/) { | ||
| 211 | ##-- multipart/form-data: part: form-data | ||
| 212 | if ($dis =~ /\bname=[\"\']?([\w\-\.\,\+]*)[\'\"]?/) { | ||
| 213 | ##-- multipart/form-data: part: form-data; name="PARAMNAME" | ||
| 214 | $h->addVars($vars, { $1 => $part->content }); | ||
| 215 | } else { | ||
| 216 | ##-- multipart/form-data: part: form-data; other (POSTDATA) | ||
| 217 | $h->addVars($vars, { $opts{defaultName} => $part->content }); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | ##-- multipart/form-data: part: anything other than 'form-data' | ||
| 222 | $h->addVars($vars, { $opts{defaultName} => $part->content }); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | return $vars; | ||
| 226 | } | ||
| 227 | elsif ($hreq->content_length > 0) { | ||
| 228 | ##-- unknown content: use default data key | ||
| 229 | return { | ||
| 230 | $opts{defaultName} => $hreq->content | ||
| 231 | }; | ||
| 232 | } | ||
| 233 | return {}; ##-- no parameters at all | ||
| 234 | } | ||
| 235 | |||
| 236 | ## \%params = $h->params($hreq,%opts) | ||
| 237 | ## + wrapper for $h->pushVars($h->uriParams(),$h->contentParams()) | ||
| 238 | ## + %opts are passed to uriParams, contentParams | ||
| 239 | sub params { | ||
| 240 | my ($h,$hreq,%opts) = @_; | ||
| 241 | my $vars = $h->uriParams($hreq,%opts); | ||
| 242 | $h->addVars($vars, $h->contentParams($hreq,%opts)); | ||
| 243 | return $vars; | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | ## \%vars = $h->cgiParams($srv,$clientConn,$httpRequest, %opts) | ||
| 248 | ## + parses cgi parameters from client request | ||
| 249 | ## + only handles GET or POST requests | ||
| 250 | ## + wrapper for $h->uriParams(), $h->contentParams() | ||
| 251 | ## + %opts are passed to uriParams, contentParams | ||
| 252 | sub cgiParams { | ||
| 253 | my ($h,$csock,$hreq,%opts) = @_; | ||
| 254 | |||
| 255 | if ($hreq->method eq 'GET') { | ||
| 256 | ##-- HTTP request: GET | ||
| 257 | return $h->cerror($csock, RC_METHOD_NOT_ALLOWED, "CGI::cgiParams(): GET method not allowed") if (!$h->{allowGet}); | ||
| 258 | return $h->uriParams($hreq,%opts); | ||
| 259 | } | ||
| 260 | elsif ($hreq->method eq 'POST') { | ||
| 261 | ##-- HTTP request: POST | ||
| 262 | return $h->cerror($csock, RC_METHOD_NOT_ALLOWED, "CGI::cgiParams(): POST method not allowed") if (!$h->{allowPost}); | ||
| 263 | return $h->params($hreq,%opts); | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | ##-- HTTP request: unknown | ||
| 267 | return $h->cerror($csock, RC_METHOD_NOT_ALLOWED, ("CGI::cgiParams(): method not allowed: ".$hreq->method)); | ||
| 268 | } | ||
| 269 | |||
| 270 | return {}; | ||
| 271 | } | ||
| 272 | |||
| 273 | ## $enc = $h->messageEncoding($httpMessage,$defaultEncoding) | ||
| 274 | ## + attempts to guess messagencoding from (in order of descending priority): | ||
| 275 | ## - HTTP::Message header Content-Type charset variable | ||
| 276 | ## - HTTP::Message header Content-Encoding | ||
| 277 | ## - $defaultEncoding (default=undef) | ||
| 278 | sub messageEncoding { | ||
| 279 | my ($h,$msg,$default) = @_; | ||
| 280 | my $ctype = $msg->header('Content-Type'); ##-- note: $msg->content_type() truncates after ';' ! | ||
| 281 | ##-- see also HTTP::Message::decoded_content() for a better way to parse header parameters! | ||
| 282 | ## | ||
| 283 | return $1 if (defined($ctype) && $ctype =~ /\bcharset=([\w\-]+)/); | ||
| 284 | #$ctype = $msg->header('Content-Encoding'); | ||
| 285 | #return $1 if (defined($ctype) && $ctype =~ /\bcharset=([\w\-]+)/); | ||
| 286 | #return $ctype if (defined($ctype)); | ||
| 287 | return $default; | ||
| 288 | } | ||
| 289 | |||
| 290 | ## $enc = $h->getEncoding(@sources) | ||
| 291 | ## + attempts to guess request encoding from the first defined | ||
| 292 | ## encoding in @sources, each element $_ of which may be: | ||
| 293 | ## - a HASH-ref : encoding is $_->{encoding} | ||
| 294 | ## - a HTTP::Message object : encoding is $h->messageEncoding($_) | ||
| 295 | ## - a literal scalar : encoding is $_ | ||
| 296 | sub getEncoding { | ||
| 297 | my $h = shift; | ||
| 298 | my ($enc); | ||
| 299 | foreach (@_) { | ||
| 300 | if (UNIVERSAL::isa($_,'HTTP::Message')) { | ||
| 301 | $enc = $h->messageEncoding($_,undef); | ||
| 302 | } | ||
| 303 | elsif (UNIVERSAL::isa($_,'HASH')) { | ||
| 304 | $enc = $_->{encoding}; | ||
| 305 | } | ||
| 306 | elsif (!ref($_)) { | ||
| 307 | $enc = $_; | ||
| 308 | } | ||
| 309 | return $enc if ($enc); | ||
| 310 | } | ||
| 311 | return undef; | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | ## $enc = $h->requestEncoding($httpRequest,\%vars) | ||
| 316 | ## + attempts to guess request encoding from (in order of descending priority): | ||
| 317 | ## - CGI param 'encoding', from $vars->{encoding} | ||
| 318 | ## - HTTP::Message encoding via $h->messageEncoding($httpRequest) | ||
| 319 | ## - $h->{encoding} | ||
| 320 | sub requestEncoding { | ||
| 321 | my ($h,$hreq,$vars) = @_; | ||
| 322 | return $h->getEncoding($vars->{encoding},$hreq,$h->{encoding}); | ||
| 323 | } | ||
| 324 | |||
| 325 | ## $rsp = $h->run($server, $localPath, $clientConn, $httpRequest) | ||
| 326 | ## + return $h->{run}->(@_) if defined | ||
| 327 | sub run { | ||
| 328 | my $h = shift; | ||
| 329 | return $h->{run}->($h,@_) if ($h->{run}); | ||
| 330 | return $h->SUPER::run(@_); | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | ## undef = $h->finish($server, $clientSocket) | ||
| 335 | ## + clean up handler state after run() | ||
| 336 | ## + override deletes @$h{qw(cgi vars cgisrc)} | ||
| 337 | sub finish { | ||
| 338 | my $h = shift; | ||
| 339 | return $h->{finish}->($h,@_) if ($h->{finish}); | ||
| 340 | delete(@$h{qw(cgi vars cgisrc)}); | ||
| 341 | return; | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | 1; ##-- be happy | ||
| 346 | |||
| 347 | __END__ |