Get it mostly working, except uri_for is still buggered
[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 {
df960201 60 unless ( ref($c)->config->{using_frontend_proxy} ) {
fbcc39ad 61 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
df960201 62 last PROXY_CHECK if ref($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 }
afdffc63 88 binmode(STDOUT); # Ensure we are sending bytes.
fc7ec1d9 89}
90
b5ecfcf0 91=head2 $self->prepare_headers($c)
fc7ec1d9 92
93=cut
94
fbcc39ad 95sub prepare_headers {
96 my ( $self, $c ) = @_;
b5ecfcf0 97 local (*ENV) = $self->env || \%ENV;
7fa2c9c1 98 my $headers = $c->request->headers;
fbcc39ad 99 # Read headers from %ENV
c82ed742 100 foreach my $header ( keys %ENV ) {
fbcc39ad 101 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
102 ( my $field = $header ) =~ s/^HTTPS?_//;
7fa2c9c1 103 $headers->header( $field => $ENV{$header} );
fbcc39ad 104 }
105}
316bf0f0 106
b5ecfcf0 107=head2 $self->prepare_path($c)
316bf0f0 108
fbcc39ad 109=cut
316bf0f0 110
eb3abf96 111# Please don't touch this method without adding tests in
112# t/aggregate/unit_core_engine_cgi-prepare_path.t
fbcc39ad 113sub prepare_path {
114 my ( $self, $c ) = @_;
b5ecfcf0 115 local (*ENV) = $self->env || \%ENV;
fbcc39ad 116
4f5ebacd 117 my $scheme = $c->request->secure ? 'https' : 'http';
294f78ca 118 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
119 my $port = $ENV{SERVER_PORT} || 80;
8bf285ed 120 my $script_name = $ENV{SCRIPT_NAME};
121 $script_name =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go if $script_name;
122
0bcb98c7 123 my $base_path;
124 if ( exists $ENV{REDIRECT_URL} ) {
125 $base_path = $ENV{REDIRECT_URL};
4dfe7bde 126 $base_path =~ s/\Q$ENV{PATH_INFO}\E$//;
0bcb98c7 127 }
128 else {
8bf285ed 129 $base_path = $script_name || '/';
0bcb98c7 130 }
4f5ebacd 131
fbcc39ad 132 # If we are running as a backend proxy, get the true hostname
4f5ebacd 133 PROXY_CHECK:
fbcc39ad 134 {
df960201 135 unless ( ref($c)->config->{using_frontend_proxy} ) {
fbcc39ad 136 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
df960201 137 last PROXY_CHECK if ref($c)->config->{ignore_frontend_proxy};
316bf0f0 138 }
fbcc39ad 139 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
316bf0f0 140
fbcc39ad 141 $host = $ENV{HTTP_X_FORWARDED_HOST};
4f5ebacd 142
143 # backend could be on any port, so
fbcc39ad 144 # assume frontend is on the default port
145 $port = $c->request->secure ? 443 : 80;
64d1c3cd 146 if ( $ENV{HTTP_X_FORWARDED_PORT} ) {
147 $port = $ENV{HTTP_X_FORWARDED_PORT};
148 }
316bf0f0 149 }
150
8bf285ed 151 # RFC 3875: "Unlike a URI path, the PATH_INFO is not URL-encoded,
152 # and cannot contain path-segment parameters." This means PATH_INFO
153 # is always decoded, and the script can't distinguish / vs %2F.
154 # See https://issues.apache.org/bugzilla/show_bug.cgi?id=35256
155 # Here we try to resurrect the original encoded URI from REQUEST_URI.
156 my $path_info = $ENV{PATH_INFO};
e8b29968 157# if (my $req_uri = $ENV{REQUEST_URI}) {
158# $req_uri =~ s/^\Q$base_path\E//;
159# $req_uri =~ s/\?.*$//;
160# if ($req_uri && $req_uri ne '/') {
b760ac3d 161 # This means that REQUEST_URI needs information from PATH_INFO
162 # prepending to it to be useful, otherwise the sub path which is
163 # being redirected to becomes the app base address which is
164 # incorrect.
e8b29968 165# my ($match) = $req_uri =~ m{^(/?[^/]+)};
166# my ($path_info_part) = $path_info =~ m|^(.*?\Q$match\E)|;
167# substr($req_uri, 0, length($match), $path_info_part)
168# if $path_info_part;
169# $path_info = $req_uri;
170# }
171# }
172 $path_info =~ s/%2F/%252F/g;
8d3c800b 173 # set the request URI
e8b29968 174 warn("Base path $base_path, path_info $path_info");
8bf285ed 175 my $path = $base_path . ( $path_info || '' );
fbcc39ad 176 $path =~ s{^/+}{};
e8b29968 177 $base_path .= '/' unless $base_path =~ m{/$};
b0ad47c1 178
933ba403 179 # Using URI directly is way too slow, so we construct the URLs manually
180 my $uri_class = "URI::$scheme";
b0ad47c1 181
de19de2e 182 # HTTP_HOST will include the port even if it's 80/443
183 $host =~ s/:(?:80|443)$//;
b0ad47c1 184
de19de2e 185 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
933ba403 186 $host .= ":$port";
187 }
b0ad47c1 188
933ba403 189 # Escape the path
190 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
191 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
b0ad47c1 192
933ba403 193 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
194 my $uri = $scheme . '://' . $host . '/' . $path . $query;
195
ca78941c 196 $c->request->uri( bless(\$uri, $uri_class)->canonical );
933ba403 197
8d3c800b 198 # set the base URI
199 # base must end in a slash
200 $base_path .= '/' unless $base_path =~ m{/$};
b0ad47c1 201
8d3c800b 202 my $base_uri = $scheme . '://' . $host . $base_path;
203
67936fd7 204 $c->request->base( bless \$base_uri, $uri_class );
e7c0c583 205}
fc7ec1d9 206
b5ecfcf0 207=head2 $self->prepare_query_parameters($c)
fc7ec1d9 208
209=cut
210
4090e3bb 211around prepare_query_parameters => sub {
212 my $orig = shift;
fbcc39ad 213 my ( $self, $c ) = @_;
b5ecfcf0 214 local (*ENV) = $self->env || \%ENV;
215
f8109766 216 if ( $ENV{QUERY_STRING} ) {
4090e3bb 217 $self->$orig( $c, $ENV{QUERY_STRING} );
f8109766 218 }
4090e3bb 219};
e7c0c583 220
b5ecfcf0 221=head2 $self->prepare_request($c, (env => \%env))
84528885 222
223=cut
224
225sub prepare_request {
226 my ( $self, $c, %args ) = @_;
227
228 if ( $args{env} ) {
b5ecfcf0 229 $self->env( $args{env} );
84528885 230 }
231}
232
b5ecfcf0 233=head2 $self->prepare_write($c)
bfde09a2 234
fbcc39ad 235Enable autoflush on the output handle for CGI-based engines.
bfde09a2 236
fbcc39ad 237=cut
e7c0c583 238
4090e3bb 239around prepare_write => sub {
4f5ebacd 240 *STDOUT->autoflush(1);
4090e3bb 241 return shift->(@_);
242};
e7c0c583 243
e512dd24 244=head2 $self->write($c, $buffer)
245
246Writes the buffer to the client.
247
248=cut
249
4090e3bb 250around write => sub {
251 my $orig = shift;
e512dd24 252 my ( $self, $c, $buffer ) = @_;
253
254 # Prepend the headers if they have not yet been sent
02570318 255 if ( $self->_has_header_buf ) {
256 $buffer = $self->_clear_header_buf . $buffer;
e512dd24 257 }
7fa2c9c1 258
4090e3bb 259 return $self->$orig( $c, $buffer );
260};
e512dd24 261
b5ecfcf0 262=head2 $self->read_chunk($c, $buffer, $length)
e7c0c583 263
fbcc39ad 264=cut
e7c0c583 265
4f5ebacd 266sub read_chunk { shift; shift; *STDIN->sysread(@_); }
e7c0c583 267
b5ecfcf0 268=head2 $self->run
bfde09a2 269
fbcc39ad 270=cut
bfde09a2 271
0c913601 272sub run { shift; shift->handle_request( env => \%ENV ) }
fc7ec1d9 273
fc7ec1d9 274=head1 SEE ALSO
275
2f381252 276L<Catalyst>, L<Catalyst::Engine>
fbcc39ad 277
278=head1 AUTHORS
279
2f381252 280Catalyst Contributors, see Catalyst.pm
fc7ec1d9 281
282=head1 COPYRIGHT
283
536bee89 284This library is free software. You can redistribute it and/or modify it under
fc7ec1d9 285the same terms as Perl itself.
286
287=cut
4090e3bb 288no Moose;
fc7ec1d9 289
2901;