Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Engine::CGI; |
2 | |
7fa2c9c1 |
3 | use Moose; |
4 | extends 'Catalyst::Engine'; |
e2fd5b5f |
5 | |
02570318 |
6 | has _header_buf => (is => 'rw', clearer => '_clear_header_buf', predicate => '_has_header_buf'); |
84528885 |
7 | |
fc7ec1d9 |
8 | =head1 NAME |
9 | |
10 | Catalyst::Engine::CGI - The CGI Engine |
11 | |
12 | =head1 SYNOPSIS |
13 | |
23f9d934 |
14 | A script using the Catalyst::Engine::CGI module might look like: |
15 | |
9a33da6a |
16 | #!/usr/bin/perl -w |
17 | |
18 | use strict; |
19 | use lib '/path/to/MyApp/lib'; |
20 | use MyApp; |
21 | |
22 | MyApp->run; |
23 | |
23f9d934 |
24 | The application module (C<MyApp>) would use C<Catalyst>, which loads the |
25 | appropriate engine module. |
fc7ec1d9 |
26 | |
27 | =head1 DESCRIPTION |
28 | |
fbcc39ad |
29 | This is the Catalyst engine specialized for the CGI environment. |
e2fd5b5f |
30 | |
23f9d934 |
31 | =head1 OVERLOADED METHODS |
fc7ec1d9 |
32 | |
fbcc39ad |
33 | This class overloads some methods from C<Catalyst::Engine>. |
fc7ec1d9 |
34 | |
b5ecfcf0 |
35 | =head2 $self->finalize_headers($c) |
fc7ec1d9 |
36 | |
37 | =cut |
38 | |
fbcc39ad |
39 | sub finalize_headers { |
40 | my ( $self, $c ) = @_; |
06e1b616 |
41 | |
fbcc39ad |
42 | $c->response->header( Status => $c->response->status ); |
06e1b616 |
43 | |
02570318 |
44 | $self->_header_buf($c->response->headers->as_string("\015\012") . "\015\012"); |
fc7ec1d9 |
45 | } |
46 | |
b5ecfcf0 |
47 | =head2 $self->prepare_connection($c) |
fc7ec1d9 |
48 | |
49 | =cut |
50 | |
fbcc39ad |
51 | sub prepare_connection { |
52 | my ( $self, $c ) = @_; |
b5ecfcf0 |
53 | local (*ENV) = $self->env || \%ENV; |
4f5ebacd |
54 | |
7fa2c9c1 |
55 | my $request = $c->request; |
56 | $request->address( $ENV{REMOTE_ADDR} ); |
4f5ebacd |
57 | |
58 | PROXY_CHECK: |
fbcc39ad |
59 | { |
df960201 |
60 | unless ( ref($c)->config->{using_frontend_proxy} ) { |
fbcc39ad |
61 | last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1'; |
df960201 |
62 | last PROXY_CHECK if ref($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]+)$/; |
7fa2c9c1 |
69 | $request->address($ip); |
64d1c3cd |
70 | if ( defined $ENV{HTTP_X_FORWARDED_PORT} ) { |
71 | $ENV{SERVER_PORT} = $ENV{HTTP_X_FORWARDED_PORT}; |
72 | } |
fc7ec1d9 |
73 | } |
08cf3dd6 |
74 | |
8fc0d39e |
75 | $request->hostname( $ENV{REMOTE_HOST} ) if exists $ENV{REMOTE_HOST}; |
7fa2c9c1 |
76 | $request->protocol( $ENV{SERVER_PROTOCOL} ); |
8026359e |
77 | $request->user( $ENV{REMOTE_USER} ); # XXX: Deprecated. See Catalyst::Request for removal information |
78 | $request->remote_user( $ENV{REMOTE_USER} ); |
7fa2c9c1 |
79 | $request->method( $ENV{REQUEST_METHOD} ); |
fbcc39ad |
80 | |
81 | if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) { |
7fa2c9c1 |
82 | $request->secure(1); |
5b387dfc |
83 | } |
bfde09a2 |
84 | |
fbcc39ad |
85 | if ( $ENV{SERVER_PORT} == 443 ) { |
7fa2c9c1 |
86 | $request->secure(1); |
fbcc39ad |
87 | } |
afdffc63 |
88 | binmode(STDOUT); # Ensure we are sending bytes. |
fc7ec1d9 |
89 | } |
90 | |
b5ecfcf0 |
91 | =head2 $self->prepare_headers($c) |
fc7ec1d9 |
92 | |
93 | =cut |
94 | |
fbcc39ad |
95 | sub prepare_headers { |
96 | my ( $self, $c ) = @_; |
b5ecfcf0 |
97 | local (*ENV) = $self->env || \%ENV; |
7fa2c9c1 |
98 | my $headers = $c->request->headers; |
fbcc39ad |
99 | # Read headers from %ENV |
c82ed742 |
100 | foreach my $header ( keys %ENV ) { |
fbcc39ad |
101 | next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i; |
102 | ( my $field = $header ) =~ s/^HTTPS?_//; |
7fa2c9c1 |
103 | $headers->header( $field => $ENV{$header} ); |
fbcc39ad |
104 | } |
105 | } |
316bf0f0 |
106 | |
b5ecfcf0 |
107 | =head2 $self->prepare_path($c) |
316bf0f0 |
108 | |
fbcc39ad |
109 | =cut |
316bf0f0 |
110 | |
fbcc39ad |
111 | sub prepare_path { |
112 | my ( $self, $c ) = @_; |
b5ecfcf0 |
113 | local (*ENV) = $self->env || \%ENV; |
fbcc39ad |
114 | |
4f5ebacd |
115 | my $scheme = $c->request->secure ? 'https' : 'http'; |
294f78ca |
116 | my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; |
117 | my $port = $ENV{SERVER_PORT} || 80; |
8bf285ed |
118 | my $script_name = $ENV{SCRIPT_NAME}; |
119 | $script_name =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go if $script_name; |
120 | |
0bcb98c7 |
121 | my $base_path; |
122 | if ( exists $ENV{REDIRECT_URL} ) { |
123 | $base_path = $ENV{REDIRECT_URL}; |
124 | $base_path =~ s/$ENV{PATH_INFO}$//; |
125 | } |
126 | else { |
8bf285ed |
127 | $base_path = $script_name || '/'; |
0bcb98c7 |
128 | } |
4f5ebacd |
129 | |
fbcc39ad |
130 | # If we are running as a backend proxy, get the true hostname |
4f5ebacd |
131 | PROXY_CHECK: |
fbcc39ad |
132 | { |
df960201 |
133 | unless ( ref($c)->config->{using_frontend_proxy} ) { |
fbcc39ad |
134 | last PROXY_CHECK if $host !~ /localhost|127.0.0.1/; |
df960201 |
135 | last PROXY_CHECK if ref($c)->config->{ignore_frontend_proxy}; |
316bf0f0 |
136 | } |
fbcc39ad |
137 | last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST}; |
316bf0f0 |
138 | |
fbcc39ad |
139 | $host = $ENV{HTTP_X_FORWARDED_HOST}; |
4f5ebacd |
140 | |
141 | # backend could be on any port, so |
fbcc39ad |
142 | # assume frontend is on the default port |
143 | $port = $c->request->secure ? 443 : 80; |
64d1c3cd |
144 | if ( $ENV{HTTP_X_FORWARDED_PORT} ) { |
145 | $port = $ENV{HTTP_X_FORWARDED_PORT}; |
146 | } |
316bf0f0 |
147 | } |
148 | |
8bf285ed |
149 | # RFC 3875: "Unlike a URI path, the PATH_INFO is not URL-encoded, |
150 | # and cannot contain path-segment parameters." This means PATH_INFO |
151 | # is always decoded, and the script can't distinguish / vs %2F. |
152 | # See https://issues.apache.org/bugzilla/show_bug.cgi?id=35256 |
153 | # Here we try to resurrect the original encoded URI from REQUEST_URI. |
154 | my $path_info = $ENV{PATH_INFO}; |
155 | if (my $req_uri = $ENV{REQUEST_URI}) { |
156 | if (defined $script_name) { |
157 | $req_uri =~ s/^\Q$script_name\E//; |
158 | } |
159 | $req_uri =~ s/\?.*$//; |
160 | $path_info = $req_uri if $req_uri; |
161 | } |
162 | |
8d3c800b |
163 | # set the request URI |
8bf285ed |
164 | my $path = $base_path . ( $path_info || '' ); |
fbcc39ad |
165 | $path =~ s{^/+}{}; |
b0ad47c1 |
166 | |
933ba403 |
167 | # Using URI directly is way too slow, so we construct the URLs manually |
168 | my $uri_class = "URI::$scheme"; |
b0ad47c1 |
169 | |
de19de2e |
170 | # HTTP_HOST will include the port even if it's 80/443 |
171 | $host =~ s/:(?:80|443)$//; |
b0ad47c1 |
172 | |
de19de2e |
173 | if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) { |
933ba403 |
174 | $host .= ":$port"; |
175 | } |
b0ad47c1 |
176 | |
933ba403 |
177 | # Escape the path |
178 | $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go; |
179 | $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE |
b0ad47c1 |
180 | |
933ba403 |
181 | my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : ''; |
182 | my $uri = $scheme . '://' . $host . '/' . $path . $query; |
183 | |
184 | $c->request->uri( bless \$uri, $uri_class ); |
185 | |
8d3c800b |
186 | # set the base URI |
187 | # base must end in a slash |
188 | $base_path .= '/' unless $base_path =~ m{/$}; |
b0ad47c1 |
189 | |
8d3c800b |
190 | my $base_uri = $scheme . '://' . $host . $base_path; |
191 | |
192 | $c->request->base( bless \$base_uri, $uri_class ); |
e7c0c583 |
193 | } |
fc7ec1d9 |
194 | |
b5ecfcf0 |
195 | =head2 $self->prepare_query_parameters($c) |
fc7ec1d9 |
196 | |
197 | =cut |
198 | |
4090e3bb |
199 | around prepare_query_parameters => sub { |
200 | my $orig = shift; |
fbcc39ad |
201 | my ( $self, $c ) = @_; |
b5ecfcf0 |
202 | local (*ENV) = $self->env || \%ENV; |
203 | |
f8109766 |
204 | if ( $ENV{QUERY_STRING} ) { |
4090e3bb |
205 | $self->$orig( $c, $ENV{QUERY_STRING} ); |
f8109766 |
206 | } |
4090e3bb |
207 | }; |
e7c0c583 |
208 | |
b5ecfcf0 |
209 | =head2 $self->prepare_request($c, (env => \%env)) |
84528885 |
210 | |
211 | =cut |
212 | |
213 | sub prepare_request { |
214 | my ( $self, $c, %args ) = @_; |
215 | |
216 | if ( $args{env} ) { |
b5ecfcf0 |
217 | $self->env( $args{env} ); |
84528885 |
218 | } |
219 | } |
220 | |
b5ecfcf0 |
221 | =head2 $self->prepare_write($c) |
bfde09a2 |
222 | |
fbcc39ad |
223 | Enable autoflush on the output handle for CGI-based engines. |
bfde09a2 |
224 | |
fbcc39ad |
225 | =cut |
e7c0c583 |
226 | |
4090e3bb |
227 | around prepare_write => sub { |
4f5ebacd |
228 | *STDOUT->autoflush(1); |
4090e3bb |
229 | return shift->(@_); |
230 | }; |
e7c0c583 |
231 | |
e512dd24 |
232 | =head2 $self->write($c, $buffer) |
233 | |
234 | Writes the buffer to the client. |
235 | |
236 | =cut |
237 | |
4090e3bb |
238 | around write => sub { |
239 | my $orig = shift; |
e512dd24 |
240 | my ( $self, $c, $buffer ) = @_; |
241 | |
242 | # Prepend the headers if they have not yet been sent |
02570318 |
243 | if ( $self->_has_header_buf ) { |
244 | $buffer = $self->_clear_header_buf . $buffer; |
e512dd24 |
245 | } |
7fa2c9c1 |
246 | |
4090e3bb |
247 | return $self->$orig( $c, $buffer ); |
248 | }; |
e512dd24 |
249 | |
b5ecfcf0 |
250 | =head2 $self->read_chunk($c, $buffer, $length) |
e7c0c583 |
251 | |
fbcc39ad |
252 | =cut |
e7c0c583 |
253 | |
4f5ebacd |
254 | sub read_chunk { shift; shift; *STDIN->sysread(@_); } |
e7c0c583 |
255 | |
b5ecfcf0 |
256 | =head2 $self->run |
bfde09a2 |
257 | |
fbcc39ad |
258 | =cut |
bfde09a2 |
259 | |
0c913601 |
260 | sub run { shift; shift->handle_request( env => \%ENV ) } |
fc7ec1d9 |
261 | |
fc7ec1d9 |
262 | =head1 SEE ALSO |
263 | |
2f381252 |
264 | L<Catalyst>, L<Catalyst::Engine> |
fbcc39ad |
265 | |
266 | =head1 AUTHORS |
267 | |
2f381252 |
268 | Catalyst Contributors, see Catalyst.pm |
fc7ec1d9 |
269 | |
270 | =head1 COPYRIGHT |
271 | |
536bee89 |
272 | This library is free software. You can redistribute it and/or modify it under |
fc7ec1d9 |
273 | the same terms as Perl itself. |
274 | |
275 | =cut |
4090e3bb |
276 | no Moose; |
fc7ec1d9 |
277 | |
278 | 1; |