package HTTP::Request::AsCGI;
-
+# ABSTRACT: Set up a CGI environment from an HTTP::Request
use strict;
use warnings;
use bytes;
use base 'Class::Accessor::Fast';
+our $VERSION = '1.0';
+
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
+=begin Pod::Coverage
+
+ enviroment
+
+=end Pod::Coverage
-__PACKAGE__->mk_accessors(qw[ enviroment request stdin stdout stderr ]);
+=cut
+
+*enviroment = \&environment;
-our $VERSION = 0.3;
+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 );
$uri->host('localhost') unless $uri->host;
$uri->port(80) unless $uri->port;
$uri->host_port($host) unless !$host || ( $host eq $uri->host_port );
-
+
$uri = $uri->canonical;
- my $enviroment = {
+ my $environment = {
GATEWAY_INTERFACE => 'CGI/1.1',
HTTP_HOST => $uri->host_port,
HTTPS => ( $uri->scheme eq 'https' ) ? 'ON' : 'OFF', # not in RFC 3875
@_
};
+ # 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 $enviroment->{$key} ) {
- $enviroment->{$key} = $request->headers->header($field);
+ unless ( exists $environment->{$key} ) {
+ $environment->{$key} = $request->headers->header($field);
}
}
- unless ( $enviroment->{SCRIPT_NAME} eq '/' && $enviroment->{PATH_INFO} ) {
- $enviroment->{PATH_INFO} =~ s/^\Q$enviroment->{SCRIPT_NAME}\E/\//;
- $enviroment->{PATH_INFO} =~ s/^\/+/\//;
+ unless ( $environment->{SCRIPT_NAME} eq '/' && $environment->{PATH_INFO} ) {
+ $environment->{PATH_INFO} =~ s/^\Q$environment->{SCRIPT_NAME}\E/\//;
+ $environment->{PATH_INFO} =~ s/^\/+/\//;
}
- $self->enviroment($enviroment);
+ $self->environment($environment);
return $self;
}
sub setup {
my $self = shift;
- $self->{restore}->{enviroment} = {%ENV};
+ $self->{restore}->{environment} = {%ENV};
binmode( $self->stdin );
if ( $self->request->content_length ) {
- syswrite( $self->stdin, $self->request->content )
+ $self->stdin->print($self->request->content)
or croak("Can't write request content to stdin handle: $!");
- sysseek( $self->stdin, 0, SEEK_SET )
+ $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 )
+ open( $self->{restore}->{stdin}, '<&'. STDIN->fileno )
or croak("Can't dup stdin: $!");
- open( STDIN, '<&=', $self->stdin->fileno )
+ open( STDIN, '<&='. $self->stdin->fileno )
or croak("Can't open stdin: $!");
binmode( STDIN );
if ( $self->stdout ) {
- open( $self->{restore}->{stdout}, '>&', STDOUT->fileno )
+ open( $self->{restore}->{stdout}, '>&'. STDOUT->fileno )
or croak("Can't dup stdout: $!");
- open( STDOUT, '>&=', $self->stdout->fileno )
+ open( STDOUT, '>&='. $self->stdout->fileno )
or croak("Can't open stdout: $!");
binmode( $self->stdout );
if ( $self->stderr ) {
- open( $self->{restore}->{stderr}, '>&', STDERR->fileno )
+ open( $self->{restore}->{stderr}, '>&'. STDERR->fileno )
or croak("Can't dup stderr: $!");
- open( STDERR, '>&=', $self->stderr->fileno )
+ open( STDERR, '>&='. $self->stderr->fileno )
or croak("Can't open stderr: $!");
binmode( $self->stderr );
{
no warnings 'uninitialized';
- %ENV = %{ $self->enviroment };
+ %ENV = %{ $self->environment };
}
-
+
if ( $INC{'CGI.pm'} ) {
CGI::initialize_globals();
- }
+ }
$self->{setuped}++;
return undef unless $self->stdout;
- require HTTP::Response;
-
seek( $self->stdout, 0, SEEK_SET )
or croak("Can't seek stdout handle: $!");
- my $message;
+ my $headers;
while ( my $line = $self->stdout->getline ) {
- $message .= $line;
- last if $message =~ /\x0d?\x0a\x0d?\x0a$/;
+ $headers .= $line;
+ last if $headers =~ /\x0d?\x0a\x0d?\x0a$/;
}
-
- unless ( $message =~ /^HTTP/ ) {
- $message = "HTTP/1.1 200 OK\x0d\x0a" . $message;
+
+ unless ( defined $headers ) {
+ $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a";
}
- my $response = HTTP::Response->new;
- my @headers = split( /\x0d?\x0a/, $message );
- my $status = shift(@headers);
-
- unless ( $status =~ s/^(HTTP\/\d\.\d) (\d{3}) (.*)$// ) {
- croak( "Invalid Status-Line: '$status'" );
+ unless ( $headers =~ /^HTTP/ ) {
+ $headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers;
}
- $response->protocol($1);
- $response->code($2);
- $response->message($3);
+ my $response = HTTP::Response->parse($headers);
+ $response->date( time() ) unless $response->date;
- my $token = qr/[^][\x00-\x1f\x7f()<>@,;:\\"\/?={} \t]+/;
+ my $message = $response->message;
+ my $status = $response->header('Status');
- foreach my $header (@headers) {
+ if ( $message && $message =~ /^(.+)\x0d$/ ) {
+ $response->message($1);
+ }
- unless( $header =~ s/^($token):[\t ]*// ) {
- croak( "Invalid header field name : '$header'" );
- }
+ if ( $status && $status =~ /^(\d\d\d)\s?(.+)?$/ ) {
- $response->push_header( $1 => $header );
- }
+ my $code = $1;
+ my $message = $2 || HTTP::Status::status_message($code);
- if ( my $code = $response->header('Status') ) {
$response->code($code);
- $response->message( HTTP::Status::status_message($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');
- $response->headers->date( time() );
+ return $response;
+ }
if ($callback) {
+
+ my $handle = $self->stdout;
+
$response->content( sub {
- if ( $self->stdout->read( my $buffer, 4096 ) ) {
+
+ 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);
}
sub restore {
my $self = shift;
-
+
{
no warnings 'uninitialized';
- %ENV = %{ $self->{restore}->{enviroment} };
+ %ENV = %{ $self->{restore}->{environment} };
}
- open( STDIN, '>&', $self->{restore}->{stdin} )
+ open( STDIN, '<&'. fileno($self->{restore}->{stdin}) )
or croak("Can't restore stdin: $!");
sysseek( $self->stdin, 0, SEEK_SET )
STDOUT->flush
or croak("Can't flush stdout: $!");
- open( STDOUT, '>&', $self->{restore}->{stdout} )
+ open( STDOUT, '>&'. fileno($self->{restore}->{stdout}) )
or croak("Can't restore stdout: $!");
sysseek( $self->stdout, 0, SEEK_SET )
STDERR->flush
or croak("Can't flush stderr: $!");
- open( STDERR, '>&', $self->{restore}->{stderr} )
+ open( STDERR, '>&'. fileno($self->{restore}->{stderr}) )
or croak("Can't restore stderr: $!");
sysseek( $self->stderr, 0, SEEK_SET )
__END__
-=head1 NAME
-
-HTTP::Request::AsCGI - Setup a CGI enviroment from a HTTP::Request
-
=head1 SYNOPSIS
use CGI;
$stdout = $c->stdout;
- # enviroment and descriptors will automatically be restored
+ # environment and descriptors will automatically be restored
# when $c is destructed.
}
=head1 DESCRIPTION
-Provides a convinient way of setting up an CGI enviroment from a HTTP::Request.
+Provides a convenient way of setting up an CGI environment from an HTTP::Request.
=head1 METHODS
=item new ( $request [, key => value ] )
-Contructor, first argument must be a instance of HTTP::Request
-followed by optional pairs of environment key and value.
+Constructor. The first argument must be a instance of HTTP::Request, followed
+by optional pairs of environment key and value.
-=item enviroment
+=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
-Setups the environment and descriptors.
+Sets up the environment and descriptors.
=item restore
-Restores the enviroment and descriptors. Can only be called after setup.
+Restores the environment and descriptors. Can only be called after setup.
=item request
Thomas L. Shinnick for his valuable win32 testing.
-=head1 AUTHOR
-
-Christian Hansen, C<ch@ngmedia.com>
-
-=head1 LICENSE
-
-This library is free software. You can redistribute it and/or modify
-it under the same terms as perl itself.
-
=cut