Merged 5.49_01 (r1339) from refactored branch to trunk
[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 use URI::Query;
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 =over 4
37
38 =item $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 =item $self->prepare_connection($c)
52
53 =cut
54
55 sub prepare_connection {
56     my ( $self, $c ) = @_;
57     
58     $c->request->address( $ENV{REMOTE_ADDR} );
59     
60     PROXY_CHECK:
61     {
62         unless ( $c->config->{using_frontend_proxy} ) {
63             last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
64             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
65         }
66         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
67    
68         # If we are running as a backend server, the user will always appear
69         # as 127.0.0.1. Select the most recent upstream IP (last in the list)
70         my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
71         $c->request->address( $ip );
72     }
73
74     $c->request->hostname( $ENV{REMOTE_HOST} );
75     $c->request->protocol( $ENV{SERVER_PROTOCOL} );
76     $c->request->user( $ENV{REMOTE_USER} );
77     $c->request->method( $ENV{REQUEST_METHOD} );
78
79     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
80         $c->request->secure(1);
81     }
82
83     if ( $ENV{SERVER_PORT} == 443 ) {
84         $c->request->secure(1);
85     }
86 }
87
88 =item $self->prepare_headers($c)
89
90 =cut
91
92 sub prepare_headers {
93     my ( $self, $c ) = @_;
94
95     # Read headers from %ENV
96     while ( my ( $header, $value ) = each %ENV ) {
97         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
98         ( my $field = $header ) =~ s/^HTTPS?_//;
99         $c->req->headers->header( $field => $value );
100     }
101 }
102
103 =item $self->prepare_path($c)
104
105 =cut
106
107 sub prepare_path {
108     my ( $self, $c ) = @_;
109
110     my $scheme    = $c->request->secure ? 'https' : 'http';
111     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
112     my $port      = $ENV{SERVER_PORT} || 80;
113     my $base_path = $ENV{SCRIPT_NAME} || '/';
114     
115     # If we are running as a backend proxy, get the true hostname
116     PROXY_CHECK:
117     {
118         unless ( $c->config->{using_frontend_proxy} ) {
119             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
120             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
121         }
122         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
123
124         $host = $ENV{HTTP_X_FORWARDED_HOST};
125         # backend could be on any port, so 
126         # assume frontend is on the default port
127         $port = $c->request->secure ? 443 : 80;
128     }
129
130     my $path = $base_path . $ENV{PATH_INFO};
131     $path =~ s{^/+}{};
132     
133     my $uri = URI->new;
134     $uri->scheme( $scheme );
135     $uri->host( $host );
136     $uri->port( $port );    
137     $uri->path( $path );
138     $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
139     
140     # sanitize the URI
141     $uri = $uri->canonical;
142     $c->request->uri( $uri );
143
144     # set the base URI
145     # base must end in a slash
146     $base_path .= '/' unless ( $base_path =~ /\/$/ );    
147     my $base = $uri->clone;
148     $base->path_query( $base_path );
149     $c->request->base( $base );
150 }
151
152 =item $self->prepare_query_parameters($c)
153
154 =cut
155
156 sub prepare_query_parameters {
157     my ( $self, $c ) = @_;
158     
159     my $u = URI::Query->new( $ENV{QUERY_STRING} );
160     $c->request->query_parameters( { $u->hash } );
161 }
162
163 =item $self->prepare_write($c)
164
165 Enable autoflush on the output handle for CGI-based engines.
166
167 =cut
168
169 sub prepare_write {
170     my ( $self, $c ) = @_;
171     
172     # Set the output handle to autoflush
173     $c->response->handle->autoflush(1);
174     
175     $self->NEXT::prepare_write( $c );
176 }
177
178 =item $self->read_chunk($c, $buffer, $length)
179
180 =cut
181
182 sub read_chunk { shift; shift->request->handle->sysread( @_ ); }
183
184 =item $self->run
185
186 =cut
187
188 sub run { shift; shift->handle_request(@_) }
189
190 =back
191
192 =head1 SEE ALSO
193
194 L<Catalyst> L<Catalyst::Engine>.
195
196 =head1 AUTHORS
197
198 Sebastian Riedel, <sri@cpan.org>
199
200 Christian Hansen, <ch@ngmedia.com>
201
202 Andy Grundman, <andy@hybridized.org>
203
204 =head1 COPYRIGHT
205
206 This program is free software, you can redistribute it and/or modify it under
207 the same terms as Perl itself.
208
209 =cut
210
211 1;