1 package Catalyst::Engine::CGI;
6 extends 'Catalyst::Engine';
8 has env => (is => 'rw');
14 Catalyst::Engine::CGI - The CGI Engine
18 A script using the Catalyst::Engine::CGI module might look like:
23 use lib '/path/to/MyApp/lib';
28 The application module (C<MyApp>) would use C<Catalyst>, which loads the
29 appropriate engine module.
33 This is the Catalyst engine specialized for the CGI environment.
35 =head1 OVERLOADED METHODS
37 This class overloads some methods from C<Catalyst::Engine>.
39 =head2 $self->finalize_headers($c)
43 sub finalize_headers {
44 my ( $self, $c ) = @_;
46 $c->response->header( Status => $c->response->status );
49 = $c->response->headers->as_string("\015\012") . "\015\012";
52 =head2 $self->prepare_connection($c)
56 sub prepare_connection {
57 my ( $self, $c ) = @_;
58 local (*ENV) = $self->env || \%ENV;
60 my $request = $c->request;
61 $request->address( $ENV{REMOTE_ADDR} );
65 unless ( $c->config->{using_frontend_proxy} ) {
66 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
67 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
69 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
71 # If we are running as a backend server, the user will always appear
72 # as 127.0.0.1. Select the most recent upstream IP (last in the list)
73 my ($ip) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
74 $request->address($ip);
77 $request->hostname( $ENV{REMOTE_HOST} );
78 $request->protocol( $ENV{SERVER_PROTOCOL} );
79 $request->user( $ENV{REMOTE_USER} );
80 $request->method( $ENV{REQUEST_METHOD} );
82 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
86 if ( $ENV{SERVER_PORT} == 443 ) {
91 =head2 $self->prepare_headers($c)
96 my ( $self, $c ) = @_;
97 local (*ENV) = $self->env || \%ENV;
98 my $headers = $c->request->headers;
99 # Read headers from %ENV
100 foreach my $header ( keys %ENV ) {
101 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
102 ( my $field = $header ) =~ s/^HTTPS?_//;
103 $headers->header( $field => $ENV{$header} );
107 =head2 $self->prepare_path($c)
112 my ( $self, $c ) = @_;
113 local (*ENV) = $self->env || \%ENV;
115 my $scheme = $c->request->secure ? 'https' : 'http';
116 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
117 my $port = $ENV{SERVER_PORT} || 80;
119 if ( exists $ENV{REDIRECT_URL} ) {
120 $base_path = $ENV{REDIRECT_URL};
121 $base_path =~ s/$ENV{PATH_INFO}$//;
124 $base_path = $ENV{SCRIPT_NAME} || '/';
127 # If we are running as a backend proxy, get the true hostname
130 unless ( $c->config->{using_frontend_proxy} ) {
131 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
132 last PROXY_CHECK if $c->config->{ignore_frontend_proxy};
134 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
136 $host = $ENV{HTTP_X_FORWARDED_HOST};
138 # backend could be on any port, so
139 # assume frontend is on the default port
140 $port = $c->request->secure ? 443 : 80;
143 # set the request URI
144 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
147 # Using URI directly is way too slow, so we construct the URLs manually
148 my $uri_class = "URI::$scheme";
150 # HTTP_HOST will include the port even if it's 80/443
151 $host =~ s/:(?:80|443)$//;
153 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
158 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
159 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
161 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
162 my $uri = $scheme . '://' . $host . '/' . $path . $query;
164 $c->request->uri( bless \$uri, $uri_class );
167 # base must end in a slash
168 $base_path .= '/' unless $base_path =~ m{/$};
170 my $base_uri = $scheme . '://' . $host . $base_path;
172 $c->request->base( bless \$base_uri, $uri_class );
175 =head2 $self->prepare_query_parameters($c)
179 sub prepare_query_parameters {
180 my ( $self, $c ) = @_;
181 local (*ENV) = $self->env || \%ENV;
183 if ( $ENV{QUERY_STRING} ) {
184 $self->next::method( $c, $ENV{QUERY_STRING} );
188 =head2 $self->prepare_request($c, (env => \%env))
192 sub prepare_request {
193 my ( $self, $c, %args ) = @_;
196 $self->env( $args{env} );
200 =head2 $self->prepare_write($c)
202 Enable autoflush on the output handle for CGI-based engines.
207 *STDOUT->autoflush(1);
208 return shift->next::method(@_);
211 =head2 $self->write($c, $buffer)
213 Writes the buffer to the client.
218 my ( $self, $c, $buffer ) = @_;
220 # Prepend the headers if they have not yet been sent
221 if ( my $headers = delete $self->{_header_buf} ) {
222 $buffer = $headers . $buffer;
225 return $self->next::method( $c, $buffer );
228 =head2 $self->read_chunk($c, $buffer, $length)
232 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
238 sub run { shift; shift->handle_request(@_) }
242 L<Catalyst> L<Catalyst::Engine>.
246 Sebastian Riedel, <sri@cpan.org>
248 Christian Hansen, <ch@ngmedia.com>
250 Andy Grundman, <andy@hybridized.org>
254 This program is free software, you can redistribute it and/or modify it under
255 the same terms as Perl itself.