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; |
7 | use URI::Query; |
e2fd5b5f |
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 | |
23f9d934 |
36 | =over 4 |
37 | |
fbcc39ad |
38 | =item $self->finalize_headers($c) |
fc7ec1d9 |
39 | |
40 | =cut |
41 | |
fbcc39ad |
42 | sub finalize_headers { |
43 | my ( $self, $c ) = @_; |
06e1b616 |
44 | |
fbcc39ad |
45 | $c->response->header( Status => $c->response->status ); |
06e1b616 |
46 | |
fbcc39ad |
47 | print $c->response->headers->as_string("\015\012"); |
48 | print "\015\012"; |
fc7ec1d9 |
49 | } |
50 | |
fbcc39ad |
51 | =item $self->prepare_connection($c) |
fc7ec1d9 |
52 | |
53 | =cut |
54 | |
fbcc39ad |
55 | sub prepare_connection { |
56 | my ( $self, $c ) = @_; |
57 | |
58 | $c->request->address( $ENV{REMOTE_ADDR} ); |
59 | |
60 | PROXY_CHECK: |
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}; |
67 | |
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]+)$/; |
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 | |
fbcc39ad |
88 | =item $self->prepare_headers($c) |
fc7ec1d9 |
89 | |
90 | =cut |
91 | |
fbcc39ad |
92 | sub prepare_headers { |
93 | my ( $self, $c ) = @_; |
316bf0f0 |
94 | |
fbcc39ad |
95 | # Read headers from %ENV |
96 | while ( my ( $header, $value ) = each %ENV ) { |
97 | next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i; |
98 | ( my $field = $header ) =~ s/^HTTPS?_//; |
99 | $c->req->headers->header( $field => $value ); |
100 | } |
101 | } |
316bf0f0 |
102 | |
fbcc39ad |
103 | =item $self->prepare_path($c) |
316bf0f0 |
104 | |
fbcc39ad |
105 | =cut |
316bf0f0 |
106 | |
fbcc39ad |
107 | sub prepare_path { |
108 | my ( $self, $c ) = @_; |
109 | |
110 | my $scheme = $c->request->secure ? 'https' : 'http'; |
111 | my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; |
112 | my $port = $ENV{SERVER_PORT} || 80; |
113 | my $base_path = $ENV{SCRIPT_NAME} || '/'; |
114 | |
115 | # If we are running as a backend proxy, get the true hostname |
116 | PROXY_CHECK: |
117 | { |
118 | unless ( $c->config->{using_frontend_proxy} ) { |
119 | last PROXY_CHECK if $host !~ /localhost|127.0.0.1/; |
120 | last PROXY_CHECK if $c->config->{ignore_frontend_proxy}; |
316bf0f0 |
121 | } |
fbcc39ad |
122 | last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST}; |
316bf0f0 |
123 | |
fbcc39ad |
124 | $host = $ENV{HTTP_X_FORWARDED_HOST}; |
125 | # backend could be on any port, so |
126 | # assume frontend is on the default port |
127 | $port = $c->request->secure ? 443 : 80; |
316bf0f0 |
128 | } |
129 | |
fbcc39ad |
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 ); |
e7c0c583 |
150 | } |
fc7ec1d9 |
151 | |
fbcc39ad |
152 | =item $self->prepare_query_parameters($c) |
fc7ec1d9 |
153 | |
154 | =cut |
155 | |
fbcc39ad |
156 | sub prepare_query_parameters { |
157 | my ( $self, $c ) = @_; |
158 | |
159 | my $u = URI::Query->new( $ENV{QUERY_STRING} ); |
160 | $c->request->query_parameters( { $u->hash } ); |
161 | } |
e7c0c583 |
162 | |
fbcc39ad |
163 | =item $self->prepare_write($c) |
bfde09a2 |
164 | |
fbcc39ad |
165 | Enable autoflush on the output handle for CGI-based engines. |
bfde09a2 |
166 | |
fbcc39ad |
167 | =cut |
e7c0c583 |
168 | |
fbcc39ad |
169 | sub prepare_write { |
170 | my ( $self, $c ) = @_; |
171 | |
172 | # Set the output handle to autoflush |
173 | $c->response->handle->autoflush(1); |
174 | |
175 | $self->NEXT::prepare_write( $c ); |
176 | } |
e7c0c583 |
177 | |
fbcc39ad |
178 | =item $self->read_chunk($c, $buffer, $length) |
e7c0c583 |
179 | |
fbcc39ad |
180 | =cut |
e7c0c583 |
181 | |
fbcc39ad |
182 | sub read_chunk { shift; shift->request->handle->sysread( @_ ); } |
e7c0c583 |
183 | |
fbcc39ad |
184 | =item $self->run |
bfde09a2 |
185 | |
fbcc39ad |
186 | =cut |
bfde09a2 |
187 | |
fbcc39ad |
188 | sub run { shift; shift->handle_request(@_) } |
fc7ec1d9 |
189 | |
23f9d934 |
190 | =back |
191 | |
fc7ec1d9 |
192 | =head1 SEE ALSO |
193 | |
fbcc39ad |
194 | L<Catalyst> L<Catalyst::Engine>. |
195 | |
196 | =head1 AUTHORS |
197 | |
198 | Sebastian Riedel, <sri@cpan.org> |
fc7ec1d9 |
199 | |
fbcc39ad |
200 | Christian Hansen, <ch@ngmedia.com> |
fc7ec1d9 |
201 | |
fbcc39ad |
202 | Andy Grundman, <andy@hybridized.org> |
fc7ec1d9 |
203 | |
204 | =head1 COPYRIGHT |
205 | |
206 | This program is free software, you can redistribute it and/or modify it under |
207 | the same terms as Perl itself. |
208 | |
209 | =cut |
210 | |
211 | 1; |