start using Class::C3, may need to add a reinitalize bit later, not sure
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
CommitLineData
fc7ec1d9 1package Catalyst::Engine::CGI;
2
0fc2d522 3use Class::C3;
7fa2c9c1 4use Moose;
5extends 'Catalyst::Engine';
e2fd5b5f 6
7fa2c9c1 7has env => (is => 'rw');
84528885 8
0fc2d522 9no Moose;
10
fc7ec1d9 11=head1 NAME
12
13Catalyst::Engine::CGI - The CGI Engine
14
15=head1 SYNOPSIS
16
23f9d934 17A script using the Catalyst::Engine::CGI module might look like:
18
9a33da6a 19 #!/usr/bin/perl -w
20
21 use strict;
22 use lib '/path/to/MyApp/lib';
23 use MyApp;
24
25 MyApp->run;
26
23f9d934 27The application module (C<MyApp>) would use C<Catalyst>, which loads the
28appropriate engine module.
fc7ec1d9 29
30=head1 DESCRIPTION
31
fbcc39ad 32This is the Catalyst engine specialized for the CGI environment.
e2fd5b5f 33
23f9d934 34=head1 OVERLOADED METHODS
fc7ec1d9 35
fbcc39ad 36This class overloads some methods from C<Catalyst::Engine>.
fc7ec1d9 37
b5ecfcf0 38=head2 $self->finalize_headers($c)
fc7ec1d9 39
40=cut
41
fbcc39ad 42sub finalize_headers {
43 my ( $self, $c ) = @_;
06e1b616 44
fbcc39ad 45 $c->response->header( Status => $c->response->status );
06e1b616 46
ac5c933b 47 $self->{_header_buf}
e512dd24 48 = $c->response->headers->as_string("\015\012") . "\015\012";
fc7ec1d9 49}
50
b5ecfcf0 51=head2 $self->prepare_connection($c)
fc7ec1d9 52
53=cut
54
fbcc39ad 55sub prepare_connection {
56 my ( $self, $c ) = @_;
b5ecfcf0 57 local (*ENV) = $self->env || \%ENV;
4f5ebacd 58
7fa2c9c1 59 my $request = $c->request;
60 $request->address( $ENV{REMOTE_ADDR} );
4f5ebacd 61
62 PROXY_CHECK:
fbcc39ad 63 {
64 unless ( $c->config->{using_frontend_proxy} ) {
65 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
66 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
5b387dfc 67 }
fbcc39ad 68 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
4f5ebacd 69
fbcc39ad 70 # If we are running as a backend server, the user will always appear
71 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
72 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
7fa2c9c1 73 $request->address($ip);
fc7ec1d9 74 }
08cf3dd6 75
7fa2c9c1 76 $request->hostname( $ENV{REMOTE_HOST} );
77 $request->protocol( $ENV{SERVER_PROTOCOL} );
78 $request->user( $ENV{REMOTE_USER} );
79 $request->method( $ENV{REQUEST_METHOD} );
fbcc39ad 80
81 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
7fa2c9c1 82 $request->secure(1);
5b387dfc 83 }
bfde09a2 84
fbcc39ad 85 if ( $ENV{SERVER_PORT} == 443 ) {
7fa2c9c1 86 $request->secure(1);
fbcc39ad 87 }
fc7ec1d9 88}
89
b5ecfcf0 90=head2 $self->prepare_headers($c)
fc7ec1d9 91
92=cut
93
fbcc39ad 94sub prepare_headers {
95 my ( $self, $c ) = @_;
b5ecfcf0 96 local (*ENV) = $self->env || \%ENV;
7fa2c9c1 97 my $headers = $c->request->headers;
fbcc39ad 98 # Read headers from %ENV
c82ed742 99 foreach my $header ( keys %ENV ) {
fbcc39ad 100 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
101 ( my $field = $header ) =~ s/^HTTPS?_//;
7fa2c9c1 102 $headers->header( $field => $ENV{$header} );
fbcc39ad 103 }
104}
316bf0f0 105
b5ecfcf0 106=head2 $self->prepare_path($c)
316bf0f0 107
fbcc39ad 108=cut
316bf0f0 109
fbcc39ad 110sub prepare_path {
111 my ( $self, $c ) = @_;
b5ecfcf0 112 local (*ENV) = $self->env || \%ENV;
fbcc39ad 113
4f5ebacd 114 my $scheme = $c->request->secure ? 'https' : 'http';
294f78ca 115 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
116 my $port = $ENV{SERVER_PORT} || 80;
0bcb98c7 117 my $base_path;
118 if ( exists $ENV{REDIRECT_URL} ) {
119 $base_path = $ENV{REDIRECT_URL};
120 $base_path =~ s/$ENV{PATH_INFO}$//;
121 }
122 else {
123 $base_path = $ENV{SCRIPT_NAME} || '/';
124 }
4f5ebacd 125
fbcc39ad 126 # If we are running as a backend proxy, get the true hostname
4f5ebacd 127 PROXY_CHECK:
fbcc39ad 128 {
129 unless ( $c->config->{using_frontend_proxy} ) {
130 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
131 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
316bf0f0 132 }
fbcc39ad 133 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
316bf0f0 134
fbcc39ad 135 $host = $ENV{HTTP_X_FORWARDED_HOST};
4f5ebacd 136
137 # backend could be on any port, so
fbcc39ad 138 # assume frontend is on the default port
139 $port = $c->request->secure ? 443 : 80;
316bf0f0 140 }
141
8d3c800b 142 # set the request URI
e701c5c6 143 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
fbcc39ad 144 $path =~ s{^/+}{};
ac5c933b 145
933ba403 146 # Using URI directly is way too slow, so we construct the URLs manually
147 my $uri_class = "URI::$scheme";
ac5c933b 148
de19de2e 149 # HTTP_HOST will include the port even if it's 80/443
150 $host =~ s/:(?:80|443)$//;
ac5c933b 151
de19de2e 152 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
933ba403 153 $host .= ":$port";
154 }
ac5c933b 155
933ba403 156 # Escape the path
157 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
158 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
ac5c933b 159
933ba403 160 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
161 my $uri = $scheme . '://' . $host . '/' . $path . $query;
162
163 $c->request->uri( bless \$uri, $uri_class );
164
8d3c800b 165 # set the base URI
166 # base must end in a slash
167 $base_path .= '/' unless $base_path =~ m{/$};
ac5c933b 168
8d3c800b 169 my $base_uri = $scheme . '://' . $host . $base_path;
170
171 $c->request->base( bless \$base_uri, $uri_class );
e7c0c583 172}
fc7ec1d9 173
b5ecfcf0 174=head2 $self->prepare_query_parameters($c)
fc7ec1d9 175
176=cut
177
0fc2d522 178sub prepare_query_parameters {
fbcc39ad 179 my ( $self, $c ) = @_;
b5ecfcf0 180 local (*ENV) = $self->env || \%ENV;
181
f8109766 182 if ( $ENV{QUERY_STRING} ) {
0fc2d522 183 $self->next::method( $c, $ENV{QUERY_STRING} );
f8109766 184 }
0fc2d522 185}
e7c0c583 186
b5ecfcf0 187=head2 $self->prepare_request($c, (env => \%env))
84528885 188
189=cut
190
191sub prepare_request {
192 my ( $self, $c, %args ) = @_;
193
194 if ( $args{env} ) {
b5ecfcf0 195 $self->env( $args{env} );
84528885 196 }
197}
198
b5ecfcf0 199=head2 $self->prepare_write($c)
bfde09a2 200
fbcc39ad 201Enable autoflush on the output handle for CGI-based engines.
bfde09a2 202
fbcc39ad 203=cut
e7c0c583 204
0fc2d522 205sub prepare_write {
4f5ebacd 206 *STDOUT->autoflush(1);
0fc2d522 207 return shift->next::method(@_);
208}
e7c0c583 209
e512dd24 210=head2 $self->write($c, $buffer)
211
212Writes the buffer to the client.
213
214=cut
215
0fc2d522 216sub write {
e512dd24 217 my ( $self, $c, $buffer ) = @_;
218
219 # Prepend the headers if they have not yet been sent
220 if ( my $headers = delete $self->{_header_buf} ) {
221 $buffer = $headers . $buffer;
222 }
7fa2c9c1 223
0fc2d522 224 return $self->next::method( $c, $buffer );
225}
e512dd24 226
b5ecfcf0 227=head2 $self->read_chunk($c, $buffer, $length)
e7c0c583 228
fbcc39ad 229=cut
e7c0c583 230
4f5ebacd 231sub read_chunk { shift; shift; *STDIN->sysread(@_); }
e7c0c583 232
b5ecfcf0 233=head2 $self->run
bfde09a2 234
fbcc39ad 235=cut
bfde09a2 236
fbcc39ad 237sub run { shift; shift->handle_request(@_) }
fc7ec1d9 238
fc7ec1d9 239=head1 SEE ALSO
240
fbcc39ad 241L<Catalyst> L<Catalyst::Engine>.
242
243=head1 AUTHORS
244
245Sebastian Riedel, <sri@cpan.org>
fc7ec1d9 246
fbcc39ad 247Christian Hansen, <ch@ngmedia.com>
fc7ec1d9 248
fbcc39ad 249Andy Grundman, <andy@hybridized.org>
fc7ec1d9 250
251=head1 COPYRIGHT
252
253This program is free software, you can redistribute it and/or modify it under
254the same terms as Perl itself.
255
256=cut
257
2581;