start using Class::C3, may need to add a reinitalize bit later, not sure
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
1 package Catalyst::Engine::CGI;
2
3 use Class::C3;
4 use Moose;
5 extends 'Catalyst::Engine';
6
7 has env => (is => 'rw');
8
9 no Moose;
10
11 =head1 NAME
12
13 Catalyst::Engine::CGI - The CGI Engine
14
15 =head1 SYNOPSIS
16
17 A script using the Catalyst::Engine::CGI module might look like:
18
19     #!/usr/bin/perl -w
20
21     use strict;
22     use lib '/path/to/MyApp/lib';
23     use MyApp;
24
25     MyApp->run;
26
27 The application module (C<MyApp>) would use C<Catalyst>, which loads the
28 appropriate engine module.
29
30 =head1 DESCRIPTION
31
32 This is the Catalyst engine specialized for the CGI environment.
33
34 =head1 OVERLOADED METHODS
35
36 This class overloads some methods from C<Catalyst::Engine>.
37
38 =head2 $self->finalize_headers($c)
39
40 =cut
41
42 sub finalize_headers {
43     my ( $self, $c ) = @_;
44
45     $c->response->header( Status => $c->response->status );
46
47     $self->{_header_buf} 
48         = $c->response->headers->as_string("\015\012") . "\015\012";
49 }
50
51 =head2 $self->prepare_connection($c)
52
53 =cut
54
55 sub prepare_connection {
56     my ( $self, $c ) = @_;
57     local (*ENV) = $self->env || \%ENV;
58
59     my $request = $c->request;
60     $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         $request->address($ip);
74     }
75
76     $request->hostname( $ENV{REMOTE_HOST} );
77     $request->protocol( $ENV{SERVER_PROTOCOL} );
78     $request->user( $ENV{REMOTE_USER} );
79     $request->method( $ENV{REQUEST_METHOD} );
80
81     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
82         $request->secure(1);
83     }
84
85     if ( $ENV{SERVER_PORT} == 443 ) {
86         $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     my $headers = $c->request->headers;
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         $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     # set the request URI
143     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
144     $path =~ s{^/+}{};
145     
146     # Using URI directly is way too slow, so we construct the URLs manually
147     my $uri_class = "URI::$scheme";
148     
149     # HTTP_HOST will include the port even if it's 80/443
150     $host =~ s/:(?:80|443)$//;
151     
152     if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
153         $host .= ":$port";
154     }
155     
156     # Escape the path
157     $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
158     $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
159     
160     my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
161     my $uri   = $scheme . '://' . $host . '/' . $path . $query;
162
163     $c->request->uri( bless \$uri, $uri_class );
164
165     # set the base URI
166     # base must end in a slash
167     $base_path .= '/' unless $base_path =~ m{/$};
168     
169     my $base_uri = $scheme . '://' . $host . $base_path;
170
171     $c->request->base( bless \$base_uri, $uri_class );
172 }
173
174 =head2 $self->prepare_query_parameters($c)
175
176 =cut
177
178 sub prepare_query_parameters {
179     my ( $self, $c ) = @_;
180     local (*ENV) = $self->env || \%ENV;
181
182     if ( $ENV{QUERY_STRING} ) {
183         $self->next::method( $c, $ENV{QUERY_STRING} );
184     }
185 }
186
187 =head2 $self->prepare_request($c, (env => \%env))
188
189 =cut
190
191 sub prepare_request {
192     my ( $self, $c, %args ) = @_;
193
194     if ( $args{env} ) {
195         $self->env( $args{env} );
196     }
197 }
198
199 =head2 $self->prepare_write($c)
200
201 Enable autoflush on the output handle for CGI-based engines.
202
203 =cut
204
205 sub prepare_write {
206     *STDOUT->autoflush(1);
207     return shift->next::method(@_);
208 }
209
210 =head2 $self->write($c, $buffer)
211
212 Writes the buffer to the client.
213
214 =cut
215
216 sub write {
217     my ( $self, $c, $buffer ) = @_;
218
219     # Prepend the headers if they have not yet been sent
220     if ( my $headers = delete $self->{_header_buf} ) {
221         $buffer = $headers . $buffer;
222     }
223
224     return $self->next::method( $c, $buffer );
225 }
226
227 =head2 $self->read_chunk($c, $buffer, $length)
228
229 =cut
230
231 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
232
233 =head2 $self->run
234
235 =cut
236
237 sub run { shift; shift->handle_request(@_) }
238
239 =head1 SEE ALSO
240
241 L<Catalyst> L<Catalyst::Engine>.
242
243 =head1 AUTHORS
244
245 Sebastian Riedel, <sri@cpan.org>
246
247 Christian Hansen, <ch@ngmedia.com>
248
249 Andy Grundman, <andy@hybridized.org>
250
251 =head1 COPYRIGHT
252
253 This program is free software, you can redistribute it and/or modify it under
254 the same terms as Perl itself.
255
256 =cut
257
258 1;