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