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