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 | { |
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]+)$/; |
7fa2c9c1 |
69 | $request->address($ip); |
fc7ec1d9 |
70 | } |
08cf3dd6 |
71 | |
8fc0d39e |
72 | $request->hostname( $ENV{REMOTE_HOST} ) if exists $ENV{REMOTE_HOST}; |
7fa2c9c1 |
73 | $request->protocol( $ENV{SERVER_PROTOCOL} ); |
8026359e |
74 | $request->user( $ENV{REMOTE_USER} ); # XXX: Deprecated. See Catalyst::Request for removal information |
75 | $request->remote_user( $ENV{REMOTE_USER} ); |
7fa2c9c1 |
76 | $request->method( $ENV{REQUEST_METHOD} ); |
fbcc39ad |
77 | |
78 | if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) { |
7fa2c9c1 |
79 | $request->secure(1); |
5b387dfc |
80 | } |
bfde09a2 |
81 | |
fbcc39ad |
82 | if ( $ENV{SERVER_PORT} == 443 ) { |
7fa2c9c1 |
83 | $request->secure(1); |
fbcc39ad |
84 | } |
fc7ec1d9 |
85 | } |
86 | |
b5ecfcf0 |
87 | =head2 $self->prepare_headers($c) |
fc7ec1d9 |
88 | |
89 | =cut |
90 | |
fbcc39ad |
91 | sub prepare_headers { |
92 | my ( $self, $c ) = @_; |
b5ecfcf0 |
93 | local (*ENV) = $self->env || \%ENV; |
7fa2c9c1 |
94 | my $headers = $c->request->headers; |
fbcc39ad |
95 | # Read headers from %ENV |
c82ed742 |
96 | foreach my $header ( keys %ENV ) { |
fbcc39ad |
97 | next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i; |
98 | ( my $field = $header ) =~ s/^HTTPS?_//; |
7fa2c9c1 |
99 | $headers->header( $field => $ENV{$header} ); |
fbcc39ad |
100 | } |
101 | } |
316bf0f0 |
102 | |
b5ecfcf0 |
103 | =head2 $self->prepare_path($c) |
316bf0f0 |
104 | |
fbcc39ad |
105 | =cut |
316bf0f0 |
106 | |
fbcc39ad |
107 | sub prepare_path { |
108 | my ( $self, $c ) = @_; |
b5ecfcf0 |
109 | local (*ENV) = $self->env || \%ENV; |
fbcc39ad |
110 | |
4f5ebacd |
111 | my $scheme = $c->request->secure ? 'https' : 'http'; |
294f78ca |
112 | my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; |
113 | my $port = $ENV{SERVER_PORT} || 80; |
0bcb98c7 |
114 | my $base_path; |
115 | if ( exists $ENV{REDIRECT_URL} ) { |
116 | $base_path = $ENV{REDIRECT_URL}; |
117 | $base_path =~ s/$ENV{PATH_INFO}$//; |
118 | } |
119 | else { |
120 | $base_path = $ENV{SCRIPT_NAME} || '/'; |
121 | } |
4f5ebacd |
122 | |
fbcc39ad |
123 | # If we are running as a backend proxy, get the true hostname |
4f5ebacd |
124 | PROXY_CHECK: |
fbcc39ad |
125 | { |
126 | unless ( $c->config->{using_frontend_proxy} ) { |
127 | last PROXY_CHECK if $host !~ /localhost|127.0.0.1/; |
128 | last PROXY_CHECK if $c->config->{ignore_frontend_proxy}; |
316bf0f0 |
129 | } |
fbcc39ad |
130 | last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST}; |
316bf0f0 |
131 | |
fbcc39ad |
132 | $host = $ENV{HTTP_X_FORWARDED_HOST}; |
4f5ebacd |
133 | |
134 | # backend could be on any port, so |
fbcc39ad |
135 | # assume frontend is on the default port |
136 | $port = $c->request->secure ? 443 : 80; |
316bf0f0 |
137 | } |
138 | |
8d3c800b |
139 | # set the request URI |
e701c5c6 |
140 | my $path = $base_path . ( $ENV{PATH_INFO} || '' ); |
fbcc39ad |
141 | $path =~ s{^/+}{}; |
b0ad47c1 |
142 | |
933ba403 |
143 | # Using URI directly is way too slow, so we construct the URLs manually |
144 | my $uri_class = "URI::$scheme"; |
b0ad47c1 |
145 | |
de19de2e |
146 | # HTTP_HOST will include the port even if it's 80/443 |
147 | $host =~ s/:(?:80|443)$//; |
b0ad47c1 |
148 | |
de19de2e |
149 | if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) { |
933ba403 |
150 | $host .= ":$port"; |
151 | } |
b0ad47c1 |
152 | |
933ba403 |
153 | # Escape the path |
154 | $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go; |
155 | $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE |
b0ad47c1 |
156 | |
933ba403 |
157 | my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : ''; |
158 | my $uri = $scheme . '://' . $host . '/' . $path . $query; |
159 | |
160 | $c->request->uri( bless \$uri, $uri_class ); |
161 | |
8d3c800b |
162 | # set the base URI |
163 | # base must end in a slash |
164 | $base_path .= '/' unless $base_path =~ m{/$}; |
b0ad47c1 |
165 | |
8d3c800b |
166 | my $base_uri = $scheme . '://' . $host . $base_path; |
167 | |
168 | $c->request->base( bless \$base_uri, $uri_class ); |
e7c0c583 |
169 | } |
fc7ec1d9 |
170 | |
b5ecfcf0 |
171 | =head2 $self->prepare_query_parameters($c) |
fc7ec1d9 |
172 | |
173 | =cut |
174 | |
4090e3bb |
175 | around prepare_query_parameters => sub { |
176 | my $orig = shift; |
fbcc39ad |
177 | my ( $self, $c ) = @_; |
b5ecfcf0 |
178 | local (*ENV) = $self->env || \%ENV; |
179 | |
f8109766 |
180 | if ( $ENV{QUERY_STRING} ) { |
4090e3bb |
181 | $self->$orig( $c, $ENV{QUERY_STRING} ); |
f8109766 |
182 | } |
4090e3bb |
183 | }; |
e7c0c583 |
184 | |
b5ecfcf0 |
185 | =head2 $self->prepare_request($c, (env => \%env)) |
84528885 |
186 | |
187 | =cut |
188 | |
189 | sub prepare_request { |
190 | my ( $self, $c, %args ) = @_; |
191 | |
192 | if ( $args{env} ) { |
b5ecfcf0 |
193 | $self->env( $args{env} ); |
84528885 |
194 | } |
195 | } |
196 | |
b5ecfcf0 |
197 | =head2 $self->prepare_write($c) |
bfde09a2 |
198 | |
fbcc39ad |
199 | Enable autoflush on the output handle for CGI-based engines. |
bfde09a2 |
200 | |
fbcc39ad |
201 | =cut |
e7c0c583 |
202 | |
4090e3bb |
203 | around prepare_write => sub { |
4f5ebacd |
204 | *STDOUT->autoflush(1); |
4090e3bb |
205 | return shift->(@_); |
206 | }; |
e7c0c583 |
207 | |
e512dd24 |
208 | =head2 $self->write($c, $buffer) |
209 | |
210 | Writes the buffer to the client. |
211 | |
212 | =cut |
213 | |
4090e3bb |
214 | around write => sub { |
215 | my $orig = shift; |
e512dd24 |
216 | my ( $self, $c, $buffer ) = @_; |
217 | |
218 | # Prepend the headers if they have not yet been sent |
02570318 |
219 | if ( $self->_has_header_buf ) { |
220 | $buffer = $self->_clear_header_buf . $buffer; |
e512dd24 |
221 | } |
7fa2c9c1 |
222 | |
4090e3bb |
223 | return $self->$orig( $c, $buffer ); |
224 | }; |
e512dd24 |
225 | |
b5ecfcf0 |
226 | =head2 $self->read_chunk($c, $buffer, $length) |
e7c0c583 |
227 | |
fbcc39ad |
228 | =cut |
e7c0c583 |
229 | |
4f5ebacd |
230 | sub read_chunk { shift; shift; *STDIN->sysread(@_); } |
e7c0c583 |
231 | |
b5ecfcf0 |
232 | =head2 $self->run |
bfde09a2 |
233 | |
fbcc39ad |
234 | =cut |
bfde09a2 |
235 | |
0c913601 |
236 | sub run { shift; shift->handle_request( env => \%ENV ) } |
fc7ec1d9 |
237 | |
fc7ec1d9 |
238 | =head1 SEE ALSO |
239 | |
2f381252 |
240 | L<Catalyst>, L<Catalyst::Engine> |
fbcc39ad |
241 | |
242 | =head1 AUTHORS |
243 | |
2f381252 |
244 | Catalyst Contributors, see Catalyst.pm |
fc7ec1d9 |
245 | |
246 | =head1 COPYRIGHT |
247 | |
536bee89 |
248 | This library is free software. You can redistribute it and/or modify it under |
fc7ec1d9 |
249 | the same terms as Perl itself. |
250 | |
251 | =cut |
4090e3bb |
252 | no Moose; |
fc7ec1d9 |
253 | |
254 | 1; |