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