Stop remote tests breaking everything, and force authors to run some remote tests.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
CommitLineData
fc7ec1d9 1package Catalyst::Engine::CGI;
2
7fa2c9c1 3use Moose;
4extends 'Catalyst::Engine';
e2fd5b5f 5
02570318 6has _header_buf => (is => 'rw', clearer => '_clear_header_buf', predicate => '_has_header_buf');
84528885 7
fc7ec1d9 8=head1 NAME
9
10Catalyst::Engine::CGI - The CGI Engine
11
12=head1 SYNOPSIS
13
23f9d934 14A script using the Catalyst::Engine::CGI module might look like:
15
9a33da6a 16 #!/usr/bin/perl -w
17
18 use strict;
19 use lib '/path/to/MyApp/lib';
20 use MyApp;
21
22 MyApp->run;
23
23f9d934 24The application module (C<MyApp>) would use C<Catalyst>, which loads the
25appropriate engine module.
fc7ec1d9 26
27=head1 DESCRIPTION
28
fbcc39ad 29This is the Catalyst engine specialized for the CGI environment.
e2fd5b5f 30
23f9d934 31=head1 OVERLOADED METHODS
fc7ec1d9 32
fbcc39ad 33This class overloads some methods from C<Catalyst::Engine>.
fc7ec1d9 34
b5ecfcf0 35=head2 $self->finalize_headers($c)
fc7ec1d9 36
37=cut
38
fbcc39ad 39sub finalize_headers {
40 my ( $self, $c ) = @_;
06e1b616 41
fbcc39ad 42 $c->response->header( Status => $c->response->status );
06e1b616 43
02570318 44 $self->_header_buf($c->response->headers->as_string("\015\012") . "\015\012");
fc7ec1d9 45}
46
b5ecfcf0 47=head2 $self->prepare_connection($c)
fc7ec1d9 48
49=cut
50
fbcc39ad 51sub prepare_connection {
52 my ( $self, $c ) = @_;
b5ecfcf0 53 local (*ENV) = $self->env || \%ENV;
4f5ebacd 54
7fa2c9c1 55 my $request = $c->request;
56 $request->address( $ENV{REMOTE_ADDR} );
4f5ebacd 57
58 PROXY_CHECK:
fbcc39ad 59 {
60 unless ( $c->config->{using_frontend_proxy} ) {
61 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
62 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
5b387dfc 63 }
fbcc39ad 64 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
4f5ebacd 65
fbcc39ad 66 # If we are running as a backend server, the user will always appear
67 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
68 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
7fa2c9c1 69 $request->address($ip);
64d1c3cd 70 if ( defined $ENV{HTTP_X_FORWARDED_PORT} ) {
71 $ENV{SERVER_PORT} = $ENV{HTTP_X_FORWARDED_PORT};
72 }
fc7ec1d9 73 }
08cf3dd6 74
8fc0d39e 75 $request->hostname( $ENV{REMOTE_HOST} ) if exists $ENV{REMOTE_HOST};
7fa2c9c1 76 $request->protocol( $ENV{SERVER_PROTOCOL} );
8026359e 77 $request->user( $ENV{REMOTE_USER} ); # XXX: Deprecated. See Catalyst::Request for removal information
78 $request->remote_user( $ENV{REMOTE_USER} );
7fa2c9c1 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;
64d1c3cd 140 if ( $ENV{HTTP_X_FORWARDED_PORT} ) {
141 $port = $ENV{HTTP_X_FORWARDED_PORT};
142 }
316bf0f0 143 }
144
8d3c800b 145 # set the request URI
e701c5c6 146 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
fbcc39ad 147 $path =~ s{^/+}{};
b0ad47c1 148
933ba403 149 # Using URI directly is way too slow, so we construct the URLs manually
150 my $uri_class = "URI::$scheme";
b0ad47c1 151
de19de2e 152 # HTTP_HOST will include the port even if it's 80/443
153 $host =~ s/:(?:80|443)$//;
b0ad47c1 154
de19de2e 155 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
933ba403 156 $host .= ":$port";
157 }
b0ad47c1 158
933ba403 159 # Escape the path
160 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
161 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
b0ad47c1 162
933ba403 163 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
164 my $uri = $scheme . '://' . $host . '/' . $path . $query;
165
166 $c->request->uri( bless \$uri, $uri_class );
167
8d3c800b 168 # set the base URI
169 # base must end in a slash
170 $base_path .= '/' unless $base_path =~ m{/$};
b0ad47c1 171
8d3c800b 172 my $base_uri = $scheme . '://' . $host . $base_path;
173
174 $c->request->base( bless \$base_uri, $uri_class );
e7c0c583 175}
fc7ec1d9 176
b5ecfcf0 177=head2 $self->prepare_query_parameters($c)
fc7ec1d9 178
179=cut
180
4090e3bb 181around prepare_query_parameters => sub {
182 my $orig = shift;
fbcc39ad 183 my ( $self, $c ) = @_;
b5ecfcf0 184 local (*ENV) = $self->env || \%ENV;
185
f8109766 186 if ( $ENV{QUERY_STRING} ) {
4090e3bb 187 $self->$orig( $c, $ENV{QUERY_STRING} );
f8109766 188 }
4090e3bb 189};
e7c0c583 190
b5ecfcf0 191=head2 $self->prepare_request($c, (env => \%env))
84528885 192
193=cut
194
195sub prepare_request {
196 my ( $self, $c, %args ) = @_;
197
198 if ( $args{env} ) {
b5ecfcf0 199 $self->env( $args{env} );
84528885 200 }
201}
202
b5ecfcf0 203=head2 $self->prepare_write($c)
bfde09a2 204
fbcc39ad 205Enable autoflush on the output handle for CGI-based engines.
bfde09a2 206
fbcc39ad 207=cut
e7c0c583 208
4090e3bb 209around prepare_write => sub {
4f5ebacd 210 *STDOUT->autoflush(1);
4090e3bb 211 return shift->(@_);
212};
e7c0c583 213
e512dd24 214=head2 $self->write($c, $buffer)
215
216Writes the buffer to the client.
217
218=cut
219
4090e3bb 220around write => sub {
221 my $orig = shift;
e512dd24 222 my ( $self, $c, $buffer ) = @_;
223
224 # Prepend the headers if they have not yet been sent
02570318 225 if ( $self->_has_header_buf ) {
226 $buffer = $self->_clear_header_buf . $buffer;
e512dd24 227 }
7fa2c9c1 228
4090e3bb 229 return $self->$orig( $c, $buffer );
230};
e512dd24 231
b5ecfcf0 232=head2 $self->read_chunk($c, $buffer, $length)
e7c0c583 233
fbcc39ad 234=cut
e7c0c583 235
4f5ebacd 236sub read_chunk { shift; shift; *STDIN->sysread(@_); }
e7c0c583 237
b5ecfcf0 238=head2 $self->run
bfde09a2 239
fbcc39ad 240=cut
bfde09a2 241
0c913601 242sub run { shift; shift->handle_request( env => \%ENV ) }
fc7ec1d9 243
fc7ec1d9 244=head1 SEE ALSO
245
2f381252 246L<Catalyst>, L<Catalyst::Engine>
fbcc39ad 247
248=head1 AUTHORS
249
2f381252 250Catalyst Contributors, see Catalyst.pm
fc7ec1d9 251
252=head1 COPYRIGHT
253
536bee89 254This library is free software. You can redistribute it and/or modify it under
fc7ec1d9 255the same terms as Perl itself.
256
257=cut
4090e3bb 258no Moose;
fc7ec1d9 259
2601;