Small tweaks to the socket code for IE crasher
[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;
6use URI;
e2fd5b5f 7
b5ecfcf0 8__PACKAGE__->mk_accessors('env');
84528885 9
fc7ec1d9 10=head1 NAME
11
12Catalyst::Engine::CGI - The CGI Engine
13
14=head1 SYNOPSIS
15
23f9d934 16A script using the Catalyst::Engine::CGI module might look like:
17
9a33da6a 18 #!/usr/bin/perl -w
19
20 use strict;
21 use lib '/path/to/MyApp/lib';
22 use MyApp;
23
24 MyApp->run;
25
23f9d934 26The application module (C<MyApp>) would use C<Catalyst>, which loads the
27appropriate engine module.
fc7ec1d9 28
29=head1 DESCRIPTION
30
fbcc39ad 31This is the Catalyst engine specialized for the CGI environment.
e2fd5b5f 32
23f9d934 33=head1 OVERLOADED METHODS
fc7ec1d9 34
fbcc39ad 35This class overloads some methods from C<Catalyst::Engine>.
fc7ec1d9 36
b5ecfcf0 37=head2 $self->finalize_headers($c)
fc7ec1d9 38
39=cut
40
fbcc39ad 41sub finalize_headers {
42 my ( $self, $c ) = @_;
06e1b616 43
fbcc39ad 44 $c->response->header( Status => $c->response->status );
4e939e53 45
46 return unless *STDOUT->opened();
06e1b616 47
fbcc39ad 48 print $c->response->headers->as_string("\015\012");
49 print "\015\012";
fc7ec1d9 50}
51
b5ecfcf0 52=head2 $self->prepare_connection($c)
fc7ec1d9 53
54=cut
55
fbcc39ad 56sub prepare_connection {
57 my ( $self, $c ) = @_;
b5ecfcf0 58 local (*ENV) = $self->env || \%ENV;
4f5ebacd 59
fbcc39ad 60 $c->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]+)$/;
4f5ebacd 73 $c->request->address($ip);
fc7ec1d9 74 }
08cf3dd6 75
fbcc39ad 76 $c->request->hostname( $ENV{REMOTE_HOST} );
77 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
78 $c->request->user( $ENV{REMOTE_USER} );
79 $c->request->method( $ENV{REQUEST_METHOD} );
80
81 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
82 $c->request->secure(1);
5b387dfc 83 }
bfde09a2 84
fbcc39ad 85 if ( $ENV{SERVER_PORT} == 443 ) {
86 $c->request->secure(1);
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;
316bf0f0 97
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?_//;
c82ed742 102 $c->req->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
e701c5c6 142 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
fbcc39ad 143 $path =~ s{^/+}{};
4f5ebacd 144
fbcc39ad 145 my $uri = URI->new;
4f5ebacd 146 $uri->scheme($scheme);
147 $uri->host($host);
148 $uri->port($port);
149 $uri->path($path);
fbcc39ad 150 $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
4f5ebacd 151
fbcc39ad 152 # sanitize the URI
153 $uri = $uri->canonical;
4f5ebacd 154 $c->request->uri($uri);
fbcc39ad 155
156 # set the base URI
157 # base must end in a slash
4f5ebacd 158 $base_path .= '/' unless ( $base_path =~ /\/$/ );
fbcc39ad 159 my $base = $uri->clone;
4f5ebacd 160 $base->path_query($base_path);
161 $c->request->base($base);
e7c0c583 162}
fc7ec1d9 163
b5ecfcf0 164=head2 $self->prepare_query_parameters($c)
fc7ec1d9 165
166=cut
167
fbcc39ad 168sub prepare_query_parameters {
169 my ( $self, $c ) = @_;
b5ecfcf0 170 local (*ENV) = $self->env || \%ENV;
171
f8109766 172 if ( $ENV{QUERY_STRING} ) {
173 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
174 }
fbcc39ad 175}
e7c0c583 176
b5ecfcf0 177=head2 $self->prepare_request($c, (env => \%env))
84528885 178
179=cut
180
181sub prepare_request {
182 my ( $self, $c, %args ) = @_;
183
184 if ( $args{env} ) {
b5ecfcf0 185 $self->env( $args{env} );
84528885 186 }
187}
188
b5ecfcf0 189=head2 $self->prepare_write($c)
bfde09a2 190
fbcc39ad 191Enable autoflush on the output handle for CGI-based engines.
bfde09a2 192
fbcc39ad 193=cut
e7c0c583 194
fbcc39ad 195sub prepare_write {
196 my ( $self, $c ) = @_;
4f5ebacd 197
fbcc39ad 198 # Set the output handle to autoflush
4f5ebacd 199 *STDOUT->autoflush(1);
200
201 $self->NEXT::prepare_write($c);
fbcc39ad 202}
e7c0c583 203
b5ecfcf0 204=head2 $self->read_chunk($c, $buffer, $length)
e7c0c583 205
fbcc39ad 206=cut
e7c0c583 207
4f5ebacd 208sub read_chunk { shift; shift; *STDIN->sysread(@_); }
e7c0c583 209
b5ecfcf0 210=head2 $self->run
bfde09a2 211
fbcc39ad 212=cut
bfde09a2 213
fbcc39ad 214sub run { shift; shift->handle_request(@_) }
fc7ec1d9 215
fc7ec1d9 216=head1 SEE ALSO
217
fbcc39ad 218L<Catalyst> L<Catalyst::Engine>.
219
220=head1 AUTHORS
221
222Sebastian Riedel, <sri@cpan.org>
fc7ec1d9 223
fbcc39ad 224Christian Hansen, <ch@ngmedia.com>
fc7ec1d9 225
fbcc39ad 226Andy Grundman, <andy@hybridized.org>
fc7ec1d9 227
228=head1 COPYRIGHT
229
230This program is free software, you can redistribute it and/or modify it under
231the same terms as Perl itself.
232
233=cut
234
2351;