1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
8 __PACKAGE__->mk_accessors( 'env' );
12 Catalyst::Engine::CGI - The CGI Engine
16 A script using the Catalyst::Engine::CGI module might look like:
21 use lib '/path/to/MyApp/lib';
26 The application module (C<MyApp>) would use C<Catalyst>, which loads the
27 appropriate engine module.
31 This is the Catalyst engine specialized for the CGI environment.
33 =head1 OVERLOADED METHODS
35 This class overloads some methods from C<Catalyst::Engine>.
39 =item $self->finalize_headers($c)
43 sub finalize_headers {
44 my ( $self, $c ) = @_;
46 $c->response->header( Status => $c->response->status );
48 print $c->response->headers->as_string("\015\012");
52 =item $self->prepare_connection($c)
56 sub prepare_connection {
57 my ( $self, $c ) = @_;
58 local(*ENV) = $self->env || \%ENV;
60 $c->request->address( $ENV{REMOTE_ADDR} );
64 unless ( $c->config->{using_frontend_proxy} ) {
65 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
66 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
68 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
70 # If we are running as a backend server, the user will always appear
71 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
72 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
73 $c->request->address($ip);
76 $c->request->hostname( $ENV{REMOTE_HOST} );
77 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
78 $c->request->user( $ENV{REMOTE_USER} );
79 $c->request->method( $ENV{REQUEST_METHOD} );
81 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
82 $c->request->secure(1);
85 if ( $ENV{SERVER_PORT} == 443 ) {
86 $c->request->secure(1);
90 =item $self->prepare_headers($c)
95 my ( $self, $c ) = @_;
96 local(*ENV) = $self->env || \%ENV;
98 # Read headers from %ENV
99 while ( my ( $header, $value ) = each %ENV ) {
100 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
101 ( my $field = $header ) =~ s/^HTTPS?_//;
102 $c->req->headers->header( $field => $value );
106 =item $self->prepare_path($c)
111 my ( $self, $c ) = @_;
112 local(*ENV) = $self->env || \%ENV;
114 my $scheme = $c->request->secure ? 'https' : 'http';
115 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
116 my $port = $ENV{SERVER_PORT} || 80;
117 my $base_path = $ENV{SCRIPT_NAME} || '/';
119 # If we are running as a backend proxy, get the true hostname
122 unless ( $c->config->{using_frontend_proxy} ) {
123 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
124 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
126 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
128 $host = $ENV{HTTP_X_FORWARDED_HOST};
130 # backend could be on any port, so
131 # assume frontend is on the default port
132 $port = $c->request->secure ? 443 : 80;
135 my $path = $base_path . $ENV{PATH_INFO};
139 $uri->scheme($scheme);
143 $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
146 $uri = $uri->canonical;
147 $c->request->uri($uri);
150 # base must end in a slash
151 $base_path .= '/' unless ( $base_path =~ /\/$/ );
152 my $base = $uri->clone;
153 $base->path_query($base_path);
154 $c->request->base($base);
157 =item $self->prepare_query_parameters($c)
161 sub prepare_query_parameters {
162 my ( $self, $c ) = @_;
163 local(*ENV) = $self->env || \%ENV;
165 if ( $ENV{QUERY_STRING} ) {
166 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
170 =item $self->prepare_request($c, (env => \%env))
174 sub prepare_request {
175 my ( $self, $c, %args ) = @_;
178 $self->env( $args{env} );
182 =item $self->prepare_write($c)
184 Enable autoflush on the output handle for CGI-based engines.
189 my ( $self, $c ) = @_;
191 # Set the output handle to autoflush
192 *STDOUT->autoflush(1);
194 $self->NEXT::prepare_write($c);
197 =item $self->read_chunk($c, $buffer, $length)
201 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
207 sub run { shift; shift->handle_request(@_) }
213 L<Catalyst> L<Catalyst::Engine>.
217 Sebastian Riedel, <sri@cpan.org>
219 Christian Hansen, <ch@ngmedia.com>
221 Andy Grundman, <andy@hybridized.org>
225 This program is free software, you can redistribute it and/or modify it under
226 the same terms as Perl itself.