Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Engine::CGI; |
2 | |
3 | use strict; |
fbcc39ad |
4 | use base 'Catalyst::Engine'; |
5 | use NEXT; |
e2fd5b5f |
6 | |
b5ecfcf0 |
7 | __PACKAGE__->mk_accessors('env'); |
84528885 |
8 | |
fc7ec1d9 |
9 | =head1 NAME |
10 | |
11 | Catalyst::Engine::CGI - The CGI Engine |
12 | |
13 | =head1 SYNOPSIS |
14 | |
23f9d934 |
15 | A 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 |
25 | The application module (C<MyApp>) would use C<Catalyst>, which loads the |
26 | appropriate engine module. |
fc7ec1d9 |
27 | |
28 | =head1 DESCRIPTION |
29 | |
fbcc39ad |
30 | This is the Catalyst engine specialized for the CGI environment. |
e2fd5b5f |
31 | |
23f9d934 |
32 | =head1 OVERLOADED METHODS |
fc7ec1d9 |
33 | |
fbcc39ad |
34 | This class overloads some methods from C<Catalyst::Engine>. |
fc7ec1d9 |
35 | |
b5ecfcf0 |
36 | =head2 $self->finalize_headers($c) |
fc7ec1d9 |
37 | |
38 | =cut |
39 | |
fbcc39ad |
40 | sub finalize_headers { |
41 | my ( $self, $c ) = @_; |
06e1b616 |
42 | |
fbcc39ad |
43 | $c->response->header( Status => $c->response->status ); |
06e1b616 |
44 | |
4bb8bd62 |
45 | print $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 |
52 | sub prepare_connection { |
53 | my ( $self, $c ) = @_; |
b5ecfcf0 |
54 | local (*ENV) = $self->env || \%ENV; |
4f5ebacd |
55 | |
fbcc39ad |
56 | $c->request->address( $ENV{REMOTE_ADDR} ); |
4f5ebacd |
57 | |
58 | PROXY_CHECK: |
fbcc39ad |
59 | { |
60 | unless ( $c->config->{using_frontend_proxy} ) { |
61 | last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1'; |
62 | last PROXY_CHECK if $c->config->{ignore_frontend_proxy}; |
5b387dfc |
63 | } |
fbcc39ad |
64 | last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR}; |
4f5ebacd |
65 | |
fbcc39ad |
66 | # If we are running as a backend server, the user will always appear |
67 | # as 127.0.0.1. Select the most recent upstream IP (last in the list) |
68 | my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/; |
4f5ebacd |
69 | $c->request->address($ip); |
fc7ec1d9 |
70 | } |
08cf3dd6 |
71 | |
fbcc39ad |
72 | $c->request->hostname( $ENV{REMOTE_HOST} ); |
73 | $c->request->protocol( $ENV{SERVER_PROTOCOL} ); |
74 | $c->request->user( $ENV{REMOTE_USER} ); |
75 | $c->request->method( $ENV{REQUEST_METHOD} ); |
76 | |
77 | if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) { |
78 | $c->request->secure(1); |
5b387dfc |
79 | } |
bfde09a2 |
80 | |
fbcc39ad |
81 | if ( $ENV{SERVER_PORT} == 443 ) { |
82 | $c->request->secure(1); |
83 | } |
fc7ec1d9 |
84 | } |
85 | |
b5ecfcf0 |
86 | =head2 $self->prepare_headers($c) |
fc7ec1d9 |
87 | |
88 | =cut |
89 | |
fbcc39ad |
90 | sub prepare_headers { |
91 | my ( $self, $c ) = @_; |
b5ecfcf0 |
92 | local (*ENV) = $self->env || \%ENV; |
316bf0f0 |
93 | |
fbcc39ad |
94 | # Read headers from %ENV |
c82ed742 |
95 | foreach my $header ( keys %ENV ) { |
fbcc39ad |
96 | next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i; |
97 | ( my $field = $header ) =~ s/^HTTPS?_//; |
c82ed742 |
98 | $c->req->headers->header( $field => $ENV{$header} ); |
fbcc39ad |
99 | } |
100 | } |
316bf0f0 |
101 | |
b5ecfcf0 |
102 | =head2 $self->prepare_path($c) |
316bf0f0 |
103 | |
fbcc39ad |
104 | =cut |
316bf0f0 |
105 | |
fbcc39ad |
106 | sub prepare_path { |
107 | my ( $self, $c ) = @_; |
b5ecfcf0 |
108 | local (*ENV) = $self->env || \%ENV; |
fbcc39ad |
109 | |
4f5ebacd |
110 | my $scheme = $c->request->secure ? 'https' : 'http'; |
294f78ca |
111 | my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; |
112 | my $port = $ENV{SERVER_PORT} || 80; |
0bcb98c7 |
113 | my $base_path; |
114 | if ( exists $ENV{REDIRECT_URL} ) { |
115 | $base_path = $ENV{REDIRECT_URL}; |
116 | $base_path =~ s/$ENV{PATH_INFO}$//; |
117 | } |
118 | else { |
119 | $base_path = $ENV{SCRIPT_NAME} || '/'; |
120 | } |
4f5ebacd |
121 | |
fbcc39ad |
122 | # If we are running as a backend proxy, get the true hostname |
4f5ebacd |
123 | PROXY_CHECK: |
fbcc39ad |
124 | { |
125 | unless ( $c->config->{using_frontend_proxy} ) { |
126 | last PROXY_CHECK if $host !~ /localhost|127.0.0.1/; |
127 | last PROXY_CHECK if $c->config->{ignore_frontend_proxy}; |
316bf0f0 |
128 | } |
fbcc39ad |
129 | last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST}; |
316bf0f0 |
130 | |
fbcc39ad |
131 | $host = $ENV{HTTP_X_FORWARDED_HOST}; |
4f5ebacd |
132 | |
133 | # backend could be on any port, so |
fbcc39ad |
134 | # assume frontend is on the default port |
135 | $port = $c->request->secure ? 443 : 80; |
316bf0f0 |
136 | } |
137 | |
8d3c800b |
138 | # set the request URI |
e701c5c6 |
139 | my $path = $base_path . ( $ENV{PATH_INFO} || '' ); |
fbcc39ad |
140 | $path =~ s{^/+}{}; |
933ba403 |
141 | |
142 | # Using URI directly is way too slow, so we construct the URLs manually |
143 | my $uri_class = "URI::$scheme"; |
144 | |
145 | # HTTP_HOST will include the port even if it's 80 |
146 | $host =~ s/:80$//; |
147 | |
148 | if ( $port != 80 && $host !~ /:/ ) { |
149 | $host .= ":$port"; |
150 | } |
151 | |
152 | # Escape the path |
153 | $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go; |
154 | $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE |
155 | |
156 | my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : ''; |
157 | my $uri = $scheme . '://' . $host . '/' . $path . $query; |
158 | |
159 | $c->request->uri( bless \$uri, $uri_class ); |
160 | |
8d3c800b |
161 | # set the base URI |
162 | # base must end in a slash |
163 | $base_path .= '/' unless $base_path =~ m{/$}; |
164 | |
165 | my $base_uri = $scheme . '://' . $host . $base_path; |
166 | |
167 | $c->request->base( bless \$base_uri, $uri_class ); |
e7c0c583 |
168 | } |
fc7ec1d9 |
169 | |
b5ecfcf0 |
170 | =head2 $self->prepare_query_parameters($c) |
fc7ec1d9 |
171 | |
172 | =cut |
173 | |
fbcc39ad |
174 | sub prepare_query_parameters { |
175 | my ( $self, $c ) = @_; |
b5ecfcf0 |
176 | local (*ENV) = $self->env || \%ENV; |
177 | |
f8109766 |
178 | if ( $ENV{QUERY_STRING} ) { |
179 | $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} ); |
180 | } |
fbcc39ad |
181 | } |
e7c0c583 |
182 | |
b5ecfcf0 |
183 | =head2 $self->prepare_request($c, (env => \%env)) |
84528885 |
184 | |
185 | =cut |
186 | |
187 | sub prepare_request { |
188 | my ( $self, $c, %args ) = @_; |
189 | |
190 | if ( $args{env} ) { |
b5ecfcf0 |
191 | $self->env( $args{env} ); |
84528885 |
192 | } |
193 | } |
194 | |
b5ecfcf0 |
195 | =head2 $self->prepare_write($c) |
bfde09a2 |
196 | |
fbcc39ad |
197 | Enable autoflush on the output handle for CGI-based engines. |
bfde09a2 |
198 | |
fbcc39ad |
199 | =cut |
e7c0c583 |
200 | |
fbcc39ad |
201 | sub prepare_write { |
202 | my ( $self, $c ) = @_; |
4f5ebacd |
203 | |
fbcc39ad |
204 | # Set the output handle to autoflush |
4f5ebacd |
205 | *STDOUT->autoflush(1); |
206 | |
207 | $self->NEXT::prepare_write($c); |
fbcc39ad |
208 | } |
e7c0c583 |
209 | |
b5ecfcf0 |
210 | =head2 $self->read_chunk($c, $buffer, $length) |
e7c0c583 |
211 | |
fbcc39ad |
212 | =cut |
e7c0c583 |
213 | |
4f5ebacd |
214 | sub read_chunk { shift; shift; *STDIN->sysread(@_); } |
e7c0c583 |
215 | |
b5ecfcf0 |
216 | =head2 $self->run |
bfde09a2 |
217 | |
fbcc39ad |
218 | =cut |
bfde09a2 |
219 | |
fbcc39ad |
220 | sub run { shift; shift->handle_request(@_) } |
fc7ec1d9 |
221 | |
fc7ec1d9 |
222 | =head1 SEE ALSO |
223 | |
fbcc39ad |
224 | L<Catalyst> L<Catalyst::Engine>. |
225 | |
226 | =head1 AUTHORS |
227 | |
228 | Sebastian Riedel, <sri@cpan.org> |
fc7ec1d9 |
229 | |
fbcc39ad |
230 | Christian Hansen, <ch@ngmedia.com> |
fc7ec1d9 |
231 | |
fbcc39ad |
232 | Andy Grundman, <andy@hybridized.org> |
fc7ec1d9 |
233 | |
234 | =head1 COPYRIGHT |
235 | |
236 | This program is free software, you can redistribute it and/or modify it under |
237 | the same terms as Perl itself. |
238 | |
239 | =cut |
240 | |
241 | 1; |