ok ok server gone too now.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
CommitLineData
fc7ec1d9 1package Catalyst::Engine::CGI;
2
3use strict;
4use base 'Catalyst::Engine';
5use URI;
399ed680 6use URI::http;
fc7ec1d9 7
8require CGI::Simple;
fc7ec1d9 9
7833fdfc 10$CGI::Simple::POST_MAX = 1048576;
11$CGI::Simple::DISABLE_UPLOADS = 0;
12
fc7ec1d9 13__PACKAGE__->mk_accessors('cgi');
14
15=head1 NAME
16
17Catalyst::Engine::CGI - The CGI Engine
18
19=head1 SYNOPSIS
20
23f9d934 21A script using the Catalyst::Engine::CGI module might look like:
22
9a33da6a 23 #!/usr/bin/perl -w
24
25 use strict;
26 use lib '/path/to/MyApp/lib';
27 use MyApp;
28
29 MyApp->run;
30
23f9d934 31The application module (C<MyApp>) would use C<Catalyst>, which loads the
32appropriate engine module.
fc7ec1d9 33
34=head1 DESCRIPTION
35
23f9d934 36This is the Catalyst engine specialized for the CGI environment (using the
37C<CGI::Simple> and C<CGI::Cookie> modules). Normally Catalyst will select the
38appropriate engine according to the environment that it detects, however you
39can force Catalyst to use the CGI engine by specifying the following in your
40application module:
41
42 use Catalyst qw(-Engine=CGI);
fc7ec1d9 43
9a33da6a 44The performance of this way of using Catalyst is not expected to be
45useful in production applications, but it may be helpful for development.
46
23f9d934 47=head1 METHODS
fc7ec1d9 48
23f9d934 49=over 4
50
23f9d934 51=item $c->cgi
fc7ec1d9 52
53This config parameter contains the C<CGI::Simple> object.
54
23f9d934 55=back
56
57=head1 OVERLOADED METHODS
fc7ec1d9 58
45374ac6 59This class overloads some methods from C<Catalyst::Engine>.
fc7ec1d9 60
23f9d934 61=over 4
62
63=item $c->finalize_headers
fc7ec1d9 64
65=cut
66
67sub finalize_headers {
68 my $c = shift;
e646f111 69 my %headers;
6dc87a0f 70
fc7ec1d9 71 $headers{-status} = $c->response->status if $c->response->status;
6dc87a0f 72
fc7ec1d9 73 for my $name ( $c->response->headers->header_field_names ) {
6dc87a0f 74 $headers{"-$name"} = $c->response->header($name);
fc7ec1d9 75 }
6dc87a0f 76
fc7ec1d9 77 print $c->cgi->header(%headers);
78}
79
23f9d934 80=item $c->finalize_output
81
82Prints the response output to STDOUT.
fc7ec1d9 83
84=cut
85
86sub finalize_output {
87 my $c = shift;
88 print $c->response->output;
89}
90
0556eb49 91=item $c->prepare_connection
92
93=cut
94
95sub prepare_connection {
96 my $c = shift;
97 $c->req->hostname( $c->cgi->remote_host );
98 $c->req->address( $c->cgi->remote_addr );
99}
100
23f9d934 101=item $c->prepare_headers
fc7ec1d9 102
103=cut
104
105sub prepare_headers {
106 my $c = shift;
107 $c->req->method( $c->cgi->request_method );
108 for my $header ( $c->cgi->http ) {
109 ( my $field = $header ) =~ s/^HTTPS?_//;
110 $c->req->headers->header( $field => $c->cgi->http($header) );
111 }
49faa307 112 $c->req->headers->header( 'Content-Type' => $c->cgi->content_type );
113 $c->req->headers->header( 'Content-Length' => $c->cgi->content_length );
fc7ec1d9 114}
115
23f9d934 116=item $c->prepare_parameters
fc7ec1d9 117
118=cut
119
120sub prepare_parameters {
121 my $c = shift;
523d44ec 122
123 $c->cgi->parse_query_string;
124
fc7ec1d9 125 my %vars = $c->cgi->Vars;
126 while ( my ( $key, $value ) = each %vars ) {
127 my @values = split "\0", $value;
128 $vars{$key} = @values <= 1 ? $values[0] : \@values;
129 }
130 $c->req->parameters( {%vars} );
131}
132
23f9d934 133=item $c->prepare_path
fc7ec1d9 134
135=cut
136
137sub prepare_path {
138 my $c = shift;
8b4483b3 139
140 my $base;
141 {
142 my $scheme = $ENV{HTTPS} ? 'https' : 'http';
143 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
144 my $port = $ENV{SERVER_PORT} || 80;
145 my $path = $ENV{SCRIPT_NAME} || '/';
146
147 $base = URI->new;
148 $base->scheme($scheme);
149 $base->host($host);
150 $base->port($port);
151 $base->path($path);
152
153 $base = $base->canonical->as_string;
7833fdfc 154 }
8b4483b3 155
156 my $path = $ENV{PATH_INFO} || '/';
6dc87a0f 157 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
8b4483b3 158 $path =~ s/^\///;
159
160 $c->req->base($base);
161 $c->req->path($path);
fc7ec1d9 162}
163
23f9d934 164=item $c->prepare_request
fc7ec1d9 165
166=cut
167
168sub prepare_request { shift->cgi( CGI::Simple->new ) }
169
23f9d934 170=item $c->prepare_uploads
fc7ec1d9 171
172=cut
173
174sub prepare_uploads {
175 my $c = shift;
176 for my $name ( $c->cgi->upload ) {
b0b7c5e0 177 next unless defined $name;
fc7ec1d9 178 $c->req->uploads->{$name} = {
7833fdfc 179 fh => $c->cgi->upload($name),
180 size => $c->cgi->upload_info( $name, 'size' ),
181 type => $c->cgi->upload_info( $name, 'mime' )
fc7ec1d9 182 };
183 }
184}
185
c9afa5fc 186=item $c->run
187
188=cut
189
fc7ec1d9 190sub run { shift->handler }
191
23f9d934 192=back
193
fc7ec1d9 194=head1 SEE ALSO
195
196L<Catalyst>.
197
198=head1 AUTHOR
199
200Sebastian Riedel, C<sri@cpan.org>
201
202=head1 COPYRIGHT
203
204This program is free software, you can redistribute it and/or modify it under
205the same terms as Perl itself.
206
207=cut
208
2091;