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