Moved IE workarounds to be only in the HTTP engine
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
1 package Catalyst::Engine::CGI;
2
3 use strict;
4 use base 'Catalyst::Engine';
5 use NEXT;
6 use URI;
7
8 __PACKAGE__->mk_accessors('env');
9
10 =head1 NAME
11
12 Catalyst::Engine::CGI - The CGI Engine
13
14 =head1 SYNOPSIS
15
16 A script using the Catalyst::Engine::CGI module might look like:
17
18     #!/usr/bin/perl -w
19
20     use strict;
21     use lib '/path/to/MyApp/lib';
22     use MyApp;
23
24     MyApp->run;
25
26 The application module (C<MyApp>) would use C<Catalyst>, which loads the
27 appropriate engine module.
28
29 =head1 DESCRIPTION
30
31 This is the Catalyst engine specialized for the CGI environment.
32
33 =head1 OVERLOADED METHODS
34
35 This class overloads some methods from C<Catalyst::Engine>.
36
37 =head2 $self->finalize_headers($c)
38
39 =cut
40
41 sub finalize_headers {
42     my ( $self, $c ) = @_;
43
44     $c->response->header( Status => $c->response->status );
45
46     print $c->response->headers->as_string("\015\012");
47     print "\015\012";
48 }
49
50 =head2 $self->prepare_connection($c)
51
52 =cut
53
54 sub prepare_connection {
55     my ( $self, $c ) = @_;
56     local (*ENV) = $self->env || \%ENV;
57
58     $c->request->address( $ENV{REMOTE_ADDR} );
59
60   PROXY_CHECK:
61     {
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};
65         }
66         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
67
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);
72     }
73
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} );
78
79     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
80         $c->request->secure(1);
81     }
82
83     if ( $ENV{SERVER_PORT} == 443 ) {
84         $c->request->secure(1);
85     }
86 }
87
88 =head2 $self->prepare_headers($c)
89
90 =cut
91
92 sub prepare_headers {
93     my ( $self, $c ) = @_;
94     local (*ENV) = $self->env || \%ENV;
95
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} );
101     }
102 }
103
104 =head2 $self->prepare_path($c)
105
106 =cut
107
108 sub prepare_path {
109     my ( $self, $c ) = @_;
110     local (*ENV) = $self->env || \%ENV;
111
112     my $scheme = $c->request->secure ? 'https' : 'http';
113     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
114     my $port      = $ENV{SERVER_PORT} || 80;
115     my $base_path;
116     if ( exists $ENV{REDIRECT_URL} ) {
117         $base_path = $ENV{REDIRECT_URL};
118         $base_path =~ s/$ENV{PATH_INFO}$//;
119     }
120     else {
121         $base_path = $ENV{SCRIPT_NAME} || '/';
122     }
123
124     # If we are running as a backend proxy, get the true hostname
125   PROXY_CHECK:
126     {
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};
130         }
131         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
132
133         $host = $ENV{HTTP_X_FORWARDED_HOST};
134
135         # backend could be on any port, so
136         # assume frontend is on the default port
137         $port = $c->request->secure ? 443 : 80;
138     }
139
140     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
141     $path =~ s{^/+}{};
142
143     my $uri = URI->new;
144     $uri->scheme($scheme);
145     $uri->host($host);
146     $uri->port($port);
147     $uri->path($path);
148     $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
149
150     # sanitize the URI
151     $uri = $uri->canonical;
152     $c->request->uri($uri);
153
154     # set the base 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);
160 }
161
162 =head2 $self->prepare_query_parameters($c)
163
164 =cut
165
166 sub prepare_query_parameters {
167     my ( $self, $c ) = @_;
168     local (*ENV) = $self->env || \%ENV;
169
170     if ( $ENV{QUERY_STRING} ) {
171         $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
172     }
173 }
174
175 =head2 $self->prepare_request($c, (env => \%env))
176
177 =cut
178
179 sub prepare_request {
180     my ( $self, $c, %args ) = @_;
181
182     if ( $args{env} ) {
183         $self->env( $args{env} );
184     }
185 }
186
187 =head2 $self->prepare_write($c)
188
189 Enable autoflush on the output handle for CGI-based engines.
190
191 =cut
192
193 sub prepare_write {
194     my ( $self, $c ) = @_;
195
196     # Set the output handle to autoflush
197     *STDOUT->autoflush(1);
198
199     $self->NEXT::prepare_write($c);
200 }
201
202 =head2 $self->read_chunk($c, $buffer, $length)
203
204 =cut
205
206 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
207
208 =head2 $self->run
209
210 =cut
211
212 sub run { shift; shift->handle_request(@_) }
213
214 =head1 SEE ALSO
215
216 L<Catalyst> L<Catalyst::Engine>.
217
218 =head1 AUTHORS
219
220 Sebastian Riedel, <sri@cpan.org>
221
222 Christian Hansen, <ch@ngmedia.com>
223
224 Andy Grundman, <andy@hybridized.org>
225
226 =head1 COPYRIGHT
227
228 This program is free software, you can redistribute it and/or modify it under
229 the same terms as Perl itself.
230
231 =cut
232
233 1;