1 package Catalyst::Engine::CGI;
4 use base 'Catalyst::Engine';
7 __PACKAGE__->mk_accessors('env');
11 Catalyst::Engine::CGI - The CGI Engine
15 A script using the Catalyst::Engine::CGI module might look like:
20 use lib '/path/to/MyApp/lib';
25 The application module (C<MyApp>) would use C<Catalyst>, which loads the
26 appropriate engine module.
30 This is the Catalyst engine specialized for the CGI environment.
32 =head1 OVERLOADED METHODS
34 This class overloads some methods from C<Catalyst::Engine>.
36 =head2 $self->finalize_headers($c)
40 sub finalize_headers {
41 my ( $self, $c ) = @_;
43 $c->response->header( Status => $c->response->status );
46 = $c->response->headers->as_string("\015\012") . "\015\012";
49 =head2 $self->prepare_connection($c)
53 sub prepare_connection {
54 my ( $self, $c ) = @_;
55 local (*ENV) = $self->env || \%ENV;
57 my $proxy = $self->_proxy_info($c);
59 $c->request->address( $proxy->{address} || $ENV{REMOTE_ADDR} );
60 $c->request->hostname( $proxy->{host} || $ENV{REMOTE_HOST} );
61 $c->request->protocol( $proxy->{scheme} || $ENV{SERVER_PROTOCOL} );
62 $c->request->user( $ENV{REMOTE_USER} );
63 $c->request->method( $ENV{REQUEST_METHOD} );
65 if ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) {
66 $c->request->secure(1);
69 if ( $ENV{SERVER_PORT} == 443 ) {
70 $c->request->secure(1);
73 if ( $c->request->protocol eq 'https' ) {
74 $c->request->secure(1);
78 =head2 $self->prepare_headers($c)
83 my ( $self, $c ) = @_;
84 local (*ENV) = $self->env || \%ENV;
86 # Read headers from %ENV
87 foreach my $header ( keys %ENV ) {
88 next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
89 ( my $field = $header ) =~ s/^HTTPS?_//;
90 $c->req->headers->header( $field => $ENV{$header} );
94 =head2 $self->prepare_path($c)
99 my ( $self, $c ) = @_;
100 local (*ENV) = $self->env || \%ENV;
102 my $proxy = $self->_proxy_info($c);
104 my $scheme = $c->request->secure ? 'https' : 'http';
105 my $host = $proxy->{host} || $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
106 my $port = $proxy->{port} || $ENV{SERVER_PORT} || 80;
108 if ( exists $ENV{REDIRECT_URL} ) {
109 $base_path = $ENV{REDIRECT_URL};
110 $base_path =~ s/$ENV{PATH_INFO}$//;
113 $base_path = $ENV{SCRIPT_NAME} || '/';
116 if ( $proxy->{path} ) {
117 $base_path = $proxy->{path} . $base_path;
118 $base_path =~ s{/$}{}g;
121 # set the request URI
122 my $path = $base_path . ( $ENV{PATH_INFO} || '' );
125 # Using URI directly is way too slow, so we construct the URLs manually
126 my $uri_class = "URI::$scheme";
128 # HTTP_HOST will include the port even if it's 80/443
129 $host =~ s/:(?:80|443)$//;
131 if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) {
136 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
137 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
139 my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : '';
140 my $uri = $scheme . '://' . $host . '/' . $path . $query;
142 $c->request->uri( bless \$uri, $uri_class );
145 # base must end in a slash
146 $base_path .= '/' unless $base_path =~ m{/$};
148 my $base_uri = $scheme . '://' . $host . $base_path;
150 $c->request->base( bless \$base_uri, $uri_class );
153 =head2 $self->prepare_query_parameters($c)
157 sub prepare_query_parameters {
158 my ( $self, $c ) = @_;
159 local (*ENV) = $self->env || \%ENV;
161 if ( $ENV{QUERY_STRING} ) {
162 $self->SUPER::prepare_query_parameters( $c, $ENV{QUERY_STRING} );
166 =head2 $self->prepare_request($c, (env => \%env))
170 sub prepare_request {
171 my ( $self, $c, %args ) = @_;
174 $self->env( $args{env} );
178 =head2 $self->prepare_write($c)
180 Enable autoflush on the output handle for CGI-based engines.
185 my ( $self, $c ) = @_;
187 # Set the output handle to autoflush
188 *STDOUT->autoflush(1);
190 $self->NEXT::prepare_write($c);
193 =head2 $self->write($c, $buffer)
195 Writes the buffer to the client.
200 my ( $self, $c, $buffer ) = @_;
202 # Prepend the headers if they have not yet been sent
203 if ( my $headers = delete $self->{_header_buf} ) {
204 $buffer = $headers . $buffer;
207 return $self->NEXT::write( $c, $buffer );
210 =head2 $self->read_chunk($c, $buffer, $length)
214 sub read_chunk { shift; shift; *STDIN->sysread(@_); }
220 sub run { shift; shift->handle_request(@_) }
224 L<Catalyst>, L<Catalyst::Engine>
228 Catalyst Contributors, see Catalyst.pm
232 This program is free software, you can redistribute it and/or modify it under
233 the same terms as Perl itself.