X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine%2FCGI.pm;h=11d8ebf4c8b09a2bd7c0870940bfc187bf402895;hb=a2f2cde95194a17fe2401ae58c92b5494bac599f;hp=e682f6e818a1db2e961c17edd98c490d163ba434;hpb=9a33da6aa4eb69de77c36f572e875a2529a39eef;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine/CGI.pm b/lib/Catalyst/Engine/CGI.pm index e682f6e..11d8ebf 100644 --- a/lib/Catalyst/Engine/CGI.pm +++ b/lib/Catalyst/Engine/CGI.pm @@ -1,14 +1,26 @@ 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 Catalyst::Exception; +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'); @@ -18,6 +30,8 @@ Catalyst::Engine::CGI - The CGI Engine =head1 SYNOPSIS +A script using the Catalyst::Engine::CGI module might look like: + #!/usr/bin/perl -w use strict; @@ -26,152 +40,158 @@ Catalyst::Engine::CGI - The CGI Engine MyApp->run; -See L. +The application module (C) would use C, which loads the +appropriate engine module. =head1 DESCRIPTION -This is the CGI engine for Catalyst. +This is the Catalyst engine specialized for the CGI environment (using the +C and C modules). -The script shown above must be designated as a "Non-parsed Headers" -script to function properly. -To do this in Apache name the script starting with C. +=head1 METHODS -The performance of this way of using Catalyst is not expected to be -useful in production applications, but it may be helpful for development. +=over 4 -=head2 METHODS +=item $c->cgi -=head3 run +Contains the C object. -To be called from a CGI script to start the Catalyst application. +=back -=head3 cgi +=head1 OVERLOADED METHODS -This config parameter contains the C object. +This class overloads some methods from C. -=head2 OVERLOADED METHODS +=over 4 -This class overloads some methods from C. - -=head3 finalize_headers +=item $c->prepare_body =cut -sub finalize_headers { +sub prepare_body { my $c = shift; - my %headers = ( -nph => 1 ); - $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); + + # 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') ); } -=head3 finalize_output +=item $c->prepare_parameters =cut -sub finalize_output { +sub prepare_parameters { my $c = shift; - print $c->response->output; + + my ( @params ); + + 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 ); + } + } + } + + for my $param ( $c->cgi->param ) { + for my $value ( $c->cgi->param($param) ) { + push ( @params, $param, $value ); + } + } + + $c->request->param(@params); } -=head3 prepare_cookies +=item $c->prepare_request =cut -sub prepare_cookies { shift->req->cookies( { CGI::Cookie->fetch } ) } +sub prepare_request { + my ( $c, $object ) = @_; -=head3 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) ); - } -} + if ( $object->isa('Apache') ) { # MP 1.3 + $cgi = CGI->new($object); + } -=head3 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); + + Catalyst::Exception->throw( + message => qq/Unknown object '$object'/ + ); + } } - $c->req->parameters( {%vars} ); + + $c->cgi( $cgi || CGI->new ); } -=head3 prepare_path +=item $c->prepare_uploads =cut -sub prepare_path { +sub prepare_uploads { my $c = shift; - $c->req->path( $c->cgi->url( -absolute => 1, -path_info => 1 ) ); - my $loc = $c->cgi->url( -absolute => 1 ); - no warnings 'uninitialized'; - $c->req->{path} =~ s/^($loc)?\///; - $c->req->{path} .= '/' if $c->req->path eq $loc; - my $base = $c->cgi->url; - if ( $ENV{CATALYST_TEST} ) { - my $script = $c->cgi->script_name; - $base =~ s/$script$//i; - } - $base = URI->new($base); - $base->path('/') if ( $ENV{CATALYST_TEST} || !$base->path ); - $c->req->base( $base->as_string ); -} -=head3 prepare_request + my @uploads; -=cut + for my $param ( $c->cgi->param ) { -sub prepare_request { shift->cgi( CGI::Simple->new ) } + my @values = $c->cgi->param($param); -=head3 prepare_uploads + next unless ref( $values[0] ); -=cut + for my $fh (@values) { -sub prepare_uploads { - my $c = shift; - for my $name ( $c->cgi->upload ) { - $c->req->uploads->{$name} = { - fh => $c->cgi->upload($name), - size => $c->cgi->upload_info( $name, 'size' ), - type => $c->cgi->upload_info( $name, 'mime' ) - }; + next unless my $size = ( stat $fh )[7]; + + 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]; + + my $upload = Catalyst::Request::Upload->new( + filename => $filename, + size => $size, + tempname => $tempname, + type => $type + ); + + push( @uploads, $param, $upload ); + } } + + $c->request->upload(@uploads); } -sub run { shift->handler } +=back =head1 SEE ALSO -L. +L L L. =head1 AUTHOR Sebastian Riedel, C +Christian Hansen, C =head1 COPYRIGHT