Removed req->handle and res->handle
[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;
7use URI::Query;
e2fd5b5f 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
23f9d934 36=over 4
37
fbcc39ad 38=item $self->finalize_headers($c)
fc7ec1d9 39
40=cut
41
fbcc39ad 42sub finalize_headers {
43 my ( $self, $c ) = @_;
06e1b616 44
fbcc39ad 45 $c->response->header( Status => $c->response->status );
06e1b616 46
fbcc39ad 47 print $c->response->headers->as_string("\015\012");
48 print "\015\012";
fc7ec1d9 49}
50
fbcc39ad 51=item $self->prepare_connection($c)
fc7ec1d9 52
53=cut
54
fbcc39ad 55sub prepare_connection {
56 my ( $self, $c ) = @_;
4f5ebacd 57
fbcc39ad 58 $c->request->address( $ENV{REMOTE_ADDR} );
4f5ebacd 59
60 PROXY_CHECK:
fbcc39ad 61 {
62 unless ( $c->config->{using_frontend_proxy} ) {
63 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
64 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
5b387dfc 65 }
fbcc39ad 66 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
4f5ebacd 67
fbcc39ad 68 # If we are running as a backend server, the user will always appear
69 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
70 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
4f5ebacd 71 $c->request->address($ip);
fc7ec1d9 72 }
08cf3dd6 73
fbcc39ad 74 $c->request->hostname( $ENV{REMOTE_HOST} );
75 $c->request->protocol( $ENV{SERVER_PROTOCOL} );
76 $c->request->user( $ENV{REMOTE_USER} );
77 $c->request->method( $ENV{REQUEST_METHOD} );
78
79 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
80 $c->request->secure(1);
5b387dfc 81 }
bfde09a2 82
fbcc39ad 83 if ( $ENV{SERVER_PORT} == 443 ) {
84 $c->request->secure(1);
85 }
fc7ec1d9 86}
87
fbcc39ad 88=item $self->prepare_headers($c)
fc7ec1d9 89
90=cut
91
fbcc39ad 92sub prepare_headers {
93 my ( $self, $c ) = @_;
316bf0f0 94
fbcc39ad 95 # Read headers from %ENV
96 while ( my ( $header, $value ) = each %ENV ) {
97 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
98 ( my $field = $header ) =~ s/^HTTPS?_//;
99 $c->req->headers->header( $field => $value );
100 }
101}
316bf0f0 102
fbcc39ad 103=item $self->prepare_path($c)
316bf0f0 104
fbcc39ad 105=cut
316bf0f0 106
fbcc39ad 107sub prepare_path {
108 my ( $self, $c ) = @_;
109
4f5ebacd 110 my $scheme = $c->request->secure ? 'https' : 'http';
fbcc39ad 111 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
112 my $port = $ENV{SERVER_PORT} || 80;
113 my $base_path = $ENV{SCRIPT_NAME} || '/';
4f5ebacd 114
fbcc39ad 115 # If we are running as a backend proxy, get the true hostname
4f5ebacd 116 PROXY_CHECK:
fbcc39ad 117 {
118 unless ( $c->config->{using_frontend_proxy} ) {
119 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
120 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
316bf0f0 121 }
fbcc39ad 122 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
316bf0f0 123
fbcc39ad 124 $host = $ENV{HTTP_X_FORWARDED_HOST};
4f5ebacd 125
126 # backend could be on any port, so
fbcc39ad 127 # assume frontend is on the default port
128 $port = $c->request->secure ? 443 : 80;
316bf0f0 129 }
130
fbcc39ad 131 my $path = $base_path . $ENV{PATH_INFO};
132 $path =~ s{^/+}{};
4f5ebacd 133
fbcc39ad 134 my $uri = URI->new;
4f5ebacd 135 $uri->scheme($scheme);
136 $uri->host($host);
137 $uri->port($port);
138 $uri->path($path);
fbcc39ad 139 $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
4f5ebacd 140
fbcc39ad 141 # sanitize the URI
142 $uri = $uri->canonical;
4f5ebacd 143 $c->request->uri($uri);
fbcc39ad 144
145 # set the base URI
146 # base must end in a slash
4f5ebacd 147 $base_path .= '/' unless ( $base_path =~ /\/$/ );
fbcc39ad 148 my $base = $uri->clone;
4f5ebacd 149 $base->path_query($base_path);
150 $c->request->base($base);
e7c0c583 151}
fc7ec1d9 152
fbcc39ad 153=item $self->prepare_query_parameters($c)
fc7ec1d9 154
155=cut
156
fbcc39ad 157sub prepare_query_parameters {
158 my ( $self, $c ) = @_;
4f5ebacd 159
fbcc39ad 160 my $u = URI::Query->new( $ENV{QUERY_STRING} );
161 $c->request->query_parameters( { $u->hash } );
162}
e7c0c583 163
fbcc39ad 164=item $self->prepare_write($c)
bfde09a2 165
fbcc39ad 166Enable autoflush on the output handle for CGI-based engines.
bfde09a2 167
fbcc39ad 168=cut
e7c0c583 169
fbcc39ad 170sub prepare_write {
171 my ( $self, $c ) = @_;
4f5ebacd 172
fbcc39ad 173 # Set the output handle to autoflush
4f5ebacd 174 *STDOUT->autoflush(1);
175
176 $self->NEXT::prepare_write($c);
fbcc39ad 177}
e7c0c583 178
fbcc39ad 179=item $self->read_chunk($c, $buffer, $length)
e7c0c583 180
fbcc39ad 181=cut
e7c0c583 182
4f5ebacd 183sub read_chunk { shift; shift; *STDIN->sysread(@_); }
e7c0c583 184
fbcc39ad 185=item $self->run
bfde09a2 186
fbcc39ad 187=cut
bfde09a2 188
fbcc39ad 189sub run { shift; shift->handle_request(@_) }
fc7ec1d9 190
23f9d934 191=back
192
fc7ec1d9 193=head1 SEE ALSO
194
fbcc39ad 195L<Catalyst> L<Catalyst::Engine>.
196
197=head1 AUTHORS
198
199Sebastian Riedel, <sri@cpan.org>
fc7ec1d9 200
fbcc39ad 201Christian Hansen, <ch@ngmedia.com>
fc7ec1d9 202
fbcc39ad 203Andy Grundman, <andy@hybridized.org>
fc7ec1d9 204
205=head1 COPYRIGHT
206
207This program is free software, you can redistribute it and/or modify it under
208the same terms as Perl itself.
209
210=cut
211
2121;