Dropped URI::Query, put back old query code, moved query param code into Engine
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
1 package Catalyst::Engine::CGI;
2
3 use strict;
4 use base 'Catalyst::Engine';
5 use NEXT;
6 use URI;
7
8 =head1 NAME
9
10 Catalyst::Engine::CGI - The CGI Engine
11
12 =head1 SYNOPSIS
13
14 A script using the Catalyst::Engine::CGI module might look like:
15
16     #!/usr/bin/perl -w
17
18     use strict;
19     use lib '/path/to/MyApp/lib';
20     use MyApp;
21
22     MyApp->run;
23
24 The application module (C<MyApp>) would use C<Catalyst>, which loads the
25 appropriate engine module.
26
27 =head1 DESCRIPTION
28
29 This is the Catalyst engine specialized for the CGI environment.
30
31 =head1 OVERLOADED METHODS
32
33 This class overloads some methods from C<Catalyst::Engine>.
34
35 =over 4
36
37 =item $self->finalize_headers($c)
38
39 =cut
40
41 sub finalize_headers {
42     my ( $self, $c ) = @_;
43
44     $c->response->header( Status => $c->response->status );
45
46     print $c->response->headers->as_string("\015\012");
47     print "\015\012";
48 }
49
50 =item $self->prepare_connection($c)
51
52 =cut
53
54 sub prepare_connection {
55     my ( $self, $c ) = @_;
56
57     $c->request->address( $ENV{REMOTE_ADDR} );
58
59   PROXY_CHECK:
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};
64         }
65         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
66
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]+)$/;
70         $c->request->address($ip);
71     }
72
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);
80     }
81
82     if ( $ENV{SERVER_PORT} == 443 ) {
83         $c->request->secure(1);
84     }
85 }
86
87 =item $self->prepare_headers($c)
88
89 =cut
90
91 sub prepare_headers {
92     my ( $self, $c ) = @_;
93
94     # Read headers from %ENV
95     while ( my ( $header, $value ) = each %ENV ) {
96         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
97         ( my $field = $header ) =~ s/^HTTPS?_//;
98         $c->req->headers->header( $field => $value );
99     }
100 }
101
102 =item $self->prepare_path($c)
103
104 =cut
105
106 sub prepare_path {
107     my ( $self, $c ) = @_;
108
109     my $scheme = $c->request->secure ? 'https' : 'http';
110     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
111     my $port      = $ENV{SERVER_PORT} || 80;
112     my $base_path = $ENV{SCRIPT_NAME} || '/';
113
114     # If we are running as a backend proxy, get the true hostname
115   PROXY_CHECK:
116     {
117         unless ( $c->config->{using_frontend_proxy} ) {
118             last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
119             last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
120         }
121         last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
122
123         $host = $ENV{HTTP_X_FORWARDED_HOST};
124
125         # backend could be on any port, so
126         # assume frontend is on the default port
127         $port = $c->request->secure ? 443 : 80;
128     }
129
130     my $path = $base_path . $ENV{PATH_INFO};
131     $path =~ s{^/+}{};
132
133     my $uri = URI->new;
134     $uri->scheme($scheme);
135     $uri->host($host);
136     $uri->port($port);
137     $uri->path($path);
138     $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING};
139
140     # sanitize the URI
141     $uri = $uri->canonical;
142     $c->request->uri($uri);
143
144     # set the base URI
145     # base must end in a slash
146     $base_path .= '/' unless ( $base_path =~ /\/$/ );
147     my $base = $uri->clone;
148     $base->path_query($base_path);
149     $c->request->base($base);
150 }
151
152 =item $self->prepare_query_parameters($c)
153
154 =cut
155
156 sub prepare_query_parameters {
157     my ( $self, $c ) = @_;
158     
159     $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
160 }
161
162 =item $self->prepare_write($c)
163
164 Enable autoflush on the output handle for CGI-based engines.
165
166 =cut
167
168 sub prepare_write {
169     my ( $self, $c ) = @_;
170
171     # Set the output handle to autoflush
172     *STDOUT->autoflush(1);
173
174     $self->NEXT::prepare_write($c);
175 }
176
177 =item $self->read_chunk($c, $buffer, $length)
178
179 =cut
180
181 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
182
183 =item $self->run
184
185 =cut
186
187 sub run { shift; shift->handle_request(@_) }
188
189 =back
190
191 =head1 SEE ALSO
192
193 L<Catalyst> L<Catalyst::Engine>.
194
195 =head1 AUTHORS
196
197 Sebastian Riedel, <sri@cpan.org>
198
199 Christian Hansen, <ch@ngmedia.com>
200
201 Andy Grundman, <andy@hybridized.org>
202
203 =head1 COPYRIGHT
204
205 This program is free software, you can redistribute it and/or modify it under
206 the same terms as Perl itself.
207
208 =cut
209
210 1;