reduce cost by cloning URI objects
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
1 package Catalyst::Engine::CGI;
2
3 use strict;
4 use base 'Catalyst::Engine';
5 use NEXT;
6 use URI;
7
8 my $uri_proto=URI->new();
9 __PACKAGE__->mk_accessors('env');
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     print $c->response->headers->as_string("\015\012");
48     print "\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     $c->request->address( $ENV{REMOTE_ADDR} );
60
61   PROXY_CHECK:
62     {
63         unless ( $c->config->{using_frontend_proxy} ) {
64             last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
65             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
66         }
67         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
68
69         # If we are running as a backend server, the user will always appear
70         # as 127.0.0.1. Select the most recent upstream IP (last in the list)
71         my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
72         $c->request->address($ip);
73     }
74
75     $c->request->hostname( $ENV{REMOTE_HOST} );
76     $c->request->protocol( $ENV{SERVER_PROTOCOL} );
77     $c->request->user( $ENV{REMOTE_USER} );
78     $c->request->method( $ENV{REQUEST_METHOD} );
79
80     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
81         $c->request->secure(1);
82     }
83
84     if ( $ENV{SERVER_PORT} == 443 ) {
85         $c->request->secure(1);
86     }
87 }
88
89 =head2 $self->prepare_headers($c)
90
91 =cut
92
93 sub prepare_headers {
94     my ( $self, $c ) = @_;
95     local (*ENV) = $self->env || \%ENV;
96
97     # Read headers from %ENV
98     foreach my $header ( keys %ENV ) {
99         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
100         ( my $field = $header ) =~ s/^HTTPS?_//;
101         $c->req->headers->header( $field => $ENV{$header} );
102     }
103 }
104
105 =head2 $self->prepare_path($c)
106
107 =cut
108
109 sub prepare_path {
110     my ( $self, $c ) = @_;
111     local (*ENV) = $self->env || \%ENV;
112
113     my $scheme = $c->request->secure ? 'https' : 'http';
114     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
115     my $port      = $ENV{SERVER_PORT} || 80;
116     my $base_path;
117     if ( exists $ENV{REDIRECT_URL} ) {
118         $base_path = $ENV{REDIRECT_URL};
119         $base_path =~ s/$ENV{PATH_INFO}$//;
120     }
121     else {
122         $base_path = $ENV{SCRIPT_NAME} || '/';
123     }
124
125     # If we are running as a backend proxy, get the true hostname
126   PROXY_CHECK:
127     {
128         unless ( $c->config->{using_frontend_proxy} ) {
129             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
130             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
131         }
132         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
133
134         $host = $ENV{HTTP_X_FORWARDED_HOST};
135
136         # backend could be on any port, so
137         # assume frontend is on the default port
138         $port = $c->request->secure ? 443 : 80;
139     }
140
141     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
142     $path =~ s{^/+}{};
143
144     my $uri = $uri_proto->clone;
145     $uri->scheme($scheme);
146     $uri->host($host);
147     $uri->port($port);
148     $uri->path($path);
149     $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
150
151     # sanitize the URI
152     $uri = $uri->canonical;
153     $c->request->uri($uri);
154
155     # set the base URI
156     # base must end in a slash
157     $base_path .= '/' unless ( $base_path =~ /\/$/ );
158     my $base = $uri->clone;
159     $base->path_query($base_path);
160     $c->request->base($base);
161 }
162
163 =head2 $self->prepare_query_parameters($c)
164
165 =cut
166
167 sub prepare_query_parameters {
168     my ( $self, $c ) = @_;
169     local (*ENV) = $self->env || \%ENV;
170
171     if ( $ENV{QUERY_STRING} ) {
172         $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
173     }
174 }
175
176 =head2 $self->prepare_request($c, (env => \%env))
177
178 =cut
179
180 sub prepare_request {
181     my ( $self, $c, %args ) = @_;
182
183     if ( $args{env} ) {
184         $self->env( $args{env} );
185     }
186 }
187
188 =head2 $self->prepare_write($c)
189
190 Enable autoflush on the output handle for CGI-based engines.
191
192 =cut
193
194 sub prepare_write {
195     my ( $self, $c ) = @_;
196
197     # Set the output handle to autoflush
198     *STDOUT->autoflush(1);
199
200     $self->NEXT::prepare_write($c);
201 }
202
203 =head2 $self->read_chunk($c, $buffer, $length)
204
205 =cut
206
207 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
208
209 =head2 $self->run
210
211 =cut
212
213 sub run { shift; shift->handle_request(@_) }
214
215 =head1 SEE ALSO
216
217 L<Catalyst> L<Catalyst::Engine>.
218
219 =head1 AUTHORS
220
221 Sebastian Riedel, <sri@cpan.org>
222
223 Christian Hansen, <ch@ngmedia.com>
224
225 Andy Grundman, <andy@hybridized.org>
226
227 =head1 COPYRIGHT
228
229 This program is free software, you can redistribute it and/or modify it under
230 the same terms as Perl itself.
231
232 =cut
233
234 1;