X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine%2FCGI.pm;h=409ce5a87a75ca667861a431dd780b7760bebdd0;hb=fc88e4951e53e4492c168fc5ffd2a0516e50346b;hp=05e86ea4276d53cf52850ac52386e02801cfc86f;hpb=45374ac6977e9464410a8c7518fb26ab812258cb;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine/CGI.pm b/lib/Catalyst/Engine/CGI.pm index 05e86ea..409ce5a 100644 --- a/lib/Catalyst/Engine/CGI.pm +++ b/lib/Catalyst/Engine/CGI.pm @@ -1,14 +1,25 @@ package Catalyst::Engine::CGI; use strict; -use base 'Catalyst::Engine'; -use URI; - -require CGI::Simple; -require CGI::Cookie; - -$CGI::Simple::POST_MAX = 1048576; -$CGI::Simple::DISABLE_UPLOADS = 0; +use base 'Catalyst::Engine::CGI::Base'; + +use CGI; + +our @compile = qw[ + delete + http + new_MultipartBuffer + param + parse_keywordlist + read_from_client + read_multipart + tmpFileName + uploadInfo + url_param + user_agent +]; + +CGI->compile(@compile); __PACKAGE__->mk_accessors('cgi'); @@ -34,20 +45,7 @@ appropriate engine module. =head1 DESCRIPTION This is the Catalyst engine specialized for the CGI environment (using the -C and C modules). Normally Catalyst will select the -appropriate engine according to the environment that it detects, however you -can force Catalyst to use the CGI engine by specifying the following in your -application module: - - use Catalyst qw(-Engine=CGI); - -Catalyst::Engine::CGI generates a full set of HTTP headers, which means that -applications using the engine must be be configured as "Non-parsed Headers" -scripts (at least when running under Apache). To configure this under Apache -name the starting with C. - -The performance of this way of using Catalyst is not expected to be -useful in production applications, but it may be helpful for development. +C and C modules). =head1 METHODS @@ -55,167 +53,141 @@ useful in production applications, but it may be helpful for development. =item $c->cgi -This config parameter contains the C object. +Contains the C object. =back =head1 OVERLOADED METHODS -This class overloads some methods from C. +This class overloads some methods from C. =over 4 -=item $c->finalize_headers +=item $c->prepare_body =cut -sub finalize_headers { +sub prepare_body { my $c = shift; - my %headers; - $headers{-status} = $c->response->status if $c->response->status; - for my $name ( $c->response->headers->header_field_names ) { - $headers{"-$name"} = $c->response->headers->header($name); - } - my @cookies; - while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) { - push @cookies, $c->cgi->cookie( - -name => $name, - -value => $cookie->{value}, - -expires => $cookie->{expires}, - -domain => $cookie->{domain}, - -path => $cookie->{path}, - -secure => $cookie->{secure} || 0 - ); - } - $headers{-cookie} = \@cookies if @cookies; - print $c->cgi->header(%headers); -} -=item $c->finalize_output + # XXX this is undocumented in CGI.pm. If Content-Type is not + # application/x-www-form-urlencoded or multipart/form-data + # CGI.pm will read STDIN into a param, POSTDATA. + + $c->request->body( $c->cgi->param('POSTDATA') ); +} -Prints the response output to STDOUT. +=item $c->prepare_parameters =cut -sub finalize_output { +sub prepare_parameters { my $c = shift; - print $c->response->output; -} -=item $c->prepare_connection + my ( @params ); -=cut + if ( $c->request->method eq 'POST' ) { + for my $param ( $c->cgi->url_param ) { + for my $value ( $c->cgi->url_param($param) ) { + push ( @params, $param, $value ); + } + } + } -sub prepare_connection { - my $c = shift; - $c->req->hostname( $c->cgi->remote_host ); - $c->req->address( $c->cgi->remote_addr ); -} + for my $param ( $c->cgi->param ) { + for my $value ( $c->cgi->param($param) ) { + push ( @params, $param, $value ); + } + } -=item $c->prepare_cookies + $c->request->param(@params); +} -Sets up cookies. +=item $c->prepare_request =cut -sub prepare_cookies { shift->req->cookies( { CGI::Cookie->fetch } ) } +sub prepare_request { + my ( $c, $object ) = @_; -=item $c->prepare_headers + my $cgi; -=cut + if ( defined($object) && ref($object) ) { -sub prepare_headers { - my $c = shift; - $c->req->method( $c->cgi->request_method ); - for my $header ( $c->cgi->http ) { - ( my $field = $header ) =~ s/^HTTPS?_//; - $c->req->headers->header( $field => $c->cgi->http($header) ); - } - $c->req->headers->header( 'Content-Type' => $c->cgi->content_type ); - $c->req->headers->header( 'Content-Length' => $c->cgi->content_length ); -} + if ( $object->isa('Apache') ) { # MP 1.3 + $cgi = CGI->new($object); + } -=item $c->prepare_parameters + elsif ( $object->isa('Apache::RequestRec') ) { # MP 1.99 + $cgi = CGI->new($object); + } -=cut + elsif ( $object->isa('Apache2::RequestRec') ) { # MP 2.00 + $cgi = CGI->new($object); + } -sub prepare_parameters { - my $c = shift; - my %vars = $c->cgi->Vars; - while ( my ( $key, $value ) = each %vars ) { - my @values = split "\0", $value; - $vars{$key} = @values <= 1 ? $values[0] : \@values; + elsif ( $object->isa('CGI') ) { + $cgi = $object; + } + + else { + my $class = ref($object); + die( qq/Invalid argument $object/ ); + } } - $c->req->parameters( {%vars} ); + + $c->cgi( $cgi || CGI->new ); } -=item $c->prepare_path +=item $c->prepare_uploads =cut -sub prepare_path { +sub prepare_uploads { my $c = shift; - my $base; - { - my $scheme = $ENV{HTTPS} ? 'https' : 'http'; - my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; - my $port = $ENV{SERVER_PORT} || 80; - my $path = $ENV{SCRIPT_NAME} || '/'; + my @uploads; - $base = URI->new; - $base->scheme($scheme); - $base->host($host); - $base->port($port); - $base->path($path); + for my $param ( $c->cgi->param ) { - $base = $base->canonical->as_string; - } + my @values = $c->cgi->param($param); - my $path = $ENV{PATH_INFO} || '/'; - $path =~ s/^\///; + next unless ref( $values[0] ); - $c->req->base($base); - $c->req->path($path); -} + for my $fh (@values) { -=item $c->prepare_request + next unless my $size = ( stat $fh )[7]; -=cut + my $info = $c->cgi->uploadInfo($fh); + my $tempname = $c->cgi->tmpFileName($fh); + my $type = $info->{'Content-Type'}; + my $disposition = $info->{'Content-Disposition'}; + my $filename = ( $disposition =~ / filename="([^;]*)"/ )[0]; -sub prepare_request { shift->cgi( CGI::Simple->new ) } + my $upload = Catalyst::Request::Upload->new( + filename => $filename, + size => $size, + tempname => $tempname, + type => $type + ); -=item $c->prepare_uploads - -=cut - -sub prepare_uploads { - my $c = shift; - for my $name ( $c->cgi->upload ) { - next unless defined $name; - $c->req->uploads->{$name} = { - fh => $c->cgi->upload($name), - size => $c->cgi->upload_info( $name, 'size' ), - type => $c->cgi->upload_info( $name, 'mime' ) - }; + push( @uploads, $param, $upload ); + } } -} - -=item $c->run -=cut - -sub run { shift->handler } + $c->request->upload(@uploads); +} =back =head1 SEE ALSO -L. +L L L. =head1 AUTHOR Sebastian Riedel, C +Christian Hansen, C =head1 COPYRIGHT