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;
138 # set the request URI
139 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
142 # Using URI directly is way too slow, so we construct the URLs manually
143 my $uri_class = "URI::$scheme";
145 # HTTP_HOST will include the port even if it's 80
148 if ( $port != 80 && $host !~ /:/ ) {
153 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
154 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
156 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
157 my $uri = $scheme . '://' . $host . '/' . $path . $query;
159 $c->request->uri( bless \$uri, $uri_class );
162 # base must end in a slash
163 $base_path .= '/' unless $base_path =~ m{/$};
165 my $base_uri = $scheme . '://' . $host . $base_path;
167 $c->request->base( bless \$base_uri, $uri_class );
170 =head2 $self->prepare_query_parameters($c)
174 sub prepare_query_parameters {
175 my ( $self, $c ) = @_;
176 local (*ENV) = $self->env || \%ENV;
178 if ( $ENV{QUERY_STRING} ) {
179 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
183 =head2 $self->prepare_request($c, (env => \%env))
187 sub prepare_request {
188 my ( $self, $c, %args ) = @_;
191 $self->env( $args{env} );
195 =head2 $self->prepare_write($c)
197 Enable autoflush on the output handle for CGI-based engines.
202 my ( $self, $c ) = @_;
204 # Set the output handle to autoflush
205 *STDOUT->autoflush(1);
207 $self->NEXT::prepare_write($c);
210 =head2 $self->read_chunk($c, $buffer, $length)
214 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
220 sub run { shift; shift->handle_request(@_) }
224 L<Catalyst> L<Catalyst::Engine>.
228 Sebastian Riedel, <sri@cpan.org>
230 Christian Hansen, <ch@ngmedia.com>
232 Andy Grundman, <andy@hybridized.org>
236 This program is free software, you can redistribute it and/or modify it under
237 the same terms as Perl itself.