Merge the branch which gives ->req->remote_user without the deprecation code which...
[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 has _header_buf => (is => 'rw', clearer => '_clear_header_buf', predicate => '_has_header_buf');
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     $self->_header_buf($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} ) if exists $ENV{REMOTE_HOST};
74     $request->protocol( $ENV{SERVER_PROTOCOL} );
75     $request->user( $ENV{REMOTE_USER} );  # XXX: Deprecated. See Catalyst::Request for removal information
76     $request->remote_user( $ENV{REMOTE_USER} );
77     $request->method( $ENV{REQUEST_METHOD} );
78
79     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
80         $request->secure(1);
81     }
82
83     if ( $ENV{SERVER_PORT} == 443 ) {
84         $request->secure(1);
85     }
86 }
87
88 =head2 $self->prepare_headers($c)
89
90 =cut
91
92 sub prepare_headers {
93     my ( $self, $c ) = @_;
94     local (*ENV) = $self->env || \%ENV;
95     my $headers = $c->request->headers;
96     # Read headers from %ENV
97     foreach my $header ( keys %ENV ) {
98         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
99         ( my $field = $header ) =~ s/^HTTPS?_//;
100         $headers->header( $field => $ENV{$header} );
101     }
102 }
103
104 =head2 $self->prepare_path($c)
105
106 =cut
107
108 sub prepare_path {
109     my ( $self, $c ) = @_;
110     local (*ENV) = $self->env || \%ENV;
111
112     my $scheme = $c->request->secure ? 'https' : 'http';
113     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
114     my $port      = $ENV{SERVER_PORT} || 80;
115     my $base_path;
116     if ( exists $ENV{REDIRECT_URL} ) {
117         $base_path = $ENV{REDIRECT_URL};
118         $base_path =~ s/$ENV{PATH_INFO}$//;
119     }
120     else {
121         $base_path = $ENV{SCRIPT_NAME} || '/';
122     }
123
124     # If we are running as a backend proxy, get the true hostname
125   PROXY_CHECK:
126     {
127         unless ( $c->config->{using_frontend_proxy} ) {
128             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
129             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
130         }
131         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
132
133         $host = $ENV{HTTP_X_FORWARDED_HOST};
134
135         # backend could be on any port, so
136         # assume frontend is on the default port
137         $port = $c->request->secure ? 443 : 80;
138     }
139
140     # set the request URI
141     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
142     $path =~ s{^/+}{};
143     
144     # Using URI directly is way too slow, so we construct the URLs manually
145     my $uri_class = "URI::$scheme";
146     
147     # HTTP_HOST will include the port even if it's 80/443
148     $host =~ s/:(?:80|443)$//;
149     
150     if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
151         $host .= ":$port";
152     }
153     
154     # Escape the path
155     $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
156     $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
157     
158     my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
159     my $uri   = $scheme . '://' . $host . '/' . $path . $query;
160
161     $c->request->uri( bless \$uri, $uri_class );
162
163     # set the base URI
164     # base must end in a slash
165     $base_path .= '/' unless $base_path =~ m{/$};
166     
167     my $base_uri = $scheme . '://' . $host . $base_path;
168
169     $c->request->base( bless \$base_uri, $uri_class );
170 }
171
172 =head2 $self->prepare_query_parameters($c)
173
174 =cut
175
176 around prepare_query_parameters => sub {
177     my $orig = shift;
178     my ( $self, $c ) = @_;
179     local (*ENV) = $self->env || \%ENV;
180
181     if ( $ENV{QUERY_STRING} ) {
182         $self->$orig( $c, $ENV{QUERY_STRING} );
183     }
184 };
185
186 =head2 $self->prepare_request($c, (env => \%env))
187
188 =cut
189
190 sub prepare_request {
191     my ( $self, $c, %args ) = @_;
192
193     if ( $args{env} ) {
194         $self->env( $args{env} );
195     }
196 }
197
198 =head2 $self->prepare_write($c)
199
200 Enable autoflush on the output handle for CGI-based engines.
201
202 =cut
203
204 around prepare_write => sub {
205     *STDOUT->autoflush(1);
206     return shift->(@_);
207 };
208
209 =head2 $self->write($c, $buffer)
210
211 Writes the buffer to the client.
212
213 =cut
214
215 around write => sub {
216     my $orig = shift;
217     my ( $self, $c, $buffer ) = @_;
218
219     # Prepend the headers if they have not yet been sent
220     if ( $self->_has_header_buf ) {
221         $buffer = $self->_clear_header_buf . $buffer;
222     }
223
224     return $self->$orig( $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 Catalyst Contributors, see Catalyst.pm
246
247 =head1 COPYRIGHT
248
249 This program is free software, you can redistribute it and/or modify it under
250 the same terms as Perl itself.
251
252 =cut
253 no Moose;
254
255 1;