X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine%2FCGI.pm;h=dbdfbbdc2a007e1ed8ce696c9c0c2153c54f6aaa;hb=4e939e53ffc8aee7c2fb179a65b7dfe08dda7f86;hp=409ce5a87a75ca667861a431dd780b7760bebdd0;hpb=66294129a6520edc031aa7a43fc9bdfce669af15;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine/CGI.pm b/lib/Catalyst/Engine/CGI.pm index 409ce5a..dbdfbbd 100644 --- a/lib/Catalyst/Engine/CGI.pm +++ b/lib/Catalyst/Engine/CGI.pm @@ -1,27 +1,11 @@ package Catalyst::Engine::CGI; use strict; -use base 'Catalyst::Engine::CGI::Base'; +use base 'Catalyst::Engine'; +use NEXT; +use URI; -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'); +__PACKAGE__->mk_accessors('env'); =head1 NAME @@ -44,150 +28,202 @@ appropriate engine module. =head1 DESCRIPTION -This is the Catalyst engine specialized for the CGI environment (using the -C and C modules). - -=head1 METHODS - -=over 4 - -=item $c->cgi - -Contains the C object. - -=back +This is the Catalyst engine specialized for the CGI environment. =head1 OVERLOADED METHODS -This class overloads some methods from C. - -=over 4 +This class overloads some methods from C. -=item $c->prepare_body +=head2 $self->finalize_headers($c) =cut -sub prepare_body { - my $c = shift; +sub finalize_headers { + my ( $self, $c ) = @_; - # 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->response->header( Status => $c->response->status ); + + return unless *STDOUT->opened(); - $c->request->body( $c->cgi->param('POSTDATA') ); + print $c->response->headers->as_string("\015\012"); + print "\015\012"; } -=item $c->prepare_parameters +=head2 $self->prepare_connection($c) =cut -sub prepare_parameters { - my $c = shift; +sub prepare_connection { + my ( $self, $c ) = @_; + local (*ENV) = $self->env || \%ENV; - my ( @params ); + $c->request->address( $ENV{REMOTE_ADDR} ); - 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 ); - } + PROXY_CHECK: + { + unless ( $c->config->{using_frontend_proxy} ) { + last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1'; + last PROXY_CHECK if $c->config->{ignore_frontend_proxy}; } + last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR}; + + # If we are running as a backend server, the user will always appear + # as 127.0.0.1. Select the most recent upstream IP (last in the list) + my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/; + $c->request->address($ip); } - for my $param ( $c->cgi->param ) { - for my $value ( $c->cgi->param($param) ) { - push ( @params, $param, $value ); - } + $c->request->hostname( $ENV{REMOTE_HOST} ); + $c->request->protocol( $ENV{SERVER_PROTOCOL} ); + $c->request->user( $ENV{REMOTE_USER} ); + $c->request->method( $ENV{REQUEST_METHOD} ); + + if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) { + $c->request->secure(1); } - $c->request->param(@params); + if ( $ENV{SERVER_PORT} == 443 ) { + $c->request->secure(1); + } } -=item $c->prepare_request +=head2 $self->prepare_headers($c) =cut -sub prepare_request { - my ( $c, $object ) = @_; +sub prepare_headers { + my ( $self, $c ) = @_; + local (*ENV) = $self->env || \%ENV; - my $cgi; + # Read headers from %ENV + foreach my $header ( keys %ENV ) { + next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i; + ( my $field = $header ) =~ s/^HTTPS?_//; + $c->req->headers->header( $field => $ENV{$header} ); + } +} - if ( defined($object) && ref($object) ) { +=head2 $self->prepare_path($c) - if ( $object->isa('Apache') ) { # MP 1.3 - $cgi = CGI->new($object); - } +=cut - elsif ( $object->isa('Apache::RequestRec') ) { # MP 1.99 - $cgi = CGI->new($object); - } +sub prepare_path { + my ( $self, $c ) = @_; + local (*ENV) = $self->env || \%ENV; + + my $scheme = $c->request->secure ? 'https' : 'http'; + my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; + my $port = $ENV{SERVER_PORT} || 80; + my $base_path; + if ( exists $ENV{REDIRECT_URL} ) { + $base_path = $ENV{REDIRECT_URL}; + $base_path =~ s/$ENV{PATH_INFO}$//; + } + else { + $base_path = $ENV{SCRIPT_NAME} || '/'; + } - elsif ( $object->isa('Apache2::RequestRec') ) { # MP 2.00 - $cgi = CGI->new($object); + # If we are running as a backend proxy, get the true hostname + PROXY_CHECK: + { + unless ( $c->config->{using_frontend_proxy} ) { + last PROXY_CHECK if $host !~ /localhost|127.0.0.1/; + last PROXY_CHECK if $c->config->{ignore_frontend_proxy}; } + last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST}; - elsif ( $object->isa('CGI') ) { - $cgi = $object; - } + $host = $ENV{HTTP_X_FORWARDED_HOST}; - else { - my $class = ref($object); - die( qq/Invalid argument $object/ ); - } + # backend could be on any port, so + # assume frontend is on the default port + $port = $c->request->secure ? 443 : 80; } - $c->cgi( $cgi || CGI->new ); + my $path = $base_path . ( $ENV{PATH_INFO} || '' ); + $path =~ s{^/+}{}; + + my $uri = URI->new; + $uri->scheme($scheme); + $uri->host($host); + $uri->port($port); + $uri->path($path); + $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING}; + + # sanitize the URI + $uri = $uri->canonical; + $c->request->uri($uri); + + # set the base URI + # base must end in a slash + $base_path .= '/' unless ( $base_path =~ /\/$/ ); + my $base = $uri->clone; + $base->path_query($base_path); + $c->request->base($base); } -=item $c->prepare_uploads +=head2 $self->prepare_query_parameters($c) =cut -sub prepare_uploads { - my $c = shift; +sub prepare_query_parameters { + my ( $self, $c ) = @_; + local (*ENV) = $self->env || \%ENV; - my @uploads; + if ( $ENV{QUERY_STRING} ) { + $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} ); + } +} - for my $param ( $c->cgi->param ) { +=head2 $self->prepare_request($c, (env => \%env)) - my @values = $c->cgi->param($param); +=cut - next unless ref( $values[0] ); +sub prepare_request { + my ( $self, $c, %args ) = @_; - for my $fh (@values) { + if ( $args{env} ) { + $self->env( $args{env} ); + } +} - next unless my $size = ( stat $fh )[7]; +=head2 $self->prepare_write($c) - 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]; +Enable autoflush on the output handle for CGI-based engines. - my $upload = Catalyst::Request::Upload->new( - filename => $filename, - size => $size, - tempname => $tempname, - type => $type - ); +=cut - push( @uploads, $param, $upload ); - } - } +sub prepare_write { + my ( $self, $c ) = @_; - $c->request->upload(@uploads); + # Set the output handle to autoflush + *STDOUT->autoflush(1); + + $self->NEXT::prepare_write($c); } -=back +=head2 $self->read_chunk($c, $buffer, $length) + +=cut + +sub read_chunk { shift; shift; *STDIN->sysread(@_); } + +=head2 $self->run + +=cut + +sub run { shift; shift->handle_request(@_) } =head1 SEE ALSO -L L L. +L L. + +=head1 AUTHORS + +Sebastian Riedel, -=head1 AUTHOR +Christian Hansen, -Sebastian Riedel, C -Christian Hansen, C +Andy Grundman, =head1 COPYRIGHT