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 ( ref($c)->config->{using_frontend_proxy} ) {
61 last PROXY_CHECK if $ENV{REMOTE_ADDR} ne '127.0.0.1';
62 last PROXY_CHECK if ref($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);
70 if ( defined $ENV{HTTP_X_FORWARDED_PORT} ) {
71 $ENV{SERVER_PORT} = $ENV{HTTP_X_FORWARDED_PORT};
75 $request->hostname( $ENV{REMOTE_HOST} ) if exists $ENV{REMOTE_HOST};
76 $request->protocol( $ENV{SERVER_PROTOCOL} );
77 $request->user( $ENV{REMOTE_USER} ); # XXX: Deprecated. See Catalyst::Request for removal information
78 $request->remote_user( $ENV{REMOTE_USER} );
79 $request->method( $ENV{REQUEST_METHOD} );
81 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
85 if ( $ENV{SERVER_PORT} == 443 ) {
88 binmode(STDOUT); # Ensure we are sending bytes.
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)
111 # Please don't touch this method without adding tests in
112 # t/aggregate/unit_core_engine_cgi-prepare_path.t
114 my ( $self, $c ) = @_;
115 local (*ENV) = $self->env || \%ENV;
117 my $scheme = $c->request->secure ? 'https' : 'http';
118 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
119 my $port = $ENV{SERVER_PORT} || 80;
122 if ($ENV{SERVER_SOFTWARE} && $ENV{SERVER_SOFTWARE} =~ m{IIS/[6-9]\.\d}) {
123 $ENV{PATH_INFO} =~ s/^\Q$ENV{SCRIPT_NAME}\E//;
126 my $script_name = $ENV{SCRIPT_NAME};
127 $script_name =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go if $script_name;
130 if ( exists $ENV{REDIRECT_URL} ) {
131 $base_path = $ENV{REDIRECT_URL};
132 $base_path =~ s/$ENV{PATH_INFO}$//;
135 $base_path = $script_name || '/';
138 # If we are running as a backend proxy, get the true hostname
141 unless ( ref($c)->config->{using_frontend_proxy} ) {
142 last PROXY_CHECK if $host !~ /localhost|127.0.0.1/;
143 last PROXY_CHECK if ref($c)->config->{ignore_frontend_proxy};
145 last PROXY_CHECK unless $ENV{HTTP_X_FORWARDED_HOST};
147 $host = $ENV{HTTP_X_FORWARDED_HOST};
149 # backend could be on any port, so
150 # assume frontend is on the default port
151 $port = $c->request->secure ? 443 : 80;
152 if ( $ENV{HTTP_X_FORWARDED_PORT} ) {
153 $port = $ENV{HTTP_X_FORWARDED_PORT};
157 # RFC 3875: "Unlike a URI path, the PATH_INFO is not URL-encoded,
158 # and cannot contain path-segment parameters." This means PATH_INFO
159 # is always decoded, and the script can't distinguish / vs %2F.
160 # See https://issues.apache.org/bugzilla/show_bug.cgi?id=35256
161 # Here we try to resurrect the original encoded URI from REQUEST_URI.
162 my $path_info = $ENV{PATH_INFO};
163 if (my $req_uri = $ENV{REQUEST_URI}) {
164 $req_uri =~ s/^\Q$base_path\E//;
165 $req_uri =~ s/\?.*$//;
167 # Note that if REQUEST_URI doesn't start with a /, then the user
168 # is probably using mod_rewrite or something to rewrite requests
169 # into a sub-path of their application..
170 # This means that REQUEST_URI needs information from PATH_INFO
171 # prepending to it to be useful, otherwise the sub path which is
172 # being redirected to becomes the app base address which is
174 if (substr($req_uri, 0, 1) ne '/') {
175 my ($match) = $req_uri =~ m|^([^/]+)|;
176 my ($path_info_part) = $path_info =~ m|^(.*?\Q$match\E)|;
177 substr($req_uri, 0, length($match), $path_info_part)
180 $path_info = $req_uri;
184 # set the request URI
185 my $path = $base_path . ( $path_info || '' );
188 # Using URI directly is way too slow, so we construct the URLs manually
189 my $uri_class = "URI::$scheme";
191 # HTTP_HOST will include the port even if it's 80/443
192 $host =~ s/:(?:80|443)$//;
194 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
199 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
200 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
202 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
203 my $uri = $scheme . '://' . $host . '/' . $path . $query;
205 $c->request->uri( bless(\$uri, $uri_class)->canonical );
208 # base must end in a slash
209 $base_path .= '/' unless $base_path =~ m{/$};
211 my $base_uri = $scheme . '://' . $host . $base_path;
213 $c->request->base( bless \$base_uri, $uri_class );
216 =head2 $self->prepare_query_parameters($c)
220 around prepare_query_parameters => sub {
222 my ( $self, $c ) = @_;
223 local (*ENV) = $self->env || \%ENV;
225 if ( $ENV{QUERY_STRING} ) {
226 $self->$orig( $c, $ENV{QUERY_STRING} );
230 =head2 $self->prepare_request($c, (env => \%env))
234 sub prepare_request {
235 my ( $self, $c, %args ) = @_;
238 $self->env( $args{env} );
242 =head2 $self->prepare_write($c)
244 Enable autoflush on the output handle for CGI-based engines.
248 around prepare_write => sub {
249 *STDOUT->autoflush(1);
253 =head2 $self->write($c, $buffer)
255 Writes the buffer to the client.
259 around write => sub {
261 my ( $self, $c, $buffer ) = @_;
263 # Prepend the headers if they have not yet been sent
264 if ( $self->_has_header_buf ) {
265 $buffer = $self->_clear_header_buf . $buffer;
268 return $self->$orig( $c, $buffer );
271 =head2 $self->read_chunk($c, $buffer, $length)
275 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
281 sub run { shift; shift->handle_request( env => \%ENV ) }
285 L<Catalyst>, L<Catalyst::Engine>
289 Catalyst Contributors, see Catalyst.pm
293 This library is free software. You can redistribute it and/or modify it under
294 the same terms as Perl itself.