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