1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
7 __PACKAGE__->mk_accessors('env');
11 Catalyst::Engine::CGI - The CGI Engine
15 A script using the Catalyst::Engine::CGI module might look like:
20 use lib '/path/to/MyApp/lib';
25 The application module (C<MyApp>) would use C<Catalyst>, which loads the
26 appropriate engine module.
30 This is the Catalyst engine specialized for the CGI environment.
32 =head1 OVERLOADED METHODS
34 This class overloads some methods from C<Catalyst::Engine>.
36 =head2 $self->finalize_headers($c)
40 sub finalize_headers {
41 my ( $self, $c ) = @_;
43 $c->response->header( Status => $c->response->status );
45 print $c->response->headers->as_string("\015\012") . "\015\012";
48 =head2 $self->prepare_connection($c)
52 sub prepare_connection {
53 my ( $self, $c ) = @_;
54 local (*ENV) = $self->env || \%ENV;
56 $c->request->address( $ENV{REMOTE_ADDR} );
60 unless ( $c->config->{using_frontend_proxy} ) {
61 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
62 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
64 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
66 # If we are running as a backend server, the user will always appear
67 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
68 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
69 $c->request->address($ip);
72 $c->request->hostname( $ENV{REMOTE_HOST} );
73 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
74 $c->request->user( $ENV{REMOTE_USER} );
75 $c->request->method( $ENV{REQUEST_METHOD} );
77 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
78 $c->request->secure(1);
81 if ( $ENV{SERVER_PORT} == 443 ) {
82 $c->request->secure(1);
86 =head2 $self->prepare_headers($c)
91 my ( $self, $c ) = @_;
92 local (*ENV) = $self->env || \%ENV;
94 # Read headers from %ENV
95 foreach my $header ( keys %ENV ) {
96 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
97 ( my $field = $header ) =~ s/^HTTPS?_//;
98 $c->req->headers->header( $field => $ENV{$header} );
102 =head2 $self->prepare_path($c)
107 my ( $self, $c ) = @_;
108 local (*ENV) = $self->env || \%ENV;
110 my $scheme = $c->request->secure ? 'https' : 'http';
111 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
112 my $port = $ENV{SERVER_PORT} || 80;
114 if ( exists $ENV{REDIRECT_URL} ) {
115 $base_path = $ENV{REDIRECT_URL};
116 $base_path =~ s/$ENV{PATH_INFO}$//;
119 $base_path = $ENV{SCRIPT_NAME} || '/';
122 # If we are running as a backend proxy, get the true hostname
125 unless ( $c->config->{using_frontend_proxy} ) {
126 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
127 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
129 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
131 $host = $ENV{HTTP_X_FORWARDED_HOST};
133 # backend could be on any port, so
134 # assume frontend is on the default port
135 $port = $c->request->secure ? 443 : 80;
139 # base must end in a slash
140 $base_path .= '/' unless ( $base_path =~ /\/$/ );
142 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
145 # Using URI directly is way too slow, so we construct the URLs manually
146 my $uri_class = "URI::$scheme";
148 # HTTP_HOST will include the port even if it's 80
151 if ( $port != 80 && $host !~ /:/ ) {
156 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
157 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
159 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
160 my $uri = $scheme . '://' . $host . '/' . $path . $query;
162 $c->request->uri( bless \$uri, $uri_class );
165 $uri = $uri->canonical;
166 $c->request->uri($uri);
167 my $base = $uri->clone;
168 $base->path_query($base_path);
169 $c->request->base($base);
172 =head2 $self->prepare_query_parameters($c)
176 sub prepare_query_parameters {
177 my ( $self, $c ) = @_;
178 local (*ENV) = $self->env || \%ENV;
180 if ( $ENV{QUERY_STRING} ) {
181 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
185 =head2 $self->prepare_request($c, (env => \%env))
189 sub prepare_request {
190 my ( $self, $c, %args ) = @_;
193 $self->env( $args{env} );
197 =head2 $self->prepare_write($c)
199 Enable autoflush on the output handle for CGI-based engines.
204 my ( $self, $c ) = @_;
206 # Set the output handle to autoflush
207 *STDOUT->autoflush(1);
209 $self->NEXT::prepare_write($c);
212 =head2 $self->read_chunk($c, $buffer, $length)
216 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
222 sub run { shift; shift->handle_request(@_) }
226 L<Catalyst> L<Catalyst::Engine>.
230 Sebastian Riedel, <sri@cpan.org>
232 Christian Hansen, <ch@ngmedia.com>
234 Andy Grundman, <andy@hybridized.org>
238 This program is free software, you can redistribute it and/or modify it under
239 the same terms as Perl itself.