authors cleanup
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
CommitLineData
fc7ec1d9 1package Catalyst::Engine::CGI;
2
3use strict;
fbcc39ad 4use base 'Catalyst::Engine';
5use NEXT;
e2fd5b5f 6
b5ecfcf0 7__PACKAGE__->mk_accessors('env');
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
e512dd24 45 $self->{_header_buf}
46 = $c->response->headers->as_string("\015\012") . "\015\012";
fc7ec1d9 47}
48
b5ecfcf0 49=head2 $self->prepare_connection($c)
fc7ec1d9 50
51=cut
52
fbcc39ad 53sub prepare_connection {
54 my ( $self, $c ) = @_;
b5ecfcf0 55 local (*ENV) = $self->env || \%ENV;
4f5ebacd 56
fbcc39ad 57 $c->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]+)$/;
4f5ebacd 70 $c->request->address($ip);
fc7ec1d9 71 }
08cf3dd6 72
fbcc39ad 73 $c->request->hostname( $ENV{REMOTE_HOST} );
74 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
75 $c->request->user( $ENV{REMOTE_USER} );
76 $c->request->method( $ENV{REQUEST_METHOD} );
77
78 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
79 $c->request->secure(1);
5b387dfc 80 }
bfde09a2 81
fbcc39ad 82 if ( $ENV{SERVER_PORT} == 443 ) {
83 $c->request->secure(1);
84 }
fc7ec1d9 85}
86
b5ecfcf0 87=head2 $self->prepare_headers($c)
fc7ec1d9 88
89=cut
90
fbcc39ad 91sub prepare_headers {
92 my ( $self, $c ) = @_;
b5ecfcf0 93 local (*ENV) = $self->env || \%ENV;
316bf0f0 94
fbcc39ad 95 # Read headers from %ENV
c82ed742 96 foreach my $header ( keys %ENV ) {
fbcc39ad 97 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
98 ( my $field = $header ) =~ s/^HTTPS?_//;
c82ed742 99 $c->req->headers->header( $field => $ENV{$header} );
fbcc39ad 100 }
101}
316bf0f0 102
b5ecfcf0 103=head2 $self->prepare_path($c)
316bf0f0 104
fbcc39ad 105=cut
316bf0f0 106
fbcc39ad 107sub prepare_path {
108 my ( $self, $c ) = @_;
b5ecfcf0 109 local (*ENV) = $self->env || \%ENV;
fbcc39ad 110
4f5ebacd 111 my $scheme = $c->request->secure ? 'https' : 'http';
294f78ca 112 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
113 my $port = $ENV{SERVER_PORT} || 80;
0bcb98c7 114 my $base_path;
115 if ( exists $ENV{REDIRECT_URL} ) {
116 $base_path = $ENV{REDIRECT_URL};
117 $base_path =~ s/$ENV{PATH_INFO}$//;
118 }
119 else {
120 $base_path = $ENV{SCRIPT_NAME} || '/';
121 }
4f5ebacd 122
fbcc39ad 123 # If we are running as a backend proxy, get the true hostname
4f5ebacd 124 PROXY_CHECK:
fbcc39ad 125 {
126 unless ( $c->config->{using_frontend_proxy} ) {
127 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
128 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
316bf0f0 129 }
fbcc39ad 130 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
316bf0f0 131
fbcc39ad 132 $host = $ENV{HTTP_X_FORWARDED_HOST};
4f5ebacd 133
134 # backend could be on any port, so
fbcc39ad 135 # assume frontend is on the default port
136 $port = $c->request->secure ? 443 : 80;
316bf0f0 137 }
138
8d3c800b 139 # set the request URI
e701c5c6 140 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
fbcc39ad 141 $path =~ s{^/+}{};
933ba403 142
143 # Using URI directly is way too slow, so we construct the URLs manually
144 my $uri_class = "URI::$scheme";
145
de19de2e 146 # HTTP_HOST will include the port even if it's 80/443
147 $host =~ s/:(?:80|443)$//;
933ba403 148
de19de2e 149 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
933ba403 150 $host .= ":$port";
151 }
152
153 # Escape the path
154 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
155 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
156
157 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
158 my $uri = $scheme . '://' . $host . '/' . $path . $query;
159
160 $c->request->uri( bless \$uri, $uri_class );
161
8d3c800b 162 # set the base URI
163 # base must end in a slash
164 $base_path .= '/' unless $base_path =~ m{/$};
165
166 my $base_uri = $scheme . '://' . $host . $base_path;
167
168 $c->request->base( bless \$base_uri, $uri_class );
e7c0c583 169}
fc7ec1d9 170
b5ecfcf0 171=head2 $self->prepare_query_parameters($c)
fc7ec1d9 172
173=cut
174
fbcc39ad 175sub prepare_query_parameters {
176 my ( $self, $c ) = @_;
b5ecfcf0 177 local (*ENV) = $self->env || \%ENV;
178
f8109766 179 if ( $ENV{QUERY_STRING} ) {
180 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
181 }
fbcc39ad 182}
e7c0c583 183
b5ecfcf0 184=head2 $self->prepare_request($c, (env => \%env))
84528885 185
186=cut
187
188sub prepare_request {
189 my ( $self, $c, %args ) = @_;
190
191 if ( $args{env} ) {
b5ecfcf0 192 $self->env( $args{env} );
84528885 193 }
194}
195
b5ecfcf0 196=head2 $self->prepare_write($c)
bfde09a2 197
fbcc39ad 198Enable autoflush on the output handle for CGI-based engines.
bfde09a2 199
fbcc39ad 200=cut
e7c0c583 201
fbcc39ad 202sub prepare_write {
203 my ( $self, $c ) = @_;
4f5ebacd 204
fbcc39ad 205 # Set the output handle to autoflush
4f5ebacd 206 *STDOUT->autoflush(1);
207
208 $self->NEXT::prepare_write($c);
fbcc39ad 209}
e7c0c583 210
e512dd24 211=head2 $self->write($c, $buffer)
212
213Writes the buffer to the client.
214
215=cut
216
217sub write {
218 my ( $self, $c, $buffer ) = @_;
219
220 # Prepend the headers if they have not yet been sent
221 if ( my $headers = delete $self->{_header_buf} ) {
222 $buffer = $headers . $buffer;
223 }
224
225 return $self->NEXT::write( $c, $buffer );
226}
227
b5ecfcf0 228=head2 $self->read_chunk($c, $buffer, $length)
e7c0c583 229
fbcc39ad 230=cut
e7c0c583 231
4f5ebacd 232sub read_chunk { shift; shift; *STDIN->sysread(@_); }
e7c0c583 233
b5ecfcf0 234=head2 $self->run
bfde09a2 235
fbcc39ad 236=cut
bfde09a2 237
fbcc39ad 238sub run { shift; shift->handle_request(@_) }
fc7ec1d9 239
fc7ec1d9 240=head1 SEE ALSO
241
0bf7ab71 242L<Catalyst>, L<Catalyst::Engine>
fbcc39ad 243
244=head1 AUTHORS
245
0bf7ab71 246Catalyst Contributors, see Catalyst.pm
fc7ec1d9 247
248=head1 COPYRIGHT
249
250This program is free software, you can redistribute it and/or modify it under
251the same terms as Perl itself.
252
253=cut
254
2551;