Stop calling class data methods on instances + commented out warning
[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 _header_buf => (is => 'rw', clearer => '_clear_header_buf', predicate => '_has_header_buf');
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($c->response->headers->as_string("\015\012") . "\015\012");
45 }
46
47 =head2 $self->prepare_connection($c)
48
49 =cut
50
51 sub prepare_connection {
52     my ( $self, $c ) = @_;
53     local (*ENV) = $self->env || \%ENV;
54
55     my $request = $c->request;
56     $request->address( $ENV{REMOTE_ADDR} );
57
58   PROXY_CHECK:
59     {
60         unless ( ref($c)->config->{using_frontend_proxy} ) {
61             last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
62             last PROXY_CHECK if ref($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         $request->address($ip);
70         if ( defined $ENV{HTTP_X_FORWARDED_PORT} ) {
71             $ENV{SERVER_PORT} = $ENV{HTTP_X_FORWARDED_PORT};
72         }
73     }
74
75     $request->hostname( $ENV{REMOTE_HOST} ) if exists $ENV{REMOTE_HOST};
76     $request->protocol( $ENV{SERVER_PROTOCOL} );
77     $request->user( $ENV{REMOTE_USER} );  # XXX: Deprecated. See Catalyst::Request for removal information
78     $request->remote_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 ( ref($c)->config->{using_frontend_proxy} ) {
130             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
131             last PROXY_CHECK if ref($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         if ( $ENV{HTTP_X_FORWARDED_PORT} ) {
141             $port = $ENV{HTTP_X_FORWARDED_PORT};
142         }
143     }
144
145     # set the request URI
146     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
147     $path =~ s{^/+}{};
148
149     # Using URI directly is way too slow, so we construct the URLs manually
150     my $uri_class = "URI::$scheme";
151
152     # HTTP_HOST will include the port even if it's 80/443
153     $host =~ s/:(?:80|443)$//;
154
155     if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
156         $host .= ":$port";
157     }
158
159     # Escape the path
160     $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
161     $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
162
163     my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
164     my $uri   = $scheme . '://' . $host . '/' . $path . $query;
165
166     $c->request->uri( bless \$uri, $uri_class );
167
168     # set the base URI
169     # base must end in a slash
170     $base_path .= '/' unless $base_path =~ m{/$};
171
172     my $base_uri = $scheme . '://' . $host . $base_path;
173
174     $c->request->base( bless \$base_uri, $uri_class );
175 }
176
177 =head2 $self->prepare_query_parameters($c)
178
179 =cut
180
181 around prepare_query_parameters => sub {
182     my $orig = shift;
183     my ( $self, $c ) = @_;
184     local (*ENV) = $self->env || \%ENV;
185
186     if ( $ENV{QUERY_STRING} ) {
187         $self->$orig( $c, $ENV{QUERY_STRING} );
188     }
189 };
190
191 =head2 $self->prepare_request($c, (env => \%env))
192
193 =cut
194
195 sub prepare_request {
196     my ( $self, $c, %args ) = @_;
197
198     if ( $args{env} ) {
199         $self->env( $args{env} );
200     }
201 }
202
203 =head2 $self->prepare_write($c)
204
205 Enable autoflush on the output handle for CGI-based engines.
206
207 =cut
208
209 around prepare_write => sub {
210     *STDOUT->autoflush(1);
211     return shift->(@_);
212 };
213
214 =head2 $self->write($c, $buffer)
215
216 Writes the buffer to the client.
217
218 =cut
219
220 around write => sub {
221     my $orig = shift;
222     my ( $self, $c, $buffer ) = @_;
223
224     # Prepend the headers if they have not yet been sent
225     if ( $self->_has_header_buf ) {
226         $buffer = $self->_clear_header_buf . $buffer;
227     }
228
229     return $self->$orig( $c, $buffer );
230 };
231
232 =head2 $self->read_chunk($c, $buffer, $length)
233
234 =cut
235
236 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
237
238 =head2 $self->run
239
240 =cut
241
242 sub run { shift; shift->handle_request( env => \%ENV ) }
243
244 =head1 SEE ALSO
245
246 L<Catalyst>, L<Catalyst::Engine>
247
248 =head1 AUTHORS
249
250 Catalyst Contributors, see Catalyst.pm
251
252 =head1 COPYRIGHT
253
254 This library is free software. You can redistribute it and/or modify it under
255 the same terms as Perl itself.
256
257 =cut
258 no Moose;
259
260 1;