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>.
37 =head2 $self->finalize_headers($c)
41 sub finalize_headers {
42 my ( $self, $c ) = @_;
44 $c->response->header( Status => $c->response->status );
46 print $c->response->headers->as_string("\015\012");
50 =head2 $self->prepare_connection($c)
54 sub prepare_connection {
55 my ( $self, $c ) = @_;
56 local (*ENV) = $self->env || \%ENV;
58 $c->request->address( $ENV{REMOTE_ADDR} );
62 unless ( $c->config->{using_frontend_proxy} ) {
63 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
64 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
66 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
68 # If we are running as a backend server, the user will always appear
69 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
70 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
71 $c->request->address($ip);
74 $c->request->hostname( $ENV{REMOTE_HOST} );
75 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
76 $c->request->user( $ENV{REMOTE_USER} );
77 $c->request->method( $ENV{REQUEST_METHOD} );
79 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
80 $c->request->secure(1);
83 if ( $ENV{SERVER_PORT} == 443 ) {
84 $c->request->secure(1);
88 =head2 $self->prepare_headers($c)
93 my ( $self, $c ) = @_;
94 local (*ENV) = $self->env || \%ENV;
96 # Read headers from %ENV
97 foreach my $header ( keys %ENV ) {
98 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
99 ( my $field = $header ) =~ s/^HTTPS?_//;
100 $c->req->headers->header( $field => $ENV{$header} );
104 =head2 $self->prepare_path($c)
109 my ( $self, $c ) = @_;
110 local (*ENV) = $self->env || \%ENV;
112 my $scheme = $c->request->secure ? 'https' : 'http';
113 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
114 my $port = $ENV{SERVER_PORT} || 80;
116 if ( exists $ENV{REDIRECT_URL} ) {
117 $base_path = $ENV{REDIRECT_URL};
118 $base_path =~ s/$ENV{PATH_INFO}$//;
121 $base_path = $ENV{SCRIPT_NAME} || '/';
124 # If we are running as a backend proxy, get the true hostname
127 unless ( $c->config->{using_frontend_proxy} ) {
128 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
129 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
131 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
133 $host = $ENV{HTTP_X_FORWARDED_HOST};
135 # backend could be on any port, so
136 # assume frontend is on the default port
137 $port = $c->request->secure ? 443 : 80;
140 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
144 $uri->scheme($scheme);
148 $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
151 $uri = $uri->canonical;
152 $c->request->uri($uri);
155 # base must end in a slash
156 $base_path .= '/' unless ( $base_path =~ /\/$/ );
157 my $base = $uri->clone;
158 $base->path_query($base_path);
159 $c->request->base($base);
162 =head2 $self->prepare_query_parameters($c)
166 sub prepare_query_parameters {
167 my ( $self, $c ) = @_;
168 local (*ENV) = $self->env || \%ENV;
170 if ( $ENV{QUERY_STRING} ) {
171 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
175 =head2 $self->prepare_request($c, (env => \%env))
179 sub prepare_request {
180 my ( $self, $c, %args ) = @_;
183 $self->env( $args{env} );
187 =head2 $self->prepare_write($c)
189 Enable autoflush on the output handle for CGI-based engines.
194 my ( $self, $c ) = @_;
196 # Set the output handle to autoflush
197 *STDOUT->autoflush(1);
199 $self->NEXT::prepare_write($c);
202 =head2 $self->read_chunk($c, $buffer, $length)
206 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
212 sub run { shift; shift->handle_request(@_) }
216 L<Catalyst> L<Catalyst::Engine>.
220 Sebastian Riedel, <sri@cpan.org>
222 Christian Hansen, <ch@ngmedia.com>
224 Andy Grundman, <andy@hybridized.org>
228 This program is free software, you can redistribute it and/or modify it under
229 the same terms as Perl itself.