Avoid 'print on closed filehandle' warnings from IE
[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     return unless *STDOUT->opened();
47
48     print $c->response->headers->as_string("\015\012");
49     print "\015\012";
50 }
51
52 =head2 $self->prepare_connection($c)
53
54 =cut
55
56 sub prepare_connection {
57     my ( $self, $c ) = @_;
58     local (*ENV) = $self->env || \%ENV;
59
60     $c->request->address( $ENV{REMOTE_ADDR} );
61
62   PROXY_CHECK:
63     {
64         unless ( $c->config->{using_frontend_proxy} ) {
65             last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
66             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
67         }
68         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
69
70         # If we are running as a backend server, the user will always appear
71         # as 127.0.0.1. Select the most recent upstream IP (last in the list)
72         my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
73         $c->request->address($ip);
74     }
75
76     $c->request->hostname( $ENV{REMOTE_HOST} );
77     $c->request->protocol( $ENV{SERVER_PROTOCOL} );
78     $c->request->user( $ENV{REMOTE_USER} );
79     $c->request->method( $ENV{REQUEST_METHOD} );
80
81     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
82         $c->request->secure(1);
83     }
84
85     if ( $ENV{SERVER_PORT} == 443 ) {
86         $c->request->secure(1);
87     }
88 }
89
90 =head2 $self->prepare_headers($c)
91
92 =cut
93
94 sub prepare_headers {
95     my ( $self, $c ) = @_;
96     local (*ENV) = $self->env || \%ENV;
97
98     # Read headers from %ENV
99     foreach my $header ( keys %ENV ) {
100         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
101         ( my $field = $header ) =~ s/^HTTPS?_//;
102         $c->req->headers->header( $field => $ENV{$header} );
103     }
104 }
105
106 =head2 $self->prepare_path($c)
107
108 =cut
109
110 sub prepare_path {
111     my ( $self, $c ) = @_;
112     local (*ENV) = $self->env || \%ENV;
113
114     my $scheme = $c->request->secure ? 'https' : 'http';
115     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
116     my $port      = $ENV{SERVER_PORT} || 80;
117     my $base_path;
118     if ( exists $ENV{REDIRECT_URL} ) {
119         $base_path = $ENV{REDIRECT_URL};
120         $base_path =~ s/$ENV{PATH_INFO}$//;
121     }
122     else {
123         $base_path = $ENV{SCRIPT_NAME} || '/';
124     }
125
126     # If we are running as a backend proxy, get the true hostname
127   PROXY_CHECK:
128     {
129         unless ( $c->config->{using_frontend_proxy} ) {
130             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
131             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
132         }
133         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
134
135         $host = $ENV{HTTP_X_FORWARDED_HOST};
136
137         # backend could be on any port, so
138         # assume frontend is on the default port
139         $port = $c->request->secure ? 443 : 80;
140     }
141
142     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
143     $path =~ s{^/+}{};
144
145     my $uri = URI->new;
146     $uri->scheme($scheme);
147     $uri->host($host);
148     $uri->port($port);
149     $uri->path($path);
150     $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
151
152     # sanitize the URI
153     $uri = $uri->canonical;
154     $c->request->uri($uri);
155
156     # set the base URI
157     # base must end in a slash
158     $base_path .= '/' unless ( $base_path =~ /\/$/ );
159     my $base = $uri->clone;
160     $base->path_query($base_path);
161     $c->request->base($base);
162 }
163
164 =head2 $self->prepare_query_parameters($c)
165
166 =cut
167
168 sub prepare_query_parameters {
169     my ( $self, $c ) = @_;
170     local (*ENV) = $self->env || \%ENV;
171
172     if ( $ENV{QUERY_STRING} ) {
173         $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
174     }
175 }
176
177 =head2 $self->prepare_request($c, (env => \%env))
178
179 =cut
180
181 sub prepare_request {
182     my ( $self, $c, %args ) = @_;
183
184     if ( $args{env} ) {
185         $self->env( $args{env} );
186     }
187 }
188
189 =head2 $self->prepare_write($c)
190
191 Enable autoflush on the output handle for CGI-based engines.
192
193 =cut
194
195 sub prepare_write {
196     my ( $self, $c ) = @_;
197
198     # Set the output handle to autoflush
199     *STDOUT->autoflush(1);
200
201     $self->NEXT::prepare_write($c);
202 }
203
204 =head2 $self->read_chunk($c, $buffer, $length)
205
206 =cut
207
208 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
209
210 =head2 $self->run
211
212 =cut
213
214 sub run { shift; shift->handle_request(@_) }
215
216 =head1 SEE ALSO
217
218 L<Catalyst> L<Catalyst::Engine>.
219
220 =head1 AUTHORS
221
222 Sebastian Riedel, <sri@cpan.org>
223
224 Christian Hansen, <ch@ngmedia.com>
225
226 Andy Grundman, <andy@hybridized.org>
227
228 =head1 COPYRIGHT
229
230 This program is free software, you can redistribute it and/or modify it under
231 the same terms as Perl itself.
232
233 =cut
234
235 1;