Removed req->handle and res->handle
[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
126         # backend could be on any port, so
127         # assume frontend is on the default port
128         $port = $c->request->secure ? 443 : 80;
129     }
130
131     my $path = $base_path . $ENV{PATH_INFO};
132     $path =~ s{^/+}{};
133
134     my $uri = URI->new;
135     $uri->scheme($scheme);
136     $uri->host($host);
137     $uri->port($port);
138     $uri->path($path);
139     $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
140
141     # sanitize the URI
142     $uri = $uri->canonical;
143     $c->request->uri($uri);
144
145     # set the base URI
146     # base must end in a slash
147     $base_path .= '/' unless ( $base_path =~ /\/$/ );
148     my $base = $uri->clone;
149     $base->path_query($base_path);
150     $c->request->base($base);
151 }
152
153 =item $self->prepare_query_parameters($c)
154
155 =cut
156
157 sub prepare_query_parameters {
158     my ( $self, $c ) = @_;
159
160     my $u = URI::Query->new( $ENV{QUERY_STRING} );
161     $c->request->query_parameters( { $u->hash } );
162 }
163
164 =item $self->prepare_write($c)
165
166 Enable autoflush on the output handle for CGI-based engines.
167
168 =cut
169
170 sub prepare_write {
171     my ( $self, $c ) = @_;
172
173     # Set the output handle to autoflush
174     *STDOUT->autoflush(1);
175
176     $self->NEXT::prepare_write($c);
177 }
178
179 =item $self->read_chunk($c, $buffer, $length)
180
181 =cut
182
183 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
184
185 =item $self->run
186
187 =cut
188
189 sub run { shift; shift->handle_request(@_) }
190
191 =back
192
193 =head1 SEE ALSO
194
195 L<Catalyst> L<Catalyst::Engine>.
196
197 =head1 AUTHORS
198
199 Sebastian Riedel, <sri@cpan.org>
200
201 Christian Hansen, <ch@ngmedia.com>
202
203 Andy Grundman, <andy@hybridized.org>
204
205 =head1 COPYRIGHT
206
207 This program is free software, you can redistribute it and/or modify it under
208 the same terms as Perl itself.
209
210 =cut
211
212 1;