package Catalyst::Engine::CGI;
use strict;
-use base 'Catalyst::Engine::CGI::Base';
+use base 'Catalyst::Engine';
+use NEXT;
-use CGI;
-
-__PACKAGE__->mk_accessors('cgi');
+__PACKAGE__->mk_accessors('env');
=head1 NAME
=head1 DESCRIPTION
-This is the Catalyst engine specialized for the CGI environment (using the
-C<CGI> and C<CGI::Cookie> 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:
+This is the Catalyst engine specialized for the CGI environment.
+
+=head1 OVERLOADED METHODS
- use Catalyst qw(-Engine=CGI);
+This class overloads some methods from C<Catalyst::Engine>.
-The performance of this way of using Catalyst is not expected to be
-useful in production applications, but it may be helpful for development.
+=head2 $self->finalize_headers($c)
-=head1 METHODS
+=cut
-=over 4
+sub finalize_headers {
+ my ( $self, $c ) = @_;
-=item $c->cgi
+ $c->response->header( Status => $c->response->status );
-Contains the C<CGI> object.
+ $self->{_header_buf}
+ = $c->response->headers->as_string("\015\012") . "\015\012";
+}
-=back
+=head2 $self->prepare_connection($c)
-=head1 OVERLOADED METHODS
+=cut
-This class overloads some methods from C<Catalyst::Engine::CGI::Base>.
+sub prepare_connection {
+ my ( $self, $c ) = @_;
+ local (*ENV) = $self->env || \%ENV;
-=over 4
+ $c->request->address( $ENV{REMOTE_ADDR} );
-=item $c->prepare_body
+ 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};
-=cut
+ # 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);
+ }
-sub prepare_body {
- my $c = shift;
+ $c->request->hostname( $ENV{REMOTE_HOST} );
+ $c->request->protocol( $ENV{SERVER_PROTOCOL} );
+ $c->request->user( $ENV{REMOTE_USER} );
+ $c->request->method( $ENV{REQUEST_METHOD} );
- # 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.
+ if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
+ $c->request->secure(1);
+ }
- $c->request->body( $c->cgi->param('POSTDATA') );
+ if ( $ENV{SERVER_PORT} == 443 ) {
+ $c->request->secure(1);
+ }
}
-=item $c->prepare_parameters
+=head2 $self->prepare_headers($c)
=cut
-sub prepare_parameters {
- my $c = shift;
+sub prepare_headers {
+ my ( $self, $c ) = @_;
+ local (*ENV) = $self->env || \%ENV;
- my ( @params );
+ # 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 ( $c->request->method eq 'POST' ) {
- for my $param ( $c->cgi->url_param ) {
- for my $value ( $c->cgi->url_param($param) ) {
- push ( @params, $param, $value );
- }
- }
+=head2 $self->prepare_path($c)
+
+=cut
+
+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} || '/';
}
- for my $param ( $c->cgi->param ) {
- for my $value ( $c->cgi->param($param) ) {
- push ( @params, $param, $value );
+ # 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};
+
+ $host = $ENV{HTTP_X_FORWARDED_HOST};
+
+ # backend could be on any port, so
+ # assume frontend is on the default port
+ $port = $c->request->secure ? 443 : 80;
}
- $c->request->param(@params);
+ # set the request URI
+ my $path = $base_path . ( $ENV{PATH_INFO} || '' );
+ $path =~ s{^/+}{};
+
+ # Using URI directly is way too slow, so we construct the URLs manually
+ my $uri_class = "URI::$scheme";
+
+ # HTTP_HOST will include the port even if it's 80/443
+ $host =~ s/:(?:80|443)$//;
+
+ if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
+ $host .= ":$port";
+ }
+
+ # Escape the path
+ $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
+ $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
+
+ my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
+ my $uri = $scheme . '://' . $host . '/' . $path . $query;
+
+ $c->request->uri( bless \$uri, $uri_class );
+
+ # set the base URI
+ # base must end in a slash
+ $base_path .= '/' unless $base_path =~ m{/$};
+
+ my $base_uri = $scheme . '://' . $host . $base_path;
+
+ $c->request->base( bless \$base_uri, $uri_class );
}
-=item $c->prepare_request
+=head2 $self->prepare_query_parameters($c)
=cut
-sub prepare_request {
- my ( $c, $object ) = @_;
-
- my $cgi;
+sub prepare_query_parameters {
+ my ( $self, $c ) = @_;
+ local (*ENV) = $self->env || \%ENV;
- if ( defined($object) && ref($object) ) {
-
- if ( $object->isa('Apache') ) { # MP 1.3
- $cgi = CGI->new($object);
- }
+ if ( $ENV{QUERY_STRING} ) {
+ $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
+ }
+}
- elsif ( $object->isa('Apache::RequestRec') ) { # MP 1.99
- $cgi = CGI->new($object);
- }
+=head2 $self->prepare_request($c, (env => \%env))
- elsif ( $object->isa('Apache2::RequestRec') ) { # MP 2.00
- $cgi = CGI->new($object);
- }
+=cut
- elsif ( $object->isa('CGI') ) {
- $cgi = $object;
- }
+sub prepare_request {
+ my ( $self, $c, %args ) = @_;
- else {
- my $class = ref($object);
- die( qq/Invalid argument $object/ );
- }
+ if ( $args{env} ) {
+ $self->env( $args{env} );
}
-
- $c->cgi( $cgi || CGI->new );
}
-=item $c->prepare_uploads
+=head2 $self->prepare_write($c)
+
+Enable autoflush on the output handle for CGI-based engines.
=cut
-sub prepare_uploads {
- my $c = shift;
+sub prepare_write {
+ my ( $self, $c ) = @_;
- my @uploads;
+ # Set the output handle to autoflush
+ *STDOUT->autoflush(1);
- for my $param ( $c->cgi->param ) {
+ $self->NEXT::prepare_write($c);
+}
- my @values = $c->cgi->param($param);
+=head2 $self->write($c, $buffer)
- next unless ref( $values[0] );
+Writes the buffer to the client.
- for my $fh (@values) {
+=cut
- next unless my $size = ( stat $fh )[7];
+sub write {
+ my ( $self, $c, $buffer ) = @_;
- 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];
+ # Prepend the headers if they have not yet been sent
+ if ( my $headers = delete $self->{_header_buf} ) {
+ $buffer = $headers . $buffer;
+ }
+
+ return $self->NEXT::write( $c, $buffer );
+}
- my $upload = Catalyst::Request::Upload->new(
- filename => $filename,
- size => $size,
- tempname => $tempname,
- type => $type
- );
+=head2 $self->read_chunk($c, $buffer, $length)
- push( @uploads, $param, $upload );
- }
- }
+=cut
- $c->request->upload(@uploads);
-}
+sub read_chunk { shift; shift; *STDIN->sysread(@_); }
+
+=head2 $self->run
+
+=cut
-=back
+sub run { shift; shift->handle_request(@_) }
=head1 SEE ALSO
-L<Catalyst> L<Catalyst::Engine> L<Catalyst::Engine::CGI::Base>.
+L<Catalyst>, L<Catalyst::Engine>
-=head1 AUTHOR
+=head1 AUTHORS
-Sebastian Riedel, C<sri@cpan.org>
-Christian Hansen, C<ch@ngmedia.com>
+Catalyst Contributors, see Catalyst.pm
=head1 COPYRIGHT