Reformatted documentation
[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 __PACKAGE__->mk_accessors('env');
9
10 =head1 NAME
11
12 Catalyst::Engine::CGI - The CGI Engine
13
14 =head1 SYNOPSIS
15
16 A script using the Catalyst::Engine::CGI module might look like:
17
18     #!/usr/bin/perl -w
19
20     use strict;
21     use lib '/path/to/MyApp/lib';
22     use MyApp;
23
24     MyApp->run;
25
26 The application module (C<MyApp>) would use C<Catalyst>, which loads the
27 appropriate engine module.
28
29 =head1 DESCRIPTION
30
31 This is the Catalyst engine specialized for the CGI environment.
32
33 =head1 OVERLOADED METHODS
34
35 This class overloads some methods from C<Catalyst::Engine>.
36
37 =head2 $self->finalize_headers($c)
38
39 =cut
40
41 sub finalize_headers {
42     my ( $self, $c ) = @_;
43
44     $c->response->header( Status => $c->response->status );
45
46     print $c->response->headers->as_string("\015\012");
47     print "\015\012";
48 }
49
50 =head2 $self->prepare_connection($c)
51
52 =cut
53
54 sub prepare_connection {
55     my ( $self, $c ) = @_;
56     local (*ENV) = $self->env || \%ENV;
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 =head2 $self->prepare_headers($c)
89
90 =cut
91
92 sub prepare_headers {
93     my ( $self, $c ) = @_;
94     local (*ENV) = $self->env || \%ENV;
95
96     # Read headers from %ENV
97     while ( my ( $header, $value ) = each %ENV ) {
98         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
99         ( my $field = $header ) =~ s/^HTTPS?_//;
100         $c->req->headers->header( $field => $value );
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 = $ENV{SCRIPT_NAME} || '/';
116
117     # If we are running as a backend proxy, get the true hostname
118   PROXY_CHECK:
119     {
120         unless ( $c->config->{using_frontend_proxy} ) {
121             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
122             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
123         }
124         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
125
126         $host = $ENV{HTTP_X_FORWARDED_HOST};
127
128         # backend could be on any port, so
129         # assume frontend is on the default port
130         $port = $c->request->secure ? 443 : 80;
131     }
132
133     my $path = $base_path . $ENV{PATH_INFO};
134     $path =~ s{^/+}{};
135
136     my $uri = URI->new;
137     $uri->scheme($scheme);
138     $uri->host($host);
139     $uri->port($port);
140     $uri->path($path);
141     $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
142
143     # sanitize the URI
144     $uri = $uri->canonical;
145     $c->request->uri($uri);
146
147     # set the base URI
148     # base must end in a slash
149     $base_path .= '/' unless ( $base_path =~ /\/$/ );
150     my $base = $uri->clone;
151     $base->path_query($base_path);
152     $c->request->base($base);
153 }
154
155 =head2 $self->prepare_query_parameters($c)
156
157 =cut
158
159 sub prepare_query_parameters {
160     my ( $self, $c ) = @_;
161     local (*ENV) = $self->env || \%ENV;
162
163     if ( $ENV{QUERY_STRING} ) {
164         $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
165     }
166 }
167
168 =head2 $self->prepare_request($c, (env => \%env))
169
170 =cut
171
172 sub prepare_request {
173     my ( $self, $c, %args ) = @_;
174
175     if ( $args{env} ) {
176         $self->env( $args{env} );
177     }
178 }
179
180 =head2 $self->prepare_write($c)
181
182 Enable autoflush on the output handle for CGI-based engines.
183
184 =cut
185
186 sub prepare_write {
187     my ( $self, $c ) = @_;
188
189     # Set the output handle to autoflush
190     *STDOUT->autoflush(1);
191
192     $self->NEXT::prepare_write($c);
193 }
194
195 =head2 $self->read_chunk($c, $buffer, $length)
196
197 =cut
198
199 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
200
201 =head2 $self->run
202
203 =cut
204
205 sub run { shift; shift->handle_request(@_) }
206
207 =head1 SEE ALSO
208
209 L<Catalyst> L<Catalyst::Engine>.
210
211 =head1 AUTHORS
212
213 Sebastian Riedel, <sri@cpan.org>
214
215 Christian Hansen, <ch@ngmedia.com>
216
217 Andy Grundman, <andy@hybridized.org>
218
219 =head1 COPYRIGHT
220
221 This program is free software, you can redistribute it and/or modify it under
222 the same terms as Perl itself.
223
224 =cut
225
226 1;