1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
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>.
38 =item $self->finalize_headers($c)
42 sub finalize_headers {
43 my ( $self, $c ) = @_;
45 $c->response->header( Status => $c->response->status );
47 print $c->response->headers->as_string("\015\012");
51 =item $self->prepare_connection($c)
55 sub prepare_connection {
56 my ( $self, $c ) = @_;
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 =item $self->prepare_headers($c)
93 my ( $self, $c ) = @_;
95 # Read headers from %ENV
96 while ( my ( $header, $value ) = each %ENV ) {
97 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
98 ( my $field = $header ) =~ s/^HTTPS?_//;
99 $c->req->headers->header( $field => $value );
103 =item $self->prepare_path($c)
108 my ( $self, $c ) = @_;
110 my $scheme = $c->request->secure ? 'https' : 'http';
111 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
112 my $port = $ENV{SERVER_PORT} || 80;
113 my $base_path = $ENV{SCRIPT_NAME} || '/';
115 # If we are running as a backend proxy, get the true hostname
118 unless ( $c->config->{using_frontend_proxy} ) {
119 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
120 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
122 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
124 $host = $ENV{HTTP_X_FORWARDED_HOST};
126 # backend could be on any port, so
127 # assume frontend is on the default port
128 $port = $c->request->secure ? 443 : 80;
131 my $path = $base_path . $ENV{PATH_INFO};
135 $uri->scheme($scheme);
139 $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
142 $uri = $uri->canonical;
143 $c->request->uri($uri);
146 # base must end in a slash
147 $base_path .= '/' unless ( $base_path =~ /\/$/ );
148 my $base = $uri->clone;
149 $base->path_query($base_path);
150 $c->request->base($base);
153 =item $self->prepare_query_parameters($c)
157 sub prepare_query_parameters {
158 my ( $self, $c ) = @_;
160 my $u = URI::Query->new( $ENV{QUERY_STRING} );
161 $c->request->query_parameters( { $u->hash } );
164 =item $self->prepare_write($c)
166 Enable autoflush on the output handle for CGI-based engines.
171 my ( $self, $c ) = @_;
173 # Set the output handle to autoflush
174 *STDOUT->autoflush(1);
176 $self->NEXT::prepare_write($c);
179 =item $self->read_chunk($c, $buffer, $length)
183 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
189 sub run { shift; shift->handle_request(@_) }
195 L<Catalyst> L<Catalyst::Engine>.
199 Sebastian Riedel, <sri@cpan.org>
201 Christian Hansen, <ch@ngmedia.com>
203 Andy Grundman, <andy@hybridized.org>
207 This program is free software, you can redistribute it and/or modify it under
208 the same terms as Perl itself.