fixed final svk botch
[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
7 __PACKAGE__->mk_accessors('env');
8
9 =head1 NAME
10
11 Catalyst::Engine::CGI - The CGI Engine
12
13 =head1 SYNOPSIS
14
15 A script using the Catalyst::Engine::CGI module might look like:
16
17     #!/usr/bin/perl -w
18
19     use strict;
20     use lib '/path/to/MyApp/lib';
21     use MyApp;
22
23     MyApp->run;
24
25 The application module (C<MyApp>) would use C<Catalyst>, which loads the
26 appropriate engine module.
27
28 =head1 DESCRIPTION
29
30 This is the Catalyst engine specialized for the CGI environment.
31
32 =head1 OVERLOADED METHODS
33
34 This class overloads some methods from C<Catalyst::Engine>.
35
36 =head2 $self->finalize_headers($c)
37
38 =cut
39
40 sub finalize_headers {
41     my ( $self, $c ) = @_;
42
43     $c->response->header( Status => $c->response->status );
44
45     print $c->response->headers->as_string("\015\012") . "\015\012";
46 }
47
48 =head2 $self->prepare_connection($c)
49
50 =cut
51
52 sub prepare_connection {
53     my ( $self, $c ) = @_;
54     local (*ENV) = $self->env || \%ENV;
55
56     $c->request->address( $ENV{REMOTE_ADDR} );
57
58   PROXY_CHECK:
59     {
60         unless ( $c->config->{using_frontend_proxy} ) {
61             last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
62             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
63         }
64         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
65
66         # If we are running as a backend server, the user will always appear
67         # as 127.0.0.1. Select the most recent upstream IP (last in the list)
68         my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
69         $c->request->address($ip);
70     }
71
72     $c->request->hostname( $ENV{REMOTE_HOST} );
73     $c->request->protocol( $ENV{SERVER_PROTOCOL} );
74     $c->request->user( $ENV{REMOTE_USER} );
75     $c->request->method( $ENV{REQUEST_METHOD} );
76
77     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
78         $c->request->secure(1);
79     }
80
81     if ( $ENV{SERVER_PORT} == 443 ) {
82         $c->request->secure(1);
83     }
84 }
85
86 =head2 $self->prepare_headers($c)
87
88 =cut
89
90 sub prepare_headers {
91     my ( $self, $c ) = @_;
92     local (*ENV) = $self->env || \%ENV;
93
94     # Read headers from %ENV
95     foreach my $header ( keys %ENV ) {
96         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
97         ( my $field = $header ) =~ s/^HTTPS?_//;
98         $c->req->headers->header( $field => $ENV{$header} );
99     }
100 }
101
102 =head2 $self->prepare_path($c)
103
104 =cut
105
106 sub prepare_path {
107     my ( $self, $c ) = @_;
108     local (*ENV) = $self->env || \%ENV;
109
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;
114     if ( exists $ENV{REDIRECT_URL} ) {
115         $base_path = $ENV{REDIRECT_URL};
116         $base_path =~ s/$ENV{PATH_INFO}$//;
117     }
118     else {
119         $base_path = $ENV{SCRIPT_NAME} || '/';
120     }
121
122     # If we are running as a backend proxy, get the true hostname
123   PROXY_CHECK:
124     {
125         unless ( $c->config->{using_frontend_proxy} ) {
126             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
127             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
128         }
129         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
130
131         $host = $ENV{HTTP_X_FORWARDED_HOST};
132
133         # backend could be on any port, so
134         # assume frontend is on the default port
135         $port = $c->request->secure ? 443 : 80;
136     }
137
138     # set the request URI
139     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
140     $path =~ s{^/+}{};
141     
142     # Using URI directly is way too slow, so we construct the URLs manually
143     my $uri_class = "URI::$scheme";
144     
145     # HTTP_HOST will include the port even if it's 80
146     $host =~ s/:80$//;
147     
148     if ( $port != 80 && $host !~ /:/ ) {
149         $host .= ":$port";
150     }
151     
152     # Escape the path
153     $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
154     $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
155     
156     my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
157     my $uri   = $scheme . '://' . $host . '/' . $path . $query;
158
159     $c->request->uri( bless \$uri, $uri_class );
160
161     # set the base URI
162     # base must end in a slash
163     $base_path .= '/' unless $base_path =~ m{/$};
164     
165     my $base_uri = $scheme . '://' . $host . $base_path;
166
167     $c->request->base( bless \$base_uri, $uri_class );
168 }
169
170 =head2 $self->prepare_query_parameters($c)
171
172 =cut
173
174 sub prepare_query_parameters {
175     my ( $self, $c ) = @_;
176     local (*ENV) = $self->env || \%ENV;
177
178     if ( $ENV{QUERY_STRING} ) {
179         $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
180     }
181 }
182
183 =head2 $self->prepare_request($c, (env => \%env))
184
185 =cut
186
187 sub prepare_request {
188     my ( $self, $c, %args ) = @_;
189
190     if ( $args{env} ) {
191         $self->env( $args{env} );
192     }
193 }
194
195 =head2 $self->prepare_write($c)
196
197 Enable autoflush on the output handle for CGI-based engines.
198
199 =cut
200
201 sub prepare_write {
202     my ( $self, $c ) = @_;
203
204     # Set the output handle to autoflush
205     *STDOUT->autoflush(1);
206
207     $self->NEXT::prepare_write($c);
208 }
209
210 =head2 $self->read_chunk($c, $buffer, $length)
211
212 =cut
213
214 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
215
216 =head2 $self->run
217
218 =cut
219
220 sub run { shift; shift->handle_request(@_) }
221
222 =head1 SEE ALSO
223
224 L<Catalyst> L<Catalyst::Engine>.
225
226 =head1 AUTHORS
227
228 Sebastian Riedel, <sri@cpan.org>
229
230 Christian Hansen, <ch@ngmedia.com>
231
232 Andy Grundman, <andy@hybridized.org>
233
234 =head1 COPYRIGHT
235
236 This program is free software, you can redistribute it and/or modify it under
237 the same terms as Perl itself.
238
239 =cut
240
241 1;