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