Merge 'trunk' into 'proxystuff'
[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
7 __PACKAGE__->mk_accessors('env');
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 =head2 $self->finalize_headers($c)
37
38 =cut
39
40 sub finalize_headers {
41     my ( $self, $c ) = @_;
42
43     $c->response->header( Status => $c->response->status );
44
45     $self->{_header_buf} 
46         = $c->response->headers->as_string("\015\012") . "\015\012";
47 }
48
49 =head2 $self->prepare_connection($c)
50
51 =cut
52
53 sub prepare_connection {
54     my ( $self, $c ) = @_;
55     local (*ENV) = $self->env || \%ENV;
56
57     my $proxy = $self->_proxy_info($c);
58
59     $c->request->address( $proxy->{address} || $ENV{REMOTE_ADDR} );
60     $c->request->hostname( $proxy->{host} || $ENV{REMOTE_HOST} );
61     $c->request->protocol( $proxy->{scheme} || $ENV{SERVER_PROTOCOL} );
62     $c->request->user( $ENV{REMOTE_USER} );
63     $c->request->method( $ENV{REQUEST_METHOD} );
64
65     if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
66         $c->request->secure(1);
67     }
68
69     if ( $ENV{SERVER_PORT} == 443 ) {
70         $c->request->secure(1);
71     }
72
73     if ( $c->request->protocol eq 'https' ) {
74         $c->request->secure(1);
75     }
76 }
77
78 =head2 $self->prepare_headers($c)
79
80 =cut
81
82 sub prepare_headers {
83     my ( $self, $c ) = @_;
84     local (*ENV) = $self->env || \%ENV;
85
86     # Read headers from %ENV
87     foreach my $header ( keys %ENV ) {
88         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
89         ( my $field = $header ) =~ s/^HTTPS?_//;
90         $c->req->headers->header( $field => $ENV{$header} );
91     }
92 }
93
94 =head2 $self->prepare_path($c)
95
96 =cut
97
98 sub prepare_path {
99     my ( $self, $c ) = @_;
100     local (*ENV) = $self->env || \%ENV;
101
102     my $proxy = $self->_proxy_info($c);
103
104     my $scheme = $c->request->secure ? 'https' : 'http';
105     my $host      = $proxy->{host} || $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
106     my $port      = $proxy->{port} || $ENV{SERVER_PORT} || 80;
107     my $base_path;
108     if ( exists $ENV{REDIRECT_URL} ) {
109         $base_path = $ENV{REDIRECT_URL};
110         $base_path =~ s/$ENV{PATH_INFO}$//;
111     }
112     else {
113         $base_path = $ENV{SCRIPT_NAME} || '/';
114     }
115
116     if ( $proxy->{path} ) {
117         $base_path = $proxy->{path} . $base_path;
118         $base_path =~ s{/$}{}g;
119     }
120
121     # set the request URI
122     my $path = $base_path . ( $ENV{PATH_INFO} || '' );
123     $path =~ s{^/+}{};
124
125     # Using URI directly is way too slow, so we construct the URLs manually
126     my $uri_class = "URI::$scheme";
127     
128     # HTTP_HOST will include the port even if it's 80/443
129     $host =~ s/:(?:80|443)$//;
130     
131     if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
132         $host .= ":$port";
133     }
134     
135     # Escape the path
136     $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
137     $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
138     
139     my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
140     my $uri   = $scheme . '://' . $host . '/' . $path . $query;
141
142     $c->request->uri( bless \$uri, $uri_class );
143
144     # set the base URI
145     # base must end in a slash
146     $base_path .= '/' unless $base_path =~ m{/$};
147     
148     my $base_uri = $scheme . '://' . $host . $base_path;
149
150     $c->request->base( bless \$base_uri, $uri_class );
151 }
152
153 =head2 $self->prepare_query_parameters($c)
154
155 =cut
156
157 sub prepare_query_parameters {
158     my ( $self, $c ) = @_;
159     local (*ENV) = $self->env || \%ENV;
160
161     if ( $ENV{QUERY_STRING} ) {
162         $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
163     }
164 }
165
166 =head2 $self->prepare_request($c, (env => \%env))
167
168 =cut
169
170 sub prepare_request {
171     my ( $self, $c, %args ) = @_;
172
173     if ( $args{env} ) {
174         $self->env( $args{env} );
175     }
176 }
177
178 =head2 $self->prepare_write($c)
179
180 Enable autoflush on the output handle for CGI-based engines.
181
182 =cut
183
184 sub prepare_write {
185     my ( $self, $c ) = @_;
186
187     # Set the output handle to autoflush
188     *STDOUT->autoflush(1);
189
190     $self->NEXT::prepare_write($c);
191 }
192
193 =head2 $self->write($c, $buffer)
194
195 Writes the buffer to the client.
196
197 =cut
198
199 sub write {
200     my ( $self, $c, $buffer ) = @_;
201
202     # Prepend the headers if they have not yet been sent
203     if ( my $headers = delete $self->{_header_buf} ) {
204         $buffer = $headers . $buffer;
205     }
206     
207     return $self->NEXT::write( $c, $buffer );
208 }
209
210 =head2 $self->read_chunk($c, $buffer, $length)
211
212 =cut
213
214 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
215
216 =head2 $self->run
217
218 =cut
219
220 sub run { shift; shift->handle_request(@_) }
221
222 =head1 SEE ALSO
223
224 L<Catalyst>, L<Catalyst::Engine>
225
226 =head1 AUTHORS
227
228 Catalyst Contributors, see Catalyst.pm
229
230 =head1 COPYRIGHT
231
232 This program is free software, you can redistribute it and/or modify it under
233 the same terms as Perl itself.
234
235 =cut
236
237 1;