line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Request::AsCGI; |
2
|
|
|
|
|
|
|
our $VERSION = '1.2'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Set up a CGI environment from an HTTP::Request |
4
|
8
|
|
|
8
|
|
377026
|
use strict; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
269
|
|
5
|
8
|
|
|
8
|
|
44
|
use warnings; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
242
|
|
6
|
8
|
|
|
8
|
|
7185
|
use bytes; |
|
8
|
|
|
|
|
68
|
|
|
8
|
|
|
|
|
40
|
|
7
|
8
|
|
|
8
|
|
215
|
use base 'Class::Accessor::Fast'; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
7094
|
|
8
|
|
|
|
|
|
|
|
9
|
8
|
|
|
8
|
|
45738
|
use Carp; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
692
|
|
10
|
8
|
|
|
8
|
|
9211
|
use HTTP::Response; |
|
8
|
|
|
|
|
117207
|
|
|
8
|
|
|
|
|
322
|
|
11
|
8
|
|
|
8
|
|
3055
|
use IO::Handle; |
|
8
|
|
|
|
|
29712
|
|
|
8
|
|
|
|
|
520
|
|
12
|
8
|
|
|
8
|
|
2876
|
use IO::File; |
|
8
|
|
|
|
|
7308
|
|
|
8
|
|
|
|
|
1365
|
|
13
|
8
|
|
|
8
|
|
50
|
use URI (); |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
115
|
|
14
|
8
|
|
|
8
|
|
103
|
use URI::Escape (); |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
9662
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw[ environment request stdin stdout stderr ]); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# old typo |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
*enviroment = \&environment; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my %reserved = map { sprintf('%02x', ord($_)) => 1 } split //, $URI::reserved; |
23
|
|
|
|
|
|
|
sub _uri_safe_unescape { |
24
|
1
|
|
|
1
|
|
9
|
my ($s) = @_; |
25
|
1
|
100
|
|
|
|
8
|
$s =~ s/%([a-fA-F0-9]{2})/$reserved{lc($1)} ? "%$1" : pack('C', hex($1))/ge; |
|
2
|
|
|
|
|
21
|
|
26
|
1
|
|
|
|
|
9
|
$s |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub new { |
30
|
7
|
|
|
7
|
1
|
72393
|
my $class = shift; |
31
|
7
|
|
|
|
|
24
|
my $request = shift; |
32
|
|
|
|
|
|
|
|
33
|
7
|
50
|
33
|
|
|
64
|
unless ( @_ % 2 == 0 && eval { $request->isa('HTTP::Request') } ) { |
|
7
|
|
|
|
|
139
|
|
34
|
0
|
|
|
|
|
0
|
croak(qq/usage: $class->new( \$request [, key => value] )/); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
7
|
|
|
|
|
114
|
my $self = $class->SUPER::new( { restored => 0, setuped => 0 } ); |
38
|
7
|
|
|
|
|
130
|
$self->request($request); |
39
|
7
|
|
|
|
|
2069
|
$self->stdin( IO::File->new_tmpfile ); |
40
|
7
|
|
|
|
|
1055
|
$self->stdout( IO::File->new_tmpfile ); |
41
|
|
|
|
|
|
|
|
42
|
7
|
|
|
|
|
148
|
my $host = $request->header('Host'); |
43
|
7
|
|
|
|
|
567
|
my $uri = $request->uri->clone; |
44
|
7
|
50
|
|
|
|
946
|
$uri->scheme('http') unless $uri->scheme; |
45
|
7
|
50
|
|
|
|
402
|
$uri->host('localhost') unless $uri->host; |
46
|
7
|
50
|
|
|
|
513
|
$uri->port(80) unless $uri->port; |
47
|
7
|
50
|
33
|
|
|
248
|
$uri->host_port($host) unless !$host || ( $host eq $uri->host_port ); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Get it before canonicalized so REQUEST_URI can be as raw as possible |
50
|
7
|
|
|
|
|
56
|
my $request_uri = $uri->path_query; |
51
|
|
|
|
|
|
|
|
52
|
7
|
|
|
|
|
91
|
$uri = $uri->canonical; |
53
|
|
|
|
|
|
|
|
54
|
7
|
50
|
100
|
|
|
871
|
my $environment = { |
|
|
|
50
|
|
|
|
|
55
|
|
|
|
|
|
|
GATEWAY_INTERFACE => 'CGI/1.1', |
56
|
|
|
|
|
|
|
HTTP_HOST => $uri->host_port, |
57
|
|
|
|
|
|
|
HTTPS => ( $uri->scheme eq 'https' ) ? 'ON' : 'OFF', # not in RFC 3875 |
58
|
|
|
|
|
|
|
PATH_INFO => $uri->path, |
59
|
|
|
|
|
|
|
QUERY_STRING => $uri->query || '', |
60
|
|
|
|
|
|
|
SCRIPT_NAME => '/', |
61
|
|
|
|
|
|
|
SERVER_NAME => $uri->host, |
62
|
|
|
|
|
|
|
SERVER_PORT => $uri->port, |
63
|
|
|
|
|
|
|
SERVER_PROTOCOL => $request->protocol || 'HTTP/1.1', |
64
|
|
|
|
|
|
|
SERVER_SOFTWARE => "HTTP-Request-AsCGI/$VERSION", |
65
|
|
|
|
|
|
|
REMOTE_ADDR => '127.0.0.1', |
66
|
|
|
|
|
|
|
REMOTE_HOST => 'localhost', |
67
|
|
|
|
|
|
|
REMOTE_PORT => int( rand(64000) + 1000 ), # not in RFC 3875 |
68
|
|
|
|
|
|
|
REQUEST_URI => $request_uri, # not in RFC 3875 |
69
|
|
|
|
|
|
|
REQUEST_METHOD => $request->method, |
70
|
|
|
|
|
|
|
@_ |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# RFC 3875 says PATH_INFO is not URI-encoded. That's really |
74
|
|
|
|
|
|
|
# annoying for applications that you can't tell "%2F" vs "/", but |
75
|
|
|
|
|
|
|
# doing the partial decoding then makes it impossible to tell |
76
|
|
|
|
|
|
|
# "%252F" vs "%2F". Encoding everything is more compatible to what |
77
|
|
|
|
|
|
|
# web servers like Apache or lighttpd do, anyways. |
78
|
7
|
|
|
|
|
1424
|
$environment->{PATH_INFO} = URI::Escape::uri_unescape($environment->{PATH_INFO}); |
79
|
|
|
|
|
|
|
|
80
|
7
|
|
|
|
|
100
|
foreach my $field ( $request->headers->header_field_names ) { |
81
|
|
|
|
|
|
|
|
82
|
3
|
|
|
|
|
105
|
my $key = uc("HTTP_$field"); |
83
|
3
|
|
|
|
|
8
|
$key =~ tr/-/_/; |
84
|
3
|
100
|
|
|
|
20
|
$key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/; |
85
|
|
|
|
|
|
|
|
86
|
3
|
50
|
|
|
|
10
|
unless ( exists $environment->{$key} ) { |
87
|
3
|
|
|
|
|
10
|
$environment->{$key} = $request->headers->header($field); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
7
|
100
|
66
|
|
|
228
|
unless ( $environment->{SCRIPT_NAME} eq '/' && $environment->{PATH_INFO} ) { |
92
|
1
|
|
|
|
|
20
|
$environment->{PATH_INFO} =~ s/^\Q$environment->{SCRIPT_NAME}\E/\//; |
93
|
1
|
|
|
|
|
6
|
$environment->{PATH_INFO} =~ s/^\/+/\//; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
7
|
|
|
|
|
40
|
$self->environment($environment); |
97
|
|
|
|
|
|
|
|
98
|
7
|
|
|
|
|
109
|
return $self; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub setup { |
102
|
6
|
|
|
6
|
1
|
1514
|
my $self = shift; |
103
|
|
|
|
|
|
|
|
104
|
6
|
|
|
|
|
332
|
$self->{restore}->{environment} = {%ENV}; |
105
|
|
|
|
|
|
|
|
106
|
6
|
|
|
|
|
83
|
binmode( $self->stdin ); |
107
|
|
|
|
|
|
|
|
108
|
6
|
100
|
|
|
|
315
|
if ( $self->request->content_length ) { |
109
|
|
|
|
|
|
|
|
110
|
1
|
50
|
|
|
|
40
|
$self->stdin->print($self->request->content) |
111
|
|
|
|
|
|
|
or croak("Can't write request content to stdin handle: $!"); |
112
|
|
|
|
|
|
|
|
113
|
1
|
50
|
|
|
|
43
|
$self->stdin->seek(0, SEEK_SET) |
114
|
|
|
|
|
|
|
or croak("Can't seek stdin handle: $!"); |
115
|
|
|
|
|
|
|
|
116
|
1
|
50
|
|
|
|
75
|
$self->stdin->flush |
117
|
|
|
|
|
|
|
or croak("Can't flush stdin handle: $!"); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
6
|
50
|
|
|
|
801
|
open( $self->{restore}->{stdin}, '<&'. STDIN->fileno ) |
121
|
|
|
|
|
|
|
or croak("Can't dup stdin: $!"); |
122
|
|
|
|
|
|
|
|
123
|
6
|
50
|
|
|
|
279
|
open( STDIN, '<&='. $self->stdin->fileno ) |
124
|
|
|
|
|
|
|
or croak("Can't open stdin: $!"); |
125
|
|
|
|
|
|
|
|
126
|
6
|
|
|
|
|
527
|
binmode( STDIN ); |
127
|
|
|
|
|
|
|
|
128
|
6
|
100
|
|
|
|
36
|
if ( $self->stdout ) { |
129
|
|
|
|
|
|
|
|
130
|
5
|
50
|
|
|
|
81
|
open( $self->{restore}->{stdout}, '>&'. STDOUT->fileno ) |
131
|
|
|
|
|
|
|
or croak("Can't dup stdout: $!"); |
132
|
|
|
|
|
|
|
|
133
|
5
|
50
|
|
|
|
117
|
open( STDOUT, '>&='. $self->stdout->fileno ) |
134
|
|
|
|
|
|
|
or croak("Can't open stdout: $!"); |
135
|
|
|
|
|
|
|
|
136
|
5
|
|
|
|
|
175
|
binmode( $self->stdout ); |
137
|
5
|
|
|
|
|
32
|
binmode( STDOUT); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
6
|
100
|
|
|
|
44
|
if ( $self->stderr ) { |
141
|
|
|
|
|
|
|
|
142
|
1
|
50
|
|
|
|
11
|
open( $self->{restore}->{stderr}, '>&'. STDERR->fileno ) |
143
|
|
|
|
|
|
|
or croak("Can't dup stderr: $!"); |
144
|
|
|
|
|
|
|
|
145
|
1
|
50
|
|
|
|
22
|
open( STDERR, '>&='. $self->stderr->fileno ) |
146
|
|
|
|
|
|
|
or croak("Can't open stderr: $!"); |
147
|
|
|
|
|
|
|
|
148
|
1
|
|
|
|
|
28
|
binmode( $self->stderr ); |
149
|
1
|
|
|
|
|
5
|
binmode( STDERR ); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
{ |
153
|
8
|
|
|
8
|
|
53
|
no warnings 'uninitialized'; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
5800
|
|
|
6
|
|
|
|
|
124
|
|
154
|
6
|
|
|
|
|
32
|
%ENV = (%ENV, %{ $self->environment }); |
|
6
|
|
|
|
|
43
|
|
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
6
|
50
|
|
|
|
847
|
if ( $INC{'CGI.pm'} ) { |
158
|
0
|
|
|
|
|
0
|
CGI::initialize_globals(); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
6
|
|
|
|
|
13
|
$self->{setuped}++; |
162
|
|
|
|
|
|
|
|
163
|
6
|
|
|
|
|
19
|
return $self; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub response { |
167
|
4
|
|
|
4
|
1
|
336039
|
my ( $self, $callback ) = @_; |
168
|
|
|
|
|
|
|
|
169
|
4
|
50
|
|
|
|
49
|
return undef unless $self->stdout; |
170
|
|
|
|
|
|
|
|
171
|
4
|
50
|
|
|
|
191
|
seek( $self->stdout, 0, SEEK_SET ) |
172
|
|
|
|
|
|
|
or croak("Can't seek stdout handle: $!"); |
173
|
|
|
|
|
|
|
|
174
|
4
|
|
|
|
|
76
|
my $headers; |
175
|
4
|
|
|
|
|
39
|
while ( my $line = $self->stdout->getline ) { |
176
|
15
|
|
|
|
|
1283
|
$headers .= $line; |
177
|
15
|
100
|
|
|
|
133
|
last if $headers =~ /\x0d?\x0a\x0d?\x0a$/; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
4
|
100
|
|
|
|
215
|
unless ( defined $headers ) { |
181
|
1
|
|
|
|
|
4
|
$headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a"; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
4
|
100
|
|
|
|
33
|
unless ( $headers =~ /^HTTP/ ) { |
185
|
2
|
|
|
|
|
6
|
$headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
4
|
|
|
|
|
103
|
my $response = HTTP::Response->parse($headers); |
189
|
4
|
100
|
|
|
|
1387
|
$response->date( time() ) unless $response->date; |
190
|
|
|
|
|
|
|
|
191
|
4
|
|
|
|
|
21083
|
my $message = $response->message; |
192
|
4
|
|
|
|
|
104
|
my $status = $response->header('Status'); |
193
|
|
|
|
|
|
|
|
194
|
4
|
100
|
66
|
|
|
228
|
if ( $message && $message =~ /^(.+)\x0d$/ ) { |
195
|
3
|
|
|
|
|
11
|
$response->message($1); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
4
|
100
|
66
|
|
|
144
|
if ( $status && $status =~ /^(\d\d\d)\s?(.+)?$/ ) { |
199
|
|
|
|
|
|
|
|
200
|
3
|
|
|
|
|
10
|
my $code = $1; |
201
|
3
|
|
66
|
|
|
20
|
my $message = $2 || HTTP::Status::status_message($code); |
202
|
|
|
|
|
|
|
|
203
|
3
|
|
|
|
|
25
|
$response->code($code); |
204
|
3
|
|
|
|
|
36
|
$response->message($message); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
4
|
|
|
|
|
45
|
my $length = ( stat( $self->stdout ) )[7] - tell( $self->stdout ); |
208
|
|
|
|
|
|
|
|
209
|
4
|
100
|
100
|
|
|
141
|
if ( $response->code == 500 && !$length ) { |
210
|
|
|
|
|
|
|
|
211
|
1
|
|
|
|
|
21
|
$response->content( $response->error_as_HTML ); |
212
|
1
|
|
|
|
|
64
|
$response->content_type('text/html'); |
213
|
|
|
|
|
|
|
|
214
|
1
|
|
|
|
|
42
|
return $response; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
3
|
50
|
|
|
|
59
|
if ($callback) { |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
0
|
my $handle = $self->stdout; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
$response->content( sub { |
222
|
|
|
|
|
|
|
|
223
|
0
|
0
|
|
0
|
|
0
|
if ( $handle->read( my $buffer, 4096 ) ) { |
224
|
0
|
|
|
|
|
0
|
return $buffer; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
0
|
return undef; |
228
|
0
|
|
|
|
|
0
|
}); |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
else { |
231
|
|
|
|
|
|
|
|
232
|
3
|
|
|
|
|
9
|
my $length = 0; |
233
|
|
|
|
|
|
|
|
234
|
3
|
|
|
|
|
18
|
while ( $self->stdout->read( my $buffer, 4096 ) ) { |
235
|
3
|
|
|
|
|
136
|
$length += length($buffer); |
236
|
3
|
|
|
|
|
34
|
$response->add_content($buffer); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
3
|
50
|
33
|
|
|
137
|
if ( $length && !$response->content_length ) { |
240
|
3
|
|
|
|
|
142
|
$response->content_length($length); |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
3
|
|
|
|
|
128
|
return $response; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub restore { |
248
|
6
|
|
|
6
|
1
|
4427
|
my $self = shift; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
{ |
251
|
8
|
|
|
8
|
|
48
|
no warnings 'uninitialized'; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
3793
|
|
|
6
|
|
|
|
|
12
|
|
252
|
6
|
|
|
|
|
17
|
%ENV = %{ $self->{restore}->{environment} }; |
|
6
|
|
|
|
|
310
|
|
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
6
|
50
|
|
|
|
178
|
open( STDIN, '<&'. fileno($self->{restore}->{stdin}) ) |
256
|
|
|
|
|
|
|
or croak("Can't restore stdin: $!"); |
257
|
|
|
|
|
|
|
|
258
|
6
|
50
|
|
|
|
28
|
sysseek( $self->stdin, 0, SEEK_SET ) |
259
|
|
|
|
|
|
|
or croak("Can't seek stdin: $!"); |
260
|
|
|
|
|
|
|
|
261
|
6
|
100
|
|
|
|
87
|
if ( $self->{restore}->{stdout} ) { |
262
|
|
|
|
|
|
|
|
263
|
5
|
50
|
|
|
|
88
|
STDOUT->flush |
264
|
|
|
|
|
|
|
or croak("Can't flush stdout: $!"); |
265
|
|
|
|
|
|
|
|
266
|
5
|
50
|
|
|
|
103
|
open( STDOUT, '>&'. fileno($self->{restore}->{stdout}) ) |
267
|
|
|
|
|
|
|
or croak("Can't restore stdout: $!"); |
268
|
|
|
|
|
|
|
|
269
|
5
|
50
|
|
|
|
19
|
sysseek( $self->stdout, 0, SEEK_SET ) |
270
|
|
|
|
|
|
|
or croak("Can't seek stdout: $!"); |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
6
|
100
|
|
|
|
79
|
if ( $self->{restore}->{stderr} ) { |
274
|
|
|
|
|
|
|
|
275
|
1
|
50
|
|
|
|
7
|
STDERR->flush |
276
|
|
|
|
|
|
|
or croak("Can't flush stderr: $!"); |
277
|
|
|
|
|
|
|
|
278
|
1
|
50
|
|
|
|
19
|
open( STDERR, '>&'. fileno($self->{restore}->{stderr}) ) |
279
|
|
|
|
|
|
|
or croak("Can't restore stderr: $!"); |
280
|
|
|
|
|
|
|
|
281
|
1
|
50
|
|
|
|
5
|
sysseek( $self->stderr, 0, SEEK_SET ) |
282
|
|
|
|
|
|
|
or croak("Can't seek stderr: $!"); |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
6
|
|
|
|
|
22
|
$self->{restored}++; |
286
|
|
|
|
|
|
|
|
287
|
6
|
|
|
|
|
27
|
return $self; |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
sub DESTROY { |
291
|
7
|
|
|
7
|
|
2369
|
my $self = shift; |
292
|
7
|
50
|
66
|
|
|
1512
|
$self->restore if $self->{setuped} && !$self->{restored}; |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
1; |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=pod |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=head1 NAME |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
HTTP::Request::AsCGI - Set up a CGI environment from an HTTP::Request |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 VERSION |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
version 1.2 |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=for Pod::Coverage enviroment |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=cut |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=pod |
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
|
|
|
|
|
|
|
=head1 AUTHORS |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
Christian Hansen <ch@ngmedia.com> |
414
|
|
|
|
|
|
|
Hans Dieter Pearcey <hdp@cpan.org> |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Christian Hansen <ch@ngmedia.com>. |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
421
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
=cut |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
__END__ |
427
|
|
|
|
|
|
|
|