1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
10 Catalyst::Engine::CGI - The CGI Engine
14 A script using the Catalyst::Engine::CGI module might look like:
19 use lib '/path/to/MyApp/lib';
24 The application module (C<MyApp>) would use C<Catalyst>, which loads the
25 appropriate engine module.
29 This is the Catalyst engine specialized for the CGI environment.
31 =head1 OVERLOADED METHODS
33 This class overloads some methods from C<Catalyst::Engine>.
37 =item $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 =item $self->prepare_connection($c)
54 sub prepare_connection {
55 my ( $self, $c ) = @_;
57 $c->request->address( $ENV{REMOTE_ADDR} );
61 unless ( $c->config->{using_frontend_proxy} ) {
62 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
63 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
65 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
67 # If we are running as a backend server, the user will always appear
68 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
69 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
70 $c->request->address($ip);
73 $c->request->hostname( $ENV{REMOTE_HOST} );
74 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
75 $c->request->user( $ENV{REMOTE_USER} );
76 $c->request->method( $ENV{REQUEST_METHOD} );
78 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
79 $c->request->secure(1);
82 if ( $ENV{SERVER_PORT} == 443 ) {
83 $c->request->secure(1);
87 =item $self->prepare_headers($c)
92 my ( $self, $c ) = @_;
94 # Read headers from %ENV
95 while ( my ( $header, $value ) = each %ENV ) {
96 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
97 ( my $field = $header ) =~ s/^HTTPS?_//;
98 $c->req->headers->header( $field => $value );
102 =item $self->prepare_path($c)
107 my ( $self, $c ) = @_;
109 my $scheme = $c->request->secure ? 'https' : 'http';
110 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
111 my $port = $ENV{SERVER_PORT} || 80;
112 my $base_path = $ENV{SCRIPT_NAME} || '/';
114 # If we are running as a backend proxy, get the true hostname
117 unless ( $c->config->{using_frontend_proxy} ) {
118 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
119 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
121 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
123 $host = $ENV{HTTP_X_FORWARDED_HOST};
125 # backend could be on any port, so
126 # assume frontend is on the default port
127 $port = $c->request->secure ? 443 : 80;
130 my $path = $base_path . $ENV{PATH_INFO};
134 $uri->scheme($scheme);
138 $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
141 $uri = $uri->canonical;
142 $c->request->uri($uri);
145 # base must end in a slash
146 $base_path .= '/' unless ( $base_path =~ /\/$/ );
147 my $base = $uri->clone;
148 $base->path_query($base_path);
149 $c->request->base($base);
152 =item $self->prepare_query_parameters($c)
156 sub prepare_query_parameters {
157 my ( $self, $c ) = @_;
159 if ( $ENV{QUERY_STRING} ) {
160 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
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.