Commit | Line | Data |
b2e1304d |
1 | package HTTP::Request::AsCGI; |
2 | |
3 | use strict; |
4 | use warnings; |
090cc060 |
5 | use bytes; |
b2e1304d |
6 | use base 'Class::Accessor::Fast'; |
7 | |
8 | use Carp; |
4e0afe7d |
9 | use HTTP::Response; |
30efa07d |
10 | use IO::Handle; |
bd7813ac |
11 | use IO::File; |
b2e1304d |
12 | |
a3875fc6 |
13 | __PACKAGE__->mk_accessors(qw[ enviroment request stdin stdout stderr ]); |
b2e1304d |
14 | |
4e0afe7d |
15 | our $VERSION = 0.4; |
b2e1304d |
16 | |
17 | sub new { |
18 | my $class = shift; |
19 | my $request = shift; |
4e0afe7d |
20 | |
2d51e42f |
21 | unless ( @_ % 2 == 0 && eval { $request->isa('HTTP::Request') } ) { |
22 | croak(qq/usage: $class->new( \$request [, key => value] )/); |
23 | } |
4e0afe7d |
24 | |
ca38286c |
25 | my $self = $class->SUPER::new( { restored => 0, setuped => 0 } ); |
26 | $self->request($request); |
a3875fc6 |
27 | $self->stdin( IO::File->new_tmpfile ); |
28 | $self->stdout( IO::File->new_tmpfile ); |
b2e1304d |
29 | |
30efa07d |
30 | my $host = $request->header('Host'); |
31 | my $uri = $request->uri->clone; |
32 | $uri->scheme('http') unless $uri->scheme; |
33 | $uri->host('localhost') unless $uri->host; |
34 | $uri->port(80) unless $uri->port; |
a3875fc6 |
35 | $uri->host_port($host) unless !$host || ( $host eq $uri->host_port ); |
4e0afe7d |
36 | |
ca38286c |
37 | $uri = $uri->canonical; |
30efa07d |
38 | |
ca38286c |
39 | my $enviroment = { |
b2e1304d |
40 | GATEWAY_INTERFACE => 'CGI/1.1', |
30efa07d |
41 | HTTP_HOST => $uri->host_port, |
a3875fc6 |
42 | HTTPS => ( $uri->scheme eq 'https' ) ? 'ON' : 'OFF', # not in RFC 3875 |
43 | PATH_INFO => $uri->path, |
44 | QUERY_STRING => $uri->query || '', |
45 | SCRIPT_NAME => '/', |
46 | SERVER_NAME => $uri->host, |
47 | SERVER_PORT => $uri->port, |
48 | SERVER_PROTOCOL => $request->protocol || 'HTTP/1.1', |
49 | SERVER_SOFTWARE => "HTTP-Request-AsCGI/$VERSION", |
50 | REMOTE_ADDR => '127.0.0.1', |
51 | REMOTE_HOST => 'localhost', |
52 | REMOTE_PORT => int( rand(64000) + 1000 ), # not in RFC 3875 |
53 | REQUEST_URI => $uri->path_query, # not in RFC 3875 |
54 | REQUEST_METHOD => $request->method, |
b2e1304d |
55 | @_ |
56 | }; |
57 | |
58 | foreach my $field ( $request->headers->header_field_names ) { |
59 | |
ca38286c |
60 | my $key = uc("HTTP_$field"); |
2aaf55bc |
61 | $key =~ tr/-/_/; |
ca38286c |
62 | $key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/; |
b2e1304d |
63 | |
ca38286c |
64 | unless ( exists $enviroment->{$key} ) { |
65 | $enviroment->{$key} = $request->headers->header($field); |
b2e1304d |
66 | } |
67 | } |
68 | |
ca38286c |
69 | unless ( $enviroment->{SCRIPT_NAME} eq '/' && $enviroment->{PATH_INFO} ) { |
70 | $enviroment->{PATH_INFO} =~ s/^\Q$enviroment->{SCRIPT_NAME}\E/\//; |
71 | $enviroment->{PATH_INFO} =~ s/^\/+/\//; |
72 | } |
73 | |
74 | $self->enviroment($enviroment); |
75 | |
76 | return $self; |
b2e1304d |
77 | } |
78 | |
79 | sub setup { |
80 | my $self = shift; |
81 | |
090cc060 |
82 | $self->{restore}->{enviroment} = {%ENV}; |
b2e1304d |
83 | |
17b370b0 |
84 | binmode( $self->stdin ); |
b2e1304d |
85 | |
86 | if ( $self->request->content_length ) { |
87 | |
30efa07d |
88 | syswrite( $self->stdin, $self->request->content ) |
780060e5 |
89 | or croak("Can't write request content to stdin handle: $!"); |
b2e1304d |
90 | |
30efa07d |
91 | sysseek( $self->stdin, 0, SEEK_SET ) |
780060e5 |
92 | or croak("Can't seek stdin handle: $!"); |
b2e1304d |
93 | } |
94 | |
4e0afe7d |
95 | open( $self->{restore}->{stdin}, '<&', STDIN->fileno ) |
a3875fc6 |
96 | or croak("Can't dup stdin: $!"); |
ca38286c |
97 | |
a3875fc6 |
98 | open( STDIN, '<&=', $self->stdin->fileno ) |
99 | or croak("Can't open stdin: $!"); |
30efa07d |
100 | |
a3875fc6 |
101 | binmode( STDIN ); |
090cc060 |
102 | |
a3875fc6 |
103 | if ( $self->stdout ) { |
441eeb04 |
104 | |
a3875fc6 |
105 | open( $self->{restore}->{stdout}, '>&', STDOUT->fileno ) |
106 | or croak("Can't dup stdout: $!"); |
76391122 |
107 | |
a3875fc6 |
108 | open( STDOUT, '>&=', $self->stdout->fileno ) |
109 | or croak("Can't open stdout: $!"); |
76391122 |
110 | |
a3875fc6 |
111 | binmode( $self->stdout ); |
112 | binmode( STDOUT); |
113 | } |
090cc060 |
114 | |
a3875fc6 |
115 | if ( $self->stderr ) { |
30efa07d |
116 | |
a3875fc6 |
117 | open( $self->{restore}->{stderr}, '>&', STDERR->fileno ) |
118 | or croak("Can't dup stderr: $!"); |
090cc060 |
119 | |
a3875fc6 |
120 | open( STDERR, '>&=', $self->stderr->fileno ) |
121 | or croak("Can't open stderr: $!"); |
441eeb04 |
122 | |
a3875fc6 |
123 | binmode( $self->stderr ); |
124 | binmode( STDERR ); |
090cc060 |
125 | } |
126 | |
3cdea3c7 |
127 | { |
128 | no warnings 'uninitialized'; |
129 | %ENV = %{ $self->enviroment }; |
130 | } |
4e0afe7d |
131 | |
30efa07d |
132 | if ( $INC{'CGI.pm'} ) { |
133 | CGI::initialize_globals(); |
4e0afe7d |
134 | } |
b2e1304d |
135 | |
6f5fb9a7 |
136 | $self->{setuped}++; |
b2e1304d |
137 | |
138 | return $self; |
139 | } |
140 | |
780060e5 |
141 | sub response { |
142 | my ( $self, $callback ) = @_; |
143 | |
74fbb9dd |
144 | return undef unless $self->stdout; |
780060e5 |
145 | |
30efa07d |
146 | seek( $self->stdout, 0, SEEK_SET ) |
147 | or croak("Can't seek stdout handle: $!"); |
780060e5 |
148 | |
4e0afe7d |
149 | my $headers; |
decf17dc |
150 | while ( my $line = $self->stdout->getline ) { |
4e0afe7d |
151 | $headers .= $line; |
152 | last if $headers =~ /\x0d?\x0a\x0d?\x0a$/; |
780060e5 |
153 | } |
154 | |
4e0afe7d |
155 | unless ( defined $headers ) { |
156 | $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a"; |
decf17dc |
157 | } |
158 | |
4e0afe7d |
159 | unless ( $headers =~ /^HTTP/ ) { |
160 | $headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers; |
780060e5 |
161 | } |
162 | |
4e0afe7d |
163 | my $response = HTTP::Response->parse($headers); |
164 | $response->date( time() ) unless $response->date; |
decf17dc |
165 | |
4e0afe7d |
166 | my $message = $response->message; |
167 | my $status = $response->header('Status'); |
decf17dc |
168 | |
4e0afe7d |
169 | if ( $message && $message =~ /^(.+)\x0d$/ ) { |
170 | $response->message($1); |
171 | } |
decf17dc |
172 | |
4e0afe7d |
173 | if ( $status && $status =~ /^(\d\d\d)\s?(.+)?$/ ) { |
decf17dc |
174 | |
4e0afe7d |
175 | my $code = $1; |
176 | my $message = $2 || HTTP::Status::status_message($code); |
780060e5 |
177 | |
780060e5 |
178 | $response->code($code); |
4e0afe7d |
179 | $response->message($message); |
780060e5 |
180 | } |
181 | |
4e0afe7d |
182 | if ( $response->code == 500 && !$response->content ) { |
183 | |
184 | $response->content( $response->error_as_HTML ); |
185 | $response->content_type('text/html'); |
186 | |
187 | return $response; |
188 | } |
780060e5 |
189 | |
090cc060 |
190 | if ($callback) { |
4e0afe7d |
191 | |
192 | my $handle = $self->stdout; |
193 | |
a3875fc6 |
194 | $response->content( sub { |
4e0afe7d |
195 | |
196 | if ( $handle->read( my $buffer, 4096 ) ) { |
a3875fc6 |
197 | return $buffer; |
780060e5 |
198 | } |
4e0afe7d |
199 | |
a3875fc6 |
200 | return undef; |
201 | }); |
780060e5 |
202 | } |
203 | else { |
4e0afe7d |
204 | |
780060e5 |
205 | my $length = 0; |
4e0afe7d |
206 | |
780060e5 |
207 | while ( $self->stdout->read( my $buffer, 4096 ) ) { |
208 | $length += length($buffer); |
209 | $response->add_content($buffer); |
210 | } |
4e0afe7d |
211 | |
decf17dc |
212 | if ( $length && !$response->content_length ) { |
213 | $response->content_length($length); |
214 | } |
780060e5 |
215 | } |
216 | |
780060e5 |
217 | return $response; |
218 | } |
219 | |
b2e1304d |
220 | sub restore { |
221 | my $self = shift; |
4e0afe7d |
222 | |
cef1c068 |
223 | { |
224 | no warnings 'uninitialized'; |
225 | %ENV = %{ $self->{restore}->{enviroment} }; |
226 | } |
b2e1304d |
227 | |
4e0afe7d |
228 | open( STDIN, '<&', $self->{restore}->{stdin} ) |
a3875fc6 |
229 | or croak("Can't restore stdin: $!"); |
b2e1304d |
230 | |
a3875fc6 |
231 | sysseek( $self->stdin, 0, SEEK_SET ) |
232 | or croak("Can't seek stdin: $!"); |
12852959 |
233 | |
a3875fc6 |
234 | if ( $self->{restore}->{stdout} ) { |
30efa07d |
235 | |
a3875fc6 |
236 | STDOUT->flush |
237 | or croak("Can't flush stdout: $!"); |
30efa07d |
238 | |
a3875fc6 |
239 | open( STDOUT, '>&', $self->{restore}->{stdout} ) |
240 | or croak("Can't restore stdout: $!"); |
090cc060 |
241 | |
a3875fc6 |
242 | sysseek( $self->stdout, 0, SEEK_SET ) |
243 | or croak("Can't seek stdout: $!"); |
244 | } |
12852959 |
245 | |
a3875fc6 |
246 | if ( $self->{restore}->{stderr} ) { |
30efa07d |
247 | |
a3875fc6 |
248 | STDERR->flush |
249 | or croak("Can't flush stderr: $!"); |
30efa07d |
250 | |
a3875fc6 |
251 | open( STDERR, '>&', $self->{restore}->{stderr} ) |
252 | or croak("Can't restore stderr: $!"); |
090cc060 |
253 | |
a3875fc6 |
254 | sysseek( $self->stderr, 0, SEEK_SET ) |
255 | or croak("Can't seek stderr: $!"); |
6f5fb9a7 |
256 | } |
12852959 |
257 | |
b2e1304d |
258 | $self->{restored}++; |
090cc060 |
259 | |
260 | return $self; |
b2e1304d |
261 | } |
262 | |
263 | sub DESTROY { |
264 | my $self = shift; |
6f5fb9a7 |
265 | $self->restore if $self->{setuped} && !$self->{restored}; |
b2e1304d |
266 | } |
267 | |
268 | 1; |
269 | |
270 | __END__ |
271 | |
272 | =head1 NAME |
273 | |
bd7813ac |
274 | HTTP::Request::AsCGI - Setup a CGI enviroment from a HTTP::Request |
b2e1304d |
275 | |
276 | =head1 SYNOPSIS |
277 | |
bd7813ac |
278 | use CGI; |
279 | use HTTP::Request; |
280 | use HTTP::Request::AsCGI; |
281 | |
282 | my $request = HTTP::Request->new( GET => 'http://www.host.com/' ); |
283 | my $stdout; |
284 | |
285 | { |
286 | my $c = HTTP::Request::AsCGI->new($request)->setup; |
287 | my $q = CGI->new; |
288 | |
289 | print $q->header, |
290 | $q->start_html('Hello World'), |
291 | $q->h1('Hello World'), |
292 | $q->end_html; |
293 | |
294 | $stdout = $c->stdout; |
295 | |
2d51e42f |
296 | # enviroment and descriptors will automatically be restored |
297 | # when $c is destructed. |
bd7813ac |
298 | } |
299 | |
bd7813ac |
300 | while ( my $line = $stdout->getline ) { |
301 | print $line; |
302 | } |
303 | |
b2e1304d |
304 | =head1 DESCRIPTION |
305 | |
2d51e42f |
306 | Provides a convinient way of setting up an CGI enviroment from a HTTP::Request. |
307 | |
b2e1304d |
308 | =head1 METHODS |
309 | |
310 | =over 4 |
311 | |
2d51e42f |
312 | =item new ( $request [, key => value ] ) |
313 | |
314 | Contructor, first argument must be a instance of HTTP::Request |
ca38286c |
315 | followed by optional pairs of environment key and value. |
b2e1304d |
316 | |
bd7813ac |
317 | =item enviroment |
318 | |
2d51e42f |
319 | Returns a hashref containing the environment that will be used in setup. |
320 | Changing the hashref after setup has been called will have no effect. |
321 | |
b2e1304d |
322 | =item setup |
323 | |
2d51e42f |
324 | Setups the environment and descriptors. |
325 | |
b2e1304d |
326 | =item restore |
327 | |
2d51e42f |
328 | Restores the enviroment and descriptors. Can only be called after setup. |
329 | |
b2e1304d |
330 | =item request |
331 | |
2d51e42f |
332 | Returns the request given to constructor. |
333 | |
780060e5 |
334 | =item response |
335 | |
2d51e42f |
336 | Returns a HTTP::Response. Can only be called after restore. |
337 | |
b2e1304d |
338 | =item stdin |
339 | |
2d51e42f |
340 | Accessor for handle that will be used for STDIN, must be a real seekable |
341 | handle with an file descriptor. Defaults to a tempoary IO::File instance. |
342 | |
b2e1304d |
343 | =item stdout |
344 | |
2d51e42f |
345 | Accessor for handle that will be used for STDOUT, must be a real seekable |
346 | handle with an file descriptor. Defaults to a tempoary IO::File instance. |
347 | |
b2e1304d |
348 | =item stderr |
349 | |
2d51e42f |
350 | Accessor for handle that will be used for STDERR, must be a real seekable |
351 | handle with an file descriptor. |
b2e1304d |
352 | |
2d51e42f |
353 | =back |
b2e1304d |
354 | |
74fbb9dd |
355 | =head1 SEE ALSO |
356 | |
357 | =over 4 |
358 | |
359 | =item examples directory in this distribution. |
360 | |
361 | =item L<WWW::Mechanize::CGI> |
362 | |
363 | =item L<Test::WWW::Mechanize::CGI> |
364 | |
365 | =back |
366 | |
2d51e42f |
367 | =head1 THANKS TO |
17b370b0 |
368 | |
369 | Thomas L. Shinnick for his valuable win32 testing. |
370 | |
b2e1304d |
371 | =head1 AUTHOR |
372 | |
373 | Christian Hansen, C<ch@ngmedia.com> |
374 | |
375 | =head1 LICENSE |
376 | |
377 | This library is free software. You can redistribute it and/or modify |
378 | it under the same terms as perl itself. |
379 | |
380 | =cut |