1 package Catalyst::Engine::CGI;
4 extends 'Catalyst::Engine';
6 has _header_buf => (is => 'rw', clearer => '_clear_header_buf', predicate => '_has_header_buf');
10 Catalyst::Engine::CGI - The CGI Engine
14 A script using the Catalyst::Engine::CGI module might look like:
19 use lib '/path/to/MyApp/lib';
24 The application module (C<MyApp>) would use C<Catalyst>, which loads the
25 appropriate engine module.
29 This is the Catalyst engine specialized for the CGI environment.
31 =head1 OVERLOADED METHODS
33 This class overloads some methods from C<Catalyst::Engine>.
35 =head2 $self->finalize_headers($c)
39 sub finalize_headers {
40 my ( $self, $c ) = @_;
42 $c->response->header( Status => $c->response->status );
44 $self->_header_buf($c->response->headers->as_string("\015\012") . "\015\012");
47 =head2 $self->prepare_connection($c)
51 sub prepare_connection {
52 my ( $self, $c ) = @_;
53 local (*ENV) = $self->env || \%ENV;
55 my $request = $c->request;
56 $request->address( $ENV{REMOTE_ADDR} );
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};
64 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_FOR};
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]+)$/;
69 $request->address($ip);
72 $request->hostname( $ENV{REMOTE_HOST} ) if exists $ENV{REMOTE_HOST};
73 $request->protocol( $ENV{SERVER_PROTOCOL} );
74 $request->user( $ENV{REMOTE_USER} ); # XXX: Deprecated. See Catalyst::Request for removal information
75 $request->remote_user( $ENV{REMOTE_USER} );
76 $request->method( $ENV{REQUEST_METHOD} );
78 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
82 if ( $ENV{SERVER_PORT} == 443 ) {
87 =head2 $self->prepare_headers($c)
92 my ( $self, $c ) = @_;
93 local (*ENV) = $self->env || \%ENV;
94 my $headers = $c->request->headers;
95 # Read headers from %ENV
96 foreach my $header ( keys %ENV ) {
97 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
98 ( my $field = $header ) =~ s/^HTTPS?_//;
99 $headers->header( $field => $ENV{$header} );
103 =head2 $self->prepare_path($c)
108 my ( $self, $c ) = @_;
109 local (*ENV) = $self->env || \%ENV;
111 my $scheme = $c->request->secure ? 'https' : 'http';
112 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
113 my $port = $ENV{SERVER_PORT} || 80;
115 if ( exists $ENV{REDIRECT_URL} ) {
116 $base_path = $ENV{REDIRECT_URL};
117 $base_path =~ s/$ENV{PATH_INFO}$//;
120 $base_path = $ENV{SCRIPT_NAME} || '/';
123 # If we are running as a backend proxy, get the true hostname
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};
130 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
132 $host = $ENV{HTTP_X_FORWARDED_HOST};
134 # backend could be on any port, so
135 # assume frontend is on the default port
136 $port = $c->request->secure ? 443 : 80;
139 # set the request URI
140 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
143 # Using URI directly is way too slow, so we construct the URLs manually
144 my $uri_class = "URI::$scheme";
146 # HTTP_HOST will include the port even if it's 80/443
147 $host =~ s/:(?:80|443)$//;
149 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
154 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
155 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
157 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
158 my $uri = $scheme . '://' . $host . '/' . $path . $query;
160 $c->request->uri( bless \$uri, $uri_class );
163 # base must end in a slash
164 $base_path .= '/' unless $base_path =~ m{/$};
166 my $base_uri = $scheme . '://' . $host . $base_path;
168 $c->request->base( bless \$base_uri, $uri_class );
171 =head2 $self->prepare_query_parameters($c)
175 around prepare_query_parameters => sub {
177 my ( $self, $c ) = @_;
178 local (*ENV) = $self->env || \%ENV;
180 if ( $ENV{QUERY_STRING} ) {
181 $self->$orig( $c, $ENV{QUERY_STRING} );
185 =head2 $self->prepare_request($c, (env => \%env))
189 sub prepare_request {
190 my ( $self, $c, %args ) = @_;
193 $self->env( $args{env} );
197 =head2 $self->prepare_write($c)
199 Enable autoflush on the output handle for CGI-based engines.
203 around prepare_write => sub {
204 *STDOUT->autoflush(1);
208 =head2 $self->write($c, $buffer)
210 Writes the buffer to the client.
214 around write => sub {
216 my ( $self, $c, $buffer ) = @_;
218 # Prepend the headers if they have not yet been sent
219 if ( $self->_has_header_buf ) {
220 $buffer = $self->_clear_header_buf . $buffer;
223 return $self->$orig( $c, $buffer );
226 =head2 $self->read_chunk($c, $buffer, $length)
230 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
236 sub run { shift; shift->handle_request( env => \%ENV ) }
240 L<Catalyst>, L<Catalyst::Engine>
244 Catalyst Contributors, see Catalyst.pm
248 This library is free software. You can redistribute it and/or modify it under
249 the same terms as Perl itself.