1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
8 my $uri_proto=URI->new();
9 __PACKAGE__->mk_accessors('env');
13 Catalyst::Engine::CGI - The CGI Engine
17 A script using the Catalyst::Engine::CGI module might look like:
22 use lib '/path/to/MyApp/lib';
27 The application module (C<MyApp>) would use C<Catalyst>, which loads the
28 appropriate engine module.
32 This is the Catalyst engine specialized for the CGI environment.
34 =head1 OVERLOADED METHODS
36 This class overloads some methods from C<Catalyst::Engine>.
38 =head2 $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 =head2 $self->prepare_connection($c)
55 sub prepare_connection {
56 my ( $self, $c ) = @_;
57 local (*ENV) = $self->env || \%ENV;
59 $c->request->address( $ENV{REMOTE_ADDR} );
63 unless ( $c->config->{using_frontend_proxy} ) {
64 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
65 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
67 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
69 # If we are running as a backend server, the user will always appear
70 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
71 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
72 $c->request->address($ip);
75 $c->request->hostname( $ENV{REMOTE_HOST} );
76 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
77 $c->request->user( $ENV{REMOTE_USER} );
78 $c->request->method( $ENV{REQUEST_METHOD} );
80 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
81 $c->request->secure(1);
84 if ( $ENV{SERVER_PORT} == 443 ) {
85 $c->request->secure(1);
89 =head2 $self->prepare_headers($c)
94 my ( $self, $c ) = @_;
95 local (*ENV) = $self->env || \%ENV;
97 # Read headers from %ENV
98 foreach my $header ( keys %ENV ) {
99 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
100 ( my $field = $header ) =~ s/^HTTPS?_//;
101 $c->req->headers->header( $field => $ENV{$header} );
105 =head2 $self->prepare_path($c)
110 my ( $self, $c ) = @_;
111 local (*ENV) = $self->env || \%ENV;
113 my $scheme = $c->request->secure ? 'https' : 'http';
114 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
115 my $port = $ENV{SERVER_PORT} || 80;
117 if ( exists $ENV{REDIRECT_URL} ) {
118 $base_path = $ENV{REDIRECT_URL};
119 $base_path =~ s/$ENV{PATH_INFO}$//;
122 $base_path = $ENV{SCRIPT_NAME} || '/';
125 # If we are running as a backend proxy, get the true hostname
128 unless ( $c->config->{using_frontend_proxy} ) {
129 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
130 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
132 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
134 $host = $ENV{HTTP_X_FORWARDED_HOST};
136 # backend could be on any port, so
137 # assume frontend is on the default port
138 $port = $c->request->secure ? 443 : 80;
141 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
144 my $uri = $uri_proto->clone;
145 $uri->scheme($scheme);
149 $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
152 $uri = $uri->canonical;
153 $c->request->uri($uri);
156 # base must end in a slash
157 $base_path .= '/' unless ( $base_path =~ /\/$/ );
158 my $base = $uri->clone;
159 $base->path_query($base_path);
160 $c->request->base($base);
163 =head2 $self->prepare_query_parameters($c)
167 sub prepare_query_parameters {
168 my ( $self, $c ) = @_;
169 local (*ENV) = $self->env || \%ENV;
171 if ( $ENV{QUERY_STRING} ) {
172 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
176 =head2 $self->prepare_request($c, (env => \%env))
180 sub prepare_request {
181 my ( $self, $c, %args ) = @_;
184 $self->env( $args{env} );
188 =head2 $self->prepare_write($c)
190 Enable autoflush on the output handle for CGI-based engines.
195 my ( $self, $c ) = @_;
197 # Set the output handle to autoflush
198 *STDOUT->autoflush(1);
200 $self->NEXT::prepare_write($c);
203 =head2 $self->read_chunk($c, $buffer, $length)
207 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
213 sub run { shift; shift->handle_request(@_) }
217 L<Catalyst> L<Catalyst::Engine>.
221 Sebastian Riedel, <sri@cpan.org>
223 Christian Hansen, <ch@ngmedia.com>
225 Andy Grundman, <andy@hybridized.org>
229 This program is free software, you can redistribute it and/or modify it under
230 the same terms as Perl itself.