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