Added a forking test
[catagits/HTTP-Request-AsCGI.git] / lib / HTTP / Request / AsCGI.pm
index e22f275..cd7394f 100644 (file)
@@ -11,19 +11,20 @@ use IO::File;
 
 __PACKAGE__->mk_accessors(qw[ enviroment request stdin stdout stderr ]);
 
-our $VERSION = 0.1;
+our $VERSION = 0.2;
 
 sub new {
     my $class   = shift;
     my $request = shift;
-
-    my $self = {
-        request  => $request,
-        restored => 0,
-        setuped  => 0,
-        stdin    => IO::File->new_tmpfile,
-        stdout   => IO::File->new_tmpfile
-    };
+    
+    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;
@@ -31,8 +32,10 @@ sub new {
     $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;
 
-    $self->{enviroment} = {
+    my $enviroment = {
         GATEWAY_INTERFACE => 'CGI/1.1',
         HTTP_HOST         => $uri->host_port,
         HTTPS             => ( $uri->scheme eq 'https' ) ? 'ON' : 'OFF',  # not in RFC 3875
@@ -46,23 +49,30 @@ sub new {
         REMOTE_ADDR       => '127.0.0.1',
         REMOTE_HOST       => 'localhost',
         REMOTE_PORT       => int( rand(64000) + 1000 ),                   # not in RFC 3875
-        REQUEST_URI       => $uri->path_query || '/',                     # not in RFC 3875
+        REQUEST_URI       => $uri->path_query,                            # not in RFC 3875
         REQUEST_METHOD    => $request->method,
         @_
     };
 
     foreach my $field ( $request->headers->header_field_names ) {
 
-        my $key = uc($field);
+        my $key = uc("HTTP_$field");
         $key =~ tr/-/_/;
-        $key = 'HTTP_' . $key unless $field =~ /^Content-(Length|Type)$/;
+        $key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/;
 
-        unless ( exists $self->{enviroment}->{$key} ) {
-            $self->{enviroment}->{$key} = $request->headers->header($field);
+        unless ( exists $enviroment->{$key} ) {
+            $enviroment->{$key} = $request->headers->header($field);
         }
     }
 
-    return $class->SUPER::new($self);
+    unless ( $enviroment->{SCRIPT_NAME} eq '/' && $enviroment->{PATH_INFO} ) {
+        $enviroment->{PATH_INFO} =~ s/^\Q$enviroment->{SCRIPT_NAME}\E/\//;
+        $enviroment->{PATH_INFO} =~ s/^\/+/\//;
+    }
+
+    $self->enviroment($enviroment);
+
+    return $self;
 }
 
 sub setup {
@@ -70,14 +80,7 @@ sub setup {
 
     $self->{restore}->{enviroment} = {%ENV};
 
-    open( $self->{restore}->{stdin}, '>&', STDIN->fileno )
-      or croak("Can't dup stdin: $!");
-
-    open( STDIN, '<&=', $self->stdin->fileno )
-      or croak("Can't open stdin: $!");
-
     binmode( $self->stdin );
-    binmode( STDIN );
 
     if ( $self->request->content_length ) {
 
@@ -88,6 +91,14 @@ sub setup {
           or croak("Can't seek 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 )
@@ -129,9 +140,7 @@ sub setup {
 sub response {
     my ( $self, $callback ) = @_;
 
-    return undef unless $self->{setuped};
-    return undef unless $self->{restored};
-    return undef unless $self->{restore}->{stdout};
+    return undef unless $self->stdout;
 
     require HTTP::Response;
 
@@ -274,7 +283,8 @@ HTTP::Request::AsCGI - Setup a CGI enviroment from a HTTP::Request
         
         $stdout = $c->stdout;
         
-        # enviroment and descriptors will automatically be restored when $c is destructed.
+        # enviroment and descriptors will automatically be restored 
+        # when $c is destructed.
     }
     
     while ( my $line = $stdout->getline ) {
@@ -283,33 +293,68 @@ HTTP::Request::AsCGI - Setup a CGI enviroment from a HTTP::Request
     
 =head1 DESCRIPTION
 
+Provides a convinient way of setting up an CGI enviroment from a HTTP::Request.
+
 =head1 METHODS
 
 =over 4 
 
-=item new
+=item new ( $request [, key => value ] )
+
+Contructor, first argument must be a instance of HTTP::Request
+followed by optional pairs of environment key and value.
 
 =item enviroment
 
+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.
+
 =item restore
 
+Restores the enviroment 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 BUGS
+=head1 SEE ALSO
+
+=over 4
+
+=item examples directory in this distribution.
+
+=item L<WWW::Mechanize::CGI>
+
+=item L<Test::WWW::Mechanize::CGI>
+
+=back
 
-=item THANKS TO
+=head1 THANKS TO
 
 Thomas L. Shinnick for his valuable win32 testing.